cpp_code_analysis 0.1.4

The analysis layer of cpp_ls: the preprocessor, symbol scopes, type inference and the semantic checks.
Documentation
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! **A measurement of the C++ corpus** — one line per file, one line per member question, one line per type question.
//!
//! ```text
//! cargo test --release -p cpp_code_analysis --test cpp_corpus -- --ignored --nocapture
//! $env:CPPLS_CPP_CORPUS = "C:\Users\zc\Desktop\test_cpp"   # the default
//! ```
//!
//! The corpus is a real project: `CMakeLists.txt` builds it with MinGW's g++ 15 in C++23 mode, and
//! `build/compile_commands.json` carries `-std=c++23`. Every construct in it is one the compiler accepted, which is
//! what makes a diagnostic here a defect of the analysis rather than of the file.
//!
//! # The three questions
//!
//! ```text
//! parse     does each file read without a syntax error, and how many declarations did it produce?
//! members   for each `x.member` / `x->member` a reader is likely to ask about, is `member` in the list the editor shows?
//! types     for each declaration, is the declared name's type the one the file wrote?
//! ```

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use cpp_code_analysis::file::coordinates::FileOffset;
use cpp_code_analysis::{DiskFiles, Known, OpenDocuments, Session, SessionFiles, WatchFilter};

fn corpus_root() -> Option<PathBuf> {
    let named = std::env::var_os("CPPLS_CPP_CORPUS")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from(r"C:\Users\zc\Desktop\test_cpp"));
    named.join("CMakeLists.txt").is_file().then_some(named)
}

/// **One member question**: the file, the expression whose operator the cursor goes after, and the member expected.
struct MemberRow {
    file: &'static str,
    needle: &'static str,
    expected: &'static str,
}

/// **One type question**: the file, a piece of text that contains the declared name, the name itself, and what the
/// type is expected to contain.
struct TypeRow {
    file: &'static str,
    needle: &'static str,
    name: &'static str,
    expected: &'static str,
}

const MEMBERS: &[MemberRow] = &[
    // A class hierarchy's own members, through an object and through a pointer.
    MemberRow {
        file: "src/classes.cpp",
        needle: "derived.name()",
        expected: "name",
    },
    MemberRow {
        file: "src/classes.cpp",
        needle: "clone->describe()",
        expected: "describe",
    },
    MemberRow {
        file: "src/smart_pointers.cpp",
        needle: "unique->identifier()",
        expected: "identifier",
    },
    // A standard container, a container adaptor's members, and a concept-constrained class.
    MemberRow {
        file: "src/containers.cpp",
        needle: "vector.push_back(4)",
        expected: "push_back",
    },
    MemberRow {
        file: "src/containers.cpp",
        needle: "deque.push_front(0)",
        expected: "push_front",
    },
    MemberRow {
        file: "src/concepts.cpp",
        needle: "accumulator.add(3)",
        expected: "add",
    },
    // A coroutine handle's own member, and a template instantiation's member.
    MemberRow {
        file: "src/coroutines.cpp",
        needle: "task.run()",
        expected: "run",
    },
    MemberRow {
        file: "src/templates.cpp",
        needle: "slot.get()",
        expected: "get",
    },
];

const TYPES: &[TypeRow] = &[
    TypeRow {
        file: "src/classes.cpp",
        needle: "Derived derived(5",
        name: "derived",
        expected: "Derived",
    },
    TypeRow {
        file: "src/templates.cpp",
        needle: "Box<int> slot",
        name: "slot",
        expected: "Box<int>",
    },
    TypeRow {
        file: "src/coroutines.cpp",
        needle: "auto task = doubled()",
        name: "task",
        expected: "Task<int>",
    },
    TypeRow {
        file: "src/concepts.cpp",
        needle: "Accumulator<int> accumulator",
        name: "accumulator",
        expected: "Accumulator<int>",
    },
];

fn corpus_files(root: &Path) -> Vec<PathBuf> {
    let opened = Session::open(
        root.to_path_buf(),
        SessionFiles::new(OpenDocuments::new(), DiskFiles),
        WatchFilter::new(root),
    );
    let mut files: Vec<PathBuf> = opened
        .project_files()
        .iter()
        .filter(|path| {
            matches!(
                path.extension().and_then(|extension| extension.to_str()),
                Some("cpp" | "h")
            )
        })
        .filter(|path| !path.components().any(|part| part.as_os_str() == "build"))
        .cloned()
        .collect();
    files.sort();
    files
}

