Skip to content

Commit 627836c

Browse files
committed
Recommit: Detect incorrect FileCheck variable CLI definition
Summary: While the backend code of FileCheck relies on definition of variable from the command-line to have an equal sign '=' and a variable name before that, the frontend does not actually enforce it. This leads to FileCheck crashing when invoked with invalid syntax for the -D option. This patch adds the missing validation in the frontend. It also makes the -D option an AlwaysPrefix option to be able to detect -D=FOO as being a define without variable and -D as missing its value. Copyright: - Linaro (changes in version 2 of revision D55940) - GraphCore (changes in later versions) Reviewers: jdenny Subscribers: JonChesterfield, hiraditya, kristina, probinson, llvm-commits Differential Revision: https://reviews.llvm.org/D55940 llvm-svn: 353173 (cherry picked from LLVM commit a5e233bf798e8045771aa20af0e8b39cb60a6d73)
1 parent 8b0300b commit 627836c

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

test/FileCheck/defines.txt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
; RUN: FileCheck -DVALUE=10 -input-file %s %s
22
; RUN: not FileCheck -DVALUE=20 -input-file %s %s 2>&1 | FileCheck %s -check-prefix ERRMSG
3+
; RUN: not FileCheck -DVALUE10 -input-file %s %s 2>&1 | FileCheck %s -check-prefix ERRCLIEQ1
4+
; RUN: not FileCheck -D -input-file %s %s 2>&1 | FileCheck %s -check-prefix ERRCLIEQ2
5+
; RUN: not FileCheck -D=10 -input-file %s %s 2>&1 | FileCheck %s -check-prefix ERRCLIVAR1
6+
; RUN: not FileCheck -D= -input-file %s %s 2>&1 | FileCheck %s -check-prefix ERRCLIVAR2
7+
; RUN: FileCheck -DVALUE= -check-prefix EMPTY -input-file %s %s 2>&1
38

49
Value = 10
510
; CHECK: Value = [[VALUE]]
611

7-
; ERRMSG: defines.txt:5:10: error: expected string not found in input
12+
; ERRMSG: defines.txt:[[@LINE-2]]:10: error: expected string not found in input
813
; ERRMSG: defines.txt:1:1: note: with variable "VALUE" equal to "20"
9-
; ERRMSG: defines.txt:4:1: note: possible intended match here
14+
; ERRMSG: defines.txt:[[@LINE-5]]:1: note: possible intended match here
15+
16+
; ERRCLIEQ1: Missing equal sign in command-line definition '-DVALUE10'
17+
18+
; ERRCLIEQ2: FileCheck{{[^:]*}}: for the -D option: requires a value!
19+
20+
; ERRCLIVAR1: Missing pattern variable name in command-line definition '-D=10'
21+
22+
; ERRCLIVAR2: Missing pattern variable name in command-line definition '-D='
23+
24+
Empty value = @@
25+
; EMPTY: Empty value = @[[VALUE]]@

utils/FileCheck/FileCheck.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static cl::list<std::string> ImplicitCheckNot(
7070
cl::value_desc("pattern"));
7171

7272
static cl::list<std::string>
73-
GlobalDefines("D", cl::Prefix,
73+
GlobalDefines("D", cl::AlwaysPrefix,
7474
cl::desc("Define a variable to be used in capture patterns."),
7575
cl::value_desc("VAR=VALUE"));
7676

@@ -1364,8 +1364,25 @@ int main(int argc, char **argv) {
13641364
/// VariableTable - This holds all the current filecheck variables.
13651365
StringMap<StringRef> VariableTable;
13661366

1367-
for (const auto &Def : GlobalDefines)
1368-
VariableTable.insert(StringRef(Def).split('='));
1367+
bool GlobalDefineError = false;
1368+
for (const auto &G : GlobalDefines) {
1369+
size_t EqIdx = G.find('=');
1370+
if (EqIdx == std::string::npos) {
1371+
errs() << "Missing equal sign in command-line definition '-D" << G
1372+
<< "'\n";
1373+
GlobalDefineError = true;
1374+
continue;
1375+
}
1376+
if (EqIdx == 0) {
1377+
errs() << "Missing pattern variable name in command-line definition '-D"
1378+
<< G << "'\n";
1379+
GlobalDefineError = true;
1380+
continue;
1381+
}
1382+
VariableTable.insert(StringRef(G).split('='));
1383+
}
1384+
if (GlobalDefineError)
1385+
return 2;
13691386

13701387
bool hasError = false;
13711388

0 commit comments

Comments
 (0)