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