[clang] d8f63bb - [clang] Fix crash in isAtEndOfMacroExpansion at FileID boundary. (#191734)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 16 00:19:49 PDT 2026
Author: Haojian Wu
Date: 2026-04-16T07:19:44Z
New Revision: d8f63bbc8e20a68e764852a15d67a06fcd14c5bd
URL: https://github.com/llvm/llvm-project/commit/d8f63bbc8e20a68e764852a15d67a06fcd14c5bd
DIFF: https://github.com/llvm/llvm-project/commit/d8f63bbc8e20a68e764852a15d67a06fcd14c5bd.diff
LOG: [clang] Fix crash in isAtEndOfMacroExpansion at FileID boundary. (#191734)
During error recovery, a synthetic token (whose length is 0) can be
inserted past the end of a FileID, e.g. inserting ")" when a macro-arg
containing a comma should be guarded by parentheses.
When calculating the location after this token, the calculated
`AfterLoc` can point exactly to the start of the next FileID
(`NextLocalOffset`), any source manager operations on the `AfterLoc` are
invalid.
This patch adds a safe guard in `Lexer::isAtEndOfMacroExpansion` to
prevent passing this invalid location to `SourceManager`.
Fixes #115007
Fixes #21755
Added:
clang/test/Parser/macro-braces-recovery.cpp
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Lex/Lexer.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5edec5f04e976..a6b6345168ef2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -500,7 +500,7 @@ Miscellaneous Clang Crashes Fixed
- Fixed a crash when explicitly casting a scalar to an atomic complex. (#GH114885)
- Fixed an assertion failure when parsing an invalid out-of-line enum definition with template parameters. (#GH187909)
- Fixed an assertion failure on invalid template template parameter during typo correction. (#GH183983)
-- Fixed an assertion failure when using CTAD for alias templates where the RHS resolves to a non-dependent class template specialization. (#GH190517)
+- Fixed an assertion failure in ``isAtEndOfMacroExpansion`` on macro expansions crossing the boundary of two fileIDs. (#GH115007), (#GH21755)
OpenACC Specific Changes
------------------------
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index 7a3d79e744ef2..e85dc6679d508 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -921,8 +921,21 @@ bool Lexer::isAtEndOfMacroExpansion(SourceLocation loc,
SourceLocation afterLoc = loc.getLocWithOffset(tokLen);
SourceLocation expansionLoc;
- if (!SM.isAtEndOfImmediateMacroExpansion(afterLoc, &expansionLoc))
- return false;
+ FileID FID = SM.getFileID(loc);
+
+ if (SM.isInFileID(afterLoc, FID)) {
+ if (!SM.isAtEndOfImmediateMacroExpansion(afterLoc, &expansionLoc))
+ return false;
+ } else {
+ // During error recovery, a zero-length synthetic token might be inserted
+ // past the end of the FileID, e.g. inserting ")" when a macro-arg
+ // containing a comma should be guarded by parentheses. In this case,
+ // afterLoc reaches the `NextLocalOffset` boundary, any operations on
+ // afterLoc will be invalid!
+ const SrcMgr::SLocEntry &Entry = SM.getSLocEntry(FID);
+ assert(Entry.isExpansion() && "Should be in an expansion");
+ expansionLoc = Entry.getExpansion().getExpansionLocEnd();
+ }
if (expansionLoc.isFileID()) {
// No other macro expansions.
diff --git a/clang/test/Parser/macro-braces-recovery.cpp b/clang/test/Parser/macro-braces-recovery.cpp
new file mode 100644
index 0000000000000..e8842f44bd623
--- /dev/null
+++ b/clang/test/Parser/macro-braces-recovery.cpp
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+namespace GH21755 {
+#define M(x) f x // expected-note {{macro 'M' defined here}}
+
+// expected-error at +5 {{too many arguments provided to function-like macro invocation}}
+// expected-note at +4 {{parentheses are required around macro argument containing braced initializer list}}
+// expected-error at +3 {{a type specifier is required for all declarations}}
+// expected-error at +2 {{expected ')'}}
+// expected-note at +1 {{to match this '('}}
+M(0 {,}) // expected-error {{expected ';' after top level declarator}}
+}
+
+namespace GH115007 {
+class Foo { // expected-note {{candidate constructor (the implicit copy constructor) not viable}} \
+ // expected-note {{candidate constructor (the implicit move constructor) not viable}}
+public:
+ Foo(int); // expected-note {{candidate constructor not viable: requires 1 argument, but 2 were provided}}
+ bool operator==(const int l); // expected-note {{candidate function not viable: no known conversion from 'Foo' to 'const int' for 1st argument}}
+};
+#define EQ(x,y) (void)(x == y) // expected-note {{macro 'EQ' defined here}}
+
+void test_EQ() {
+ Foo F = Foo{1};
+ // expected-error at +4 {{too many arguments provided to function-like macro invocation}}
+ // expected-note at +3 {{parentheses are required around macro argument containing braced initializer list}}
+ // expected-error at +2 {{no matching constructor for initialization of 'Foo'}}
+ // expected-error at +1 {{invalid operands to binary expression ('Foo' and 'Foo')}}
+ EQ(F,Foo{1,2});
+}
+}
More information about the cfe-commits
mailing list