Skip to main content

luaur_analyze_cli/functions/
report_module_result.rs

1use crate::enums::report_format::ReportFormat;
2use crate::functions::report_error::report_error;
3use crate::functions::report_warning::report_warning;
4use alloc::sync::Arc;
5use luaur_analysis::functions::attach_type_data::attach_type_data;
6use luaur_analysis::records::file_resolver::FileResolver;
7use luaur_analysis::records::frontend::Frontend;
8use luaur_analysis::records::module::Module;
9use luaur_analysis::type_aliases::module_name_file_resolver::ModuleName;
10use luaur_ast::functions::pretty_print_with_types_pretty_printer_alt_b::pretty_print_with_types_ast_stat_block;
11
12/// C++ `static bool reportModuleResult(Frontend& frontend, const ModuleName& name, ReportFormat format, bool annotate)`
13/// (`CLI/src/Analyze.cpp:91-129`).
14pub fn report_module_result(
15    frontend: &mut Frontend,
16    name: &ModuleName,
17    format: ReportFormat,
18    annotate: bool,
19) -> bool {
20    // std::optional<CheckResult> cr = frontend.getCheckResult(name, false);
21    let cr = frontend.get_check_result(name, false, false);
22
23    let cr = match cr {
24        None => {
25            eprintln!("Failed to find result for {}", name);
26            return false;
27        }
28        Some(cr) => cr,
29    };
30
31    // if (!frontend.getSourceModule(name))
32    if frontend.get_source_module(name).is_null() {
33        eprintln!("Error opening {}", name);
34        return false;
35    }
36
37    // for (auto& error : cr->errors) reportError(frontend, format, error);
38    for error in &cr.errors {
39        report_error(frontend, format, error);
40    }
41
42    // std::string humanReadableName = frontend.fileResolver->getHumanReadableModuleName(name);
43    let human_readable_name =
44        unsafe { FileResolver::get_human_readable_module_name(frontend.file_resolver, name) };
45
46    // for (auto& error : cr->lintResult.errors) reportWarning(format, humanReadableName.c_str(), error);
47    for error in &cr.lint_result.errors {
48        report_warning(format, &human_readable_name, error);
49    }
50    // for (auto& warning : cr->lintResult.warnings) reportWarning(format, humanReadableName.c_str(), warning);
51    for warning in &cr.lint_result.warnings {
52        report_warning(format, &human_readable_name, warning);
53    }
54
55    if annotate {
56        // SourceModule* sm = frontend.getSourceModule(name);
57        // ModulePtr m = frontend.moduleResolver.getModule(name);
58        let sm = frontend.get_source_module_mut(name);
59        let module = frontend.module_resolver.get_module(name);
60
61        // attachTypeData(*sm, *m);
62        // ModulePtr is Arc<Module>; attachTypeData mutates the Module, mirroring the
63        // C++ `*m` dereference of the shared module pointer.
64        let module_ptr = Arc::as_ptr(&module) as *mut Module;
65        unsafe {
66            attach_type_data(&mut *sm, &mut *module_ptr);
67
68            // std::string annotated = prettyPrintWithTypes(*sm->root);
69            let annotated = pretty_print_with_types_ast_stat_block(&mut *(*sm).root);
70
71            // printf("%s", annotated.c_str());
72            print!("{}", annotated);
73        }
74    }
75
76    // return cr->errors.empty() && cr->lintResult.errors.empty();
77    cr.errors.is_empty() && cr.lint_result.errors.is_empty()
78}