LLVM  15.0.0git
FileCheck.h
Go to the documentation of this file.
1 //==-- llvm/FileCheck/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
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_FILECHECK_FILECHECK_H
14 #define LLVM_FILECHECK_FILECHECK_H
15 
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/Regex.h"
18 #include "llvm/Support/SMLoc.h"
19 #include <bitset>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 namespace llvm {
25 class MemoryBuffer;
26 class SourceMgr;
27 template <typename T> class SmallVectorImpl;
28 
29 /// Contains info about various FileCheck options.
31  std::vector<StringRef> CheckPrefixes;
32  std::vector<StringRef> CommentPrefixes;
34  std::vector<StringRef> ImplicitCheckNot;
35  std::vector<StringRef> GlobalDefines;
36  bool AllowEmptyInput = false;
37  bool AllowUnusedPrefixes = false;
38  bool MatchFullLines = false;
39  bool IgnoreCase = false;
40  bool IsDefaultCheckPrefix = false;
41  bool EnableVarScope = false;
43  bool Verbose = false;
44  bool VerboseVerbose = false;
45 };
46 
47 namespace Check {
48 
50  CheckNone = 0,
59 
60  /// Indicates the pattern only matches the end of file. This is used for
61  /// trailing CHECK-NOTs.
63 
64  /// Marks when parsing found a -NOT check combined with another CHECK suffix.
66 
67  /// Marks when parsing found a -COUNT directive with invalid count value.
69 };
70 
72  /// Modifies directive to perform literal match.
74 
75  // The number of modifier.
77 };
78 
80  FileCheckKind Kind;
81  int Count; ///< optional Count for some checks
82  /// Modifers for the check directive.
83  std::bitset<FileCheckKindModifier::Size> Modifiers;
84 
85 public:
86  FileCheckType(FileCheckKind Kind = CheckNone) : Kind(Kind), Count(1) {}
87  FileCheckType(const FileCheckType &) = default;
88  FileCheckType &operator=(const FileCheckType &) = default;
89 
90  operator FileCheckKind() const { return Kind; }
91 
92  int getCount() const { return Count; }
93  FileCheckType &setCount(int C);
94 
95  bool isLiteralMatch() const {
97  }
98  FileCheckType &setLiteralMatch(bool Literal = true) {
99  Modifiers.set(FileCheckKindModifier::ModifierLiteral, Literal);
100  return *this;
101  }
102 
103  // \returns a description of \p Prefix.
104  std::string getDescription(StringRef Prefix) const;
105 
106  // \returns a description of \p Modifiers.
107  std::string getModifiersDescription() const;
108 };
109 } // namespace Check
110 
111 /// Summary of a FileCheck diagnostic.
113  /// What is the FileCheck directive for this diagnostic?
115  /// Where is the FileCheck directive for this diagnostic?
117  /// What type of match result does this diagnostic describe?
118  ///
119  /// A directive's supplied pattern is said to be either expected or excluded
120  /// depending on whether the pattern must have or must not have a match in
121  /// order for the directive to succeed. For example, a CHECK directive's
122  /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
123  ///
124  /// There might be more than one match result for a single pattern. For
125  /// example, there might be several discarded matches
126  /// (MatchFoundButDiscarded) before either a good match
127  /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
128  /// and there might be a fuzzy match (MatchFuzzy) after the latter.
129  enum MatchType {
130  /// Indicates a good match for an expected pattern.
132  /// Indicates a match for an excluded pattern.
134  /// Indicates a match for an expected pattern, but the match is on the
135  /// wrong line.
137  /// Indicates a discarded match for an expected pattern.
139  /// Indicates an error while processing a match after the match was found
140  /// for an expected or excluded pattern. The error is specified by \c Note,
141  /// to which it should be appropriate to prepend "error: " later. The full
142  /// match itself should be recorded in a preceding diagnostic of a different
143  /// \c MatchFound match type.
145  /// Indicates no match for an excluded pattern.
147  /// Indicates no match for an expected pattern, but this might follow good
148  /// matches when multiple matches are expected for the pattern, or it might
149  /// follow discarded matches for the pattern.
151  /// Indicates no match due to an expected or excluded pattern that has
152  /// proven to be invalid at match time. The exact problems are usually
153  /// reported in subsequent diagnostics of the same match type but with
154  /// \c Note set.
156  /// Indicates a fuzzy match that serves as a suggestion for the next
157  /// intended match for an expected pattern with too few or no good matches.
159  } MatchTy;
160  /// The search range if MatchTy starts with MatchNone, or the match range
161  /// otherwise.
162  unsigned InputStartLine;
163  unsigned InputStartCol;
164  unsigned InputEndLine;
165  unsigned InputEndCol;
166  /// A note to replace the one normally indicated by MatchTy, or the empty
167  /// string if none.
168  std::string Note;
170  SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange,
171  StringRef Note = "");
172 };
173 
175 struct FileCheckString;
176 
177 /// FileCheck class takes the request and exposes various methods that
178 /// use information from the request.
179 class FileCheck {
180  FileCheckRequest Req;
181  std::unique_ptr<FileCheckPatternContext> PatternContext;
182  // C++17 TODO: make this a plain std::vector.
183  std::unique_ptr<std::vector<FileCheckString>> CheckStrings;
184 
185 public:
186  explicit FileCheck(FileCheckRequest Req);
187  ~FileCheck();
188 
189  // Combines the check prefixes into a single regex so that we can efficiently
190  // scan for any of the set.
191  //
192  // The semantics are that the longest-match wins which matches our regex
193  // library.
195 
196  /// Reads the check file from \p Buffer and records the expected strings it
197  /// contains. Errors are reported against \p SM.
198  ///
199  /// Only expected strings whose prefix is one of those listed in \p PrefixRE
200  /// are recorded. \returns true in case of an error, false otherwise.
201  ///
202  /// If \p ImpPatBufferIDRange, then the range (inclusive start, exclusive end)
203  /// of IDs for source buffers added to \p SM for implicit patterns are
204  /// recorded in it. The range is empty if there are none.
205  bool
206  readCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
207  std::pair<unsigned, unsigned> *ImpPatBufferIDRange = nullptr);
208 
209  bool ValidateCheckPrefixes();
210 
211  /// Canonicalizes whitespaces in the file. Line endings are replaced with
212  /// UNIX-style '\n'.
215 
216  /// Checks the input to FileCheck provided in the \p Buffer against the
217  /// expected strings read from the check file and record diagnostics emitted
218  /// in \p Diags. Errors are recorded against \p SM.
219  ///
220  /// \returns false if the input fails to satisfy the checks.
221  bool checkInput(SourceMgr &SM, StringRef Buffer,
222  std::vector<FileCheckDiag> *Diags = nullptr);
223 };
224 
225 } // namespace llvm
226 
227 #endif
llvm::Check::Size
@ Size
Definition: FileCheck.h:76
llvm::FileCheckDiag::MatchNoneAndExcluded
@ MatchNoneAndExcluded
Indicates no match for an excluded pattern.
Definition: FileCheck.h:146
llvm::FileCheckDiag
Summary of a FileCheck diagnostic.
Definition: FileCheck.h:112
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::FileCheckRequest::AllowDeprecatedDagOverlap
bool AllowDeprecatedDagOverlap
Definition: FileCheck.h:42
llvm::FileCheckRequest::GlobalDefines
std::vector< StringRef > GlobalDefines
Definition: FileCheck.h:35
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:160
llvm::FileCheckRequest::NoCanonicalizeWhiteSpace
bool NoCanonicalizeWhiteSpace
Definition: FileCheck.h:33
llvm::FileCheckRequest::IsDefaultCheckPrefix
bool IsDefaultCheckPrefix
Definition: FileCheck.h:40
llvm::FileCheckRequest::AllowEmptyInput
bool AllowEmptyInput
Definition: FileCheck.h:36
llvm::Check::FileCheckKindModifier
FileCheckKindModifier
Definition: FileCheck.h:71
llvm::Check::ModifierLiteral
@ ModifierLiteral
Modifies directive to perform literal match.
Definition: FileCheck.h:73
StringRef.h
llvm::FileCheckDiag::MatchTy
enum llvm::FileCheckDiag::MatchType MatchTy
llvm::FileCheckRequest::Verbose
bool Verbose
Definition: FileCheck.h:43
llvm::Check::CheckSame
@ CheckSame
Definition: FileCheck.h:53
llvm::Check::CheckLabel
@ CheckLabel
Definition: FileCheck.h:56
llvm::FileCheck::~FileCheck
~FileCheck()
llvm::FileCheckDiag::InputEndCol
unsigned InputEndCol
Definition: FileCheck.h:165
llvm::Check::FileCheckType::setCount
FileCheckType & setCount(int C)
llvm::FileCheckDiag::FileCheckDiag
FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy, SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange, StringRef Note="")
llvm::FileCheck::checkInput
bool checkInput(SourceMgr &SM, StringRef Buffer, std::vector< FileCheckDiag > *Diags=nullptr)
Checks the input to FileCheck provided in the Buffer against the expected strings read from the check...
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
llvm::Check::FileCheckType::operator=
FileCheckType & operator=(const FileCheckType &)=default
llvm::FileCheck::buildCheckPrefixRegex
Regex buildCheckPrefixRegex()
llvm::FileCheckDiag::InputStartLine
unsigned InputStartLine
The search range if MatchTy starts with MatchNone, or the match range otherwise.
Definition: FileCheck.h:162
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::Check::FileCheckType::getCount
int getCount() const
Definition: FileCheck.h:92
llvm::FileCheckDiag::InputStartCol
unsigned InputStartCol
Definition: FileCheck.h:163
llvm::FileCheck::ValidateCheckPrefixes
bool ValidateCheckPrefixes()
llvm::FileCheckRequest::CheckPrefixes
std::vector< StringRef > CheckPrefixes
Definition: FileCheck.h:31
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::FileCheckDiag::CheckLoc
SMLoc CheckLoc
Where is the FileCheck directive for this diagnostic?
Definition: FileCheck.h:116
Check
#define Check(C,...)
Definition: Lint.cpp:170
llvm::FileCheckDiag::MatchFoundAndExpected
@ MatchFoundAndExpected
Indicates a good match for an expected pattern.
Definition: FileCheck.h:131
llvm::Check::CheckNot
@ CheckNot
Definition: FileCheck.h:54
llvm::Check::FileCheckType::FileCheckType
FileCheckType(FileCheckKind Kind=CheckNone)
Definition: FileCheck.h:86
llvm::FileCheckDiag::MatchFoundErrorNote
@ MatchFoundErrorNote
Indicates an error while processing a match after the match was found for an expected or excluded pat...
Definition: FileCheck.h:144
SMLoc.h
llvm::FileCheckDiag::Note
std::string Note
A note to replace the one normally indicated by MatchTy, or the empty string if none.
Definition: FileCheck.h:168
llvm::FileCheckPatternContext
Class holding the Pattern global state, shared by all patterns: tables holding values of variables an...
Definition: FileCheckImpl.h:459
llvm::FileCheckDiag::MatchFoundButWrongLine
@ MatchFoundButWrongLine
Indicates a match for an expected pattern, but the match is on the wrong line.
Definition: FileCheck.h:136
llvm::Check::FileCheckType::getDescription
std::string getDescription(StringRef Prefix) const
llvm::FileCheckDiag::MatchType
MatchType
What type of match result does this diagnostic describe?
Definition: FileCheck.h:129
llvm::Check::FileCheckType
Definition: FileCheck.h:79
llvm::FileCheck::readCheckFile
bool readCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE, std::pair< unsigned, unsigned > *ImpPatBufferIDRange=nullptr)
Reads the check file from Buffer and records the expected strings it contains.
llvm::FileCheckString
A check that we found in the input file.
Definition: FileCheckImpl.h:865
llvm::Check::CheckNext
@ CheckNext
Definition: FileCheck.h:52
llvm::FileCheckDiag::MatchFoundButExcluded
@ MatchFoundButExcluded
Indicates a match for an excluded pattern.
Definition: FileCheck.h:133
llvm::Check::FileCheckType::setLiteralMatch
FileCheckType & setLiteralMatch(bool Literal=true)
Definition: FileCheck.h:98
Regex.h
llvm::Check::CheckBadCount
@ CheckBadCount
Marks when parsing found a -COUNT directive with invalid count value.
Definition: FileCheck.h:68
llvm::FileCheck::FileCheck
FileCheck(FileCheckRequest Req)
llvm::Check::CheckBadNot
@ CheckBadNot
Marks when parsing found a -NOT check combined with another CHECK suffix.
Definition: FileCheck.h:65
llvm::FileCheckDiag::MatchNoneForInvalidPattern
@ MatchNoneForInvalidPattern
Indicates no match due to an expected or excluded pattern that has proven to be invalid at match time...
Definition: FileCheck.h:155
llvm::FileCheck
FileCheck class takes the request and exposes various methods that use information from the request.
Definition: FileCheck.h:179
llvm::FileCheckRequest::MatchFullLines
bool MatchFullLines
Definition: FileCheck.h:38
llvm::FileCheck::CanonicalizeFile
StringRef CanonicalizeFile(MemoryBuffer &MB, SmallVectorImpl< char > &OutputBuffer)
Canonicalizes whitespaces in the file.
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::SourceMgr
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
llvm::FileCheckDiag::MatchFuzzy
@ MatchFuzzy
Indicates a fuzzy match that serves as a suggestion for the next intended match for an expected patte...
Definition: FileCheck.h:158
llvm::FileCheckDiag::MatchNoneButExpected
@ MatchNoneButExpected
Indicates no match for an expected pattern, but this might follow good matches when multiple matches ...
Definition: FileCheck.h:150
llvm::FileCheckRequest::EnableVarScope
bool EnableVarScope
Definition: FileCheck.h:41
llvm::Check::CheckDAG
@ CheckDAG
Definition: FileCheck.h:55
llvm::Check::CheckEmpty
@ CheckEmpty
Definition: FileCheck.h:57
llvm::Check::FileCheckKind
FileCheckKind
Definition: FileCheck.h:49
llvm::Check::FileCheckType::getModifiersDescription
std::string getModifiersDescription() const
llvm::FileCheckDiag::CheckTy
Check::FileCheckType CheckTy
What is the FileCheck directive for this diagnostic?
Definition: FileCheck.h:114
llvm::FileCheckDiag::MatchFoundButDiscarded
@ MatchFoundButDiscarded
Indicates a discarded match for an expected pattern.
Definition: FileCheck.h:138
OutputBuffer
Definition: Utility.h:31
llvm::FileCheckRequest::CommentPrefixes
std::vector< StringRef > CommentPrefixes
Definition: FileCheck.h:32
llvm::FileCheckRequest::IgnoreCase
bool IgnoreCase
Definition: FileCheck.h:39
llvm::FileCheckRequest
Contains info about various FileCheck options.
Definition: FileCheck.h:30
llvm::FileCheckRequest::VerboseVerbose
bool VerboseVerbose
Definition: FileCheck.h:44
llvm::SMRange
Represents a range in source code.
Definition: SMLoc.h:48
llvm::Check::CheckPlain
@ CheckPlain
Definition: FileCheck.h:51
llvm::FileCheckDiag::InputEndLine
unsigned InputEndLine
Definition: FileCheck.h:164
llvm::SmallVectorImpl< char >
llvm::Check::CheckNone
@ CheckNone
Definition: FileCheck.h:50
llvm::Check::FileCheckType::isLiteralMatch
bool isLiteralMatch() const
Definition: FileCheck.h:95
llvm::Regex
Definition: Regex.h:28
llvm::FileCheckRequest::ImplicitCheckNot
std::vector< StringRef > ImplicitCheckNot
Definition: FileCheck.h:34
llvm::FileCheckRequest::AllowUnusedPrefixes
bool AllowUnusedPrefixes
Definition: FileCheck.h:37
llvm::Check::CheckEOF
@ CheckEOF
Indicates the pattern only matches the end of file.
Definition: FileCheck.h:62
llvm::Check::CheckComment
@ CheckComment
Definition: FileCheck.h:58