use cpp_code_analysis::macros::MacroTable;
use cpp_code_analysis::preprocess::cooked::cook_with;
fn cooked(source: &str) -> String {
let (tokens, _) = cpp_parser::lex(source, &cpp_parser::LexerConfig::default());
cook_with(source, &tokens, &MacroTable::new()).render().text
}
#[test]
fn an_invocation_with_empty_arguments_expands() {
let source = "#define F(a, b, c, d) int a##b##c##d;\nF(X, , , )\n";
let rendered = cooked(source);
assert!(
!rendered.contains("F("),
"the invocation was expanded: {rendered:?}"
);
assert!(
rendered.contains("int X"),
"and its arguments were substituted: {rendered:?}"
);
}
#[test]
fn a_variadic_invocation_with_an_empty_argument_expands() {
let source = "#define NON_MEMBER_CALL(NAME, ...) struct NAME##_tag {};\n\
NON_MEMBER_CALL(_MEMFUNPTR, , , )\n\
NON_MEMBER_CALL(_MEMFUNPTR, , , noexcept)\n";
let rendered = cooked(source);
assert!(
!rendered.contains("NON_MEMBER_CALL("),
"both invocations were expanded: {rendered:?}"
);
assert!(
rendered.matches("struct _MEMFUNPTR_tag").count() == 2,
"twice, once per invocation: {rendered:?}"
);
}
#[test]
fn empty_arguments_do_not_swallow_the_next_one() {
let source = "#define SHOW(a, b, c) [a][b][c]\nSHOW(X, , noexcept)\n";
let rendered = cooked(source);
assert!(!rendered.contains("SHOW("), "expanded: {rendered:?}");
assert!(
rendered.contains("[noexcept]"),
"the third argument is `noexcept`, not lost to the empty second: {rendered:?}"
);
}