Author: Shivam Gupta
Date: 2026-08-17T00:07:23+05:30
New Revision: d2e52ee4783f48945bade69c0991fd69338a44b3

URL: 
https://github.com/llvm/llvm-project/commit/d2e52ee4783f48945bade69c0991fd69338a44b3
DIFF: 
https://github.com/llvm/llvm-project/commit/d2e52ee4783f48945bade69c0991fd69338a44b3.diff

LOG: [clang][Lex] Preserve physical line start after comments in -C mode 
(#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

Added: 
    clang/test/Preprocessor/comment_directive.c

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/Lex/Lexer.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index aa741bbf961e2..db065773d5285 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -414,6 +414,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