[clang] [Clang][Lex] Treat consecutive ## macro tokens as a single paste (PR #239693)
Daniel Petrovic via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 12:55:22 PST 2026
https://github.com/daniel-petrovic updated https://github.com/llvm/llvm-project/pull/239693
>From fd21b76b3cc275c41f50c66210f0f24877e05426 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] Fix crash in pasteTokens on consecutive ## operators
Treat consecutive macro token-pasting operators (##) as a single paste operator during macro argument expansion, aligning Clang's behavior with GCC.
Also replaces an unhandled assert(!IsAtEnd()) in TokenLexer::pasteTokens() with proper error handling (err_paste_at_end) if the token stream unexpectedly ends.
Fixes #239457
---
clang/lib/Lex/TokenLexer.cpp | 9 +++++++-
clang/test/Preprocessor/GH239457.c | 5 +++++
.../Preprocessor/macro_paste_consecutive.c | 22 +++++++++++++++++++
3 files changed, 35 insertions(+), 1 deletion(-)
create mode 100644 clang/test/Preprocessor/GH239457.c
create mode 100644 clang/test/Preprocessor/macro_paste_consecutive.c
diff --git a/clang/lib/Lex/TokenLexer.cpp b/clang/lib/Lex/TokenLexer.cpp
index 1e6132887e319c..4c0d94854266e0 100644
--- a/clang/lib/Lex/TokenLexer.cpp
+++ b/clang/lib/Lex/TokenLexer.cpp
@@ -790,7 +790,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/GH239457.c b/clang/test/Preprocessor/GH239457.c
new file mode 100644
index 00000000000000..7e6682912c3564
--- /dev/null
+++ b/clang/test/Preprocessor/GH239457.c
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -Eonly -verify %s
+// RUN: %clang_cc1 -Eonly -verify -x c++ %s
+
+#define FOO(x, y) x####y
+FOO(bar, ) // expected-error {{'##' cannot appear at end of macro expansion}}
diff --git a/clang/test/Preprocessor/macro_paste_consecutive.c b/clang/test/Preprocessor/macro_paste_consecutive.c
new file mode 100644
index 00000000000000..3c5a5f8187f273
--- /dev/null
+++ b/clang/test/Preprocessor/macro_paste_consecutive.c
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -Eonly -verify %s
+// RUN: %clang_cc1 -Eonly -verify -x c++ %s
+
+// 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)
+PASTE(,)
+
+#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}}
More information about the cfe-commits
mailing list