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,
60 
61  /// Indicates the pattern only matches the end of file. This is used for
62  /// trailing CHECK-NOTs.
64 
65  /// Marks when parsing found a -NOT check combined with another CHECK suffix.
67 
68  /// Marks when parsing found a -COUNT directive with invalid count value.
70 };
71 
73  /// Modifies directive to perform literal match.
75 
76  // The number of modifier.
78 };
79 
81  FileCheckKind Kind;
82  int Count; ///< optional Count for some checks
83  /// Modifers for the check directive.
84  std::bitset<FileCheckKindModifier::Size> Modifiers;
85 
86 public:
87  FileCheckType(FileCheckKind Kind = CheckNone) : Kind(Kind), Count(1) {}
88  FileCheckType(const FileCheckType &) = default;
89  FileCheckType &operator=(const FileCheckType &) = default;
90 
91  operator FileCheckKind() const { return Kind; }
92 
93  int getCount() const { return Count; }
94  FileCheckType &setCount(int C);
95 
96  bool isLiteralMatch() const {
98  }
99  FileCheckType &setLiteralMatch(bool Literal = true) {
100  Modifiers.set(FileCheckKindModifier::ModifierLiteral, Literal);
101  return *this;
102  }
103 
104  // \returns a description of \p Prefix.
105  std::string getDescription(StringRef Prefix) const;
106 
107  // \returns a description of \p Modifiers.
108  std::string getModifiersDescription() const;
109 };
110 } // namespace Check
111 
112 /// Summary of a FileCheck diagnostic.
114  /// What is the FileCheck directive for this diagnostic?
116  /// Where is the FileCheck directive for this diagnostic?
118  /// What type of match result does this diagnostic describe?
119  ///
120  /// A directive's supplied pattern is said to be either expected or excluded
121  /// depending on whether the pattern must have or must not have a match in
122  /// order for the directive to succeed. For example, a CHECK directive's
123  /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
124  ///
125  /// There might be more than one match result for a single pattern. For
126  /// example, there might be several discarded matches
127  /// (MatchFoundButDiscarded) before either a good match
128  /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
129  /// and there might be a fuzzy match (MatchFuzzy) after the latter.
130  enum MatchType {
131  /// Indicates a good match for an expected pattern.
133  /// Indicates a match for an excluded pattern.
135  /// Indicates a match for an expected pattern, but the match is on the
136  /// wrong line.
138  /// Indicates a discarded match for an expected pattern.
140  /// Indicates an error while processing a match after the match was found
141  /// for an expected or excluded pattern. The error is specified by \c Note,
142  /// to which it should be appropriate to prepend "error: " later. The full
143  /// match itself should be recorded in a preceding diagnostic of a different
144  /// \c MatchFound match type.
146  /// Indicates no match for an excluded pattern.
148  /// Indicates no match for an expected pattern, but this might follow good
149  /// matches when multiple matches are expected for the pattern, or it might
150  /// follow discarded matches for the pattern.
152  /// Indicates no match due to an expected or excluded pattern that has
153  /// proven to be invalid at match time. The exact problems are usually
154  /// reported in subsequent diagnostics of the same match type but with
155  /// \c Note set.
157  /// Indicates a fuzzy match that serves as a suggestion for the next
158  /// intended match for an expected pattern with too few or no good matches.
160  } MatchTy;
161  /// The search range if MatchTy starts with MatchNone, or the match range
162  /// otherwise.
163  unsigned InputStartLine;
164  unsigned InputStartCol;
165  unsigned InputEndLine;
166  unsigned InputEndCol;
167  /// A note to replace the one normally indicated by MatchTy, or the empty
168  /// string if none.
169  std::string Note;
171  SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange,
172  StringRef Note = "");
173 };
174 
176 struct FileCheckString;
177 
178 /// FileCheck class takes the request and exposes various methods that
179 /// use information from the request.
180 class FileCheck {
181  FileCheckRequest Req;
182  std::unique_ptr<FileCheckPatternContext> PatternContext;
183  // C++17 TODO: make this a plain std::vector.
184  std::unique_ptr<std::vector<FileCheckString>> CheckStrings;
185 
186 public:
187  explicit FileCheck(FileCheckRequest Req);
188  ~FileCheck();
189 
190  // Combines the check prefixes into a single regex so that we can efficiently
191  // scan for any of the set.
192  //
193  // The semantics are that the longest-match wins which matches our regex
194  // library.
196 
197  /// Reads the check file from \p Buffer and records the expected strings it
198  /// contains. Errors are reported against \p SM.
199  ///
200  /// Only expected strings whose prefix is one of those listed in \p PrefixRE
201  /// are recorded. \returns true in case of an error, false otherwise.
202  ///
203  /// If \p ImpPatBufferIDRange, then the range (inclusive start, exclusive end)
204  /// of IDs for source buffers added to \p SM for implicit patterns are
205  /// recorded in it. The range is empty if there are none.
206  bool
207  readCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
208  std::pair<unsigned, unsigned> *ImpPatBufferIDRange = nullptr);
209 
210  bool ValidateCheckPrefixes();
211 
212  /// Canonicalizes whitespaces in the file. Line endings are replaced with
213  /// UNIX-style '\n'.
216 
217  /// Checks the input to FileCheck provided in the \p Buffer against the
218  /// expected strings read from the check file and record diagnostics emitted
219  /// in \p Diags. Errors are recorded against \p SM.
220  ///
221  /// \returns false if the input fails to satisfy the checks.
222  bool checkInput(SourceMgr &SM, StringRef Buffer,
223  std::vector<FileCheckDiag> *Diags = nullptr);
224 };
225 
226 } // namespace llvm
227 
228 #endif
llvm::Check::Size
@ Size
Definition: FileCheck.h:77
llvm::FileCheckDiag::MatchNoneAndExcluded
@ MatchNoneAndExcluded
Indicates no match for an excluded pattern.
Definition: FileCheck.h:147
llvm::FileCheckDiag
Summary of a FileCheck diagnostic.
Definition: FileCheck.h:113
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:72
llvm::Check::ModifierLiteral
@ ModifierLiteral
Modifies directive to perform literal match.
Definition: FileCheck.h:74
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:54
llvm::Check::CheckLabel
@ CheckLabel
Definition: FileCheck.h:57
llvm::FileCheck::~FileCheck
~FileCheck()
llvm::FileCheckDiag::InputEndCol
unsigned InputEndCol
Definition: FileCheck.h:166
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:163
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::Check::FileCheckType::getCount
int getCount() const
Definition: FileCheck.h:93
llvm::FileCheckDiag::InputStartCol
unsigned InputStartCol
Definition: FileCheck.h:164
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:117
Check
#define Check(C,...)
Definition: Lint.cpp:170
llvm::FileCheckDiag::MatchFoundAndExpected
@ MatchFoundAndExpected
Indicates a good match for an expected pattern.
Definition: FileCheck.h:132
llvm::Check::CheckNot
@ CheckNot
Definition: FileCheck.h:55
llvm::Check::FileCheckType::FileCheckType
FileCheckType(FileCheckKind Kind=CheckNone)
Definition: FileCheck.h:87
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:145
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:169
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:137
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:130
llvm::Check::FileCheckType
Definition: FileCheck.h:80
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:53
llvm::Check::CheckMisspelled
@ CheckMisspelled
Definition: FileCheck.h:51
llvm::FileCheckDiag::MatchFoundButExcluded
@ MatchFoundButExcluded
Indicates a match for an excluded pattern.
Definition: FileCheck.h:134
llvm::Check::FileCheckType::setLiteralMatch
FileCheckType & setLiteralMatch(bool Literal=true)
Definition: FileCheck.h:99
Regex.h
llvm::Check::CheckBadCount
@ CheckBadCount
Marks when parsing found a -COUNT directive with invalid count value.
Definition: FileCheck.h:69
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:66
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:156
llvm::FileCheck
FileCheck class takes the request and exposes various methods that use information from the request.
Definition: FileCheck.h:180
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:159
llvm::FileCheckDiag::MatchNoneButExpected
@ MatchNoneButExpected
Indicates no match for an expected pattern, but this might follow good matches when multiple matches ...
Definition: FileCheck.h:151
llvm::FileCheckRequest::EnableVarScope
bool EnableVarScope
Definition: FileCheck.h:41
llvm::Check::CheckDAG
@ CheckDAG
Definition: FileCheck.h:56
llvm::Check::CheckEmpty
@ CheckEmpty
Definition: FileCheck.h:58
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:115
llvm::FileCheckDiag::MatchFoundButDiscarded
@ MatchFoundButDiscarded
Indicates a discarded match for an expected pattern.
Definition: FileCheck.h:139
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:52
llvm::FileCheckDiag::InputEndLine
unsigned InputEndLine
Definition: FileCheck.h:165
llvm::SmallVectorImpl< char >
llvm::Check::CheckNone
@ CheckNone
Definition: FileCheck.h:50
llvm::Check::FileCheckType::isLiteralMatch
bool isLiteralMatch() const
Definition: FileCheck.h:96
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:63
llvm::Check::CheckComment
@ CheckComment
Definition: FileCheck.h:59