[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
Sat Nov 7 00:08:17 PST 2026
================
@@ -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)
----------------
daniel-petrovic wrote:
> Although we currently do not crash/assert for this case, it either creates `##2` or it leaves `##` at the start of the macro expression. In either case, a diagnostic is required. Please fix or add a TODO in the test here.
I handled TODO by fixing it and (maybe a bit "naively") introducing a simple HasLeadingPlaceMarker flag for tracing for leading ## leftovers and triggering diagnose message. But this may be not the best solution. Would need some more time to go over current implementation in more detail and maybe check some edge cases too.
https://github.com/llvm/llvm-project/pull/239693
More information about the cfe-commits
mailing list