use cpp_parser::{CppAstNode, CppNamespaceDecl, CppParser, ParserConfig};
fn parse(text: &str) -> cpp_parser::CppSyntaxTree {
CppParser::parse(text, ParserConfig::default())
}
fn at_file_scope(tree: &cpp_parser::CppSyntaxTree, text: &str, name: &str) -> bool {
let root = tree.get_red_root();
let at = text.find(name).expect("the fixture names it");
let mut found = None;
for node in root.descendants() {
let range = cpp_parser::source_range(node.text_range());
if range.start_offset <= at && at <= range.end_offset() {
found = Some(node);
}
}
let mut node = found;
while let Some(current) = node {
if CppNamespaceDecl::cast(current.clone()).is_some() {
return false;
}
node = current.parent();
}
true
}
#[test]
fn extern_cxx_before_a_class_is_a_declaration() {
let text = "namespace std { extern \"C++\" template <class _Elem> class basic_ios; } int after_this;\n";
let tree = parse(text);
assert!(
tree.get_errors().is_empty(),
"no error: {:?}",
tree.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range).start_offset)
.collect::<Vec<_>>()
);
assert!(
at_file_scope(&tree, text, "after_this"),
"the namespace closed"
);
}
#[test]
fn export_before_a_language_linkage_declaration_is_a_declaration() {
let text = "namespace std { export extern \"C++\" template <class _Elem> class basic_ios; } int after_this;\n";
let tree = parse(text);
assert!(
tree.get_errors().is_empty(),
"no error: {:?}",
tree.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range).start_offset)
.collect::<Vec<_>>()
);
assert!(
at_file_scope(&tree, text, "after_this"),
"the namespace closed"
);
}
#[test]
fn a_run_of_exported_language_linkage_declarations_keeps_the_namespace_closed() {
let text = "namespace std { export using streamoff = long long; export template <class _Statetype> class fpos; \
export extern \"C++\" class ios_base; \
export extern \"C++\" template <class _Elem, class _Traits = char_traits<_Elem>> class basic_ios; \
export extern \"C++\" template <class _Elem, class _Traits = char_traits<_Elem>> class basic_streambuf; \
} int after_this;\n";
let tree = parse(text);
assert!(
tree.get_errors().is_empty(),
"no error: {:?}",
tree.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range).start_offset)
.collect::<Vec<_>>()
);
assert!(
at_file_scope(&tree, text, "after_this"),
"the namespace closed"
);
}
#[test]
fn an_attribute_after_the_class_key_is_an_attribute() {
let text = "namespace std { class [[deprecated(\"old\")]] Widget { public: int size; }; } int after_this;\n";
let tree = parse(text);
assert!(
tree.get_errors().is_empty(),
"no error: {:?}",
tree.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range).start_offset)
.collect::<Vec<_>>()
);
assert!(
at_file_scope(&tree, text, "after_this"),
"the namespace closed"
);
}
#[test]
fn an_exported_template_class_with_an_attribute_keeps_the_namespace_closed() {
let text = "namespace std { export template <class _Elem, unsigned long _Mymax = 0x10ffff> \
class [[deprecated(\"warning STL4017: \")]] codecvt_utf8 { public: explicit codecvt_utf8(size_t _Refs = 0); \
~codecvt_utf8() noexcept {} }; } int after_this;\n";
let tree = parse(text);
assert!(
tree.get_errors().is_empty(),
"no error: {:?}",
tree.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range).start_offset)
.collect::<Vec<_>>()
);
assert!(
at_file_scope(&tree, text, "after_this"),
"the namespace closed"
);
}
#[test]
fn a_pragma_operator_at_declaration_position_is_a_declaration() {
let text = "namespace std { _Pragma(\"warning(push)\") _Pragma(\"warning(disable : 4996)\") class Widget { public: int size; }; } int after_this;\n";
let tree = parse(text);
assert!(
tree.get_errors().is_empty(),
"no error: {:?}",
tree.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range).start_offset)
.collect::<Vec<_>>()
);
assert!(
at_file_scope(&tree, text, "after_this"),
"the namespace closed"
);
}
#[test]
fn the_deprecated_template_class_of_the_standard_library_keeps_the_namespace_closed() {
let text = "namespace std { _Pragma(\"warning(push)\") _Pragma(\"warning(disable : 4996)\") export template <class _Codecvt, class _Elem = wchar_t, class _Traits = char_traits<_Elem>> class [[deprecated(\"warning STL4017: \" \"std::wbuffer_convert, std::wstring_convert, and the <codecvt> header are deprecated.\")]] wstring_convert { public: wstring_convert() : _State() {} }; } int after_this;\n";
let tree = parse(text);
assert!(
tree.get_errors().is_empty(),
"no error: {:?}",
tree.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range).start_offset)
.collect::<Vec<_>>()
);
assert!(
at_file_scope(&tree, text, "after_this"),
"the namespace closed"
);
}
#[test]
fn which_piece_of_the_deprecated_line_breaks_it() {
let variants = [
(
"pragma then class",
"namespace std { _Pragma(\"a\") class W { }; } int after_this;",
),
(
"pragma then export class",
"namespace std { _Pragma(\"a\") export class W { }; } int after_this;",
),
(
"pragma then export template class",
"namespace std { _Pragma(\"a\") export template <class T> class W { }; } int after_this;",
),
(
"export template class with attribute",
"namespace std { export template <class T> class [[deprecated(\"x\")]] W { }; } int after_this;",
),
(
"pragma then export template class with attribute",
"namespace std { _Pragma(\"a\") export template <class T> class [[deprecated(\"x\")]] W { }; } int after_this;",
),
(
"two pragmas then export template class",
"namespace std { _Pragma(\"a\") _Pragma(\"b\") export template <class T> class W { }; } int after_this;",
),
];
let mut broken = Vec::new();
for (name, text) in variants {
let tree = parse(text);
if !tree.get_errors().is_empty() || !at_file_scope(&tree, text, "after_this") {
broken.push((
name,
tree.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range).start_offset)
.collect::<Vec<_>>(),
));
}
}
assert!(broken.is_empty(), "broken variants: {broken:?}");
}
#[test]
fn an_unexpanded_macro_invocation_at_declaration_position_is_not_an_error() {
let text = "_NON_MEMBER_CALL(_IS_MEMFUNPTR_EXPLICIT_THIS_GUIDES, , , )\n\
_NON_MEMBER_CALL(_IS_MEMFUNPTR_EXPLICIT_THIS_GUIDES, , , noexcept)\n\
export template <class _Ty> inline constexpr bool is_member_function_pointer_v = false;\n\
int after_this;\n";
let tree = parse(text);
assert!(
tree.get_errors().is_empty(),
"no error: {:?}",
tree.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range).start_offset)
.collect::<Vec<_>>()
);
assert!(
at_file_scope(&tree, text, "after_this"),
"the file stayed at file scope"
);
}
#[test]
fn an_unexpanded_macro_invocation_in_a_namespace_keeps_it_closed() {
let text = "namespace std { _NON_MEMBER_CALL(X, , , noexcept) export template <class _Ty> bool v = false; } int after_this;\n";
let tree = parse(text);
assert!(
tree.get_errors().is_empty(),
"no error: {:?}",
tree.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range).start_offset)
.collect::<Vec<_>>()
);
assert!(
at_file_scope(&tree, text, "after_this"),
"the namespace closed"
);
}