[llvm] [FileCheck] improve prefix validation (PR #92248)

via llvm-commits llvm-commits at lists.llvm.org
Wed May 15 08:09:28 PDT 2024


https://github.com/klensy updated https://github.com/llvm/llvm-project/pull/92248

>From afd946c9123265383ef4e028bcd35864b099870f Mon Sep 17 00:00:00 2001
From: klensy <klensy at users.noreply.github.com>
Date: Wed, 15 May 2024 13:52:26 +0300
Subject: [PATCH 1/2] fix prefix regex validation

"prefix must start with a letter and contain only alphanumeric characters, hyphens, and underscores"
but current regex didn't enforced that rule, fix this.
---
 llvm/lib/FileCheck/FileCheck.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index 1719f8ef2b436..3e69d6d5b76c7 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -2474,7 +2474,7 @@ static bool ValidatePrefixes(StringRef Kind, StringSet<> &UniquePrefixes,
              << "string\n";
       return false;
     }
-    static const Regex Validator("^[a-zA-Z0-9_-]*$");
+    static const Regex Validator("^[a-zA-Z][a-zA-Z0-9_-]*$");
     if (!Validator.match(Prefix)) {
       errs() << "error: supplied " << Kind << " prefix must start with a "
              << "letter and contain only alphanumeric characters, hyphens, and "

>From b688d5e880623bcc8986029a35977d642dcd5796 Mon Sep 17 00:00:00 2001
From: klensy <klensy at users.noreply.github.com>
Date: Wed, 15 May 2024 14:30:55 +0300
Subject: [PATCH 2/2] filecheck: forbid filecheck prefix to ends with directive
 name

---
 llvm/lib/FileCheck/FileCheck.cpp | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index 3e69d6d5b76c7..a48a15bf6d97b 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -2468,6 +2468,9 @@ FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
 
 static bool ValidatePrefixes(StringRef Kind, StringSet<> &UniquePrefixes,
                              ArrayRef<StringRef> SuppliedPrefixes) {
+  static const char *Directives[] = {"-NEXT",  "-SAME", "-EMPTY", "-NOT",
+                                     "-COUNT", "-DAG",  "-LABEL"};
+
   for (StringRef Prefix : SuppliedPrefixes) {
     if (Prefix.empty()) {
       errs() << "error: supplied " << Kind << " prefix must not be the empty "
@@ -2486,6 +2489,14 @@ static bool ValidatePrefixes(StringRef Kind, StringSet<> &UniquePrefixes,
              << "check and comment prefixes: '" << Prefix << "'\n";
       return false;
     }
+    for (StringRef Directive : Directives) {
+      if (Prefix.ends_with(Directive)) {
+        errs() << "error: supplied " << Kind << " prefix must not end with "
+               << "directive: '" << Directive << "', prefix: '" << Prefix
+               << "'\n";
+        return false;
+      }
+    }
   }
   return true;
 }



More information about the llvm-commits mailing list