use cpp_code_analysis::{CookedStream, Origin, cook};
fn cooked(source: &str) -> CookedStream {
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
cook(source, &tokens)
}
fn words(stream: &CookedStream) -> Vec<String> {
stream
.tokens
.iter()
.map(|token| token.token.text().to_string())
.collect()
}
fn text(source: &str) -> String {
cooked(source).spellings()
}
fn spelled(rendered: &str) -> Vec<String> {
let (tokens, _) = cpp_parser::lex(rendered, &cpp_parser::LexerConfig::default());
tokens
.iter()
.filter(|token| {
!matches!(
token.kind,
cpp_parser::CppTokenKind::Whitespace
| cpp_parser::CppTokenKind::Newline
| cpp_parser::CppTokenKind::LineComment
| cpp_parser::CppTokenKind::BlockComment
)
})
.map(|token| rendered[token.range.start_offset..token.range.end_offset()].to_string())
.collect()
}
#[test]
fn an_object_like_macro_is_replaced_by_its_body() {
assert_eq!(text("#define N 42\nint x = N;\n"), "int x = 42 ;");
}
#[test]
fn a_function_like_macro_takes_its_arguments() {
assert_eq!(
text("#define MAX(a, b) ((a) > (b) ? (a) : (b))\nint m = MAX(x, y);\n"),
"int m = ( ( x ) > ( y ) ? ( x ) : ( y ) ) ;"
);
}
#[test]
fn a_macro_argument_is_not_expanded_twice() {
assert_eq!(
text("#define TWICE(f) f\nint x = TWICE(g)(2);\n"),
"int x = g ( 2 ) ;"
);
}
#[test]
fn the_instructions_to_the_processor_are_gone() {
assert_eq!(
text("#include <vector>\n#define A 1\nint x = A;\n"),
"int x = 1 ;"
);
assert_eq!(text("#pragma once\nint x;\n"), "# pragma once int x ;");
}
#[test]
fn only_the_compiled_branch_survives() {
assert_eq!(
text("#if 0\nint no;\n#else\nint yes;\n#endif\n"),
"int yes ;"
);
assert_eq!(
text("#if 1\nint yes;\n#else\nint no;\n#endif\n"),
"int yes ;"
);
}
#[test]
fn an_elif_chain_takes_the_first_branch_that_holds() {
let source =
"#define B 2\n#if B == 1\nint one;\n#elif B == 2\nint two;\n#else\nint other;\n#endif\n";
assert_eq!(text(source), "int two ;");
}
#[test]
fn a_define_in_a_branch_nobody_compiles_does_not_take_effect() {
let source =
"#if 0\n#define NOPE 1\n#endif\n#ifdef NOPE\nint wrong;\n#else\nint right;\n#endif\n";
assert_eq!(text(source), "int right ;");
}
#[test]
fn a_define_in_a_compiled_branch_does_take_effect() {
let source =
"#if 1\n#define YES 1\n#endif\n#ifdef YES\nint right;\n#else\nint wrong;\n#endif\n";
assert_eq!(text(source), "int right ;");
}
#[test]
fn a_conditional_inside_a_dead_branch_is_dead_too() {
let source = "#if 0\n#if 1\nint never;\n#endif\nint also_never;\n#endif\nint here;\n";
assert_eq!(text(source), "int here ;");
}
#[test]
fn the_dead_branch_is_reported_as_one_span() {
let source = "int a;\n#if 0\nint b;\n#define M 1\nint c;\n#endif\nint d;\n";
let stream = cooked(source);
assert_eq!(words(&stream), ["int", "a", ";", "int", "d", ";"]);
assert_eq!(stream.inactive.len(), 1, "{:?}", stream.inactive);
let dead = &stream.inactive[0];
assert_eq!(
&source[dead.start_offset..dead.end_offset()],
"#if 0\nint b;\n#define M 1\nint c;\n#endif\n"
);
}
#[test]
fn an_undef_stops_a_macro_from_applying() {
let source = "#define N 1\n#undef N\nint x = N;\n";
assert_eq!(text(source), "int x = N ;");
}
#[test]
fn a_definition_later_in_the_file_does_not_apply_earlier() {
let source = "#define N 1\nint a = N;\n#undef N\n#define N 2\nint b = N;\n";
assert_eq!(text(source), "int a = 1 ; int b = 2 ;");
}
#[test]
fn stringize_and_paste_are_the_operators_and_not_text() {
assert_eq!(
text("#define S(x) #x\nchar* s = S(a b);\n"),
"char * s = \"a b\" ;"
);
assert_eq!(
text("#define P(a, b) a##b\nint xy = P(x, y);\n"),
"int xy = xy ;"
);
}
#[test]
fn a_token_from_a_macro_body_keeps_its_origin() {
let stream = cooked("#define N 42\nint x = N;\n");
let forty_two = stream
.tokens
.iter()
.find(|token| token.token.text() == "42")
.expect("the body's token is in the stream");
match &forty_two.origin {
Origin::Expanded { invocations } => {
assert_eq!(invocations.len(), 1);
assert_eq!(&*invocations[0].name, "N");
}
other => panic!("a token of a body must say which call produced it: {other:?}"),
}
let user_token = stream
.tokens
.iter()
.find(|token| token.token.text() == "x")
.expect("`x` is in the stream");
assert_eq!(user_token.origin, Origin::Source);
}
#[test]
fn a_condition_nobody_can_decide_is_counted_and_treated_as_false() {
let stream = cooked("#if SOMETHING\nint a;\n#else\nint b;\n#endif\n");
assert_eq!(words(&stream), ["int", "b", ";"]);
assert_eq!(stream.assumed_undefined, 1);
}
#[test]
fn a_file_with_nothing_to_cook_is_its_own_tokens() {
let source = "struct S { int a; };\n";
assert_eq!(text(source), "struct S { int a ; } ;");
let stream = cooked(source);
assert!(stream.inactive.is_empty());
assert_eq!(stream.assumed_undefined, 0);
assert!(stream.diagnostics.is_empty());
}
#[test]
fn cooking_is_idempotent_on_its_own_output() {
let source = "#define N 1\n#if N\nint a = N;\n#endif\n";
let once = cooked(source).spellings();
let twice = text(&once);
assert_eq!(once, twice);
}
fn defines(source: &str) -> cpp_code_analysis::MacroTable {
let mut table = cpp_code_analysis::MacroTable::new();
for line in source.lines() {
let Some(definition) = line.strip_prefix("#define ") else {
continue;
};
let (tokens, _) = cpp_parser::lex(definition, &cpp_parser::LexerConfig::default());
let range = cpp_parser::SourceRange::new(0, definition.len());
let tokens: Vec<cpp_code_analysis::Token> = tokens
.iter()
.map(|token| {
cpp_code_analysis::Token::new(
token.kind,
&definition[token.range.start_offset..token.range.end_offset()],
token.range,
)
})
.collect();
if let Some(definition) =
cpp_code_analysis::preprocess::macros::parse_define(&tokens, range)
{
table.define(definition);
}
}
table
}
fn parse_rendered(rendered: &cpp_code_analysis::RenderedCooked) -> cpp_parser::CppSyntaxTree {
cpp_parser::CppParser::parse(&rendered.text, cpp_parser::ParserConfig::default())
}
fn count_of(tree: &cpp_parser::CppSyntaxTree, word: &str) -> usize {
tree.get_red_root()
.descendants_with_tokens()
.filter_map(|element| element.into_token())
.filter(|token| token.text() == word)
.count()
}
#[test]
fn the_raw_reading_reads_every_branch_and_the_cooked_one_reads_the_compiled_branch() {
let source =
"struct S {\n#if FEATURE\n int only_when_on;\n#else\n int only_when_off;\n#endif\n};\n";
let raw = cpp_parser::CppParser::parse(source, cpp_parser::ParserConfig::default());
assert_eq!(raw.get_errors(), [], "both branches are readable as text");
assert_eq!(count_of(&raw, "only_when_on"), 1);
assert_eq!(count_of(&raw, "only_when_off"), 1);
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let cooked = cook(source, &tokens);
let rendered = cooked.render();
let tree = parse_rendered(&rendered);
assert_eq!(tree.get_errors(), []);
assert_eq!(count_of(&tree, "only_when_on"), 0);
assert_eq!(count_of(&tree, "only_when_off"), 1);
assert_eq!(count_of(&tree, "S"), 1, "the class itself is still there");
let cooked =
cpp_code_analysis::cook_with(source, tokens.as_slice(), &defines("#define FEATURE 1\n"));
let tree = parse_rendered(&cooked.render());
assert_eq!(count_of(&tree, "only_when_on"), 1);
assert_eq!(count_of(&tree, "only_when_off"), 0);
}
#[test]
fn the_annotation_families_read_the_same_way_cooked_as_they_do_raw() {
let sal = "#define _Check_return_wat_\n#define _Check_return_opt_\n#define _Success_(x)\n#define _In_\n#define _ACRTIMP __declspec(dllimport)\n";
let nodiscard = "#define _NODISCARD [[nodiscard]]\n";
let cases: &[(&str, &str)] = &[
(
"_Check_return_wat_\n_Success_(return == 0)\n_ACRTIMP errno_t __cdecl fopen_s(FILE** _Stream);\n",
sal,
),
("_ACRTIMP int __cdecl _rmtmp(void);\n", sal),
(
"_Check_return_opt_\n_ACRTIMP int __cdecl __stdio_common_vfwprintf(\n _In_ unsigned __int64 _Options,\n FILE* _Stream\n );\n",
sal,
),
("_NODISCARD int f(void);\n", nodiscard),
];
for (source, configuration) in cases {
let raw = cpp_parser::CppParser::parse(source, cpp_parser::ParserConfig::default());
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let cooked =
cpp_code_analysis::cook_with(source, tokens.as_slice(), &defines(configuration));
let rendered = cooked.render();
let tree = parse_rendered(&rendered);
assert_eq!(
tree.get_errors(),
raw.get_errors(),
"cooked and raw must agree about {source:?}; rendered as {:?}",
rendered.text
);
}
}
#[test]
fn a_pragma_stays_in_the_stream_the_way_the_compiler_keeps_it() {
let source = "#pragma once\n#pragma pack(push, 1)\n#pragma region Name\nint x;\n#pragma endregion Name\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let cooked = cook(source, &tokens);
assert_eq!(
cooked.spellings(),
"# pragma once # pragma pack ( push , 1 ) # pragma region Name int x ; # pragma endregion Name",
"a pragma survives with the arguments that give it meaning: `pack(push, 1)` is not the same pragma as `pack`"
);
assert!(
!cooked.spellings().contains('\n'),
"whitespace stays out of the stream: {:?}",
cooked.spellings()
);
let source = "#if 0\n#pragma pack(1)\n#endif\nint y;\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let cooked = cook(source, &tokens);
assert_eq!(
cooked.spellings(),
"int y ;",
"a pragma in a dead branch is not compiled and does not appear"
);
}
#[test]
fn push_macro_and_pop_macro_save_and_restore_a_definition() {
let source = "\
#define WIDTH 8
#pragma push_macro(\"WIDTH\")
#undef WIDTH
int during = WIDTH;
#pragma pop_macro(\"WIDTH\")
int after = WIDTH;
";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let cooked = cook(source, &tokens);
assert_eq!(
cooked.spellings(),
"int during = WIDTH ; int after = 8 ;",
"the pushed definition is restored by the pop, and the pragmas themselves are not text"
);
let source = "#pragma push_macro(\"GONE\")\n#define GONE 1\n#pragma pop_macro(\"GONE\")\n#ifdef GONE\nint defined;\n#else\nint not_defined;\n#endif\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
assert_eq!(
cook(source, &tokens).spellings(),
"int not_defined ;",
"pushing an undefined name and popping it leaves it undefined, not defined-and-empty"
);
let source = "#define P 8\n#pragma pack(push, P)\nint x;\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
assert_eq!(
cook(source, &tokens).spellings(),
"# pragma pack ( push , 8 ) int x ;",
"`#pragma pack` is program text, and its argument is a macro that expands"
);
}
#[test]
fn an_empty_body_leaves_nothing_behind_and_the_declaration_still_reads() {
let source = "_NODISCARD int f(void);\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let cooked = cpp_code_analysis::cook_with(source, &tokens, &defines("#define _NODISCARD\n"));
assert_eq!(cooked.spellings(), "int f ( void ) ;");
assert_eq!(parse_rendered(&cooked.render()).get_errors(), []);
}
#[test]
fn a_token_out_of_a_body_maps_back_to_the_define() {
let source = "#define API __declspec(dllexport)\nAPI int f(void);\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let rendered = cook(source, &tokens).render();
let offset = rendered
.text
.find("__declspec")
.expect("it is in the rendering");
let written = rendered
.written_at(offset)
.expect("it was written somewhere");
assert_eq!(
&source[written.start_offset..written.end_offset()],
"__declspec"
);
assert!(
written.start_offset < source.find("API int").unwrap(),
"the body's own position is inside the `#define`, before the call site"
);
let offset = rendered.text.find("int").expect("it is in the rendering");
let written = rendered
.written_at(offset)
.expect("it was written somewhere");
assert_eq!(&source[written.start_offset..written.end_offset()], "int");
}
#[test]
fn a_pasted_token_maps_to_the_call_site() {
let source = "#define P(a, b) a##b\nint xy = P(x, y);\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let rendered = cook(source, &tokens).render();
let offset = rendered.text.rfind("xy").expect("the pasted token");
let written = rendered.written_at(offset).expect("the call site is known");
let at = &source[written.start_offset..written.end_offset()];
assert!(
written.start_offset >= source.find("P(x").unwrap(),
"a pasted token points at the call site it was joined at, not at the argument text: {at:?}"
);
}
#[test]
fn a_node_maps_to_the_span_its_tokens_cover() {
let source = "#define U unsigned\nU int a;\nU int b;\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let rendered = cook(source, &tokens).render();
let tree = parse_rendered(&rendered);
let declaration = tree
.get_red_root()
.descendants()
.find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind()) == cpp_parser::CppSyntaxKind::Declaration
})
.expect("a declaration is in the rendered tree");
let range = cpp_parser::source_range(declaration.text_range());
let written = rendered
.written_span(range)
.expect("it came from somewhere");
assert!(
&source[written.start_offset..written.end_offset()].contains("int a"),
"the node's span covers its own tokens: {:?}",
&source[written.start_offset..written.end_offset()]
);
}
fn environment(entries: &[(&str, bool, &str)]) -> cpp_parser::MacroEnvironment {
cpp_parser::MacroEnvironment::from_included_macros(entries.iter().map(
|(name, function_like, body)| {
cpp_parser::IncludedMacro::defined_with_body(
0,
name,
*function_like,
cpp_parser::MacroBody::Unknown,
Some(body),
)
},
))
}
#[test]
fn a_macro_from_an_included_header_is_expanded_by_the_cooked_stream() {
let source = "_STD_BEGIN\nclass W { int a; };\n_STD_END\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let configuration = cpp_code_analysis::configuration_from_environment(&environment(&[
("_STD_BEGIN", false, "namespace std {"),
("_STD_END", false, "}"),
]));
assert_eq!(configuration.function_like_without_parameters, 0);
assert_eq!(configuration.without_a_body, 0);
assert_eq!(configuration.unreadable, 0);
let rendered = cpp_code_analysis::cook_with(source, &tokens, &configuration.table).render();
assert!(
spelled(&rendered.text).starts_with(&[
"namespace".to_string(),
"std".to_string(),
"{".to_string()
]),
"the body of the included macro is what the parser sees: {:?}",
rendered.text
);
assert_eq!(parse_rendered(&rendered).get_errors(), []);
}
#[test]
fn a_function_like_definition_without_its_parameters_is_counted_and_not_guessed() {
let configuration = cpp_code_analysis::configuration_from_environment(&environment(&[
("_Success_", true, "((void)0)"),
("_In_", false, ""),
]));
assert_eq!(configuration.function_like_without_parameters, 1);
assert_eq!(
configuration.table.defined_names(),
["_In_"],
"the object-like one is in the table and the function-like one is not"
);
}
#[test]
fn a_definition_with_no_body_stored_is_counted_separately() {
let environment = cpp_parser::MacroEnvironment::from_included_macros([
cpp_parser::IncludedMacro::defined_with_body(
0,
"NO_BODY",
false,
cpp_parser::MacroBody::Unknown,
None,
),
]);
let configuration = cpp_code_analysis::configuration_from_environment(&environment);
assert_eq!(configuration.without_a_body, 1);
assert!(configuration.table.defined_names().is_empty());
}
#[test]
fn an_object_like_body_in_force_is_used_by_default_and_can_be_turned_off() {
let environment =
cpp_parser::MacroEnvironment::from_included_macros([]).with_bodies_in_force([
(
Box::from("_ACRTIMP"),
Some(false),
Box::from("__declspec(dllimport)"),
),
(Box::from("_Success_"), Some(true), Box::from("((void)0)")),
(Box::from("_Unknown_"), None, Box::from("int")),
]);
let source = "_ACRTIMP int f(void);\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let default = cpp_code_analysis::configuration_from_environment(&environment);
assert_eq!(default.in_force_without_a_parameter_list, 2);
let rendered = cpp_code_analysis::cook_with(source, &tokens, &default.table).render();
assert!(
spelled(&rendered.text).starts_with(&[
"__declspec".to_string(),
"(".to_string(),
"dllimport".to_string(),
")".to_string(),
"int".to_string(),
"f".to_string(),
]),
"{:?}",
rendered.text
);
let conservative = cpp_code_analysis::configuration_from_environment_with(&environment, false);
assert_eq!(conservative.in_force_without_a_parameter_list, 3);
let rendered = cpp_code_analysis::cook_with(source, &tokens, &conservative.table).render();
assert!(rendered.text.starts_with("_ACRTIMP"), "{:?}", rendered.text);
}
#[test]
fn a_cook_over_two_layers_answers_like_the_two_merged() {
let seed = defines("#define MY_API 1\n#define ONLY_IN_THE_SEED 2\n#define LATE(x) seed_##x\n");
let environment = defines("#define MY_API 3\n");
let mut merged = seed.clone();
merged.extend_from(&environment);
let source = "MY_API + ONLY_IN_THE_SEED + LATE(1)\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let layered = cpp_code_analysis::Over::new(&environment, &seed);
let from_layers = cpp_code_analysis::cook_with(source, &tokens, &layered).render();
let from_the_merge = cpp_code_analysis::cook_with(source, &tokens, &merged).render();
assert_eq!(from_layers.text, from_the_merge.text);
assert_eq!(
spelled(&from_layers.text).join(" "),
"3 + 2 + seed_1",
"the newer layer's `MY_API` wins, the seed answers for what the newer layer never mentions, and both \
directions expand"
);
}
#[test]
fn a_fact_that_cannot_be_placed_is_dropped_and_counted() {
let source = "int declared_here;\n";
let mut summary = cpp_code_analysis::summarize(
std::path::Path::new("/p/a.h"),
source,
cpp_code_analysis::SummaryKey::new(0, 0),
);
assert_eq!(
summary.declarations.len(),
1,
"the fixture declares one thing"
);
let empty = cpp_code_analysis::RenderedCooked::default();
let report = summary.map_into_the_file(&empty);
assert!(
summary.declarations.is_empty(),
"nothing is left pointing into a rendering"
);
assert_eq!(report.placed, 0);
assert_eq!(
report.dropped, 2,
"the declaration's range and its name's range: the report counts ranges"
);
}
#[test]
fn a_live_error_and_warning_are_reported_as_what_the_file_said() {
let source = "#define FLAG 1\n#if FLAG\n#error IT_FIRED\n#endif\n#if 0\n#error IN_A_DEAD_BRANCH\n#endif\n#warning A_WORD\nint x;\n";
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
let cooked = cpp_code_analysis::cook_with(source, &tokens, &defines(""));
let said: Vec<String> = cooked
.messages
.iter()
.map(|(kind, message, _)| format!("{kind:?}:{message}"))
.collect();
assert_eq!(
said,
vec!["Error:IT_FIRED".to_string(), "Warning:A_WORD".to_string()],
"the live message fires and the dead branch's does not: {said:?}"
);
}