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