Files
Colby Mchenry fa25883ab0 fix(cpp): resolve local constructors and macro visibility (#1839) (#2035)
* fix(cpp): macro invocations are not calls; local objects call their constructor

A function-like macro invocation (`TRACE_POINT(1)`) parses as a call, and
the resolver bound it to a same-named free function in another translation
unit — a caller and a callee that never existed (#1838). And local object
initialization (`Widget w;` / `Widget w(1);` / `Widget w{1};`) reached the
class node instead of a constructor, or nothing at all for the default
form (#1839).

Extraction (wasm walker and the Rust kernel, in parity):
- every `preproc_function_def` becomes a `constant` node whose signature
  is the directive, so the index knows which names are macros;
- a C++ declaration emits one `calls` ref per constructed object, shaped
  `ns::T::T/<arity>`; pointer, reference and function declarators,
  `extern`, and array element braces construct nothing; the #1035
  `instantiates` ref is unchanged;
- constructors carry their parameter list as the signature.

Resolution:
- `cpp-macro-visibility.ts` walks the translation unit (root file plus
  in-repo includes, each file once) over per-file summaries of
  function-like defines, undefs and includes, evaluating what each file
  can decide itself (literals, its own defines, the include-guard idiom).
  A call whose name is definitely a macro at that point, or a name that
  only macros bear, resolves to nothing. A macro that exists in one build
  configuration only (CMSIS's `__DSB()` under the ARM compilers, a
  target's HAL compat header) keeps the call to the function the other
  configuration compiles; a wrapper macro that calls its own name is how
  that function gets called and hides nothing.
- `cpp-constructor.ts` resolves the constructor refs: the type in the
  lexical namespaces of the call site, then the single overload whose
  parameter count admits the arguments; two admitting overloads, an
  initializer_list overload, or an aggregate yield no edge.
- a `#define` constant is never a `calls` target and does not count
  toward the same-name ceiling.

Measured on a betaflight tree (4,017 files, wasm arm): 153 `calls` edges
lost — all fabricated (`MIN`/`MAX` bound to a CLI enum member, macros
misparsed as functions, a cross-vendor `SWAPBYTE`) — and 34 constructor
edges gained; indexing 34s vs 30s.

Fixes #1838
Fixes #1839

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(cpp): resolve local constructors and macro visibility (#1839)

Local construction lacked constructor calls, while macro invocations could bind unrelated functions.
Resolve constructors using lexical types and overload signatures, preserving separately declared defaults and array element construction.
Evaluate macro directives in translation-unit order, respecting reinclusion, guards and pragma once.
Mirror extraction in Rust and WASM, preserve type dependencies, and distinguish them in CLI output.

Co-authored-by: danusha2345 <danusha2345@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: danusha2345 <danusha2345@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-27 13:46:34 +00:00

67 lines
1.6 KiB
C++

/// Parity fixture for #1838 / #1839: function-like macros as `constant`
/// nodes (file scope, inside a namespace, inside a body), constructor
/// signatures, and the per-declarator constructor refs of local object
/// initialization — default, direct, brace, multi-declarator, qualified and
/// template types — beside the declarators that construct nothing (pointer,
/// reference, most-vexing-parse, extern). Must parse ERROR-FREE.
#define TRACE_POINT(value) ((void)(value))
#define VERSION 7
namespace app {
#define APP_LOG(fmt, ...) log_impl(fmt, __VA_ARGS__)
struct Aggregate {
int value;
};
class Widget {
public:
Widget();
explicit Widget(int value);
Widget(int a, int b = 2) : first(a), second(b) {}
private:
int first;
int second;
};
Widget::Widget() : first(0), second(0) {}
Widget::Widget(int value) : first(value), second(0) {}
template <typename T>
struct Box {
Box(T inner) : value(inner) {}
T value;
};
void constructions() {
Aggregate item{};
Widget by_default;
Widget braced{};
Widget direct(1);
Widget two(1, 2);
Widget a, b(1), c{1, 2};
app::Widget qualified(3);
Box<int> boxed(4);
Widget();
TRACE_POINT(VERSION);
APP_LOG("x", 1);
#define LOCAL_TRACE(v) ((void)(v))
LOCAL_TRACE(1);
}
void non_constructions(Widget &other) {
Widget *pointer{};
Widget *null_pointer(nullptr);
Widget &reference{other};
Widget &bound(other);
Widget most_vexing();
Widget items[2]{}; // array elements do construct objects
Widget *table[2]{};
Widget (*callback)(){};
extern Widget external;
int primitive(5);
int braced_primitive{6};
}
} // namespace app