Skip to content

Commit 28196a5

Browse files
author
Thomas Preud'homme
committedJul 5, 2019
[FileCheck] Factor some parsing checks out
Summary: Both callers of parseNumericVariableDefinition() perform the same extra check that no character is found after the variable name. This patch factors out this check into parseNumericVariableDefinition(). Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson, rnk Subscribers: JonChesterfield, rogfer01, hfinkel, kristina, rnk, tra, arichardson, grimar, dblaikie, probinson, llvm-commits, hiraditya Tags: #llvm Differential Revision: https://reviews.llvm.org/D64226 llvm-svn: 365191
1 parent a188ad2 commit 28196a5

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed
 

‎llvm/lib/Support/FileCheck.cpp

+14-35
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ Error FileCheckPattern::parseNumericVariableDefinition(
130130
return FileCheckErrorDiagnostic::get(
131131
SM, Name, "string variable with name '" + Name + "' already exists");
132132

133+
Expr = Expr.ltrim(SpaceChars);
134+
if (!Expr.empty())
135+
return FileCheckErrorDiagnostic::get(
136+
SM, Expr, "unexpected characters after numeric variable name");
137+
133138
return Error::success();
134139
}
135140

@@ -229,27 +234,21 @@ Expected<FileCheckExpression *> FileCheckPattern::parseNumericSubstitutionBlock(
229234
size_t DefEnd = Expr.find(':');
230235
if (DefEnd != StringRef::npos) {
231236
StringRef DefExpr = Expr.substr(0, DefEnd);
232-
StringRef UseExpr = Expr = Expr.substr(DefEnd + 1);
237+
StringRef UseExpr = Expr.substr(DefEnd + 1);
233238

234-
DefExpr = DefExpr.ltrim(SpaceChars);
235-
StringRef Name;
236-
Error ErrorDiagnostic =
237-
parseNumericVariableDefinition(DefExpr, Name, Context, SM);
238-
if (ErrorDiagnostic)
239-
return std::move(ErrorDiagnostic);
240-
241-
DefinedNumericVariable =
242-
Context->makeNumericVariable(this->LineNumber, Name);
243-
244-
DefExpr = DefExpr.ltrim(SpaceChars);
245-
if (!DefExpr.empty())
246-
return FileCheckErrorDiagnostic::get(
247-
SM, DefExpr, "invalid numeric variable definition");
248239
UseExpr = UseExpr.ltrim(SpaceChars);
249240
if (!UseExpr.empty())
250241
return FileCheckErrorDiagnostic::get(
251242
SM, UseExpr,
252243
"unexpected string after variable definition: '" + UseExpr + "'");
244+
245+
DefExpr = DefExpr.ltrim(SpaceChars);
246+
StringRef Name;
247+
Error Err = parseNumericVariableDefinition(DefExpr, Name, Context, SM);
248+
if (Err)
249+
return std::move(Err);
250+
DefinedNumericVariable = Context->makeNumericVariable(LineNumber, Name);
251+
253252
return Context->makeExpression(add, nullptr, 0);
254253
}
255254

@@ -1735,32 +1734,12 @@ Error FileCheckPatternContext::defineCmdlineVariables(
17351734
if (CmdlineDef[0] == '#') {
17361735
StringRef CmdlineName = CmdlineDef.substr(1, EqIdx - 1);
17371736
StringRef VarName;
1738-
SMLoc CmdlineNameLoc = SMLoc::getFromPointer(CmdlineName.data());
17391737
Error ErrorDiagnostic = FileCheckPattern::parseNumericVariableDefinition(
17401738
CmdlineName, VarName, this, SM);
17411739
if (ErrorDiagnostic) {
17421740
Errs = joinErrors(std::move(Errs), std::move(ErrorDiagnostic));
17431741
continue;
17441742
}
1745-
// Check that CmdlineName is only composed of the parsed numeric
1746-
// variable. This catches cases like "FOO+2" in a "FOO+2=10" definition.
1747-
if (!CmdlineName.empty()) {
1748-
Errs = joinErrors(std::move(Errs),
1749-
FileCheckErrorDiagnostic::get(
1750-
SM, CmdlineNameLoc, "invalid variable name"));
1751-
continue;
1752-
}
1753-
1754-
// Detect collisions between string and numeric variables when the latter
1755-
// is created later than the former.
1756-
if (DefinedVariableTable.find(VarName) != DefinedVariableTable.end()) {
1757-
Errs = joinErrors(
1758-
std::move(Errs),
1759-
FileCheckErrorDiagnostic::get(SM, VarName,
1760-
"string variable with name '" +
1761-
VarName + "' already exists"));
1762-
continue;
1763-
}
17641743

17651744
StringRef CmdlineVal = CmdlineDef.substr(EqIdx + 1);
17661745
uint64_t Val;

‎llvm/test/FileCheck/numeric-defines-diagnostics.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ NUMERRCLIPSEUDO-NEXT: {{^ \^$}}
2020
RUN: not FileCheck -D#VALUE+2=10 --input-file %s %s 2>&1 \
2121
RUN: | FileCheck %s --strict-whitespace --check-prefix NUMERRCLITRAIL
2222

23-
NUMERRCLITRAIL: Global defines:1:20: error: invalid variable name
23+
NUMERRCLITRAIL: Global defines:1:25: error: unexpected characters after numeric variable name
2424
NUMERRCLITRAIL-NEXT: Global define #1: #VALUE+2=10
25-
NUMERRCLITRAIL-NEXT: {{^ \^$}}
25+
NUMERRCLITRAIL-NEXT: {{^ \^$}}
2626

2727
; Invalid value: numeric expression instead of literal.
2828
RUN: not FileCheck -D#VALUE1=3 -D#VALUE2='VALUE1 + 2' --input-file %s %s 2>&1 \

0 commit comments

Comments
 (0)
Please sign in to comment.