[Preprocessor] Fix newline before/after _Pragma.
authorMichael Kruse <llvm-project@meinersbur.de>
Fri, 5 Nov 2021 05:03:26 +0000 (00:03 -0500)
committerMichael Kruse <llvm-project@meinersbur.de>
Fri, 5 Nov 2021 05:43:40 +0000 (00:43 -0500)
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

clang/lib/Frontend/PrintPreprocessedOutput.cpp
clang/test/Preprocessor/_Pragma-newline.c [new file with mode: 0644]

index fadf0c0..45df86e 100644 (file)
@@ -188,19 +188,17 @@ public:
   /// @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 (file)
index 0000000..43628ea
--- /dev/null
@@ -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{{$}}