FileCheckImpl.h 38 KB

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