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