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

via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 5 09:29:36 PDT 2024


================
@@ -3183,8 +3193,35 @@ 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
+      StringRef WithoutLastNewline =
+          StringRef(BufferStart, LastNewline.data() - BufferStart);
+      while (!WithoutLastNewline.empty()) {
+        if (isHorizontalWhitespace(WithoutLastNewline.back())) {
+          WithoutLastNewline = WithoutLastNewline.drop_back();
+        } else {
+          break;
+        }
+      }
+
+      if (WithoutLastNewline.ends_with('\\') ||
+          (LangOpts.Trigraphs && WithoutLastNewline.ends_with("??"
+                                                              "/"))) {
----------------
Sirraide wrote:

> I had no idea `\?` was a valid escape sequence

Yeah, the entire point of it is basically exactly this.

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


More information about the cfe-commits mailing list