https://github.com/xgupta created 
https://github.com/llvm/llvm-project/pull/216556

When comments are preserved with -C, a comment at the start of a physical line 
consumes the PhysicalStartOfLine state. As a result, a preprocessor directive 
following the comment is not recognized.

Preserve the PhysicalStartOfLine state when returning a comment token so that 
directives following comments are still handled correctly.

Fixes: #48361

>From 0ba17ad78a0cb6b0a687a3204e265a66d5fef7b4 Mon Sep 17 00:00:00 2001
From: Shivam Gupta <[email protected]>
Date: Sun, 16 Aug 2026 15:24:41 +0530
Subject: [PATCH] [clang][Lex] Preserve physical line start after comments in
 -C mode

When comments are preserved with -C, a comment at the start of a
physical line consumes the PhysicalStartOfLine state. As a result, a
preprocessor directive following the comment is not recognized.

Preserve the PhysicalStartOfLine state when returning a comment token so
that directives following comments are still handled correctly.

Fixes: #48361
---
 clang/docs/ReleaseNotes.md                  |  1 +
 clang/lib/Lex/Lexer.cpp                     |  1 +
 clang/test/Preprocessor/comment_directive.c | 33 +++++++++++++++++++++
 3 files changed, 35 insertions(+)
 create mode 100644 clang/test/Preprocessor/comment_directive.c

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 70ea2e35090c6..979a268e23d3a 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -412,6 +412,7 @@ features cannot lower the translation-unit ABI level;
 - Clang now defines the GCC-compatible predefined macro `__SIG_ATOMIC_TYPE__`. 
(#GH213895)
 - Fixed an ICE that occurred when a structured binding pack is expanded 
outside the lambda where it was declared. (#GH214160)
 - Fixed a bug where a stray closing curley brace in an OpenMP/OpenACC pragma 
could cause pragma parsing issues when inside of a member function. (#GH214195)
+- Fixed a bug where preprocessor directives following comments were not 
correctly recognized when using -C. (#GH48361)
 
 #### Bug Fixes to Compiler Builtins
 
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index 37e33861c4470..fe6a11f29b73b 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -3163,6 +3163,7 @@ bool Lexer::SkipBlockComment(Token &Result, const char 
*CurPtr) {
   // If we are returning comments as tokens, return this comment as a token.
   if (inKeepCommentMode()) {
     FormTokenWithChars(Result, CurPtr, tok::comment);
+    IsAtPhysicalStartOfLine = Result.isAtPhysicalStartOfLine();
     return true;
   }
 
diff --git a/clang/test/Preprocessor/comment_directive.c 
b/clang/test/Preprocessor/comment_directive.c
new file mode 100644
index 0000000000000..980ae4a233e08
--- /dev/null
+++ b/clang/test/Preprocessor/comment_directive.c
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -E -C %s | FileCheck %s
+
+/* comment */ #define A 1
+int a = A;
+
+/*
+ * multiline comment
+ */ #define B 2
+int b = B;
+
+int c; /* comment */ #define C 3
+int d = C;
+
+int e; /*
+ * multiline comment
+ */ #define D 4
+int f = D;
+
+// CHECK: /* comment */
+// CHECK-NEXT: int a = 1;
+
+// CHECK: /*
+// CHECK-NEXT:  * multiline comment
+// CHECK-NEXT:  */
+// CHECK-NEXT: int b = 2;
+
+// CHECK: int c; /* comment */ #define C 3
+// CHECK-NEXT: int d = C;
+
+// CHECK: int e; /*
+// CHECK-NEXT:  * multiline comment
+// CHECK-NEXT:  */ #define D 4
+// CHECK-NEXT: int f = D;

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to