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