[llvm] 871d658 - [FileCheck] Report missing prefixes when more than one is provided.

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 30 12:39:41 PDT 2020


Author: Mircea Trofin
Date: 2020-10-30T12:39:29-07:00
New Revision: 871d658c9ceb391df34e43d8f7af596c0b8a36df

URL: https://github.com/llvm/llvm-project/commit/871d658c9ceb391df34e43d8f7af596c0b8a36df
DIFF: https://github.com/llvm/llvm-project/commit/871d658c9ceb391df34e43d8f7af596c0b8a36df.diff

LOG: [FileCheck] Report missing prefixes when more than one is provided.

If more than a prefix is provided - e.g. --check-prefixes=CHECK,FOO - we
don't report if (say) FOO is never used. This may lead to a gap in our
test coverage.

This patch introduces a new option, --allow-unused-prefixes. It
currently is set to true, keeping today's behavior. After we explicitly
set it in tests where this behavior was actually intentional, we will
switch it to false by default.

Differential Revision: https://reviews.llvm.org/D90281

Added: 
    llvm/test/FileCheck/Inputs/one-check.txt
    llvm/test/FileCheck/allow-unused-prefixes.txt

Modified: 
    llvm/include/llvm/FileCheck/FileCheck.h
    llvm/lib/FileCheck/FileCheck.cpp
    llvm/utils/FileCheck/FileCheck.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/FileCheck/FileCheck.h b/llvm/include/llvm/FileCheck/FileCheck.h
index f9fd3d72f888..b10db710ef38 100644
--- a/llvm/include/llvm/FileCheck/FileCheck.h
+++ b/llvm/include/llvm/FileCheck/FileCheck.h
@@ -30,6 +30,7 @@ struct FileCheckRequest {
   std::vector<StringRef> ImplicitCheckNot;
   std::vector<StringRef> GlobalDefines;
   bool AllowEmptyInput = false;
+  bool AllowUnusedPrefixes = false;
   bool MatchFullLines = false;
   bool IgnoreCase = false;
   bool IsDefaultCheckPrefix = false;

diff  --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index a0371cf88b3c..8e234b2c72a8 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -22,6 +22,7 @@
 #include "llvm/Support/FormatVariadic.h"
 #include <cstdint>
 #include <list>
+#include <set>
 #include <tuple>
 #include <utility>
 
@@ -1825,8 +1826,10 @@ bool FileCheck::readCheckFile(
   // found.
   unsigned LineNumber = 1;
 
-  bool FoundUsedCheckPrefix = false;
-  while (1) {
+  std::set<StringRef> PrefixesNotFound(Req.CheckPrefixes.begin(),
+                                       Req.CheckPrefixes.end());
+  const size_t DistinctPrefixes = PrefixesNotFound.size();
+  while (true) {
     Check::FileCheckType CheckTy;
 
     // See if a prefix occurs in the memory buffer.
@@ -1837,7 +1840,7 @@ bool FileCheck::readCheckFile(
     if (UsedPrefix.empty())
       break;
     if (CheckTy != Check::CheckComment)
-      FoundUsedCheckPrefix = true;
+      PrefixesNotFound.erase(UsedPrefix);
 
     assert(UsedPrefix.data() == Buffer.data() &&
            "Failed to move Buffer's start forward, or pointed prefix outside "
@@ -1930,14 +1933,19 @@ bool FileCheck::readCheckFile(
 
   // When there are no used prefixes we report an error except in the case that
   // no prefix is specified explicitly but -implicit-check-not is specified.
-  if (!FoundUsedCheckPrefix &&
+  const bool NoPrefixesFound = PrefixesNotFound.size() == DistinctPrefixes;
+  const bool SomePrefixesUnexpectedlyNotUsed =
+      !Req.AllowUnusedPrefixes && !PrefixesNotFound.empty();
+  if ((NoPrefixesFound || SomePrefixesUnexpectedlyNotUsed) &&
       (ImplicitNegativeChecks.empty() || !Req.IsDefaultCheckPrefix)) {
     errs() << "error: no check strings found with prefix"
-           << (Req.CheckPrefixes.size() > 1 ? "es " : " ");
-    for (size_t I = 0, E = Req.CheckPrefixes.size(); I != E; ++I) {
-      if (I != 0)
+           << (PrefixesNotFound.size() > 1 ? "es " : " ");
+    bool First = true;
+    for (StringRef MissingPrefix : PrefixesNotFound) {
+      if (!First)
         errs() << ", ";
-      errs() << "\'" << Req.CheckPrefixes[I] << ":'";
+      errs() << "\'" << MissingPrefix << ":'";
+      First = false;
     }
     errs() << '\n';
     return true;

diff  --git a/llvm/test/FileCheck/Inputs/one-check.txt b/llvm/test/FileCheck/Inputs/one-check.txt
new file mode 100644
index 000000000000..1cdefaff6585
--- /dev/null
+++ b/llvm/test/FileCheck/Inputs/one-check.txt
@@ -0,0 +1,2 @@
+hello
+; P1: hello
\ No newline at end of file

diff  --git a/llvm/test/FileCheck/allow-unused-prefixes.txt b/llvm/test/FileCheck/allow-unused-prefixes.txt
new file mode 100644
index 000000000000..a9693fb2fc03
--- /dev/null
+++ b/llvm/test/FileCheck/allow-unused-prefixes.txt
@@ -0,0 +1,10 @@
+; RUN: not FileCheck --allow-unused-prefixes=false --check-prefixes=P1,P2 --input-file %S/Inputs/one-check.txt %S/Inputs/one-check.txt 2>&1 | FileCheck --check-prefix=MISSING-ONE %s
+; RUN: not FileCheck --allow-unused-prefixes=false --check-prefixes=P1,P2,P3 --input-file %S/Inputs/one-check.txt %S/Inputs/one-check.txt 2>&1 | FileCheck --check-prefix=MISSING-MORE %s
+; RUN: FileCheck --allow-unused-prefixes=true  --check-prefixes=P1,P2 --input-file %S/Inputs/one-check.txt %S/Inputs/one-check.txt 
+
+; Note: the default will be changed to 'false', at which time this run line
+; should be changed accordingly.
+; RUN: FileCheck --check-prefixes=P1,P2 --input-file %S/Inputs/one-check.txt %S/Inputs/one-check.txt 
+
+; MISSING-ONE: error: no check strings found with prefix 'P2:' 
+; MISSING-MORE: error: no check strings found with prefixes 'P2:', 'P3:' 
\ No newline at end of file

diff  --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index b48ae0b10c9a..1721b23ad7b5 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/utils/FileCheck/FileCheck.cpp
@@ -77,6 +77,10 @@ static cl::opt<bool> AllowEmptyInput(
     cl::desc("Allow the input file to be empty. This is useful when making\n"
              "checks that some error message does not occur, for example."));
 
+static cl::opt<bool> AllowUnusedPrefixes(
+    "allow-unused-prefixes", cl::init(true),
+    cl::desc("Allow prefixes to be specified but not appear in the test."));
+
 static cl::opt<bool> MatchFullLines(
     "match-full-lines", cl::init(false),
     cl::desc("Require all positive matches to cover an entire input line.\n"
@@ -771,6 +775,7 @@ int main(int argc, char **argv) {
     return 2;
 
   Req.AllowEmptyInput = AllowEmptyInput;
+  Req.AllowUnusedPrefixes = AllowUnusedPrefixes;
   Req.EnableVarScope = EnableVarScope;
   Req.AllowDeprecatedDagOverlap = AllowDeprecatedDagOverlap;
   Req.Verbose = Verbose;


        


More information about the llvm-commits mailing list