fn name_of(path: &Path) -> String {
    path.strip_prefix(path.ancestors().nth(2).unwrap_or(path))
        .unwrap_or(path)
        .display()
        .to_string()
        .replace('\\', "/")
}

fn member_labels(session: &Session<DiskFiles>, path: &Path, cursor: usize) -> Vec<String> {
    let Some(view) = session.view(path) else {
        return Vec::new();
    };
    session
        .completions(&view, FileOffset::new(cursor))
        .items
        .into_iter()
        .map(|item| item.label)
        .collect()
}

fn declared_type(session: &Session<DiskFiles>, path: &Path, cursor: usize) -> Option<String> {
    let view = session.view(path)?;
    let reading = view.reading_offset_of(FileOffset::new(cursor))?;
    match session.type_at(&view, reading) {
        Known::Yes(found) => Some(found.type_of.to_string()),
        Known::Unknown(reason) => Some(format!("<unknown: {}>", reason.describe())),
        Known::No => None,
    }
}

#[test]
#[ignore = "opens a real project; run with --ignored"]
fn the_cpp_corpus_parses_and_its_members_resolve() {
    let Some(root) = corpus_root() else {
        eprintln!("skipped: no corpus (set CPPLS_CPP_CORPUS to the test_cpp project)");
        return;
    };
    let files = corpus_files(&root);
    println!(
        "\n=== the corpus: {} file(s) under {}",
        files.len(),
        root.display()
    );

    let mut session = Session::open(
        root.clone(),
        SessionFiles::new(OpenDocuments::new(), DiskFiles),
        WatchFilter::new(&root),
    );
    session.add_project_files(files.clone());
    let indexing = std::time::Instant::now();
    session.index_everything();
    let mut steps = 0usize;
    while session.pending_work() > 0 && steps < 20_000 {
        session.advance(64);
        steps += 1;
    }
    println!("indexed in {:?} ({steps} step(s))", indexing.elapsed());

    println!("\n--- parse: one line per file");
    let mut files_with_errors = 0usize;
    let mut total_errors = 0usize;
    let mut declarations = 0usize;
    for path in &files {
        let Ok(text) = std::fs::read_to_string(path) else {
            println!("  {:<30} unreadable", name_of(path));
            continue;
        };
        let started = std::time::Instant::now();
        let cooked = session.cook(path).is_some();
        let cooking = started.elapsed();
        let found = session.diagnostics(path);
        let errors = found.as_ref().map(|found| found.errors.len()).unwrap_or(0);
        let checks = found.as_ref().map(|found| found.checks.len()).unwrap_or(0);
        total_errors += errors;
        if errors > 0 {
            files_with_errors += 1;
        }
        let bound = session
            .parsed_summary(path)
            .map(|summary| summary.declarations.len())
            .unwrap_or(0);
        declarations += bound;
        println!(
            "  {:<30} {:>3} error(s)  {:>3} finding(s)  {:>5} decl  {:>5} ms  {:>4} line(s){}",
            name_of(path),
            errors,
            checks,
            bound,
            cooking.as_millis(),
            text.lines().count(),
            if cooked { "" } else { "  NOT COOKED" }
        );
        if let Some(found) = &found {
            let mut by_code: BTreeMap<&str, Vec<&cpp_code_analysis::Finding>> = BTreeMap::new();
            for check in &found.checks {
                by_code.entry(check.code.name()).or_default().push(check);
            }
            let all = std::env::var_os("CPPLS_C_ALL").is_some();
            for (code, entries) in by_code {
                println!("      [{code}] {} finding(s)", entries.len());
                let shown = if all { entries.len() } else { 3 };
                for check in entries.iter().take(shown) {
                    let from = check.range.start_offset.saturating_sub(48).min(text.len());
                    println!(
                        "        …{}«{}»…  ← {}",
                        text[from..check.range.start_offset].replace('\n', " "),
                        text[check.range.start_offset..check.range.end_offset().min(text.len())]
                            .replace('\n', " "),
                        check.message.chars().take(80).collect::<String>()
                    );
                }
            }
            for error in found.errors.iter().take(6) {
                // **The line, not a byte window.** A parse error's offset can sit on a newline or at the end of the
                // token that failed, and a window then shows nothing around it; the line the offset is on is what a
                // reader can point at.
                let at = error.start.min(text.len());
                let line = text[..at].bytes().filter(|byte| *byte == b'\n').count() + 1;
                let from = text[..at].rfind('\n').map_or(0, |newline| newline + 1);
                let to = text[at..]
                    .find('\n')
                    .map_or(text.len(), |newline| at + newline);
                println!(
                    "      parse: line {line}: {}  ← {}",
                    text[from..to].trim(),
                    error.message.chars().take(90).collect::<String>()
                );
            }
        }
    }
    println!(
        "  totals: {} file(s), {} with a syntax error, {} error(s), {} declaration(s)",
        files.len(),
        files_with_errors,
        total_errors,
        declarations
    );

    for path in &files {
        if session.view(path).is_none() && session.cook(path).is_none() {
            println!("  WARN  {} has no reading to ask about", path.display());
        }
    }

    println!("\n--- members: the completion at the position right after `.` / `->`");
    let mut member_failures = 0usize;
    for row in MEMBERS {
        let path = root.join(row.file);
        let Ok(text) = std::fs::read_to_string(&path) else {
            println!("  SKIP  {:<24} {} (unreadable)", row.file, row.needle);
            continue;
        };
        let Some(found_at) = text.find(row.needle) else {
            println!("  SKIP  {:<24} {} (not written)", row.file, row.needle);
            continue;
        };
        let operator_at = row
            .needle
            .find("->")
            .or_else(|| row.needle.find('.'))
            .expect("every member row names an operator");
        let width = if row.needle[operator_at..].starts_with("->") {
            2
        } else {
            1
        };
        let cursor = found_at + operator_at + width;

        if session.view(&path).is_none() {
            session.cook(&path);
        }
        let labels = member_labels(&session, &path, cursor);
        let pass = labels.iter().any(|label| label == row.expected);
        if !pass {
            member_failures += 1;
        }
        println!(
            "  {:<4}  {:<28} {:<26} {:>3} offered  {}",
            if pass { "PASS" } else { "FAIL" },
            row.file,
            row.needle,
            labels.len(),
            if pass {
                String::new()
            } else if labels.is_empty() {
                format!("nothing offered — `{}` is not reachable", row.expected)
            } else {
                format!(
                    "`{}` missing from {:?}",
                    row.expected,
                    labels.iter().take(10).collect::<Vec<_>>()
                )
            }
        );
    }

    println!("\n--- types: `type_at` with the cursor inside the declared name");
    let mut type_failures = 0usize;
    for row in TYPES {
        let path = root.join(row.file);
        let Ok(text) = std::fs::read_to_string(&path) else {
            println!("  SKIP  {:<24} {}", row.file, row.needle);
            continue;
        };
        let Some(found_at) = text.find(row.needle) else {
            println!("  SKIP  {:<24} {} (not written)", row.file, row.needle);
            continue;
        };
        let Some(name_at) = row.needle.find(row.name) else {
            println!(
                "  SKIP  {:<24} {} (the name is not in the needle)",
                row.file, row.needle
            );
            continue;
        };
        let cursor = found_at + name_at + 1;
        if session.view(&path).is_none() {
            session.cook(&path);
        }
        let answer = declared_type(&session, &path, cursor);
        let pass = answer
            .as_deref()
            .is_some_and(|answ| answ.contains(row.expected));
        if !pass {
            type_failures += 1;
        }
        println!(
            "  {:<4}  {:<28} {:<20} → {}",
            if pass { "PASS" } else { "FAIL" },
            row.file,
            row.name,
            answer.unwrap_or_else(|| "<no type>".to_string())
        );
    }

    if std::env::var_os("CPPLS_C_MEMBERS").is_some()
        && let Some(view) = session.view(root.join("src/strings.cpp"))
    {
        println!(
            "\n[members] config standard: {:?}",
            session.config().standard
        );
        for spelling in ["std::string_view", "std::string", "std::future"] {
            match session.members_of(&view, spelling) {
                Known::Yes(list) => println!(
                    "\n[members] {spelling}: {} member(s), unlisted={}, has starts_with={}",
                    list.members.len(),
                    list.unlisted.len(),
                    list.members
                        .iter()
                        .any(|member| member.fact.name == "starts_with")
                ),
                Known::Unknown(reason) => println!("\n[members] {spelling}: unknown ({reason:?})"),
                Known::No => println!("\n[members] {spelling}: no"),
            }
        }
    }

    println!(
        "\n=== summary: {} file(s) ({} with errors), {}/{} members, {}/{} types",
        files.len(),
        files_with_errors,
        MEMBERS.len() - member_failures,
        MEMBERS.len(),
        TYPES.len() - type_failures,
        TYPES.len()
    );
}