1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
//==-- llvm/Support/FileCheck.h ---------------------------*- C++ -*-==//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
/// \file This file has some utilities to use FileCheck as an API
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_FILECHECK_H
#define LLVM_SUPPORT_FILECHECK_H
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/SourceMgr.h"
#include <vector>
#include <map>
namespace llvm {
/// Contains info about various FileCheck options.
struct FileCheckRequest {
std::vector<std::string> CheckPrefixes;
bool NoCanonicalizeWhiteSpace = false;
std::vector<std::string> ImplicitCheckNot;
std::vector<std::string> GlobalDefines;
bool AllowEmptyInput = false;
bool MatchFullLines = false;
bool EnableVarScope = false;
bool AllowDeprecatedDagOverlap = false;
bool Verbose = false;
bool VerboseVerbose = false;
};
//===----------------------------------------------------------------------===//
// Pattern Handling Code.
//===----------------------------------------------------------------------===//
namespace Check {
enum FileCheckType {
CheckNone = 0,
CheckPlain,
CheckNext,
CheckSame,
CheckNot,
CheckDAG,
CheckLabel,
CheckEmpty,
/// Indicates the pattern only matches the end of file. This is used for
/// trailing CHECK-NOTs.
CheckEOF,
/// Marks when parsing found a -NOT check combined with another CHECK suffix.
CheckBadNot
};
}
class FileCheckPattern {
SMLoc PatternLoc;
/// A fixed string to match as the pattern or empty if this pattern requires
/// a regex match.
StringRef FixedStr;
/// A regex string to match as the pattern or empty if this pattern requires
/// a fixed string to match.
std::string RegExStr;
/// Entries in this vector map to uses of a variable in the pattern, e.g.
/// "foo[[bar]]baz". In this case, the RegExStr will contain "foobaz" and
/// we'll get an entry in this vector that tells us to insert the value of
/// bar at offset 3.
std::vector<std::pair<StringRef, unsigned>> VariableUses;
/// Maps definitions of variables to their parenthesized capture numbers.
///
/// E.g. for the pattern "foo[[bar:.*]]baz", VariableDefs will map "bar" to
/// 1.
std::map<StringRef, unsigned> VariableDefs;
Check::FileCheckType CheckTy;
/// Contains the number of line this pattern is in.
unsigned LineNumber;
public:
explicit FileCheckPattern(Check::FileCheckType Ty)
: CheckTy(Ty) {}
/// Returns the location in source code.
SMLoc getLoc() const { return PatternLoc; }
bool ParsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
unsigned LineNumber, const FileCheckRequest &Req);
size_t Match(StringRef Buffer, size_t &MatchLen,
StringMap<StringRef> &VariableTable) const;
void PrintVariableUses(const SourceMgr &SM, StringRef Buffer,
const StringMap<StringRef> &VariableTable,
SMRange MatchRange = None) const;
void PrintFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
const StringMap<StringRef> &VariableTable) const;
bool hasVariable() const {
return !(VariableUses.empty() && VariableDefs.empty());
}
Check::FileCheckType getCheckTy() const { return CheckTy; }
private:
bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
void AddBackrefToRegEx(unsigned BackrefNum);
unsigned
ComputeMatchDistance(StringRef Buffer,
const StringMap<StringRef> &VariableTable) const;
bool EvaluateExpression(StringRef Expr, std::string &Value) const;
size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
};
//===----------------------------------------------------------------------===//
// Check Strings.
//===----------------------------------------------------------------------===//
/// A check that we found in the input file.
struct FileCheckString {
/// The pattern to match.
FileCheckPattern Pat;
/// Which prefix name this check matched.
StringRef Prefix;
/// The location in the match file that the check string was specified.
SMLoc Loc;
/// All of the strings that are disallowed from occurring between this match
/// string and the previous one (or start of file).
std::vector<FileCheckPattern> DagNotStrings;
FileCheckString(const FileCheckPattern &P, StringRef S, SMLoc L)
: Pat(P), Prefix(S), Loc(L) {}
size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
size_t &MatchLen, StringMap<StringRef> &VariableTable,
FileCheckRequest &Req) const;
bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
bool CheckNot(const SourceMgr &SM, StringRef Buffer,
const std::vector<const FileCheckPattern *> &NotStrings,
StringMap<StringRef> &VariableTable,
const FileCheckRequest &Req) const;
size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
std::vector<const FileCheckPattern *> &NotStrings,
StringMap<StringRef> &VariableTable,
const FileCheckRequest &Req) const;
};
/// FileCheck class takes the request and exposes various methods that
/// use information from the request.
class FileCheck {
FileCheckRequest Req;
public:
FileCheck(FileCheckRequest Req) : Req(Req) {}
// Combines the check prefixes into a single regex so that we can efficiently
// scan for any of the set.
//
// The semantics are that the longest-match wins which matches our regex
// library.
Regex buildCheckPrefixRegex();
/// Read the check file, which specifies the sequence of expected strings.
///
/// The strings are added to the CheckStrings vector. Returns true in case of
/// an error, false otherwise.
bool ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
std::vector<FileCheckString> &CheckStrings);
bool ValidateCheckPrefixes();
/// Canonicalize whitespaces in the file. Line endings are replaced with
/// UNIX-style '\n'.
StringRef CanonicalizeFile(MemoryBuffer &MB,
SmallVectorImpl<char> &OutputBuffer);
/// Check the input to FileCheck provided in the \p Buffer against the \p
/// CheckStrings read from the check file.
///
/// Returns false if the input fails to satisfy the checks.
bool CheckInput(SourceMgr &SM, StringRef Buffer,
ArrayRef<FileCheckString> CheckStrings);
};
} // namespace llvm
#endif
|