Meinersbur created this revision.
Meinersbur added reviewers: aaron.ballman, mstorsjo, dblaikie.
Meinersbur added a project: clang.
Meinersbur requested review of this revision.

The implementation of -fminimize-whitespace (D104601 
<https://reviews.llvm.org/D104601>) revised the logic when to emit newlines. 
There was no case to handle when more than 8 lines were skippped in `-P` 
(DisableLineMarkers) mode and instead fell through the case intended for 
`-fminimize-whitespace`, i.e. emit nothing. This patch will emit one newline in 
this case.

The newline logic is slightly reorganized. The `-fminimize-whitespace` case is 
handled explicitly and the emit at least one newline is the new fallback case. 
Without `DisableLineMarkers`, there is the choice between emitting a line 
marker or up to 8 empty lines. 
The up to 8 newlines likely are fewer characters that a line directive, but in 
`-P` mode this had the paradoxic effect that it would print up to 7 empty 
lines, but none at all if more than 8 lines had to be skipped. With 
`DisableLineMarkers`, we now don't consider printing empty lines (just start a 
new line) which matches gcc's behaviour.

The `line-directive-output-mincol.c` test is replaced with a more comprehensive 
test `skip-empty-lines.c` that also tests the more than 8 skipped lines 
behaviour with a various flag combinations.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D106924

Files:
  clang/lib/Frontend/PrintPreprocessedOutput.cpp
  clang/test/Preprocessor/line-directive-output-mincol.c
  clang/test/Preprocessor/minimize-whitespace.c
  clang/test/Preprocessor/skip-empty-lines.c

Index: clang/test/Preprocessor/skip-empty-lines.c
===================================================================
--- /dev/null
+++ clang/test/Preprocessor/skip-empty-lines.c
@@ -0,0 +1,44 @@
+int  a ;
+int  b ;
+// A single empty line
+int  c ;
+/*
+
+more than 8 empty lines
+
+
+
+
+
+*/
+int  d ;
+
+// RUN: %clang_cc1 -E %s 2>&1 | FileCheck %s --strict-whitespace --check-prefix=LINEMARKES
+// RUN: %clang_cc1 -E -P %s 2>&1 | FileCheck %s --strict-whitespace --check-prefix=COLSONLY
+// RUN: %clang_cc1 -E -fminimize-whitespace %s 2>&1 | FileCheck %s --strict-whitespace --check-prefix=MINWS
+// RUN: %clang_cc1 -E -P -fminimize-whitespace %s 2>&1 | FileCheck %s --strict-whitespace --check-prefix=MINCOL
+
+// Check behavior after variying number of lines without emitted tokens.
+
+// LINEMARKES:       # 1 "{{.*}}skip-empty-lines.c" 2
+// LINEMARKERS-NEXT: int a ;
+// LINEMARKERS-NEXT: int b ;
+// LINEMARKERS-EMPTY:
+// LINEMARKERS-NEXT: int c ;
+// LINEMARKERS-NEXT: # 14 "/c/Users/meinersbur/src/llvm-project/clang/test/Preprocessor/skip-empty-lines.c"
+// LINEMARKERS-NEXT: int d ;
+
+// COLSONLY:      int a ;
+// COLSONLY-NEXT: int b ;
+// COLSONLY-NEXT: int c ;
+// COLSONLY-NEXT: int d ;
+
+// MINWS:      # 1 "{{.*}}skip-empty-lines.c" 2
+// MINWS-NEXT: int a;
+// MINWS-NEXT: int b;
+// MINWS-EMPTY:
+// MINWS-NEXT: int c;
+// MINWS-NEXT: # 14 "{{.*}}skip-empty-lines.c"
+// MINWS-NEXT: int d;
+
+// MINCOL: int a;int b;int c;int d;
Index: clang/test/Preprocessor/minimize-whitespace.c
===================================================================
--- clang/test/Preprocessor/minimize-whitespace.c
+++ clang/test/Preprocessor/minimize-whitespace.c
@@ -2,6 +2,12 @@
 // RUN: %clang_cc1 -fminimize-whitespace -E -C %s 2>&1 | FileCheck %s --strict-whitespace --check-prefix=MINCCOL
 // RUN: %clang_cc1 -fminimize-whitespace -E -P %s 2>&1 | FileCheck %s --strict-whitespace --check-prefix=MINWS
 // RUN: %clang_cc1 -fminimize-whitespace -E -C -P %s 2>&1 | FileCheck %s --strict-whitespace --check-prefix=MINCWS
