[llvm] [FileCheck] improve prefix validation (PR #92248)
via llvm-commits
llvm-commits at lists.llvm.org
Sun May 19 06:58:47 PDT 2024
https://github.com/klensy updated https://github.com/llvm/llvm-project/pull/92248
>From f6e9d935f01176d2f65aa5d291278faff1650f71 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/3] fix prefix regex validation
"prefix must start with a letter and contain only alphanumeric characters, hyphens, and underscores"
But actual prefixes starting with digit quite common, so allow them too, but forbid to start prefix with hyphen and underscore.
---
llvm/lib/FileCheck/FileCheck.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index 1719f8ef2b436..fb65731974cac 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -2474,11 +2474,12 @@ static bool ValidatePrefixes(StringRef Kind, StringSet<> &UniquePrefixes,
<< "string\n";
return false;
}
- static const Regex Validator("^[a-zA-Z0-9_-]*$");
+ // TODO: restrict prefixes to start with only letter eventually
+ static const Regex Validator("^[a-zA-Z0-9][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 "
- << "underscores: '" << Prefix << "'\n";
+ << "letter or digit and contain only ascii alphanumeric "
+ << "characters, hyphens, and underscores: '" << Prefix << "'\n";
return false;
}
if (!UniquePrefixes.insert(Prefix).second) {
>From 512b5a7a992cfb821612d5613ff8bc8c54f09748 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/3] 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 fb65731974cac..f5f56aa5a2883 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 *Suffixes[] = {"-NEXT", "-SAME", "-EMPTY", "-NOT",
+ "-COUNT", "-DAG", "-LABEL"};
+
for (StringRef Prefix : SuppliedPrefixes) {
if (Prefix.empty()) {
errs() << "error: supplied " << Kind << " prefix must not be the empty "
@@ -2487,6 +2490,14 @@ static bool ValidatePrefixes(StringRef Kind, StringSet<> &UniquePrefixes,
<< "check and comment prefixes: '" << Prefix << "'\n";
return false;
}
+ for (StringRef Directive : Suffixes) {
+ if (Prefix.ends_with(Directive)) {
+ errs() << "error: supplied " << Kind << " prefix must not end with "
+ << "directive: '" << Directive << "', prefix: '" << Prefix
+ << "'\n";
+ return false;
+ }
+ }
}
return true;
}
>From 16ee61b4815e718cb597aacecbb10f1960e91fb4 Mon Sep 17 00:00:00 2001
From: klensy <klensy at users.noreply.github.com>
Date: Sun, 19 May 2024 15:54:31 +0300
Subject: [PATCH 3/3] filecheck: fix self tests for "forbid prefixes end with
directive name"
---
.../FileCheck/comment/bad-comment-prefix.txt | 14 +++++++-------
llvm/test/FileCheck/comment/suffixes.txt | 8 ++++----
llvm/test/FileCheck/numeric-expression.txt | 16 ++++++++--------
llvm/test/FileCheck/validate-check-prefix.txt | 2 +-
4 files changed, 20 insertions(+), 20 deletions(-)
diff --git a/llvm/test/FileCheck/comment/bad-comment-prefix.txt b/llvm/test/FileCheck/comment/bad-comment-prefix.txt
index 58a8873da3218..9d005e619c209 100644
--- a/llvm/test/FileCheck/comment/bad-comment-prefix.txt
+++ b/llvm/test/FileCheck/comment/bad-comment-prefix.txt
@@ -3,17 +3,17 @@
# Check empty comment prefix.
RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
RUN: -comment-prefixes= | \
-RUN: FileCheck -check-prefix=PREFIX-EMPTY %s
+RUN: FileCheck -check-prefix=EMPTY-PREFIX %s
RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
RUN: -comment-prefixes=,FOO | \
-RUN: FileCheck -check-prefix=PREFIX-EMPTY %s
+RUN: FileCheck -check-prefix=EMPTY-PREFIX %s
RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
RUN: -comment-prefixes=FOO, | \
-RUN: FileCheck -check-prefix=PREFIX-EMPTY %s
+RUN: FileCheck -check-prefix=EMPTY-PREFIX %s
RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
RUN: -comment-prefixes=FOO,,BAR | \
-RUN: FileCheck -check-prefix=PREFIX-EMPTY %s
-PREFIX-EMPTY: error: supplied comment prefix must not be the empty string
+RUN: FileCheck -check-prefix=EMPTY-PREFIX %s
+EMPTY-PREFIX: error: supplied comment prefix must not be the empty string
# Check invalid characters in comment prefix.
RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
@@ -22,8 +22,8 @@ RUN: FileCheck -check-prefix=PREFIX-BAD-CHAR1 %s
RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
RUN: -comment-prefixes='foo ' | \
RUN: FileCheck -check-prefix=PREFIX-BAD-CHAR2 %s
-PREFIX-BAD-CHAR1: error: supplied comment prefix must start with a letter and contain only alphanumeric characters, hyphens, and underscores: '.'
-PREFIX-BAD-CHAR2: error: supplied comment prefix must start with a letter and contain only alphanumeric characters, hyphens, and underscores: 'foo '
+PREFIX-BAD-CHAR1: error: supplied comment prefix must start with a letter or digit and contain only ascii alphanumeric characters, hyphens, and underscores: '.'
+PREFIX-BAD-CHAR2: error: supplied comment prefix must start with a letter or digit and contain only ascii alphanumeric characters, hyphens, and underscores: 'foo '
# Check duplicate comment prefixes.
RUN: %ProtectFileCheckOutput not FileCheck /dev/null < /dev/null 2>&1 \
diff --git a/llvm/test/FileCheck/comment/suffixes.txt b/llvm/test/FileCheck/comment/suffixes.txt
index 85b05fb5778cf..88a5c4cd3bf9e 100644
--- a/llvm/test/FileCheck/comment/suffixes.txt
+++ b/llvm/test/FileCheck/comment/suffixes.txt
@@ -1,5 +1,5 @@
-# Comment prefixes plus check directive suffixes are not comment directives
-# and are treated as plain text.
+# Comment prefixes plus check directive suffixes are forbidden.
+# FIXME: currently not verified bq ValidatePrefixes missing defaulted comment prefixes?
RUN: echo foo > %t.in
RUN: echo bar >> %t.in
@@ -12,11 +12,11 @@ RUN: FileCheck -check-prefix=CHECK1 %s
CHECK1: .chk:1:18: remark: CHECK: expected string found in input
CHECK1: .chk:2:17: remark: CHECK: expected string found in input
-# But we can define them as comment prefixes.
+# But we can define them as comment prefixes; still forbidden.
RUN: %ProtectFileCheckOutput \
RUN: FileCheck -dump-input=never -vv -comment-prefixes=COM,RUN,RUN-NOT %t.chk < %t.in 2>&1 | \
RUN: FileCheck -check-prefix=CHECK2 %s
-CHECK2: .chk:1:18: remark: CHECK: expected string found in input
+CHECK2: error: supplied comment prefix must not end with directive: '-NOT', prefix: 'RUN-NOT'
CHECK2-NOT: .chk:2
diff --git a/llvm/test/FileCheck/numeric-expression.txt b/llvm/test/FileCheck/numeric-expression.txt
index 1430484d08ebc..f23628f5fbc9a 100644
--- a/llvm/test/FileCheck/numeric-expression.txt
+++ b/llvm/test/FileCheck/numeric-expression.txt
@@ -593,16 +593,16 @@ CALL-MISSING-ARGUMENT-MSG-NEXT: {{C}}ALL-MISSING-ARGUMENT-NEXT: {{\[\[#add\(NUMV
CALL-MISSING-ARGUMENT-MSG-NEXT: {{^}} ^{{$}}
RUN: %ProtectFileCheckOutput \
-RUN: not FileCheck -D#NUMVAR=10 --check-prefix CALL-WRONG-ARGUMENT-COUNT --input-file %s %s 2>&1 \
-RUN: | FileCheck --strict-whitespace --check-prefix CALL-WRONG-ARGUMENT-COUNT-MSG %s
+RUN: not FileCheck -D#NUMVAR=10 --check-prefix CALL-WRONG-ARGUMENT-NUM --input-file %s %s 2>&1 \
+RUN: | FileCheck --strict-whitespace --check-prefix CALL-WRONG-ARGUMENT-NUM-MSG %s
-CALL WRONG ARGUMENT COUNT
+CALL WRONG ARGUMENT NUM
30
-CALL-WRONG-ARGUMENT-COUNT-LABEL: CALL WRONG ARGUMENT COUNT
-CALL-WRONG-ARGUMENT-COUNT-NEXT: [[#add(NUMVAR)]]
-CALL-WRONG-ARGUMENT-COUNT-MSG: numeric-expression.txt:[[#@LINE-1]]:36: error: function 'add' takes 2 arguments but 1 given
-CALL-WRONG-ARGUMENT-COUNT-MSG-NEXT: {{C}}ALL-WRONG-ARGUMENT-COUNT-NEXT: {{\[\[#add\(NUMVAR\)\]\]}}
-CALL-WRONG-ARGUMENT-COUNT-MSG-NEXT: {{^}} ^{{$}}
+CALL-WRONG-ARGUMENT-NUM-LABEL: CALL WRONG ARGUMENT NUM
+CALL-WRONG-ARGUMENT-NUM-NEXT: [[#add(NUMVAR)]]
+CALL-WRONG-ARGUMENT-NUM-MSG: numeric-expression.txt:[[#@LINE-1]]:34: error: function 'add' takes 2 arguments but 1 given
+CALL-WRONG-ARGUMENT-NUM-MSG-NEXT: {{C}}ALL-WRONG-ARGUMENT-NUM-NEXT: {{\[\[#add\(NUMVAR\)\]\]}}
+CALL-WRONG-ARGUMENT-NUM-MSG-NEXT: {{^}} ^{{$}}
RUN: %ProtectFileCheckOutput \
RUN: not FileCheck -D#NUMVAR=10 --check-prefix CALL-UNDEFINED-FUNCTION --input-file %s %s 2>&1 \
diff --git a/llvm/test/FileCheck/validate-check-prefix.txt b/llvm/test/FileCheck/validate-check-prefix.txt
index af43afded1b3e..b67c103b80a05 100644
--- a/llvm/test/FileCheck/validate-check-prefix.txt
+++ b/llvm/test/FileCheck/validate-check-prefix.txt
@@ -6,7 +6,7 @@
foobar
; A1a-B_c: foobar
-; BAD_PREFIX: supplied check prefix must start with a letter and contain only alphanumeric characters, hyphens, and underscores: 'A!'
+; BAD_PREFIX: supplied check prefix must start with a letter or digit and contain only ascii alphanumeric characters, hyphens, and underscores: 'A!'
; DUPLICATE_PREFIX: error: supplied check prefix must be unique among check and comment prefixes: 'REPEAT'
More information about the llvm-commits
mailing list