Skip to content

Commit b743c19

Browse files
committed
[FileCheck] Turn errors into assert in valueFromStringRepr()
getWildcardRegex() guarantees that only valid hex numbers are matched by FileCheck numeric expressions. This commit therefore only asserts the lack of parsing failure in valueFromStringRepr(). Depends On D154430 Reviewed By: arichardson Differential Revision: https://reviews.llvm.org/D154431
1 parent 4dce6d3 commit b743c19

File tree

3 files changed

+22
-41
lines changed

3 files changed

+22
-41
lines changed

llvm/lib/FileCheck/FileCheck.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,26 +134,22 @@ static APInt toSigned(APInt AbsVal, bool Negative) {
134134
return Result;
135135
}
136136

137-
Expected<APInt>
138-
ExpressionFormat::valueFromStringRepr(StringRef StrVal,
139-
const SourceMgr &SM) const {
137+
APInt ExpressionFormat::valueFromStringRepr(StringRef StrVal,
138+
const SourceMgr &SM) const {
140139
bool ValueIsSigned = Value == Kind::Signed;
141-
// Both the FileCheck utility and library only call this method with a valid
142-
// value in StrVal. This is guaranteed by the regex returned by
143-
// getWildcardRegex() above. Only underflow and overflow errors can thus
144-
// occur. However new uses of this method could be added in the future so
145-
// the error message does not make assumptions about StrVal.
146140
bool Negative = StrVal.consume_front("-");
147141
bool Hex = Value == Kind::HexUpper || Value == Kind::HexLower;
148142
bool MissingFormPrefix =
149143
!ValueIsSigned && AlternateForm && !StrVal.consume_front("0x");
150144
(void)MissingFormPrefix;
151145
assert(!MissingFormPrefix && "missing alternate form prefix");
152146
APInt ResultValue;
153-
bool ParseFailure = StrVal.getAsInteger(Hex ? 16 : 10, ResultValue);
154-
if (ParseFailure)
155-
return ErrorDiagnostic::get(SM, StrVal,
156-
"unable to represent numeric value");
147+
[[maybe_unused]] bool ParseFailure =
148+
StrVal.getAsInteger(Hex ? 16 : 10, ResultValue);
149+
// Both the FileCheck utility and library only call this method with a valid
150+
// value in StrVal. This is guaranteed by the regex returned by
151+
// getWildcardRegex() above.
152+
assert(!ParseFailure && "unable to represent numeric value");
157153
return toSigned(ResultValue, Negative);
158154
}
159155

@@ -1176,10 +1172,8 @@ Pattern::MatchResult Pattern::match(StringRef Buffer,
11761172

11771173
StringRef MatchedValue = MatchInfo[CaptureParenGroup];
11781174
ExpressionFormat Format = DefinedNumericVariable->getImplicitFormat();
1179-
Expected<APInt> Value = Format.valueFromStringRepr(MatchedValue, SM);
1180-
if (!Value)
1181-
return MatchResult(TheMatch, Value.takeError());
1182-
DefinedNumericVariable->setValue(*Value, MatchedValue);
1175+
APInt Value = Format.valueFromStringRepr(MatchedValue, SM);
1176+
DefinedNumericVariable->setValue(Value, MatchedValue);
11831177
}
11841178

11851179
return MatchResult(TheMatch, Error::success());

llvm/lib/FileCheck/FileCheckImpl.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,8 @@ struct ExpressionFormat {
9696
Expected<std::string> getMatchingString(APInt Value) const;
9797

9898
/// \returns the value corresponding to string representation \p StrVal
99-
/// according to the matching format represented by this instance or an error
100-
/// with diagnostic against \p SM if \p StrVal does not correspond to a valid
101-
/// and representable value.
102-
Expected<APInt> valueFromStringRepr(StringRef StrVal,
103-
const SourceMgr &SM) const;
99+
/// according to the matching format represented by this instance.
100+
APInt valueFromStringRepr(StringRef StrVal, const SourceMgr &SM) const;
104101
};
105102

106103
/// Class to represent an overflow error that might result when manipulating a

llvm/unittests/FileCheck/FileCheckTest.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -201,29 +201,16 @@ struct ExpressionFormatParameterisedFixture
201201
EXPECT_THAT_EXPECTED(MatchingString, Failed());
202202
}
203203

204-
Expected<APInt> getValueFromStringReprFailure(StringRef Str) {
205-
StringRef BufferizedStr = bufferize(SM, Str);
206-
return Format.valueFromStringRepr(BufferizedStr, SM);
207-
}
208-
209204
template <class T>
210205
void checkValueFromStringRepr(StringRef Str, T ExpectedVal) {
211-
Expected<APInt> ResultValue = getValueFromStringReprFailure(Str);
212-
ASSERT_THAT_EXPECTED(ResultValue, Succeeded())
213-
<< "Failed to get value from " << Str;
214-
ASSERT_EQ(ResultValue->isNegative(), ExpectedVal < 0)
206+
StringRef BufferizedStr = bufferize(SM, Str);
207+
APInt ResultValue = Format.valueFromStringRepr(BufferizedStr, SM);
208+
ASSERT_EQ(ResultValue.isNegative(), ExpectedVal < 0)
215209
<< "Value for " << Str << " is not " << ExpectedVal;
216-
if (ResultValue->isNegative())
217-
EXPECT_EQ(ResultValue->getSExtValue(), static_cast<int64_t>(ExpectedVal));
210+
if (ResultValue.isNegative())
211+
EXPECT_EQ(ResultValue.getSExtValue(), static_cast<int64_t>(ExpectedVal));
218212
else
219-
EXPECT_EQ(ResultValue->getZExtValue(),
220-
static_cast<uint64_t>(ExpectedVal));
221-
}
222-
223-
void checkValueFromStringReprFailure(
224-
StringRef Str, StringRef ErrorStr = "unable to represent numeric value") {
225-
Expected<APInt> ResultValue = getValueFromStringReprFailure(Str);
226-
expectDiagnosticError(ErrorStr, ResultValue.takeError());
213+
EXPECT_EQ(ResultValue.getZExtValue(), static_cast<uint64_t>(ExpectedVal));
227214
}
228215
};
229216

@@ -321,7 +308,10 @@ TEST_P(ExpressionFormatParameterisedFixture, FormatValueFromStringRepr) {
321308

322309
// Wrong casing is not tested because valueFromStringRepr() relies on
323310
// StringRef's getAsInteger() which does not allow to restrict casing.
324-
checkValueFromStringReprFailure(addBasePrefix("G"));
311+
312+
// Likewise, wrong letter digit for hex value is not tested because it is
313+
// only caught by an assert in FileCheck due to getWildcardRegex()
314+
// guaranteeing only valid letter digits are used.
325315
}
326316

327317
TEST_P(ExpressionFormatParameterisedFixture, FormatBoolOperator) {

0 commit comments

Comments
 (0)