From 28db0415f3e47cb2ef0678d0c797b6f5f39baa4b Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Tue, 16 Mar 2021 10:19:52 +0000 Subject: [PATCH] [FileCheck] Fix PR49531: invalid use of string var 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. Reviewed By: jdenny, jhenderson Differential Revision: https://reviews.llvm.org/D98691 --- lib/FileCheck/FileCheck.cpp | 9 ++++++++- test/FileCheck/simple-var-capture.txt | 12 ++++++++++++ unittests/FileCheck/FileCheckTest.cpp | 3 +++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/FileCheck/FileCheck.cpp b/lib/FileCheck/FileCheck.cpp index bcf828d20ee..12a0ae63419 100644 --- a/lib/FileCheck/FileCheck.cpp +++ b/lib/FileCheck/FileCheck.cpp @@ -1083,8 +1083,15 @@ bool Pattern::parsePattern(StringRef PatternStr, StringRef Prefix, if (IsPseudo) { MatchStr = OrigMatchStr; IsLegacyLineExpr = IsNumBlock = true; - } else + } else { + if (!MatchStr.empty()) { + SM.PrintMessage(SMLoc::getFromPointer(Name.data()), + SourceMgr::DK_Error, + "invalid name in string variable use"); + return true; + } SubstStr = Name; + } } } diff --git a/test/FileCheck/simple-var-capture.txt b/test/FileCheck/simple-var-capture.txt index a487baaa531..d9f456130e0 100644 --- a/test/FileCheck/simple-var-capture.txt +++ b/test/FileCheck/simple-var-capture.txt @@ -11,3 +11,15 @@ op4 r30, r18, r21 ; CHECK-NEXT: op4 {{r[0-9]+}}, [[REGa]], [[REGb]] +// RUN: %ProtectFileCheckOutput \ +// RUN: not FileCheck --check-prefixes INVALID-VARNAME --input-file %s %s 2>&1 \ +// RUN: | FileCheck --check-prefix INVALID-VARNAME-MSG --strict-whitespace %s + +5 +4 +; INVALID-VARNAME: [[X:]] +; INVALID-VARNAME-NEXT: [[Y:]] +; INVALID-VARNAME-NEXT: [[X-Y]] +; INVALID-VARNAME-MSG: simple-var-capture.txt:[[#@LINE-1]]:27: error: invalid name in string variable use +; INVALID-VARNAME-MSG-NEXT: ; {{I}}NVALID-VARNAME-NEXT: {{\[\[X-Y\]\]}} +; INVALID-VARNAME-MSG-NEXT: {{^}} ^{{$}} diff --git a/unittests/FileCheck/FileCheckTest.cpp b/unittests/FileCheck/FileCheckTest.cpp index 299fbb8a798..f54b940d68c 100644 --- a/unittests/FileCheck/FileCheckTest.cpp +++ b/unittests/FileCheck/FileCheckTest.cpp @@ -1343,6 +1343,9 @@ TEST_F(FileCheckTest, ParsePattern) { // Collision with numeric variable. EXPECT_TRUE(Tester.parsePattern("[[FOO:]]")); + // Invalid use of string variable. + EXPECT_TRUE(Tester.parsePattern("[[FOO-BAR]]")); + // Valid use of string variable. EXPECT_FALSE(Tester.parsePattern("[[BAR]]"));