FileCheckImpl.h 35 KB

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