[llvm] [FileCheck] Handle CHECK-EMPTY at EOF without trailing newline (PR #196576)

via llvm-commits llvm-commits at lists.llvm.org
Fri May 8 09:49:57 PDT 2026


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

## Summary

  Fix a FileCheck assertion when a check file ends immediately after a
  `CHECK-EMPTY:` directive without a trailing newline.

  The parser could reach the EOF boundary after consuming the check suffix and hit
  an internal assertion instead of producing the normal diagnostic. This change
  handles the EOF case consistently with the newline-terminated form, so FileCheck
  reports the existing `CHECK-EMPTY` diagnostic rather than crashing.

  ## Tests

  - `build/bin/llvm-lit -sv llvm/test/FileCheck/check-empty-tag.txt`

  Fixes #101582

>From f77f3464bd6f58862c4961503cdedae7b49def63 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 CHECK-EMPTY at EOF without trailing
 newline

---
 llvm/lib/FileCheck/FileCheck.cpp        | 10 +++++++---
 llvm/test/FileCheck/check-empty-tag.txt |  7 +++++++
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index d50e5d9cb088b..28000c7cae038 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -1903,8 +1903,10 @@ 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!");
 
     // Location to use for error messages.
@@ -1912,8 +1914,10 @@ bool FileCheck::readCheckFile(
 
     // 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;
+    bool AfterSuffixAtEOF = AfterSuffix.data() == BufferEnd;
+    Buffer = (AfterSuffix.empty() && !AfterSuffixAtEOF)
+                 ? Buffer.drop_front(UsedPrefix.size())
+                 : AfterSuffix;
 
     // Complain about misspelled directives.
     if (CheckTy == Check::CheckMisspelled) {
diff --git a/llvm/test/FileCheck/check-empty-tag.txt b/llvm/test/FileCheck/check-empty-tag.txt
index c398bcf596fcf..b95410ab4619a 100644
--- a/llvm/test/FileCheck/check-empty-tag.txt
+++ b/llvm/test/FileCheck/check-empty-tag.txt
@@ -31,6 +31,13 @@ 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-NOT and CHECK-NOT-EMPTY rejected
 ; RUN: %ProtectFileCheckOutput \
 ; RUN: not FileCheck %s --input-file %s --check-prefixes=CHECK5A 2>&1 \



More information about the llvm-commits mailing list