LLVM  9.0.0svn
FileCheck.h
Go to the documentation of this file.
1 //==-- llvm/Support/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_SUPPORT_FILECHECK_H
14 #define LLVM_SUPPORT_FILECHECK_H
15 
16 #include "llvm/ADT/StringMap.h"
18 #include "llvm/Support/Regex.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include <vector>
21 #include <map>
22 
23 namespace llvm {
24 
25 /// Contains info about various FileCheck options.
27  std::vector<std::string> CheckPrefixes;
29  std::vector<std::string> ImplicitCheckNot;
30  std::vector<std::string> GlobalDefines;
31  bool AllowEmptyInput = false;
32  bool MatchFullLines = false;
33  bool EnableVarScope = false;
35  bool Verbose = false;
36  bool VerboseVerbose = false;
37 };
38 
39 
40 //===----------------------------------------------------------------------===//
41 // Pattern Handling Code.
42 //===----------------------------------------------------------------------===//
43 
44 namespace Check {
45 
47  CheckNone = 0,
55 
56  /// Indicates the pattern only matches the end of file. This is used for
57  /// trailing CHECK-NOTs.
59 
60  /// Marks when parsing found a -NOT check combined with another CHECK suffix.
62 
63  /// Marks when parsing found a -COUNT directive with invalid count value.
65 };
66 
69  int Count; ///< optional Count for some checks
70 
71 public:
72  FileCheckType(FileCheckKind Kind = CheckNone) : Kind(Kind), Count(1) {}
73  FileCheckType(const FileCheckType &) = default;
74 
75  operator FileCheckKind() const { return Kind; }
76 
77  int getCount() const { return Count; }
78  FileCheckType &setCount(int C);
79 
80  std::string getDescription(StringRef Prefix) const;
81 };
82 } // namespace Check
83 
84 struct FileCheckDiag;
85 
86 /// Class holding the FileCheckPattern global state, shared by all patterns:
87 /// tables holding values of variables and whether they are defined or not at
88 /// any given time in the matching process.
90  friend class FileCheckPattern;
91 
92 private:
93  /// When matching a given pattern, this holds the value of all the FileCheck
94  /// variables defined in previous patterns. In a pattern only the last
95  /// definition for a given variable is recorded in this table, back-references
96  /// are used for uses after any the other definition.
97  StringMap<StringRef> GlobalVariableTable;
98 
99 public:
100  /// Return the value of variable \p VarName or None if no such variable has
101  /// been defined.
103 
104  /// Define variables from definitions given on the command line passed as a
105  /// vector of VAR=VAL strings in \p CmdlineDefines.
106  void defineCmdlineVariables(std::vector<std::string> &CmdlineDefines);
107 
108  /// Undefine local variables (variables whose name does not start with a '$'
109  /// sign), i.e. remove them from GlobalVariableTable.
110  void clearLocalVars();
111 };
112 
114  SMLoc PatternLoc;
115 
116  /// A fixed string to match as the pattern or empty if this pattern requires
117  /// a regex match.
118  StringRef FixedStr;
119 
120  /// A regex string to match as the pattern or empty if this pattern requires
121  /// a fixed string to match.
122  std::string RegExStr;
123 
124  /// Entries in this vector map to uses of a variable in the pattern, e.g.
125  /// "foo[[bar]]baz". In this case, the RegExStr will contain "foobaz" and
126  /// we'll get an entry in this vector that tells us to insert the value of
127  /// bar at offset 3.
128  std::vector<std::pair<StringRef, unsigned>> VariableUses;
129 
130  /// Maps definitions of variables to their parenthesized capture numbers.
131  ///
132  /// E.g. for the pattern "foo[[bar:.*]]baz", VariableDefs will map "bar" to
133  /// 1.
134  std::map<StringRef, unsigned> VariableDefs;
135 
136  /// Pointer to the class instance shared by all patterns holding a table with
137  /// the values of live variables at the start of any given CHECK line.
139 
140  Check::FileCheckType CheckTy;
141 
142  /// Contains the number of line this pattern is in.
143  unsigned LineNumber;
144 
145 public:
147  FileCheckPatternContext *Context)
148  : Context(Context), CheckTy(Ty) {}
149 
150  /// Returns the location in source code.
151  SMLoc getLoc() const { return PatternLoc; }
152 
153  /// Returns the pointer to the global state for all patterns in this
154  /// FileCheck instance.
156  bool ParsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
157  unsigned LineNumber, const FileCheckRequest &Req);
158  size_t match(StringRef Buffer, size_t &MatchLen) const;
159  /// Print value of successful substitutions or name of undefined pattern
160  /// variables preventing such a successful substitution.
161  void printVariableUses(const SourceMgr &SM, StringRef Buffer,
162  SMRange MatchRange = None) const;
163  void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
164  std::vector<FileCheckDiag> *Diags) const;
165 
166  bool hasVariable() const {
167  return !(VariableUses.empty() && VariableDefs.empty());
168  }
169 
170  Check::FileCheckType getCheckTy() const { return CheckTy; }
171 
172  int getCount() const { return CheckTy.getCount(); }
173 
174 private:
175  bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
176  void AddBackrefToRegEx(unsigned BackrefNum);
177  unsigned computeMatchDistance(StringRef Buffer) const;
178  bool EvaluateExpression(StringRef Expr, std::string &Value) const;
179  size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
180 };
181 
182 //===----------------------------------------------------------------------===//
183 /// Summary of a FileCheck diagnostic.
184 //===----------------------------------------------------------------------===//
185 
187  /// What is the FileCheck directive for this diagnostic?
189  /// Where is the FileCheck directive for this diagnostic?
190  unsigned CheckLine, CheckCol;
191  /// What type of match result does this diagnostic describe?
192  ///
193  /// A directive's supplied pattern is said to be either expected or excluded
194  /// depending on whether the pattern must have or must not have a match in
195  /// order for the directive to succeed. For example, a CHECK directive's
196  /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
197  /// All match result types whose names end with "Excluded" are for excluded
198  /// patterns, and all others are for expected patterns.
199  ///
200  /// There might be more than one match result for a single pattern. For
201  /// example, there might be several discarded matches
202  /// (MatchFoundButDiscarded) before either a good match
203  /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
204  /// and there might be a fuzzy match (MatchFuzzy) after the latter.
205  enum MatchType {
206  /// Indicates a good match for an expected pattern.
208  /// Indicates a match for an excluded pattern.
210  /// Indicates a match for an expected pattern, but the match is on the
211  /// wrong line.
213  /// Indicates a discarded match for an expected pattern.
215  /// Indicates no match for an excluded pattern.
217  /// Indicates no match for an expected pattern, but this might follow good
218  /// matches when multiple matches are expected for the pattern, or it might
219  /// follow discarded matches for the pattern.
221  /// Indicates a fuzzy match that serves as a suggestion for the next
222  /// intended match for an expected pattern with too few or no good matches.
224  } MatchTy;
225  /// The search range if MatchTy is MatchNoneAndExcluded or
226  /// MatchNoneButExpected, or the match range otherwise.
227  unsigned InputStartLine;
228  unsigned InputStartCol;
229  unsigned InputEndLine;
230  unsigned InputEndCol;
231  FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy,
232  SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange);
233 };
234 
235 //===----------------------------------------------------------------------===//
236 // Check Strings.
237 //===----------------------------------------------------------------------===//
238 
239 /// A check that we found in the input file.
241  /// The pattern to match.
243 
244  /// Which prefix name this check matched.
246 
247  /// The location in the match file that the check string was specified.
249 
250  /// All of the strings that are disallowed from occurring between this match
251  /// string and the previous one (or start of file).
252  std::vector<FileCheckPattern> DagNotStrings;
253 
255  : Pat(P), Prefix(S), Loc(L) {}
256 
257  size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
258  size_t &MatchLen, FileCheckRequest &Req,
259  std::vector<FileCheckDiag> *Diags) const;
260 
261  bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
262  bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
263  bool CheckNot(const SourceMgr &SM, StringRef Buffer,
264  const std::vector<const FileCheckPattern *> &NotStrings,
265  const FileCheckRequest &Req,
266  std::vector<FileCheckDiag> *Diags) const;
267  size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
268  std::vector<const FileCheckPattern *> &NotStrings,
269  const FileCheckRequest &Req,
270  std::vector<FileCheckDiag> *Diags) const;
271 };
272 
273 /// FileCheck class takes the request and exposes various methods that
274 /// use information from the request.
275 class FileCheck {
276  FileCheckRequest Req;
277  FileCheckPatternContext PatternContext;
278 
279 public:
280  FileCheck(FileCheckRequest Req) : Req(Req) {}
281 
282  // Combines the check prefixes into a single regex so that we can efficiently
283  // scan for any of the set.
284  //
285  // The semantics are that the longest-match wins which matches our regex
286  // library.
287  Regex buildCheckPrefixRegex();
288 
289  /// Read the check file, which specifies the sequence of expected strings.
290  ///
291  /// The strings are added to the CheckStrings vector. Returns true in case of
292  /// an error, false otherwise.
293  bool ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
294  std::vector<FileCheckString> &CheckStrings);
295 
296  bool ValidateCheckPrefixes();
297 
298  /// Canonicalize whitespaces in the file. Line endings are replaced with
299  /// UNIX-style '\n'.
300  StringRef CanonicalizeFile(MemoryBuffer &MB,
301  SmallVectorImpl<char> &OutputBuffer);
302 
303  /// Check the input to FileCheck provided in the \p Buffer against the \p
304  /// CheckStrings read from the check file.
305  ///
306  /// Returns false if the input fails to satisfy the checks.
307  bool CheckInput(SourceMgr &SM, StringRef Buffer,
308  ArrayRef<FileCheckString> CheckStrings,
309  std::vector<FileCheckDiag> *Diags = nullptr);
310 };
311 } // namespace llvm
312 #endif
static bool Check(DecodeStatus &Out, DecodeStatus In)
uint64_t CallInst * C
Represents a range in source code.
Definition: SMLoc.h:48
Indicates a good match for an expected pattern.
Definition: FileCheck.h:207
Indicates no match for an excluded pattern.
Definition: FileCheck.h:216
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::vector< std::string > ImplicitCheckNot
Definition: FileCheck.h:29
Indicates a discarded match for an expected pattern.
Definition: FileCheck.h:214
Marks when parsing found a -NOT check combined with another CHECK suffix.
Definition: FileCheck.h:61
static Constant * EvaluateExpression(Value *V, const Loop *L, DenseMap< Instruction *, Constant *> &Vals, const DataLayout &DL, const TargetLibraryInfo *TLI)
EvaluateExpression - Given an expression that passes the getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node in the loop has the value PHIVal.
FileCheckType(FileCheckKind Kind=CheckNone)
Definition: FileCheck.h:72
bool AllowDeprecatedDagOverlap
Definition: FileCheck.h:34
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckSame(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N, const SmallVectorImpl< std::pair< SDValue, SDNode *>> &RecordedNodes)
CheckSame - Implements OP_CheckSame.
StringRef Prefix
Which prefix name this check matched.
Definition: FileCheck.h:245
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:47
FileCheckPattern Pat
The pattern to match.
Definition: FileCheck.h:242
Class holding the FileCheckPattern global state, shared by all patterns: tables holding values of var...
Definition: FileCheck.h:89
SMLoc Loc
The location in the match file that the check string was specified.
Definition: FileCheck.h:248
Check::FileCheckType CheckTy
What is the FileCheck directive for this diagnostic?
Definition: FileCheck.h:188
Indicates a match for an expected pattern, but the match is on the wrong line.
Definition: FileCheck.h:212
FileCheckPattern(Check::FileCheckType Ty, FileCheckPatternContext *Context)
Definition: FileCheck.h:146
Marks when parsing found a -COUNT directive with invalid count value.
Definition: FileCheck.h:64
std::vector< std::string > CheckPrefixes
Definition: FileCheck.h:27
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
Contains info about various FileCheck options.
Definition: FileCheck.h:26
FileCheck(FileCheckRequest Req)
Definition: FileCheck.h:280
std::vector< std::string > GlobalDefines
Definition: FileCheck.h:30
static std::string getDescription(const CallGraphSCC &SCC)
unsigned InputEndLine
Definition: FileCheck.h:229
#define P(N)
A check that we found in the input file.
Definition: FileCheck.h:240
std::vector< FileCheckPattern > DagNotStrings
All of the strings that are disallowed from occurring between this match string and the previous one ...
Definition: FileCheck.h:252
unsigned CheckLine
Where is the FileCheck directive for this diagnostic?
Definition: FileCheck.h:190
SMLoc getLoc() const
Returns the location in source code.
Definition: FileCheck.h:151
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:41
Check::FileCheckType getCheckTy() const
Definition: FileCheck.h:170
bool hasVariable() const
Definition: FileCheck.h:166
Indicates the pattern only matches the end of file.
Definition: FileCheck.h:58
Indicates a fuzzy match that serves as a suggestion for the next intended match for an expected patte...
Definition: FileCheck.h:223
Indicates no match for an expected pattern, but this might follow good matches when multiple matches ...
Definition: FileCheck.h:220
unsigned InputEndCol
Definition: FileCheck.h:230
FileCheckPatternContext * getContext() const
Returns the pointer to the global state for all patterns in this FileCheck instance.
Definition: FileCheck.h:155
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:219
unsigned InputStartLine
The search range if MatchTy is MatchNoneAndExcluded or MatchNoneButExpected, or the match range other...
Definition: FileCheck.h:227
Indicates a match for an excluded pattern.
Definition: FileCheck.h:209
Summary of a FileCheck diagnostic.
Definition: FileCheck.h:186
unsigned InputStartCol
Definition: FileCheck.h:228
LLVM Value Representation.
Definition: Value.h:72
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
MatchType
What type of match result does this diagnostic describe?
Definition: FileCheck.h:205
int getCount() const
Definition: FileCheck.h:172
Represents a location in source code.
Definition: SMLoc.h:23
FileCheck class takes the request and exposes various methods that use information from the request...
Definition: FileCheck.h:275
FileCheckString(const FileCheckPattern &P, StringRef S, SMLoc L)
Definition: FileCheck.h:254