https://github.com/daniel-petrovic updated https://github.com/llvm/llvm-project/pull/239693
>From e32651497a6d640e8f7c0bf4d1975bca2c0978e1 Mon Sep 17 00:00:00 2001 From: Daniel Petrovic <[email protected]> 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 | 9 +++++++- clang/test/Preprocessor/macro_paste_bad.c | 24 +++++++++++++++++++++ clang/test/Preprocessor/macro_paste_empty.c | 7 ++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index a367196a601bc7..c5a5a80da4b547 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 the + invalid paste. (#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..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/macro_paste_bad.c b/clang/test/Preprocessor/macro_paste_bad.c index 23777240cbf7fe..79bcdbb23e57f4 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,26 @@ 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@-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}} +// TODO: Diagnose the leading ## left by substituting an empty first argument. +PASTE(, 2) +// TODO: Diagnose the ## left by substituting two empty arguments. +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}} diff --git a/clang/test/Preprocessor/macro_paste_empty.c b/clang/test/Preprocessor/macro_paste_empty.c index e9b50f0e8e8733..c80839542f3ee8 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,9 @@ 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: ## _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
