use super::unreal::annotated_declarations;
use super::{Check, CheckContext, DiagnosticCode};
pub struct UeGeneratedHeader;
impl Check for UeGeneratedHeader {
const CODES: &'static [DiagnosticCode] = &[DiagnosticCode::UeGeneratedHeader];
fn check(context: &mut CheckContext<'_>) {
let annotated =
annotated_declarations(context, &["UCLASS", "USTRUCT", "UENUM", "UINTERFACE"]);
if annotated.is_empty() {
return;
}
let Some(last) = context.summary.includes.last() else {
report_missing(context, &annotated[0]);
return;
};
if is_a_generated_header(&last.spelling) {
return;
}
let spelling = last.spelling.clone();
context.add_in_the_file(
DiagnosticCode::UeGeneratedHeader,
last.range,
spelling.clone(),
format!(
"`{spelling}` is the last `#include`, and the generated header has to be — Unreal's build tool writes \
the reflection code into `*.generated.h` and the macros it defines are used by the declarations below, \
so the build fails when another header follows it"
),
);
}
}
fn report_missing(context: &mut CheckContext<'_>, first: &super::unreal::Annotated) {
let name = first.name.clone();
context.add_in_the_file(
DiagnosticCode::UeGeneratedHeader,
first.name_in_the_file,
name.clone(),
format!(
"`{name}` is declared with `{}` and this file includes no `*.generated.h` — the build tool writes its \
reflection code into that header, and the class does not build without it",
first.macro_name
),
);
}
fn is_a_generated_header(spelling: &str) -> bool {
spelling.ends_with(".generated.h") || spelling.ends_with(".generated.hpp")
}