Skip to content

Commit 1606022

Browse files
committed
[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
1 parent 8f099d1 commit 1606022

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

clang/lib/Frontend/PrintPreprocessedOutput.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,17 @@ class PrintPPOutputPPCallbacks : public PPCallbacks {
188188
/// @return Whether column adjustments are necessary.
189189
bool MoveToLine(const Token &Tok, bool RequireStartOfLine) {
190190
PresumedLoc PLoc = SM.getPresumedLoc(Tok.getLocation());
191-
if (PLoc.isInvalid())
192-
return false;
191+
unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine;
193192
bool IsFirstInFile = Tok.isAtStartOfLine() && PLoc.getLine() == 1;
194-
return MoveToLine(PLoc.getLine(), RequireStartOfLine) || IsFirstInFile;
193+
return MoveToLine(TargetLine, RequireStartOfLine) || IsFirstInFile;
195194
}
196195

197196
/// Move to the line of the provided source location. Returns true if a new
198197
/// line was inserted.
199198
bool MoveToLine(SourceLocation Loc, bool RequireStartOfLine) {
200199
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
201-
if (PLoc.isInvalid())
202-
return false;
203-
return MoveToLine(PLoc.getLine(), RequireStartOfLine);
200+
unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine;
201+
return MoveToLine(TargetLine, RequireStartOfLine);
204202
}
205203
bool MoveToLine(unsigned LineNo, bool RequireStartOfLine);
206204

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 -E -o - %s | FileCheck %s
2+
// RUN: %clang_cc1 -E -P -o - %s | FileCheck %s
3+
// RUN: %clang_cc1 -E -fminimize-whitespace -o - %s | FileCheck %s
4+
// RUN: %clang_cc1 -E -fminimize-whitespace -P -o - %s | FileCheck %s
5+
6+
// The PragmaAssumeNonNullHandler (and maybe others) passes an invalid
7+
// SourceLocation when inside a _Pragma. Ensure we still emit semantic
8+
// newlines.
9+
// See report at https://reviews.llvm.org/D104601#3105044
10+
11+
_Pragma("clang assume_nonnull begin") test _Pragma("clang assume_nonnull end")
12+
13+
// CHECK: {{^}}#pragma clang assume_nonnull begin{{$}}
14+
// CHECK: test
15+
// CHECK: {{^}}#pragma clang assume_nonnull end{{$}}

0 commit comments

Comments
 (0)