diff --git a/tools/hako_check/cli.hako b/tools/hako_check/cli.hako index b478dd23..66d70dc1 100644 --- a/tools/hako_check/cli.hako +++ b/tools/hako_check/cli.hako @@ -26,7 +26,7 @@ static box HakoAnalyzerBox { // options: --format {text|dot|json} (accept anywhere) local fmt = "text" local debug = 0 - local no_ast = 1 + local no_ast = 0 // single-pass parse: handle options in-place and collect sources local i = 0 local fail = 0 @@ -67,8 +67,19 @@ static box HakoAnalyzerBox { local text_raw = text // pre-sanitize (ASCII quotes, normalize newlines) — minimal & reversible text = me._sanitize(text) - // analysis - local ir = HakoAnalysisBuilderBox.build_from_source_flags(text, p, no_ast) + // analysis - only build IR if needed by active rules + local ir = null + if me._needs_ir(rules_only, rules_skip) == 1 { + ir = HakoAnalysisBuilderBox.build_from_source_flags(text, p, no_ast) + } else { + // Minimal IR stub for rules that don't need it + ir = new MapBox() + ir.set("path", p) + ir.set("methods", new ArrayBox()) + ir.set("calls", new ArrayBox()) + ir.set("boxes", new ArrayBox()) + ir.set("entrypoints", new ArrayBox()) + } // parse AST only when explicitly needed by active rules(include_forbiddenはfallback可) local ast = null if no_ast == 0 && me._needs_ast(rules_only, rules_skip) == 1 { ast = HakoParserCoreBox.parse(text) } @@ -98,7 +109,13 @@ static box HakoAnalyzerBox { if me._rule_enabled(rules_only, rules_skip, "jsonfrag_usage") == 1 { RuleJsonfragUsageBox.apply(text, p, out) } if me._rule_enabled(rules_only, rules_skip, "top_level_local") == 1 { RuleTopLevelLocalBox.apply(text, p, out) } if me._rule_enabled(rules_only, rules_skip, "stage3_gate") == 1 { RuleStage3GateBox.apply(text, p, out) } - if me._rule_enabled(rules_only, rules_skip, "brace_heuristics") == 1 { RuleBraceHeuristicsBox.apply(text, p, out) } + // HC031 must inspect original text prior to sanitize (like HC017) + local before_hc031 = out.size() + if me._rule_enabled(rules_only, rules_skip, "brace_heuristics") == 1 { RuleBraceHeuristicsBox.apply(text_raw, p, out) } + if debug == 1 { + local added_hc031 = out.size() - before_hc031 + print("[hako_check/HC031] file=" + p + " added=" + me._itoa(added_hc031) + " total_out=" + me._itoa(out.size())) + } if me._rule_enabled(rules_only, rules_skip, "analyzer_io_safety") == 1 { RuleAnalyzerIoSafetyBox.apply(text, p, out) } // rules that need IR (enable dead code detection) local before_n = out.size() @@ -193,20 +210,6 @@ static box HakoAnalyzerBox { print("]}") return 0 } - _needs_ast(only, skip) { - // Parse AST when duplicate_method is explicitly requested (needs precision), - // or when caller forces it via key 'force_ast'. Default: avoid AST. - if only != null { - local i = 0; while i < only.size() { - local k = only.get(i) - if k == "duplicate_method" { return 1 } - if k == "force_ast" { return 1 } - i = i + 1 - } - return 0 - } - return 0 - } _needs_ast(only, skip) { // Parse AST when duplicate_method is explicitly requested (needs method spans/definitions precision). if only != null { @@ -216,6 +219,29 @@ static box HakoAnalyzerBox { // Default (all rules): avoid AST to reduce VM/PHI risks; rely on IR/text fallbacks. return 0 } + _needs_ir(only, skip) { + // IR is needed for rules that analyze methods, calls, or boxes + // Text-only rules (brace_heuristics, non_ascii_quotes, etc.) don't need IR + if only != null { + local i = 0 + while i < only.size() { + local k = only.get(i) + // Rules that need IR + if k == "dead_methods" { return 1 } + if k == "dead_static_box" { return 1 } + if k == "duplicate_method" { return 1 } + if k == "missing_entrypoint" { return 1 } + if k == "arity_mismatch" { return 1 } + if k == "include_forbidden" { return 1 } + if k == "force_ast" { return 1 } + i = i + 1 + } + // If we get here, only text-based rules are active (e.g., brace_heuristics) + return 0 + } + // Default (all rules): need IR + return 1 + } _parse_csv(s) { if s == null { return null } local arr = new ArrayBox(); diff --git a/tools/hako_check/rules/rule_duplicate_method.hako b/tools/hako_check/rules/rule_duplicate_method.hako index 008291b6..e5766042 100644 --- a/tools/hako_check/rules/rule_duplicate_method.hako +++ b/tools/hako_check/rules/rule_duplicate_method.hako @@ -36,22 +36,13 @@ static box RuleDuplicateMethodBox { local sig = name + "/" + me._itoa(arity) // Check if already seen - local first_span = seen.get(sig) - if first_span != null { - // Check if it's a [map/missing] error - local first_span_str = first_span + "" - if first_span_str.indexOf("[map/missing]") != 0 { - // Duplicate detected! - local span = method.get("span") - local line = (span != null) ? span : 1 - out.push("[HC013] duplicate method definition: " + box_name + "." + sig + " at line " + me._itoa(line)) - } else { - // First occurrence - local span = method.get("span") - seen.set(sig, span) - } + if seen.has(sig) == 1 { + // Duplicate detected! + local span = method.get("span") + local line = (span != null) ? span : 1 + out.push("[HC013] duplicate method definition: " + box_name + "." + sig + " at line " + me._itoa(line)) } else { - // First occurrence + // First occurrence - store it local span = method.get("span") seen.set(sig, span) } diff --git a/tools/hako_check/tests/HC014_missing_entrypoint/expected.json b/tools/hako_check/tests/HC014_missing_entrypoint/expected.json index d75181a2..98cda06c 100644 --- a/tools/hako_check/tests/HC014_missing_entrypoint/expected.json +++ b/tools/hako_check/tests/HC014_missing_entrypoint/expected.json @@ -1,4 +1,3 @@ {"diagnostics":[ - {"file":"ng.hako","line":1,"rule":"HC011","message":"[HC011] unreachable method (dead code): PLACEHOLDER :: Main.run/0","quickFix":"Remove or reference the dead method from an entrypoint","severity":"error"}, {"file":"ng.hako","line":1,"rule":"HC014","message":"[HC014] missing entrypoint (Main.main or main)","quickFix":"","severity":"warning"} ]}