This is an archive of the discontinued LLVM Phabricator instance.

[FileCheck] Fix PR49531: invalid use of string var
ClosedPublic

Authored by thopre on Mar 16 2021, 3:23 AM.

Details

Summary

FileCheck string substitution block parsing code only report an invalid
variable name in a string variable use if it starts with a forbidden
character. It does not report anything if there are unparsed characters
after the variable name, i.e. [[X-Y]] is parsed as [[X]] and no error is
returned. This commit fixes that.

Diff Detail

Event Timeline

thopre created this revision.Mar 16 2021, 3:23 AM
thopre requested review of this revision.Mar 16 2021, 3:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 16 2021, 3:23 AM
jhenderson added inline comments.Mar 17 2021, 1:54 AM
llvm/lib/FileCheck/FileCheck.cpp
460

I wonder if it would make more sense to add the check to this function, somewhere around here. This is the "parseVariable" function after all, so seems like the logical place to reject invalid variable names, rather than the place it is currently detected. What do you think?

llvm/test/FileCheck/simple-var-capture.txt
22

I would suggest you also want an invalid variable name which is a definition (e.g. [[X-Y:.+]]) or something like that.

llvm/unittests/FileCheck/FileCheckTest.cpp
1347

Ditto - consider also checking for a definition with an invalid name.

thopre added inline comments.Mar 17 2021, 3:17 AM
llvm/lib/FileCheck/FileCheck.cpp
460

A trailer has to be allowed when parsing a variable use because it could be [[@LINE+1]]. If the variable is a pseudo variable, parsing is deferred to the numeric substitution block parsing. Otherwise any trailer is an error. Perhaps it's time to remove all uses of [[@LINE]] in favor of [[#@LINE]]

llvm/test/FileCheck/simple-var-capture.txt
22

The new code is only executed for string variable use. String variable definition and pseudo variable use different codepath.

llvm/unittests/FileCheck/FileCheckTest.cpp
1347

See line 1339 for a test for that already.

jdenny accepted this revision.Mar 17 2021, 9:37 AM

LGTM if @jhenderson is happy.

This revision is now accepted and ready to land.Mar 17 2021, 9:37 AM
jhenderson added inline comments.Mar 18 2021, 1:39 AM
llvm/lib/FileCheck/FileCheck.cpp
460

This function already knows whether it is a pseudo variable, so doesn't that mean you can do something like if(!IsPseudo) /*error if trailer*/?

thopre added inline comments.Mar 18 2021, 2:26 AM
llvm/lib/FileCheck/FileCheck.cpp
460

parseVariable is also called for numeric expression where it's fine to have a trailer after a variable ([[#FOO+2]]). I would need to distinguish 3 states:

  • No trailer (e.g. string variable def and use)
  • Trailer for pseudo only (Legacy @LINE expression)
  • Trailer allowed (numeric expression)

That's why I suggested removing legacy @LINE expression, it would reduce this to 2 options which can be dealt with a AllowTrailer (false by default) parameter.

jhenderson accepted this revision.Mar 18 2021, 2:31 AM

LGTM! I don't think there's a pressing need to get rid of the legacy pseudo variables, but I'm also not opposed to it, if you think it's worth doing.

llvm/lib/FileCheck/FileCheck.cpp
460

Ah, thanks. Didn't realise this was used elsewhere.

Sorry it seems I forgot to run the whole testsuite when I put this up for review. The patch found 3 issues:

https://reviews.llvm.org/D98852
https://reviews.llvm.org/D98853
https://reviews.llvm.org/D98854

Sorry it seems I forgot to run the whole testsuite when I put this up for review. The patch found 3 issues:

https://reviews.llvm.org/D98852
https://reviews.llvm.org/D98853
https://reviews.llvm.org/D98854

Ah, it's good those are now being caught. Thanks for working on all this!

This revision was landed with ongoing or failed builds.Mar 24 2021, 11:49 AM
This revision was automatically updated to reflect the committed changes.