[ARM] MVE integer min and max
[llvm-complete.git] / include / llvm / Support / FileCheck.h
blobcaff50b0ca4667d8c3ba52ddb7c2152ecf030f10
1 //==-- llvm/Support/FileCheck.h ---------------------------*- 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 /// \file This file has some utilities to use FileCheck as an API
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_SUPPORT_FILECHECK_H
14 #define LLVM_SUPPORT_FILECHECK_H
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/Regex.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include <vector>
21 #include <map>
23 namespace llvm {
25 /// Contains info about various FileCheck options.
26 struct FileCheckRequest {
27 std::vector<std::string> CheckPrefixes;
28 bool NoCanonicalizeWhiteSpace = false;
29 std::vector<std::string> ImplicitCheckNot;
30 std::vector<std::string> GlobalDefines;
31 bool AllowEmptyInput = false;
32 bool MatchFullLines = false;
33 bool EnableVarScope = false;
34 bool AllowDeprecatedDagOverlap = false;
35 bool Verbose = false;
36 bool VerboseVerbose = false;
39 //===----------------------------------------------------------------------===//
40 // Numeric substitution handling code.
41 //===----------------------------------------------------------------------===//
43 /// Base class representing the AST of a given expression.
44 class FileCheckExpressionAST {
45 public:
46 virtual ~FileCheckExpressionAST() = default;
48 /// Evaluates and \returns the value of the expression represented by this
49 /// AST or an error if evaluation fails.
50 virtual Expected<uint64_t> eval() const = 0;
53 /// Class representing an unsigned literal in the AST of an expression.
54 class FileCheckExpressionLiteral : public FileCheckExpressionAST {
55 private:
56 /// Actual value of the literal.
57 uint64_t Value;
59 public:
60 /// Constructs a literal with the specified value.
61 FileCheckExpressionLiteral(uint64_t Val) : Value(Val) {}
63 /// \returns the literal's value.
64 Expected<uint64_t> eval() const { return Value; }
67 /// Class to represent an undefined variable error, which quotes that
68 /// variable's name when printed.
69 class FileCheckUndefVarError : public ErrorInfo<FileCheckUndefVarError> {
70 private:
71 StringRef VarName;
73 public:
74 static char ID;
76 FileCheckUndefVarError(StringRef VarName) : VarName(VarName) {}
78 StringRef getVarName() const { return VarName; }
80 std::error_code convertToErrorCode() const override {
81 return inconvertibleErrorCode();
84 /// Print name of variable associated with this error.
85 void log(raw_ostream &OS) const override {
86 OS << "\"";
87 OS.write_escaped(VarName) << "\"";
91 /// Class representing a numeric variable and its associated current value.
92 class FileCheckNumericVariable {
93 private:
94 /// Name of the numeric variable.
95 StringRef Name;
97 /// Value of numeric variable, if defined, or None otherwise.
98 Optional<uint64_t> Value;
100 /// Line number where this variable is defined. Used to determine whether a
101 /// variable is defined on the same line as a given use.
102 size_t DefLineNumber;
104 public:
105 /// Constructor for a variable \p Name defined at line \p DefLineNumber.
106 FileCheckNumericVariable(size_t DefLineNumber, StringRef Name)
107 : Name(Name), DefLineNumber(DefLineNumber) {}
109 /// Constructor for numeric variable \p Name with a known \p Value at parse
110 /// time (e.g. the @LINE numeric variable).
111 FileCheckNumericVariable(StringRef Name, uint64_t Value)
112 : Name(Name), Value(Value), DefLineNumber(0) {}
114 /// \returns name of this numeric variable.
115 StringRef getName() const { return Name; }
117 /// \returns this variable's value.
118 Optional<uint64_t> getValue() const { return Value; }
120 /// Sets value of this numeric variable, if undefined. Triggers an assertion
121 /// failure if the variable is actually defined.
122 void setValue(uint64_t Value);
124 /// Clears value of this numeric variable, regardless of whether it is
125 /// currently defined or not.
126 void clearValue();
128 /// \returns the line number where this variable is defined.
129 size_t getDefLineNumber() { return DefLineNumber; }
132 /// Class representing the use of a numeric variable in the AST of an
133 /// expression.
134 class FileCheckNumericVariableUse : public FileCheckExpressionAST {
135 private:
136 /// Name of the numeric variable.
137 StringRef Name;
139 /// Pointer to the class instance for the variable this use is about.
140 FileCheckNumericVariable *NumericVariable;
142 public:
143 FileCheckNumericVariableUse(StringRef Name,
144 FileCheckNumericVariable *NumericVariable)
145 : Name(Name), NumericVariable(NumericVariable) {}
147 /// \returns the value of the variable referenced by this instance.
148 Expected<uint64_t> eval() const;
151 /// Type of functions evaluating a given binary operation.
152 using binop_eval_t = uint64_t (*)(uint64_t, uint64_t);
154 /// Class representing a single binary operation in the AST of an expression.
155 class FileCheckASTBinop : public FileCheckExpressionAST {
156 private:
157 /// Left operand.
158 std::unique_ptr<FileCheckExpressionAST> LeftOperand;
160 /// Right operand.
161 std::unique_ptr<FileCheckExpressionAST> RightOperand;
163 /// Pointer to function that can evaluate this binary operation.
164 binop_eval_t EvalBinop;
166 public:
167 FileCheckASTBinop(binop_eval_t EvalBinop,
168 std::unique_ptr<FileCheckExpressionAST> LeftOp,
169 std::unique_ptr<FileCheckExpressionAST> RightOp)
170 : EvalBinop(EvalBinop) {
171 LeftOperand = std::move(LeftOp);
172 RightOperand = std::move(RightOp);
175 /// Evaluates the value of the binary operation represented by this AST,
176 /// using EvalBinop on the result of recursively evaluating the operands.
177 /// \returns the expression value or an error if an undefined numeric
178 /// variable is used in one of the operands.
179 Expected<uint64_t> eval() const;
182 class FileCheckPatternContext;
184 /// Class representing a substitution to perform in the RegExStr string.
185 class FileCheckSubstitution {
186 protected:
187 /// Pointer to a class instance holding, among other things, the table with
188 /// the values of live string variables at the start of any given CHECK line.
189 /// Used for substituting string variables with the text they were defined
190 /// as. Expressions are linked to the numeric variables they use at
191 /// parse time and directly access the value of the numeric variable to
192 /// evaluate their value.
193 FileCheckPatternContext *Context;
195 /// The string that needs to be substituted for something else. For a
196 /// string variable this is its name, otherwise this is the whole expression.
197 StringRef FromStr;
199 // Index in RegExStr of where to do the substitution.
200 size_t InsertIdx;
202 public:
203 FileCheckSubstitution(FileCheckPatternContext *Context, StringRef VarName,
204 size_t InsertIdx)
205 : Context(Context), FromStr(VarName), InsertIdx(InsertIdx) {}
207 virtual ~FileCheckSubstitution() = default;
209 /// \returns the string to be substituted for something else.
210 StringRef getFromString() const { return FromStr; }
212 /// \returns the index where the substitution is to be performed in RegExStr.
213 size_t getIndex() const { return InsertIdx; }
215 /// \returns a string containing the result of the substitution represented
216 /// by this class instance or an error if substitution failed.
217 virtual Expected<std::string> getResult() const = 0;
220 class FileCheckStringSubstitution : public FileCheckSubstitution {
221 public:
222 FileCheckStringSubstitution(FileCheckPatternContext *Context,
223 StringRef VarName, size_t InsertIdx)
224 : FileCheckSubstitution(Context, VarName, InsertIdx) {}
226 /// \returns the text that the string variable in this substitution matched
227 /// when defined, or an error if the variable is undefined.
228 Expected<std::string> getResult() const override;
231 class FileCheckNumericSubstitution : public FileCheckSubstitution {
232 private:
233 /// Pointer to the class representing the expression whose value is to be
234 /// substituted.
235 std::unique_ptr<FileCheckExpressionAST> ExpressionAST;
237 public:
238 FileCheckNumericSubstitution(FileCheckPatternContext *Context, StringRef Expr,
239 std::unique_ptr<FileCheckExpressionAST> ExprAST,
240 size_t InsertIdx)
241 : FileCheckSubstitution(Context, Expr, InsertIdx) {
242 ExpressionAST = std::move(ExprAST);
245 /// \returns a string containing the result of evaluating the expression in
246 /// this substitution, or an error if evaluation failed.
247 Expected<std::string> getResult() const override;
250 //===----------------------------------------------------------------------===//
251 // Pattern handling code.
252 //===----------------------------------------------------------------------===//
254 namespace Check {
256 enum FileCheckKind {
257 CheckNone = 0,
258 CheckPlain,
259 CheckNext,
260 CheckSame,
261 CheckNot,
262 CheckDAG,
263 CheckLabel,
264 CheckEmpty,
266 /// Indicates the pattern only matches the end of file. This is used for
267 /// trailing CHECK-NOTs.
268 CheckEOF,
270 /// Marks when parsing found a -NOT check combined with another CHECK suffix.
271 CheckBadNot,
273 /// Marks when parsing found a -COUNT directive with invalid count value.
274 CheckBadCount
277 class FileCheckType {
278 FileCheckKind Kind;
279 int Count; ///< optional Count for some checks
281 public:
282 FileCheckType(FileCheckKind Kind = CheckNone) : Kind(Kind), Count(1) {}
283 FileCheckType(const FileCheckType &) = default;
285 operator FileCheckKind() const { return Kind; }
287 int getCount() const { return Count; }
288 FileCheckType &setCount(int C);
290 // \returns a description of \p Prefix.
291 std::string getDescription(StringRef Prefix) const;
293 } // namespace Check
295 struct FileCheckDiag;
297 /// Class holding the FileCheckPattern global state, shared by all patterns:
298 /// tables holding values of variables and whether they are defined or not at
299 /// any given time in the matching process.
300 class FileCheckPatternContext {
301 friend class FileCheckPattern;
303 private:
304 /// When matching a given pattern, this holds the value of all the string
305 /// variables defined in previous patterns. In a pattern, only the last
306 /// definition for a given variable is recorded in this table.
307 /// Back-references are used for uses after any the other definition.
308 StringMap<StringRef> GlobalVariableTable;
310 /// Map of all string variables defined so far. Used at parse time to detect
311 /// a name conflict between a numeric variable and a string variable when
312 /// the former is defined on a later line than the latter.
313 StringMap<bool> DefinedVariableTable;
315 /// When matching a given pattern, this holds the pointers to the classes
316 /// representing the numeric variables defined in previous patterns. When
317 /// matching a pattern all definitions for that pattern are recorded in the
318 /// NumericVariableDefs table in the FileCheckPattern instance of that
319 /// pattern.
320 StringMap<FileCheckNumericVariable *> GlobalNumericVariableTable;
322 /// Pointer to the class instance representing the @LINE pseudo variable for
323 /// easily updating its value.
324 FileCheckNumericVariable *LineVariable = nullptr;
326 /// Vector holding pointers to all parsed numeric variables. Used to
327 /// automatically free them once they are guaranteed to no longer be used.
328 std::vector<std::unique_ptr<FileCheckNumericVariable>> NumericVariables;
330 /// Vector holding pointers to all substitutions. Used to automatically free
331 /// them once they are guaranteed to no longer be used.
332 std::vector<std::unique_ptr<FileCheckSubstitution>> Substitutions;
334 public:
335 /// \returns the value of string variable \p VarName or an error if no such
336 /// variable has been defined.
337 Expected<StringRef> getPatternVarValue(StringRef VarName);
339 /// Defines string and numeric variables from definitions given on the
340 /// command line, passed as a vector of [#]VAR=VAL strings in
341 /// \p CmdlineDefines. \returns an error list containing diagnostics against
342 /// \p SM for all definition parsing failures, if any, or Success otherwise.
343 Error defineCmdlineVariables(std::vector<std::string> &CmdlineDefines,
344 SourceMgr &SM);
346 /// Create @LINE pseudo variable. Value is set when pattern are being
347 /// matched.
348 void createLineVariable();
350 /// Undefines local variables (variables whose name does not start with a '$'
351 /// sign), i.e. removes them from GlobalVariableTable and from
352 /// GlobalNumericVariableTable and also clears the value of numeric
353 /// variables.
354 void clearLocalVars();
356 private:
357 /// Makes a new numeric variable and registers it for destruction when the
358 /// context is destroyed.
359 template <class... Types>
360 FileCheckNumericVariable *makeNumericVariable(Types... args);
362 /// Makes a new string substitution and registers it for destruction when the
363 /// context is destroyed.
364 FileCheckSubstitution *makeStringSubstitution(StringRef VarName,
365 size_t InsertIdx);
367 /// Makes a new numeric substitution and registers it for destruction when
368 /// the context is destroyed.
369 FileCheckSubstitution *
370 makeNumericSubstitution(StringRef ExpressionStr,
371 std::unique_ptr<FileCheckExpressionAST> ExpressionAST,
372 size_t InsertIdx);
375 /// Class to represent an error holding a diagnostic with location information
376 /// used when printing it.
377 class FileCheckErrorDiagnostic : public ErrorInfo<FileCheckErrorDiagnostic> {
378 private:
379 SMDiagnostic Diagnostic;
381 public:
382 static char ID;
384 FileCheckErrorDiagnostic(SMDiagnostic &&Diag) : Diagnostic(Diag) {}
386 std::error_code convertToErrorCode() const override {
387 return inconvertibleErrorCode();
390 /// Print diagnostic associated with this error when printing the error.
391 void log(raw_ostream &OS) const override { Diagnostic.print(nullptr, OS); }
393 static Error get(const SourceMgr &SM, SMLoc Loc, const Twine &ErrMsg) {
394 return make_error<FileCheckErrorDiagnostic>(
395 SM.GetMessage(Loc, SourceMgr::DK_Error, ErrMsg));
398 static Error get(const SourceMgr &SM, StringRef Buffer, const Twine &ErrMsg) {
399 return get(SM, SMLoc::getFromPointer(Buffer.data()), ErrMsg);
403 class FileCheckNotFoundError : public ErrorInfo<FileCheckNotFoundError> {
404 public:
405 static char ID;
407 std::error_code convertToErrorCode() const override {
408 return inconvertibleErrorCode();
411 /// Print diagnostic associated with this error when printing the error.
412 void log(raw_ostream &OS) const override {
413 OS << "String not found in input";
417 class FileCheckPattern {
418 SMLoc PatternLoc;
420 /// A fixed string to match as the pattern or empty if this pattern requires
421 /// a regex match.
422 StringRef FixedStr;
424 /// A regex string to match as the pattern or empty if this pattern requires
425 /// a fixed string to match.
426 std::string RegExStr;
428 /// Entries in this vector represent a substitution of a string variable or
429 /// an expression in the RegExStr regex at match time. For example, in the
430 /// case of a CHECK directive with the pattern "foo[[bar]]baz[[#N+1]]",
431 /// RegExStr will contain "foobaz" and we'll get two entries in this vector
432 /// that tells us to insert the value of string variable "bar" at offset 3
433 /// and the value of expression "N+1" at offset 6.
434 std::vector<FileCheckSubstitution *> Substitutions;
436 /// Maps names of string variables defined in a pattern to the number of
437 /// their parenthesis group in RegExStr capturing their last definition.
439 /// E.g. for the pattern "foo[[bar:.*]]baz([[bar]][[QUUX]][[bar:.*]])",
440 /// RegExStr will be "foo(.*)baz(\1<quux value>(.*))" where <quux value> is
441 /// the value captured for QUUX on the earlier line where it was defined, and
442 /// VariableDefs will map "bar" to the third parenthesis group which captures
443 /// the second definition of "bar".
445 /// Note: uses std::map rather than StringMap to be able to get the key when
446 /// iterating over values.
447 std::map<StringRef, unsigned> VariableDefs;
449 /// Structure representing the definition of a numeric variable in a pattern.
450 /// It holds the pointer to the class representing the numeric variable whose
451 /// value is being defined and the number of the parenthesis group in
452 /// RegExStr to capture that value.
453 struct FileCheckNumericVariableMatch {
454 /// Pointer to class representing the numeric variable whose value is being
455 /// defined.
456 FileCheckNumericVariable *DefinedNumericVariable;
458 /// Number of the parenthesis group in RegExStr that captures the value of
459 /// this numeric variable definition.
460 unsigned CaptureParenGroup;
463 /// Holds the number of the parenthesis group in RegExStr and pointer to the
464 /// corresponding FileCheckNumericVariable class instance of all numeric
465 /// variable definitions. Used to set the matched value of all those
466 /// variables.
467 StringMap<FileCheckNumericVariableMatch> NumericVariableDefs;
469 /// Pointer to a class instance holding the global state shared by all
470 /// patterns:
471 /// - separate tables with the values of live string and numeric variables
472 /// respectively at the start of any given CHECK line;
473 /// - table holding whether a string variable has been defined at any given
474 /// point during the parsing phase.
475 FileCheckPatternContext *Context;
477 Check::FileCheckType CheckTy;
479 /// Line number for this CHECK pattern. Used to determine whether a variable
480 /// definition is made on an earlier line to the one with this CHECK.
481 size_t LineNumber;
483 public:
484 FileCheckPattern(Check::FileCheckType Ty, FileCheckPatternContext *Context,
485 size_t Line)
486 : Context(Context), CheckTy(Ty), LineNumber(Line) {}
488 /// \returns the location in source code.
489 SMLoc getLoc() const { return PatternLoc; }
491 /// \returns the pointer to the global state for all patterns in this
492 /// FileCheck instance.
493 FileCheckPatternContext *getContext() const { return Context; }
495 /// \returns whether \p C is a valid first character for a variable name.
496 static bool isValidVarNameStart(char C);
498 /// Parsing information about a variable.
499 struct VariableProperties {
500 StringRef Name;
501 bool IsPseudo;
504 /// Parses the string at the start of \p Str for a variable name. \returns
505 /// a VariableProperties structure holding the variable name and whether it
506 /// is the name of a pseudo variable, or an error holding a diagnostic
507 /// against \p SM if parsing fail. If parsing was successful, also strips
508 /// \p Str from the variable name.
509 static Expected<VariableProperties> parseVariable(StringRef &Str,
510 const SourceMgr &SM);
511 /// Parses \p Expr for the name of a numeric variable to be defined at line
512 /// \p LineNumber. \returns a pointer to the class instance representing that
513 /// variable, creating it if needed, or an error holding a diagnostic against
514 /// \p SM should defining such a variable be invalid.
515 static Expected<FileCheckNumericVariable *>
516 parseNumericVariableDefinition(StringRef &Expr,
517 FileCheckPatternContext *Context,
518 size_t LineNumber, const SourceMgr &SM);
519 /// Parses \p Expr for a numeric substitution block. Parameter
520 /// \p IsLegacyLineExpr indicates whether \p Expr should be a legacy @LINE
521 /// expression. \returns a pointer to the class instance representing the AST
522 /// of the expression whose value must be substituted, or an error holding a
523 /// diagnostic against \p SM if parsing fails. If substitution was
524 /// successful, sets \p DefinedNumericVariable to point to the class
525 /// representing the numeric variable being defined in this numeric
526 /// substitution block, or None if this block does not define any variable.
527 Expected<std::unique_ptr<FileCheckExpressionAST>>
528 parseNumericSubstitutionBlock(
529 StringRef Expr,
530 Optional<FileCheckNumericVariable *> &DefinedNumericVariable,
531 bool IsLegacyLineExpr, const SourceMgr &SM) const;
532 /// Parses the pattern in \p PatternStr and initializes this FileCheckPattern
533 /// instance accordingly.
535 /// \p Prefix provides which prefix is being matched, \p Req describes the
536 /// global options that influence the parsing such as whitespace
537 /// canonicalization, \p SM provides the SourceMgr used for error reports.
538 /// \returns true in case of an error, false otherwise.
539 bool parsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
540 const FileCheckRequest &Req);
541 /// Matches the pattern string against the input buffer \p Buffer
543 /// \returns the position that is matched or an error indicating why matching
544 /// failed. If there is a match, updates \p MatchLen with the size of the
545 /// matched string.
547 /// The GlobalVariableTable StringMap in the FileCheckPatternContext class
548 /// instance provides the current values of FileCheck string variables and
549 /// is updated if this match defines new values. Likewise, the
550 /// GlobalNumericVariableTable StringMap in the same class provides the
551 /// current values of FileCheck numeric variables and is updated if this
552 /// match defines new numeric values.
553 Expected<size_t> match(StringRef Buffer, size_t &MatchLen,
554 const SourceMgr &SM) const;
555 /// Prints the value of successful substitutions or the name of the undefined
556 /// string or numeric variables preventing a successful substitution.
557 void printSubstitutions(const SourceMgr &SM, StringRef Buffer,
558 SMRange MatchRange = None) const;
559 void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
560 std::vector<FileCheckDiag> *Diags) const;
562 bool hasVariable() const {
563 return !(Substitutions.empty() && VariableDefs.empty());
566 Check::FileCheckType getCheckTy() const { return CheckTy; }
568 int getCount() const { return CheckTy.getCount(); }
570 private:
571 bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
572 void AddBackrefToRegEx(unsigned BackrefNum);
573 /// Computes an arbitrary estimate for the quality of matching this pattern
574 /// at the start of \p Buffer; a distance of zero should correspond to a
575 /// perfect match.
576 unsigned computeMatchDistance(StringRef Buffer) const;
577 /// Finds the closing sequence of a regex variable usage or definition.
579 /// \p Str has to point in the beginning of the definition (right after the
580 /// opening sequence). \p SM holds the SourceMgr used for error repporting.
581 /// \returns the offset of the closing sequence within Str, or npos if it
582 /// was not found.
583 size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
585 /// Parses \p Name as a (pseudo if \p IsPseudo is true) numeric variable use.
586 /// \returns the pointer to the class instance representing that variable if
587 /// successful, or an error holding a diagnostic against \p SM otherwise.
588 Expected<std::unique_ptr<FileCheckNumericVariableUse>>
589 parseNumericVariableUse(StringRef Name, bool IsPseudo,
590 const SourceMgr &SM) const;
591 enum class AllowedOperand { LineVar, Literal, Any };
592 /// Parses \p Expr for use of a numeric operand. Accepts both literal values
593 /// and numeric variables, depending on the value of \p AO. \returns the
594 /// class representing that operand in the AST of the expression or an error
595 /// holding a diagnostic against \p SM otherwise.
596 Expected<std::unique_ptr<FileCheckExpressionAST>>
597 parseNumericOperand(StringRef &Expr, AllowedOperand AO,
598 const SourceMgr &SM) const;
599 /// Parses \p Expr for a binary operation. The left operand of this binary
600 /// operation is given in \p LeftOp and \p IsLegacyLineExpr indicates whether
601 /// we are parsing a legacy @LINE expression. \returns the class representing
602 /// the binary operation in the AST of the expression, or an error holding a
603 /// diagnostic against \p SM otherwise.
604 Expected<std::unique_ptr<FileCheckExpressionAST>>
605 parseBinop(StringRef &Expr, std::unique_ptr<FileCheckExpressionAST> LeftOp,
606 bool IsLegacyLineExpr, const SourceMgr &SM) const;
609 //===----------------------------------------------------------------------===//
610 /// Summary of a FileCheck diagnostic.
611 //===----------------------------------------------------------------------===//
613 struct FileCheckDiag {
614 /// What is the FileCheck directive for this diagnostic?
615 Check::FileCheckType CheckTy;
616 /// Where is the FileCheck directive for this diagnostic?
617 unsigned CheckLine, CheckCol;
618 /// What type of match result does this diagnostic describe?
620 /// A directive's supplied pattern is said to be either expected or excluded
621 /// depending on whether the pattern must have or must not have a match in
622 /// order for the directive to succeed. For example, a CHECK directive's
623 /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
624 /// All match result types whose names end with "Excluded" are for excluded
625 /// patterns, and all others are for expected patterns.
627 /// There might be more than one match result for a single pattern. For
628 /// example, there might be several discarded matches
629 /// (MatchFoundButDiscarded) before either a good match
630 /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
631 /// and there might be a fuzzy match (MatchFuzzy) after the latter.
632 enum MatchType {
633 /// Indicates a good match for an expected pattern.
634 MatchFoundAndExpected,
635 /// Indicates a match for an excluded pattern.
636 MatchFoundButExcluded,
637 /// Indicates a match for an expected pattern, but the match is on the
638 /// wrong line.
639 MatchFoundButWrongLine,
640 /// Indicates a discarded match for an expected pattern.
641 MatchFoundButDiscarded,
642 /// Indicates no match for an excluded pattern.
643 MatchNoneAndExcluded,
644 /// Indicates no match for an expected pattern, but this might follow good
645 /// matches when multiple matches are expected for the pattern, or it might
646 /// follow discarded matches for the pattern.
647 MatchNoneButExpected,
648 /// Indicates a fuzzy match that serves as a suggestion for the next
649 /// intended match for an expected pattern with too few or no good matches.
650 MatchFuzzy,
651 } MatchTy;
652 /// The search range if MatchTy is MatchNoneAndExcluded or
653 /// MatchNoneButExpected, or the match range otherwise.
654 unsigned InputStartLine;
655 unsigned InputStartCol;
656 unsigned InputEndLine;
657 unsigned InputEndCol;
658 FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy,
659 SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange);
662 //===----------------------------------------------------------------------===//
663 // Check Strings.
664 //===----------------------------------------------------------------------===//
666 /// A check that we found in the input file.
667 struct FileCheckString {
668 /// The pattern to match.
669 FileCheckPattern Pat;
671 /// Which prefix name this check matched.
672 StringRef Prefix;
674 /// The location in the match file that the check string was specified.
675 SMLoc Loc;
677 /// All of the strings that are disallowed from occurring between this match
678 /// string and the previous one (or start of file).
679 std::vector<FileCheckPattern> DagNotStrings;
681 FileCheckString(const FileCheckPattern &P, StringRef S, SMLoc L)
682 : Pat(P), Prefix(S), Loc(L) {}
684 /// Matches check string and its "not strings" and/or "dag strings".
685 size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
686 size_t &MatchLen, FileCheckRequest &Req,
687 std::vector<FileCheckDiag> *Diags) const;
689 /// Verifies that there is a single line in the given \p Buffer. Errors are
690 /// reported against \p SM.
691 bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
692 /// Verifies that there is no newline in the given \p Buffer. Errors are
693 /// reported against \p SM.
694 bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
695 /// Verifies that none of the strings in \p NotStrings are found in the given
696 /// \p Buffer. Errors are reported against \p SM and diagnostics recorded in
697 /// \p Diags according to the verbosity level set in \p Req.
698 bool CheckNot(const SourceMgr &SM, StringRef Buffer,
699 const std::vector<const FileCheckPattern *> &NotStrings,
700 const FileCheckRequest &Req,
701 std::vector<FileCheckDiag> *Diags) const;
702 /// Matches "dag strings" and their mixed "not strings".
703 size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
704 std::vector<const FileCheckPattern *> &NotStrings,
705 const FileCheckRequest &Req,
706 std::vector<FileCheckDiag> *Diags) const;
709 /// FileCheck class takes the request and exposes various methods that
710 /// use information from the request.
711 class FileCheck {
712 FileCheckRequest Req;
713 FileCheckPatternContext PatternContext;
715 public:
716 FileCheck(FileCheckRequest Req) : Req(Req) {}
718 // Combines the check prefixes into a single regex so that we can efficiently
719 // scan for any of the set.
721 // The semantics are that the longest-match wins which matches our regex
722 // library.
723 Regex buildCheckPrefixRegex();
725 /// Reads the check file from \p Buffer and records the expected strings it
726 /// contains in the \p CheckStrings vector. Errors are reported against
727 /// \p SM.
729 /// Only expected strings whose prefix is one of those listed in \p PrefixRE
730 /// are recorded. \returns true in case of an error, false otherwise.
731 bool ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
732 std::vector<FileCheckString> &CheckStrings);
734 bool ValidateCheckPrefixes();
736 /// Canonicalizes whitespaces in the file. Line endings are replaced with
737 /// UNIX-style '\n'.
738 StringRef CanonicalizeFile(MemoryBuffer &MB,
739 SmallVectorImpl<char> &OutputBuffer);
741 /// Checks the input to FileCheck provided in the \p Buffer against the
742 /// \p CheckStrings read from the check file and record diagnostics emitted
743 /// in \p Diags. Errors are recorded against \p SM.
745 /// \returns false if the input fails to satisfy the checks.
746 bool CheckInput(SourceMgr &SM, StringRef Buffer,
747 ArrayRef<FileCheckString> CheckStrings,
748 std::vector<FileCheckDiag> *Diags = nullptr);
750 } // namespace llvm
751 #endif