.h files are parsed with the C grammar: C++ headers land in skipped_files as parse_error in aft_outline
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)
-
detect_languagemaps.htoLangId::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::LANGUAGEand matched againstC_QUERY(parser.rs:817,parser.rs:248). -
tree-sitter-c-0.24.2/src/grammar.jsonhas noclass_specifier,namespace_definition,template_declarationoraccess_specifiernodes at all;tree-sitter-cpp-0.23.4does (2 / 3 / 5 / 8 occurrences). Any C++-specific construct therefore produces an ERROR node. -
outline_skip_reasonrunsvalidate_syntax→FileParser::parse→root.has_error()and maps that toparse_error—crates/aft/src/commands/outline.rs:3187andoutline.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 toskipped_filesasparse_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 incrates/aft/src/ast_grep_lang.rs:164(Self::C => &["c", "h"]), so ast-grep also treats.has C. crates/aft/src/semantic_index.rs:10208andcrates/aft/src/lsp/registry.rs:731includehin 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)
-
Map
.htoLangId::Cpp. Requires extendingCPP_QUERYwithpreproc_def,preproc_function_defandtype_definition(declarator: (type_identifier))— those exist inC_QUERYbut not inCPP_QUERY, and without them.hfiles would lose every macro and typedef. With them, extraction for a plain C header matches today's output node-for-node (plus one extrastruct foofortypedef struct foo foo_t;, sinceCPP_QUERY'sstruct_specifierusesname: (_)without a body constraint). Touchesparser.rs:10557andcrates/aft/tests/integration/objc_test.rs:93assertions. -
Fallback language. Parse
.has 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. NoteCachedTreedoes not storelangtoday, so the resolved language would need to be cached alongside the tree or a warm cache would reportCfor a file parsed asCpp. -
Content sniffing inside
detect_language. Most accurate for genuinely mixed trees, butdetect_languageis path-only and has ~130 call sites, and it would need to read file content on a hot path. -
Project context. Treat
.has 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.
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.
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.