xref: /freebsd-src/contrib/llvm-project/llvm/lib/FileCheck/FileCheckImpl.h (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
1e8d8bef9SDimitry Andric //===-- FileCheckImpl.h - Private FileCheck Interface ------------*- C++ -*-==//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
9e8d8bef9SDimitry Andric // This file defines the private interfaces of FileCheck. Its purpose is to
10e8d8bef9SDimitry Andric // allow unit testing of FileCheck and to separate the interface from the
11e8d8bef9SDimitry Andric // implementation. It is only meant to be used by FileCheck.
12e8d8bef9SDimitry Andric //
13e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
14e8d8bef9SDimitry Andric 
15e8d8bef9SDimitry Andric #ifndef LLVM_LIB_FILECHECK_FILECHECKIMPL_H
16e8d8bef9SDimitry Andric #define LLVM_LIB_FILECHECK_FILECHECKIMPL_H
17e8d8bef9SDimitry Andric 
1806c3fb27SDimitry Andric #include "llvm/ADT/APInt.h"
19e8d8bef9SDimitry Andric #include "llvm/ADT/StringMap.h"
20e8d8bef9SDimitry Andric #include "llvm/ADT/StringRef.h"
21e8d8bef9SDimitry Andric #include "llvm/FileCheck/FileCheck.h"
22e8d8bef9SDimitry Andric #include "llvm/Support/Error.h"
23e8d8bef9SDimitry Andric #include "llvm/Support/SourceMgr.h"
24e8d8bef9SDimitry Andric #include <map>
25bdd1243dSDimitry Andric #include <optional>
26e8d8bef9SDimitry Andric #include <string>
27e8d8bef9SDimitry Andric #include <vector>
28e8d8bef9SDimitry Andric 
29e8d8bef9SDimitry Andric namespace llvm {
30e8d8bef9SDimitry Andric 
31e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
32e8d8bef9SDimitry Andric // Numeric substitution handling code.
33e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
34e8d8bef9SDimitry Andric 
35e8d8bef9SDimitry Andric /// Type representing the format an expression value should be textualized into
36e8d8bef9SDimitry Andric /// for matching. Used to represent both explicit format specifiers as well as
37e8d8bef9SDimitry Andric /// implicit format from using numeric variables.
38e8d8bef9SDimitry Andric struct ExpressionFormat {
39e8d8bef9SDimitry Andric   enum class Kind {
40e8d8bef9SDimitry Andric     /// Denote absence of format. Used for implicit format of literals and
41e8d8bef9SDimitry Andric     /// empty expressions.
42e8d8bef9SDimitry Andric     NoFormat,
43e8d8bef9SDimitry Andric     /// Value is an unsigned integer and should be printed as a decimal number.
44e8d8bef9SDimitry Andric     Unsigned,
45e8d8bef9SDimitry Andric     /// Value is a signed integer and should be printed as a decimal number.
46e8d8bef9SDimitry Andric     Signed,
47e8d8bef9SDimitry Andric     /// Value should be printed as an uppercase hex number.
48e8d8bef9SDimitry Andric     HexUpper,
49e8d8bef9SDimitry Andric     /// Value should be printed as a lowercase hex number.
50e8d8bef9SDimitry Andric     HexLower
51e8d8bef9SDimitry Andric   };
52e8d8bef9SDimitry Andric 
53e8d8bef9SDimitry Andric private:
54e8d8bef9SDimitry Andric   Kind Value;
55e8d8bef9SDimitry Andric   unsigned Precision = 0;
56fe6060f1SDimitry Andric   /// printf-like "alternate form" selected.
57fe6060f1SDimitry Andric   bool AlternateForm = false;
58e8d8bef9SDimitry Andric 
59e8d8bef9SDimitry Andric public:
60e8d8bef9SDimitry Andric   /// Evaluates a format to true if it can be used in a match.
61e8d8bef9SDimitry Andric   explicit operator bool() const { return Value != Kind::NoFormat; }
62e8d8bef9SDimitry Andric 
63e8d8bef9SDimitry Andric   /// Define format equality: formats are equal if neither is NoFormat and
64e8d8bef9SDimitry Andric   /// their kinds and precision are the same.
65e8d8bef9SDimitry Andric   bool operator==(const ExpressionFormat &Other) const {
66e8d8bef9SDimitry Andric     return Value != Kind::NoFormat && Value == Other.Value &&
67fe6060f1SDimitry Andric            Precision == Other.Precision && AlternateForm == Other.AlternateForm;
68e8d8bef9SDimitry Andric   }
69e8d8bef9SDimitry Andric 
70e8d8bef9SDimitry Andric   bool operator!=(const ExpressionFormat &Other) const {
71e8d8bef9SDimitry Andric     return !(*this == Other);
72e8d8bef9SDimitry Andric   }
73e8d8bef9SDimitry Andric 
74e8d8bef9SDimitry Andric   bool operator==(Kind OtherValue) const { return Value == OtherValue; }
75e8d8bef9SDimitry Andric 
76e8d8bef9SDimitry Andric   bool operator!=(Kind OtherValue) const { return !(*this == OtherValue); }
77e8d8bef9SDimitry Andric 
78e8d8bef9SDimitry Andric   /// \returns the format specifier corresponding to this format as a string.
79e8d8bef9SDimitry Andric   StringRef toString() const;
80e8d8bef9SDimitry Andric 
ExpressionFormatExpressionFormat81e8d8bef9SDimitry Andric   ExpressionFormat() : Value(Kind::NoFormat){};
ExpressionFormatExpressionFormat82e8d8bef9SDimitry Andric   explicit ExpressionFormat(Kind Value) : Value(Value), Precision(0){};
ExpressionFormatExpressionFormat83e8d8bef9SDimitry Andric   explicit ExpressionFormat(Kind Value, unsigned Precision)
84e8d8bef9SDimitry Andric       : Value(Value), Precision(Precision){};
ExpressionFormatExpressionFormat85fe6060f1SDimitry Andric   explicit ExpressionFormat(Kind Value, unsigned Precision, bool AlternateForm)
86fe6060f1SDimitry Andric       : Value(Value), Precision(Precision), AlternateForm(AlternateForm){};
87e8d8bef9SDimitry Andric 
88e8d8bef9SDimitry Andric   /// \returns a wildcard regular expression string that matches any value in
89e8d8bef9SDimitry Andric   /// the format represented by this instance and no other value, or an error
90e8d8bef9SDimitry Andric   /// if the format is NoFormat.
91e8d8bef9SDimitry Andric   Expected<std::string> getWildcardRegex() const;
92e8d8bef9SDimitry Andric 
93e8d8bef9SDimitry Andric   /// \returns the string representation of \p Value in the format represented
94e8d8bef9SDimitry Andric   /// by this instance, or an error if conversion to this format failed or the
95e8d8bef9SDimitry Andric   /// format is NoFormat.
965f757f3fSDimitry Andric   Expected<std::string> getMatchingString(APInt Value) const;
97e8d8bef9SDimitry Andric 
98e8d8bef9SDimitry Andric   /// \returns the value corresponding to string representation \p StrVal
995f757f3fSDimitry Andric   /// according to the matching format represented by this instance.
1005f757f3fSDimitry Andric   APInt valueFromStringRepr(StringRef StrVal, const SourceMgr &SM) const;
101e8d8bef9SDimitry Andric };
102e8d8bef9SDimitry Andric 
103e8d8bef9SDimitry Andric /// Class to represent an overflow error that might result when manipulating a
104e8d8bef9SDimitry Andric /// value.
105e8d8bef9SDimitry Andric class OverflowError : public ErrorInfo<OverflowError> {
106e8d8bef9SDimitry Andric public:
107e8d8bef9SDimitry Andric   static char ID;
108e8d8bef9SDimitry Andric 
convertToErrorCode()109e8d8bef9SDimitry Andric   std::error_code convertToErrorCode() const override {
110e8d8bef9SDimitry Andric     return std::make_error_code(std::errc::value_too_large);
111e8d8bef9SDimitry Andric   }
112e8d8bef9SDimitry Andric 
log(raw_ostream & OS)113e8d8bef9SDimitry Andric   void log(raw_ostream &OS) const override { OS << "overflow error"; }
114e8d8bef9SDimitry Andric };
115e8d8bef9SDimitry Andric 
116e8d8bef9SDimitry Andric /// Performs operation and \returns its result or an error in case of failure,
117e8d8bef9SDimitry Andric /// such as if an overflow occurs.
1185f757f3fSDimitry Andric Expected<APInt> exprAdd(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
1195f757f3fSDimitry Andric Expected<APInt> exprSub(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
1205f757f3fSDimitry Andric Expected<APInt> exprMul(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
1215f757f3fSDimitry Andric Expected<APInt> exprDiv(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
1225f757f3fSDimitry Andric Expected<APInt> exprMax(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
1235f757f3fSDimitry Andric Expected<APInt> exprMin(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
124e8d8bef9SDimitry Andric 
125e8d8bef9SDimitry Andric /// Base class representing the AST of a given expression.
126e8d8bef9SDimitry Andric class ExpressionAST {
127e8d8bef9SDimitry Andric private:
128e8d8bef9SDimitry Andric   StringRef ExpressionStr;
129e8d8bef9SDimitry Andric 
130e8d8bef9SDimitry Andric public:
ExpressionAST(StringRef ExpressionStr)131e8d8bef9SDimitry Andric   ExpressionAST(StringRef ExpressionStr) : ExpressionStr(ExpressionStr) {}
132e8d8bef9SDimitry Andric 
133e8d8bef9SDimitry Andric   virtual ~ExpressionAST() = default;
134e8d8bef9SDimitry Andric 
getExpressionStr()135e8d8bef9SDimitry Andric   StringRef getExpressionStr() const { return ExpressionStr; }
136e8d8bef9SDimitry Andric 
137e8d8bef9SDimitry Andric   /// Evaluates and \returns the value of the expression represented by this
138e8d8bef9SDimitry Andric   /// AST or an error if evaluation fails.
1395f757f3fSDimitry Andric   virtual Expected<APInt> eval() const = 0;
140e8d8bef9SDimitry Andric 
141e8d8bef9SDimitry Andric   /// \returns either the implicit format of this AST, a diagnostic against
142e8d8bef9SDimitry Andric   /// \p SM if implicit formats of the AST's components conflict, or NoFormat
143e8d8bef9SDimitry Andric   /// if the AST has no implicit format (e.g. AST is made up of a single
144e8d8bef9SDimitry Andric   /// literal).
145e8d8bef9SDimitry Andric   virtual Expected<ExpressionFormat>
getImplicitFormat(const SourceMgr & SM)146e8d8bef9SDimitry Andric   getImplicitFormat(const SourceMgr &SM) const {
147e8d8bef9SDimitry Andric     return ExpressionFormat();
148e8d8bef9SDimitry Andric   }
149e8d8bef9SDimitry Andric };
150e8d8bef9SDimitry Andric 
151e8d8bef9SDimitry Andric /// Class representing an unsigned literal in the AST of an expression.
152e8d8bef9SDimitry Andric class ExpressionLiteral : public ExpressionAST {
153e8d8bef9SDimitry Andric private:
154e8d8bef9SDimitry Andric   /// Actual value of the literal.
1555f757f3fSDimitry Andric   APInt Value;
156e8d8bef9SDimitry Andric 
157e8d8bef9SDimitry Andric public:
ExpressionLiteral(StringRef ExpressionStr,APInt Val)1585f757f3fSDimitry Andric   explicit ExpressionLiteral(StringRef ExpressionStr, APInt Val)
159e8d8bef9SDimitry Andric       : ExpressionAST(ExpressionStr), Value(Val) {}
160e8d8bef9SDimitry Andric 
161e8d8bef9SDimitry Andric   /// \returns the literal's value.
eval()1625f757f3fSDimitry Andric   Expected<APInt> eval() const override { return Value; }
163e8d8bef9SDimitry Andric };
164e8d8bef9SDimitry Andric 
165e8d8bef9SDimitry Andric /// Class to represent an undefined variable error, which quotes that
166e8d8bef9SDimitry Andric /// variable's name when printed.
167e8d8bef9SDimitry Andric class UndefVarError : public ErrorInfo<UndefVarError> {
168e8d8bef9SDimitry Andric private:
169e8d8bef9SDimitry Andric   StringRef VarName;
170e8d8bef9SDimitry Andric 
171e8d8bef9SDimitry Andric public:
172e8d8bef9SDimitry Andric   static char ID;
173e8d8bef9SDimitry Andric 
UndefVarError(StringRef VarName)174e8d8bef9SDimitry Andric   UndefVarError(StringRef VarName) : VarName(VarName) {}
175e8d8bef9SDimitry Andric 
getVarName()176e8d8bef9SDimitry Andric   StringRef getVarName() const { return VarName; }
177e8d8bef9SDimitry Andric 
convertToErrorCode()178e8d8bef9SDimitry Andric   std::error_code convertToErrorCode() const override {
179e8d8bef9SDimitry Andric     return inconvertibleErrorCode();
180e8d8bef9SDimitry Andric   }
181e8d8bef9SDimitry Andric 
182e8d8bef9SDimitry Andric   /// Print name of variable associated with this error.
log(raw_ostream & OS)183e8d8bef9SDimitry Andric   void log(raw_ostream &OS) const override {
184fe6060f1SDimitry Andric     OS << "undefined variable: " << VarName;
185e8d8bef9SDimitry Andric   }
186e8d8bef9SDimitry Andric };
187e8d8bef9SDimitry Andric 
188e8d8bef9SDimitry Andric /// Class representing an expression and its matching format.
189e8d8bef9SDimitry Andric class Expression {
190e8d8bef9SDimitry Andric private:
191e8d8bef9SDimitry Andric   /// Pointer to AST of the expression.
192e8d8bef9SDimitry Andric   std::unique_ptr<ExpressionAST> AST;
193e8d8bef9SDimitry Andric 
194e8d8bef9SDimitry Andric   /// Format to use (e.g. hex upper case letters) when matching the value.
195e8d8bef9SDimitry Andric   ExpressionFormat Format;
196e8d8bef9SDimitry Andric 
197e8d8bef9SDimitry Andric public:
198e8d8bef9SDimitry Andric   /// Generic constructor for an expression represented by the given \p AST and
199e8d8bef9SDimitry Andric   /// whose matching format is \p Format.
Expression(std::unique_ptr<ExpressionAST> AST,ExpressionFormat Format)200e8d8bef9SDimitry Andric   Expression(std::unique_ptr<ExpressionAST> AST, ExpressionFormat Format)
201e8d8bef9SDimitry Andric       : AST(std::move(AST)), Format(Format) {}
202e8d8bef9SDimitry Andric 
203e8d8bef9SDimitry Andric   /// \returns pointer to AST of the expression. Pointer is guaranteed to be
204e8d8bef9SDimitry Andric   /// valid as long as this object is.
getAST()205e8d8bef9SDimitry Andric   ExpressionAST *getAST() const { return AST.get(); }
206e8d8bef9SDimitry Andric 
getFormat()207e8d8bef9SDimitry Andric   ExpressionFormat getFormat() const { return Format; }
208e8d8bef9SDimitry Andric };
209e8d8bef9SDimitry Andric 
210e8d8bef9SDimitry Andric /// Class representing a numeric variable and its associated current value.
211e8d8bef9SDimitry Andric class NumericVariable {
212e8d8bef9SDimitry Andric private:
213e8d8bef9SDimitry Andric   /// Name of the numeric variable.
214e8d8bef9SDimitry Andric   StringRef Name;
215e8d8bef9SDimitry Andric 
216e8d8bef9SDimitry Andric   /// Format to use for expressions using this variable without an explicit
217e8d8bef9SDimitry Andric   /// format.
218e8d8bef9SDimitry Andric   ExpressionFormat ImplicitFormat;
219e8d8bef9SDimitry Andric 
220bdd1243dSDimitry Andric   /// Value of numeric variable, if defined, or std::nullopt otherwise.
2215f757f3fSDimitry Andric   std::optional<APInt> Value;
222e8d8bef9SDimitry Andric 
223bdd1243dSDimitry Andric   /// The input buffer's string from which Value was parsed, or std::nullopt.
22406c3fb27SDimitry Andric   /// See comments on getStringValue for a discussion of the std::nullopt case.
225bdd1243dSDimitry Andric   std::optional<StringRef> StrValue;
226e8d8bef9SDimitry Andric 
227bdd1243dSDimitry Andric   /// Line number where this variable is defined, or std::nullopt if defined
228bdd1243dSDimitry Andric   /// before input is parsed. Used to determine whether a variable is defined on
229bdd1243dSDimitry Andric   /// the same line as a given use.
230bdd1243dSDimitry Andric   std::optional<size_t> DefLineNumber;
231e8d8bef9SDimitry Andric 
232e8d8bef9SDimitry Andric public:
233e8d8bef9SDimitry Andric   /// Constructor for a variable \p Name with implicit format \p ImplicitFormat
234e8d8bef9SDimitry Andric   /// defined at line \p DefLineNumber or defined before input is parsed if
23506c3fb27SDimitry Andric   /// \p DefLineNumber is std::nullopt.
236e8d8bef9SDimitry Andric   explicit NumericVariable(StringRef Name, ExpressionFormat ImplicitFormat,
237bdd1243dSDimitry Andric                            std::optional<size_t> DefLineNumber = std::nullopt)
Name(Name)238e8d8bef9SDimitry Andric       : Name(Name), ImplicitFormat(ImplicitFormat),
239e8d8bef9SDimitry Andric         DefLineNumber(DefLineNumber) {}
240e8d8bef9SDimitry Andric 
241e8d8bef9SDimitry Andric   /// \returns name of this numeric variable.
getName()242e8d8bef9SDimitry Andric   StringRef getName() const { return Name; }
243e8d8bef9SDimitry Andric 
244e8d8bef9SDimitry Andric   /// \returns implicit format of this numeric variable.
getImplicitFormat()245e8d8bef9SDimitry Andric   ExpressionFormat getImplicitFormat() const { return ImplicitFormat; }
246e8d8bef9SDimitry Andric 
247e8d8bef9SDimitry Andric   /// \returns this variable's value.
getValue()2485f757f3fSDimitry Andric   std::optional<APInt> getValue() const { return Value; }
249e8d8bef9SDimitry Andric 
250e8d8bef9SDimitry Andric   /// \returns the input buffer's string from which this variable's value was
251bdd1243dSDimitry Andric   /// parsed, or std::nullopt if the value is not yet defined or was not parsed
252bdd1243dSDimitry Andric   /// from the input buffer.  For example, the value of @LINE is not parsed from
253bdd1243dSDimitry Andric   /// the input buffer, and some numeric variables are parsed from the command
254e8d8bef9SDimitry Andric   /// line instead.
getStringValue()255bdd1243dSDimitry Andric   std::optional<StringRef> getStringValue() const { return StrValue; }
256e8d8bef9SDimitry Andric 
257e8d8bef9SDimitry Andric   /// Sets value of this numeric variable to \p NewValue, and sets the input
258e8d8bef9SDimitry Andric   /// buffer string from which it was parsed to \p NewStrValue.  See comments on
25906c3fb27SDimitry Andric   /// getStringValue for a discussion of when the latter can be std::nullopt.
2605f757f3fSDimitry Andric   void setValue(APInt NewValue,
261bdd1243dSDimitry Andric                 std::optional<StringRef> NewStrValue = std::nullopt) {
262e8d8bef9SDimitry Andric     Value = NewValue;
263e8d8bef9SDimitry Andric     StrValue = NewStrValue;
264e8d8bef9SDimitry Andric   }
265e8d8bef9SDimitry Andric 
266e8d8bef9SDimitry Andric   /// Clears value of this numeric variable, regardless of whether it is
267e8d8bef9SDimitry Andric   /// currently defined or not.
clearValue()268e8d8bef9SDimitry Andric   void clearValue() {
269bdd1243dSDimitry Andric     Value = std::nullopt;
270bdd1243dSDimitry Andric     StrValue = std::nullopt;
271e8d8bef9SDimitry Andric   }
272e8d8bef9SDimitry Andric 
273bdd1243dSDimitry Andric   /// \returns the line number where this variable is defined, if any, or
274bdd1243dSDimitry Andric   /// std::nullopt if defined before input is parsed.
getDefLineNumber()275bdd1243dSDimitry Andric   std::optional<size_t> getDefLineNumber() const { return DefLineNumber; }
276e8d8bef9SDimitry Andric };
277e8d8bef9SDimitry Andric 
278e8d8bef9SDimitry Andric /// Class representing the use of a numeric variable in the AST of an
279e8d8bef9SDimitry Andric /// expression.
280e8d8bef9SDimitry Andric class NumericVariableUse : public ExpressionAST {
281e8d8bef9SDimitry Andric private:
282e8d8bef9SDimitry Andric   /// Pointer to the class instance for the variable this use is about.
283e8d8bef9SDimitry Andric   NumericVariable *Variable;
284e8d8bef9SDimitry Andric 
285e8d8bef9SDimitry Andric public:
NumericVariableUse(StringRef Name,NumericVariable * Variable)286e8d8bef9SDimitry Andric   NumericVariableUse(StringRef Name, NumericVariable *Variable)
287e8d8bef9SDimitry Andric       : ExpressionAST(Name), Variable(Variable) {}
288e8d8bef9SDimitry Andric   /// \returns the value of the variable referenced by this instance.
2895f757f3fSDimitry Andric   Expected<APInt> eval() const override;
290e8d8bef9SDimitry Andric 
291e8d8bef9SDimitry Andric   /// \returns implicit format of this numeric variable.
292e8d8bef9SDimitry Andric   Expected<ExpressionFormat>
getImplicitFormat(const SourceMgr & SM)293e8d8bef9SDimitry Andric   getImplicitFormat(const SourceMgr &SM) const override {
294e8d8bef9SDimitry Andric     return Variable->getImplicitFormat();
295e8d8bef9SDimitry Andric   }
296e8d8bef9SDimitry Andric };
297e8d8bef9SDimitry Andric 
298e8d8bef9SDimitry Andric /// Type of functions evaluating a given binary operation.
2995f757f3fSDimitry Andric using binop_eval_t = Expected<APInt> (*)(const APInt &, const APInt &, bool &);
300e8d8bef9SDimitry Andric 
301e8d8bef9SDimitry Andric /// Class representing a single binary operation in the AST of an expression.
302e8d8bef9SDimitry Andric class BinaryOperation : public ExpressionAST {
303e8d8bef9SDimitry Andric private:
304e8d8bef9SDimitry Andric   /// Left operand.
305e8d8bef9SDimitry Andric   std::unique_ptr<ExpressionAST> LeftOperand;
306e8d8bef9SDimitry Andric 
307e8d8bef9SDimitry Andric   /// Right operand.
308e8d8bef9SDimitry Andric   std::unique_ptr<ExpressionAST> RightOperand;
309e8d8bef9SDimitry Andric 
310e8d8bef9SDimitry Andric   /// Pointer to function that can evaluate this binary operation.
311e8d8bef9SDimitry Andric   binop_eval_t EvalBinop;
312e8d8bef9SDimitry Andric 
313e8d8bef9SDimitry Andric public:
BinaryOperation(StringRef ExpressionStr,binop_eval_t EvalBinop,std::unique_ptr<ExpressionAST> LeftOp,std::unique_ptr<ExpressionAST> RightOp)314e8d8bef9SDimitry Andric   BinaryOperation(StringRef ExpressionStr, binop_eval_t EvalBinop,
315e8d8bef9SDimitry Andric                   std::unique_ptr<ExpressionAST> LeftOp,
316e8d8bef9SDimitry Andric                   std::unique_ptr<ExpressionAST> RightOp)
317e8d8bef9SDimitry Andric       : ExpressionAST(ExpressionStr), EvalBinop(EvalBinop) {
318e8d8bef9SDimitry Andric     LeftOperand = std::move(LeftOp);
319e8d8bef9SDimitry Andric     RightOperand = std::move(RightOp);
320e8d8bef9SDimitry Andric   }
321e8d8bef9SDimitry Andric 
322e8d8bef9SDimitry Andric   /// Evaluates the value of the binary operation represented by this AST,
323e8d8bef9SDimitry Andric   /// using EvalBinop on the result of recursively evaluating the operands.
324e8d8bef9SDimitry Andric   /// \returns the expression value or an error if an undefined numeric
325e8d8bef9SDimitry Andric   /// variable is used in one of the operands.
3265f757f3fSDimitry Andric   Expected<APInt> eval() const override;
327e8d8bef9SDimitry Andric 
328e8d8bef9SDimitry Andric   /// \returns the implicit format of this AST, if any, a diagnostic against
329e8d8bef9SDimitry Andric   /// \p SM if the implicit formats of the AST's components conflict, or no
330e8d8bef9SDimitry Andric   /// format if the AST has no implicit format (e.g. AST is made of a single
331e8d8bef9SDimitry Andric   /// literal).
332e8d8bef9SDimitry Andric   Expected<ExpressionFormat>
333e8d8bef9SDimitry Andric   getImplicitFormat(const SourceMgr &SM) const override;
334e8d8bef9SDimitry Andric };
335e8d8bef9SDimitry Andric 
336e8d8bef9SDimitry Andric class FileCheckPatternContext;
337e8d8bef9SDimitry Andric 
338e8d8bef9SDimitry Andric /// Class representing a substitution to perform in the RegExStr string.
339e8d8bef9SDimitry Andric class Substitution {
340e8d8bef9SDimitry Andric protected:
341e8d8bef9SDimitry Andric   /// Pointer to a class instance holding, among other things, the table with
342e8d8bef9SDimitry Andric   /// the values of live string variables at the start of any given CHECK line.
343e8d8bef9SDimitry Andric   /// Used for substituting string variables with the text they were defined
344e8d8bef9SDimitry Andric   /// as. Expressions are linked to the numeric variables they use at
345e8d8bef9SDimitry Andric   /// parse time and directly access the value of the numeric variable to
346e8d8bef9SDimitry Andric   /// evaluate their value.
347e8d8bef9SDimitry Andric   FileCheckPatternContext *Context;
348e8d8bef9SDimitry Andric 
349e8d8bef9SDimitry Andric   /// The string that needs to be substituted for something else. For a
350e8d8bef9SDimitry Andric   /// string variable this is its name, otherwise this is the whole expression.
351e8d8bef9SDimitry Andric   StringRef FromStr;
352e8d8bef9SDimitry Andric 
353e8d8bef9SDimitry Andric   // Index in RegExStr of where to do the substitution.
354e8d8bef9SDimitry Andric   size_t InsertIdx;
355e8d8bef9SDimitry Andric 
356e8d8bef9SDimitry Andric public:
Substitution(FileCheckPatternContext * Context,StringRef VarName,size_t InsertIdx)357e8d8bef9SDimitry Andric   Substitution(FileCheckPatternContext *Context, StringRef VarName,
358e8d8bef9SDimitry Andric                size_t InsertIdx)
359e8d8bef9SDimitry Andric       : Context(Context), FromStr(VarName), InsertIdx(InsertIdx) {}
360e8d8bef9SDimitry Andric 
361e8d8bef9SDimitry Andric   virtual ~Substitution() = default;
362e8d8bef9SDimitry Andric 
363e8d8bef9SDimitry Andric   /// \returns the string to be substituted for something else.
getFromString()364e8d8bef9SDimitry Andric   StringRef getFromString() const { return FromStr; }
365e8d8bef9SDimitry Andric 
366e8d8bef9SDimitry Andric   /// \returns the index where the substitution is to be performed in RegExStr.
getIndex()367e8d8bef9SDimitry Andric   size_t getIndex() const { return InsertIdx; }
368e8d8bef9SDimitry Andric 
369e8d8bef9SDimitry Andric   /// \returns a string containing the result of the substitution represented
370e8d8bef9SDimitry Andric   /// by this class instance or an error if substitution failed.
371e8d8bef9SDimitry Andric   virtual Expected<std::string> getResult() const = 0;
372e8d8bef9SDimitry Andric };
373e8d8bef9SDimitry Andric 
374e8d8bef9SDimitry Andric class StringSubstitution : public Substitution {
375e8d8bef9SDimitry Andric public:
StringSubstitution(FileCheckPatternContext * Context,StringRef VarName,size_t InsertIdx)376e8d8bef9SDimitry Andric   StringSubstitution(FileCheckPatternContext *Context, StringRef VarName,
377e8d8bef9SDimitry Andric                      size_t InsertIdx)
378e8d8bef9SDimitry Andric       : Substitution(Context, VarName, InsertIdx) {}
379e8d8bef9SDimitry Andric 
380e8d8bef9SDimitry Andric   /// \returns the text that the string variable in this substitution matched
381e8d8bef9SDimitry Andric   /// when defined, or an error if the variable is undefined.
382e8d8bef9SDimitry Andric   Expected<std::string> getResult() const override;
383e8d8bef9SDimitry Andric };
384e8d8bef9SDimitry Andric 
385e8d8bef9SDimitry Andric class NumericSubstitution : public Substitution {
386e8d8bef9SDimitry Andric private:
387e8d8bef9SDimitry Andric   /// Pointer to the class representing the expression whose value is to be
388e8d8bef9SDimitry Andric   /// substituted.
389e8d8bef9SDimitry Andric   std::unique_ptr<Expression> ExpressionPointer;
390e8d8bef9SDimitry Andric 
391e8d8bef9SDimitry Andric public:
NumericSubstitution(FileCheckPatternContext * Context,StringRef ExpressionStr,std::unique_ptr<Expression> ExpressionPointer,size_t InsertIdx)392e8d8bef9SDimitry Andric   NumericSubstitution(FileCheckPatternContext *Context, StringRef ExpressionStr,
393e8d8bef9SDimitry Andric                       std::unique_ptr<Expression> ExpressionPointer,
394e8d8bef9SDimitry Andric                       size_t InsertIdx)
395e8d8bef9SDimitry Andric       : Substitution(Context, ExpressionStr, InsertIdx),
396e8d8bef9SDimitry Andric         ExpressionPointer(std::move(ExpressionPointer)) {}
397e8d8bef9SDimitry Andric 
398e8d8bef9SDimitry Andric   /// \returns a string containing the result of evaluating the expression in
399e8d8bef9SDimitry Andric   /// this substitution, or an error if evaluation failed.
400e8d8bef9SDimitry Andric   Expected<std::string> getResult() const override;
401e8d8bef9SDimitry Andric };
402e8d8bef9SDimitry Andric 
403e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
404e8d8bef9SDimitry Andric // Pattern handling code.
405e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
406e8d8bef9SDimitry Andric 
407e8d8bef9SDimitry Andric /// Class holding the Pattern global state, shared by all patterns: tables
408e8d8bef9SDimitry Andric /// holding values of variables and whether they are defined or not at any
409e8d8bef9SDimitry Andric /// given time in the matching process.
410e8d8bef9SDimitry Andric class FileCheckPatternContext {
411e8d8bef9SDimitry Andric   friend class Pattern;
412e8d8bef9SDimitry Andric 
413e8d8bef9SDimitry Andric private:
414e8d8bef9SDimitry Andric   /// When matching a given pattern, this holds the value of all the string
415e8d8bef9SDimitry Andric   /// variables defined in previous patterns. In a pattern, only the last
416e8d8bef9SDimitry Andric   /// definition for a given variable is recorded in this table.
417e8d8bef9SDimitry Andric   /// Back-references are used for uses after any the other definition.
418e8d8bef9SDimitry Andric   StringMap<StringRef> GlobalVariableTable;
419e8d8bef9SDimitry Andric 
420e8d8bef9SDimitry Andric   /// Map of all string variables defined so far. Used at parse time to detect
421e8d8bef9SDimitry Andric   /// a name conflict between a numeric variable and a string variable when
422e8d8bef9SDimitry Andric   /// the former is defined on a later line than the latter.
423e8d8bef9SDimitry Andric   StringMap<bool> DefinedVariableTable;
424e8d8bef9SDimitry Andric 
425e8d8bef9SDimitry Andric   /// When matching a given pattern, this holds the pointers to the classes
426e8d8bef9SDimitry Andric   /// representing the numeric variables defined in previous patterns. When
427e8d8bef9SDimitry Andric   /// matching a pattern all definitions for that pattern are recorded in the
428e8d8bef9SDimitry Andric   /// NumericVariableDefs table in the Pattern instance of that pattern.
429e8d8bef9SDimitry Andric   StringMap<NumericVariable *> GlobalNumericVariableTable;
430e8d8bef9SDimitry Andric 
431e8d8bef9SDimitry Andric   /// Pointer to the class instance representing the @LINE pseudo variable for
432e8d8bef9SDimitry Andric   /// easily updating its value.
433e8d8bef9SDimitry Andric   NumericVariable *LineVariable = nullptr;
434e8d8bef9SDimitry Andric 
435e8d8bef9SDimitry Andric   /// Vector holding pointers to all parsed numeric variables. Used to
436e8d8bef9SDimitry Andric   /// automatically free them once they are guaranteed to no longer be used.
437e8d8bef9SDimitry Andric   std::vector<std::unique_ptr<NumericVariable>> NumericVariables;
438e8d8bef9SDimitry Andric 
439e8d8bef9SDimitry Andric   /// Vector holding pointers to all parsed expressions. Used to automatically
440e8d8bef9SDimitry Andric   /// free the expressions once they are guaranteed to no longer be used.
441e8d8bef9SDimitry Andric   std::vector<std::unique_ptr<Expression>> Expressions;
442e8d8bef9SDimitry Andric 
443e8d8bef9SDimitry Andric   /// Vector holding pointers to all substitutions. Used to automatically free
444e8d8bef9SDimitry Andric   /// them once they are guaranteed to no longer be used.
445e8d8bef9SDimitry Andric   std::vector<std::unique_ptr<Substitution>> Substitutions;
446e8d8bef9SDimitry Andric 
447e8d8bef9SDimitry Andric public:
448e8d8bef9SDimitry Andric   /// \returns the value of string variable \p VarName or an error if no such
449e8d8bef9SDimitry Andric   /// variable has been defined.
450e8d8bef9SDimitry Andric   Expected<StringRef> getPatternVarValue(StringRef VarName);
451e8d8bef9SDimitry Andric 
452e8d8bef9SDimitry Andric   /// Defines string and numeric variables from definitions given on the
453e8d8bef9SDimitry Andric   /// command line, passed as a vector of [#]VAR=VAL strings in
454e8d8bef9SDimitry Andric   /// \p CmdlineDefines. \returns an error list containing diagnostics against
455e8d8bef9SDimitry Andric   /// \p SM for all definition parsing failures, if any, or Success otherwise.
456e8d8bef9SDimitry Andric   Error defineCmdlineVariables(ArrayRef<StringRef> CmdlineDefines,
457e8d8bef9SDimitry Andric                                SourceMgr &SM);
458e8d8bef9SDimitry Andric 
459e8d8bef9SDimitry Andric   /// Create @LINE pseudo variable. Value is set when pattern are being
460e8d8bef9SDimitry Andric   /// matched.
461e8d8bef9SDimitry Andric   void createLineVariable();
462e8d8bef9SDimitry Andric 
463e8d8bef9SDimitry Andric   /// Undefines local variables (variables whose name does not start with a '$'
464e8d8bef9SDimitry Andric   /// sign), i.e. removes them from GlobalVariableTable and from
465e8d8bef9SDimitry Andric   /// GlobalNumericVariableTable and also clears the value of numeric
466e8d8bef9SDimitry Andric   /// variables.
467e8d8bef9SDimitry Andric   void clearLocalVars();
468e8d8bef9SDimitry Andric 
469e8d8bef9SDimitry Andric private:
470e8d8bef9SDimitry Andric   /// Makes a new numeric variable and registers it for destruction when the
471e8d8bef9SDimitry Andric   /// context is destroyed.
472e8d8bef9SDimitry Andric   template <class... Types> NumericVariable *makeNumericVariable(Types... args);
473e8d8bef9SDimitry Andric 
474e8d8bef9SDimitry Andric   /// Makes a new string substitution and registers it for destruction when the
475e8d8bef9SDimitry Andric   /// context is destroyed.
476e8d8bef9SDimitry Andric   Substitution *makeStringSubstitution(StringRef VarName, size_t InsertIdx);
477e8d8bef9SDimitry Andric 
478e8d8bef9SDimitry Andric   /// Makes a new numeric substitution and registers it for destruction when
479e8d8bef9SDimitry Andric   /// the context is destroyed.
480e8d8bef9SDimitry Andric   Substitution *makeNumericSubstitution(StringRef ExpressionStr,
481e8d8bef9SDimitry Andric                                         std::unique_ptr<Expression> Expression,
482e8d8bef9SDimitry Andric                                         size_t InsertIdx);
483e8d8bef9SDimitry Andric };
484e8d8bef9SDimitry Andric 
485e8d8bef9SDimitry Andric /// Class to represent an error holding a diagnostic with location information
486e8d8bef9SDimitry Andric /// used when printing it.
487e8d8bef9SDimitry Andric class ErrorDiagnostic : public ErrorInfo<ErrorDiagnostic> {
488e8d8bef9SDimitry Andric private:
489e8d8bef9SDimitry Andric   SMDiagnostic Diagnostic;
490fe6060f1SDimitry Andric   SMRange Range;
491e8d8bef9SDimitry Andric 
492e8d8bef9SDimitry Andric public:
493e8d8bef9SDimitry Andric   static char ID;
494e8d8bef9SDimitry Andric 
ErrorDiagnostic(SMDiagnostic && Diag,SMRange Range)495fe6060f1SDimitry Andric   ErrorDiagnostic(SMDiagnostic &&Diag, SMRange Range)
496fe6060f1SDimitry Andric       : Diagnostic(Diag), Range(Range) {}
497e8d8bef9SDimitry Andric 
convertToErrorCode()498e8d8bef9SDimitry Andric   std::error_code convertToErrorCode() const override {
499e8d8bef9SDimitry Andric     return inconvertibleErrorCode();
500e8d8bef9SDimitry Andric   }
501e8d8bef9SDimitry Andric 
502e8d8bef9SDimitry Andric   /// Print diagnostic associated with this error when printing the error.
log(raw_ostream & OS)503e8d8bef9SDimitry Andric   void log(raw_ostream &OS) const override { Diagnostic.print(nullptr, OS); }
504e8d8bef9SDimitry Andric 
getMessage()505fe6060f1SDimitry Andric   StringRef getMessage() const { return Diagnostic.getMessage(); }
getRange()506fe6060f1SDimitry Andric   SMRange getRange() const { return Range; }
507fe6060f1SDimitry Andric 
508fe6060f1SDimitry Andric   static Error get(const SourceMgr &SM, SMLoc Loc, const Twine &ErrMsg,
509bdd1243dSDimitry Andric                    SMRange Range = std::nullopt) {
510e8d8bef9SDimitry Andric     return make_error<ErrorDiagnostic>(
511fe6060f1SDimitry Andric         SM.GetMessage(Loc, SourceMgr::DK_Error, ErrMsg), Range);
512e8d8bef9SDimitry Andric   }
513e8d8bef9SDimitry Andric 
get(const SourceMgr & SM,StringRef Buffer,const Twine & ErrMsg)514e8d8bef9SDimitry Andric   static Error get(const SourceMgr &SM, StringRef Buffer, const Twine &ErrMsg) {
515fe6060f1SDimitry Andric     SMLoc Start = SMLoc::getFromPointer(Buffer.data());
516fe6060f1SDimitry Andric     SMLoc End = SMLoc::getFromPointer(Buffer.data() + Buffer.size());
517fe6060f1SDimitry Andric     return get(SM, Start, ErrMsg, SMRange(Start, End));
518e8d8bef9SDimitry Andric   }
519e8d8bef9SDimitry Andric };
520e8d8bef9SDimitry Andric 
521e8d8bef9SDimitry Andric class NotFoundError : public ErrorInfo<NotFoundError> {
522e8d8bef9SDimitry Andric public:
523e8d8bef9SDimitry Andric   static char ID;
524e8d8bef9SDimitry Andric 
convertToErrorCode()525e8d8bef9SDimitry Andric   std::error_code convertToErrorCode() const override {
526e8d8bef9SDimitry Andric     return inconvertibleErrorCode();
527e8d8bef9SDimitry Andric   }
528e8d8bef9SDimitry Andric 
529e8d8bef9SDimitry Andric   /// Print diagnostic associated with this error when printing the error.
log(raw_ostream & OS)530e8d8bef9SDimitry Andric   void log(raw_ostream &OS) const override {
531e8d8bef9SDimitry Andric     OS << "String not found in input";
532e8d8bef9SDimitry Andric   }
533e8d8bef9SDimitry Andric };
534e8d8bef9SDimitry Andric 
535fe6060f1SDimitry Andric /// An error that has already been reported.
536fe6060f1SDimitry Andric ///
537fe6060f1SDimitry Andric /// This class is designed to support a function whose callers may need to know
538fe6060f1SDimitry Andric /// whether the function encountered and reported an error but never need to
539fe6060f1SDimitry Andric /// know the nature of that error.  For example, the function has a return type
540fe6060f1SDimitry Andric /// of \c Error and always returns either \c ErrorReported or \c ErrorSuccess.
541fe6060f1SDimitry Andric /// That interface is similar to that of a function returning bool to indicate
542fe6060f1SDimitry Andric /// an error except, in the former case, (1) there is no confusion over polarity
543fe6060f1SDimitry Andric /// and (2) the caller must either check the result or explicitly ignore it with
544fe6060f1SDimitry Andric /// a call like \c consumeError.
545fe6060f1SDimitry Andric class ErrorReported final : public ErrorInfo<ErrorReported> {
546fe6060f1SDimitry Andric public:
547fe6060f1SDimitry Andric   static char ID;
548fe6060f1SDimitry Andric 
convertToErrorCode()549fe6060f1SDimitry Andric   std::error_code convertToErrorCode() const override {
550fe6060f1SDimitry Andric     return inconvertibleErrorCode();
551fe6060f1SDimitry Andric   }
552fe6060f1SDimitry Andric 
553fe6060f1SDimitry Andric   /// Print diagnostic associated with this error when printing the error.
log(raw_ostream & OS)554fe6060f1SDimitry Andric   void log(raw_ostream &OS) const override {
555fe6060f1SDimitry Andric     OS << "error previously reported";
556fe6060f1SDimitry Andric   }
557fe6060f1SDimitry Andric 
reportedOrSuccess(bool HasErrorReported)558fe6060f1SDimitry Andric   static inline Error reportedOrSuccess(bool HasErrorReported) {
559fe6060f1SDimitry Andric     if (HasErrorReported)
560fe6060f1SDimitry Andric       return make_error<ErrorReported>();
561fe6060f1SDimitry Andric     return Error::success();
562fe6060f1SDimitry Andric   }
563fe6060f1SDimitry Andric };
564fe6060f1SDimitry Andric 
565e8d8bef9SDimitry Andric class Pattern {
566e8d8bef9SDimitry Andric   SMLoc PatternLoc;
567e8d8bef9SDimitry Andric 
568e8d8bef9SDimitry Andric   /// A fixed string to match as the pattern or empty if this pattern requires
569e8d8bef9SDimitry Andric   /// a regex match.
570e8d8bef9SDimitry Andric   StringRef FixedStr;
571e8d8bef9SDimitry Andric 
572e8d8bef9SDimitry Andric   /// A regex string to match as the pattern or empty if this pattern requires
573e8d8bef9SDimitry Andric   /// a fixed string to match.
574e8d8bef9SDimitry Andric   std::string RegExStr;
575e8d8bef9SDimitry Andric 
576e8d8bef9SDimitry Andric   /// Entries in this vector represent a substitution of a string variable or
577e8d8bef9SDimitry Andric   /// an expression in the RegExStr regex at match time. For example, in the
578e8d8bef9SDimitry Andric   /// case of a CHECK directive with the pattern "foo[[bar]]baz[[#N+1]]",
579e8d8bef9SDimitry Andric   /// RegExStr will contain "foobaz" and we'll get two entries in this vector
580e8d8bef9SDimitry Andric   /// that tells us to insert the value of string variable "bar" at offset 3
581e8d8bef9SDimitry Andric   /// and the value of expression "N+1" at offset 6.
582e8d8bef9SDimitry Andric   std::vector<Substitution *> Substitutions;
583e8d8bef9SDimitry Andric 
584e8d8bef9SDimitry Andric   /// Maps names of string variables defined in a pattern to the number of
585e8d8bef9SDimitry Andric   /// their parenthesis group in RegExStr capturing their last definition.
586e8d8bef9SDimitry Andric   ///
587e8d8bef9SDimitry Andric   /// E.g. for the pattern "foo[[bar:.*]]baz([[bar]][[QUUX]][[bar:.*]])",
588e8d8bef9SDimitry Andric   /// RegExStr will be "foo(.*)baz(\1<quux value>(.*))" where <quux value> is
589e8d8bef9SDimitry Andric   /// the value captured for QUUX on the earlier line where it was defined, and
590e8d8bef9SDimitry Andric   /// VariableDefs will map "bar" to the third parenthesis group which captures
591e8d8bef9SDimitry Andric   /// the second definition of "bar".
592e8d8bef9SDimitry Andric   ///
593e8d8bef9SDimitry Andric   /// Note: uses std::map rather than StringMap to be able to get the key when
594e8d8bef9SDimitry Andric   /// iterating over values.
595e8d8bef9SDimitry Andric   std::map<StringRef, unsigned> VariableDefs;
596e8d8bef9SDimitry Andric 
597e8d8bef9SDimitry Andric   /// Structure representing the definition of a numeric variable in a pattern.
598e8d8bef9SDimitry Andric   /// It holds the pointer to the class instance holding the value and matching
599e8d8bef9SDimitry Andric   /// format of the numeric variable whose value is being defined and the
600e8d8bef9SDimitry Andric   /// number of the parenthesis group in RegExStr to capture that value.
601e8d8bef9SDimitry Andric   struct NumericVariableMatch {
602e8d8bef9SDimitry Andric     /// Pointer to class instance holding the value and matching format of the
603e8d8bef9SDimitry Andric     /// numeric variable being defined.
604e8d8bef9SDimitry Andric     NumericVariable *DefinedNumericVariable;
605e8d8bef9SDimitry Andric 
606e8d8bef9SDimitry Andric     /// Number of the parenthesis group in RegExStr that captures the value of
607e8d8bef9SDimitry Andric     /// this numeric variable definition.
608e8d8bef9SDimitry Andric     unsigned CaptureParenGroup;
609e8d8bef9SDimitry Andric   };
610e8d8bef9SDimitry Andric 
611e8d8bef9SDimitry Andric   /// Holds the number of the parenthesis group in RegExStr and pointer to the
612e8d8bef9SDimitry Andric   /// corresponding NumericVariable class instance of all numeric variable
613e8d8bef9SDimitry Andric   /// definitions. Used to set the matched value of all those variables.
614e8d8bef9SDimitry Andric   StringMap<NumericVariableMatch> NumericVariableDefs;
615e8d8bef9SDimitry Andric 
616e8d8bef9SDimitry Andric   /// Pointer to a class instance holding the global state shared by all
617e8d8bef9SDimitry Andric   /// patterns:
618e8d8bef9SDimitry Andric   /// - separate tables with the values of live string and numeric variables
619e8d8bef9SDimitry Andric   ///   respectively at the start of any given CHECK line;
620e8d8bef9SDimitry Andric   /// - table holding whether a string variable has been defined at any given
621e8d8bef9SDimitry Andric   ///   point during the parsing phase.
622e8d8bef9SDimitry Andric   FileCheckPatternContext *Context;
623e8d8bef9SDimitry Andric 
624e8d8bef9SDimitry Andric   Check::FileCheckType CheckTy;
625e8d8bef9SDimitry Andric 
626bdd1243dSDimitry Andric   /// Line number for this CHECK pattern or std::nullopt if it is an implicit
627bdd1243dSDimitry Andric   /// pattern. Used to determine whether a variable definition is made on an
628bdd1243dSDimitry Andric   /// earlier line to the one with this CHECK.
629bdd1243dSDimitry Andric   std::optional<size_t> LineNumber;
630e8d8bef9SDimitry Andric 
631e8d8bef9SDimitry Andric   /// Ignore case while matching if set to true.
632e8d8bef9SDimitry Andric   bool IgnoreCase = false;
633e8d8bef9SDimitry Andric 
634e8d8bef9SDimitry Andric public:
635e8d8bef9SDimitry Andric   Pattern(Check::FileCheckType Ty, FileCheckPatternContext *Context,
636bdd1243dSDimitry Andric           std::optional<size_t> Line = std::nullopt)
Context(Context)637e8d8bef9SDimitry Andric       : Context(Context), CheckTy(Ty), LineNumber(Line) {}
638e8d8bef9SDimitry Andric 
639e8d8bef9SDimitry Andric   /// \returns the location in source code.
getLoc()640e8d8bef9SDimitry Andric   SMLoc getLoc() const { return PatternLoc; }
641e8d8bef9SDimitry Andric 
642e8d8bef9SDimitry Andric   /// \returns the pointer to the global state for all patterns in this
643e8d8bef9SDimitry Andric   /// FileCheck instance.
getContext()644e8d8bef9SDimitry Andric   FileCheckPatternContext *getContext() const { return Context; }
645e8d8bef9SDimitry Andric 
646e8d8bef9SDimitry Andric   /// \returns whether \p C is a valid first character for a variable name.
647e8d8bef9SDimitry Andric   static bool isValidVarNameStart(char C);
648e8d8bef9SDimitry Andric 
649e8d8bef9SDimitry Andric   /// Parsing information about a variable.
650e8d8bef9SDimitry Andric   struct VariableProperties {
651e8d8bef9SDimitry Andric     StringRef Name;
652e8d8bef9SDimitry Andric     bool IsPseudo;
653e8d8bef9SDimitry Andric   };
654e8d8bef9SDimitry Andric 
655e8d8bef9SDimitry Andric   /// Parses the string at the start of \p Str for a variable name. \returns
656e8d8bef9SDimitry Andric   /// a VariableProperties structure holding the variable name and whether it
657e8d8bef9SDimitry Andric   /// is the name of a pseudo variable, or an error holding a diagnostic
658e8d8bef9SDimitry Andric   /// against \p SM if parsing fail. If parsing was successful, also strips
659e8d8bef9SDimitry Andric   /// \p Str from the variable name.
660e8d8bef9SDimitry Andric   static Expected<VariableProperties> parseVariable(StringRef &Str,
661e8d8bef9SDimitry Andric                                                     const SourceMgr &SM);
662e8d8bef9SDimitry Andric   /// Parses \p Expr for a numeric substitution block at line \p LineNumber,
663e8d8bef9SDimitry Andric   /// or before input is parsed if \p LineNumber is None. Parameter
664e8d8bef9SDimitry Andric   /// \p IsLegacyLineExpr indicates whether \p Expr should be a legacy @LINE
665e8d8bef9SDimitry Andric   /// expression and \p Context points to the class instance holding the live
666e8d8bef9SDimitry Andric   /// string and numeric variables. \returns a pointer to the class instance
667e8d8bef9SDimitry Andric   /// representing the expression whose value must be substitued, or an error
668e8d8bef9SDimitry Andric   /// holding a diagnostic against \p SM if parsing fails. If substitution was
669e8d8bef9SDimitry Andric   /// successful, sets \p DefinedNumericVariable to point to the class
670e8d8bef9SDimitry Andric   /// representing the numeric variable defined in this numeric substitution
671bdd1243dSDimitry Andric   /// block, or std::nullopt if this block does not define any variable.
672e8d8bef9SDimitry Andric   static Expected<std::unique_ptr<Expression>> parseNumericSubstitutionBlock(
673bdd1243dSDimitry Andric       StringRef Expr, std::optional<NumericVariable *> &DefinedNumericVariable,
674bdd1243dSDimitry Andric       bool IsLegacyLineExpr, std::optional<size_t> LineNumber,
675e8d8bef9SDimitry Andric       FileCheckPatternContext *Context, const SourceMgr &SM);
676e8d8bef9SDimitry Andric   /// Parses the pattern in \p PatternStr and initializes this Pattern instance
677e8d8bef9SDimitry Andric   /// accordingly.
678e8d8bef9SDimitry Andric   ///
679e8d8bef9SDimitry Andric   /// \p Prefix provides which prefix is being matched, \p Req describes the
680e8d8bef9SDimitry Andric   /// global options that influence the parsing such as whitespace
681e8d8bef9SDimitry Andric   /// canonicalization, \p SM provides the SourceMgr used for error reports.
682e8d8bef9SDimitry Andric   /// \returns true in case of an error, false otherwise.
683e8d8bef9SDimitry Andric   bool parsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
684e8d8bef9SDimitry Andric                     const FileCheckRequest &Req);
685fe6060f1SDimitry Andric   struct Match {
686fe6060f1SDimitry Andric     size_t Pos;
687fe6060f1SDimitry Andric     size_t Len;
688fe6060f1SDimitry Andric   };
689fe6060f1SDimitry Andric   struct MatchResult {
690bdd1243dSDimitry Andric     std::optional<Match> TheMatch;
691fe6060f1SDimitry Andric     Error TheError;
MatchResultMatchResult692fe6060f1SDimitry Andric     MatchResult(size_t MatchPos, size_t MatchLen, Error E)
693fe6060f1SDimitry Andric         : TheMatch(Match{MatchPos, MatchLen}), TheError(std::move(E)) {}
MatchResultMatchResult694fe6060f1SDimitry Andric     MatchResult(Match M, Error E) : TheMatch(M), TheError(std::move(E)) {}
MatchResultMatchResult695fe6060f1SDimitry Andric     MatchResult(Error E) : TheError(std::move(E)) {}
696fe6060f1SDimitry Andric   };
697fe6060f1SDimitry Andric   /// Matches the pattern string against the input buffer \p Buffer.
698e8d8bef9SDimitry Andric   ///
699fe6060f1SDimitry Andric   /// \returns either (1) an error resulting in no match or (2) a match possibly
700fe6060f1SDimitry Andric   /// with an error encountered while processing the match.
701e8d8bef9SDimitry Andric   ///
702e8d8bef9SDimitry Andric   /// The GlobalVariableTable StringMap in the FileCheckPatternContext class
703e8d8bef9SDimitry Andric   /// instance provides the current values of FileCheck string variables and is
704e8d8bef9SDimitry Andric   /// updated if this match defines new values. Likewise, the
705e8d8bef9SDimitry Andric   /// GlobalNumericVariableTable StringMap in the same class provides the
706e8d8bef9SDimitry Andric   /// current values of FileCheck numeric variables and is updated if this
707e8d8bef9SDimitry Andric   /// match defines new numeric values.
708fe6060f1SDimitry Andric   MatchResult match(StringRef Buffer, const SourceMgr &SM) const;
709fe6060f1SDimitry Andric   /// Prints the value of successful substitutions.
710e8d8bef9SDimitry Andric   void printSubstitutions(const SourceMgr &SM, StringRef Buffer,
711e8d8bef9SDimitry Andric                           SMRange MatchRange, FileCheckDiag::MatchType MatchTy,
712e8d8bef9SDimitry Andric                           std::vector<FileCheckDiag> *Diags) const;
713e8d8bef9SDimitry Andric   void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
714e8d8bef9SDimitry Andric                        std::vector<FileCheckDiag> *Diags) const;
715e8d8bef9SDimitry Andric 
hasVariable()716e8d8bef9SDimitry Andric   bool hasVariable() const {
717e8d8bef9SDimitry Andric     return !(Substitutions.empty() && VariableDefs.empty());
718e8d8bef9SDimitry Andric   }
719e8d8bef9SDimitry Andric   void printVariableDefs(const SourceMgr &SM, FileCheckDiag::MatchType MatchTy,
720e8d8bef9SDimitry Andric                          std::vector<FileCheckDiag> *Diags) const;
721e8d8bef9SDimitry Andric 
getCheckTy()722e8d8bef9SDimitry Andric   Check::FileCheckType getCheckTy() const { return CheckTy; }
723e8d8bef9SDimitry Andric 
getCount()724e8d8bef9SDimitry Andric   int getCount() const { return CheckTy.getCount(); }
725e8d8bef9SDimitry Andric 
726e8d8bef9SDimitry Andric private:
727e8d8bef9SDimitry Andric   bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
728e8d8bef9SDimitry Andric   void AddBackrefToRegEx(unsigned BackrefNum);
729e8d8bef9SDimitry Andric   /// Computes an arbitrary estimate for the quality of matching this pattern
730e8d8bef9SDimitry Andric   /// at the start of \p Buffer; a distance of zero should correspond to a
731e8d8bef9SDimitry Andric   /// perfect match.
732e8d8bef9SDimitry Andric   unsigned computeMatchDistance(StringRef Buffer) const;
733e8d8bef9SDimitry Andric   /// Finds the closing sequence of a regex variable usage or definition.
734e8d8bef9SDimitry Andric   ///
735e8d8bef9SDimitry Andric   /// \p Str has to point in the beginning of the definition (right after the
736e8d8bef9SDimitry Andric   /// opening sequence). \p SM holds the SourceMgr used for error reporting.
737e8d8bef9SDimitry Andric   ///  \returns the offset of the closing sequence within Str, or npos if it
738e8d8bef9SDimitry Andric   /// was not found.
739e8d8bef9SDimitry Andric   static size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
740e8d8bef9SDimitry Andric 
741e8d8bef9SDimitry Andric   /// Parses \p Expr for the name of a numeric variable to be defined at line
742e8d8bef9SDimitry Andric   /// \p LineNumber, or before input is parsed if \p LineNumber is None.
743e8d8bef9SDimitry Andric   /// \returns a pointer to the class instance representing that variable,
744e8d8bef9SDimitry Andric   /// creating it if needed, or an error holding a diagnostic against \p SM
745e8d8bef9SDimitry Andric   /// should defining such a variable be invalid.
746e8d8bef9SDimitry Andric   static Expected<NumericVariable *> parseNumericVariableDefinition(
747e8d8bef9SDimitry Andric       StringRef &Expr, FileCheckPatternContext *Context,
748bdd1243dSDimitry Andric       std::optional<size_t> LineNumber, ExpressionFormat ImplicitFormat,
749e8d8bef9SDimitry Andric       const SourceMgr &SM);
750e8d8bef9SDimitry Andric   /// Parses \p Name as a (pseudo if \p IsPseudo is true) numeric variable use
751e8d8bef9SDimitry Andric   /// at line \p LineNumber, or before input is parsed if \p LineNumber is
752e8d8bef9SDimitry Andric   /// None. Parameter \p Context points to the class instance holding the live
753e8d8bef9SDimitry Andric   /// string and numeric variables. \returns the pointer to the class instance
754e8d8bef9SDimitry Andric   /// representing that variable if successful, or an error holding a
755e8d8bef9SDimitry Andric   /// diagnostic against \p SM otherwise.
756e8d8bef9SDimitry Andric   static Expected<std::unique_ptr<NumericVariableUse>> parseNumericVariableUse(
757bdd1243dSDimitry Andric       StringRef Name, bool IsPseudo, std::optional<size_t> LineNumber,
758e8d8bef9SDimitry Andric       FileCheckPatternContext *Context, const SourceMgr &SM);
759e8d8bef9SDimitry Andric   enum class AllowedOperand { LineVar, LegacyLiteral, Any };
760e8d8bef9SDimitry Andric   /// Parses \p Expr for use of a numeric operand at line \p LineNumber, or
761e8d8bef9SDimitry Andric   /// before input is parsed if \p LineNumber is None. Accepts literal values,
762e8d8bef9SDimitry Andric   /// numeric variables and function calls, depending on the value of \p AO.
763e8d8bef9SDimitry Andric   /// \p MaybeInvalidConstraint indicates whether the text being parsed could
764e8d8bef9SDimitry Andric   /// be an invalid constraint. \p Context points to the class instance holding
765e8d8bef9SDimitry Andric   /// the live string and numeric variables. \returns the class representing
766e8d8bef9SDimitry Andric   /// that operand in the AST of the expression or an error holding a
767e8d8bef9SDimitry Andric   /// diagnostic against \p SM otherwise. If \p Expr starts with a "(" this
768e8d8bef9SDimitry Andric   /// function will attempt to parse a parenthesized expression.
769e8d8bef9SDimitry Andric   static Expected<std::unique_ptr<ExpressionAST>>
770e8d8bef9SDimitry Andric   parseNumericOperand(StringRef &Expr, AllowedOperand AO, bool ConstraintParsed,
771bdd1243dSDimitry Andric                       std::optional<size_t> LineNumber,
772e8d8bef9SDimitry Andric                       FileCheckPatternContext *Context, const SourceMgr &SM);
773e8d8bef9SDimitry Andric   /// Parses and updates \p RemainingExpr for a binary operation at line
774e8d8bef9SDimitry Andric   /// \p LineNumber, or before input is parsed if \p LineNumber is None. The
775e8d8bef9SDimitry Andric   /// left operand of this binary operation is given in \p LeftOp and \p Expr
776e8d8bef9SDimitry Andric   /// holds the string for the full expression, including the left operand.
777e8d8bef9SDimitry Andric   /// Parameter \p IsLegacyLineExpr indicates whether we are parsing a legacy
778e8d8bef9SDimitry Andric   /// @LINE expression. Parameter \p Context points to the class instance
779e8d8bef9SDimitry Andric   /// holding the live string and numeric variables. \returns the class
780e8d8bef9SDimitry Andric   /// representing the binary operation in the AST of the expression, or an
781e8d8bef9SDimitry Andric   /// error holding a diagnostic against \p SM otherwise.
782e8d8bef9SDimitry Andric   static Expected<std::unique_ptr<ExpressionAST>>
783e8d8bef9SDimitry Andric   parseBinop(StringRef Expr, StringRef &RemainingExpr,
784e8d8bef9SDimitry Andric              std::unique_ptr<ExpressionAST> LeftOp, bool IsLegacyLineExpr,
785bdd1243dSDimitry Andric              std::optional<size_t> LineNumber, FileCheckPatternContext *Context,
786e8d8bef9SDimitry Andric              const SourceMgr &SM);
787e8d8bef9SDimitry Andric 
788e8d8bef9SDimitry Andric   /// Parses a parenthesized expression inside \p Expr at line \p LineNumber, or
789e8d8bef9SDimitry Andric   /// before input is parsed if \p LineNumber is None. \p Expr must start with
790e8d8bef9SDimitry Andric   /// a '('. Accepts both literal values and numeric variables. Parameter \p
791e8d8bef9SDimitry Andric   /// Context points to the class instance holding the live string and numeric
792e8d8bef9SDimitry Andric   /// variables. \returns the class representing that operand in the AST of the
793e8d8bef9SDimitry Andric   /// expression or an error holding a diagnostic against \p SM otherwise.
794e8d8bef9SDimitry Andric   static Expected<std::unique_ptr<ExpressionAST>>
795bdd1243dSDimitry Andric   parseParenExpr(StringRef &Expr, std::optional<size_t> LineNumber,
796e8d8bef9SDimitry Andric                  FileCheckPatternContext *Context, const SourceMgr &SM);
797e8d8bef9SDimitry Andric 
798e8d8bef9SDimitry Andric   /// Parses \p Expr for an argument list belonging to a call to function \p
799e8d8bef9SDimitry Andric   /// FuncName at line \p LineNumber, or before input is parsed if \p LineNumber
800e8d8bef9SDimitry Andric   /// is None. Parameter \p FuncLoc is the source location used for diagnostics.
801e8d8bef9SDimitry Andric   /// Parameter \p Context points to the class instance holding the live string
802e8d8bef9SDimitry Andric   /// and numeric variables. \returns the class representing that call in the
803e8d8bef9SDimitry Andric   /// AST of the expression or an error holding a diagnostic against \p SM
804e8d8bef9SDimitry Andric   /// otherwise.
805e8d8bef9SDimitry Andric   static Expected<std::unique_ptr<ExpressionAST>>
806e8d8bef9SDimitry Andric   parseCallExpr(StringRef &Expr, StringRef FuncName,
807bdd1243dSDimitry Andric                 std::optional<size_t> LineNumber,
808bdd1243dSDimitry Andric                 FileCheckPatternContext *Context, const SourceMgr &SM);
809e8d8bef9SDimitry Andric };
810e8d8bef9SDimitry Andric 
811e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
812e8d8bef9SDimitry Andric // Check Strings.
813e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
814e8d8bef9SDimitry Andric 
815e8d8bef9SDimitry Andric /// A check that we found in the input file.
816e8d8bef9SDimitry Andric struct FileCheckString {
817e8d8bef9SDimitry Andric   /// The pattern to match.
818e8d8bef9SDimitry Andric   Pattern Pat;
819e8d8bef9SDimitry Andric 
820e8d8bef9SDimitry Andric   /// Which prefix name this check matched.
821e8d8bef9SDimitry Andric   StringRef Prefix;
822e8d8bef9SDimitry Andric 
823e8d8bef9SDimitry Andric   /// The location in the match file that the check string was specified.
824e8d8bef9SDimitry Andric   SMLoc Loc;
825e8d8bef9SDimitry Andric 
826*7a6dacacSDimitry Andric   /// Hold the information about the DAG/NOT strings in the program, which are
827*7a6dacacSDimitry Andric   /// not explicitly stored otherwise. This allows for better and more accurate
828*7a6dacacSDimitry Andric   /// diagnostic messages.
829*7a6dacacSDimitry Andric   struct DagNotPrefixInfo {
830*7a6dacacSDimitry Andric     Pattern DagNotPat;
831*7a6dacacSDimitry Andric     StringRef DagNotPrefix;
832*7a6dacacSDimitry Andric 
DagNotPrefixInfoFileCheckString::DagNotPrefixInfo833*7a6dacacSDimitry Andric     DagNotPrefixInfo(const Pattern &P, StringRef S)
834*7a6dacacSDimitry Andric         : DagNotPat(P), DagNotPrefix(S) {}
835*7a6dacacSDimitry Andric   };
836*7a6dacacSDimitry Andric 
837*7a6dacacSDimitry Andric   /// Hold the DAG/NOT strings occurring in the input file.
838*7a6dacacSDimitry Andric   std::vector<DagNotPrefixInfo> DagNotStrings;
839e8d8bef9SDimitry Andric 
FileCheckStringFileCheckString840e8d8bef9SDimitry Andric   FileCheckString(const Pattern &P, StringRef S, SMLoc L)
841e8d8bef9SDimitry Andric       : Pat(P), Prefix(S), Loc(L) {}
842e8d8bef9SDimitry Andric 
843e8d8bef9SDimitry Andric   /// Matches check string and its "not strings" and/or "dag strings".
844e8d8bef9SDimitry Andric   size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
845e8d8bef9SDimitry Andric                size_t &MatchLen, FileCheckRequest &Req,
846e8d8bef9SDimitry Andric                std::vector<FileCheckDiag> *Diags) const;
847e8d8bef9SDimitry Andric 
848e8d8bef9SDimitry Andric   /// Verifies that there is a single line in the given \p Buffer. Errors are
849e8d8bef9SDimitry Andric   /// reported against \p SM.
850e8d8bef9SDimitry Andric   bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
851e8d8bef9SDimitry Andric   /// Verifies that there is no newline in the given \p Buffer. Errors are
852e8d8bef9SDimitry Andric   /// reported against \p SM.
853e8d8bef9SDimitry Andric   bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
854e8d8bef9SDimitry Andric   /// Verifies that none of the strings in \p NotStrings are found in the given
855e8d8bef9SDimitry Andric   /// \p Buffer. Errors are reported against \p SM and diagnostics recorded in
856e8d8bef9SDimitry Andric   /// \p Diags according to the verbosity level set in \p Req.
857e8d8bef9SDimitry Andric   bool CheckNot(const SourceMgr &SM, StringRef Buffer,
858*7a6dacacSDimitry Andric                 const std::vector<const DagNotPrefixInfo *> &NotStrings,
859e8d8bef9SDimitry Andric                 const FileCheckRequest &Req,
860e8d8bef9SDimitry Andric                 std::vector<FileCheckDiag> *Diags) const;
861e8d8bef9SDimitry Andric   /// Matches "dag strings" and their mixed "not strings".
862e8d8bef9SDimitry Andric   size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
863*7a6dacacSDimitry Andric                   std::vector<const DagNotPrefixInfo *> &NotStrings,
864e8d8bef9SDimitry Andric                   const FileCheckRequest &Req,
865e8d8bef9SDimitry Andric                   std::vector<FileCheckDiag> *Diags) const;
866e8d8bef9SDimitry Andric };
867e8d8bef9SDimitry Andric 
868e8d8bef9SDimitry Andric } // namespace llvm
869e8d8bef9SDimitry Andric 
870e8d8bef9SDimitry Andric #endif
871