use super::unreal::{the_annotation_before, the_declared_name};
use super::{Check, CheckContext, DiagnosticCode};
use cpp_parser::CppAstNode;
const ANNOTATIONS: &[&str] = &["UPROPERTY", "UFUNCTION"];
pub struct UeMacroScope;
impl Check for UeMacroScope {
const CODES: &'static [DiagnosticCode] = &[DiagnosticCode::UeMacroScope];
fn check(context: &mut CheckContext<'_>) {
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;
}
check_declaration(context, &node);
}
}
}
fn check_declaration(context: &mut CheckContext<'_>, declaration: &cpp_parser::CppSyntaxNode) {
for node in declaration.descendants() {
if !cpp_parser::CppDeclaration::can_cast(node.kind().into()) {
continue;
}
let Some(name) = the_declared_name(&node) else {
continue;
};
let in_the_file = context.in_the_file(cpp_parser::source_range(node.text_range()));
let Some(annotation) = the_annotation_before(context.written, in_the_file.start_offset)
else {
continue;
};
if !ANNOTATIONS.contains(&annotation.as_str()) {
continue;
}
let mut is_a_member = false;
for parent in node.ancestors() {
if cpp_parser::CppClassBody::can_cast(parent.kind().into()) {
is_a_member = true;
break;
}
if cpp_parser::CppCompoundStat::can_cast(parent.kind().into())
|| cpp_parser::CppParamList::can_cast(parent.kind().into())
{
break;
}
}
if is_a_member {
continue;
}
let (spelled, name_in_the_file) = (name.0.clone(), context.in_the_file(name.1));
context.add_in_the_file(
DiagnosticCode::UeMacroScope,
name_in_the_file,
spelled.clone(),
format!(
"`{spelled}` is annotated `{annotation}` and is not a class member — the build tool reflects members \
of a `UCLASS`/`USTRUCT`, so this annotation is never read: the property does not appear in the editor, \
the function is not callable from Blueprint"
),
);
}
}