1 //==-- llvm/Support/FileCheck.h ---------------------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \file This file has some utilities to use FileCheck as an API
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_FILECHECK_H
15 #define LLVM_SUPPORT_FILECHECK_H
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/Regex.h"
20 #include "llvm/Support/SourceMgr.h"
26 /// Contains info about various FileCheck options.
27 struct FileCheckRequest {
28 std::vector<std::string> CheckPrefixes;
29 bool NoCanonicalizeWhiteSpace = false;
30 std::vector<std::string> ImplicitCheckNot;
31 std::vector<std::string> GlobalDefines;
32 bool AllowEmptyInput = false;
33 bool MatchFullLines = false;
34 bool EnableVarScope = false;
35 bool AllowDeprecatedDagOverlap = false;
37 bool VerboseVerbose = false;
41 //===----------------------------------------------------------------------===//
42 // Pattern Handling Code.
43 //===----------------------------------------------------------------------===//
57 /// Indicates the pattern only matches the end of file. This is used for
58 /// trailing CHECK-NOTs.
61 /// Marks when parsing found a -NOT check combined with another CHECK suffix.
64 /// Marks when parsing found a -COUNT directive with invalid count value.
70 int Count; ///< optional Count for some checks
73 FileCheckType(FileCheckKind Kind = CheckNone) : Kind(Kind), Count(1) {}
74 FileCheckType(const FileCheckType &) = default;
76 operator FileCheckKind() const { return Kind; }
78 int getCount() const { return Count; }
79 FileCheckType &setCount(int C);
81 std::string getDescription(StringRef Prefix) const;
87 class FileCheckPattern {
90 /// A fixed string to match as the pattern or empty if this pattern requires
94 /// A regex string to match as the pattern or empty if this pattern requires
95 /// a fixed string to match.
98 /// Entries in this vector map to uses of a variable in the pattern, e.g.
99 /// "foo[[bar]]baz". In this case, the RegExStr will contain "foobaz" and
100 /// we'll get an entry in this vector that tells us to insert the value of
102 std::vector<std::pair<StringRef, unsigned>> VariableUses;
104 /// Maps definitions of variables to their parenthesized capture numbers.
106 /// E.g. for the pattern "foo[[bar:.*]]baz", VariableDefs will map "bar" to
108 std::map<StringRef, unsigned> VariableDefs;
110 Check::FileCheckType CheckTy;
112 /// Contains the number of line this pattern is in.
116 explicit FileCheckPattern(Check::FileCheckType Ty)
119 /// Returns the location in source code.
120 SMLoc getLoc() const { return PatternLoc; }
122 bool ParsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
123 unsigned LineNumber, const FileCheckRequest &Req);
124 size_t Match(StringRef Buffer, size_t &MatchLen,
125 StringMap<StringRef> &VariableTable) const;
126 void PrintVariableUses(const SourceMgr &SM, StringRef Buffer,
127 const StringMap<StringRef> &VariableTable,
128 SMRange MatchRange = None) const;
129 void PrintFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
130 const StringMap<StringRef> &VariableTable,
131 std::vector<FileCheckDiag> *Diags) const;
133 bool hasVariable() const {
134 return !(VariableUses.empty() && VariableDefs.empty());
137 Check::FileCheckType getCheckTy() const { return CheckTy; }
139 int getCount() const { return CheckTy.getCount(); }
142 bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
143 void AddBackrefToRegEx(unsigned BackrefNum);
145 ComputeMatchDistance(StringRef Buffer,
146 const StringMap<StringRef> &VariableTable) const;
147 bool EvaluateExpression(StringRef Expr, std::string &Value) const;
148 size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
151 //===----------------------------------------------------------------------===//
152 /// Summary of a FileCheck diagnostic.
153 //===----------------------------------------------------------------------===//
155 struct FileCheckDiag {
156 /// What is the FileCheck directive for this diagnostic?
157 Check::FileCheckType CheckTy;
158 /// Where is the FileCheck directive for this diagnostic?
159 unsigned CheckLine, CheckCol;
160 /// What type of match result does this diagnostic describe?
162 /// A directive's supplied pattern is said to be either expected or excluded
163 /// depending on whether the pattern must have or must not have a match in
164 /// order for the directive to succeed. For example, a CHECK directive's
165 /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
166 /// All match result types whose names end with "Excluded" are for excluded
167 /// patterns, and all others are for expected patterns.
169 /// There might be more than one match result for a single pattern. For
170 /// example, there might be several discarded matches
171 /// (MatchFoundButDiscarded) before either a good match
172 /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
173 /// and there might be a fuzzy match (MatchFuzzy) after the latter.
175 /// Indicates a good match for an expected pattern.
176 MatchFoundAndExpected,
177 /// Indicates a match for an excluded pattern.
178 MatchFoundButExcluded,
179 /// Indicates a match for an expected pattern, but the match is on the
181 MatchFoundButWrongLine,
182 /// Indicates a discarded match for an expected pattern.
183 MatchFoundButDiscarded,
184 /// Indicates no match for an excluded pattern.
185 MatchNoneAndExcluded,
186 /// Indicates no match for an expected pattern, but this might follow good
187 /// matches when multiple matches are expected for the pattern, or it might
188 /// follow discarded matches for the pattern.
189 MatchNoneButExpected,
190 /// Indicates a fuzzy match that serves as a suggestion for the next
191 /// intended match for an expected pattern with too few or no good matches.
194 /// The search range if MatchTy is MatchNoneAndExcluded or
195 /// MatchNoneButExpected, or the match range otherwise.
196 unsigned InputStartLine;
197 unsigned InputStartCol;
198 unsigned InputEndLine;
199 unsigned InputEndCol;
200 FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy,
201 SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange);
204 //===----------------------------------------------------------------------===//
206 //===----------------------------------------------------------------------===//
208 /// A check that we found in the input file.
209 struct FileCheckString {
210 /// The pattern to match.
211 FileCheckPattern Pat;
213 /// Which prefix name this check matched.
216 /// The location in the match file that the check string was specified.
219 /// All of the strings that are disallowed from occurring between this match
220 /// string and the previous one (or start of file).
221 std::vector<FileCheckPattern> DagNotStrings;
223 FileCheckString(const FileCheckPattern &P, StringRef S, SMLoc L)
224 : Pat(P), Prefix(S), Loc(L) {}
226 size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
227 size_t &MatchLen, StringMap<StringRef> &VariableTable,
228 FileCheckRequest &Req, std::vector<FileCheckDiag> *Diags) const;
230 bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
231 bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
232 bool CheckNot(const SourceMgr &SM, StringRef Buffer,
233 const std::vector<const FileCheckPattern *> &NotStrings,
234 StringMap<StringRef> &VariableTable,
235 const FileCheckRequest &Req,
236 std::vector<FileCheckDiag> *Diags) const;
237 size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
238 std::vector<const FileCheckPattern *> &NotStrings,
239 StringMap<StringRef> &VariableTable,
240 const FileCheckRequest &Req,
241 std::vector<FileCheckDiag> *Diags) const;
244 /// FileCheck class takes the request and exposes various methods that
245 /// use information from the request.
247 FileCheckRequest Req;
250 FileCheck(FileCheckRequest Req) : Req(Req) {}
252 // Combines the check prefixes into a single regex so that we can efficiently
253 // scan for any of the set.
255 // The semantics are that the longest-match wins which matches our regex
257 Regex buildCheckPrefixRegex();
259 /// Read the check file, which specifies the sequence of expected strings.
261 /// The strings are added to the CheckStrings vector. Returns true in case of
262 /// an error, false otherwise.
263 bool ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
264 std::vector<FileCheckString> &CheckStrings);
266 bool ValidateCheckPrefixes();
268 /// Canonicalize whitespaces in the file. Line endings are replaced with
270 StringRef CanonicalizeFile(MemoryBuffer &MB,
271 SmallVectorImpl<char> &OutputBuffer);
273 /// Check the input to FileCheck provided in the \p Buffer against the \p
274 /// CheckStrings read from the check file.
276 /// Returns false if the input fails to satisfy the checks.
277 bool CheckInput(SourceMgr &SM, StringRef Buffer,
278 ArrayRef<FileCheckString> CheckStrings,
279 std::vector<FileCheckDiag> *Diags = nullptr);