Skip to content

Commit e15e969

Browse files
committed
[FileCheck, 4/4] NFC: Stop using ExpressionValue
Use APInt directly instead. Depends On D150880 Reviewed By: arichardson Differential Revision: https://reviews.llvm.org/D154430
1 parent e894c3d commit e15e969

File tree

3 files changed

+98
-149
lines changed

3 files changed

+98
-149
lines changed

llvm/lib/FileCheck/FileCheck.cpp

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ Expected<std::string> ExpressionFormat::getWildcardRegex() const {
7878
}
7979

8080
Expected<std::string>
81-
ExpressionFormat::getMatchingString(ExpressionValue IntegerValue) const {
82-
APInt IntValue = IntegerValue.getAPIntValue();
81+
ExpressionFormat::getMatchingString(APInt IntValue) const {
8382
if (Value != Kind::Signed && IntValue.isNegative())
8483
return make_error<OverflowError>();
8584

@@ -135,7 +134,7 @@ static APInt toSigned(APInt AbsVal, bool Negative) {
135134
return Result;
136135
}
137136

138-
Expected<ExpressionValue>
137+
Expected<APInt>
139138
ExpressionFormat::valueFromStringRepr(StringRef StrVal,
140139
const SourceMgr &SM) const {
141140
bool ValueIsSigned = Value == Kind::Signed;
@@ -155,76 +154,59 @@ ExpressionFormat::valueFromStringRepr(StringRef StrVal,
155154
if (ParseFailure)
156155
return ErrorDiagnostic::get(SM, StrVal,
157156
"unable to represent numeric value");
158-
return ExpressionValue(toSigned(ResultValue, Negative));
157+
return toSigned(ResultValue, Negative);
159158
}
160159

161-
Expected<ExpressionValue> llvm::exprAdd(const ExpressionValue &LeftOperand,
162-
const ExpressionValue &RightOperand,
163-
bool &Overflow) {
164-
APInt Result = LeftOperand.getAPIntValue().sadd_ov(
165-
RightOperand.getAPIntValue(), Overflow);
166-
return ExpressionValue(Result);
160+
Expected<APInt> llvm::exprAdd(const APInt &LeftOperand,
161+
const APInt &RightOperand, bool &Overflow) {
162+
return LeftOperand.sadd_ov(RightOperand, Overflow);
167163
}
168164

169-
Expected<ExpressionValue> llvm::exprSub(const ExpressionValue &LeftOperand,
170-
const ExpressionValue &RightOperand,
171-
bool &Overflow) {
172-
APInt Result = LeftOperand.getAPIntValue().ssub_ov(
173-
RightOperand.getAPIntValue(), Overflow);
174-
return ExpressionValue(Result);
165+
Expected<APInt> llvm::exprSub(const APInt &LeftOperand,
166+
const APInt &RightOperand, bool &Overflow) {
167+
return LeftOperand.ssub_ov(RightOperand, Overflow);
175168
}
176169

177-
Expected<ExpressionValue> llvm::exprMul(const ExpressionValue &LeftOperand,
178-
const ExpressionValue &RightOperand,
179-
bool &Overflow) {
180-
APInt Result = LeftOperand.getAPIntValue().smul_ov(
181-
RightOperand.getAPIntValue(), Overflow);
182-
return ExpressionValue(Result);
170+
Expected<APInt> llvm::exprMul(const APInt &LeftOperand,
171+
const APInt &RightOperand, bool &Overflow) {
172+
return LeftOperand.smul_ov(RightOperand, Overflow);
183173
}
184174

185-
Expected<ExpressionValue> llvm::exprDiv(const ExpressionValue &LeftOperand,
186-
const ExpressionValue &RightOperand,
187-
bool &Overflow) {
175+
Expected<APInt> llvm::exprDiv(const APInt &LeftOperand,
176+
const APInt &RightOperand, bool &Overflow) {
188177
// Check for division by zero.
189-
if (RightOperand.getAPIntValue().isZero())
178+
if (RightOperand.isZero())
190179
return make_error<OverflowError>();
191180

192-
APInt Result = LeftOperand.getAPIntValue().sdiv_ov(
193-
RightOperand.getAPIntValue(), Overflow);
194-
return ExpressionValue(Result);
181+
return LeftOperand.sdiv_ov(RightOperand, Overflow);
195182
}
196183

197-
Expected<ExpressionValue> llvm::exprMax(const ExpressionValue &LeftOperand,
198-
const ExpressionValue &RightOperand,
199-
bool &Overflow) {
184+
Expected<APInt> llvm::exprMax(const APInt &LeftOperand,
185+
const APInt &RightOperand, bool &Overflow) {
200186
Overflow = false;
201-
return LeftOperand.getAPIntValue().slt(RightOperand.getAPIntValue())
202-
? RightOperand
203-
: LeftOperand;
187+
return LeftOperand.slt(RightOperand) ? RightOperand : LeftOperand;
204188
}
205189

206-
Expected<ExpressionValue> llvm::exprMin(const ExpressionValue &LeftOperand,
207-
const ExpressionValue &RightOperand,
208-
bool &Overflow) {
190+
Expected<APInt> llvm::exprMin(const APInt &LeftOperand,
191+
const APInt &RightOperand, bool &Overflow) {
209192
Overflow = false;
210-
if (cantFail(exprMax(LeftOperand, RightOperand, Overflow)).getAPIntValue() ==
211-
LeftOperand.getAPIntValue())
193+
if (cantFail(exprMax(LeftOperand, RightOperand, Overflow)) == LeftOperand)
212194
return RightOperand;
213195

214196
return LeftOperand;
215197
}
216198

217-
Expected<ExpressionValue> NumericVariableUse::eval() const {
218-
std::optional<ExpressionValue> Value = Variable->getValue();
199+
Expected<APInt> NumericVariableUse::eval() const {
200+
std::optional<APInt> Value = Variable->getValue();
219201
if (Value)
220202
return *Value;
221203

222204
return make_error<UndefVarError>(getExpressionStr());
223205
}
224206

225-
Expected<ExpressionValue> BinaryOperation::eval() const {
226-
Expected<ExpressionValue> MaybeLeftOp = LeftOperand->eval();
227-
Expected<ExpressionValue> MaybeRightOp = RightOperand->eval();
207+
Expected<APInt> BinaryOperation::eval() const {
208+
Expected<APInt> MaybeLeftOp = LeftOperand->eval();
209+
Expected<APInt> MaybeRightOp = RightOperand->eval();
228210

229211
// Bubble up any error (e.g. undefined variables) in the recursive
230212
// evaluation.
@@ -237,8 +219,8 @@ Expected<ExpressionValue> BinaryOperation::eval() const {
237219
return std::move(Err);
238220
}
239221

240-
APInt LeftOp = MaybeLeftOp->getAPIntValue();
241-
APInt RightOp = MaybeRightOp->getAPIntValue();
222+
APInt LeftOp = *MaybeLeftOp;
223+
APInt RightOp = *MaybeRightOp;
242224
bool Overflow;
243225
// Ensure both operands have the same bitwidth.
244226
unsigned LeftBitWidth = LeftOp.getBitWidth();
@@ -247,8 +229,7 @@ Expected<ExpressionValue> BinaryOperation::eval() const {
247229
LeftOp = LeftOp.sext(NewBitWidth);
248230
RightOp = RightOp.sext(NewBitWidth);
249231
do {
250-
Expected<ExpressionValue> MaybeResult =
251-
EvalBinop(ExpressionValue(LeftOp), ExpressionValue(RightOp), Overflow);
232+
Expected<APInt> MaybeResult = EvalBinop(LeftOp, RightOp, Overflow);
252233
if (!MaybeResult)
253234
return MaybeResult.takeError();
254235

@@ -291,8 +272,7 @@ BinaryOperation::getImplicitFormat(const SourceMgr &SM) const {
291272
Expected<std::string> NumericSubstitution::getResult() const {
292273
assert(ExpressionPointer->getAST() != nullptr &&
293274
"Substituting empty expression");
294-
Expected<ExpressionValue> EvaluatedValue =
295-
ExpressionPointer->getAST()->eval();
275+
Expected<APInt> EvaluatedValue = ExpressionPointer->getAST()->eval();
296276
if (!EvaluatedValue)
297277
return EvaluatedValue.takeError();
298278
ExpressionFormat Format = ExpressionPointer->getFormat();
@@ -1117,7 +1097,7 @@ Pattern::MatchResult Pattern::match(StringRef Buffer,
11171097
TmpStr = RegExStr;
11181098
if (LineNumber)
11191099
Context->LineVariable->setValue(
1120-
ExpressionValue(APInt(sizeof(*LineNumber) * 8, *LineNumber)));
1100+
APInt(sizeof(*LineNumber) * 8, *LineNumber));
11211101

11221102
size_t InsertOffset = 0;
11231103
// Substitute all string variables and expressions whose values are only
@@ -1196,8 +1176,7 @@ Pattern::MatchResult Pattern::match(StringRef Buffer,
11961176

11971177
StringRef MatchedValue = MatchInfo[CaptureParenGroup];
11981178
ExpressionFormat Format = DefinedNumericVariable->getImplicitFormat();
1199-
Expected<ExpressionValue> Value =
1200-
Format.valueFromStringRepr(MatchedValue, SM);
1179+
Expected<APInt> Value = Format.valueFromStringRepr(MatchedValue, SM);
12011180
if (!Value)
12021181
return MatchResult(TheMatch, Value.takeError());
12031182
DefinedNumericVariable->setValue(*Value, MatchedValue);
@@ -2583,7 +2562,7 @@ Error FileCheckPatternContext::defineCmdlineVariables(
25832562
// to, since the expression of a command-line variable definition should
25842563
// only use variables defined earlier on the command-line. If not, this
25852564
// is an error and we report it.
2586-
Expected<ExpressionValue> Value = Expression->getAST()->eval();
2565+
Expected<APInt> Value = Expression->getAST()->eval();
25872566
if (!Value) {
25882567
Errs = joinErrors(std::move(Errs), Value.takeError());
25892568
continue;

llvm/lib/FileCheck/FileCheckImpl.h

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ namespace llvm {
3232
// Numeric substitution handling code.
3333
//===----------------------------------------------------------------------===//
3434

35-
class ExpressionValue;
36-
3735
/// Type representing the format an expression value should be textualized into
3836
/// for matching. Used to represent both explicit format specifiers as well as
3937
/// implicit format from using numeric variables.
@@ -95,14 +93,14 @@ struct ExpressionFormat {
9593
/// \returns the string representation of \p Value in the format represented
9694
/// by this instance, or an error if conversion to this format failed or the
9795
/// format is NoFormat.
98-
Expected<std::string> getMatchingString(ExpressionValue Value) const;
96+
Expected<std::string> getMatchingString(APInt Value) const;
9997

10098
/// \returns the value corresponding to string representation \p StrVal
10199
/// according to the matching format represented by this instance or an error
102100
/// with diagnostic against \p SM if \p StrVal does not correspond to a valid
103101
/// and representable value.
104-
Expected<ExpressionValue> valueFromStringRepr(StringRef StrVal,
105-
const SourceMgr &SM) const;
102+
Expected<APInt> valueFromStringRepr(StringRef StrVal,
103+
const SourceMgr &SM) const;
106104
};
107105

108106
/// Class to represent an overflow error that might result when manipulating a
@@ -118,31 +116,14 @@ class OverflowError : public ErrorInfo<OverflowError> {
118116
void log(raw_ostream &OS) const override { OS << "overflow error"; }
119117
};
120118

121-
/// Class representing a numeric value.
122-
class ExpressionValue {
123-
private:
124-
APInt Value;
125-
126-
public:
127-
ExpressionValue(APInt Val) : Value(Val) {}
128-
129-
APInt getAPIntValue() const { return Value; }
130-
};
131-
132119
/// Performs operation and \returns its result or an error in case of failure,
133120
/// such as if an overflow occurs.
134-
Expected<ExpressionValue> exprAdd(const ExpressionValue &Lhs,
135-
const ExpressionValue &Rhs, bool &Overflow);
136-
Expected<ExpressionValue> exprSub(const ExpressionValue &Lhs,
137-
const ExpressionValue &Rhs, bool &Overflow);
138-
Expected<ExpressionValue> exprMul(const ExpressionValue &Lhs,
139-
const ExpressionValue &Rhs, bool &Overflow);
140-
Expected<ExpressionValue> exprDiv(const ExpressionValue &Lhs,
141-
const ExpressionValue &Rhs, bool &Overflow);
142-
Expected<ExpressionValue> exprMax(const ExpressionValue &Lhs,
143-
const ExpressionValue &Rhs, bool &Overflow);
144-
Expected<ExpressionValue> exprMin(const ExpressionValue &Lhs,
145-
const ExpressionValue &Rhs, bool &Overflow);
121+
Expected<APInt> exprAdd(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
122+
Expected<APInt> exprSub(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
123+
Expected<APInt> exprMul(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
124+
Expected<APInt> exprDiv(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
125+
Expected<APInt> exprMax(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
126+
Expected<APInt> exprMin(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
146127

147128
/// Base class representing the AST of a given expression.
148129
class ExpressionAST {
@@ -158,7 +139,7 @@ class ExpressionAST {
158139

159140
/// Evaluates and \returns the value of the expression represented by this
160141
/// AST or an error if evaluation fails.
161-
virtual Expected<ExpressionValue> eval() const = 0;
142+
virtual Expected<APInt> eval() const = 0;
162143

163144
/// \returns either the implicit format of this AST, a diagnostic against
164145
/// \p SM if implicit formats of the AST's components conflict, or NoFormat
@@ -174,14 +155,14 @@ class ExpressionAST {
174155
class ExpressionLiteral : public ExpressionAST {
175156
private:
176157
/// Actual value of the literal.
177-
ExpressionValue Value;
158+
APInt Value;
178159

179160
public:
180161
explicit ExpressionLiteral(StringRef ExpressionStr, APInt Val)
181162
: ExpressionAST(ExpressionStr), Value(Val) {}
182163

183164
/// \returns the literal's value.
184-
Expected<ExpressionValue> eval() const override { return Value; }
165+
Expected<APInt> eval() const override { return Value; }
185166
};
186167

187168
/// Class to represent an undefined variable error, which quotes that
@@ -240,7 +221,7 @@ class NumericVariable {
240221
ExpressionFormat ImplicitFormat;
241222

242223
/// Value of numeric variable, if defined, or std::nullopt otherwise.
243-
std::optional<ExpressionValue> Value;
224+
std::optional<APInt> Value;
244225

245226
/// The input buffer's string from which Value was parsed, or std::nullopt.
246227
/// See comments on getStringValue for a discussion of the std::nullopt case.
@@ -267,7 +248,7 @@ class NumericVariable {
267248
ExpressionFormat getImplicitFormat() const { return ImplicitFormat; }
268249

269250
/// \returns this variable's value.
270-
std::optional<ExpressionValue> getValue() const { return Value; }
251+
std::optional<APInt> getValue() const { return Value; }
271252

272253
/// \returns the input buffer's string from which this variable's value was
273254
/// parsed, or std::nullopt if the value is not yet defined or was not parsed
@@ -279,7 +260,7 @@ class NumericVariable {
279260
/// Sets value of this numeric variable to \p NewValue, and sets the input
280261
/// buffer string from which it was parsed to \p NewStrValue. See comments on
281262
/// getStringValue for a discussion of when the latter can be std::nullopt.
282-
void setValue(ExpressionValue NewValue,
263+
void setValue(APInt NewValue,
283264
std::optional<StringRef> NewStrValue = std::nullopt) {
284265
Value = NewValue;
285266
StrValue = NewStrValue;
@@ -308,7 +289,7 @@ class NumericVariableUse : public ExpressionAST {
308289
NumericVariableUse(StringRef Name, NumericVariable *Variable)
309290
: ExpressionAST(Name), Variable(Variable) {}
310291
/// \returns the value of the variable referenced by this instance.
311-
Expected<ExpressionValue> eval() const override;
292+
Expected<APInt> eval() const override;
312293

313294
/// \returns implicit format of this numeric variable.
314295
Expected<ExpressionFormat>
@@ -318,9 +299,7 @@ class NumericVariableUse : public ExpressionAST {
318299
};
319300

320301
/// Type of functions evaluating a given binary operation.
321-
using binop_eval_t = Expected<ExpressionValue> (*)(const ExpressionValue &,
322-
const ExpressionValue &,
323-
bool &);
302+
using binop_eval_t = Expected<APInt> (*)(const APInt &, const APInt &, bool &);
324303

325304
/// Class representing a single binary operation in the AST of an expression.
326305
class BinaryOperation : public ExpressionAST {
@@ -347,7 +326,7 @@ class BinaryOperation : public ExpressionAST {
347326
/// using EvalBinop on the result of recursively evaluating the operands.
348327
/// \returns the expression value or an error if an undefined numeric
349328
/// variable is used in one of the operands.
350-
Expected<ExpressionValue> eval() const override;
329+
Expected<APInt> eval() const override;
351330

352331
/// \returns the implicit format of this AST, if any, a diagnostic against
353332
/// \p SM if the implicit formats of the AST's components conflict, or no

0 commit comments

Comments
 (0)