aft icon indicating copy to clipboard operation
aft copied to clipboard

.h files are parsed with the C grammar: C++ headers land in skipped_files as parse_error in aft_outline

Open trim21 opened this issue 1 month ago • 2 comments

What happened?

A .h file that compiles cleanly as C++ never gets an outline: aft_outline reports it in skipped_files with reason: "parse_error". The file is being parsed with the C grammar, not the C++ one.

Observed: a header containing namespace, template <typename T> class Foo : public Base and public: access specifiers is skipped as parse_error. Expected: the same symbols aft_outline returns for a .hpp file with identical content.

Renaming the file to .hpp is the only workaround today — aft_outline has no language override.

Root cause (verified against commit e2dcaf60e)

  1. detect_language maps .h to LangId::C — crates/aft/src/parser.rs:777:

    "c" | "h" => Some(LangId::C),
    "cc" | "cpp" | "cxx" | "hpp" | "hh" => Some(LangId::Cpp),
    

    so the file is parsed with tree_sitter_c::LANGUAGE and matched against C_QUERY (parser.rs:817, parser.rs:248).

  2. tree-sitter-c-0.24.2/src/grammar.json has no class_specifier, namespace_definition, template_declaration or access_specifier nodes at all; tree-sitter-cpp-0.23.4 does (2 / 3 / 5 / 8 occurrences). Any C++-specific construct therefore produces an ERROR node.

  3. outline_skip_reason runs validate_syntax → FileParser::parse → root.has_error() and maps that to parse_error — crates/aft/src/commands/outline.rs:3187 and outline.rs:3209. There is no content sniffing and no C → C++ retry anywhere in that path.

Even when a C++ header happens to parse without ERROR nodes, C_QUERY only matches function_definition, declaration, struct_specifier, enum_specifier, type_definition and preprocessor macros, so classes, methods and namespace members are silently missing from the outline rather than reported.

Impact

  • aft_outline: whole file goes to skipped_files as parse_error.
  • Symbol extraction / list_symbols: class members and namespace-scoped symbols are lost even in the non-error case (extract_c_symbols, parser.rs:2400).
  • The same .h → C assumption is duplicated in crates/aft/src/ast_grep_lang.rs:164 (Self::C => &["c", "h"]), so ast-grep also treats .h as C.
  • crates/aft/src/semantic_index.rs:10208 and crates/aft/src/lsp/registry.rs:731 include h in their whitelists, so these files are indexed / served as C too.

Downstream consumers that already branch on C | Cpp together (imports, calls.rs, callgraph.rs, format.rs, the inspect scanners) would not change behaviour if .h became Cpp.

Reproduction

// widget.h -- compiles fine as C++
#pragma once
#include <string>
namespace detail { static constexpr int kMax = 10; }
class Widget {
public:
    explicit Widget(int id);
    ~Widget();
    std::string name() const noexcept;
private:
    int id_;
};

aft_outline on this file returns it under skipped_files with reason: "parse_error". The same content as widget.hpp produces class Widget, method name, namespace detail.

Measured grammar comparison

Both grammars are the versions pinned in Cargo.lock (tree-sitter-c 0.24.2, tree-sitter-cpp 0.23.4), run over isolated feature samples:

sample C grammar C++ grammar
C++ header (class / template / namespace / public:) has_error=true, recovers 1 struct has_error=false, class + methods + namespace + qualified defs
plain C header (macros + typedefs + struct + prototypes + enum) clean, 7 symbols clean, but loses 3 macros and the typedef under C_QUERY
restrict, _Generic, _Static_assert, _Noreturn, designated init, anonymous union, bitfield, __attribute__ clean clean
_Atomic(int) has_error=true has_error=true (pre-existing, unrelated)
K&R-style function definition clean has_error=true

So the C++ grammar is effectively a superset for real-world headers, with K&R-style function definitions as the one known C-only construct it rejects.

Options (leaving the decision to maintainers)

  1. Map .h to LangId::Cpp. Requires extending CPP_QUERY with preproc_def, preproc_function_def and type_definition(declarator: (type_identifier)) — those exist in C_QUERY but not in CPP_QUERY, and without them .h files would lose every macro and typedef. With them, extraction for a plain C header matches today's output node-for-node (plus one extra struct foo for typedef struct foo foo_t;, since CPP_QUERY's struct_specifier uses name: (_) without a body constraint). Touches parser.rs:10557 and crates/aft/tests/integration/objc_test.rs:93 assertions.

  2. Fallback language. Parse .h as C first; if the tree has errors and the source looks like C++, re-parse as C++. This is where a general "fallback language" facility would fit if you would rather not special-case .h. Note CachedTree does not store lang today, so the resolved language would need to be cached alongside the tree or a warm cache would report C for a file parsed as Cpp.

  3. Content sniffing inside detect_language. Most accurate for genuinely mixed trees, but detect_language is path-only and has ~130 call sites, and it would need to read file content on a hot path.

  4. Project context. Treat .h as C++ when a sibling or ancestor directory contains .cpp / .cc / .cxx. Matches IDE/CMake intuition, but ambiguous for repos that mix C and C++.

Each option also raises whether ast_grep_lang.rs:164 and lsp/manager.rs:7202 ("c" | "h" => "c", the languageId sent to clangd) should follow the same mapping, so that all subsystems agree on what a .h file is.

Diagnostics

  • AFT binary / plugin version: unknown — reported from source inspection at e2dcaf60e, not from a packaged build.
  • Platform: n/a.

trim21 avatar Oct 30 '26 08:10 trim21

Thanks for the report. This is fixed on main and ships in the next release.

AFT used to parse every .h file as C, so C++ headers (classes, namespaces, templates) came back with an empty or partial outline. A .h file is now parsed as C first; if that parse fails, AFT tries C++, then Objective-C, and uses the first one that parses. The same choice is used everywhere a header is read: aft_outline, symbols, the call graph and search. Plain C headers are handled exactly as before, and nothing else gets re-indexed.

aft-alfonso[bot] avatar Nov 01 '26 12:11 aft-alfonso[bot]

This shipped in v0.64.0: .h files now resolve to C, C++ or Objective-C, so C++ headers get a full outline. Closing; please reopen if a header still comes back empty.

aft-alfonso[bot] avatar Nov 11 '26 18:11 aft-alfonso[bot]