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
//! **One class, asked what it holds** — the probe the anonymous-`struct` question needs.
//!
//! ```text
//! $env:CPPLS_PROJECT_ROOT = "E:\neovim"
//! $env:CPPLS_FILE = "E:\neovim\src\nvim\api\private\helpers.c" # any file of the project
//! $env:CPPLS_CLASS = "Object"
//! cargo test --release -p cpp_code_analysis --test members_probe -- --ignored --nocapture
//! ```
//!
//! # Why this exists next to `resolution_rate`
//!
//! The scoreboard answers "how many member questions resolve" over a whole project and takes minutes. When one of its
//! buckets names a class — "a list from **no visible declaration**" was 21 539 of 40 000 refusals on `neovim` — the next
//! question is about that class, and re-running the project to ask it is the wrong shape. This asks it in one index.
//!
//! What it prints is the four answers that separate the causes:
//!
//! ```text
//! the facts filed under the spelling (scope postings)
//! what `members_of` answers, and its size
//! how many declarations are *visible* from the asking file
//! what the class's own declaration says its type is (the `typedef struct { … } Name;` shape)
//! ```
use std::path::PathBuf;
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};
#[test]
#[ignore = "opens a real project; run with --ignored"]
fn what_one_class_answers_for_its_members() {
let (Ok(root), Ok(file), Ok(asked)) = (
std::env::var("CPPLS_PROJECT_ROOT"),
std::env::var("CPPLS_FILE"),
std::env::var("CPPLS_CLASS"),
) else {
// The question it asks is answerable on any project, and a checkout hardcoded here would be
// one machine's.
eprintln!("skipped: set CPPLS_PROJECT_ROOT, CPPLS_FILE and CPPLS_CLASS");
return;
};
let (root, file) = (PathBuf::from(root), PathBuf::from(file));
let mut session = Session::open(
&root,
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
let started = std::time::Instant::now();
session.add_project_files([file.clone()]);
session.index_everything();
while session.pending_work() > 0 {
session.advance(64);
}
println!(
"indexed {} file(s) in {:?}",
session.cache_sizes().summaries,
started.elapsed()
);
let view = session
.view_of_the_file(&file)
.expect("a view of the asking file");
println!(
"\n{asked}: {} fact(s) filed under that scope, {} visible from {}",
session.index().scope_postings(&asked),
session.index().declarations_in(&asked, &file).len(),
file.display()
);
// **The class's own declaration**, which is where a `typedef struct { … } Name;` keeps its answer: the members of
// an anonymous struct are declared in a scope with no name, and the typedef is the only thing that names them.
for (where_it_is, fact) in session.index().every_fact_named(&asked) {
println!(
" declared in {}: kind {:?} scope {:?} type_of {:?}",
where_it_is
.file_name()
.unwrap_or_default()
.to_string_lossy(),
fact.kind,
fact.scope,
fact.type_of
);
}
match session.members_of(&view, &asked) {
Known::Yes(list) => {
println!("members_of: {} member(s)", list.members.len());
for member in list.members.iter().take(30) {
println!(
" {:?} {:?} scope {:?} type {:?} returns {:?} parameters {:?}",
member.fact.kind,
member.fact.name,
member.fact.scope,
member.fact.type_of,
member.fact.returns,
member.fact.parameters()
);
}
}
Known::Unknown(reason) => println!("members_of: no list, {reason:?}"),
Known::No => println!("members_of: no list"),
}
}