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

via llvm-commits llvm-commits at lists.llvm.org
Wed May 15 04:37:05 PDT 2024


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

First commit fixes already existing check with wrong regex, this should break few tests: like https://github.com/llvm/llvm-project/blob/7621a0d36465cf870769cd54035d254d409c2ce4/llvm/test/CodeGen/AArch64/Atomics/aarch64-atomic-load-outline_atomics.ll#L3

Second commit forbids prefixes ends with directive name, like here, to prevent ambiguity: https://github.com/llvm/llvm-project/blob/de18f5ecf80ef7183625c80b04445c614a17c483/lld/test/ELF/arm-exidx-shared.s#L5

>From 9b015227558560d2c72b4587f83f775287cf3d49 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 ba702aa42e413657cffbfc3970a0eedf2a97a33d 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..0d511611511d2 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 ends with "
+               << "directive: '" << Directive << "', prefix: '" << Prefix
+               << "'\n";
+        return false;
+      }
+    }
   }
   return true;
 }



More information about the llvm-commits mailing list