LLVM  10.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/StringRef.h"
18 #include "llvm/Support/Regex.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include <string>
21 #include <vector>
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 IgnoreCase = false;
34  bool EnableVarScope = false;
36  bool Verbose = false;
37  bool VerboseVerbose = false;
38 };
39 
40 //===----------------------------------------------------------------------===//
41 // Summary of a FileCheck diagnostic.
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  // \returns a description of \p Prefix.
81  std::string getDescription(StringRef Prefix) const;
82 };
83 } // namespace Check
84 
85 struct FileCheckDiag {
86  /// What is the FileCheck directive for this diagnostic?
88  /// Where is the FileCheck directive for this diagnostic?
89  unsigned CheckLine, CheckCol;
90  /// What type of match result does this diagnostic describe?
91  ///
92  /// A directive's supplied pattern is said to be either expected or excluded
93  /// depending on whether the pattern must have or must not have a match in
94  /// order for the directive to succeed. For example, a CHECK directive's
95  /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
96  /// All match result types whose names end with "Excluded" are for excluded
97  /// patterns, and all others are for expected patterns.
98  ///
99  /// There might be more than one match result for a single pattern. For
100  /// example, there might be several discarded matches
101  /// (MatchFoundButDiscarded) before either a good match
102  /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
103  /// and there might be a fuzzy match (MatchFuzzy) after the latter.
104  enum MatchType {
105  /// Indicates a good match for an expected pattern.
107  /// Indicates a match for an excluded pattern.
109  /// Indicates a match for an expected pattern, but the match is on the
110  /// wrong line.
112  /// Indicates a discarded match for an expected pattern.
114  /// Indicates no match for an excluded pattern.
116  /// Indicates no match for an expected pattern, but this might follow good
117  /// matches when multiple matches are expected for the pattern, or it might
118  /// follow discarded matches for the pattern.
120  /// Indicates a fuzzy match that serves as a suggestion for the next
121  /// intended match for an expected pattern with too few or no good matches.
123  } MatchTy;
124  /// The search range if MatchTy is MatchNoneAndExcluded or
125  /// MatchNoneButExpected, or the match range otherwise.
126  unsigned InputStartLine;
127  unsigned InputStartCol;
128  unsigned InputEndLine;
129  unsigned InputEndCol;
130  FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy,
131  SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange);
132 };
133 
135 struct FileCheckString;
136 
137 /// FileCheck class takes the request and exposes various methods that
138 /// use information from the request.
139 class FileCheck {
140  FileCheckRequest Req;
141  std::unique_ptr<FileCheckPatternContext> PatternContext;
142  // C++17 TODO: make this a plain std::vector.
143  std::unique_ptr<std::vector<FileCheckString>> CheckStrings;
144 
145 public:
146  explicit FileCheck(FileCheckRequest Req);
147  ~FileCheck();
148 
149  // Combines the check prefixes into a single regex so that we can efficiently
150  // scan for any of the set.
151  //
152  // The semantics are that the longest-match wins which matches our regex
153  // library.
154  Regex buildCheckPrefixRegex();
155 
156  /// Reads the check file from \p Buffer and records the expected strings it
157  /// contains. Errors are reported against \p SM.
158  ///
159  /// Only expected strings whose prefix is one of those listed in \p PrefixRE
160  /// are recorded. \returns true in case of an error, false otherwise.
161  bool readCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE);
162 
163  bool ValidateCheckPrefixes();
164 
165  /// Canonicalizes whitespaces in the file. Line endings are replaced with
166  /// UNIX-style '\n'.
167  StringRef CanonicalizeFile(MemoryBuffer &MB,
168  SmallVectorImpl<char> &OutputBuffer);
169 
170  /// Checks the input to FileCheck provided in the \p Buffer against the
171  /// expected strings read from the check file and record diagnostics emitted
172  /// in \p Diags. Errors are recorded against \p SM.
173  ///
174  /// \returns false if the input fails to satisfy the checks.
175  bool checkInput(SourceMgr &SM, StringRef Buffer,
176  std::vector<FileCheckDiag> *Diags = nullptr);
177 };
178 
179 } // namespace llvm
180 
181 #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:106
Indicates no match for an excluded pattern.
Definition: FileCheck.h:115
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:113
Marks when parsing found a -NOT check combined with another CHECK suffix.
Definition: FileCheck.h:61
FileCheckType(FileCheckKind Kind=CheckNone)
Definition: FileCheck.h:72
bool AllowDeprecatedDagOverlap
Definition: FileCheck.h:35
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.
Class holding the FileCheckPattern global state, shared by all patterns: tables holding values of var...
Check::FileCheckType CheckTy
What is the FileCheck directive for this diagnostic?
Definition: FileCheck.h:87
Indicates a match for an expected pattern, but the match is on the wrong line.
Definition: FileCheck.h:111
Marks when parsing found a -COUNT directive with invalid count value.
Definition: FileCheck.h:64
std::vector< std::string > CheckPrefixes
Definition: FileCheck.h:27
Contains info about various FileCheck options.
Definition: FileCheck.h:26
std::vector< std::string > GlobalDefines
Definition: FileCheck.h:30
static std::string getDescription(const CallGraphSCC &SCC)
unsigned InputEndLine
Definition: FileCheck.h:128
A check that we found in the input file.
unsigned CheckLine
Where is the FileCheck directive for this diagnostic?
Definition: FileCheck.h:89
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:41
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:122
Indicates no match for an expected pattern, but this might follow good matches when multiple matches ...
Definition: FileCheck.h:119
unsigned InputEndCol
Definition: FileCheck.h:129
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
unsigned InputStartLine
The search range if MatchTy is MatchNoneAndExcluded or MatchNoneButExpected, or the match range other...
Definition: FileCheck.h:126
Indicates a match for an excluded pattern.
Definition: FileCheck.h:108
unsigned InputStartCol
Definition: FileCheck.h:127
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:104
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:139