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