[clang] [Clang] Warn on backslash-newline-EOF (PR #97585)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 3 07:41:29 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Mital Ashok (MitalAshok)

<details>
<summary>Changes</summary>

C99 §5.1.1.2p2, C23 §5.1.1.2p2

> A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.

Also adds tests for C++ DRs [CWG1698](https://cplusplus.github.io/CWG/issues/1698.html) and [CWG2747](https://cplusplus.github.io/CWG/issues/2747.html) related to line splices near the end of a source file.


---
Full diff: https://github.com/llvm/llvm-project/pull/97585.diff


10 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+2) 
- (modified) clang/include/clang/Basic/DiagnosticLexKinds.td (+1) 
- (modified) clang/lib/Lex/Lexer.cpp (+36-3) 
- (modified) clang/test/CXX/drs/cwg16xx.cpp (+9) 
- (added) clang/test/CXX/drs/cwg2747.cpp (+11) 
- (modified) clang/test/CXX/drs/cwg27xx.cpp (+2) 
- (added) clang/test/Preprocessor/backslash_newline_eof.c (+12) 
- (added) clang/test/Preprocessor/backslash_without_newline.c (+8) 
- (added) clang/test/Preprocessor/backslash_without_newline.h (+4) 
- (modified) clang/www/cxx_dr_status.html (+2-2) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f40fd1cd145bb..7c0ac3a504f98 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -647,6 +647,8 @@ Improvements to Clang's diagnostics
 
 - Clang now shows implicit deduction guides when diagnosing overload resolution failure. #GH92393.
 
+- Clang now emits ``-Wnewline-eof`` when the last newline is deleted by a preceding backslash.
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 12d7b8c0205ee..e6b2c1385944c 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -56,6 +56,7 @@ def ext_no_newline_eof : Extension<"no newline at end of file">,
   InGroup<NewlineEOF>;
 def warn_no_newline_eof : Warning<"no newline at end of file">,
   InGroup<NewlineEOF>, DefaultIgnore;
+def note_backslash_newline_eof : Note<"last newline deleted by splice here">;
 
 def warn_cxx98_compat_no_newline_eof : Warning<
   "C++98 requires newline at end of file">,
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index e59c7805b3862..0e540834b473b 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -3165,7 +3165,17 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
 
   // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
   // a pedwarn.
-  if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) {
+  if (CurPtr != BufferStart) {
+    StringRef LastNewline;
+    if (CurPtr[-1] == '\r' || CurPtr[-1] == '\n') {
+      LastNewline = StringRef(CurPtr - 1, 1);
+      if (CurPtr - 1 != BufferStart && CurPtr[-2] != CurPtr[-1] &&
+          (CurPtr[-2] == '\r' || CurPtr[-2] == '\n')) {
+        // \r\n or \n\r is one newline
+        LastNewline = StringRef(CurPtr - 2, 2);
+      }
+    }
+
     DiagnosticsEngine &Diags = PP->getDiagnostics();
     SourceLocation EndLoc = getSourceLocation(BufferEnd);
     unsigned DiagID;
@@ -3183,8 +3193,31 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
       DiagID = diag::ext_no_newline_eof;
     }
 
-    Diag(BufferEnd, DiagID)
-      << FixItHint::CreateInsertion(EndLoc, "\n");
+    if (LastNewline.empty()) {
+      Diag(BufferEnd, DiagID) << FixItHint::CreateInsertion(EndLoc, "\n");
+    } else {
+      // While the file physically ends in a newline, the previous
+      // line might have ended in a splice, so it would be deleted
+      const char *LastSpliceLocation = LastNewline.data();
+      while (LastSpliceLocation != BufferStart &&
+             isHorizontalWhitespace(*--LastSpliceLocation))
+        ;
+
+      bool LastIsSplice = *LastSpliceLocation == '\\';
+      if (*LastSpliceLocation == '/' && LangOpts.Trigraphs)
+        // Check for "??/" trigraph for "\"
+        LastIsSplice =
+            LastSpliceLocation != BufferStart && *--LastSpliceLocation == '?' &&
+            LastSpliceLocation != BufferStart && *--LastSpliceLocation == '?';
+
+      if (LastIsSplice) {
+        PP->Diag(getSourceLocation(LastNewline.data(), LastNewline.size()),
+                 DiagID);
+        Diag(LastSpliceLocation, diag::note_backslash_newline_eof)
+            << FixItHint::CreateRemoval(getSourceLocation(
+                   LastSpliceLocation, *LastSpliceLocation == '\\' ? 1 : 3));
+      }
+    }
   }
 
   BufferPtr = CurPtr;
diff --git a/clang/test/CXX/drs/cwg16xx.cpp b/clang/test/CXX/drs/cwg16xx.cpp
index cf6b45ceabf2c..dca941fa30624 100644
--- a/clang/test/CXX/drs/cwg16xx.cpp
+++ b/clang/test/CXX/drs/cwg16xx.cpp
@@ -536,3 +536,12 @@ namespace cwg1696 { // cwg1696: 7
   };
 #endif
 }
