LLVM 20.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
24namespace llvm {
25class MemoryBuffer;
26class SourceMgr;
27template <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;
41 bool EnableVarScope = false;
43 bool Verbose = false;
44 bool VerboseVerbose = false;
45};
46
47namespace Check {
48
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.
77 Size
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
86public:
87 FileCheckType(FileCheckKind Kind = CheckNone) : Kind(Kind), Count(1) {}
88 FileCheckType(const FileCheckType &) = default;
90
91 operator FileCheckKind() const { return Kind; }
92
93 int getCount() const { return Count; }
95
96 bool isLiteralMatch() const {
98 }
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.
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.
161 /// The search range if MatchTy starts with MatchNone, or the match range
162 /// otherwise.
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;
172 StringRef Note = "");
173};
174
176struct FileCheckString;
177
178/// FileCheck class takes the request and exposes various methods that
179/// use information from the request.
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
186public:
187 explicit FileCheck(FileCheckRequest Req);
189
190 /// Reads the check file from \p Buffer and records the expected strings it
191 /// contains. Errors are reported against \p SM.
192 ///
193 /// If \p ImpPatBufferIDRange, then the range (inclusive start, exclusive end)
194 /// of IDs for source buffers added to \p SM for implicit patterns are
195 /// recorded in it. The range is empty if there are none.
196 bool
198 std::pair<unsigned, unsigned> *ImpPatBufferIDRange = nullptr);
199
201
202 /// Canonicalizes whitespaces in the file. Line endings are replaced with
203 /// UNIX-style '\n'.
206
207 /// Checks the input to FileCheck provided in the \p Buffer against the
208 /// expected strings read from the check file and record diagnostics emitted
209 /// in \p Diags. Errors are recorded against \p SM.
210 ///
211 /// \returns false if the input fails to satisfy the checks.
212 bool checkInput(SourceMgr &SM, StringRef Buffer,
213 std::vector<FileCheckDiag> *Diags = nullptr);
214};
215
216} // namespace llvm
217
218#endif
#define Check(C,...)
std::string getDescription(StringRef Prefix) const
Definition: FileCheck.cpp:1494
FileCheckType(FileCheckKind Kind=CheckNone)
Definition: FileCheck.h:87
FileCheckType & operator=(const FileCheckType &)=default
bool isLiteralMatch() const
Definition: FileCheck.h:96
std::string getModifiersDescription() const
Definition: FileCheck.cpp:1482
FileCheckType & setLiteralMatch(bool Literal=true)
Definition: FileCheck.h:99
FileCheckType & setCount(int C)
Definition: FileCheck.cpp:1474
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:180
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:1774
StringRef CanonicalizeFile(MemoryBuffer &MB, SmallVectorImpl< char > &OutputBuffer)
Canonicalizes whitespaces in the file.
Definition: FileCheck.cpp:1428
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:2675
bool ValidateCheckPrefixes()
Definition: FileCheck.cpp:2493
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
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:573
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
FileCheckKindModifier
Definition: FileCheck.h:72
@ ModifierLiteral
Modifies directive to perform literal match.
Definition: FileCheck.h:74
@ CheckBadNot
Marks when parsing found a -NOT check combined with another CHECK suffix.
Definition: FileCheck.h:66
@ CheckBadCount
Marks when parsing found a -COUNT directive with invalid count value.
Definition: FileCheck.h:69
@ CheckEOF
Indicates the pattern only matches the end of file.
Definition: FileCheck.h:63
@ CheckMisspelled
Definition: FileCheck.h:51
@ CheckComment
Definition: FileCheck.h:59
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Summary of a FileCheck diagnostic.
Definition: FileCheck.h:113
std::string Note
A note to replace the one normally indicated by MatchTy, or the empty string if none.
Definition: FileCheck.h:169
unsigned InputStartCol
Definition: FileCheck.h:164
enum llvm::FileCheckDiag::MatchType MatchTy
unsigned InputStartLine
The search range if MatchTy starts with MatchNone, or the match range otherwise.
Definition: FileCheck.h:163
unsigned InputEndLine
Definition: FileCheck.h:165
Check::FileCheckType CheckTy
What is the FileCheck directive for this diagnostic?
Definition: FileCheck.h:115
unsigned InputEndCol
Definition: FileCheck.h:166
MatchType
What type of match result does this diagnostic describe?
Definition: FileCheck.h:130
@ MatchFoundButWrongLine
Indicates a match for an expected pattern, but the match is on the wrong line.
Definition: FileCheck.h:137
@ MatchNoneAndExcluded
Indicates no match for an excluded pattern.
Definition: FileCheck.h:147
@ MatchFoundButExcluded
Indicates a match for an excluded pattern.
Definition: FileCheck.h:134
@ MatchFuzzy
Indicates a fuzzy match that serves as a suggestion for the next intended match for an expected patte...
Definition: FileCheck.h:159
@ MatchFoundErrorNote
Indicates an error while processing a match after the match was found for an expected or excluded pat...
Definition: FileCheck.h:145
@ MatchFoundButDiscarded
Indicates a discarded match for an expected pattern.
Definition: FileCheck.h:139
@ MatchNoneForInvalidPattern
Indicates no match due to an expected or excluded pattern that has proven to be invalid at match time...
Definition: FileCheck.h:156
@ MatchFoundAndExpected
Indicates a good match for an expected pattern.
Definition: FileCheck.h:132
@ MatchNoneButExpected
Indicates no match for an expected pattern, but this might follow good matches when multiple matches ...
Definition: FileCheck.h:151
SMLoc CheckLoc
Where is the FileCheck directive for this diagnostic?
Definition: FileCheck.h:117
Contains info about various FileCheck options.
Definition: FileCheck.h:30
std::vector< StringRef > GlobalDefines
Definition: FileCheck.h:35
std::vector< StringRef > ImplicitCheckNot
Definition: FileCheck.h:34
std::vector< StringRef > CommentPrefixes
Definition: FileCheck.h:32
std::vector< StringRef > CheckPrefixes
Definition: FileCheck.h:31
bool AllowDeprecatedDagOverlap
Definition: FileCheck.h:42
A check that we found in the input file.