[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
Fri Nov 6 22:27:08 PST 2026


https://github.com/daniel-petrovic updated https://github.com/llvm/llvm-project/pull/239693

>From 7cf3870915e9cbab7b448ae5c14d947fb9bed419 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().

Extend existing macro-paste tests with C and C++ regression coverage,
including a valid use of consecutive ## operators with empty arguments.
Document remaining missing diagnostics with TODOs and add a release note.

Fixes #239457
---
 clang/docs/ReleaseNotes.md                  |  4 ++++
 clang/lib/Lex/TokenLexer.cpp                | 25 ++++++++++++++++++++-
 clang/test/Preprocessor/macro_paste_bad.c   | 22 ++++++++++++++++++
 clang/test/Preprocessor/macro_paste_empty.c | 12 ++++++++++
 4 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index a367196a601bc7..4a517ed8f49a5d 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1176,6 +1176,10 @@ 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 and cases that leave a leading `##`. (#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..94f11a4d84d6b8 100644
--- a/clang/lib/Lex/TokenLexer.cpp
+++ b/clang/lib/Lex/TokenLexer.cpp
@@ -250,6 +250,8 @@ void TokenLexer::stringifyVAOPTContents(
 /// return preexpanded tokens from Tokens.
 void TokenLexer::ExpandFunctionArguments() {
   SmallVector<Token, 128> ResultToks;
+  // Remember an empty paste result that can supply the LHS of a later ##.
+  bool HasLeadingPlacemarker = false;
 
   // Loop through 'Tokens', expanding them into ResultToks.  Keep
   // track of whether we change anything.  If not, no need to keep them.  If so,
@@ -425,6 +427,17 @@ void TokenLexer::ExpandFunctionArguments() {
     IdentifierInfo *II = CurTok.getIdentifierInfo();
     int ArgNo = II ? Macro->getParameterNum(II) : -1;
     if (ArgNo == -1) {
+      if (CurTok.is(tok::hashhash) && ResultToks.empty()) {
+        // A preceding empty paste can leave a placemarker even though no
+        // tokens were emitted. It supplies the LHS of this paste.
+        if (HasLeadingPlacemarker) {
+          HasLeadingPlacemarker = false;
+          continue;
+        }
+        SourceLocation Loc = PP.getSourceManager().createExpansionLoc(
+            CurTok.getLocation(), ExpandLocStart, ExpandLocEnd, 2);
+        PP.Diag(Loc, diag::err_paste_at_start);
+      }
       // This isn't an argument, just add it.
       ResultToks.push_back(CurTok);
 
@@ -610,6 +623,9 @@ void TokenLexer::ExpandFunctionArguments() {
       MaybeRemoveCommaBeforeVaArgs(ResultToks,
                                    /*HasPasteOperator=*/true,
                                    Macro, ArgNo, PP);
+
+    if (ResultToks.empty())
+      HasLeadingPlacemarker = true;
   }
 
   // If anything changed, install this as the new Tokens list.
@@ -790,7 +806,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..4139af432c0c54 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,24 @@ 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}}
+PASTE(, 2) // expected-error {{'##' cannot appear at start of macro expansion}}
+PASTE(,) // expected-error {{'##' cannot appear at start of macro expansion}}
+
+#define PASTE3(x, y) x #### y #### y
+PASTE3(1, 2) // expected-error {{pasting formed '1##', an invalid preprocessing token}} \
+             // expected-error {{pasting formed '2##', an invalid preprocessing token}}
+
+#define PASTE_EMPTY_MIDDLE(x, y, z) x####y####z
+PASTE_EMPTY_MIDDLE(1, , 3) // expected-error {{pasting formed '1##', an invalid preprocessing token}} \
+                          // expected-error {{pasting formed '##3', 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..fb344c2e73968c 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,14 @@ 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:
+// CHECK-NOT: ##
+
+// The leading placemarker can also supply the LHS of a nonempty paste.
+#define PASTE_LEADING(X) X ## X X ## ## 2
+f:PASTE_LEADING()
+// CHECK: f:2



More information about the cfe-commits mailing list