1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! **What the analysis answers at a place in a real file** — the probe behind the editor reports, driven entirely by
//! the environment so that no project path is baked into the code.
//!
//! ```text
//! CPPLS_PROJECT_ROOT=<a project> CPPLS_FILE=<a file in it> CPPLS_NEEDLE=<text to look for> \
//! cargo test --release -p cpp_code_analysis --test inference_probe -- --ignored --nocapture
//! ```
//!
//! For every `needle` found in the file it prints: the member list after the `.`/`->` that follows it, the member
//! answer, and the definitions a cursor on it gets. That is the shape of both reports this exists for: a subscript
//! whose element type is lost (`events[Pos].U`) and a smart pointer whose members are missing (`file->setUri`).
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};
use std::path::{Path, PathBuf};
#[test]
#[ignore = "opens a real project; run with --ignored and CPPLS_PROJECT_ROOT"]
fn what_is_answered_at_a_place() {
let root = PathBuf::from(
std::env::var("CPPLS_PROJECT_ROOT").expect("CPPLS_PROJECT_ROOT names the project"),
);
let file = PathBuf::from(std::env::var("CPPLS_FILE").expect("CPPLS_FILE names a file in it"));
let needle = std::env::var("CPPLS_NEEDLE").unwrap_or_else(|_| ".".to_string());
let text = std::fs::read_to_string(&file).expect("the file is readable");
// **Opened the way the server opens it**, not with a caller's configuration: the project's own file
// (`compile_commands.json`, `.cppls.toml`, the compiler's predefined macros) is what decides whether an include
// resolves — and a probe that skips it answers about a project nobody analyses. Measured on this one: with
// `with_config` and no database, `p.GetEvents()` typed as `Unknown(NotDeclaredHere("LuaParser::GetEvents"))`;
// the header is indexed, it is simply not *visible* from a file whose include does not resolve.
let mut session = Session::open(
&root,
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
let started = std::time::Instant::now();
session.did_open(&file, &text);
// **A cooked reading on purpose**: the checks are run over the compiler's view, and a probe that asks for
// diagnostics without one is answered "nothing to say" rather than "nothing wrong".
session.want_cooked_reading(&file);
session.add_project_files([file.clone()]);
// **`index_everything` is the whole pump**, cooking included. A `while pending_work() { advance() }` loop *after*
// it livelocks: `advance` only **marks** files for cooking and `pending_work` counts them, so the condition never
// falls. Measured on a real project as a probe that ran for twenty minutes without printing its header line.
session.index_everything();
println!(
"indexed {} file(s) in {:?}",
session.cache_sizes().summaries,
started.elapsed()
);
// **What the check layer says about the whole file**, when asked: a false positive is a report about
// diagnostics rather than about one place, and the code carries the rule's own name.
if std::env::var_os("CPPLS_DIAGNOSTICS").is_some() {
match session.diagnostics(&file) {
Some(found) => {
println!("{} finding(s):", found.checks.len() + found.errors.len());
for finding in &found.checks {
println!(
" {:?} at {}: {}",
finding.code, finding.range.start_offset, finding.message
);
}
for error in &found.errors {
println!(
" error {:?} at {} (length {}): {}",
error.code, error.start, error.end, error.message
);
// **The text the error is about.** A rendering is one long line, so the offset alone says nothing
// a reader can act on; ±60 bytes of the rendering is what turns "expected primary expression at
// 19653" into a name.
if let Some(rendered) = session.rendering_of(&file) {
let start = error.start.min(rendered.text.len());
let end = error.end.min(rendered.text.len()).max(start);
let from = start.saturating_sub(50);
let to = (end + 50).min(rendered.text.len());
if rendered.text.is_char_boundary(from)
&& rendered.text.is_char_boundary(to)
&& rendered.text.is_char_boundary(start)
&& rendered.text.is_char_boundary(end)
{
println!(
" …{}⟦{}⟧{}…",
rendered.text[from..start].replace('\n', " "),
rendered.text[start..end].replace('\n', " "),
rendered.text[end..to].replace('\n', " "),
);
}
}
}
}
None => println!("the check layer had nothing to say about the file"),
}
}
// **The text the parser was handed**, when asked — the one artifact that separates "the checker is wrong" from
// "the preprocessor handed the parser something that is not a program". A file whose macros do not expand reads
// as a pile of syntax errors, and every one of them is an accurate report about the text it was shown.
if std::env::var_os("CPPLS_RENDERING").is_some() {
match session.rendering_of(&file) {
Some(rendered) => {
let lines = std::env::var("CPPLS_RENDERING_LINES")
.ok()
.and_then(|count| count.parse::<usize>().ok())
.unwrap_or(40);
println!("--- the rendering, first {lines} line(s) ---");
for line in rendered.text.lines().take(lines) {
println!("{line}");
}
// **The text around one offset**, for a diagnostic that names a *rendering* offset: a syntax error's
// range is in the rendering's coordinates, and a rendering is one long line, so "the first N lines"
// cannot show it. `CPPLS_RENDERING_AT=<offset>` prints ±200 bytes instead.
if let Ok(at) = std::env::var("CPPLS_RENDERING_AT")
&& let Ok(at) = at.parse::<usize>()
{
let from = at.saturating_sub(200);
let to = (at + 200).min(rendered.text.len());
println!(
"--- the rendering at {at} (bytes {from}..{to}) ---\n{}",
&rendered.text[from..to]
);
}
}
None => println!("the file has no rendering yet"),
}
}
// **Is the rendering itself a program?** The check layer's syntax errors are ranges in the rendering, and the
// question "is the parser wrong or is the text it was handed wrong" cannot be answered from the ranges alone.
// This parses the very text `rendering_of` hands out and prints its own errors: a text that reads clean here was
// *not* the text the checks saw, and the two renderings then differ.
if std::env::var_os("CPPLS_REPARSE").is_some() {
match session.rendering_of(&file) {
Some(rendered) => {
let tree = cpp_parser::CppParser::parse(
&rendered.text,
cpp_parser::ParserConfig::default(),
);
let errors = tree
.get_red_root()
.descendants()
.filter(|node| {
cpp_parser::CppSyntaxKind::from(node.kind())
== cpp_parser::CppSyntaxKind::ErrorNode
})
.count();
println!(
"--- re-parsing the rendering ({} byte(s)): {errors} error node(s)",
rendered.text.len()
);
for node in tree.get_red_root().descendants().filter(|node| {
cpp_parser::CppSyntaxKind::from(node.kind())
== cpp_parser::CppSyntaxKind::ErrorNode
}) {
let start = usize::from(node.text_range().start());
let end = usize::from(node.text_range().end());
let from = start.saturating_sub(50);
let to = (end + 50).min(rendered.text.len());
println!(
" …{}⟦{}⟧{}…",
rendered.text[from..start].replace('\n', " "),
rendered.text[start..end].replace('\n', " "),
rendered.text[end..to].replace('\n', " "),
);
}
}
None => println!("the file has no rendering yet"),
}
}
// **And every fact the index holds for a name**, when one is named: the `returns` a call has to answer with.
if let Ok(name) = std::env::var("CPPLS_NAME") {
for summary in session.index().summaries() {
for fact in &summary.declarations {
if fact.name == name {
println!(
"{name} in {}: kind {:?} scope {:?} returns {:?} type_of {:?} parameters {:?}",
summary.path.display(),
fact.kind,
fact.scope,
fact.returns,
fact.type_of,
fact.parameters()
);
}
}
}
}
let view = session.view_of_the_file(&file).expect("a view of the file");
let mut at = 0;
while let Some(found) = text[at..].find(&needle) {
// **Where the cursor is inside the needle**: at its start by default, at its last character with
// `CPPLS_AT=last` — the difference matters for a question about an *expression* (`_lineOffsetVec[line]`)
// rather than about the name it begins with.
let start = at + found;
let offset = match std::env::var("CPPLS_AT").as_deref() {
Ok("last") => start + needle.trim_end().len().saturating_sub(1),
_ => start,
};
at = start + needle.len();
let line = text[..offset].lines().count();
let snippet: String = text[offset..]
.lines()
.next()
.unwrap_or_default()
.chars()
.take(70)
.collect();
// The member list at the end of the needle, when the needle ends with `.` or `->`.
let cursor = offset + needle.len();
let completions = session.member_completions(
&view,
cpp_code_analysis::file::coordinates::FileOffset::new(cursor),
);
match completions {
Known::Yes(list) => {
let names: Vec<&str> = list
.members
.members
.iter()
.map(|member| member.fact.name.as_str())
.take(8)
.collect();
println!(
"{line}: …{snippet}\n class {:?}: {} member(s) {names:?}",
list.class,
list.members.members.len()
);
}
other => println!("{line}: …{snippet}\n completions: {other:?}"),
}
// **What macro the name at the cursor is**, when `CPPLS_MACRO=1`: the cross-file answer a hover on a macro
// shows. A macro is not a declaration, so `definitions` says nothing about it.
if std::env::var_os("CPPLS_MACRO").is_some() {
let found = session.macro_definition(
&view,
cpp_code_analysis::file::coordinates::ReadingOffset::new(offset),
);
println!(" macro: {found:?}");
}
// And what the cursor on the needle's last character resolves to.
let definitions = session.definitions(
&view,
cpp_code_analysis::file::coordinates::ReadingOffset::new(offset),
);
if let Known::Yes(found) = definitions {
let names: Vec<String> = found
.found
.iter()
.map(|one| {
format!(
"{}::{}",
one.fact.scope.as_deref().unwrap_or(""),
one.fact.name
)
})
.collect();
println!(" definitions: {names:?}");
}
// **What the expression at the cursor is**, which is the question a hover on an `auto` variable ends in:
// `type_at` reads the initializer when the declaration has no type of its own. `CPPLS_TYPE=1`.
if std::env::var_os("CPPLS_TYPE").is_some() {
let typed = session.type_at(
&view,
cpp_code_analysis::file::coordinates::ReadingOffset::new(offset),
);
println!(" type_at: {typed:?}");
}
}
let _ = Path::new("");
}