Don't crash on undefined function-like macros#739
Merged
tannergooding merged 1 commit intoJul 13, 2026
Merged
Conversation
An undefined function-like macro used in another macro's body (e.g. `#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)` where `TESTRESULT_FROM_WIN32` is not defined) synthesized a `ClangSharpMacro_` `VarDecl` whose initializer resolved to a plain `Stmt` rather than an `Expr`. `VarDecl.InitFactory` hard-cast the result via `GetOrCreate<Expr>`, throwing `InvalidCastException`. Resolve the initializer via `GetOrCreate<Stmt>(...) as Expr` so a non-`Expr` initializer yields `null` instead of throwing, and have the macro handling in `VisitVarDecl` gracefully skip such a macro while emitting a warning diagnostic. Also warn, rather than silently skip, when the initializer resolves to a `RecoveryExpr`, which is how modern Clang represents these unresolved invocations. Fixes dotnet#222 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Using an undefined function-like macro in another macro's body crashed the generator with
InvalidCastException: Unable to cast object of type 'ClangSharp.Stmt' to type 'ClangSharp.Expr':The generator synthesizes a
ClangSharpMacro_VarDecland lazily resolves its initializer viaVarDecl.InitFactory, which hard-cast the cursor throughGetOrCreate<Expr>. When the initializer resolves to a plainStmtrather than anExpr, that cast threw.InitFactorynow resolves viaGetOrCreate<Stmt>(...) as Expr, so a non-Exprinitializer yieldsnullinstead of throwing. This is behavior-preserving for real initializers, which are alwaysExpr. The macro handling inVisitVarDeclgracefully skips such a macro and emits a warning diagnostic instead of crashing, and the existingRecoveryExprskip now emits the same warning rather than silently dropping the macro (modern Clang represents these unresolved invocations as aRecoveryExpr).Added
UndefinedFunctionLikeMacroTestto theVarDeclarationgolden suite (Base + all 16 configuration variants), asserting no crash, empty output, and the expected warning diagnostic.Note: on the pinned libClang 21.1.8 the exact
Stmt -> Exprcast is no longer produced (Clang yields aRecoveryExpr/CallExpr), so this hardens the reported cast site defensively and locks in the graceful warn-and-skip behavior via the new regression test.Fixes #222