[clang] 66e37c9 - [Preprocessor] Elide empty line(s) at start of file.

Michael Kruse via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 25 10:50:35 PDT 2021


Author: Michael Kruse
Date: 2021-08-25T12:48:59-05:00
New Revision: 66e37c99ef5d7da1b17b3bbaa4ef05b633a473f7

URL: https://github.com/llvm/llvm-project/commit/66e37c99ef5d7da1b17b3bbaa4ef05b633a473f7
DIFF: https://github.com/llvm/llvm-project/commit/66e37c99ef5d7da1b17b3bbaa4ef05b633a473f7.diff

LOG: [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

Added: 
    clang/test/Preprocessor/print_empty_include.c
    clang/test/Preprocessor/print_empty_include.h

Modified: 
    clang/lib/Frontend/PrintPreprocessedOutput.cpp
    clang/test/Preprocessor/print_line_track.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
index 5c5fc751179d1..3a3b158aafdb7 100644
--- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp
+++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -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';

diff  --git a/clang/test/Preprocessor/print_empty_include.c b/clang/test/Preprocessor/print_empty_include.c
new file mode 100644
index 0000000000000..04cda99223fed
--- /dev/null
+++ b/clang/test/Preprocessor/print_empty_include.c
@@ -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
+

diff  --git a/clang/test/Preprocessor/print_empty_include.h b/clang/test/Preprocessor/print_empty_include.h
new file mode 100644
index 0000000000000..34b07013ed6d2
--- /dev/null
+++ b/clang/test/Preprocessor/print_empty_include.h
@@ -0,0 +1,4 @@
+#if 0
+intentionally empty
+#endif
+

diff  --git a/clang/test/Preprocessor/print_line_track.c b/clang/test/Preprocessor/print_line_track.c
index fb2ccf270e242..156ae22693b85 100644
--- a/clang/test/Preprocessor/print_line_track.c
+++ b/clang/test/Preprocessor/print_line_track.c
@@ -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
 */
 


        


More information about the cfe-commits mailing list