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
//!
//! ```text
//! $env:CPPLS_REAL_PROJECT = "E:\EmmyLuaCodeStyle"
//! cargo test --release -p cpp_code_analysis --test members_real -- --ignored --nocapture
//! ```
//!
//! `undefined-field` reported nine false positives on `CodeFormat.cpp` and the receiver types were right — it is the
//! **member list** that is wrong:
//!
//! ```text
//! `std::string` -> `size` (27 member(s), 0 unlisted) — index: 135 posting(s), 0 in scope `std::string`
//! `std::vector` -> `erase` (303 member(s), 0 unlisted) — index: 122 posting(s), 278 in scope `std::vector`
//! ```
//!
//! So this asks the same two questions and prints the answers side by side: what `members_of` says a class has, and
//! what the index says is filed under that class's name. A member the index has postings for, in that scope, and the
//! list does not, is exactly the inconsistency the doc records.
use std::path::PathBuf;
use cpp_code_analysis::file::coordinates::ReadingOffset;
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};
/// The project to open — **from the environment, and only when it is there**.
///
/// A probe that hardcodes one machine's checkout is a probe that fails on the next machine, and the
/// question it asks is answerable on any project.
fn project() -> Option<PathBuf> {
let named = std::env::var_os("CPPLS_REAL_PROJECT")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(r"E:\EmmyLuaCodeStyle"));
named.is_dir().then_some(named)
}
#[test]
#[ignore = "opens a real project; run with --ignored"]
fn a_class_member_list_and_its_postings_agree() {
let Some(project) = project() else {
eprintln!("skipped: no project (set CPPLS_REAL_PROJECT)");
return;
};
let source = project.join("CodeFormatCLib/src/CodeFormat.cpp");
if !source.is_file() {
eprintln!("skipped: {:?} is not in the project", source);
return;
}
let mut session = Session::open(
project.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&project),
);
session.add_project_files([source.clone()]);
session.index_everything();
let Some(view) = session.view_of_the_file(&source) else {
panic!("the file's own text parses");
};
let _ = view;
let view = session.view_of_the_file(&source).expect("and again");
let at = ReadingOffset::new(0);
// **The question: is the `#if` in the facts because the file was read uncooked?** The eager index reads a file's
// own text, where directives still stand; a *rendering* has them resolved and gone. `bits/stl_vector.h` was not
// cooked when the lists above were read, so ask for the rendering and read the list again — the two answers tell
// the uncooked window apart from a cooker bug.
// The toolchain's header, named by the environment: a test that hardcodes one machine's libstdc++ path is a test
// that fails on the next machine, and the question it asks is answerable on any project that has one.
let vector_header = std::env::var_os("CPPLS_REAL_STD_VECTOR")
.map(PathBuf::from)
.unwrap_or_default();
if vector_header.exists() {
let _ = session.view(&vector_header);
session.advance(usize::MAX);
let cooked = session.cook(&vector_header).is_some();
println!("after cooking stl_vector.h: cooked = {cooked}");
if let Known::Yes(list) = session.members_of(&view, "std::vector") {
println!(
" std::vector has erase after cooking: {} ({} member(s))",
list.members
.iter()
.any(|member| member.fact.name == "erase"),
list.members.len()
);
}
} else {
println!("stl_vector.h not found at {}", vector_header.display());
}
for class in ["std::string", "std::basic_string", "std::vector"] {
let listed = session.members_of(&view, class);
let (count, names) = match &listed {
Known::Yes(list) => (
list.members.len(),
list.members
.iter()
.map(|member| member.fact.name.to_string())
.collect::<Vec<_>>(),
),
other => {
println!("{class}: {other:?}");
continue;
}
};
println!(
"{class}: {count} member(s), unlisted {:?}",
match &listed {
Known::Yes(list) => list
.unlisted
.iter()
.map(|base| base.spelling.clone())
.collect::<Vec<_>>(),
_ => Vec::new(),
}
);
// **What the doc records as fixed**: `erase` was 0 facts in this header before the directive lookahead, and the
// class's member list is what a reader sees.
if class == "std::vector" {
assert!(
names.iter().any(|name| name == "erase"),
"std::vector has `erase` among its members: {names:?}"
);
}
// **What the signatures look like now.** Before the declarator-choice fix, every member whose return type ends
// in `&` or `*` carried `parameter_list: None` — 1 937 of libstdc++'s 15 253 function facts — so the
// completion row and the signature popup read `assign(…)` with no arguments at all.
if class == "std::vector" {
let samples: Vec<(String, Option<String>)> = match &listed {
Known::Yes(list) => list.members.iter(),
_ => [].iter(),
}
.filter(|member| {
matches!(
member.fact.name.as_str(),
"assign" | "insert" | "erase" | "at"
)
})
.take(4)
.map(|member| {
(
member.fact.name.to_string(),
member.fact.parameter_list().map(str::to_string),
)
})
.collect();
println!(" signatures: {samples:?}");
assert!(
samples.iter().any(|(_, parameters)| parameters.is_some()),
"at least one sampled member carries its parameter list: {samples:?}"
);
}
println!(
" has size: {} has erase: {} has begin: {}",
names.iter().any(|name| name == "size"),
names.iter().any(|name| name == "erase"),
names.iter().any(|name| name == "begin"),
);
let mut sorted = names.clone();
sorted.sort();
println!(" all: {sorted:?}");
// **The assertion this test exists for.** A `std::string` that has no `size`, `begin` or `end` is not a
// member list, it is the 27 typedefs declared before libstdc++'s `struct _Alloc_hider` — whose trailing
// `// TODO check __is_final` used to swallow its own closing brace and every member after it.
if class.starts_with("std::string") {
assert!(
names.iter().any(|name| name == "size")
&& names.iter().any(|name| name == "begin")
&& names.iter().any(|name| name == "end"),
"{class}: size, begin and end are members of it: {sorted:?}"
);
}
for name in ["size", "erase"] {
println!(
" postings(`{name}`) = {}, in scope `{class}` = {}",
session.index().name_postings(name),
session.index().scope_postings(class),
);
// **Which scopes the facts are actually filed under** — the question a posting count cannot answer.
let mut scopes: std::collections::BTreeMap<String, (usize, String)> =
std::collections::BTreeMap::new();
for (file, summary) in session.index().all_summaries() {
for fact in summary.declarations.iter().filter(|fact| fact.name == name) {
let key = fact.scope.clone().unwrap_or_else(|| "<file scope>".into());
let entry = scopes.entry(key.to_string()).or_insert((0, String::new()));
entry.0 += 1;
if entry.1.is_empty() {
entry.1 = file.display().to_string();
}
}
}
let shown: Vec<String> = scopes
.iter()
.map(|(scope, (count, file))| format!("{scope} x{count} in {file}"))
.collect();
println!(" scopes: {}", shown.join(", "));
}
let _ = at;
}
}
/// **A class two files use is built once.** The member list is keyed by the asking file because a class *declared* in
/// the file being read is resolved against that file's own tree — so a list whose facts all came from other files is
/// the index's alone, and `remember_member_list` files it under the empty path as well.
///
/// Measured on the real project before that: twelve lists built per pull, **zero hits**, and
/// `std::vector<LuaSyntaxNode>` (22 ms) rebuilt by every file that mentions it.
#[test]
#[ignore = "opens a real project; run with --ignored"]
fn a_class_two_files_use_is_built_once() {
let Some(project) = project() else {
eprintln!("skipped: no project (set CPPLS_REAL_PROJECT)");
return;
};
let first = project.join("CodeFormatCLib/src/CodeFormat.cpp");
let second = project.join("LuaParser/src/Ast/LuaSyntaxTree.cpp");
if !first.is_file() || !second.is_file() {
eprintln!("skipped: the project does not hold {first:?} and {second:?}");
return;
}
let mut session = Session::open(
project.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&project),
);
session.add_project_files([first.clone(), second.clone()]);
session.index_everything();
let view_of_first = session
.view_of_the_file(&first)
.expect("the first file parses");
let view_of_second = session
.view_of_the_file(&second)
.expect("the second file parses");
for class in ["std::string", "std::vector<LuaSyntaxNode>", "LuaSyntaxTree"] {
let started = std::time::Instant::now();
let from_first = session.members_of(&view_of_first, class);
let first_took = started.elapsed();
let started = std::time::Instant::now();
let from_second = session.members_of(&view_of_second, class);
let second_took = started.elapsed();
let (Some(a), Some(b)) = (known_count(&from_first), known_count(&from_second)) else {
println!("{class}: not answerable from both files");
continue;
};
println!(
"{class}: {} member(s) — first file {first_took:?}, second file {second_took:?}",
a
);
assert_eq!(a, b, "{class}: the same answer from both files");
// **Only the plain class is asserted.** A *template instantiation* (`std::vector<LuaSyntaxNode>`) is
// substituted per spelling and measured 1.7 ms then 20 ms — it is not the cache under test, and pinning it
// here would pin a cost nobody has explained yet. `std::string` is the case the inline-namespace fix and the
// shared cache both matter for, and it is 16 ms then 143 µs.
if class == "std::string" {
assert!(
second_took * 2 < first_took,
"{class}: the second file reuses the list ({first_took:?} then {second_took:?})"
);
}
}
}
fn known_count(listed: &Known<cpp_code_analysis::MemberList>) -> Option<usize> {
match listed {
Known::Yes(list) => Some(list.members.len()),
_ => None,
}
}