[Preprocessor] Elide empty line(s) at start of file.

In -P mode, PrintPPOutputPPCallbacks::MoveToLine started at least one
newline if current and target line number mismatched. The method is also
called when entering a new file, be it the main file or an include file.
In this situation line numbers always almost mismatch, resulting in a
newline for each occurance even if no tokens have been printed
in-between.

Empty lines at the beginning of the output must be trimmed because it
may be parsed by scripts expecting the result to appear on the first
output line, as done by LibreOffice's configure script.

Fix by only emitting a newline if tokens have been printed so far using
the EmittedTokensOnThisLine flag. Also adding a test case of FileChanged
callbacks occuring with empty include files.

This fixes llvm.org/PR51616
This commit is contained in:
Michael Kruse 2021-08-25 11:31:53 -05:00
parent 846e562dcc
commit 66e37c99ef
4 changed files with 16 additions and 2 deletions

View File

@ -293,7 +293,7 @@ bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
WriteLineInfo(LineNo, nullptr, 0);
}
StartedNewLine = true;
} else if (!StartedNewLine) {
} else if (EmittedTokensOnThisLine) {
// If we are not on the correct line and don't need to be line-correct,
// at least ensure we start on a new line.
OS << '\n';

View File

@ -0,0 +1,10 @@
// RUN: %clang_cc1 -E -P %s | count 1
// Ensure no superfluous newlines are printed
// llvm.org/PR51616
#include "print_empty_include.h"
#include "print_empty_include.h"
#define EXPANDED_TO_NOTHING
EXPANDED_TO_NOTHING

View File

@ -0,0 +1,4 @@
#if 0
intentionally empty
#endif

View File

@ -3,7 +3,7 @@
* RUN: %clang_cc1 -E -P %s | grep 'a 3'
* RUN: %clang_cc1 -E -P %s | grep 'b 16'
* RUN: %clang_cc1 -E %s | not grep '# 0 '
* RUN: %clang_cc1 -E -P %s | count 4
* RUN: %clang_cc1 -E -P %s | count 2
* PR1848 PR3437 PR7360
*/