11d5ae102SDimitry Andric //===-- FileCheckImpl.h - Private FileCheck Interface ------------*- C++ -*-==// 21d5ae102SDimitry Andric // 31d5ae102SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 41d5ae102SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 51d5ae102SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 61d5ae102SDimitry Andric // 71d5ae102SDimitry Andric //===----------------------------------------------------------------------===// 81d5ae102SDimitry Andric // 91d5ae102SDimitry Andric // This file defines the private interfaces of FileCheck. Its purpose is to 101d5ae102SDimitry Andric // allow unit testing of FileCheck and to separate the interface from the 111d5ae102SDimitry Andric // implementation. It is only meant to be used by FileCheck. 121d5ae102SDimitry Andric // 131d5ae102SDimitry Andric //===----------------------------------------------------------------------===// 141d5ae102SDimitry Andric 15b60736ecSDimitry Andric #ifndef LLVM_LIB_FILECHECK_FILECHECKIMPL_H 16b60736ecSDimitry Andric #define LLVM_LIB_FILECHECK_FILECHECKIMPL_H 171d5ae102SDimitry Andric 187fa27ce4SDimitry Andric #include "llvm/ADT/APInt.h" 191d5ae102SDimitry Andric #include "llvm/ADT/StringMap.h" 201d5ae102SDimitry Andric #include "llvm/ADT/StringRef.h" 21b60736ecSDimitry Andric #include "llvm/FileCheck/FileCheck.h" 221d5ae102SDimitry Andric #include "llvm/Support/Error.h" 231d5ae102SDimitry Andric #include "llvm/Support/SourceMgr.h" 241d5ae102SDimitry Andric #include <map> 25e3b55780SDimitry Andric #include <optional> 261d5ae102SDimitry Andric #include <string> 271d5ae102SDimitry Andric #include <vector> 281d5ae102SDimitry Andric 291d5ae102SDimitry Andric namespace llvm { 301d5ae102SDimitry Andric 311d5ae102SDimitry Andric //===----------------------------------------------------------------------===// 321d5ae102SDimitry Andric // Numeric substitution handling code. 331d5ae102SDimitry Andric //===----------------------------------------------------------------------===// 341d5ae102SDimitry Andric 35cfca06d7SDimitry Andric /// Type representing the format an expression value should be textualized into 36cfca06d7SDimitry Andric /// for matching. Used to represent both explicit format specifiers as well as 37cfca06d7SDimitry Andric /// implicit format from using numeric variables. 38cfca06d7SDimitry Andric struct ExpressionFormat { 39cfca06d7SDimitry Andric enum class Kind { 40cfca06d7SDimitry Andric /// Denote absence of format. Used for implicit format of literals and 41cfca06d7SDimitry Andric /// empty expressions. 42cfca06d7SDimitry Andric NoFormat, 43cfca06d7SDimitry Andric /// Value is an unsigned integer and should be printed as a decimal number. 44cfca06d7SDimitry Andric Unsigned, 45cfca06d7SDimitry Andric /// Value is a signed integer and should be printed as a decimal number. 46cfca06d7SDimitry Andric Signed, 47cfca06d7SDimitry Andric /// Value should be printed as an uppercase hex number. 48cfca06d7SDimitry Andric HexUpper, 49cfca06d7SDimitry Andric /// Value should be printed as a lowercase hex number. 50cfca06d7SDimitry Andric HexLower 51cfca06d7SDimitry Andric }; 52cfca06d7SDimitry Andric 53cfca06d7SDimitry Andric private: 54cfca06d7SDimitry Andric Kind Value; 55b60736ecSDimitry Andric unsigned Precision = 0; 56344a3780SDimitry Andric /// printf-like "alternate form" selected. 57344a3780SDimitry Andric bool AlternateForm = false; 58cfca06d7SDimitry Andric 59cfca06d7SDimitry Andric public: 60cfca06d7SDimitry Andric /// Evaluates a format to true if it can be used in a match. 61cfca06d7SDimitry Andric explicit operator bool() const { return Value != Kind::NoFormat; } 62cfca06d7SDimitry Andric 63cfca06d7SDimitry Andric /// Define format equality: formats are equal if neither is NoFormat and 64b60736ecSDimitry Andric /// their kinds and precision are the same. 65cfca06d7SDimitry Andric bool operator==(const ExpressionFormat &Other) const { 66b60736ecSDimitry Andric return Value != Kind::NoFormat && Value == Other.Value && 67344a3780SDimitry Andric Precision == Other.Precision && AlternateForm == Other.AlternateForm; 68cfca06d7SDimitry Andric } 69cfca06d7SDimitry Andric 70cfca06d7SDimitry Andric bool operator!=(const ExpressionFormat &Other) const { 71cfca06d7SDimitry Andric return !(*this == Other); 72cfca06d7SDimitry Andric } 73cfca06d7SDimitry Andric 74cfca06d7SDimitry Andric bool operator==(Kind OtherValue) const { return Value == OtherValue; } 75cfca06d7SDimitry Andric 76cfca06d7SDimitry Andric bool operator!=(Kind OtherValue) const { return !(*this == OtherValue); } 77cfca06d7SDimitry Andric 78cfca06d7SDimitry Andric /// \returns the format specifier corresponding to this format as a string. 79cfca06d7SDimitry Andric StringRef toString() const; 80cfca06d7SDimitry Andric ExpressionFormatExpressionFormat81cfca06d7SDimitry Andric ExpressionFormat() : Value(Kind::NoFormat){}; ExpressionFormatExpressionFormat82b60736ecSDimitry Andric explicit ExpressionFormat(Kind Value) : Value(Value), Precision(0){}; ExpressionFormatExpressionFormat83b60736ecSDimitry Andric explicit ExpressionFormat(Kind Value, unsigned Precision) 84b60736ecSDimitry Andric : Value(Value), Precision(Precision){}; ExpressionFormatExpressionFormat85344a3780SDimitry Andric explicit ExpressionFormat(Kind Value, unsigned Precision, bool AlternateForm) 86344a3780SDimitry Andric : Value(Value), Precision(Precision), AlternateForm(AlternateForm){}; 87cfca06d7SDimitry Andric 88b60736ecSDimitry Andric /// \returns a wildcard regular expression string that matches any value in 89b60736ecSDimitry Andric /// the format represented by this instance and no other value, or an error 90b60736ecSDimitry Andric /// if the format is NoFormat. 91b60736ecSDimitry Andric Expected<std::string> getWildcardRegex() const; 92cfca06d7SDimitry Andric 93cfca06d7SDimitry Andric /// \returns the string representation of \p Value in the format represented 94cfca06d7SDimitry Andric /// by this instance, or an error if conversion to this format failed or the 95cfca06d7SDimitry Andric /// format is NoFormat. 96b1c73532SDimitry Andric Expected<std::string> getMatchingString(APInt Value) const; 97cfca06d7SDimitry Andric 98cfca06d7SDimitry Andric /// \returns the value corresponding to string representation \p StrVal 99b1c73532SDimitry Andric /// according to the matching format represented by this instance. 100b1c73532SDimitry Andric APInt valueFromStringRepr(StringRef StrVal, const SourceMgr &SM) const; 101cfca06d7SDimitry Andric }; 102cfca06d7SDimitry Andric 103cfca06d7SDimitry Andric /// Class to represent an overflow error that might result when manipulating a 104cfca06d7SDimitry Andric /// value. 105cfca06d7SDimitry Andric class OverflowError : public ErrorInfo<OverflowError> { 106cfca06d7SDimitry Andric public: 107cfca06d7SDimitry Andric static char ID; 108cfca06d7SDimitry Andric convertToErrorCode()109cfca06d7SDimitry Andric std::error_code convertToErrorCode() const override { 110cfca06d7SDimitry Andric return std::make_error_code(std::errc::value_too_large); 111cfca06d7SDimitry Andric } 112cfca06d7SDimitry Andric log(raw_ostream & OS)113cfca06d7SDimitry Andric void log(raw_ostream &OS) const override { OS << "overflow error"; } 114cfca06d7SDimitry Andric }; 115cfca06d7SDimitry Andric 116cfca06d7SDimitry Andric /// Performs operation and \returns its result or an error in case of failure, 117cfca06d7SDimitry Andric /// such as if an overflow occurs. 118b1c73532SDimitry Andric Expected<APInt> exprAdd(const APInt &Lhs, const APInt &Rhs, bool &Overflow); 119b1c73532SDimitry Andric Expected<APInt> exprSub(const APInt &Lhs, const APInt &Rhs, bool &Overflow); 120b1c73532SDimitry Andric Expected<APInt> exprMul(const APInt &Lhs, const APInt &Rhs, bool &Overflow); 121b1c73532SDimitry Andric Expected<APInt> exprDiv(const APInt &Lhs, const APInt &Rhs, bool &Overflow); 122b1c73532SDimitry Andric Expected<APInt> exprMax(const APInt &Lhs, const APInt &Rhs, bool &Overflow); 123b1c73532SDimitry Andric Expected<APInt> exprMin(const APInt &Lhs, const APInt &Rhs, bool &Overflow); 124cfca06d7SDimitry Andric 1251d5ae102SDimitry Andric /// Base class representing the AST of a given expression. 126706b4fc4SDimitry Andric class ExpressionAST { 127cfca06d7SDimitry Andric private: 128cfca06d7SDimitry Andric StringRef ExpressionStr; 129cfca06d7SDimitry Andric 1301d5ae102SDimitry Andric public: ExpressionAST(StringRef ExpressionStr)131cfca06d7SDimitry Andric ExpressionAST(StringRef ExpressionStr) : ExpressionStr(ExpressionStr) {} 132cfca06d7SDimitry Andric 133706b4fc4SDimitry Andric virtual ~ExpressionAST() = default; 1341d5ae102SDimitry Andric getExpressionStr()135cfca06d7SDimitry Andric StringRef getExpressionStr() const { return ExpressionStr; } 136cfca06d7SDimitry Andric 1371d5ae102SDimitry Andric /// Evaluates and \returns the value of the expression represented by this 1381d5ae102SDimitry Andric /// AST or an error if evaluation fails. 139b1c73532SDimitry Andric virtual Expected<APInt> eval() const = 0; 140cfca06d7SDimitry Andric 141cfca06d7SDimitry Andric /// \returns either the implicit format of this AST, a diagnostic against 142cfca06d7SDimitry Andric /// \p SM if implicit formats of the AST's components conflict, or NoFormat 143cfca06d7SDimitry Andric /// if the AST has no implicit format (e.g. AST is made up of a single 144cfca06d7SDimitry Andric /// literal). 145cfca06d7SDimitry Andric virtual Expected<ExpressionFormat> getImplicitFormat(const SourceMgr & SM)146cfca06d7SDimitry Andric getImplicitFormat(const SourceMgr &SM) const { 147cfca06d7SDimitry Andric return ExpressionFormat(); 148cfca06d7SDimitry Andric } 1491d5ae102SDimitry Andric }; 1501d5ae102SDimitry Andric 1511d5ae102SDimitry Andric /// Class representing an unsigned literal in the AST of an expression. 152706b4fc4SDimitry Andric class ExpressionLiteral : public ExpressionAST { 1531d5ae102SDimitry Andric private: 1541d5ae102SDimitry Andric /// Actual value of the literal. 155b1c73532SDimitry Andric APInt Value; 1561d5ae102SDimitry Andric 1571d5ae102SDimitry Andric public: ExpressionLiteral(StringRef ExpressionStr,APInt Val)158b1c73532SDimitry Andric explicit ExpressionLiteral(StringRef ExpressionStr, APInt Val) 159cfca06d7SDimitry Andric : ExpressionAST(ExpressionStr), Value(Val) {} 1601d5ae102SDimitry Andric 1611d5ae102SDimitry Andric /// \returns the literal's value. eval()162b1c73532SDimitry Andric Expected<APInt> eval() const override { return Value; } 1631d5ae102SDimitry Andric }; 1641d5ae102SDimitry Andric 1651d5ae102SDimitry Andric /// Class to represent an undefined variable error, which quotes that 1661d5ae102SDimitry Andric /// variable's name when printed. 167706b4fc4SDimitry Andric class UndefVarError : public ErrorInfo<UndefVarError> { 1681d5ae102SDimitry Andric private: 1691d5ae102SDimitry Andric StringRef VarName; 1701d5ae102SDimitry Andric 1711d5ae102SDimitry Andric public: 1721d5ae102SDimitry Andric static char ID; 1731d5ae102SDimitry Andric UndefVarError(StringRef VarName)174706b4fc4SDimitry Andric UndefVarError(StringRef VarName) : VarName(VarName) {} 1751d5ae102SDimitry Andric getVarName()1761d5ae102SDimitry Andric StringRef getVarName() const { return VarName; } 1771d5ae102SDimitry Andric convertToErrorCode()1781d5ae102SDimitry Andric std::error_code convertToErrorCode() const override { 1791d5ae102SDimitry Andric return inconvertibleErrorCode(); 1801d5ae102SDimitry Andric } 1811d5ae102SDimitry Andric 1821d5ae102SDimitry Andric /// Print name of variable associated with this error. log(raw_ostream & OS)1831d5ae102SDimitry Andric void log(raw_ostream &OS) const override { 184344a3780SDimitry Andric OS << "undefined variable: " << VarName; 1851d5ae102SDimitry Andric } 1861d5ae102SDimitry Andric }; 1871d5ae102SDimitry Andric 188cfca06d7SDimitry Andric /// Class representing an expression and its matching format. 189cfca06d7SDimitry Andric class Expression { 190cfca06d7SDimitry Andric private: 191cfca06d7SDimitry Andric /// Pointer to AST of the expression. 192cfca06d7SDimitry Andric std::unique_ptr<ExpressionAST> AST; 193cfca06d7SDimitry Andric 194cfca06d7SDimitry Andric /// Format to use (e.g. hex upper case letters) when matching the value. 195cfca06d7SDimitry Andric ExpressionFormat Format; 196cfca06d7SDimitry Andric 197cfca06d7SDimitry Andric public: 198cfca06d7SDimitry Andric /// Generic constructor for an expression represented by the given \p AST and 199cfca06d7SDimitry Andric /// whose matching format is \p Format. Expression(std::unique_ptr<ExpressionAST> AST,ExpressionFormat Format)200cfca06d7SDimitry Andric Expression(std::unique_ptr<ExpressionAST> AST, ExpressionFormat Format) 201cfca06d7SDimitry Andric : AST(std::move(AST)), Format(Format) {} 202cfca06d7SDimitry Andric 203cfca06d7SDimitry Andric /// \returns pointer to AST of the expression. Pointer is guaranteed to be 204cfca06d7SDimitry Andric /// valid as long as this object is. getAST()205cfca06d7SDimitry Andric ExpressionAST *getAST() const { return AST.get(); } 206cfca06d7SDimitry Andric getFormat()207cfca06d7SDimitry Andric ExpressionFormat getFormat() const { return Format; } 208cfca06d7SDimitry Andric }; 209cfca06d7SDimitry Andric 2101d5ae102SDimitry Andric /// Class representing a numeric variable and its associated current value. 211706b4fc4SDimitry Andric class NumericVariable { 2121d5ae102SDimitry Andric private: 2131d5ae102SDimitry Andric /// Name of the numeric variable. 2141d5ae102SDimitry Andric StringRef Name; 2151d5ae102SDimitry Andric 216cfca06d7SDimitry Andric /// Format to use for expressions using this variable without an explicit 217cfca06d7SDimitry Andric /// format. 218cfca06d7SDimitry Andric ExpressionFormat ImplicitFormat; 219cfca06d7SDimitry Andric 220e3b55780SDimitry Andric /// Value of numeric variable, if defined, or std::nullopt otherwise. 221b1c73532SDimitry Andric std::optional<APInt> Value; 2221d5ae102SDimitry Andric 223e3b55780SDimitry Andric /// The input buffer's string from which Value was parsed, or std::nullopt. 2247fa27ce4SDimitry Andric /// See comments on getStringValue for a discussion of the std::nullopt case. 225e3b55780SDimitry Andric std::optional<StringRef> StrValue; 226b60736ecSDimitry Andric 227e3b55780SDimitry Andric /// Line number where this variable is defined, or std::nullopt if defined 228e3b55780SDimitry Andric /// before input is parsed. Used to determine whether a variable is defined on 229e3b55780SDimitry Andric /// the same line as a given use. 230e3b55780SDimitry Andric std::optional<size_t> DefLineNumber; 2311d5ae102SDimitry Andric 2321d5ae102SDimitry Andric public: 233cfca06d7SDimitry Andric /// Constructor for a variable \p Name with implicit format \p ImplicitFormat 234cfca06d7SDimitry Andric /// defined at line \p DefLineNumber or defined before input is parsed if 2357fa27ce4SDimitry Andric /// \p DefLineNumber is std::nullopt. 236cfca06d7SDimitry Andric explicit NumericVariable(StringRef Name, ExpressionFormat ImplicitFormat, 237e3b55780SDimitry Andric std::optional<size_t> DefLineNumber = std::nullopt) Name(Name)238cfca06d7SDimitry Andric : Name(Name), ImplicitFormat(ImplicitFormat), 239cfca06d7SDimitry Andric DefLineNumber(DefLineNumber) {} 2401d5ae102SDimitry Andric 2411d5ae102SDimitry Andric /// \returns name of this numeric variable. getName()2421d5ae102SDimitry Andric StringRef getName() const { return Name; } 2431d5ae102SDimitry Andric 244cfca06d7SDimitry Andric /// \returns implicit format of this numeric variable. getImplicitFormat()245cfca06d7SDimitry Andric ExpressionFormat getImplicitFormat() const { return ImplicitFormat; } 246cfca06d7SDimitry Andric 2471d5ae102SDimitry Andric /// \returns this variable's value. getValue()248b1c73532SDimitry Andric std::optional<APInt> getValue() const { return Value; } 2491d5ae102SDimitry Andric 250b60736ecSDimitry Andric /// \returns the input buffer's string from which this variable's value was 251e3b55780SDimitry Andric /// parsed, or std::nullopt if the value is not yet defined or was not parsed 252e3b55780SDimitry Andric /// from the input buffer. For example, the value of @LINE is not parsed from 253e3b55780SDimitry Andric /// the input buffer, and some numeric variables are parsed from the command 254b60736ecSDimitry Andric /// line instead. getStringValue()255e3b55780SDimitry Andric std::optional<StringRef> getStringValue() const { return StrValue; } 256b60736ecSDimitry Andric 257b60736ecSDimitry Andric /// Sets value of this numeric variable to \p NewValue, and sets the input 258b60736ecSDimitry Andric /// buffer string from which it was parsed to \p NewStrValue. See comments on 2597fa27ce4SDimitry Andric /// getStringValue for a discussion of when the latter can be std::nullopt. 260b1c73532SDimitry Andric void setValue(APInt NewValue, 261e3b55780SDimitry Andric std::optional<StringRef> NewStrValue = std::nullopt) { 262b60736ecSDimitry Andric Value = NewValue; 263b60736ecSDimitry Andric StrValue = NewStrValue; 264b60736ecSDimitry Andric } 2651d5ae102SDimitry Andric 2661d5ae102SDimitry Andric /// Clears value of this numeric variable, regardless of whether it is 2671d5ae102SDimitry Andric /// currently defined or not. clearValue()268b60736ecSDimitry Andric void clearValue() { 269e3b55780SDimitry Andric Value = std::nullopt; 270e3b55780SDimitry Andric StrValue = std::nullopt; 271b60736ecSDimitry Andric } 2721d5ae102SDimitry Andric 273e3b55780SDimitry Andric /// \returns the line number where this variable is defined, if any, or 274e3b55780SDimitry Andric /// std::nullopt if defined before input is parsed. getDefLineNumber()275e3b55780SDimitry Andric std::optional<size_t> getDefLineNumber() const { return DefLineNumber; } 2761d5ae102SDimitry Andric }; 2771d5ae102SDimitry Andric 2781d5ae102SDimitry Andric /// Class representing the use of a numeric variable in the AST of an 2791d5ae102SDimitry Andric /// expression. 280706b4fc4SDimitry Andric class NumericVariableUse : public ExpressionAST { 2811d5ae102SDimitry Andric private: 2821d5ae102SDimitry Andric /// Pointer to the class instance for the variable this use is about. 283706b4fc4SDimitry Andric NumericVariable *Variable; 2841d5ae102SDimitry Andric 2851d5ae102SDimitry Andric public: NumericVariableUse(StringRef Name,NumericVariable * Variable)286706b4fc4SDimitry Andric NumericVariableUse(StringRef Name, NumericVariable *Variable) 287cfca06d7SDimitry Andric : ExpressionAST(Name), Variable(Variable) {} 2881d5ae102SDimitry Andric /// \returns the value of the variable referenced by this instance. 289b1c73532SDimitry Andric Expected<APInt> eval() const override; 290cfca06d7SDimitry Andric 291cfca06d7SDimitry Andric /// \returns implicit format of this numeric variable. 292cfca06d7SDimitry Andric Expected<ExpressionFormat> getImplicitFormat(const SourceMgr & SM)293cfca06d7SDimitry Andric getImplicitFormat(const SourceMgr &SM) const override { 294cfca06d7SDimitry Andric return Variable->getImplicitFormat(); 295cfca06d7SDimitry Andric } 2961d5ae102SDimitry Andric }; 2971d5ae102SDimitry Andric 2981d5ae102SDimitry Andric /// Type of functions evaluating a given binary operation. 299b1c73532SDimitry Andric using binop_eval_t = Expected<APInt> (*)(const APInt &, const APInt &, bool &); 3001d5ae102SDimitry Andric 3011d5ae102SDimitry Andric /// Class representing a single binary operation in the AST of an expression. 302706b4fc4SDimitry Andric class BinaryOperation : public ExpressionAST { 3031d5ae102SDimitry Andric private: 3041d5ae102SDimitry Andric /// Left operand. 305706b4fc4SDimitry Andric std::unique_ptr<ExpressionAST> LeftOperand; 3061d5ae102SDimitry Andric 3071d5ae102SDimitry Andric /// Right operand. 308706b4fc4SDimitry Andric std::unique_ptr<ExpressionAST> RightOperand; 3091d5ae102SDimitry Andric 3101d5ae102SDimitry Andric /// Pointer to function that can evaluate this binary operation. 3111d5ae102SDimitry Andric binop_eval_t EvalBinop; 3121d5ae102SDimitry Andric 3131d5ae102SDimitry Andric public: BinaryOperation(StringRef ExpressionStr,binop_eval_t EvalBinop,std::unique_ptr<ExpressionAST> LeftOp,std::unique_ptr<ExpressionAST> RightOp)314cfca06d7SDimitry Andric BinaryOperation(StringRef ExpressionStr, binop_eval_t EvalBinop, 315cfca06d7SDimitry Andric std::unique_ptr<ExpressionAST> LeftOp, 316706b4fc4SDimitry Andric std::unique_ptr<ExpressionAST> RightOp) 317cfca06d7SDimitry Andric : ExpressionAST(ExpressionStr), EvalBinop(EvalBinop) { 3181d5ae102SDimitry Andric LeftOperand = std::move(LeftOp); 3191d5ae102SDimitry Andric RightOperand = std::move(RightOp); 3201d5ae102SDimitry Andric } 3211d5ae102SDimitry Andric 3221d5ae102SDimitry Andric /// Evaluates the value of the binary operation represented by this AST, 3231d5ae102SDimitry Andric /// using EvalBinop on the result of recursively evaluating the operands. 3241d5ae102SDimitry Andric /// \returns the expression value or an error if an undefined numeric 3251d5ae102SDimitry Andric /// variable is used in one of the operands. 326b1c73532SDimitry Andric Expected<APInt> eval() const override; 327cfca06d7SDimitry Andric 328cfca06d7SDimitry Andric /// \returns the implicit format of this AST, if any, a diagnostic against 329cfca06d7SDimitry Andric /// \p SM if the implicit formats of the AST's components conflict, or no 330cfca06d7SDimitry Andric /// format if the AST has no implicit format (e.g. AST is made of a single 331cfca06d7SDimitry Andric /// literal). 332cfca06d7SDimitry Andric Expected<ExpressionFormat> 333cfca06d7SDimitry Andric getImplicitFormat(const SourceMgr &SM) const override; 3341d5ae102SDimitry Andric }; 3351d5ae102SDimitry Andric 3361d5ae102SDimitry Andric class FileCheckPatternContext; 3371d5ae102SDimitry Andric 3381d5ae102SDimitry Andric /// Class representing a substitution to perform in the RegExStr string. 339706b4fc4SDimitry Andric class Substitution { 3401d5ae102SDimitry Andric protected: 3411d5ae102SDimitry Andric /// Pointer to a class instance holding, among other things, the table with 3421d5ae102SDimitry Andric /// the values of live string variables at the start of any given CHECK line. 3431d5ae102SDimitry Andric /// Used for substituting string variables with the text they were defined 3441d5ae102SDimitry Andric /// as. Expressions are linked to the numeric variables they use at 3451d5ae102SDimitry Andric /// parse time and directly access the value of the numeric variable to 3461d5ae102SDimitry Andric /// evaluate their value. 3471d5ae102SDimitry Andric FileCheckPatternContext *Context; 3481d5ae102SDimitry Andric 3491d5ae102SDimitry Andric /// The string that needs to be substituted for something else. For a 3501d5ae102SDimitry Andric /// string variable this is its name, otherwise this is the whole expression. 3511d5ae102SDimitry Andric StringRef FromStr; 3521d5ae102SDimitry Andric 3531d5ae102SDimitry Andric // Index in RegExStr of where to do the substitution. 3541d5ae102SDimitry Andric size_t InsertIdx; 3551d5ae102SDimitry Andric 3561d5ae102SDimitry Andric public: Substitution(FileCheckPatternContext * Context,StringRef VarName,size_t InsertIdx)357706b4fc4SDimitry Andric Substitution(FileCheckPatternContext *Context, StringRef VarName, 3581d5ae102SDimitry Andric size_t InsertIdx) 3591d5ae102SDimitry Andric : Context(Context), FromStr(VarName), InsertIdx(InsertIdx) {} 3601d5ae102SDimitry Andric 361706b4fc4SDimitry Andric virtual ~Substitution() = default; 3621d5ae102SDimitry Andric 3631d5ae102SDimitry Andric /// \returns the string to be substituted for something else. getFromString()3641d5ae102SDimitry Andric StringRef getFromString() const { return FromStr; } 3651d5ae102SDimitry Andric 3661d5ae102SDimitry Andric /// \returns the index where the substitution is to be performed in RegExStr. getIndex()3671d5ae102SDimitry Andric size_t getIndex() const { return InsertIdx; } 3681d5ae102SDimitry Andric 3691d5ae102SDimitry Andric /// \returns a string containing the result of the substitution represented 3701d5ae102SDimitry Andric /// by this class instance or an error if substitution failed. 3711d5ae102SDimitry Andric virtual Expected<std::string> getResult() const = 0; 3721d5ae102SDimitry Andric }; 3731d5ae102SDimitry Andric 374706b4fc4SDimitry Andric class StringSubstitution : public Substitution { 3751d5ae102SDimitry Andric public: StringSubstitution(FileCheckPatternContext * Context,StringRef VarName,size_t InsertIdx)376706b4fc4SDimitry Andric StringSubstitution(FileCheckPatternContext *Context, StringRef VarName, 377706b4fc4SDimitry Andric size_t InsertIdx) 378706b4fc4SDimitry Andric : Substitution(Context, VarName, InsertIdx) {} 3791d5ae102SDimitry Andric 3801d5ae102SDimitry Andric /// \returns the text that the string variable in this substitution matched 3811d5ae102SDimitry Andric /// when defined, or an error if the variable is undefined. 3821d5ae102SDimitry Andric Expected<std::string> getResult() const override; 3831d5ae102SDimitry Andric }; 3841d5ae102SDimitry Andric 385706b4fc4SDimitry Andric class NumericSubstitution : public Substitution { 3861d5ae102SDimitry Andric private: 3871d5ae102SDimitry Andric /// Pointer to the class representing the expression whose value is to be 3881d5ae102SDimitry Andric /// substituted. 389cfca06d7SDimitry Andric std::unique_ptr<Expression> ExpressionPointer; 3901d5ae102SDimitry Andric 3911d5ae102SDimitry Andric public: NumericSubstitution(FileCheckPatternContext * Context,StringRef ExpressionStr,std::unique_ptr<Expression> ExpressionPointer,size_t InsertIdx)392cfca06d7SDimitry Andric NumericSubstitution(FileCheckPatternContext *Context, StringRef ExpressionStr, 393cfca06d7SDimitry Andric std::unique_ptr<Expression> ExpressionPointer, 394cfca06d7SDimitry Andric size_t InsertIdx) 395cfca06d7SDimitry Andric : Substitution(Context, ExpressionStr, InsertIdx), 396cfca06d7SDimitry Andric ExpressionPointer(std::move(ExpressionPointer)) {} 3971d5ae102SDimitry Andric 3981d5ae102SDimitry Andric /// \returns a string containing the result of evaluating the expression in 3991d5ae102SDimitry Andric /// this substitution, or an error if evaluation failed. 4001d5ae102SDimitry Andric Expected<std::string> getResult() const override; 4011d5ae102SDimitry Andric }; 4021d5ae102SDimitry Andric 4031d5ae102SDimitry Andric //===----------------------------------------------------------------------===// 4041d5ae102SDimitry Andric // Pattern handling code. 4051d5ae102SDimitry Andric //===----------------------------------------------------------------------===// 4061d5ae102SDimitry Andric 407706b4fc4SDimitry Andric /// Class holding the Pattern global state, shared by all patterns: tables 408706b4fc4SDimitry Andric /// holding values of variables and whether they are defined or not at any 409706b4fc4SDimitry Andric /// given time in the matching process. 4101d5ae102SDimitry Andric class FileCheckPatternContext { 411706b4fc4SDimitry Andric friend class Pattern; 4121d5ae102SDimitry Andric 4131d5ae102SDimitry Andric private: 4141d5ae102SDimitry Andric /// When matching a given pattern, this holds the value of all the string 4151d5ae102SDimitry Andric /// variables defined in previous patterns. In a pattern, only the last 4161d5ae102SDimitry Andric /// definition for a given variable is recorded in this table. 4171d5ae102SDimitry Andric /// Back-references are used for uses after any the other definition. 4181d5ae102SDimitry Andric StringMap<StringRef> GlobalVariableTable; 4191d5ae102SDimitry Andric 4201d5ae102SDimitry Andric /// Map of all string variables defined so far. Used at parse time to detect 4211d5ae102SDimitry Andric /// a name conflict between a numeric variable and a string variable when 4221d5ae102SDimitry Andric /// the former is defined on a later line than the latter. 4231d5ae102SDimitry Andric StringMap<bool> DefinedVariableTable; 4241d5ae102SDimitry Andric 4251d5ae102SDimitry Andric /// When matching a given pattern, this holds the pointers to the classes 4261d5ae102SDimitry Andric /// representing the numeric variables defined in previous patterns. When 4271d5ae102SDimitry Andric /// matching a pattern all definitions for that pattern are recorded in the 428706b4fc4SDimitry Andric /// NumericVariableDefs table in the Pattern instance of that pattern. 429706b4fc4SDimitry Andric StringMap<NumericVariable *> GlobalNumericVariableTable; 4301d5ae102SDimitry Andric 4311d5ae102SDimitry Andric /// Pointer to the class instance representing the @LINE pseudo variable for 4321d5ae102SDimitry Andric /// easily updating its value. 433706b4fc4SDimitry Andric NumericVariable *LineVariable = nullptr; 4341d5ae102SDimitry Andric 4351d5ae102SDimitry Andric /// Vector holding pointers to all parsed numeric variables. Used to 4361d5ae102SDimitry Andric /// automatically free them once they are guaranteed to no longer be used. 437706b4fc4SDimitry Andric std::vector<std::unique_ptr<NumericVariable>> NumericVariables; 4381d5ae102SDimitry Andric 439cfca06d7SDimitry Andric /// Vector holding pointers to all parsed expressions. Used to automatically 440cfca06d7SDimitry Andric /// free the expressions once they are guaranteed to no longer be used. 441cfca06d7SDimitry Andric std::vector<std::unique_ptr<Expression>> Expressions; 442cfca06d7SDimitry Andric 4431d5ae102SDimitry Andric /// Vector holding pointers to all substitutions. Used to automatically free 4441d5ae102SDimitry Andric /// them once they are guaranteed to no longer be used. 445706b4fc4SDimitry Andric std::vector<std::unique_ptr<Substitution>> Substitutions; 4461d5ae102SDimitry Andric 4471d5ae102SDimitry Andric public: 4481d5ae102SDimitry Andric /// \returns the value of string variable \p VarName or an error if no such 4491d5ae102SDimitry Andric /// variable has been defined. 4501d5ae102SDimitry Andric Expected<StringRef> getPatternVarValue(StringRef VarName); 4511d5ae102SDimitry Andric 4521d5ae102SDimitry Andric /// Defines string and numeric variables from definitions given on the 4531d5ae102SDimitry Andric /// command line, passed as a vector of [#]VAR=VAL strings in 4541d5ae102SDimitry Andric /// \p CmdlineDefines. \returns an error list containing diagnostics against 4551d5ae102SDimitry Andric /// \p SM for all definition parsing failures, if any, or Success otherwise. 456cfca06d7SDimitry Andric Error defineCmdlineVariables(ArrayRef<StringRef> CmdlineDefines, 4571d5ae102SDimitry Andric SourceMgr &SM); 4581d5ae102SDimitry Andric 4591d5ae102SDimitry Andric /// Create @LINE pseudo variable. Value is set when pattern are being 4601d5ae102SDimitry Andric /// matched. 4611d5ae102SDimitry Andric void createLineVariable(); 4621d5ae102SDimitry Andric 4631d5ae102SDimitry Andric /// Undefines local variables (variables whose name does not start with a '$' 4641d5ae102SDimitry Andric /// sign), i.e. removes them from GlobalVariableTable and from 4651d5ae102SDimitry Andric /// GlobalNumericVariableTable and also clears the value of numeric 4661d5ae102SDimitry Andric /// variables. 4671d5ae102SDimitry Andric void clearLocalVars(); 4681d5ae102SDimitry Andric 4691d5ae102SDimitry Andric private: 4701d5ae102SDimitry Andric /// Makes a new numeric variable and registers it for destruction when the 4711d5ae102SDimitry Andric /// context is destroyed. 472706b4fc4SDimitry Andric template <class... Types> NumericVariable *makeNumericVariable(Types... args); 4731d5ae102SDimitry Andric 4741d5ae102SDimitry Andric /// Makes a new string substitution and registers it for destruction when the 4751d5ae102SDimitry Andric /// context is destroyed. 476706b4fc4SDimitry Andric Substitution *makeStringSubstitution(StringRef VarName, size_t InsertIdx); 4771d5ae102SDimitry Andric 4781d5ae102SDimitry Andric /// Makes a new numeric substitution and registers it for destruction when 4791d5ae102SDimitry Andric /// the context is destroyed. 480cfca06d7SDimitry Andric Substitution *makeNumericSubstitution(StringRef ExpressionStr, 481cfca06d7SDimitry Andric std::unique_ptr<Expression> Expression, 4821d5ae102SDimitry Andric size_t InsertIdx); 4831d5ae102SDimitry Andric }; 4841d5ae102SDimitry Andric 4851d5ae102SDimitry Andric /// Class to represent an error holding a diagnostic with location information 4861d5ae102SDimitry Andric /// used when printing it. 487706b4fc4SDimitry Andric class ErrorDiagnostic : public ErrorInfo<ErrorDiagnostic> { 4881d5ae102SDimitry Andric private: 4891d5ae102SDimitry Andric SMDiagnostic Diagnostic; 490344a3780SDimitry Andric SMRange Range; 4911d5ae102SDimitry Andric 4921d5ae102SDimitry Andric public: 4931d5ae102SDimitry Andric static char ID; 4941d5ae102SDimitry Andric ErrorDiagnostic(SMDiagnostic && Diag,SMRange Range)495344a3780SDimitry Andric ErrorDiagnostic(SMDiagnostic &&Diag, SMRange Range) 496344a3780SDimitry Andric : Diagnostic(Diag), Range(Range) {} 4971d5ae102SDimitry Andric convertToErrorCode()4981d5ae102SDimitry Andric std::error_code convertToErrorCode() const override { 4991d5ae102SDimitry Andric return inconvertibleErrorCode(); 5001d5ae102SDimitry Andric } 5011d5ae102SDimitry Andric 5021d5ae102SDimitry Andric /// Print diagnostic associated with this error when printing the error. log(raw_ostream & OS)5031d5ae102SDimitry Andric void log(raw_ostream &OS) const override { Diagnostic.print(nullptr, OS); } 5041d5ae102SDimitry Andric getMessage()505344a3780SDimitry Andric StringRef getMessage() const { return Diagnostic.getMessage(); } getRange()506344a3780SDimitry Andric SMRange getRange() const { return Range; } 507344a3780SDimitry Andric 508344a3780SDimitry Andric static Error get(const SourceMgr &SM, SMLoc Loc, const Twine &ErrMsg, 509e3b55780SDimitry Andric SMRange Range = std::nullopt) { 510706b4fc4SDimitry Andric return make_error<ErrorDiagnostic>( 511344a3780SDimitry Andric SM.GetMessage(Loc, SourceMgr::DK_Error, ErrMsg), Range); 5121d5ae102SDimitry Andric } 5131d5ae102SDimitry Andric get(const SourceMgr & SM,StringRef Buffer,const Twine & ErrMsg)5141d5ae102SDimitry Andric static Error get(const SourceMgr &SM, StringRef Buffer, const Twine &ErrMsg) { 515344a3780SDimitry Andric SMLoc Start = SMLoc::getFromPointer(Buffer.data()); 516344a3780SDimitry Andric SMLoc End = SMLoc::getFromPointer(Buffer.data() + Buffer.size()); 517344a3780SDimitry Andric return get(SM, Start, ErrMsg, SMRange(Start, End)); 5181d5ae102SDimitry Andric } 5191d5ae102SDimitry Andric }; 5201d5ae102SDimitry Andric 521706b4fc4SDimitry Andric class NotFoundError : public ErrorInfo<NotFoundError> { 5221d5ae102SDimitry Andric public: 5231d5ae102SDimitry Andric static char ID; 5241d5ae102SDimitry Andric convertToErrorCode()5251d5ae102SDimitry Andric std::error_code convertToErrorCode() const override { 5261d5ae102SDimitry Andric return inconvertibleErrorCode(); 5271d5ae102SDimitry Andric } 5281d5ae102SDimitry Andric 5291d5ae102SDimitry Andric /// Print diagnostic associated with this error when printing the error. log(raw_ostream & OS)5301d5ae102SDimitry Andric void log(raw_ostream &OS) const override { 5311d5ae102SDimitry Andric OS << "String not found in input"; 5321d5ae102SDimitry Andric } 5331d5ae102SDimitry Andric }; 5341d5ae102SDimitry Andric 535344a3780SDimitry Andric /// An error that has already been reported. 536344a3780SDimitry Andric /// 537344a3780SDimitry Andric /// This class is designed to support a function whose callers may need to know 538344a3780SDimitry Andric /// whether the function encountered and reported an error but never need to 539344a3780SDimitry Andric /// know the nature of that error. For example, the function has a return type 540344a3780SDimitry Andric /// of \c Error and always returns either \c ErrorReported or \c ErrorSuccess. 541344a3780SDimitry Andric /// That interface is similar to that of a function returning bool to indicate 542344a3780SDimitry Andric /// an error except, in the former case, (1) there is no confusion over polarity 543344a3780SDimitry Andric /// and (2) the caller must either check the result or explicitly ignore it with 544344a3780SDimitry Andric /// a call like \c consumeError. 545344a3780SDimitry Andric class ErrorReported final : public ErrorInfo<ErrorReported> { 546344a3780SDimitry Andric public: 547344a3780SDimitry Andric static char ID; 548344a3780SDimitry Andric convertToErrorCode()549344a3780SDimitry Andric std::error_code convertToErrorCode() const override { 550344a3780SDimitry Andric return inconvertibleErrorCode(); 551344a3780SDimitry Andric } 552344a3780SDimitry Andric 553344a3780SDimitry Andric /// Print diagnostic associated with this error when printing the error. log(raw_ostream & OS)554344a3780SDimitry Andric void log(raw_ostream &OS) const override { 555344a3780SDimitry Andric OS << "error previously reported"; 556344a3780SDimitry Andric } 557344a3780SDimitry Andric reportedOrSuccess(bool HasErrorReported)558344a3780SDimitry Andric static inline Error reportedOrSuccess(bool HasErrorReported) { 559344a3780SDimitry Andric if (HasErrorReported) 560344a3780SDimitry Andric return make_error<ErrorReported>(); 561344a3780SDimitry Andric return Error::success(); 562344a3780SDimitry Andric } 563344a3780SDimitry Andric }; 564344a3780SDimitry Andric 565706b4fc4SDimitry Andric class Pattern { 5661d5ae102SDimitry Andric SMLoc PatternLoc; 5671d5ae102SDimitry Andric 5681d5ae102SDimitry Andric /// A fixed string to match as the pattern or empty if this pattern requires 5691d5ae102SDimitry Andric /// a regex match. 5701d5ae102SDimitry Andric StringRef FixedStr; 5711d5ae102SDimitry Andric 5721d5ae102SDimitry Andric /// A regex string to match as the pattern or empty if this pattern requires 5731d5ae102SDimitry Andric /// a fixed string to match. 5741d5ae102SDimitry Andric std::string RegExStr; 5751d5ae102SDimitry Andric 5761d5ae102SDimitry Andric /// Entries in this vector represent a substitution of a string variable or 5771d5ae102SDimitry Andric /// an expression in the RegExStr regex at match time. For example, in the 5781d5ae102SDimitry Andric /// case of a CHECK directive with the pattern "foo[[bar]]baz[[#N+1]]", 5791d5ae102SDimitry Andric /// RegExStr will contain "foobaz" and we'll get two entries in this vector 5801d5ae102SDimitry Andric /// that tells us to insert the value of string variable "bar" at offset 3 5811d5ae102SDimitry Andric /// and the value of expression "N+1" at offset 6. 582706b4fc4SDimitry Andric std::vector<Substitution *> Substitutions; 5831d5ae102SDimitry Andric 5841d5ae102SDimitry Andric /// Maps names of string variables defined in a pattern to the number of 5851d5ae102SDimitry Andric /// their parenthesis group in RegExStr capturing their last definition. 5861d5ae102SDimitry Andric /// 5871d5ae102SDimitry Andric /// E.g. for the pattern "foo[[bar:.*]]baz([[bar]][[QUUX]][[bar:.*]])", 5881d5ae102SDimitry Andric /// RegExStr will be "foo(.*)baz(\1<quux value>(.*))" where <quux value> is 5891d5ae102SDimitry Andric /// the value captured for QUUX on the earlier line where it was defined, and 5901d5ae102SDimitry Andric /// VariableDefs will map "bar" to the third parenthesis group which captures 5911d5ae102SDimitry Andric /// the second definition of "bar". 5921d5ae102SDimitry Andric /// 5931d5ae102SDimitry Andric /// Note: uses std::map rather than StringMap to be able to get the key when 5941d5ae102SDimitry Andric /// iterating over values. 5951d5ae102SDimitry Andric std::map<StringRef, unsigned> VariableDefs; 5961d5ae102SDimitry Andric 5971d5ae102SDimitry Andric /// Structure representing the definition of a numeric variable in a pattern. 598cfca06d7SDimitry Andric /// It holds the pointer to the class instance holding the value and matching 599cfca06d7SDimitry Andric /// format of the numeric variable whose value is being defined and the 600cfca06d7SDimitry Andric /// number of the parenthesis group in RegExStr to capture that value. 601706b4fc4SDimitry Andric struct NumericVariableMatch { 602cfca06d7SDimitry Andric /// Pointer to class instance holding the value and matching format of the 603cfca06d7SDimitry Andric /// numeric variable being defined. 604706b4fc4SDimitry Andric NumericVariable *DefinedNumericVariable; 6051d5ae102SDimitry Andric 6061d5ae102SDimitry Andric /// Number of the parenthesis group in RegExStr that captures the value of 6071d5ae102SDimitry Andric /// this numeric variable definition. 6081d5ae102SDimitry Andric unsigned CaptureParenGroup; 6091d5ae102SDimitry Andric }; 6101d5ae102SDimitry Andric 6111d5ae102SDimitry Andric /// Holds the number of the parenthesis group in RegExStr and pointer to the 612706b4fc4SDimitry Andric /// corresponding NumericVariable class instance of all numeric variable 613706b4fc4SDimitry Andric /// definitions. Used to set the matched value of all those variables. 614706b4fc4SDimitry Andric StringMap<NumericVariableMatch> NumericVariableDefs; 6151d5ae102SDimitry Andric 6161d5ae102SDimitry Andric /// Pointer to a class instance holding the global state shared by all 6171d5ae102SDimitry Andric /// patterns: 6181d5ae102SDimitry Andric /// - separate tables with the values of live string and numeric variables 6191d5ae102SDimitry Andric /// respectively at the start of any given CHECK line; 6201d5ae102SDimitry Andric /// - table holding whether a string variable has been defined at any given 6211d5ae102SDimitry Andric /// point during the parsing phase. 6221d5ae102SDimitry Andric FileCheckPatternContext *Context; 6231d5ae102SDimitry Andric 6241d5ae102SDimitry Andric Check::FileCheckType CheckTy; 6251d5ae102SDimitry Andric 626e3b55780SDimitry Andric /// Line number for this CHECK pattern or std::nullopt if it is an implicit 627e3b55780SDimitry Andric /// pattern. Used to determine whether a variable definition is made on an 628e3b55780SDimitry Andric /// earlier line to the one with this CHECK. 629e3b55780SDimitry Andric std::optional<size_t> LineNumber; 6301d5ae102SDimitry Andric 6311d5ae102SDimitry Andric /// Ignore case while matching if set to true. 6321d5ae102SDimitry Andric bool IgnoreCase = false; 6331d5ae102SDimitry Andric 6341d5ae102SDimitry Andric public: 635706b4fc4SDimitry Andric Pattern(Check::FileCheckType Ty, FileCheckPatternContext *Context, 636e3b55780SDimitry Andric std::optional<size_t> Line = std::nullopt) Context(Context)6371d5ae102SDimitry Andric : Context(Context), CheckTy(Ty), LineNumber(Line) {} 6381d5ae102SDimitry Andric 6391d5ae102SDimitry Andric /// \returns the location in source code. getLoc()6401d5ae102SDimitry Andric SMLoc getLoc() const { return PatternLoc; } 6411d5ae102SDimitry Andric 6421d5ae102SDimitry Andric /// \returns the pointer to the global state for all patterns in this 6431d5ae102SDimitry Andric /// FileCheck instance. getContext()6441d5ae102SDimitry Andric FileCheckPatternContext *getContext() const { return Context; } 6451d5ae102SDimitry Andric 6461d5ae102SDimitry Andric /// \returns whether \p C is a valid first character for a variable name. 6471d5ae102SDimitry Andric static bool isValidVarNameStart(char C); 6481d5ae102SDimitry Andric 6491d5ae102SDimitry Andric /// Parsing information about a variable. 6501d5ae102SDimitry Andric struct VariableProperties { 6511d5ae102SDimitry Andric StringRef Name; 6521d5ae102SDimitry Andric bool IsPseudo; 6531d5ae102SDimitry Andric }; 6541d5ae102SDimitry Andric 6551d5ae102SDimitry Andric /// Parses the string at the start of \p Str for a variable name. \returns 6561d5ae102SDimitry Andric /// a VariableProperties structure holding the variable name and whether it 6571d5ae102SDimitry Andric /// is the name of a pseudo variable, or an error holding a diagnostic 6581d5ae102SDimitry Andric /// against \p SM if parsing fail. If parsing was successful, also strips 6591d5ae102SDimitry Andric /// \p Str from the variable name. 6601d5ae102SDimitry Andric static Expected<VariableProperties> parseVariable(StringRef &Str, 6611d5ae102SDimitry Andric const SourceMgr &SM); 6621d5ae102SDimitry Andric /// Parses \p Expr for a numeric substitution block at line \p LineNumber, 6631d5ae102SDimitry Andric /// or before input is parsed if \p LineNumber is None. Parameter 6641d5ae102SDimitry Andric /// \p IsLegacyLineExpr indicates whether \p Expr should be a legacy @LINE 6651d5ae102SDimitry Andric /// expression and \p Context points to the class instance holding the live 6661d5ae102SDimitry Andric /// string and numeric variables. \returns a pointer to the class instance 667cfca06d7SDimitry Andric /// representing the expression whose value must be substitued, or an error 668cfca06d7SDimitry Andric /// holding a diagnostic against \p SM if parsing fails. If substitution was 669cfca06d7SDimitry Andric /// successful, sets \p DefinedNumericVariable to point to the class 670cfca06d7SDimitry Andric /// representing the numeric variable defined in this numeric substitution 671e3b55780SDimitry Andric /// block, or std::nullopt if this block does not define any variable. 672cfca06d7SDimitry Andric static Expected<std::unique_ptr<Expression>> parseNumericSubstitutionBlock( 673e3b55780SDimitry Andric StringRef Expr, std::optional<NumericVariable *> &DefinedNumericVariable, 674e3b55780SDimitry Andric bool IsLegacyLineExpr, std::optional<size_t> LineNumber, 6751d5ae102SDimitry Andric FileCheckPatternContext *Context, const SourceMgr &SM); 676706b4fc4SDimitry Andric /// Parses the pattern in \p PatternStr and initializes this Pattern instance 677706b4fc4SDimitry Andric /// accordingly. 6781d5ae102SDimitry Andric /// 6791d5ae102SDimitry Andric /// \p Prefix provides which prefix is being matched, \p Req describes the 6801d5ae102SDimitry Andric /// global options that influence the parsing such as whitespace 6811d5ae102SDimitry Andric /// canonicalization, \p SM provides the SourceMgr used for error reports. 6821d5ae102SDimitry Andric /// \returns true in case of an error, false otherwise. 6831d5ae102SDimitry Andric bool parsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM, 6841d5ae102SDimitry Andric const FileCheckRequest &Req); 685344a3780SDimitry Andric struct Match { 686344a3780SDimitry Andric size_t Pos; 687344a3780SDimitry Andric size_t Len; 688344a3780SDimitry Andric }; 689344a3780SDimitry Andric struct MatchResult { 690e3b55780SDimitry Andric std::optional<Match> TheMatch; 691344a3780SDimitry Andric Error TheError; MatchResultMatchResult692344a3780SDimitry Andric MatchResult(size_t MatchPos, size_t MatchLen, Error E) 693344a3780SDimitry Andric : TheMatch(Match{MatchPos, MatchLen}), TheError(std::move(E)) {} MatchResultMatchResult694344a3780SDimitry Andric MatchResult(Match M, Error E) : TheMatch(M), TheError(std::move(E)) {} MatchResultMatchResult695344a3780SDimitry Andric MatchResult(Error E) : TheError(std::move(E)) {} 696344a3780SDimitry Andric }; 697344a3780SDimitry Andric /// Matches the pattern string against the input buffer \p Buffer. 6981d5ae102SDimitry Andric /// 699344a3780SDimitry Andric /// \returns either (1) an error resulting in no match or (2) a match possibly 700344a3780SDimitry Andric /// with an error encountered while processing the match. 7011d5ae102SDimitry Andric /// 7021d5ae102SDimitry Andric /// The GlobalVariableTable StringMap in the FileCheckPatternContext class 703706b4fc4SDimitry Andric /// instance provides the current values of FileCheck string variables and is 704706b4fc4SDimitry Andric /// updated if this match defines new values. Likewise, the 7051d5ae102SDimitry Andric /// GlobalNumericVariableTable StringMap in the same class provides the 7061d5ae102SDimitry Andric /// current values of FileCheck numeric variables and is updated if this 7071d5ae102SDimitry Andric /// match defines new numeric values. 708344a3780SDimitry Andric MatchResult match(StringRef Buffer, const SourceMgr &SM) const; 709344a3780SDimitry Andric /// Prints the value of successful substitutions. 7101d5ae102SDimitry Andric void printSubstitutions(const SourceMgr &SM, StringRef Buffer, 711b60736ecSDimitry Andric SMRange MatchRange, FileCheckDiag::MatchType MatchTy, 712b60736ecSDimitry Andric std::vector<FileCheckDiag> *Diags) const; 7131d5ae102SDimitry Andric void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer, 7141d5ae102SDimitry Andric std::vector<FileCheckDiag> *Diags) const; 7151d5ae102SDimitry Andric hasVariable()7161d5ae102SDimitry Andric bool hasVariable() const { 7171d5ae102SDimitry Andric return !(Substitutions.empty() && VariableDefs.empty()); 7181d5ae102SDimitry Andric } 719b60736ecSDimitry Andric void printVariableDefs(const SourceMgr &SM, FileCheckDiag::MatchType MatchTy, 720b60736ecSDimitry Andric std::vector<FileCheckDiag> *Diags) const; 7211d5ae102SDimitry Andric getCheckTy()7221d5ae102SDimitry Andric Check::FileCheckType getCheckTy() const { return CheckTy; } 7231d5ae102SDimitry Andric getCount()7241d5ae102SDimitry Andric int getCount() const { return CheckTy.getCount(); } 7251d5ae102SDimitry Andric 7261d5ae102SDimitry Andric private: 7271d5ae102SDimitry Andric bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM); 7281d5ae102SDimitry Andric void AddBackrefToRegEx(unsigned BackrefNum); 7291d5ae102SDimitry Andric /// Computes an arbitrary estimate for the quality of matching this pattern 7301d5ae102SDimitry Andric /// at the start of \p Buffer; a distance of zero should correspond to a 7311d5ae102SDimitry Andric /// perfect match. 7321d5ae102SDimitry Andric unsigned computeMatchDistance(StringRef Buffer) const; 7331d5ae102SDimitry Andric /// Finds the closing sequence of a regex variable usage or definition. 7341d5ae102SDimitry Andric /// 7351d5ae102SDimitry Andric /// \p Str has to point in the beginning of the definition (right after the 736706b4fc4SDimitry Andric /// opening sequence). \p SM holds the SourceMgr used for error reporting. 7371d5ae102SDimitry Andric /// \returns the offset of the closing sequence within Str, or npos if it 7381d5ae102SDimitry Andric /// was not found. 739706b4fc4SDimitry Andric static size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM); 7401d5ae102SDimitry Andric 7411d5ae102SDimitry Andric /// Parses \p Expr for the name of a numeric variable to be defined at line 7421d5ae102SDimitry Andric /// \p LineNumber, or before input is parsed if \p LineNumber is None. 7431d5ae102SDimitry Andric /// \returns a pointer to the class instance representing that variable, 7441d5ae102SDimitry Andric /// creating it if needed, or an error holding a diagnostic against \p SM 7451d5ae102SDimitry Andric /// should defining such a variable be invalid. 746706b4fc4SDimitry Andric static Expected<NumericVariable *> parseNumericVariableDefinition( 7471d5ae102SDimitry Andric StringRef &Expr, FileCheckPatternContext *Context, 748e3b55780SDimitry Andric std::optional<size_t> LineNumber, ExpressionFormat ImplicitFormat, 749cfca06d7SDimitry Andric const SourceMgr &SM); 7501d5ae102SDimitry Andric /// Parses \p Name as a (pseudo if \p IsPseudo is true) numeric variable use 7511d5ae102SDimitry Andric /// at line \p LineNumber, or before input is parsed if \p LineNumber is 7521d5ae102SDimitry Andric /// None. Parameter \p Context points to the class instance holding the live 7531d5ae102SDimitry Andric /// string and numeric variables. \returns the pointer to the class instance 7541d5ae102SDimitry Andric /// representing that variable if successful, or an error holding a 7551d5ae102SDimitry Andric /// diagnostic against \p SM otherwise. 756706b4fc4SDimitry Andric static Expected<std::unique_ptr<NumericVariableUse>> parseNumericVariableUse( 757e3b55780SDimitry Andric StringRef Name, bool IsPseudo, std::optional<size_t> LineNumber, 758706b4fc4SDimitry Andric FileCheckPatternContext *Context, const SourceMgr &SM); 759cfca06d7SDimitry Andric enum class AllowedOperand { LineVar, LegacyLiteral, Any }; 7601d5ae102SDimitry Andric /// Parses \p Expr for use of a numeric operand at line \p LineNumber, or 761cfca06d7SDimitry Andric /// before input is parsed if \p LineNumber is None. Accepts literal values, 762cfca06d7SDimitry Andric /// numeric variables and function calls, depending on the value of \p AO. 763cfca06d7SDimitry Andric /// \p MaybeInvalidConstraint indicates whether the text being parsed could 764cfca06d7SDimitry Andric /// be an invalid constraint. \p Context points to the class instance holding 765cfca06d7SDimitry Andric /// the live string and numeric variables. \returns the class representing 766cfca06d7SDimitry Andric /// that operand in the AST of the expression or an error holding a 767cfca06d7SDimitry Andric /// diagnostic against \p SM otherwise. If \p Expr starts with a "(" this 768cfca06d7SDimitry Andric /// function will attempt to parse a parenthesized expression. 769cfca06d7SDimitry Andric static Expected<std::unique_ptr<ExpressionAST>> 770cfca06d7SDimitry Andric parseNumericOperand(StringRef &Expr, AllowedOperand AO, bool ConstraintParsed, 771e3b55780SDimitry Andric std::optional<size_t> LineNumber, 772cfca06d7SDimitry Andric FileCheckPatternContext *Context, const SourceMgr &SM); 773cfca06d7SDimitry Andric /// Parses and updates \p RemainingExpr for a binary operation at line 774cfca06d7SDimitry Andric /// \p LineNumber, or before input is parsed if \p LineNumber is None. The 775cfca06d7SDimitry Andric /// left operand of this binary operation is given in \p LeftOp and \p Expr 776cfca06d7SDimitry Andric /// holds the string for the full expression, including the left operand. 777cfca06d7SDimitry Andric /// Parameter \p IsLegacyLineExpr indicates whether we are parsing a legacy 778cfca06d7SDimitry Andric /// @LINE expression. Parameter \p Context points to the class instance 779cfca06d7SDimitry Andric /// holding the live string and numeric variables. \returns the class 780cfca06d7SDimitry Andric /// representing the binary operation in the AST of the expression, or an 781cfca06d7SDimitry Andric /// error holding a diagnostic against \p SM otherwise. 782cfca06d7SDimitry Andric static Expected<std::unique_ptr<ExpressionAST>> 783cfca06d7SDimitry Andric parseBinop(StringRef Expr, StringRef &RemainingExpr, 784cfca06d7SDimitry Andric std::unique_ptr<ExpressionAST> LeftOp, bool IsLegacyLineExpr, 785e3b55780SDimitry Andric std::optional<size_t> LineNumber, FileCheckPatternContext *Context, 786cfca06d7SDimitry Andric const SourceMgr &SM); 787cfca06d7SDimitry Andric 788cfca06d7SDimitry Andric /// Parses a parenthesized expression inside \p Expr at line \p LineNumber, or 789cfca06d7SDimitry Andric /// before input is parsed if \p LineNumber is None. \p Expr must start with 790cfca06d7SDimitry Andric /// a '('. Accepts both literal values and numeric variables. Parameter \p 791cfca06d7SDimitry Andric /// Context points to the class instance holding the live string and numeric 792cfca06d7SDimitry Andric /// variables. \returns the class representing that operand in the AST of the 793cfca06d7SDimitry Andric /// expression or an error holding a diagnostic against \p SM otherwise. 794cfca06d7SDimitry Andric static Expected<std::unique_ptr<ExpressionAST>> 795e3b55780SDimitry Andric parseParenExpr(StringRef &Expr, std::optional<size_t> LineNumber, 796cfca06d7SDimitry Andric FileCheckPatternContext *Context, const SourceMgr &SM); 797cfca06d7SDimitry Andric 798cfca06d7SDimitry Andric /// Parses \p Expr for an argument list belonging to a call to function \p 799cfca06d7SDimitry Andric /// FuncName at line \p LineNumber, or before input is parsed if \p LineNumber 800cfca06d7SDimitry Andric /// is None. Parameter \p FuncLoc is the source location used for diagnostics. 801cfca06d7SDimitry Andric /// Parameter \p Context points to the class instance holding the live string 802cfca06d7SDimitry Andric /// and numeric variables. \returns the class representing that call in the 8031d5ae102SDimitry Andric /// AST of the expression or an error holding a diagnostic against \p SM 8041d5ae102SDimitry Andric /// otherwise. 805706b4fc4SDimitry Andric static Expected<std::unique_ptr<ExpressionAST>> 806cfca06d7SDimitry Andric parseCallExpr(StringRef &Expr, StringRef FuncName, 807e3b55780SDimitry Andric std::optional<size_t> LineNumber, 808e3b55780SDimitry Andric FileCheckPatternContext *Context, const SourceMgr &SM); 8091d5ae102SDimitry Andric }; 8101d5ae102SDimitry Andric 8111d5ae102SDimitry Andric //===----------------------------------------------------------------------===// 8121d5ae102SDimitry Andric // Check Strings. 8131d5ae102SDimitry Andric //===----------------------------------------------------------------------===// 8141d5ae102SDimitry Andric 8151d5ae102SDimitry Andric /// A check that we found in the input file. 8161d5ae102SDimitry Andric struct FileCheckString { 8171d5ae102SDimitry Andric /// The pattern to match. 818706b4fc4SDimitry Andric Pattern Pat; 8191d5ae102SDimitry Andric 8201d5ae102SDimitry Andric /// Which prefix name this check matched. 8211d5ae102SDimitry Andric StringRef Prefix; 8221d5ae102SDimitry Andric 8231d5ae102SDimitry Andric /// The location in the match file that the check string was specified. 8241d5ae102SDimitry Andric SMLoc Loc; 8251d5ae102SDimitry Andric 8264df029ccSDimitry Andric /// Hold the information about the DAG/NOT strings in the program, which are 8274df029ccSDimitry Andric /// not explicitly stored otherwise. This allows for better and more accurate 8284df029ccSDimitry Andric /// diagnostic messages. 8294df029ccSDimitry Andric struct DagNotPrefixInfo { 8304df029ccSDimitry Andric Pattern DagNotPat; 8314df029ccSDimitry Andric StringRef DagNotPrefix; 8324df029ccSDimitry Andric DagNotPrefixInfoFileCheckString::DagNotPrefixInfo8334df029ccSDimitry Andric DagNotPrefixInfo(const Pattern &P, StringRef S) 8344df029ccSDimitry Andric : DagNotPat(P), DagNotPrefix(S) {} 8354df029ccSDimitry Andric }; 8364df029ccSDimitry Andric 8374df029ccSDimitry Andric /// Hold the DAG/NOT strings occurring in the input file. 8384df029ccSDimitry Andric std::vector<DagNotPrefixInfo> DagNotStrings; 8391d5ae102SDimitry Andric FileCheckStringFileCheckString840706b4fc4SDimitry Andric FileCheckString(const Pattern &P, StringRef S, SMLoc L) 8411d5ae102SDimitry Andric : Pat(P), Prefix(S), Loc(L) {} 8421d5ae102SDimitry Andric 8431d5ae102SDimitry Andric /// Matches check string and its "not strings" and/or "dag strings". 8441d5ae102SDimitry Andric size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode, 8451d5ae102SDimitry Andric size_t &MatchLen, FileCheckRequest &Req, 8461d5ae102SDimitry Andric std::vector<FileCheckDiag> *Diags) const; 8471d5ae102SDimitry Andric 8481d5ae102SDimitry Andric /// Verifies that there is a single line in the given \p Buffer. Errors are 8491d5ae102SDimitry Andric /// reported against \p SM. 8501d5ae102SDimitry Andric bool CheckNext(const SourceMgr &SM, StringRef Buffer) const; 8511d5ae102SDimitry Andric /// Verifies that there is no newline in the given \p Buffer. Errors are 8521d5ae102SDimitry Andric /// reported against \p SM. 8531d5ae102SDimitry Andric bool CheckSame(const SourceMgr &SM, StringRef Buffer) const; 8541d5ae102SDimitry Andric /// Verifies that none of the strings in \p NotStrings are found in the given 8551d5ae102SDimitry Andric /// \p Buffer. Errors are reported against \p SM and diagnostics recorded in 8561d5ae102SDimitry Andric /// \p Diags according to the verbosity level set in \p Req. 8571d5ae102SDimitry Andric bool CheckNot(const SourceMgr &SM, StringRef Buffer, 8584df029ccSDimitry Andric const std::vector<const DagNotPrefixInfo *> &NotStrings, 8591d5ae102SDimitry Andric const FileCheckRequest &Req, 8601d5ae102SDimitry Andric std::vector<FileCheckDiag> *Diags) const; 8611d5ae102SDimitry Andric /// Matches "dag strings" and their mixed "not strings". 8621d5ae102SDimitry Andric size_t CheckDag(const SourceMgr &SM, StringRef Buffer, 8634df029ccSDimitry Andric std::vector<const DagNotPrefixInfo *> &NotStrings, 8641d5ae102SDimitry Andric const FileCheckRequest &Req, 8651d5ae102SDimitry Andric std::vector<FileCheckDiag> *Diags) const; 8661d5ae102SDimitry Andric }; 8671d5ae102SDimitry Andric 8681d5ae102SDimitry Andric } // namespace llvm 8691d5ae102SDimitry Andric 8701d5ae102SDimitry Andric #endif 871