+// The follow empty lines ensure that a #line directive is emitted instead of newline padding after the RUN lines
+
+
+
+
+
 
 #define NOT_OMP  omp  something
 #define HASH #
@@ -16,11 +22,11 @@
 f  ;
 
 
-// MINCOL:      {{^}}# 9 "{{.*}}minimize-whitespace.c"{{$}}
+// MINCOL:      {{^}}# 15 "{{.*}}minimize-whitespace.c"{{$}}
 // MINCOL:      {{^}}int a;{{$}}
 // MINCOL-NEXT: {{^}}int b;{{$}}
 // MINCOL-NEXT: {{^}}#pragma omp barrier{{$}}
-// MINCOL-NEXT: # 11 "{{.*}}minimize-whitespace.c"
+// MINCOL-NEXT: # 17 "{{.*}}minimize-whitespace.c"
 // MINCOL-NEXT: {{^}}x{{$}}
 // MINCOL-NEXT: {{^}}#pragma omp nothing{{$}}
 // MINCOL-NEXT: {{^ }}#pragma omp something{{$}}
@@ -28,11 +34,11 @@
 // MINCOL-NEXT: {{^}}int f;{{$}}
 
 // FIXME: Comments after pragmas disappear, even without -fminimize-whitespace
-// MINCCOL:      {{^}}# 9 "{{.*}}minimize-whitespace.c"{{$}}
+// MINCCOL:      {{^}}# 15 "{{.*}}minimize-whitespace.c"{{$}}
 // MINCCOL:      {{^}}int a;/*  span-comment  */{{$}}
 // MINCCOL-NEXT: {{^}}int b;//  line-comment{{$}}
 // MINCCOL-NEXT: {{^}}#pragma omp barrier{{$}}
-// MINCCOL-NEXT: # 11 "{{.*}}minimize-whitespace.c"
+// MINCCOL-NEXT: # 17 "{{.*}}minimize-whitespace.c"
 // MINCCOL-NEXT: {{^}}x//  more line-comments{{$}}
 // MINCCOL-NEXT: {{^}}#pragma omp nothing{{$}}
 // MINCCOL-NEXT: {{^ }}#pragma omp something{{$}}
Index: clang/test/Preprocessor/line-directive-output-mincol.c
===================================================================
--- clang/test/Preprocessor/line-directive-output-mincol.c
+++ /dev/null
@@ -1,11 +0,0 @@
-// RUN: %clang_cc1 -E -fminimize-whitespace %s 2>&1 | FileCheck %s -strict-whitespace
-
-// CHECK:      # 6 "{{.*}}line-directive-output-mincol.c"
-// CHECK-NEXT: int x;
-// CHECK-NEXT: int y;
-int x;
-int y;
-// CHECK-NEXT: # 10 "{{.*}}line-directive-output-mincol.c"
-// CHECK-NEXT: int z;
-int z;
-
Index: clang/lib/Frontend/PrintPreprocessedOutput.cpp
===================================================================
--- clang/lib/Frontend/PrintPreprocessedOutput.cpp
+++ clang/lib/Frontend/PrintPreprocessedOutput.cpp
@@ -276,20 +276,27 @@
   // otherwise print a #line directive.
   if (CurLine == LineNo) {
     // Nothing to do if we are already on the correct line.
-  } else if (!StartedNewLine && (!MinimizeWhitespace || !DisableLineMarkers) &&
-             LineNo - CurLine == 1) {
+  } else if (MinimizeWhitespace && DisableLineMarkers) {
+    // With -E -P -fminimize-whitespace, don't emit anything if not necessary.
+  } else if (!StartedNewLine && LineNo - CurLine == 1) {
     // Printing a single line has priority over printing a #line directive, even
     // when minimizing whitespace which otherwise would print #line directives
     // for every single line.
     OS << '\n';
     StartedNewLine = true;
-  } else if (!MinimizeWhitespace && LineNo - CurLine <= 8) {
-    const char *NewLines = "\n\n\n\n\n\n\n\n";
-    OS.write(NewLines, LineNo - CurLine);
-    StartedNewLine = true;
   } else if (!DisableLineMarkers) {
-    // Emit a #line or line marker.
-    WriteLineInfo(LineNo, nullptr, 0);
+    if (LineNo - CurLine <= 8) {
+      const char *NewLines = "\n\n\n\n\n\n\n\n";
+      OS.write(NewLines, LineNo - CurLine);
+    } else {
+      // Emit a #line or line marker.
+      WriteLineInfo(LineNo, nullptr, 0);
+    }
+    StartedNewLine = true;
+  } else if (!StartedNewLine) {
+    // 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';
     StartedNewLine = true;
   }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to