[llvm] [FileCheck] Handle directives at EOF without a trailing newline (PR #196576)

via llvm-commits llvm-commits at lists.llvm.org
Sun May 10 13:34:01 PDT 2026


https://github.com/savchart updated https://github.com/llvm/llvm-project/pull/196576

>From cff4a35247925d2bc222e89d63694d7df19c6b7b Mon Sep 17 00:00:00 2001
From: savchart <savchart at fit.cvut.cz>
Date: Fri, 8 May 2026 18:22:31 +0200
Subject: [PATCH] [FileCheck] Handle directives at EOF without trailing newline

---
 llvm/lib/FileCheck/FileCheck.cpp             | 12 ++++++------
 llvm/test/FileCheck/check-empty-tag.txt      | 12 ++++++++++++
 llvm/test/FileCheck/check-eof-no-pattern.txt |  7 +++++++
 3 files changed, 25 insertions(+), 6 deletions(-)
 create mode 100644 llvm/test/FileCheck/check-eof-no-pattern.txt

diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index d50e5d9cb088b..17c5d8c68ed4d 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -1903,18 +1903,18 @@ bool FileCheck::readCheckFile(
     assert(UsedPrefix.data() == Buffer.data() &&
            "Failed to move Buffer's start forward, or pointed prefix outside "
            "of the buffer!");
+
+    const char *BufferEnd = Buffer.data() + Buffer.size();
     assert(AfterSuffix.data() >= Buffer.data() &&
-           AfterSuffix.data() < Buffer.data() + Buffer.size() &&
+           AfterSuffix.data() <= BufferEnd &&
            "Parsing after suffix doesn't start inside of buffer!");
 
+    // Skip the buffer to the end of the parsed directive suffix.
+    Buffer = AfterSuffix;
+
     // Location to use for error messages.
     const char *UsedPrefixStart = UsedPrefix.data();
 
-    // Skip the buffer to the end of parsed suffix (or just prefix, if no good
-    // suffix was processed).
-    Buffer = AfterSuffix.empty() ? Buffer.drop_front(UsedPrefix.size())
-                                 : AfterSuffix;
-
     // Complain about misspelled directives.
     if (CheckTy == Check::CheckMisspelled) {
       StringRef UsedDirective(UsedPrefix.data(),
diff --git a/llvm/test/FileCheck/check-empty-tag.txt b/llvm/test/FileCheck/check-empty-tag.txt
index c398bcf596fcf..7818fc75d0fd5 100644
--- a/llvm/test/FileCheck/check-empty-tag.txt
+++ b/llvm/test/FileCheck/check-empty-tag.txt
@@ -31,6 +31,18 @@ CHECK3B: found non-empty check string for empty check with prefix 'CHECK3A:'
 CHECK4A-EMPTY:
 CHECK4B: found 'CHECK4A-EMPTY' without previous 'CHECK4A: line
 
+; CHECK-EMPTY at EOF without trailing newline cannot be the first check.
+; RUN: printf "# CHECK-EMPTY:" > %t.no-newline
+; RUN: %ProtectFileCheckOutput \
+; RUN: not FileCheck --allow-empty --input-file=/dev/null %t.no-newline 2>&1 \
+; RUN: | FileCheck %s --check-prefix=CHECK4C
+CHECK4C: found 'CHECK-EMPTY' without previous 'CHECK: line
+
+; CHECK-EMPTY at EOF without trailing newline still works after a prior check.
+; RUN: printf "foo\n\n" > %t.input
+; RUN: printf "CHECK: foo\nCHECK-EMPTY:" > %t.success
+; RUN: FileCheck --allow-empty --input-file=%t.input %t.success
+
 ; CHECK-EMPTY-NOT and CHECK-NOT-EMPTY rejected
 ; RUN: %ProtectFileCheckOutput \
 ; RUN: not FileCheck %s --input-file %s --check-prefixes=CHECK5A 2>&1 \
diff --git a/llvm/test/FileCheck/check-eof-no-pattern.txt b/llvm/test/FileCheck/check-eof-no-pattern.txt
new file mode 100644
index 0000000000000..e5e9c62e0d8b5
--- /dev/null
+++ b/llvm/test/FileCheck/check-eof-no-pattern.txt
@@ -0,0 +1,7 @@
+; CHECK requires a pattern, so the no-newline EOF case must fail here.
+; RUN: printf "CHECK:" > %t.no-newline
+; RUN: %ProtectFileCheckOutput \
+; RUN: not FileCheck --allow-empty --input-file=/dev/null %t.no-newline 2>&1 \
+; RUN: | FileCheck %s --check-prefix=ERR-EMPTY-CHECK
+
+ERR-EMPTY-CHECK: error: found empty check string



More information about the llvm-commits mailing list