+
+// cwg1698: yes
+// This file intentionally does not end in a newline
+// to facilitate the CWG1698 test
+
+// cxx98-error at +3 {{no newline at end of file}}
+// expected-error at +2 {{expected unqualified-id}}
+
+\
\ No newline at end of file
diff --git a/clang/test/CXX/drs/cwg2747.cpp b/clang/test/CXX/drs/cwg2747.cpp
new file mode 100644
index 0000000000000..3f971452c5193
--- /dev/null
+++ b/clang/test/CXX/drs/cwg2747.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++11 -pedantic-errors -verify=expected %s -E | FileCheck %s --strict-whitespace --allow-empty
+
+// expected-no-diagnostics
+// cwg2747: yes
+
+// Check that a newline is still added even though there is a
+// physical newline at the end of the file (which is spliced)
+// CHECK: int x;{{$[[:space:]]^}}int y;int z;{{$[[:space:]]^$}}
+int x;
+int y;\
+int z;\
diff --git a/clang/test/CXX/drs/cwg27xx.cpp b/clang/test/CXX/drs/cwg27xx.cpp
index 406c8ea41f3b2..6e47af6333569 100644
--- a/clang/test/CXX/drs/cwg27xx.cpp
+++ b/clang/test/CXX/drs/cwg27xx.cpp
@@ -18,6 +18,8 @@ void f(B b) {
 struct D : B {};
 } // namespace cwg2718
 
+// cwg2747 is in cwg2747.cpp
+
 namespace cwg2759 { // cwg2759: 19
 #if __cplusplus >= 201103L
 
diff --git a/clang/test/Preprocessor/backslash_newline_eof.c b/clang/test/Preprocessor/backslash_newline_eof.c
new file mode 100644
index 0000000000000..b41bc92a42514
--- /dev/null
+++ b/clang/test/Preprocessor/backslash_newline_eof.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -x c -fsyntax-only -pedantic -verify %s
+// RUN: %clang_cc1 -x c++ -fsyntax-only -std=c++98 -pedantic -verify %s
+// RUN: %clang_cc1 -x c++ -fsyntax-only -std=c++11 -Wc++98-compat-pedantic -verify=cxx11-compat %s
+// RUN: %clang_cc1 -x c++ -fsyntax-only -std=c++11 -verify=cxx11 %s
+
+// cxx11-no-diagnostics
+
+// expected-warning at +4 {{no newline at end of file}}
+// expected-note at +3 {{last newline deleted by splice here}}
+// cxx11-compat-warning at +2 {{C++98 requires newline at end of file}}
+// cxx11-compat-note at +1 {{last newline deleted by splice here}}
+int x; \
diff --git a/clang/test/Preprocessor/backslash_without_newline.c b/clang/test/Preprocessor/backslash_without_newline.c
new file mode 100644
index 0000000000000..84be4e04b54ad
--- /dev/null
+++ b/clang/test/Preprocessor/backslash_without_newline.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -x c -E -o - %s | FileCheck %s
+// RUN: %clang_cc1 -x c++ -E -o - %s | FileCheck %s
+// RUN: %clang_cc1 -x c++ -E -o - %s | FileCheck %s
+
+#include "./backslash_without_newline.h"
+
+// CHECK: A B \ C
+A BACKSLASH_WITH_NEWLINE B BACKSLASH_WITHOUT_NEWLINE C
diff --git a/clang/test/Preprocessor/backslash_without_newline.h b/clang/test/Preprocessor/backslash_without_newline.h
new file mode 100644
index 0000000000000..db21184332c3b
--- /dev/null
+++ b/clang/test/Preprocessor/backslash_without_newline.h
@@ -0,0 +1,4 @@
+#define BACKSLASH_WITH_NEWLINE \
+
+// This file intentionally does not end with a newline.
+#define BACKSLASH_WITHOUT_NEWLINE \
\ No newline at end of file
diff --git a/clang/www/cxx_dr_status.html b/clang/www/cxx_dr_status.html
index 937f67981e296..2da096b746611 100755
--- a/clang/www/cxx_dr_status.html
+++ b/clang/www/cxx_dr_status.html
@@ -10003,7 +10003,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
     <td><a href="https://cplusplus.github.io/CWG/issues/1698.html">1698</a></td>
     <td>DRWP</td>
     <td>Files ending in <TT>\</TT></td>
-    <td class="unknown" align="center">Unknown</td>
+    <td class="full" align="center">Yes</td>
   </tr>
   <tr class="open" id="1699">
     <td><a href="https://cplusplus.github.io/CWG/issues/1699.html">1699</a></td>
@@ -16297,7 +16297,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
     <td><a href="https://cplusplus.github.io/CWG/issues/2747.html">2747</a></td>
     <td>DRWP</td>
     <td>Cannot depend on an already-deleted splice</td>
-    <td class="unknown" align="center">Unknown</td>
+    <td class="full" align="center">Yes</td>
   </tr>
   <tr id="2748">
     <td><a href="https://cplusplus.github.io/CWG/issues/2748.html">2748</a></td>

``````````

</details>


https://github.com/llvm/llvm-project/pull/97585


More information about the cfe-commits mailing list