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
//! **A guarded define in a nested file is not seen by the walk, and this is the reproduction.**
//!
//! The fixture is three files and no includes outside itself:
//!
//! ```text
//! base.h #define BASE 1
//! guarded.h #include "base.h"
//! #ifdef BASE
//! #define INSIDE 1
//! #endif
//! root.cpp #include "guarded.h"
//! ```
//!
//! `BASE` is defined before the `#ifdef BASE` that asks about it, so `INSIDE` is defined in `guarded.h` and in
//! everything that includes it. The chain in `src/effect.rs` says so; `TranslationUnit::walk` does not:
//!
//! ```text
//! guarded.h BASE → defined, body " 1\n"
//! guarded.h INSIDE → defined: false ← the guard was decided against a table without BASE
//! root.cpp INSIDE → not reached at all
//! ```
//!
//! So the guard is evaluated against something narrower than the translation unit's own state: the name it asks
//! about *is* in force by the line that asks. This test is `#[ignore]`d because it fails on the current code — it is
//! a regression test waiting for its fix, not a passing assertion. `the_chain_agrees_with_the_walk_on_every_file`
//! (in `src/effect.rs`) is the broader comparison that found it, and the three names it disagrees on are all this.
//!
use std::collections::HashMap;
use std::path::PathBuf;
use cpp_code_analysis::summary::MacroDefinitions;
use cpp_code_analysis::{CompilerConfig, FileIndexer, MemoryFiles, SummaryKey, TranslationUnit};
fn unit_of(files: &[(&str, &str)], root: &str) -> TranslationUnit {
let mut provider = MemoryFiles::new();
for (path, text) in files {
provider.insert(*path, *text);
}
let config = CompilerConfig::default().with_include_path(PathBuf::from("/p"));
let indexer = FileIndexer::new(&provider, &config);
let mut summaries: HashMap<PathBuf, cpp_code_analysis::FileSummary> = HashMap::new();
let mut texts: HashMap<PathBuf, String> = HashMap::new();
for (path, text) in files {
let path = PathBuf::from(*path);
summaries.insert(
path.clone(),
indexer.index(&path, text, SummaryKey::new(0, 0)),
);
texts.insert(path, text.to_string());
}
TranslationUnit::walk(
summaries.get(std::path::Path::new(root)).expect("indexed"),
|wanted| {
summaries
.get(wanted)
.map(|summary| (summary, texts.get(wanted).map(String::as_str).unwrap_or("")))
},
&cpp_code_analysis::include::graph::Marked::default(),
&mut MacroDefinitions::default(),
false,
&cpp_code_analysis::WalkCache::new(),
)
}
/// **The same question without an include at all**, which narrows the defect: it is not about frames or include
/// order. A `#define` guarded by `#ifdef` on a name the *same file* defined one line earlier is judged out of
/// force too — so the name the condition asks about is answered from somewhere narrower than the file`s own table.
#[test]
fn a_guarded_define_in_the_same_file_is_in_force_too() {
let unit = unit_of(
&[
(
"/p/same.h",
"#define SAME 1\n#ifdef SAME\n#define OWN_INSIDE 1\n#endif\n",
),
("/p/root.cpp", "#include \"same.h\"\nint x;\n"),
],
"/p/root.cpp",
);
let own = unit.macro_at(std::path::Path::new("/p/same.h"), "OWN_INSIDE", usize::MAX);
assert_eq!(
own.map(|found| found.defined),
Some(true),
"`SAME` is defined one line above the `#ifdef SAME` that asks about it"
);
}
#[test]
fn a_guarded_define_is_in_force_after_the_include_that_defines_its_condition() {
let unit = unit_of(
&[
("/p/base.h", "#define BASE 1\n"),
(
"/p/guarded.h",
"#include \"base.h\"\n#ifdef BASE\n#define INSIDE 1\n#endif\n",
),
("/p/root.cpp", "#include \"guarded.h\"\nint x;\n"),
],
"/p/root.cpp",
);
// What the condition asks about, asked of the unit itself: defined, and by the line above it.
let base = unit
.macro_at(std::path::Path::new("/p/guarded.h"), "BASE", usize::MAX)
.expect("BASE was reached");
assert!(base.defined, "the include above the guard defined BASE");
let inside = unit
.macro_at(std::path::Path::new("/p/guarded.h"), "INSIDE", usize::MAX)
.expect("INSIDE is in guarded.h");
assert!(
inside.defined,
"a `#ifdef BASE` after `#include \"base.h\"` is in force: {:?}",
inside.defined
);
let inherited = unit
.macro_at(std::path::Path::new("/p/root.cpp"), "INSIDE", usize::MAX)
.expect("root.cpp includes guarded.h");
assert!(
inherited.defined,
"and it reaches the file that includes it"
);
}