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