use std::collections::HashMap;
use std::path::{Path, PathBuf};
use cpp_code_analysis::graph::Marked;
use cpp_code_analysis::{
CompilerConfig, FileIndexer, FileSummary, MacroBindings, MacroDefinitions, MemoryFiles,
SummaryKey, TranslationUnit,
};
use cpp_parser::MacroFacts;
fn the_spellings_of(text: &str) -> Vec<String> {
let (tokens, _) = cpp_parser::lex(text, &cpp_parser::LexerConfig::default());
tokens
.iter()
.filter(|token| {
!matches!(
token.kind,
cpp_parser::CppTokenKind::None
| cpp_parser::CppTokenKind::Whitespace
| cpp_parser::CppTokenKind::Newline
| cpp_parser::CppTokenKind::LineComment
| cpp_parser::CppTokenKind::BlockComment
| cpp_parser::CppTokenKind::LineContinuation
| cpp_parser::CppTokenKind::DocTrivia
| cpp_parser::CppTokenKind::Eof
)
})
.map(|token| text[token.range.start_offset..token.range.end_offset()].to_string())
.collect()
}
fn the_stream_says(spellings: &[String], expected: &[&str]) -> bool {
spellings.windows(expected.len()).any(|window| {
window
.iter()
.zip(expected)
.all(|(held, wanted)| held == wanted)
})
}
struct Unit {
summaries: HashMap<PathBuf, FileSummary>,
sources: HashMap<PathBuf, String>,
files: MemoryFiles,
seed: Marked,
}
impl Unit {
fn new(files: &[(&str, &str)]) -> Self {
let mut provider = MemoryFiles::new();
for (path, text) in files {
provider.insert(path, *text);
}
let mut config = CompilerConfig::default();
for (path, _) in files {
if let Some(parent) = Path::new(path).parent() {
config = config.with_include_path(parent.to_path_buf());
}
}
let indexer = FileIndexer::new(&provider, &config);
let key = SummaryKey::new(0, 0);
let mut summaries = HashMap::new();
let mut sources = HashMap::new();
for (path, text) in files {
let path = PathBuf::from(path);
summaries.insert(path.clone(), indexer.index(&path, text, key));
sources.insert(path, (*text).to_string());
}
Unit {
summaries,
sources,
files: provider,
seed: {
let mut marked = Marked::default();
marked.define_on_the_command_line("__cplusplus", Some("202002L"));
marked.define_on_the_command_line("_WIN32", Some("1"));
marked
},
}
}
fn look_up(&self, wanted: &Path) -> Option<(&FileSummary, &str)> {
Some((
self.summaries.get(wanted)?,
self.sources.get(wanted).map(String::as_str).unwrap_or(""),
))
}
fn per_file(
&self,
path: &str,
definitions: &mut MacroDefinitions,
) -> cpp_parser::MacroEnvironment {
let path = PathBuf::from(path);
let summary = self.summaries.get(&path).expect("indexed");
let evidence = cpp_code_analysis::macros_from_the_closure_with_bodies(
summary,
|wanted| self.look_up(wanted),
&self.seed,
definitions,
);
cpp_parser::MacroEnvironment::from_included_macros(evidence.macros)
.with_bodies_in_force(evidence.conditional_bodies)
}
fn timeline_of(&self, root: &str, definitions: &mut MacroDefinitions) -> TranslationUnit {
let root = PathBuf::from(root);
TranslationUnit::walk(
self.summaries.get(&root).expect("indexed"),
|wanted| self.look_up(wanted),
&self.seed,
definitions,
false,
&cpp_code_analysis::WalkCache::new(),
)
}
}
fn answers(
environment: &dyn cpp_parser::MacroFacts,
name: &str,
offset: usize,
) -> (
Option<String>,
Option<String>,
Option<String>,
Option<String>,
) {
let kind = environment
.kind_of(name, offset)
.map(|kind| format!("{kind:?}"));
(
kind,
environment.body_text_of(name, offset).map(str::to_string),
environment
.parameters_of(name, offset)
.map(|list| list.to_string()),
environment.body_text_in_force(name).map(str::to_string),
)
}
#[test]
fn the_timeline_answers_what_the_per_file_walk_answered() {
let files = [
(
"/p/config.h",
"#ifndef _CONFIG_H_\n\
#define _CONFIG_H_\n\
#ifdef __cplusplus\n\
#define SA(id) id\n\
#else\n\
#define SA(id) SA_##id\n\
#endif\n\
#define REPEATABLE [repeatable]\n\
#endif\n",
),
(
"/p/api.h",
"#include \"config.h\"\n\
#define API_CALL __cdecl\n\
#if _WIN32\n\
#define WINDOWS 1\n\
#endif\n\
API_CALL void f(SA(int) value);\n",
),
(
"/p/main.cpp",
"#include \"api.h\"\n\
int before = API_CALL;\n\
WINDOWS\n",
),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let per_file = unit.per_file("/p/api.h", &mut definitions);
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
let view = timeline
.environment_of(Path::new("/p/api.h"))
.expect("the unit reaches api.h");
let source = unit.sources.get(Path::new("/p/api.h")).expect("read");
let names = [
"SA",
"REPEATABLE",
"API_CALL",
"WINDOWS",
"_CONFIG_H_",
"f",
"value",
];
let mut the_timeline_knew_more = 0usize;
for name in names {
for offset in (0..source.len()).step_by(7) {
let materialised = answers(&per_file, name, offset);
let walked = answers(&view, name, offset);
let positioned =
|answer: &(
Option<String>,
Option<String>,
Option<String>,
Option<String>,
)| { (answer.0.clone(), answer.1.clone(), answer.2.clone()) };
if materialised.0.is_some() || materialised.1.is_some() || materialised.2.is_some() {
assert_eq!(
positioned(&walked),
positioned(&materialised),
"`{name}` at offset {offset} of api.h: the fallback answers this position and the timeline \
does not answer it the same way"
);
} else if walked.0.is_some() || walked.1.is_some() || walked.2.is_some() {
assert!(
per_file.body_text_in_force(name).is_some(),
"`{name}` at offset {offset} of api.h: the timeline answered {:?}, the fallback {:?} — and the \
fallback does not even carry the name's body in force, so this is not a conditional it could \
not place",
positioned(&walked),
positioned(&materialised),
);
the_timeline_knew_more += 1;
}
if let Some(body) = materialised.3.as_deref() {
assert_eq!(
walked.3.as_deref(),
Some(body),
"`{name}` at offset {offset} of api.h: the fallback has a body in force and the timeline has \
another"
);
}
}
}
assert!(
the_timeline_knew_more > 0,
"the allowance has to be used by this fixture, or the test is not asserting anything about it"
);
assert_eq!(
per_file.body_text_of("API_CALL", source.len()),
None,
"the per-file walk carries what the includes contribute, and `API_CALL` is api.h's own `#define`"
);
assert_eq!(
view.body_text_in_force("SA").map(str::trim),
Some("id"),
"`#ifdef __cplusplus` was answered as C++ in the walk, so the C++ spelling of `SA` is the one in force — \
and a definition inside a taken branch is a **body**, not a definition, in both readings"
);
assert_eq!(
per_file.body_text_in_force("SA").map(str::trim),
Some("id"),
"the per-file walk answered the same branch"
);
assert_eq!(
view.body_text_of("API_CALL", source.len()),
None,
"a file's own facts are the parser's own reading in both readings — see `environment_at`'s contract"
);
assert!(
timeline
.facts_of(Path::new("/p/api.h"))
.any(|(name, at)| name == "API_CALL" && source[at..].starts_with("API_CALL")),
"the timeline records what the file wrote, at the offset it wrote it"
);
}
#[test]
fn a_files_environment_carries_the_state_of_the_unit_it_is_read_in() {
let files = [
("/p/api.h", "int inside = FROM_THE_SOURCE;\n"),
(
"/p/main.cpp",
"#define FROM_THE_SOURCE 1\n#include \"api.h\"\n",
),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let per_file = unit.per_file("/p/api.h", &mut definitions);
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
let view = timeline
.environment_of(Path::new("/p/api.h"))
.expect("the unit reaches api.h");
assert_eq!(
per_file.kind_of("FROM_THE_SOURCE", 0),
None,
"api.h's own closure says nothing about it — the name is the source file's"
);
assert!(
view.kind_of("FROM_THE_SOURCE", 0).is_some(),
"…and the unit that includes api.h has it in force, which is what the header really sees"
);
assert_eq!(
view.body_text_of("FROM_THE_SOURCE", 0).map(str::trim),
Some("1"),
"with the body the compilation had"
);
}
#[test]
fn a_file_is_found_in_its_timeline_however_its_path_is_spelled() {
let files = [
("/p/api.h", "int inside;\n"),
("/p/main.cpp", "#include \"api.h\"\n"),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
assert!(
timeline.environment_of(Path::new("/p/main.cpp")).is_some(),
"the root is in its own timeline"
);
assert!(
timeline.environment_of(Path::new("/p/api.h")).is_some(),
"and so is the file it includes"
);
#[cfg(windows)]
for spelled_differently in ["\\p\\MAIN.CPP", "/P/Api.h"] {
assert!(
timeline
.environment_of(Path::new(spelled_differently))
.is_some(),
"`{spelled_differently}` names a file this unit entered, so it must answer"
);
}
}
#[test]
fn an_included_definition_is_in_force_from_the_include() {
let files = [
("/p/late.h", "#define LATE 1\n"),
(
"/p/user.h",
"int before = LATE;\n#include \"late.h\"\nint after = LATE;\n",
),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/user.h", &mut definitions);
let view = timeline.root_environment();
let source = unit.sources.get(Path::new("/p/user.h")).expect("read");
let include_end =
source.find("#include \"late.h\"").expect("the include") + "#include \"late.h\"".len();
assert_eq!(
view.kind_of("LATE", include_end - 2),
None,
"before the include, nothing here has defined `LATE`"
);
assert!(
view.kind_of("LATE", include_end + 2).is_some(),
"after the include, it is a macro — from the end of the `#include`, not from offset 0"
);
}
#[test]
fn a_file_outside_the_unit_has_no_environment() {
let files = [
("/p/api.h", "#define API 1\n"),
("/p/main.cpp", "#include \"api.h\"\n"),
("/p/unrelated.h", "#define UNRELATED 1\n"),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
assert!(timeline.environment_of(Path::new("/p/api.h")).is_some());
assert!(
timeline
.environment_of(Path::new("/p/unrelated.h"))
.is_none(),
"nothing includes it, so nothing here says what it sees"
);
assert_eq!(
timeline.files().count(),
2,
"the walk entered the source and the header it includes, and nothing else"
);
}
#[test]
fn the_unit_walks_its_files_once() {
let files = [
("/p/a.h", "#define A 1\n"),
("/p/b.h", "#include \"a.h\"\n#define B 1\n"),
(
"/p/c.h",
"#include \"a.h\"\n#include \"b.h\"\n#define C 1\n",
),
("/p/main.cpp", "#include \"c.h\"\n"),
];
let mut provider = MemoryFiles::new();
for (path, text) in files {
provider.insert(path, text);
}
let mut config = CompilerConfig::default();
config = config.with_include_path(PathBuf::from("/p"));
let indexer = FileIndexer::new(&provider, &config);
let key = SummaryKey::new(0, 0);
let mut summaries = HashMap::new();
let mut sources = HashMap::new();
for (path, text) in files {
let path = PathBuf::from(path);
summaries.insert(path.clone(), indexer.index(&path, text, key));
sources.insert(path, (*text).to_string());
}
let mut seed = Marked::default();
seed.define_on_the_command_line("__cplusplus", Some("202002L"));
let look_up = |wanted: &Path| {
Some((
summaries.get(wanted)?,
sources.get(wanted).map(String::as_str).unwrap_or(""),
))
};
let mut definitions = MacroDefinitions::default();
let timeline = TranslationUnit::walk(
summaries.get(Path::new("/p/main.cpp")).expect("indexed"),
look_up,
&seed,
&mut definitions,
false,
&cpp_code_analysis::WalkCache::new(),
);
for path in ["/p/a.h", "/p/b.h", "/p/c.h", "/p/main.cpp"] {
assert!(
provider.reads_of(path) <= 1,
"{path} was read {} times for one unit",
provider.reads_of(path)
);
}
let frames: Vec<&Path> = timeline.files().collect();
assert_eq!(frames.len(), 5, "four files, five entries: {frames:?}");
assert_eq!(
frames
.iter()
.filter(|path| **path == Path::new("/p/a.h"))
.count(),
2,
"`a.h` has no guard, so both includes read it: {frames:?}"
);
}
#[test]
fn an_unguarded_header_is_read_again_and_defines_what_its_second_reader_asked_for() {
let files = [
(
"/p/version.h",
"#ifdef WANT_PRINT\n#define HAS_PRINT 1\n#endif\n",
),
("/p/print.h", "#define WANT_PRINT\n#include \"version.h\"\n"),
(
"/p/a.h",
"#include \"version.h\"\n#ifdef HAS_PRINT\nint a_print;\n#endif\n",
),
(
"/p/b.h",
"#include \"print.h\"\n#ifdef HAS_PRINT\nint b_print;\n#endif\n",
),
("/p/main.cpp", "#include \"a.h\"\n#include \"b.h\"\n"),
];
let mut provider = MemoryFiles::new();
for (path, text) in files {
provider.insert(path, text);
}
let mut config = CompilerConfig::default();
config = config.with_include_path(PathBuf::from("/p"));
let indexer = FileIndexer::new(&provider, &config);
let key = SummaryKey::new(0, 0);
let mut summaries = HashMap::new();
let mut sources = HashMap::new();
for (path, text) in files {
let path = PathBuf::from(path);
summaries.insert(path.clone(), indexer.index(&path, text, key));
sources.insert(path, (*text).to_string());
}
let seed = Marked::default();
let look_up = |wanted: &Path| {
Some((
summaries.get(wanted)?,
sources.get(wanted).map(String::as_str).unwrap_or(""),
))
};
let mut definitions = MacroDefinitions::default();
let timeline = TranslationUnit::walk(
summaries.get(Path::new("/p/main.cpp")).expect("indexed"),
look_up,
&seed,
&mut definitions,
false,
&cpp_code_analysis::WalkCache::new(),
);
let at_the_end = |path: &str| sources.get(Path::new(path)).map_or(0, String::len);
let a = timeline
.environment_of(Path::new("/p/a.h"))
.expect("`a.h` has an environment");
assert!(
a.kind_of("HAS_PRINT", at_the_end("/p/a.h")).is_none(),
"`a.h` asked `version.h` for nothing, so nothing was defined"
);
let b = timeline
.environment_of(Path::new("/p/b.h"))
.expect("`b.h` has an environment");
assert!(
b.kind_of("HAS_PRINT", at_the_end("/p/b.h")).is_some(),
"`b.h` reaches `version.h` through `print.h`, which asks for `HAS_PRINT` — the second read is the one that \
defines it"
);
}
#[test]
fn the_unit_cooks_as_one_stream_in_include_order() {
let files = [
("/p/config.h", "#define WIDTH 4\nstruct Cfg { int a; };\n"),
(
"/p/api.h",
"#include \"config.h\"\n#define API WIDTH\nstruct Api { Cfg c; };\n",
),
("/p/main.cpp", "#include \"api.h\"\nint main_use = API;\n"),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
let shared = timeline.definitions();
let stitched = timeline.cook_the_unit(&unit.sources, &shared, None, true, None);
assert_eq!(
stitched.missing, 0,
"every file the unit reached has text here"
);
assert_eq!(
stitched.files_with_tokens(),
3,
"all three files contributed a token: {}",
stitched.text
);
let at = |needle: &str| {
stitched
.text
.find(needle)
.unwrap_or_else(|| panic!("`{needle}` is in the stream: {}", stitched.text))
};
assert!(at("struct Cfg") < at("struct Api"), "{}", stitched.text);
assert!(at("struct Api") < at("main_use"), "{}", stitched.text);
let tree = cpp_parser::CppParser::parse(&stitched.text, cpp_parser::ParserConfig::default());
assert!(
tree.get_errors().is_empty(),
"the stitched unit has errors: {:?} in {}",
tree.get_errors(),
stitched.text
);
let spellings = the_spellings_of(&stitched.text);
assert!(
the_stream_says(&spellings, &["main_use", "=", "4"]),
"`API` expanded to `WIDTH`'s value, which is defined two files away: {}",
stitched.text
);
for name in ["API", "WIDTH"] {
assert!(
!stitched.text.contains(name),
"`{name}` is gone from the stream, replaced by what it stands for: {}",
stitched.text
);
}
let api = Path::new("/p/api.h");
let api_text = unit.sources.get(api).expect("read");
let api_at = stitched.text.find("Api").expect("in the stream");
let (file, written) = stitched.written_at(api_at).expect("a token is there");
assert_eq!(stitched.file_of(file), Some(api));
assert_eq!(&api_text[written.start_offset..written.end_offset()], "Api");
let main = Path::new("/p/main.cpp");
let main_text = unit.sources.get(main).expect("read");
let four = stitched
.text
.find('4')
.expect("the expansion is in the stream");
let (file, written) = stitched.written_at(four).expect("a token is there");
assert_eq!(
stitched.file_of(file),
Some(main),
"a macro's token stands in the file that invoked it"
);
assert_eq!(
written.start_offset,
main_text.find("API").expect("the invocation"),
"and it points at the call site, not at a body in another file"
);
}
#[test]
fn a_declaration_starts_in_the_file_it_stands_in() {
let files = [
(
"/p/macros.h",
"#define DECLARE_WIDGET struct Widget { int size; };\n",
),
(
"/p/decl.h",
"#include \"macros.h\"\nnamespace outer {\nDECLARE_WIDGET\n}\n",
),
("/p/main.cpp", "#include \"decl.h\"\nint use = 0;\n"),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
let shared = timeline.definitions();
let stream = timeline.cook_the_unit(&unit.sources, &shared, None, true, None);
let spellings = the_spellings_of(&stream.text);
assert!(
the_stream_says(
&spellings,
&["struct", "Widget", "{", "int", "size", ";", "}", ";"]
),
"the macro expanded into the stream: {}",
stream.text
);
let tree = cpp_parser::CppParser::parse(&stream.text, cpp_parser::ParserConfig::default());
let widget = tree
.get_red_root()
.descendants()
.find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::StructDef
})
.expect("the struct is in the stream");
let range = cpp_parser::source_range(widget.text_range());
let (file, written) = stream.written_span(range).expect("it maps");
assert_eq!(
stream.file_of(file),
Some(Path::new("/p/decl.h")),
"the declaration **stands in** decl.h, which is the file whose invocation produced it"
);
let decl = &unit.sources[Path::new("/p/decl.h")];
let acted_on = decl
.get(written.start_offset..written.end_offset())
.unwrap_or_else(|| {
panic!(
"the answer does not land in the file it names: {written:?} of {} bytes",
decl.len()
)
});
assert!(
acted_on.contains("DECLARE_WIDGET"),
"the range is the declaration as that file spells it — the invocation, not the body: {acted_on:?}"
);
}
#[test]
fn taking_a_file_out_of_the_stream_keeps_the_offsets() {
let files = [
("/p/first.h", "struct First { int a; };\n"),
("/p/leaky.h", "struct Leaky { int b; };\n"),
("/p/third.h", "struct Third { int c; };\n"),
(
"/p/main.cpp",
"#include \"first.h\"\n#include \"leaky.h\"\n#include \"third.h\"\nint use = 1;\n",
),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
let shared = timeline.definitions();
let whole = timeline.cook_the_unit(&unit.sources, &shared, None, true, None);
let leaky = whole
.files
.iter()
.position(|path| path == Path::new("/p/leaky.h"))
.expect("the file is in the unit") as u32;
let mut left_out = std::collections::BTreeSet::new();
left_out.insert(leaky);
let without = whole.without(&left_out);
assert_eq!(
without.text.len(),
whole.text.len(),
"the text is the same length, which is what keeps every offset where it was"
);
assert_eq!(
without.text.find("Third"),
whole.text.find("Third"),
"so a declaration after the left-out file is found at the same place"
);
assert!(
!without.text.contains("Leaky"),
"and the left-out file's own tokens are gone: {}",
without.text
);
let third = without.text.find("Third").expect("still in the stream");
let (file, written) = without
.written_span(cpp_parser::SourceRange::new(third, 5))
.expect("mapped");
assert_eq!(
without.file_of(file),
Some(Path::new("/p/third.h")),
"a token after the left-out file still belongs to its own file"
);
assert_eq!(
written.start_offset,
"struct Third { int c; };\n"
.find("Third")
.expect("its own text"),
"and to its own offset in that file, not to where it stands in the stream"
);
let tree = cpp_parser::CppParser::parse(&without.text, cpp_parser::ParserConfig::default());
assert!(tree.get_errors().is_empty(), "{:?}", tree.get_errors());
}
#[test]
fn a_file_that_does_not_balance_still_contributes_its_tokens() {
let files = [
(
"/p/leaky.h",
"namespace vc_attributes {\nstruct First { int a; };\n",
),
("/p/second.h", "struct Second { int b; };\n"),
(
"/p/main.cpp",
"#include \"leaky.h\"\n#include \"second.h\"\n",
),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
let shared = timeline.definitions();
let stitched = timeline.cook_the_unit(&unit.sources, &shared, None, true, None);
assert!(
stitched.text.contains("First"),
"the unbalanced file's own declaration is in the stream: {}",
stitched.text
);
assert!(
stitched.text.contains("Second"),
"and so is the file after it: {}",
stitched.text
);
assert_eq!(
stitched.unbalanced,
vec![PathBuf::from("/p/leaky.h")],
"the file that does not balance is named, and only it"
);
assert!(
stitched.braces > 0,
"an unclosed `{{` leaves the whole stream one closer short, so the repair is load-bearing: {}",
stitched.braces
);
let second = stitched
.text
.find("Second")
.expect("the file after the unbalanced one is in the stream");
let (file, written) = stitched
.written_span(cpp_parser::SourceRange::new(second, "Second".len()))
.expect("mapped");
assert_eq!(
stitched.file_of(file),
Some(Path::new("/p/second.h")),
"a token after the unbalanced file still belongs to its own file"
);
assert_eq!(
written.start_offset,
"struct Second { int b; };\n"
.find("Second")
.expect("its own text"),
"and to its own offset in that file"
);
}
#[test]
fn a_file_without_text_is_a_hole_in_the_unit() {
let files = [
("/p/config.h", "#define WIDTH 4\nstruct Cfg { int a; };\n"),
(
"/p/api.h",
"#include \"config.h\"\nstruct Api { int a[WIDTH]; };\n",
),
("/p/main.cpp", "#include \"api.h\"\n"),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
let shared = timeline.definitions();
let mut partial: HashMap<PathBuf, String> = unit.sources.clone();
partial.remove(Path::new("/p/api.h"));
let stitched = timeline.cook_the_unit(&partial, &shared, None, true, None);
assert_eq!(stitched.missing, 1, "api.h was reached and not read");
assert!(
!stitched.text.contains("Api"),
"nothing was invented for the missing file: {}",
stitched.text
);
assert!(
stitched.text.contains("Cfg"),
"and the file *it* included is still stitched, because the walk reached it through the hole: {}",
stitched.text
);
}
#[test]
fn a_macro_that_declares_a_type_declares_it_in_the_cooked_reading() {
let declaration = "#define DECLARE_HANDLE(name) struct name##__ { int unused; }; \
typedef struct name##__ *name\n";
let files = [
("/p/decl.h", declaration),
("/p/api.h", "#include \"decl.h\"\nDECLARE_HANDLE(HWND);\n"),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/api.h", &mut definitions);
let shared = timeline.definitions();
let config = CompilerConfig::default();
let indexer = FileIndexer::new(&unit.files, &config);
let key = SummaryKey::new(0, 0);
let api = Path::new("/p/api.h");
let source = unit.sources.get(api).expect("read");
let raw = indexer.index(api, source, key);
let raw_names: Vec<&str> = raw.declarations.iter().map(|fact| &*fact.name).collect();
assert!(
!raw_names.contains(&"HWND__") && !raw_names.contains(&"HWND"),
"the raw reading cannot see a declaration that is inside a macro body: {raw_names:?}"
);
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let view = timeline
.environment_of(api)
.expect("the unit reaches api.h");
let macros = cpp_code_analysis::FileMacros::new(&view, &shared, None, true);
let cooked = cpp_code_analysis::cook_with(source, &tokens, ¯os);
let rendered = cooked.render();
assert!(
rendered.text.contains("HWND__"),
"the rendering has the declaration: {}",
rendered.text
);
let indexed = indexer.index_rendering(api, &rendered, key);
let cooked_summary = &indexed.summary;
let report = indexed.mapped;
let cooked_names: Vec<&str> = cooked_summary
.declarations
.iter()
.map(|fact| &*fact.name)
.collect();
assert!(
cooked_names.contains(&"HWND__"),
"the struct the macro declares is a declaration here: {cooked_names:?}"
);
assert!(
cooked_names.contains(&"HWND"),
"…and the typedef beside it: {cooked_names:?}"
);
assert_eq!(report.dropped, 0, "every range landed in the file");
assert!(report.placed > 0);
let hwnd = cooked_summary
.declarations
.iter()
.find(|fact| fact.name == "HWND__")
.expect("declared");
let written = &source[hwnd.range.start_offset..hwnd.range.end_offset()];
assert!(
written.contains("DECLARE_HANDLE"),
"the declaration is reported at the invocation: {written:?}"
);
}
#[test]
fn a_definition_says_which_file_it_was_written_in() {
let files = [
("/p/config.h", "#define WIDTH 4\n"),
("/p/api.h", "#include \"config.h\"\n#define API WIDTH\n"),
("/p/main.cpp", "#include \"api.h\"\nint v = API;\n"),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
let shared = timeline.definitions();
let main = Path::new("/p/main.cpp");
let text = unit.sources.get(main).expect("read");
let (tokens, _) = cpp_parser::lex(text, &cpp_parser::LexerConfig::default());
let view = timeline
.environment_of(main)
.expect("the unit reaches main.cpp");
let macros = cpp_code_analysis::FileMacros::new(&view, &shared, None, true);
let cooked = cpp_code_analysis::cook_with(text, &tokens, ¯os);
let rendered = cooked.render();
let four = cooked
.tokens
.iter()
.find(|token| token.text() == "4")
.expect("the expansion is in the stream");
let at = rendered
.text
.find('4')
.expect("the expansion is in the rendering");
assert_eq!(
rendered.written_at(at),
None,
"the spelling of `4` is in config.h, not in main.cpp"
);
let reported = rendered.reported_at(at).expect("a place to report");
assert_eq!(
&text[reported.start_offset..reported.end_offset()],
"API",
"a diagnostic lands on the call site"
);
let (file, name_at) = four.navigation_at().expect("a definition in a file");
let config = Path::new("/p/config.h");
let cpp_code_analysis::macros::MacroFile::Frame(frame) = file else {
panic!("`WIDTH` was not written in the file being cooked: {file:?}");
};
assert_eq!(
timeline.frame_file(frame),
Some(config),
"the frame names config.h"
);
let config_text = unit.sources.get(config).expect("read");
assert_eq!(
&config_text[name_at.start_offset..name_at.end_offset()],
"WIDTH",
"and the range is the macro's name, in that file"
);
let local = [("/p/local.cpp", "#define HERE 7\nint v = HERE;\n")];
let unit = Unit::new(&local);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/local.cpp", &mut definitions);
let shared = timeline.definitions();
let path = Path::new("/p/local.cpp");
let text = unit.sources.get(path).expect("read");
let (tokens, _) = cpp_parser::lex(text, &cpp_parser::LexerConfig::default());
let view = timeline.environment_of(path).expect("reached");
let macros = cpp_code_analysis::FileMacros::new(&view, &shared, None, true);
let cooked = cpp_code_analysis::cook_with(text, &tokens, ¯os);
let rendered = cooked.render();
let seven = cooked
.tokens
.iter()
.find(|token| token.text() == "7")
.expect("the expansion");
let at = rendered.text.find('7').expect("in the rendering");
let written = rendered.written_at(at).expect("written in this file");
assert_eq!(
&text[written.start_offset..written.end_offset()],
"7",
"a body token of a file's own macro is at the body, in this file"
);
assert_eq!(
seven.navigation_at().expect("a definition in a file").0,
cpp_code_analysis::macros::MacroFile::Here
);
}
#[test]
fn a_file_cooks_the_same_through_the_unit_as_through_a_table_of_its_own() {
let files = [
(
"/p/config.h",
"#ifndef _CONFIG_H_\n\
#define _CONFIG_H_\n\
#ifdef __cplusplus\n\
#define SA(id) id\n\
#else\n\
#define SA(id) SA_##id\n\
#endif\n\
#define OBJECT 1\n\
#endif\n",
),
(
"/p/api.h",
"#include \"config.h\"\n\
#define LATE(x) (x)\n\
int f = OBJECT + LATE(2);\n\
#undef OBJECT\n\
int g = SA(GG);\n",
),
(
"/p/main.cpp",
"#include \"api.h\"\n\
#define LATE(x) [x]\n\
int h = OBJECT;\n\
LATE(3)\n",
),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
let shared = timeline.definitions();
let mut rendered_something_the_reading_can_be_told_apart_by = false;
for (path, text) in &files {
let path = Path::new(path);
let view = timeline
.environment_of(path)
.unwrap_or_else(|| panic!("the unit reaches {}", path.display()));
let mut per_file_definitions = cpp_code_analysis::ParsedDefinitions::new();
let configuration = cpp_code_analysis::configuration_from_environment_and(
&view,
true,
&mut per_file_definitions,
);
let file_macros = cpp_code_analysis::FileMacros::new(&view, &shared, None, true);
let (tokens, _) = cpp_parser::lex(text, &cpp_parser::LexerConfig::default());
let from_the_table = cpp_code_analysis::cook_with(text, &tokens, &configuration.table);
let from_the_unit = cpp_code_analysis::cook_with(text, &tokens, &file_macros);
rendered_something_the_reading_can_be_told_apart_by |=
!from_the_table.render().text.trim().is_empty();
assert_eq!(
from_the_unit.render().text,
from_the_table.render().text,
"{} cooked differently through the unit than through its own table",
path.display()
);
}
assert!(
rendered_something_the_reading_can_be_told_apart_by,
"the fixture rendered nothing at all, so the comparison above was vacuous"
);
let builtins = "#define LATE 99\n#define OBJECT 99\n#define _FROM_THE_SEED_ 1\n";
let (tokens, _) = cpp_parser::lex(builtins, &cpp_parser::LexerConfig::default());
let seed = cpp_code_analysis::preprocess::preprocess(builtins, &tokens).macros;
let body_of = |definition: &cpp_code_analysis::macros::MacroDef| -> String {
definition
.body
.significant()
.map(|token| token.text().to_string())
.collect()
};
let root = timeline.root_environment();
let with_seed = cpp_code_analysis::FileMacros::new(&root, &shared, Some(&seed), true);
assert_eq!(
with_seed.definition("LATE").map(body_of),
Some("(x)".to_string()),
"api.h's `#define LATE(x) (x)` arrives through the include and is in force over the seed's `99` \
(main.cpp's own `LATE(x) [x]` is the *own* layer, which the cook adds — not this one)"
);
assert!(
with_seed.definition("_FROM_THE_SEED_").is_some(),
"a name only the seed defines still reaches the cook — that is what the oldest layer is for"
);
assert_eq!(
with_seed.definition("OBJECT").map(body_of),
Some("99".to_string()),
"the unit's `#undef` does not reach the cook's table — see the note above"
);
assert!(
with_seed.definition("_NOBODY_SAYS_").is_none(),
"and a name nobody defines is nobody's: the seed does not invent one"
);
}
#[test]
#[ignore = "a probe: prints a cooked stream and a parse tree"]
fn a_scope_macro_two_headers_use() {
let files = [
(
"/p/core.h",
"#define _STD_BEGIN namespace std {\n#define _STD_END }\n",
),
(
"/p/a.h",
"#include \"core.h\"\n_STD_BEGIN\nstruct A { int a; };\n_STD_END\n",
),
(
"/p/b.h",
"#include \"core.h\"\n_STD_BEGIN\nstruct B { int b; };\n_STD_END\n",
),
("/p/main.cpp", "#include \"a.h\"\n#include \"b.h\"\n"),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
let shared = timeline.definitions();
let stitched = timeline.cook_the_unit(&unit.sources, &shared, None, true, None);
println!("--- cooked ---\n{}--- end ---", stitched.text);
println!("boundaries: {:?}", stitched.file_boundaries());
let tree = cpp_parser::CppParser::parse(
&stitched.text,
cpp_parser::ParserConfig::default().with_file_boundaries(stitched.file_boundaries()),
);
let mut inside: Vec<String> = Vec::new();
let mut node = tree.get_red_root().descendants().find(|node| {
&stitched.text[usize::from(node.text_range().start())..usize::from(node.text_range().end())]
== "B"
});
while let Some(current) = node {
let kind = format!("{:?}", current.kind());
if kind.contains("Namespace") {
inside.push(kind);
}
node = current.parent();
}
println!(
"namespace ancestors of `B`: {} ({:?})",
inside.len(),
inside
);
let configuration = cpp_code_analysis::CompilerConfig::default();
let indexer = FileIndexer::new(&unit.files, &configuration);
let indexed =
indexer.index_unit_rendering(Path::new("/p/main.cpp"), &stitched, SummaryKey::new(0, 0));
for (path, cooked) in &indexed.files {
for fact in &cooked.declarations {
if fact.name.as_str() == "A" || fact.name.as_str() == "B" {
println!(
"{}: {} in scope {:?}",
path.display(),
fact.name,
fact.scope
);
}
}
}
}
#[test]
#[ignore = "a probe: prints where a declaration lands when the `{` ends the file"]
fn a_namespace_whose_brace_ends_the_file() {
let first = "namespace std {";
let second = "int b;";
let text = format!("{first}\n{second}\n");
let boundary = first.len() + 1;
let tree = cpp_parser::CppParser::parse(
&text,
cpp_parser::ParserConfig::default().with_file_boundaries(vec![boundary]),
);
let mut inside: Vec<String> = Vec::new();
let mut node = tree.get_red_root().descendants().find(|node| {
&text[usize::from(node.text_range().start())..usize::from(node.text_range().end())] == "b"
});
while let Some(current) = node {
let kind = format!("{:?}", current.kind());
if kind.contains("Namespace") {
inside.push(kind);
}
node = current.parent();
}
println!("boundary {boundary}; namespace ancestors of `b`: {inside:?}");
}
#[test]
fn a_header_the_stream_holds_twice_declares_its_member_once() {
let files = [
(
"/p/range.h",
"struct FormatRange {\n unsigned long EndLine;\n unsigned long EndCol;\n};\n",
),
(
"/p/main.cpp",
"#include \"range.h\"\n#include \"range.h\"\n",
),
];
let unit = Unit::new(&files);
let mut definitions = MacroDefinitions::default();
let timeline = unit.timeline_of("/p/main.cpp", &mut definitions);
let shared = timeline.definitions();
let stitched = timeline.cook_the_unit(&unit.sources, &shared, None, true, None);
assert_eq!(
stitched.text.matches("struct FormatRange").count(),
2,
"an unguarded header included twice is spliced twice:\n{}",
stitched.text
);
let configuration = CompilerConfig::default();
let indexer = FileIndexer::new(&unit.files, &configuration);
let indexed =
indexer.index_unit_rendering(Path::new("/p/main.cpp"), &stitched, SummaryKey::new(0, 0));
let (_, cooked) = indexed
.files
.iter()
.find(|(path, _)| path.ends_with("range.h"))
.expect("the header is one entry of the reading");
let named = |wanted: &str| {
cooked
.declarations
.iter()
.filter(|fact| fact.name == wanted)
.count()
};
assert_eq!(
named("FormatRange"),
1,
"one class, however many inclusions"
);
assert_eq!(named("EndLine"), 1, "one member, however many inclusions");
assert_eq!(named("EndCol"), 1, "and its neighbours are not lost either");
assert_eq!(cooked.declarations.len(), 3, "{:?}", cooked.declarations);
}
fn fingerprint_of(unit: &Unit, timeline: &TranslationUnit, path: &str) -> u64 {
let path = Path::new(path);
let text = unit.sources.get(path).expect("the file has text");
let (tokens, _) = cpp_parser::lex(text, &cpp_parser::LexerConfig::default());
let view = timeline.environment_of(path).expect("the unit reaches it");
view.environment_fingerprint(text, &tokens)
}
#[test]
fn a_header_has_one_fingerprint_per_environment() {
let unit = Unit::new(&[
(
"/p/shared.h",
"#define SHARED 1\nstruct Shared { int x; };\n",
),
("/p/a.cpp", "#include \"shared.h\"\n#define A_ONLY 1\n"),
("/p/b.cpp", "#include \"shared.h\"\n#define B_ONLY 2\n"),
("/p/c.cpp", "#define SHARED 2\n#include \"shared.h\"\n"),
]);
let mut definitions = MacroDefinitions::default();
let a = unit.timeline_of("/p/a.cpp", &mut definitions);
let b = unit.timeline_of("/p/b.cpp", &mut definitions);
let c = unit.timeline_of("/p/c.cpp", &mut definitions);
let in_a = fingerprint_of(&unit, &a, "/p/shared.h");
let in_b = fingerprint_of(&unit, &b, "/p/shared.h");
assert_eq!(
in_a, in_b,
"two programs that include the header in the same state must share its reading"
);
assert_ne!(
in_a,
fingerprint_of(&unit, &c, "/p/shared.h"),
"a program that defines SHARED before including it renders the header differently"
);
}
#[test]
fn a_macro_the_file_never_mentions_does_not_change_its_fingerprint() {
let unit = Unit::new(&[
("/p/shared.h", "struct Shared { int x; };\n"),
("/p/a.cpp", "#define UNUSED 1\n#include \"shared.h\"\n"),
("/p/b.cpp", "#define UNUSED 2\n#include \"shared.h\"\n"),
]);
let mut definitions = MacroDefinitions::default();
let a = unit.timeline_of("/p/a.cpp", &mut definitions);
let b = unit.timeline_of("/p/b.cpp", &mut definitions);
assert_eq!(
fingerprint_of(&unit, &a, "/p/shared.h"),
fingerprint_of(&unit, &b, "/p/shared.h"),
"`UNUSED` is not a name `shared.h` writes, so it cannot change its rendering"
);
}
#[test]
fn a_macro_reached_through_a_body_is_part_of_the_environment() {
let unit = Unit::new(&[
("/p/chain.h", "int x = A;\n"),
(
"/p/h1.cpp",
"#define B 1\n#define A B\n#include \"chain.h\"\n",
),
(
"/p/h2.cpp",
"#define B 2\n#define A B\n#include \"chain.h\"\n",
),
]);
let mut definitions = MacroDefinitions::default();
let one = unit.timeline_of("/p/h1.cpp", &mut definitions);
let two = unit.timeline_of("/p/h2.cpp", &mut definitions);
assert_ne!(
fingerprint_of(&unit, &one, "/p/chain.h"),
fingerprint_of(&unit, &two, "/p/chain.h"),
"`A`'s body is `B`, and `B` renders `1` in one program and `2` in the other"
);
}