use cpp_code_analysis::{
Directive, DirectiveKind, FilePreprocessing, Include, IncludeForm, Lookup, MacroValues,
NoMacros, Token, Value, Visibility,
directive::parse_directive_tokens,
guard::{Branch, GuardStack},
macros::MacroTable,
preprocess::preprocess,
};
use cpp_parser::{CppParser, ParserConfig, source_range};
fn run(source: &str) -> FilePreprocessing {
let tree = CppParser::parse(source, ParserConfig::default());
assert_eq!(
tree.to_source_text(),
source,
"the parse must stay lossless before anything is read out of it"
);
preprocess(source, tree.get_tokens())
}
fn kinds(source: &str) -> Vec<DirectiveKind> {
run(source)
.directives
.iter()
.map(|spanned| spanned.directive.kind())
.collect()
}
fn first(source: &str, kind: DirectiveKind) -> Directive {
run(source)
.directives
.into_iter()
.find(|spanned| spanned.directive.kind() == kind)
.map(|spanned| spanned.directive)
.unwrap_or_else(|| panic!("no {kind:?} directive in {source:?}"))
}
fn directive(text: &str) -> Directive {
let tree = CppParser::parse(text, ParserConfig::default());
let node = tree
.get_red_root()
.descendants()
.find(|node| {
cpp_parser::CppSyntaxKind::from(node.kind())
== cpp_parser::CppSyntaxKind::PreprocessorDirective
})
.expect("a directive node");
parse_directive_tokens(
&cpp_code_analysis::token::tokens_of(&node),
source_range(node.text_range()),
)
}
fn macro_of(source: &str) -> cpp_code_analysis::MacroDef {
match first(source, DirectiveKind::Define) {
Directive::Define(define) => define
.macro_def
.unwrap_or_else(|| panic!("{source:?} defines something")),
other => panic!("expected a define, got {other:?}"),
}
}
#[test]
fn every_directive_kind_is_recognised() {
assert_eq!(
kinds(concat!(
"#include <vector>\n",
"#define A 1\n",
"#undef A\n",
"#if X\n",
"#elif Y\n",
"#else\n",
"#endif\n",
"#ifdef FOO\n",
"#ifndef BAR\n",
"#pragma once\n",
"#error nope\n",
"#warning careful\n",
"#line 3\n",
)),
vec![
DirectiveKind::Include,
DirectiveKind::Define,
DirectiveKind::Undef,
DirectiveKind::If,
DirectiveKind::Elif,
DirectiveKind::Else,
DirectiveKind::Endif,
DirectiveKind::Ifdef,
DirectiveKind::Ifndef,
DirectiveKind::Pragma,
DirectiveKind::Error,
DirectiveKind::Warning,
DirectiveKind::Line,
]
);
}
#[test]
fn an_unknown_directive_keeps_its_name() {
match directive("#whatever 1 2\n") {
Directive::Other { name } => assert_eq!(&*name, "whatever"),
other => panic!("expected Other, got {other:?}"),
}
}
#[test]
fn a_null_directive_is_recognised() {
assert_eq!(directive("#\n"), Directive::Null);
assert_eq!(directive("# \n"), Directive::Null);
}
#[test]
fn a_truncated_directive_is_read_as_far_as_it_goes() {
for text in [
"#\n",
"#if\n",
"#ifdef\n",
"#define\n",
"#include\n",
"#undef\n",
"#pragma\n",
"#error\n",
] {
let tree = CppParser::parse(text, ParserConfig::default());
assert_eq!(tree.to_source_text(), text, "{text:?}");
let _ = preprocess(text, tree.get_tokens());
}
}
#[test]
fn an_angle_include_is_an_angle_include() {
match first("#include <vector>\n", DirectiveKind::Include) {
Directive::Include(Include {
form,
target,
is_next,
..
}) => {
assert_eq!(form, IncludeForm::Angle);
assert_eq!(&*target, "vector");
assert!(!is_next);
}
other => panic!("expected an include, got {other:?}"),
}
}
#[test]
fn a_quoted_include_keeps_its_form() {
match first("#include \"local.h\"\n", DirectiveKind::Include) {
Directive::Include(Include { form, target, .. }) => {
assert_eq!(form, IncludeForm::Quote);
assert_eq!(&*target, "local.h");
}
other => panic!("expected an include, got {other:?}"),
}
}
#[test]
fn an_include_of_a_macro_is_not_malformed() {
match first("#include BOOST_HEADER\n", DirectiveKind::Include) {
Directive::Include(Include { form, target, .. }) => {
assert_eq!(form, IncludeForm::Macro);
assert_eq!(&*target, "BOOST_HEADER");
}
other => panic!("expected an include, got {other:?}"),
}
}
#[test]
fn include_next_is_distinguished_from_include() {
let preprocessing = run("#include_next <stdio.h>\n");
let directive = &preprocessing.directives[0].directive;
assert_eq!(directive.kind(), DirectiveKind::IncludeNext);
assert!(directive.as_include().expect("an include").is_next);
}
#[test]
fn an_unfolded_angle_include_names_the_file_between_its_delimiters() {
match first("# include <bits/c++config.h>\n", DirectiveKind::Include) {
Directive::Include(Include {
form,
target,
is_next,
..
}) => {
assert_eq!(form, IncludeForm::Angle);
assert_eq!(
&*target, "bits/c++config.h",
"the delimiters are not part of the name"
);
assert!(!is_next);
}
other => panic!("expected an include, got {other:?}"),
}
}
#[test]
fn an_unfolded_angle_include_keeps_the_text_verbatim() {
match first("# include <sys/a-b.h>\n", DirectiveKind::Include) {
Directive::Include(Include { target, .. }) => assert_eq!(&*target, "sys/a-b.h"),
other => panic!("expected an include, got {other:?}"),
}
}
#[test]
fn an_object_like_macro_has_no_parameters() {
let definition = macro_of("#define VERSION 3\n");
assert_eq!(&*definition.name, "VERSION");
assert_eq!(definition.params, None);
assert!(!definition.is_function_like());
assert_eq!(
definition
.body
.significant()
.map(|token| token.text())
.collect::<Vec<_>>(),
vec!["3"]
);
}
#[test]
fn a_function_like_macro_has_parameters() {
let definition = macro_of("#define MAX(a, b) ((a) > (b) ? (a) : (b))\n");
assert_eq!(&*definition.name, "MAX");
let params = definition.params.as_ref().expect("parameters");
assert_eq!(
params.iter().map(|p| &*p.name).collect::<Vec<_>>(),
vec!["a", "b"]
);
assert!(!definition.is_variadic());
assert_eq!(definition.min_arguments(), 2);
assert!(definition.accepts_argument_count(2));
assert!(!definition.accepts_argument_count(1));
assert!(!definition.accepts_argument_count(3));
}
#[test]
fn the_space_before_the_parenthesis_decides_the_kind_of_macro() {
let object_like = macro_of("#define A (1)\n");
assert_eq!(object_like.params, None);
assert!(!object_like.is_function_like());
let function_like = macro_of("#define A(x) x\n");
assert!(function_like.is_function_like());
assert_eq!(function_like.min_arguments(), 1);
}
#[test]
fn a_variadic_macro_is_recognised() {
let definition = macro_of("#define LOG(fmt, ...) printf(fmt, __VA_ARGS__)\n");
assert!(definition.is_variadic());
assert_eq!(definition.min_arguments(), 1);
assert!(definition.accepts_argument_count(1));
assert!(definition.accepts_argument_count(7));
assert!(!definition.accepts_argument_count(0));
}
#[test]
fn the_gnu_variadic_spelling_means_the_same_thing() {
let definition = macro_of("#define F(a...)\n");
assert!(definition.is_variadic());
assert_eq!(definition.min_arguments(), 0);
}
#[test]
fn a_macro_with_an_empty_body_is_recognised() {
let definition = macro_of("#define FEATURE_ON\n");
assert!(definition.body.is_empty());
assert_eq!(definition.body.significant().count(), 0);
}
#[test]
fn stringize_and_paste_are_recorded() {
let stringize = macro_of("#define STR(x) #x\n");
assert_eq!(stringize.body.stringize.len(), 1);
assert!(stringize.body.paste.is_empty());
let paste = macro_of("#define CAT(a, b) a ## b\n");
assert_eq!(paste.body.paste.len(), 1);
assert!(paste.body.stringize.is_empty());
assert!(paste.body.tokens[paste.body.paste[0]].is(cpp_parser::CppTokenKind::HashHash));
}
#[test]
fn a_hash_that_does_not_stringize_is_not_recorded() {
let definition = macro_of("#define HASHLIT(x) a # b\n");
assert!(
definition.body.stringize.is_empty(),
"the `#` is not followed by a parameter: {:?}",
definition.body
);
}
#[test]
fn a_line_splice_continues_a_macro_body() {
let definition = macro_of("#define BOTH(a, b) \\\n do { a; b; } while (0)\n");
let text: Vec<&str> = definition
.body
.significant()
.map(|token| token.text())
.collect();
assert_eq!(
text,
vec!["do", "{", "a", ";", "b", ";", "}", "while", "(", "0", ")"],
"the spliced newline is gone and the body is whole"
);
}
#[test]
fn a_comment_in_a_body_is_not_part_of_it() {
let definition = macro_of("#define X 1 /* gone */ + 2\n");
let text: Vec<&str> = definition
.body
.significant()
.map(|token| token.text())
.collect();
assert_eq!(text, vec!["1", "+", "2"]);
}
#[test]
fn a_macro_is_visible_after_its_definition() {
let preprocessing = run("#define FOO 1\nint x;\n");
assert!(preprocessing.macros.is_defined("FOO"));
assert!(!preprocessing.macros.is_defined("BAR"));
}
#[test]
fn undef_shadows_without_deleting() {
let preprocessing = run("#define FOO 1\n#undef FOO\n");
assert!(!preprocessing.macros.is_defined("FOO"));
let offset_inside_the_definition = 10;
assert!(
preprocessing
.macros
.is_defined_at("FOO", offset_inside_the_definition),
"the macro was defined at that offset"
);
}
#[test]
fn a_redefinition_shadows_the_earlier_one() {
let preprocessing = run("#define N 1\n#define N 2\n");
let definition = preprocessing.macros.get("N").expect("defined");
assert_eq!(
definition
.body
.significant()
.map(|token| token.text())
.collect::<Vec<_>>(),
vec!["2"]
);
assert_eq!(preprocessing.macros.len(), 1, "one name, not two");
}
#[test]
fn defined_names_are_sorted_and_deduplicated() {
let preprocessing = run("#define B 1\n#define A 1\n#define B 2\n");
assert_eq!(preprocessing.macros.defined_names(), vec!["A", "B"]);
}
fn no_macros() -> impl MacroValues {
cpp_code_analysis::condition::NoMacros
}
fn condition_value(source: &str, macros: &impl MacroValues) -> Value {
let directive = first(source, DirectiveKind::If);
match directive {
Directive::Conditional { condition, .. } => cpp_code_analysis::evaluate(&condition, macros),
other => panic!("expected a conditional, got {other:?}"),
}
}
struct HalfKnown;
impl MacroValues for HalfKnown {
fn lookup(&self, name: &str) -> cpp_code_analysis::Lookup<'_> {
match name {
"HERE" => cpp_code_analysis::Lookup::DefinedWithoutAValue,
"GONE" => cpp_code_analysis::Lookup::Undefined,
_ => cpp_code_analysis::Lookup::Unanswered,
}
}
}
#[test]
fn a_name_the_table_cannot_speak_about_is_not_zero() {
for condition in [
"#if FOO\n",
"#if defined(FOO)\n",
"#if !defined(FOO)\n",
"#if FOO == 0\n",
"#if defined(FOO) && defined(HERE)\n",
"#if !FOO\n",
"#if FOO ? 1 : 2\n",
] {
assert_eq!(
condition_value(condition, &HalfKnown),
Value::Unknown,
"{condition:?} asks about a name this table cannot speak about"
);
}
}
#[test]
fn a_table_that_read_part_of_the_input_still_answers_what_it_read() {
assert_eq!(
condition_value("#if defined(HERE)\n", &HalfKnown),
Value::Known(1),
"the body is not held, and `defined` does not need it"
);
assert_eq!(
condition_value("#if HERE\n", &HalfKnown),
Value::Unknown,
"but a *value* cannot be read out of a body the table does not have — not even as `1`"
);
assert_eq!(
condition_value("#if defined(GONE)\n", &HalfKnown),
Value::Known(0),
"an `#undef` this table read is a definite no"
);
assert_eq!(
condition_value("#if GONE\n", &HalfKnown),
Value::Known(0),
"and the standard's `0` is right there, because the file that undefined it was read"
);
assert_eq!(
condition_value("#if defined(HERE) && !defined(GONE)\n", &HalfKnown),
Value::Known(1),
"a condition over names the table read is decided, whatever it does not mention"
);
}
#[test]
fn an_ifdef_on_a_name_the_table_cannot_speak_about_is_undecided() {
let preprocessing = run("#ifdef FOO\nint x;\n#endif\n#if HERE\nint y;\n#endif\n");
let visibility = |offset: usize| preprocessing.guard_at(offset).visibility(&HalfKnown);
let at = |kind: DirectiveKind| {
preprocessing
.directives
.iter()
.find(|spanned| spanned.directive.kind() == kind)
.map(|spanned| spanned.range.end_offset())
.expect("the directive")
};
assert_eq!(
visibility(at(DirectiveKind::Ifdef)),
Visibility::Unknown,
"`#ifdef FOO`: FOO may be defined by a file this table has not read"
);
assert_eq!(
visibility(at(DirectiveKind::If)),
Visibility::Unknown,
"`#if HERE`: HERE is certainly a macro, and a condition with no readable value is unknown, not false"
);
}
#[test]
fn arithmetic_follows_c_precedence() {
let cases = [
("#if 1 + 2 * 3 == 7\n", true),
("#if (1 + 2) * 3 == 9\n", true),
("#if 1 << 4 == 16\n", true),
("#if 10 / 3 == 3\n", true),
("#if 10 % 3 == 1\n", true),
("#if 2 > 1 && 1 > 2\n", false),
("#if 2 > 1 || 1 > 2\n", true),
("#if !0\n", true),
("#if ~0 == -1\n", true),
("#if 1 ? 2 : 3\n", true),
("#if 0 ? 2 : 0\n", false),
];
for (source, expected) in cases {
assert_eq!(
condition_value(source, &no_macros()).is_true(),
Some(expected),
"{source:?}"
);
}
}
#[test]
fn integer_literals_are_read_the_way_a_preprocessor_reads_them() {
let cases = [
("#if 0x10 == 16\n", true),
("#if 010 == 8\n", true),
("#if 0b1010 == 10\n", true),
("#if 1'000 == 1000\n", true),
("#if 42ULL == 42\n", true),
];
for (source, expected) in cases {
assert_eq!(
condition_value(source, &no_macros()).is_true(),
Some(expected),
"{source:?}"
);
}
}
#[test]
fn an_undefined_identifier_is_zero_and_not_an_error() {
assert_eq!(condition_value("#if FOO\n", &no_macros()), Value::Known(0));
assert_eq!(
condition_value("#if defined(FOO)\n", &no_macros()),
Value::Known(0)
);
assert_eq!(
condition_value("#if !defined(FOO)\n", &no_macros()),
Value::Known(1)
);
}
#[test]
fn defined_works_without_parentheses() {
let mut macros = MacroTable::new();
macros.define(macro_of("#define FOO 1\n"));
assert_eq!(
condition_value("#if defined FOO\n", ¯os),
Value::Known(1)
);
}
#[test]
fn defined_does_not_expand_its_operand() {
let mut macros = MacroTable::new();
macros.define(macro_of("#define FOO NOT_DEFINED_AT_ALL\n"));
assert_eq!(
condition_value("#if defined(FOO)\n", ¯os),
Value::Known(1)
);
}
#[test]
fn a_numeric_macro_evaluates_to_its_value() {
let mut macros = MacroTable::new();
macros.define(macro_of("#define VERSION 3\n"));
assert_eq!(
condition_value("#if VERSION > 2\n", ¯os),
Value::Known(1)
);
assert_eq!(
condition_value("#if VERSION > 3\n", ¯os),
Value::Known(0)
);
}
#[test]
fn a_macros_body_is_read_as_an_expression() {
let mut macros = MacroTable::new();
macros.define(macro_of("#define _MSC_VER (1900 + 1)\n"));
assert_eq!(
condition_value("#if _MSC_VER > 1900\n", ¯os),
Value::Known(1),
"`(1900 + 1) > 1900` is an expression, and a body is one too"
);
let mut macros = MacroTable::new();
macros.define(macro_of("#define ASSERT_ALL(x) do { } while (0)\n"));
macros.define(macro_of("#define WEIRD int x\n"));
assert_eq!(condition_value("#if WEIRD\n", ¯os), Value::Unknown);
assert_eq!(
condition_value("#if WEIRD\n", ¯os).is_true(),
None,
"and `is_true` says so rather than guessing"
);
}
#[test]
fn logical_operators_short_circuit() {
let mut macros = MacroTable::new();
macros.define(macro_of("#define X SOMETHING\n"));
assert_eq!(
condition_value("#if defined(NOPE) && X > 2\n", ¯os),
Value::Known(0),
"the left side is false, so the right side is never evaluated"
);
assert_eq!(
condition_value("#if defined(X) || X > 2\n", ¯os),
Value::Known(1),
"the left side is true, so the right side is never evaluated"
);
}
#[test]
fn division_by_zero_is_unknown_rather_than_a_panic() {
assert_eq!(condition_value("#if 1 / 0\n", &no_macros()), Value::Unknown);
assert_eq!(condition_value("#if 1 % 0\n", &no_macros()), Value::Unknown);
}
#[test]
fn a_condition_that_does_not_parse_is_unknown() {
for source in ["#if\n", "#if 1 +\n", "#if (1\n", "#if defined(\n"] {
assert_eq!(
condition_value(source, &no_macros()),
Value::Unknown,
"{source:?}"
);
}
}
#[test]
fn true_and_false_are_identifiers_in_a_condition() {
assert_eq!(condition_value("#if true\n", &no_macros()), Value::Known(0));
let mut macros = MacroTable::new();
macros.define(macro_of("#define true 1\n"));
assert_eq!(condition_value("#if true\n", ¯os), Value::Known(1));
}
#[test]
fn a_macro_may_be_named_after_a_keyword() {
let definition = macro_of("#define true 1\n");
assert_eq!(&*definition.name, "true");
let preprocessing = run("#define private public\n");
assert!(
preprocessing.macros.is_defined("private"),
"the definition is in the table: {:?}",
preprocessing.macros.defined_names()
);
}
#[test]
fn a_literal_is_not_a_macro_name() {
let preprocessing = run("#define 1 2\n");
assert!(
preprocessing.macros.is_empty(),
"nothing was defined: {:?}",
preprocessing.macros.defined_names()
);
assert_eq!(
preprocessing.directives[0].directive.kind(),
DirectiveKind::Define,
"but the directive is still a #define, and still in the tree"
);
}
#[test]
fn code_at_file_scope_is_active() {
let preprocessing = run("int x;\n");
assert!(preprocessing.directives.is_empty());
let guard = preprocessing.guard_at(0);
assert!(guard.is_empty());
assert_eq!(guard.visibility(&no_macros()), Visibility::Active);
}
#[test]
fn code_inside_if_zero_is_inactive() {
let preprocessing = run("#if 0\nint x;\n#endif\nint y;\n");
let offset_of_x = 8;
assert_eq!(
preprocessing.visibility_at(offset_of_x),
Visibility::Inactive
);
}
#[test]
fn an_undecidable_guard_is_reachable_but_not_diagnosable() {
let preprocessing = run("#define _MSC_VER int x\n#if _MSC_VER > 1900\nint x;\n#endif\n");
let offset_of_x = preprocessing
.directives
.iter()
.find(|spanned| spanned.directive.kind() == DirectiveKind::Endif)
.map(|spanned| spanned.range.start_offset)
.expect("the #endif");
let visibility = preprocessing.visibility_at(offset_of_x);
assert_eq!(visibility, Visibility::Unknown);
assert!(visibility.is_reachable(), "it might be compiled");
assert!(
!visibility.is_diagnosable(),
"but do not report errors in it"
);
}
#[test]
fn an_undefined_identifier_makes_a_condition_decided_not_unknown() {
let preprocessing = run("#if _MSC_VER > 1900\nint x;\n#endif\n");
let offset_of_x = preprocessing
.directives
.iter()
.find(|spanned| spanned.directive.kind() == DirectiveKind::Endif)
.map(|spanned| spanned.range.start_offset)
.expect("the #endif");
assert_eq!(
preprocessing.visibility_at(offset_of_x),
Visibility::Inactive,
"0 > 1900 is false, and the standard says an undefined macro is 0"
);
}
#[test]
fn the_two_branches_of_an_if_are_decided_oppositely() {
let preprocessing = run("#if defined(WIN32)\nint win;\n#else\nint posix;\n#endif\n");
let mut macros = MacroTable::new();
macros.define(macro_of("#define WIN32 1\n"));
let win_offset = preprocessing
.directives
.iter()
.find(|spanned| spanned.directive.kind() == DirectiveKind::If)
.map(|spanned| spanned.range.end_offset())
.expect("the #if");
let elsewhere = preprocessing
.directives
.iter()
.find(|spanned| spanned.directive.kind() == DirectiveKind::Else)
.map(|spanned| spanned.range.end_offset())
.expect("the #else");
assert_eq!(
preprocessing.guard_at(win_offset).visibility(¯os),
Visibility::Active,
"WIN32 is defined, so the first branch is compiled"
);
assert_eq!(
preprocessing.guard_at(elsewhere).visibility(¯os),
Visibility::Inactive,
"and the second is not"
);
}
#[test]
fn an_outer_condition_settles_an_inner_one() {
let preprocessing = run("#if 0\n#if UNDECIDABLE\nint x;\n#endif\n#endif\n");
let offset_of_x = preprocessing
.directives
.iter()
.find(|spanned| spanned.directive.kind() == DirectiveKind::Endif)
.map(|spanned| spanned.range.start_offset)
.expect("the inner #endif");
assert_eq!(
preprocessing.visibility_at(offset_of_x),
Visibility::Inactive,
"`#if 0` decided it, so the undecidable inner condition never matters"
);
}
#[test]
fn a_guard_reports_its_depth() {
let preprocessing = run("#if A\n#if B\nint x;\n#endif\n#endif\n");
let ends: Vec<usize> = preprocessing
.directives
.iter()
.map(|spanned| spanned.range.end_offset())
.collect();
assert_eq!(preprocessing.guard_at(0).depth(), 0, "before anything");
assert_eq!(
preprocessing.guard_at(ends[0]).depth(),
1,
"inside the outer region"
);
assert_eq!(
preprocessing.guard_at(ends[1]).depth(),
2,
"inside both regions"
);
}
#[test]
fn an_unclosed_conditional_leaves_an_unclosed_guard() {
let preprocessing = run("#if A\nint x;\n");
assert!(
!preprocessing.unclosed_guard.is_empty(),
"the file ends inside a conditional"
);
}
#[test]
fn an_elif_is_an_alternative_to_the_branches_before_it() {
let mut stack = GuardStack::new();
let branch = |kind: DirectiveKind| {
Branch::new(kind, Vec::new(), None, cpp_parser::SourceRange::new(0, 1))
};
stack.observe(DirectiveKind::If, branch(DirectiveKind::If));
stack.observe(DirectiveKind::Elif, branch(DirectiveKind::Elif));
let guard = stack.guard();
assert_eq!(
guard.depth(),
1,
"an #elif continues the region, it does not nest"
);
assert_eq!(guard.regions()[0].branches.len(), 2);
assert_eq!(guard.regions()[0].active_branch, 1);
}
#[test]
fn an_else_does_not_change_the_depth() {
let sources = [
"#if 1\n#define A\n#else\n#define B\n#endif\n",
"#if 1\n#define A\n#elif 2\n#define B\n#else\n#define C\n#endif\n",
];
for source in sources {
let depths: Vec<(DirectiveKind, usize)> = run(source)
.directives
.iter()
.map(|spanned| (spanned.directive.kind(), spanned.condition_depth))
.collect();
assert_eq!(
depths[0],
(DirectiveKind::If, 0),
"the `#if` is at file scope: {depths:?}"
);
assert_eq!(
depths[1],
(DirectiveKind::Define, 1),
"and so its body is one deeper: {depths:?}"
);
assert!(
depths
.iter()
.filter(|(kind, _)| matches!(kind, DirectiveKind::Define))
.all(|(_, depth)| *depth == 1),
"every branch's body is at the same depth: {depths:?}"
);
assert_eq!(
depths.last(),
Some(&(DirectiveKind::Endif, 0)),
"and the `#endif` closes the region: {depths:?}"
);
}
}
#[test]
fn nesting_inside_an_else_still_counts() {
let depths: Vec<(DirectiveKind, usize)> =
run("#if A\n#else\n#if B\n#define X\n#endif\n#endif\n")
.directives
.iter()
.map(|spanned| (spanned.directive.kind(), spanned.condition_depth))
.collect();
assert_eq!(
depths,
vec![
(DirectiveKind::If, 0),
(DirectiveKind::Else, 1),
(DirectiveKind::If, 1),
(DirectiveKind::Define, 2),
(DirectiveKind::Endif, 1),
(DirectiveKind::Endif, 0),
]
);
}
#[test]
fn a_define_in_an_else_branch_is_inside_a_guard() {
let preprocessing = run("#if 1\n#define A\n#else\n#define B\n#endif\n");
let offset = preprocessing
.directives
.iter()
.find(|spanned| {
matches!(spanned.directive.kind(), DirectiveKind::Define)
&& spanned
.directive
.defines()
.is_some_and(|def| &*def.name == "B")
})
.map(|spanned| spanned.range.start_offset)
.expect("the `#else` branch's define");
let guard = preprocessing.guard_at(offset + 1);
assert_eq!(
guard.depth(),
1,
"it is inside the region, not at file scope"
);
assert!(
matches!(
guard.visibility(&preprocessing.macros_at(offset)),
cpp_code_analysis::Visibility::Inactive
),
"and the branch it is in is the one not taken"
);
}
#[test]
fn preprocessing_never_panics() {
let cases = [
"#",
"#\n",
"##\n",
"#define\n",
"#define(\n",
"#define A(\n",
"#define A(\n",
"#define A(,)\n",
"#define A(a,\n",
"#define A(a, ...)\n",
"#define A(#)\n",
"#define A(##)\n",
"#define A(#x)\n",
"#if\n",
"#if #\n",
"#if 0x\n",
"#if 1/0\n",
"#if ((((((((((\n",
"#if defined\n",
"#if defined()\n",
"#if 'x'\n",
"#if \"x\"\n",
"#if ...\n",
"#include\n",
"#include <\n",
"#include \"\n",
"#include <a\n",
"#include \"a\n",
"#undef\n",
"#else\n",
"#endif\n",
"#endif\n#endif\n#endif\n",
"#if\n#else\n#else\n#endif\n",
"#define \\\n",
"#define A \\\n",
"#if \\\n",
"#pragma\n",
"#error\n",
"#error \"unterminated\n",
"#line\n",
"#line 999999999999999999999999\n",
"#if 99999999999999999999999999\n",
"#if 0b\n",
"#if 0x\n",
"#if 08\n",
"#if 1 ? 2\n",
"#if 1:2\n",
"#if )\n",
"#if ]\n",
];
for source in cases {
let preprocessing = run(source);
let _ = preprocessing.macros.len();
let _ = preprocessing.visibility_at(0);
let _ = preprocessing.guard_at(usize::MAX);
}
}
#[test]
fn every_token_points_inside_the_file() {
let source = "#define MAX(a, b) ((a) > (b) ? (a) : (b))\n#define F(x, ...) #x __VA_ARGS__\n";
let preprocessing = run(source);
for spanned in &preprocessing.directives {
let tokens: Vec<Token> = match &spanned.directive {
Directive::Define(define) => define
.macro_def
.as_ref()
.map(|definition| definition.body.tokens.clone())
.unwrap_or_default(),
Directive::Conditional { condition, .. } => condition.clone(),
Directive::Pragma { tokens } => tokens.clone(),
_ => Vec::new(),
};
for token in tokens {
assert!(
token.range.end_offset() <= source.len(),
"a token's range is inside the file: {token:?}"
);
}
}
}
fn table_of(source: &str) -> MacroTable {
run(source).macros
}
fn evaluate(condition: &str, macros: &impl MacroValues) -> Value {
let mut errors = Vec::new();
let mut lexer =
cpp_parser::CppLexer::new(condition, cpp_parser::LexerConfig::default(), &mut errors);
let tokens: Vec<Token> = lexer
.tokenize()
.into_iter()
.filter(|token| !cpp_parser::is_trivia(token.kind))
.map(|token| {
let text = &condition[token.range.start_offset..token.range.end_offset()];
Token::new(token.kind, text, token.range)
})
.collect();
cpp_code_analysis::condition::evaluate(&tokens, macros)
}
#[test]
fn a_condition_expands_a_name_whose_body_names_another() {
let table = table_of("#define B 3\n#define A B\n");
assert_eq!(evaluate("A", &table), Value::Known(3), "`A` is `B` is `3`");
assert_eq!(
evaluate("A + 1", &table),
Value::Known(4),
"and it is a value"
);
let table = table_of(
"#define PART_DESKTOP 0x1\n#define PART_APP 0x2\n#define DESKTOP (PART_DESKTOP \\\n | PART_APP)\n",
);
assert_eq!(
evaluate("DESKTOP", &table),
Value::Known(3),
"a body written across two lines is `0x1 | 0x2`"
);
let table = table_of(
"#define PART_DESKTOP 0x1\n\
#define PART_APP 0x2\n\
#define FAMILY (PART_DESKTOP \\\n | PART_APP)\n\
#define PARTITION(v) ((FAMILY & v) == v)\n",
);
assert_eq!(
evaluate("PARTITION ( PART_APP )", &table),
Value::Known(1),
"`(0x1 | 0x2) & 0x2` is `0x2`, so the partition holds"
);
}
#[test]
fn has_include_is_answered_from_a_search_or_not_at_all() {
struct OneDirectory(Vec<&'static str>);
impl OneDirectory {
fn has(&self, name: &str) -> bool {
self.0.contains(&name)
}
}
impl cpp_code_analysis::preprocess::cooked::HeaderSearch for OneDirectory {
fn has(&self, name: &str, _quoted: bool, _directory: &std::path::Path) -> bool {
self.0.contains(&name)
}
}
struct WithSearch(OneDirectory);
impl MacroValues for WithSearch {
fn lookup(&self, _name: &str) -> Lookup<'_> {
Lookup::Undefined
}
fn builtin_operator(&self, name: &str, operand: &str) -> Option<Value> {
if name != "__has_include" {
return None;
}
let (header, _quoted) =
cpp_code_analysis::preprocess::cooked::split_a_header_operand(operand)?;
Some(Value::Known(i128::from(self.0.has(header))))
}
}
let table = WithSearch(OneDirectory(vec!["vector", "local.h"]));
assert_eq!(
evaluate("__has_include(<vector>)", &table),
Value::Known(1),
"the header is in the search, so the branch that includes it is taken"
);
assert_eq!(
evaluate("__has_include(<span>)", &table),
Value::Known(0),
"and one that is genuinely absent is 0 — a search *was* made"
);
assert_eq!(
evaluate("__has_include(\"local.h\")", &table),
Value::Known(1),
"the quoted form is a different question and is asked the same way"
);
assert_eq!(
evaluate("__has_include(<vector>)", &NoMacros),
Value::Unknown,
"nobody looked, so the answer is not `false`"
);
assert_eq!(
evaluate("__has_include(HEADER)", &table),
Value::Unknown,
"a macro operand needs an expansion, which is another layer's job"
);
assert_eq!(
evaluate("__has_include(<vector>) && 1", &table),
Value::Known(1),
"an operator composes with the rest of a condition"
);
}
#[test]
fn a_header_operand_keeps_the_form_it_was_written_in() {
use cpp_code_analysis::preprocess::cooked::split_a_header_operand as split;
assert_eq!(split("<vector>"), Some(("vector", false)), "the angle form");
assert_eq!(
split("\"local.h\""),
Some(("local.h", true)),
"the quoted form"
);
assert_eq!(
split("<sys/types.h>"),
Some(("sys/types.h", false)),
"a path is still a name"
);
assert_eq!(split("HEADER"), None, "a macro operand is not a file name");
assert_eq!(
split("<>"),
None,
"an empty angle name is nothing to search for"
);
assert_eq!(split(""), None, "and neither is nothing");
assert_eq!(
split("<vector"),
None,
"an unterminated form is a file being typed"
);
}