Skip to content

[Bug]: C++: an export macro in front of a function or variable declaration is a syntax error, and on a definition it becomes a phantom node #5550

Backlog

Description

@z103cb

What happened?

#3648 / #3661 handle an export macro between class/struct and the type name (class MODULE_API Widget : public Base). The same macro in front of a free function or variable declaration is not handled:

MYLIB_EXPORT uint64_t compute(int x);

This is how module/DLL export headers usually declare their public functions. tree-sitter-cpp has no rule for a bare macro in front of a plain return type, so the ERROR lands on the declaration. Two symptoms, depending on the shape:

  • Declaration (prototype): the file is reported as had syntax errors and may be partially extracted ... (error region at line N, no symbols extracted). In a header that consists of such declarations, that is the whole header.
  • Definition (with a body): no warning, but the macro is extracted as a node of its own, next to the real function. A file with MYLIB_EXPORT uint64_t helper(int x) {...} and void plain() {} yields four nodes (file, helper(), MYLIB_EXPORT, plain()) where the same file without the macro yields three.

The phantom MYLIB_EXPORT node is a name that exists nowhere as a symbol.

Steps to reproduce

mkdir graphify-cpp-repro && cd graphify-cpp-repro

cat > a.hpp <<'SRC'
#pragma once
#include <cstdint>

MYLIB_EXPORT uint64_t compute(int x);

void plain();
SRC
sed 's/MYLIB_EXPORT //' a.hpp > a_control.hpp

cat > b.cpp <<'SRC'
#include <cstdint>

MYLIB_EXPORT uint64_t helper(int x)
{
    return static_cast<uint64_t>(x);
}

void plain() {}
SRC
sed 's/MYLIB_EXPORT //' b.cpp > b_control.cpp

cat > check.py <<'SRC'
import sys
from pathlib import Path
from graphify.extract import extract

for name in sys.argv[1:]:
    out = extract([Path(name).resolve()], cache_root=Path(".cache_" + name), root=Path("."), parallel=False)
    print(name, len(out["nodes"]), "nodes", sorted(n["label"] for n in out["nodes"]))
SRC

python check.py a.hpp a_control.hpp b.cpp b_control.cpp

The same declaration also fails when it is inside a namespace, and when the return type and the function name are on separate lines (both checked), so it does not depend on the layout of the declaration.

Error output or graph output

Results on 0.9.111 (the control file is the same file with the macro removed):

file tree-sitter ERROR graphify warning nodes
a.hpp (MYLIB_EXPORT uint64_t compute(int x);) yes 1 file(s) had syntax errors and may be partially extracted: a.hpp (error region at line 4, no symbols extracted) 1
a_control.hpp no 1 code file(s) yielded no symbols 1
b.cpp (definition) yes none 4: helper(), MYLIB_EXPORT, plain() and the file
b_control.cpp no none 3

Note that the prototypes themselves are not extracted in either case (a declaration without a body does not produce a node, so the control file also has no symbols). The difference for declarations is the false syntax errors ... no symbols extracted warning; for definitions it is the phantom node.

Graphify version

Reproduced on current upstream/v8 (0.9.121) and on 0.9.111.

Operating System

macOS (arm64). Python 3.15.2, tree-sitter 0.25.2, tree-sitter-cpp 0.23.4.

Proposed fix

Extend the existing byte-length-preserving normalization (_normalize_cpp_export_macros) with one more pattern: an ALL-CAPS identifier ending in EXPORT or _API at the start of a line, followed by whitespace and an identifier, is overwritten with spaces. Offsets, lines and columns stay unchanged, as in the existing class/struct case.

+# Export macro (``MYLIB_EXPORT``, ``FOO_API`` ...) leading a free-function or
+# variable declaration, e.g. ``MYLIB_EXPORT uint64_t f(...)``. tree-sitter-cpp
+# cannot parse a bare macro before a plain return type.
+_CPP_EXPORT_DECL_MACRO_RE = re.compile(
+    rb"(^[ \t]*)([A-Z][A-Z0-9_]*(?:EXPORT|_API)[ \t]+)(?=[A-Za-z_:])",
+    re.MULTILINE,
+)
...
 def _normalize_cpp_export_macros(source: bytes) -> bytes:
...
+    source = _CPP_EXPORT_DECL_MACRO_RE.sub(
+        lambda m: m.group(1) + re.sub(rb"[^\r\n]", b" ", m.group(2)), source
+    )
     return _CPP_EXPORT_MACRO_RE.sub(repl, source)

With this applied, a.hpp no longer reports a syntax error, and b.cpp yields three nodes, the same as b_control.cpp.

Limits of this sketch, for discussion:

  • It is narrower than the class/struct pattern on purpose. That one accepts any ALL-CAPS macro because the surrounding keyword makes it unambiguous. A line-leading ALL-CAPS token is much more often real code, so the sketch only matches names ending in EXPORT or _API. A project that spells its export macro differently (for example FOO_DLL) would not be covered; a configurable list could be an alternative.
  • A .h header is routed to the C extractor, where this normalization is not applied, so the same declaration in a .h file still reports the syntax error. Whether .h files that use C++ constructs should go through the C++ path is a separate question.
  • Declaration-only headers still produce no symbols after the fix, because prototypes are not extracted. That is a separate limitation; I did not find an existing issue for it.

I am happy to turn this into a PR with a regression test that fails on the current code (two-file pair as above).

Activity

  1. github-actions commented on Nov 27, 2026

    @github-actions

    Thanks for opening this issue, @z103cb. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

  2. added a commit that references this issue on Nov 27, 2026
    f291b17
  3. Deepjyoti-Sarmah commented on Nov 27, 2026

    @Deepjyoti-Sarmah
    Contributor

    Thanks for the detailed report and the sketch. I opened #5558 for this, with regression tests that fail on current v8.

    It uses your idea of matching names that end in EXPORT or _API, but not only at the start of a line. extern MYLIB_API int f(int); and static/inline forms give the same broken tree. It skips #define lines and the word after class/struct/union/enum, so class MODULE_API var{1}; keeps working. The .h routing and prototype-symbol limits are listed in the PR as out of scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions