[clang] 1606022 - [Preprocessor] Fix newline before/after _Pragma.

Michael Kruse via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 4 22:45:42 PDT 2021


Author: Michael Kruse
Date: 2021-11-05T00:43:40-05:00
New Revision: 1606022fab2d90ed8ee6d15800ec1c2c293db20e

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

LOG: [Preprocessor] Fix newline before/after _Pragma.

The PragmaAssumeNonNullHandler (and maybe others) passes an invalid
SourceLocation to its callback, hence PrintPreprocessedOutput does not
know how many lines to insert between the previous token and the
pragma and does nothing.

With this patch we instead assume that the unknown token is on the same
line as the previous such that we can call the procedure that also emits
semantically significant whitespace.

Fixes bug reported here: https://reviews.llvm.org/D104601#3105044

Added: 
    clang/test/Preprocessor/_Pragma-newline.c

Modified: 
    clang/lib/Frontend/PrintPreprocessedOutput.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
index fadf0c054310d..45df86ef91cdb 100644
--- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -188,19 +188,17 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
   /// @return Whether column adjustments are necessary.
   bool MoveToLine(const Token &Tok, bool RequireStartOfLine) {
     PresumedLoc PLoc = SM.getPresumedLoc(Tok.getLocation());
-    if (PLoc.isInvalid())
-      return false;
+    unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine;
     bool IsFirstInFile = Tok.isAtStartOfLine() && PLoc.getLine() == 1;
-    return MoveToLine(PLoc.getLine(), RequireStartOfLine) || IsFirstInFile;
+    return MoveToLine(TargetLine, RequireStartOfLine) || IsFirstInFile;
   }
 
   /// Move to the line of the provided source location. Returns true if a new
   /// line was inserted.
   bool MoveToLine(SourceLocation Loc, bool RequireStartOfLine) {
     PresumedLoc PLoc = SM.getPresumedLoc(Loc);
-    if (PLoc.isInvalid())
-      return false;
-    return MoveToLine(PLoc.getLine(), RequireStartOfLine);
+    unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine;
+    return MoveToLine(TargetLine, RequireStartOfLine);
   }
   bool MoveToLine(unsigned LineNo, bool RequireStartOfLine);
 

diff  --git a/clang/test/Preprocessor/_Pragma-newline.c b/clang/test/Preprocessor/_Pragma-newline.c
new file mode 100644
index 0000000000000..43628eaef4674
--- /dev/null
+++ b/clang/test/Preprocessor/_Pragma-newline.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -E -o - %s | FileCheck %s
+// RUN: %clang_cc1 -E -P -o - %s | FileCheck %s
+// RUN: %clang_cc1 -E -fminimize-whitespace -o - %s | FileCheck %s
+// RUN: %clang_cc1 -E -fminimize-whitespace -P -o - %s | FileCheck %s
+
+// The PragmaAssumeNonNullHandler (and maybe others) passes an invalid
+// SourceLocation when inside a _Pragma. Ensure we still emit semantic
+// newlines.
+// See report at https://reviews.llvm.org/D104601#3105044
+
+_Pragma("clang assume_nonnull begin") test _Pragma("clang assume_nonnull end")
+
+// CHECK: {{^}}#pragma clang assume_nonnull begin{{$}}
+// CHECK: test
+// CHECK: {{^}}#pragma clang assume_nonnull end{{$}}


        


More information about the cfe-commits mailing list