[clang] [Clang][Lex] Fix crash when empty macro arguments leave a trailing ## (PR #239693)
Daniel Petrovic via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 8 12:10:01 PST 2026
https://github.com/daniel-petrovic updated https://github.com/llvm/llvm-project/pull/239693
>From 72ad5a6105d21f70faae2ad736c16966ec887c46 Mon Sep 17 00:00:00 2001
From: Daniel Petrovic <daniel-dev at hotmail.de>
Date: Fri, 6 Nov 2026 18:08:35 +0100
Subject: [PATCH] [Clang][Lex] Fix crash when empty macro arguments leave a
trailing ##
Diagnose a trailing ## left after substituting empty macro arguments
instead of asserting in TokenLexer::pasteTokens().
When an empty argument is pasted with a ## token, preserve the resulting
token without treating it as another paste operator.
Extend existing macro-paste tests with C and C++ regression coverage.
Fixes #239457
---
clang/docs/ReleaseNotes.md | 5 ++++
clang/lib/Lex/TokenLexer.cpp | 17 +++++++++++-
clang/test/Preprocessor/macro_paste_bad.c | 12 +++++++++
clang/test/Preprocessor/macro_paste_empty.c | 30 +++++++++++++++++++++
4 files changed, 63 insertions(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index a367196a601bc7..31ff1eceefe9d5 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1176,6 +1176,11 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
#### Miscellaneous Clang Crashes Fixed
+- Fixed a crash when expanding a macro with consecutive `##` operators and
+ an empty argument that leaves a trailing `##`. Clang now diagnoses this
+ invalid paste. Pasting an empty argument with a `##` token preserves the
+ token without treating it as another paste operator. (#GH239457)
+
- Fixed a crash in preprocessor conditionals containing `_Pragma`.
- Fixed a crash when Microsoft extensions were enabled and an unterminated
`__identifier` expression reached the end of a preprocessing directive or
diff --git a/clang/lib/Lex/TokenLexer.cpp b/clang/lib/Lex/TokenLexer.cpp
index 1e6132887e319c..060b1628ff3533 100644
--- a/clang/lib/Lex/TokenLexer.cpp
+++ b/clang/lib/Lex/TokenLexer.cpp
@@ -579,6 +579,14 @@ void TokenLexer::ExpandFunctionArguments() {
// Discard the argument token and skip (don't copy to the expansion
// buffer) the paste operator after it.
++I;
+ if (I + 1 != E && Tokens[I + 1].is(tok::hashhash)) {
+ // Pasting a placemarker with ## produces a ## token, not another
+ // paste operator. Turn ## into 'unknown' to avoid the pasted ## from
+ // looking like a paste operator.
+ Token PasteResult = Tokens[++I];
+ PasteResult.setKind(tok::unknown);
+ ResultToks.push_back(PasteResult);
+ }
continue;
}
@@ -790,7 +798,14 @@ bool TokenLexer::pasteTokens(Token &LHSTok, ArrayRef<Token> TokenStream,
PasteOpLoc = TokenStream[CurIdx].getLocation();
if (TokenStream[CurIdx].is(tok::hashhash))
++CurIdx;
- assert(!IsAtEnd() && "No token on the RHS of a paste operator!");
+
+ if (IsAtEnd()) {
+ SourceManager &SM = PP.getSourceManager();
+ SourceLocation Loc =
+ SM.createExpansionLoc(PasteOpLoc, ExpandLocStart, ExpandLocEnd, 2);
+ PP.Diag(Loc, diag::err_paste_at_end);
+ break;
+ }
// Get the RHS token.
const Token &RHS = TokenStream[CurIdx];
diff --git a/clang/test/Preprocessor/macro_paste_bad.c b/clang/test/Preprocessor/macro_paste_bad.c
index 23777240cbf7fe..c9c34d0bd5a74e 100644
--- a/clang/test/Preprocessor/macro_paste_bad.c
+++ b/clang/test/Preprocessor/macro_paste_bad.c
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -Eonly -verify -pedantic %s
+// RUN: %clang_cc1 -Eonly -verify -pedantic -x c++ %s
// pasting ""x"" and ""+"" does not give a valid preprocessing token
#define XYZ x ## +
XYZ // expected-error {{pasting formed 'x+', an invalid preprocessing token}}
@@ -42,3 +43,14 @@ PR21379A(0 {, }) // expected-error {{too many arguments provided to function-lik
#define PR21379B(x) printf #x // expected-note {{macro 'PR21379B' defined here}}
PR21379B(0 {, }) // expected-error {{too many arguments provided to function-like macro invocation}}
// expected-note at -1 {{parentheses are required around macro argument containing braced initializer list}}
+
+// Regression test for GH239457.
+// Consecutive ## operators must not be collapsed into a single paste.
+#define PASTE(x, y) x####y
+
+PASTE(1,) // expected-error {{'##' cannot appear at end of macro expansion}}
+PASTE(1, 2) // expected-error {{pasting formed '1##', an invalid preprocessing token}}
+
+// Object-like macros also diagnose invalid intermediate paste results.
+#define OBJECT 1 ## ## 2
+OBJECT // expected-error {{pasting formed '1##', an invalid preprocessing token}}
diff --git a/clang/test/Preprocessor/macro_paste_empty.c b/clang/test/Preprocessor/macro_paste_empty.c
index e9b50f0e8e8733..f6ed9dbed8c085 100644
--- a/clang/test/Preprocessor/macro_paste_empty.c
+++ b/clang/test/Preprocessor/macro_paste_empty.c
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -E %s | FileCheck --strict-whitespace %s
+// RUN: %clang_cc1 -E -x c++ %s | FileCheck --strict-whitespace %s
#define FOO(X) X ## Y
a:FOO()
@@ -15,3 +16,32 @@ c:FOO3()
#define FOO4(X, Y) X ## Y
d:FOO4(,FOO4(,))
// CHECK: d:FOO4
+
+// Consecutive ## operators can be valid when pasting empty arguments.
+#define PASTE1(X) X ## X X ## ## X X ## X
+e:PASTE1()
+// CHECK: e:##
+
+// The ## token produced by pasting is not another paste operator.
+#define PASTE_LEADING(X) X ## X X ## ## 2
+f:PASTE_LEADING()
+// CHECK: f:## 2
+
+// An empty __VA_OPT__ supplies a placemarker. Pasting an empty X with the
+// second ## produces a ## token, which is not another paste operator.
+#define PASTE_VAOPT(X, ...) __VA_OPT__() X ## ## 2
+g:PASTE_VAOPT(,)
+// CHECK: g:## 2
+h:PASTE_VAOPT(, token)
+// CHECK: h:## 2
+
+// Empty left operands preserve the ## token even without an earlier placemarker.
+#define PASTE_EMPTY_LEFT(X, Y) X ## ## Y
+i:PASTE_EMPTY_LEFT(, 2)
+// CHECK: i:##2
+j:PASTE_EMPTY_LEFT(,)
+// CHECK: j:##
+
+#define PASTE_VARARGS(...) __VA_ARGS__ ## ## 2
+k:PASTE_VARARGS()
+// CHECK: k:##2
More information about the cfe-commits
mailing list