Kai Nacke | dfd2b6f | 2019-10-10 13:15:41 +0000 | [diff] [blame] | 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/ADT/Optional.h"
|
| 19 | #include "llvm/ADT/StringMap.h"
|
| 20 | #include "llvm/ADT/StringRef.h"
|
| 21 | #include "llvm/Support/Error.h"
|
| 22 | #include "llvm/Support/SourceMgr.h"
|
| 23 | #include <map>
|
| 24 | #include <string>
|
| 25 | #include <vector>
|
| 26 |
|
| 27 | namespace llvm {
|
| 28 |
|
| 29 | //===----------------------------------------------------------------------===//
|
| 30 | // Numeric substitution handling code.
|
| 31 | //===----------------------------------------------------------------------===//
|
| 32 |
|
| 33 | /// Base class representing the AST of a given expression.
|
| 34 | class FileCheckExpressionAST {
|
| 35 | public:
|
| 36 | virtual ~FileCheckExpressionAST() = default;
|
| 37 |
|
| 38 | /// Evaluates and \returns the value of the expression represented by this
|
| 39 | /// AST or an error if evaluation fails.
|
| 40 | virtual Expected<uint64_t> eval() const = 0;
|
| 41 | };
|
| 42 |
|
| 43 | /// Class representing an unsigned literal in the AST of an expression.
|
| 44 | class FileCheckExpressionLiteral : public FileCheckExpressionAST {
|
| 45 | private:
|
| 46 | /// Actual value of the literal.
|
| 47 | uint64_t Value;
|
| 48 |
|
| 49 | public:
|
| 50 | /// Constructs a literal with the specified value.
|
| 51 | FileCheckExpressionLiteral(uint64_t Val) : Value(Val) {}
|
| 52 |
|
| 53 | /// \returns the literal's value.
|
| 54 | Expected<uint64_t> eval() const { return Value; }
|
| 55 | };
|
| 56 |
|
| 57 | /// Class to represent an undefined variable error, which quotes that
|
| 58 | /// variable's name when printed.
|
| 59 | class FileCheckUndefVarError : public ErrorInfo<FileCheckUndefVarError> {
|
| 60 | private:
|
| 61 | StringRef VarName;
|
| 62 |
|
| 63 | public:
|
| 64 | static char ID;
|
| 65 |
|
| 66 | FileCheckUndefVarError(StringRef VarName) : VarName(VarName) {}
|
| 67 |
|
| 68 | StringRef getVarName() const { return VarName; }
|
| 69 |
|
| 70 | std::error_code convertToErrorCode() const override {
|
| 71 | return inconvertibleErrorCode();
|
| 72 | }
|
| 73 |
|
| 74 | /// Print name of variable associated with this error.
|
| 75 | void log(raw_ostream &OS) const override {
|
| 76 | OS << "\"";
|
| 77 | OS.write_escaped(VarName) << "\"";
|
| 78 | }
|
| 79 | };
|
| 80 |
|
| 81 | /// Class representing a numeric variable and its associated current value.
|
| 82 | class FileCheckNumericVariable {
|
| 83 | private:
|
| 84 | /// Name of the numeric variable.
|
| 85 | StringRef Name;
|
| 86 |
|
| 87 | /// Value of numeric variable, if defined, or None otherwise.
|
| 88 | Optional<uint64_t> Value;
|
| 89 |
|
| 90 | /// Line number where this variable is defined, or None if defined before
|
| 91 | /// input is parsed. Used to determine whether a variable is defined on the
|
| 92 | /// same line as a given use.
|
| 93 | Optional<size_t> DefLineNumber;
|
| 94 |
|
| 95 | public:
|
| 96 | /// Constructor for a variable \p Name defined at line \p DefLineNumber or
|
| 97 | /// defined before input is parsed if \p DefLineNumber is None.
|
| 98 | explicit FileCheckNumericVariable(StringRef Name,
|
| 99 | Optional<size_t> DefLineNumber = None)
|
| 100 | : Name(Name), DefLineNumber(DefLineNumber) {}
|
| 101 |
|
| 102 | /// \returns name of this numeric variable.
|
| 103 | StringRef getName() const { return Name; }
|
| 104 |
|
| 105 | /// \returns this variable's value.
|
| 106 | Optional<uint64_t> getValue() const { return Value; }
|
| 107 |
|
| 108 | /// Sets value of this numeric variable to \p NewValue.
|
| 109 | void setValue(uint64_t NewValue) { Value = NewValue; }
|
| 110 |
|
| 111 | /// Clears value of this numeric variable, regardless of whether it is
|
| 112 | /// currently defined or not.
|
| 113 | void clearValue() { Value = None; }
|
| 114 |
|
| 115 | /// \returns the line number where this variable is defined, if any, or None
|
| 116 | /// if defined before input is parsed.
|
| 117 | Optional<size_t> getDefLineNumber() { return DefLineNumber; }
|
| 118 | };
|
| 119 |
|
| 120 | /// Class representing the use of a numeric variable in the AST of an
|
| 121 | /// expression.
|
| 122 | class FileCheckNumericVariableUse : public FileCheckExpressionAST {
|
| 123 | private:
|
| 124 | /// Name of the numeric variable.
|
| 125 | StringRef Name;
|
| 126 |
|
| 127 | /// Pointer to the class instance for the variable this use is about.
|
| 128 | FileCheckNumericVariable *NumericVariable;
|
| 129 |
|
| 130 | public:
|
| 131 | FileCheckNumericVariableUse(StringRef Name,
|
| 132 | FileCheckNumericVariable *NumericVariable)
|
| 133 | : Name(Name), NumericVariable(NumericVariable) {}
|
| 134 |
|
| 135 | /// \returns the value of the variable referenced by this instance.
|
| 136 | Expected<uint64_t> eval() const;
|
| 137 | };
|
| 138 |
|
| 139 | /// Type of functions evaluating a given binary operation.
|
| 140 | using binop_eval_t = uint64_t (*)(uint64_t, uint64_t);
|
| 141 |
|
| 142 | /// Class representing a single binary operation in the AST of an expression.
|
| 143 | class FileCheckASTBinop : public FileCheckExpressionAST {
|
| 144 | private:
|
| 145 | /// Left operand.
|
| 146 | std::unique_ptr<FileCheckExpressionAST> LeftOperand;
|
| 147 |
|
| 148 | /// Right operand.
|
| 149 | std::unique_ptr<FileCheckExpressionAST> RightOperand;
|
| 150 |
|
| 151 | /// Pointer to function that can evaluate this binary operation.
|
| 152 | binop_eval_t EvalBinop;
|
| 153 |
|
| 154 | public:
|
| 155 | FileCheckASTBinop(binop_eval_t EvalBinop,
|
| 156 | std::unique_ptr<FileCheckExpressionAST> LeftOp,
|
| 157 | std::unique_ptr<FileCheckExpressionAST> RightOp)
|
| 158 | : EvalBinop(EvalBinop) {
|
| 159 | LeftOperand = std::move(LeftOp);
|
| 160 | RightOperand = std::move(RightOp);
|
| 161 | }
|
| 162 |
|
| 163 | /// Evaluates the value of the binary operation represented by this AST,
|
| 164 | /// using EvalBinop on the result of recursively evaluating the operands.
|
| 165 | /// \returns the expression value or an error if an undefined numeric
|
| 166 | /// variable is used in one of the operands.
|
| 167 | Expected<uint64_t> eval() const;
|
| 168 | };
|
| 169 |
|
| 170 | class FileCheckPatternContext;
|
| 171 |
|
| 172 | /// Class representing a substitution to perform in the RegExStr string.
|
| 173 | class FileCheckSubstitution {
|
| 174 | protected:
|
| 175 | /// Pointer to a class instance holding, among other things, the table with
|
| 176 | /// the values of live string variables at the start of any given CHECK line.
|
| 177 | /// Used for substituting string variables with the text they were defined
|
| 178 | /// as. Expressions are linked to the numeric variables they use at
|
| 179 | /// parse time and directly access the value of the numeric variable to
|
| 180 | /// evaluate their value.
|
| 181 | FileCheckPatternContext *Context;
|
| 182 |
|
| 183 | /// The string that needs to be substituted for something else. For a
|
| 184 | /// string variable this is its name, otherwise this is the whole expression.
|
| 185 | StringRef FromStr;
|
| 186 |
|
| 187 | // Index in RegExStr of where to do the substitution.
|
| 188 | size_t InsertIdx;
|
| 189 |
|
| 190 | public:
|
| 191 | FileCheckSubstitution(FileCheckPatternContext *Context, StringRef VarName,
|
| 192 | size_t InsertIdx)
|
| 193 | : Context(Context), FromStr(VarName), InsertIdx(InsertIdx) {}
|
| 194 |
|
| 195 | virtual ~FileCheckSubstitution() = default;
|
| 196 |
|
| 197 | /// \returns the string to be substituted for something else.
|
| 198 | StringRef getFromString() const { return FromStr; }
|
| 199 |
|
| 200 | /// \returns the index where the substitution is to be performed in RegExStr.
|
| 201 | size_t getIndex() const { return InsertIdx; }
|
| 202 |
|
| 203 | /// \returns a string containing the result of the substitution represented
|
| 204 | /// by this class instance or an error if substitution failed.
|
| 205 | virtual Expected<std::string> getResult() const = 0;
|
| 206 | };
|
| 207 |
|
| 208 | class FileCheckStringSubstitution : public FileCheckSubstitution {
|
| 209 | public:
|
| 210 | FileCheckStringSubstitution(FileCheckPatternContext *Context,
|
| 211 | StringRef VarName, size_t InsertIdx)
|
| 212 | : FileCheckSubstitution(Context, VarName, InsertIdx) {}
|
| 213 |
|
| 214 | /// \returns the text that the string variable in this substitution matched
|
| 215 | /// when defined, or an error if the variable is undefined.
|
| 216 | Expected<std::string> getResult() const override;
|
| 217 | };
|
| 218 |
|
| 219 | class FileCheckNumericSubstitution : public FileCheckSubstitution {
|
| 220 | private:
|
| 221 | /// Pointer to the class representing the expression whose value is to be
|
| 222 | /// substituted.
|
| 223 | std::unique_ptr<FileCheckExpressionAST> ExpressionAST;
|
| 224 |
|
| 225 | public:
|
| 226 | FileCheckNumericSubstitution(FileCheckPatternContext *Context, StringRef Expr,
|
| 227 | std::unique_ptr<FileCheckExpressionAST> ExprAST,
|
| 228 | size_t InsertIdx)
|
| 229 | : FileCheckSubstitution(Context, Expr, InsertIdx) {
|
| 230 | ExpressionAST = std::move(ExprAST);
|
| 231 | }
|
| 232 |
|
| 233 | /// \returns a string containing the result of evaluating the expression in
|
| 234 | /// this substitution, or an error if evaluation failed.
|
| 235 | Expected<std::string> getResult() const override;
|
| 236 | };
|
| 237 |
|
| 238 | //===----------------------------------------------------------------------===//
|
| 239 | // Pattern handling code.
|
| 240 | //===----------------------------------------------------------------------===//
|
| 241 |
|
| 242 | struct FileCheckDiag;
|
| 243 |
|
| 244 | /// Class holding the FileCheckPattern global state, shared by all patterns:
|
| 245 | /// tables holding values of variables and whether they are defined or not at
|
| 246 | /// any given time in the matching process.
|
| 247 | class FileCheckPatternContext {
|
| 248 | friend class FileCheckPattern;
|
| 249 |
|
| 250 | private:
|
| 251 | /// When matching a given pattern, this holds the value of all the string
|
| 252 | /// variables defined in previous patterns. In a pattern, only the last
|
| 253 | /// definition for a given variable is recorded in this table.
|
| 254 | /// Back-references are used for uses after any the other definition.
|
| 255 | StringMap<StringRef> GlobalVariableTable;
|
| 256 |
|
| 257 | /// Map of all string variables defined so far. Used at parse time to detect
|
| 258 | /// a name conflict between a numeric variable and a string variable when
|
| 259 | /// the former is defined on a later line than the latter.
|
| 260 | StringMap<bool> DefinedVariableTable;
|
| 261 |
|
| 262 | /// When matching a given pattern, this holds the pointers to the classes
|
| 263 | /// representing the numeric variables defined in previous patterns. When
|
| 264 | /// matching a pattern all definitions for that pattern are recorded in the
|
| 265 | /// NumericVariableDefs table in the FileCheckPattern instance of that
|
| 266 | /// pattern.
|
| 267 | StringMap<FileCheckNumericVariable *> GlobalNumericVariableTable;
|
| 268 |
|
| 269 | /// Pointer to the class instance representing the @LINE pseudo variable for
|
| 270 | /// easily updating its value.
|
| 271 | FileCheckNumericVariable *LineVariable = nullptr;
|
| 272 |
|
| 273 | /// Vector holding pointers to all parsed numeric variables. Used to
|
| 274 | /// automatically free them once they are guaranteed to no longer be used.
|
| 275 | std::vector<std::unique_ptr<FileCheckNumericVariable>> NumericVariables;
|
| 276 |
|
| 277 | /// Vector holding pointers to all substitutions. Used to automatically free
|
| 278 | /// them once they are guaranteed to no longer be used.
|
| 279 | std::vector<std::unique_ptr<FileCheckSubstitution>> Substitutions;
|
| 280 |
|
| 281 | public:
|
| 282 | /// \returns the value of string variable \p VarName or an error if no such
|
| 283 | /// variable has been defined.
|
| 284 | Expected<StringRef> getPatternVarValue(StringRef VarName);
|
| 285 |
|
| 286 | /// Defines string and numeric variables from definitions given on the
|
| 287 | /// command line, passed as a vector of [#]VAR=VAL strings in
|
| 288 | /// \p CmdlineDefines. \returns an error list containing diagnostics against
|
| 289 | /// \p SM for all definition parsing failures, if any, or Success otherwise.
|
| 290 | Error defineCmdlineVariables(std::vector<std::string> &CmdlineDefines,
|
| 291 | SourceMgr &SM);
|
| 292 |
|
| 293 | /// Create @LINE pseudo variable. Value is set when pattern are being
|
| 294 | /// matched.
|
| 295 | void createLineVariable();
|
| 296 |
|
| 297 | /// Undefines local variables (variables whose name does not start with a '$'
|
| 298 | /// sign), i.e. removes them from GlobalVariableTable and from
|
| 299 | /// GlobalNumericVariableTable and also clears the value of numeric
|
| 300 | /// variables.
|
| 301 | void clearLocalVars();
|
| 302 |
|
| 303 | private:
|
| 304 | /// Makes a new numeric variable and registers it for destruction when the
|
| 305 | /// context is destroyed.
|
| 306 | template <class... Types>
|
| 307 | FileCheckNumericVariable *makeNumericVariable(Types... args);
|
| 308 |
|
| 309 | /// Makes a new string substitution and registers it for destruction when the
|
| 310 | /// context is destroyed.
|
| 311 | FileCheckSubstitution *makeStringSubstitution(StringRef VarName,
|
| 312 | size_t InsertIdx);
|
| 313 |
|
| 314 | /// Makes a new numeric substitution and registers it for destruction when
|
| 315 | /// the context is destroyed.
|
| 316 | FileCheckSubstitution *
|
| 317 | makeNumericSubstitution(StringRef ExpressionStr,
|
| 318 | std::unique_ptr<FileCheckExpressionAST> ExpressionAST,
|
| 319 | size_t InsertIdx);
|
| 320 | };
|
| 321 |
|
| 322 | /// Class to represent an error holding a diagnostic with location information
|
| 323 | /// used when printing it.
|
| 324 | class FileCheckErrorDiagnostic : public ErrorInfo<FileCheckErrorDiagnostic> {
|
| 325 | private:
|
| 326 | SMDiagnostic Diagnostic;
|
| 327 |
|
| 328 | public:
|
| 329 | static char ID;
|
| 330 |
|
| 331 | FileCheckErrorDiagnostic(SMDiagnostic &&Diag) : Diagnostic(Diag) {}
|
| 332 |
|
| 333 | std::error_code convertToErrorCode() const override {
|
| 334 | return inconvertibleErrorCode();
|
| 335 | }
|
| 336 |
|
| 337 | /// Print diagnostic associated with this error when printing the error.
|
| 338 | void log(raw_ostream &OS) const override { Diagnostic.print(nullptr, OS); }
|
| 339 |
|
| 340 | static Error get(const SourceMgr &SM, SMLoc Loc, const Twine &ErrMsg) {
|
| 341 | return make_error<FileCheckErrorDiagnostic>(
|
| 342 | SM.GetMessage(Loc, SourceMgr::DK_Error, ErrMsg));
|
| 343 | }
|
| 344 |
|
| 345 | static Error get(const SourceMgr &SM, StringRef Buffer, const Twine &ErrMsg) {
|
| 346 | return get(SM, SMLoc::getFromPointer(Buffer.data()), ErrMsg);
|
| 347 | }
|
| 348 | };
|
| 349 |
|
| 350 | class FileCheckNotFoundError : public ErrorInfo<FileCheckNotFoundError> {
|
| 351 | public:
|
| 352 | static char ID;
|
| 353 |
|
| 354 | std::error_code convertToErrorCode() const override {
|
| 355 | return inconvertibleErrorCode();
|
| 356 | }
|
| 357 |
|
| 358 | /// Print diagnostic associated with this error when printing the error.
|
| 359 | void log(raw_ostream &OS) const override {
|
| 360 | OS << "String not found in input";
|
| 361 | }
|
| 362 | };
|
| 363 |
|
| 364 | class FileCheckPattern {
|
| 365 | SMLoc PatternLoc;
|
| 366 |
|
| 367 | /// A fixed string to match as the pattern or empty if this pattern requires
|
| 368 | /// a regex match.
|
| 369 | StringRef FixedStr;
|
| 370 |
|
| 371 | /// A regex string to match as the pattern or empty if this pattern requires
|
| 372 | /// a fixed string to match.
|
| 373 | std::string RegExStr;
|
| 374 |
|
| 375 | /// Entries in this vector represent a substitution of a string variable or
|
| 376 | /// an expression in the RegExStr regex at match time. For example, in the
|
| 377 | /// case of a CHECK directive with the pattern "foo[[bar]]baz[[#N+1]]",
|
| 378 | /// RegExStr will contain "foobaz" and we'll get two entries in this vector
|
| 379 | /// that tells us to insert the value of string variable "bar" at offset 3
|
| 380 | /// and the value of expression "N+1" at offset 6.
|
| 381 | std::vector<FileCheckSubstitution *> Substitutions;
|
| 382 |
|
| 383 | /// Maps names of string variables defined in a pattern to the number of
|
| 384 | /// their parenthesis group in RegExStr capturing their last definition.
|
| 385 | ///
|
| 386 | /// E.g. for the pattern "foo[[bar:.*]]baz([[bar]][[QUUX]][[bar:.*]])",
|
| 387 | /// RegExStr will be "foo(.*)baz(\1<quux value>(.*))" where <quux value> is
|
| 388 | /// the value captured for QUUX on the earlier line where it was defined, and
|
| 389 | /// VariableDefs will map "bar" to the third parenthesis group which captures
|
| 390 | /// the second definition of "bar".
|
| 391 | ///
|
| 392 | /// Note: uses std::map rather than StringMap to be able to get the key when
|
| 393 | /// iterating over values.
|
| 394 | std::map<StringRef, unsigned> VariableDefs;
|
| 395 |
|
| 396 | /// Structure representing the definition of a numeric variable in a pattern.
|
| 397 | /// It holds the pointer to the class representing the numeric variable whose
|
| 398 | /// value is being defined and the number of the parenthesis group in
|
| 399 | /// RegExStr to capture that value.
|
| 400 | struct FileCheckNumericVariableMatch {
|
| 401 | /// Pointer to class representing the numeric variable whose value is being
|
| 402 | /// defined.
|
| 403 | FileCheckNumericVariable *DefinedNumericVariable;
|
| 404 |
|
| 405 | /// Number of the parenthesis group in RegExStr that captures the value of
|
| 406 | /// this numeric variable definition.
|
| 407 | unsigned CaptureParenGroup;
|
| 408 | };
|
| 409 |
|
| 410 | /// Holds the number of the parenthesis group in RegExStr and pointer to the
|
| 411 | /// corresponding FileCheckNumericVariable class instance of all numeric
|
| 412 | /// variable definitions. Used to set the matched value of all those
|
| 413 | /// variables.
|
| 414 | StringMap<FileCheckNumericVariableMatch> NumericVariableDefs;
|
| 415 |
|
| 416 | /// Pointer to a class instance holding the global state shared by all
|
| 417 | /// patterns:
|
| 418 | /// - separate tables with the values of live string and numeric variables
|
| 419 | /// respectively at the start of any given CHECK line;
|
| 420 | /// - table holding whether a string variable has been defined at any given
|
| 421 | /// point during the parsing phase.
|
| 422 | FileCheckPatternContext *Context;
|
| 423 |
|
| 424 | Check::FileCheckType CheckTy;
|
| 425 |
|
| 426 | /// Line number for this CHECK pattern or None if it is an implicit pattern.
|
| 427 | /// Used to determine whether a variable definition is made on an earlier
|
| 428 | /// line to the one with this CHECK.
|
| 429 | Optional<size_t> LineNumber;
|
| 430 |
|
| 431 | /// Ignore case while matching if set to true.
|
| 432 | bool IgnoreCase = false;
|
| 433 |
|
| 434 | public:
|
| 435 | FileCheckPattern(Check::FileCheckType Ty, FileCheckPatternContext *Context,
|
| 436 | Optional<size_t> Line = None)
|
| 437 | : Context(Context), CheckTy(Ty), LineNumber(Line) {}
|
| 438 |
|
| 439 | /// \returns the location in source code.
|
| 440 | SMLoc getLoc() const { return PatternLoc; }
|
| 441 |
|
| 442 | /// \returns the pointer to the global state for all patterns in this
|
| 443 | /// FileCheck instance.
|
| 444 | FileCheckPatternContext *getContext() const { return Context; }
|
| 445 |
|
| 446 | /// \returns whether \p C is a valid first character for a variable name.
|
| 447 | static bool isValidVarNameStart(char C);
|
| 448 |
|
| 449 | /// Parsing information about a variable.
|
| 450 | struct VariableProperties {
|
| 451 | StringRef Name;
|
| 452 | bool IsPseudo;
|
| 453 | };
|
| 454 |
|
| 455 | /// Parses the string at the start of \p Str for a variable name. \returns
|
| 456 | /// a VariableProperties structure holding the variable name and whether it
|
| 457 | /// is the name of a pseudo variable, or an error holding a diagnostic
|
| 458 | /// against \p SM if parsing fail. If parsing was successful, also strips
|
| 459 | /// \p Str from the variable name.
|
| 460 | static Expected<VariableProperties> parseVariable(StringRef &Str,
|
| 461 | const SourceMgr &SM);
|
| 462 | /// Parses \p Expr for a numeric substitution block at line \p LineNumber,
|
| 463 | /// or before input is parsed if \p LineNumber is None. Parameter
|
| 464 | /// \p IsLegacyLineExpr indicates whether \p Expr should be a legacy @LINE
|
| 465 | /// expression and \p Context points to the class instance holding the live
|
| 466 | /// string and numeric variables. \returns a pointer to the class instance
|
| 467 | /// representing the AST of the expression whose value must be substitued, or
|
| 468 | /// an error holding a diagnostic against \p SM if parsing fails. If
|
| 469 | /// substitution was successful, sets \p DefinedNumericVariable to point to
|
| 470 | /// the class representing the numeric variable defined in this numeric
|
| 471 | /// substitution block, or None if this block does not define any variable.
|
| 472 | static Expected<std::unique_ptr<FileCheckExpressionAST>>
|
| 473 | parseNumericSubstitutionBlock(
|
| 474 | StringRef Expr,
|
| 475 | Optional<FileCheckNumericVariable *> &DefinedNumericVariable,
|
| 476 | bool IsLegacyLineExpr, Optional<size_t> LineNumber,
|
| 477 | FileCheckPatternContext *Context, const SourceMgr &SM);
|
| 478 | /// Parses the pattern in \p PatternStr and initializes this FileCheckPattern
|
| 479 | /// instance accordingly.
|
| 480 | ///
|
| 481 | /// \p Prefix provides which prefix is being matched, \p Req describes the
|
| 482 | /// global options that influence the parsing such as whitespace
|
| 483 | /// canonicalization, \p SM provides the SourceMgr used for error reports.
|
| 484 | /// \returns true in case of an error, false otherwise.
|
| 485 | bool parsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
|
| 486 | const FileCheckRequest &Req);
|
| 487 | /// Matches the pattern string against the input buffer \p Buffer
|
| 488 | ///
|
| 489 | /// \returns the position that is matched or an error indicating why matching
|
| 490 | /// failed. If there is a match, updates \p MatchLen with the size of the
|
| 491 | /// matched string.
|
| 492 | ///
|
| 493 | /// The GlobalVariableTable StringMap in the FileCheckPatternContext class
|
| 494 | /// instance provides the current values of FileCheck string variables and
|
| 495 | /// is updated if this match defines new values. Likewise, the
|
| 496 | /// GlobalNumericVariableTable StringMap in the same class provides the
|
| 497 | /// current values of FileCheck numeric variables and is updated if this
|
| 498 | /// match defines new numeric values.
|
| 499 | Expected<size_t> match(StringRef Buffer, size_t &MatchLen,
|
| 500 | const SourceMgr &SM) const;
|
| 501 | /// Prints the value of successful substitutions or the name of the undefined
|
| 502 | /// string or numeric variables preventing a successful substitution.
|
| 503 | void printSubstitutions(const SourceMgr &SM, StringRef Buffer,
|
| 504 | SMRange MatchRange = None) const;
|
| 505 | void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
|
| 506 | std::vector<FileCheckDiag> *Diags) const;
|
| 507 |
|
| 508 | bool hasVariable() const {
|
| 509 | return !(Substitutions.empty() && VariableDefs.empty());
|
| 510 | }
|
| 511 |
|
| 512 | Check::FileCheckType getCheckTy() const { return CheckTy; }
|
| 513 |
|
| 514 | int getCount() const { return CheckTy.getCount(); }
|
| 515 |
|
| 516 | private:
|
| 517 | bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
|
| 518 | void AddBackrefToRegEx(unsigned BackrefNum);
|
| 519 | /// Computes an arbitrary estimate for the quality of matching this pattern
|
| 520 | /// at the start of \p Buffer; a distance of zero should correspond to a
|
| 521 | /// perfect match.
|
| 522 | unsigned computeMatchDistance(StringRef Buffer) const;
|
| 523 | /// Finds the closing sequence of a regex variable usage or definition.
|
| 524 | ///
|
| 525 | /// \p Str has to point in the beginning of the definition (right after the
|
| 526 | /// opening sequence). \p SM holds the SourceMgr used for error repporting.
|
| 527 | /// \returns the offset of the closing sequence within Str, or npos if it
|
| 528 | /// was not found.
|
| 529 | size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
|
| 530 |
|
| 531 | /// Parses \p Expr for the name of a numeric variable to be defined at line
|
| 532 | /// \p LineNumber, or before input is parsed if \p LineNumber is None.
|
| 533 | /// \returns a pointer to the class instance representing that variable,
|
| 534 | /// creating it if needed, or an error holding a diagnostic against \p SM
|
| 535 | /// should defining such a variable be invalid.
|
| 536 | static Expected<FileCheckNumericVariable *> parseNumericVariableDefinition(
|
| 537 | StringRef &Expr, FileCheckPatternContext *Context,
|
| 538 | Optional<size_t> LineNumber, const SourceMgr &SM);
|
| 539 | /// Parses \p Name as a (pseudo if \p IsPseudo is true) numeric variable use
|
| 540 | /// at line \p LineNumber, or before input is parsed if \p LineNumber is
|
| 541 | /// None. Parameter \p Context points to the class instance holding the live
|
| 542 | /// string and numeric variables. \returns the pointer to the class instance
|
| 543 | /// representing that variable if successful, or an error holding a
|
| 544 | /// diagnostic against \p SM otherwise.
|
| 545 | static Expected<std::unique_ptr<FileCheckNumericVariableUse>>
|
| 546 | parseNumericVariableUse(StringRef Name, bool IsPseudo,
|
| 547 | Optional<size_t> LineNumber,
|
| 548 | FileCheckPatternContext *Context,
|
| 549 | const SourceMgr &SM);
|
| 550 | enum class AllowedOperand { LineVar, Literal, Any };
|
| 551 | /// Parses \p Expr for use of a numeric operand at line \p LineNumber, or
|
| 552 | /// before input is parsed if \p LineNumber is None. Accepts both literal
|
| 553 | /// values and numeric variables, depending on the value of \p AO. Parameter
|
| 554 | /// \p Context points to the class instance holding the live string and
|
| 555 | /// numeric variables. \returns the class representing that operand in the
|
| 556 | /// AST of the expression or an error holding a diagnostic against \p SM
|
| 557 | /// otherwise.
|
| 558 | static Expected<std::unique_ptr<FileCheckExpressionAST>>
|
| 559 | parseNumericOperand(StringRef &Expr, AllowedOperand AO,
|
| 560 | Optional<size_t> LineNumber,
|
| 561 | FileCheckPatternContext *Context, const SourceMgr &SM);
|
| 562 | /// Parses \p Expr for a binary operation at line \p LineNumber, or before
|
| 563 | /// input is parsed if \p LineNumber is None. The left operand of this binary
|
| 564 | /// operation is given in \p LeftOp and \p IsLegacyLineExpr indicates whether
|
| 565 | /// we are parsing a legacy @LINE expression. Parameter \p Context points to
|
| 566 | /// the class instance holding the live string and numeric variables.
|
| 567 | /// \returns the class representing the binary operation in the AST of the
|
| 568 | /// expression, or an error holding a diagnostic against \p SM otherwise.
|
| 569 | static Expected<std::unique_ptr<FileCheckExpressionAST>>
|
| 570 | parseBinop(StringRef &Expr, std::unique_ptr<FileCheckExpressionAST> LeftOp,
|
| 571 | bool IsLegacyLineExpr, Optional<size_t> LineNumber,
|
| 572 | FileCheckPatternContext *Context, const SourceMgr &SM);
|
| 573 | };
|
| 574 |
|
| 575 | //===----------------------------------------------------------------------===//
|
| 576 | // Check Strings.
|
| 577 | //===----------------------------------------------------------------------===//
|
| 578 |
|
| 579 | /// A check that we found in the input file.
|
| 580 | struct FileCheckString {
|
| 581 | /// The pattern to match.
|
| 582 | FileCheckPattern Pat;
|
| 583 |
|
| 584 | /// Which prefix name this check matched.
|
| 585 | StringRef Prefix;
|
| 586 |
|
| 587 | /// The location in the match file that the check string was specified.
|
| 588 | SMLoc Loc;
|
| 589 |
|
| 590 | /// All of the strings that are disallowed from occurring between this match
|
| 591 | /// string and the previous one (or start of file).
|
| 592 | std::vector<FileCheckPattern> DagNotStrings;
|
| 593 |
|
| 594 | FileCheckString(const FileCheckPattern &P, StringRef S, SMLoc L)
|
| 595 | : Pat(P), Prefix(S), Loc(L) {}
|
| 596 |
|
| 597 | /// Matches check string and its "not strings" and/or "dag strings".
|
| 598 | size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
|
| 599 | size_t &MatchLen, FileCheckRequest &Req,
|
| 600 | std::vector<FileCheckDiag> *Diags) const;
|
| 601 |
|
| 602 | /// Verifies that there is a single line in the given \p Buffer. Errors are
|
| 603 | /// reported against \p SM.
|
| 604 | bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
|
| 605 | /// Verifies that there is no newline in the given \p Buffer. Errors are
|
| 606 | /// reported against \p SM.
|
| 607 | bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
|
| 608 | /// Verifies that none of the strings in \p NotStrings are found in the given
|
| 609 | /// \p Buffer. Errors are reported against \p SM and diagnostics recorded in
|
| 610 | /// \p Diags according to the verbosity level set in \p Req.
|
| 611 | bool CheckNot(const SourceMgr &SM, StringRef Buffer,
|
| 612 | const std::vector<const FileCheckPattern *> &NotStrings,
|
| 613 | const FileCheckRequest &Req,
|
| 614 | std::vector<FileCheckDiag> *Diags) const;
|
| 615 | /// Matches "dag strings" and their mixed "not strings".
|
| 616 | size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
|
| 617 | std::vector<const FileCheckPattern *> &NotStrings,
|
| 618 | const FileCheckRequest &Req,
|
| 619 | std::vector<FileCheckDiag> *Diags) const;
|
| 620 | };
|
| 621 |
|
| 622 | } // namespace llvm
|
| 623 |
|
| 624 | #endif
|