[Preprocessor] Ensure newline after #pragma introduced by -fms-extensions.

The -fms-extensions converts __pragma (and _Pragma) into a #pragma that
has to occur at the beginning of a line and end with a newline. This
patch ensures that the newline after the #pragma is added even if
Token::isAtStartOfLine() indicated that we should not start a newline.

Committing relying post-commit review since the change is small, some
downstream uses might be blocked without this fix, and to make clear the
decision of the new -fminimize-whitespace feature (fix on main, revert
on clang-13.x branch) suggested by @aaron.ballman in D104601.

Differential Revision: https://reviews.llvm.org/D107183
This commit is contained in:
Michael Kruse 2021-08-01 18:50:40 -05:00
parent 2b9b5bc040
commit 0e2586779c
2 changed files with 22 additions and 1 deletions

View File

@ -646,7 +646,9 @@ void PrintPPOutputPPCallbacks::HandleWhitespaceBeforeTok(const Token &Tok,
!Tok.is(tok::annot_module_begin) && !Tok.is(tok::annot_module_end)))
return;
if (!RequireSameLine && MoveToLine(Tok, /*RequireStartOfLine=*/false)) {
// EmittedDirectiveOnThisLine takes priority over RequireSameLine.
if ((!RequireSameLine || EmittedDirectiveOnThisLine) &&
MoveToLine(Tok, /*RequireStartOfLine=*/EmittedDirectiveOnThisLine)) {
if (MinimizeWhitespace) {
// Avoid interpreting hash as a directive under -fpreprocessed.
if (Tok.is(tok::hash))

View File

@ -0,0 +1,19 @@
// RUN: %clang_cc1 -E -P %s -o - | FileCheck %s
// RUN: %clang_cc1 -E -P -fms-extensions %s -o - | FileCheck %s --check-prefix=MSEXT
// -fms-extensions changes __pragma into #pragma
// Ensure that there is a newline after the #pragma line.
#define MACRO \
text \
__pragma(PRAGMA) \
after
before MACRO text
// CHECK: before text __pragma(PRAGMA) after text
// MSEXT: before text
// MSEXT-NEXT: #pragma PRAGMA
// MSEXT-NEXT: after text