use cpp_parser::CppSyntaxKind;
use super::CheckContext;
pub(crate) struct Annotated {
pub macro_name: String,
pub name: cpp_parser::CppSyntaxNode,
pub name_in_the_file: cpp_parser::SourceRange,
pub body_in_the_file: Option<(usize, usize)>,
pub has_a_class_body: bool,
}
pub(crate) fn annotated_declarations(
context: &CheckContext<'_>,
macros: &[&str],
) -> Vec<Annotated> {
let mut found = Vec::new();
for node in context.own_syntax_nodes() {
if CppSyntaxKind::from(node.kind()) != CppSyntaxKind::Declaration {
continue;
}
let is_an_enum = node
.descendants_with_tokens()
.filter_map(|element| element.into_token())
.any(|token| {
cpp_parser::CppTokenKind::from(token.kind())
== cpp_parser::CppTokenKind::EnumKeyword
});
let body = node
.descendants()
.find(|child| match CppSyntaxKind::from(child.kind()) {
CppSyntaxKind::ClassBody => true,
CppSyntaxKind::EnumDef => true,
CppSyntaxKind::CompoundStat => is_an_enum,
_ => false,
});
let Some(body) = body else {
continue;
};
let Some(name) = declared_name(&node) else {
continue;
};
let declaration_in_the_file =
context.in_the_file(cpp_parser::source_range(node.text_range()));
let Some(macro_name) =
the_annotation_before(context.written, declaration_in_the_file.start_offset)
else {
continue;
};
if !macros.contains(¯o_name.as_str()) {
continue;
}
let name_in_the_file = context.in_the_file(cpp_parser::source_range(name.text_range()));
let body_in_the_file = context.in_the_file(cpp_parser::source_range(body.text_range()));
found.push(Annotated {
macro_name,
name: name.clone(),
name_in_the_file,
body_in_the_file: Some((body_in_the_file.start_offset, body_in_the_file.end_offset())),
has_a_class_body: CppSyntaxKind::from(body.kind()) == CppSyntaxKind::ClassBody,
});
}
found
}
fn the_annotation_before(written: &str, at: usize) -> Option<String> {
let before = written.get(..at)?;
let trimmed = before.trim_end();
if !trimmed.ends_with(')') {
return None;
}
let bytes = trimmed.as_bytes();
let mut depth = 0usize;
let mut index = bytes.len();
while index > 0 {
index -= 1;
match bytes[index] {
b')' => depth += 1,
b'(' => {
depth -= 1;
if depth == 0 {
break;
}
}
_ => {}
}
}
if depth != 0 {
return None;
}
let head = trimmed[..index].trim_end();
let start = head
.rfind(|ch: char| !(ch.is_alphanumeric() || ch == '_'))
.map(|offset| offset + 1)
.unwrap_or(0);
let identifier = &head[start..];
if identifier.is_empty()
|| identifier
.chars()
.next()
.is_some_and(|ch| ch.is_ascii_digit())
{
return None;
}
Some(identifier.to_string())
}
pub(crate) fn declared_name(
declaration: &cpp_parser::CppSyntaxNode,
) -> Option<cpp_parser::CppSyntaxNode> {
declaration
.descendants()
.find(|node| CppSyntaxKind::from(node.kind()) == CppSyntaxKind::NameExpr)
}
pub(crate) fn the_type_written_before(
written: &str,
start: usize,
name_start: usize,
) -> Option<String> {
let mut text = written.get(start..name_start)?.trim();
loop {
let trimmed = text.trim_start();
let Some(paren) = trimmed.find('(') else {
break;
};
let head = trimmed[..paren].trim_end();
let is_a_macro_name = !head.is_empty()
&& head
.chars()
.all(|ch| ch.is_alphanumeric() || ch == '_' || ch == ':')
&& head.chars().next().is_some_and(|ch| !ch.is_ascii_digit());
if !is_a_macro_name {
break;
}
let mut depth = 0usize;
let mut close = None;
for (offset, ch) in trimmed.char_indices().skip(paren) {
match ch {
'(' => depth += 1,
')' => {
depth -= 1;
if depth == 0 {
close = Some(offset);
break;
}
}
_ => {}
}
}
let Some(close) = close else {
break;
};
text = &trimmed[close + 1..];
}
Some(text.trim().to_string())
}