[llvm] [FileCheck]: Fix diagnostic for trailing CHECK-NOT (PR #78412)

Vinayak Dev via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 17 01:00:16 PST 2024


https://github.com/vinayakdsci created https://github.com/llvm/llvm-project/pull/78412

Fixes #70221

Fix a bug in FileCheck that corrects the error message when multiple prefixes are provided
through --check-prefixes and the trailing prefix is a PREFIX-NOT.

Earlier, only the first of the provided prefixes was displayed as the erroneous prefix, while the
actual error was on the prefix that occurred at the end of the prefix list in the input file.
 
Now, a variable keeps track of the last NOT prefix, and that prefix is used in the error message.

>From 0b47500d13a20b5d35cc2da8c2e34e4301b52e68 Mon Sep 17 00:00:00 2001
From: Vinayak Dev <vinayakdev.sci at gmail.com>
Date: Mon, 15 Jan 2024 22:40:44 +0530
Subject: [PATCH] [FileCheck]: Fix diagnostic for trailing CHECK-NOT

---
 llvm/lib/FileCheck/FileCheck.cpp                     |  6 ++++--
 .../FileCheck/check-not-custom-prefix-trailing.txt   | 12 ++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/FileCheck/check-not-custom-prefix-trailing.txt

diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index b728c14d288aa5..526adf3349f099 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -1814,6 +1814,7 @@ bool FileCheck::readCheckFile(
   }
 
   std::vector<Pattern> DagNotMatches = ImplicitNegativeChecks;
+  StringRef TrailingNotPrefix;
 
   // LineNumber keeps track of the line on which CheckPrefix instances are
   // found.
@@ -1927,6 +1928,7 @@ bool FileCheck::readCheckFile(
     // Handle CHECK-DAG/-NOT.
     if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
       DagNotMatches.push_back(P);
+      TrailingNotPrefix = UsedPrefix;
       continue;
     }
 
@@ -1957,11 +1959,11 @@ bool FileCheck::readCheckFile(
   }
 
   // Add an EOF pattern for any trailing --implicit-check-not/CHECK-DAG/-NOTs,
-  // and use the first prefix as a filler for the error message.
+  // and use the prefix from the last/trailing CHECK-NOT for the error message
   if (!DagNotMatches.empty()) {
     CheckStrings->emplace_back(
         Pattern(Check::CheckEOF, PatternContext.get(), LineNumber + 1),
-        *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data()));
+        TrailingNotPrefix, SMLoc::getFromPointer(Buffer.data()));
     std::swap(DagNotMatches, CheckStrings->back().DagNotStrings);
   }
 
diff --git a/llvm/test/FileCheck/check-not-custom-prefix-trailing.txt b/llvm/test/FileCheck/check-not-custom-prefix-trailing.txt
new file mode 100644
index 00000000000000..6f9d13538304d6
--- /dev/null
+++ b/llvm/test/FileCheck/check-not-custom-prefix-trailing.txt
@@ -0,0 +1,12 @@
+; RUN: rm %t && \
+; RUN: echo "LEADING: placeholder1" >>%t && echo "TRAILING-NOT: placeholder2" >>%t && \
+; RUN: %ProtectFileCheckOutput not FileCheck --strict-whitespace --check-prefixes LEADING,TRAILING --input-file %t %t 2>&1 | \ 
+; RUN: FileCheck %s
+; END
+
+CHECK:           error: TRAILING-NOT: excluded string found in input
+CHECK-NEXT:      TRAILING-NOT: placeholder2
+CHECK-NEXT: {{^}}              ^{{$}}
+CHECK-NEXT:      note: found here
+CHECK-NEXT:      TRAILING-NOT: placeholder2
+CHECK-NEXT: {{^}}              ^~~~~~~~~~~~{{$}}



More information about the llvm-commits mailing list