-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[FileCheck] improve prefix validation #92248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2468,24 +2468,36 @@ 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 " | ||
<< "string\n"; | ||
return false; | ||
} | ||
static const Regex Validator("^[a-zA-Z0-9_-]*$"); | ||
// TODO: restrict prefixes to start with only letter eventually | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please delete this TODO. We DO NOT want to restrict this: there is no benefit to doing so. Please move adjusting the error message to match the actual behaviour into a separate PR, as it is not related to "improving prefix validation". |
||
static const Regex Validator("^[a-zA-Z0-9][a-zA-Z0-9_-]*$"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you want to change the behaviour? What's wrong with a leading underscore or dash? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expected for prefixes to be kind-of-ident (which can't start from digits and dashes, but digits actually used, bc no check for that was added, oh, well), and be similar to substitution blocks (which should be Less restrictions makes it harder to catch possible errors in directives; currently possible typos simply ignored. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In most languages, identifiers can start with a leading underscore (this is sometimes reserved by the system, but it's still a valid identifier). FileCheck's behaviour essentially treats dashes like underscores (hence why custom check prefixes can contain them). Have you seen any examples of typos where the problem was a leading underscore/dash/digit? |
||
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) { | ||
errs() << "error: supplied " << Kind << " prefix must be unique among " | ||
<< "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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change the variable name and message here to use "suffix", per the previous request. |
||
<< "'\n"; | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There still some unresolved error, working on it
|
||
|
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a place that checks for some (but not all) cases of multiple suffixes. That place should use the same list of suffixes that this function does, that is, there should be one list that is shared between them. Ideally that other place would also be enhanced to check for all combinations, but that can wait for another patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which place exactly? I don't very knowledgeable about codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Search for "DAG-NOT" and you will see the place I mean. Looks like it specifically checks for NOT combined with anything else, but not other combinations. FileCheck does not have defined semantics for any combination of suffixes.