use super::CheckContext;
use cpp_parser::CppAstNode;
pub(crate) struct Annotated {
pub macro_name: String,
pub name: String,
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.tree().children() {
if !cpp_parser::CppDeclaration::can_cast(node.kind().into()) {
continue;
}
let Some(first) = node.first_token() else {
continue;
};
if !context
.view
.was_written_here(cpp_parser::source_range(first.text_range()))
{
continue;
}
let enumeration = cpp_parser::typed_descendants::<cpp_parser::CppEnumDef>(&node)
.find(|definition| definition.get_body().is_some());
let body = enumeration
.as_ref()
.map(|definition| definition.syntax().clone())
.or_else(|| {
cpp_parser::typed_descendants::<cpp_parser::CppClassBody>(&node)
.next()
.map(|class_body| class_body.syntax().clone())
})
.or_else(|| {
enumeration.as_ref()?;
cpp_parser::typed_descendants::<cpp_parser::CppCompoundStat>(&node)
.next()
.map(|statement| statement.syntax().clone())
});
let Some(body) = body else {
continue;
};
let Some((name, name_range)) = 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(name_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: cpp_parser::CppClassBody::can_cast(body.kind().into()),
});
}
found
}
pub(crate) 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
.char_indices()
.rev()
.find(|(_, ch)| !(ch.is_alphanumeric() || *ch == '_'))
.map(|(offset, ch)| offset + ch.len_utf8())
.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<(String, cpp_parser::SourceRange)> {
use cpp_parser::{CppAstNode, CppAstToken, CppClassDef, CppDeclaration, CppEnumDef};
if let Some(class) = cpp_parser::typed_descendants::<CppClassDef>(declaration)
.find(|class| class.get_name().is_some())
{
let name = class.get_name()?;
return Some((
name.get_name_text().to_string(),
cpp_parser::source_range(name.get_range()),
));
}
if let Some(enumeration) = cpp_parser::typed_descendants::<CppEnumDef>(declaration)
.find(|enumeration| enumeration.get_body().is_some())
{
let name = enumeration.get_name()?;
return Some((
name.get_name_text().to_string(),
cpp_parser::source_range(name.get_range()),
));
}
let declaration = CppDeclaration::cast(declaration.clone())?;
let name = declaration.get_name()?;
Some((
name.get_name_text().to_string(),
cpp_parser::source_range(name.get_range()),
))
}
pub(crate) fn the_declared_name(
declaration: &cpp_parser::CppSyntaxNode,
) -> Option<(String, cpp_parser::SourceRange)> {
use cpp_parser::{CppAstNode, CppAstToken, CppDeclaration};
let declaration = CppDeclaration::cast(declaration.clone())?;
let name = declaration.get_name()?;
Some((
name.get_name_text().to_string(),
cpp_parser::source_range(name.get_range()),
))
}
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())
}