blob: cd62870dfd7aa06a6c37ab5bf887716a30c3655e [file] [log] [blame]
Chris Lattner81cb8ca2009-07-08 18:44:05 +00001//===- FileCheck.cpp - Check that File's Contents match what is expected --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// FileCheck does a line-by line check of a file that validates whether it
11// contains the expected content. This is useful for regression tests etc.
12//
13// This program exits with an error status of 2 on error, exit status of 0 if
14// the file matched the expected contents, and exit status of 1 if it did not
15// contain the expected contents.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/PrettyStackTrace.h"
22#include "llvm/Support/SourceMgr.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/System/Signals.h"
25using namespace llvm;
26
27static cl::opt<std::string>
28CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
29
30static cl::opt<std::string>
31InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
32 cl::init("-"), cl::value_desc("filename"));
33
34static cl::opt<std::string>
35CheckPrefix("check-prefix", cl::init("CHECK"),
36 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
37
Chris Lattner88a7e9e2009-07-11 18:58:15 +000038static cl::opt<bool>
39NoCanonicalizeWhiteSpace("strict-whitespace",
40 cl::desc("Do not treat all horizontal whitespace as equivalent"));
41
Chris Lattnera29703e2009-09-24 20:39:13 +000042//===----------------------------------------------------------------------===//
43// Pattern Handling Code.
44//===----------------------------------------------------------------------===//
45
Chris Lattner9fc66782009-09-24 20:25:55 +000046class Pattern {
Chris Lattner207e1bc2009-08-15 17:41:04 +000047 /// Str - The string to match.
Chris Lattneradea46e2009-09-24 20:45:07 +000048 StringRef Str;
Chris Lattner9fc66782009-09-24 20:25:55 +000049public:
50
Chris Lattnera29703e2009-09-24 20:39:13 +000051 Pattern() { }
52
53 bool ParsePattern(StringRef PatternStr, SourceMgr &SM);
Chris Lattner9fc66782009-09-24 20:25:55 +000054
55 /// Match - Match the pattern string against the input buffer Buffer. This
56 /// returns the position that is matched or npos if there is no match. If
57 /// there is a match, the size of the matched string is returned in MatchLen.
58 size_t Match(StringRef Buffer, size_t &MatchLen) const {
59 MatchLen = Str.size();
60 return Buffer.find(Str);
61 }
Chris Lattner9fc66782009-09-24 20:25:55 +000062};
63
Chris Lattnera29703e2009-09-24 20:39:13 +000064bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) {
65 // Ignore trailing whitespace.
66 while (!PatternStr.empty() &&
67 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
68 PatternStr = PatternStr.substr(0, PatternStr.size()-1);
69
70 // Check that there is something on the line.
71 if (PatternStr.empty()) {
72 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
73 "found empty check string with prefix '"+CheckPrefix+":'",
74 "error");
75 return true;
76 }
Chris Lattneradea46e2009-09-24 20:45:07 +000077
Chris Lattnera29703e2009-09-24 20:39:13 +000078
Chris Lattnera29703e2009-09-24 20:39:13 +000079
Chris Lattneradea46e2009-09-24 20:45:07 +000080 Str = PatternStr;
Chris Lattnera29703e2009-09-24 20:39:13 +000081 return false;
82}
83
84
85//===----------------------------------------------------------------------===//
86// Check Strings.
87//===----------------------------------------------------------------------===//
Chris Lattner9fc66782009-09-24 20:25:55 +000088
89/// CheckString - This is a check that we found in the input file.
90struct CheckString {
91 /// Pat - The pattern to match.
92 Pattern Pat;
Chris Lattner207e1bc2009-08-15 17:41:04 +000093
94 /// Loc - The location in the match file that the check string was specified.
95 SMLoc Loc;
96
Chris Lattner5dafafd2009-08-15 18:32:21 +000097 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
98 /// to a CHECK: directive.
99 bool IsCheckNext;
100
Chris Lattnerf15380b2009-09-20 22:35:26 +0000101 /// NotStrings - These are all of the strings that are disallowed from
102 /// occurring between this match string and the previous one (or start of
103 /// file).
Chris Lattnera29703e2009-09-24 20:39:13 +0000104 std::vector<std::pair<SMLoc, Pattern> > NotStrings;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000105
Chris Lattner9fc66782009-09-24 20:25:55 +0000106 CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
107 : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
Chris Lattner207e1bc2009-08-15 17:41:04 +0000108};
109
Chris Lattneradea46e2009-09-24 20:45:07 +0000110/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
111/// memory buffer, free it, and return a new one.
112static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
113 SmallVector<char, 16> NewFile;
114 NewFile.reserve(MB->getBufferSize());
115
116 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
117 Ptr != End; ++Ptr) {
118 // If C is not a horizontal whitespace, skip it.
119 if (*Ptr != ' ' && *Ptr != '\t') {
120 NewFile.push_back(*Ptr);
121 continue;
122 }
123
124 // Otherwise, add one space and advance over neighboring space.
125 NewFile.push_back(' ');
126 while (Ptr+1 != End &&
127 (Ptr[1] == ' ' || Ptr[1] == '\t'))
128 ++Ptr;
129 }
130
131 // Free the old buffer and return a new one.
132 MemoryBuffer *MB2 =
133 MemoryBuffer::getMemBufferCopy(NewFile.data(),
134 NewFile.data() + NewFile.size(),
135 MB->getBufferIdentifier());
136
137 delete MB;
138 return MB2;
139}
140
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000141
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000142/// ReadCheckFile - Read the check file, which specifies the sequence of
143/// expected strings. The strings are added to the CheckStrings vector.
144static bool ReadCheckFile(SourceMgr &SM,
Chris Lattner207e1bc2009-08-15 17:41:04 +0000145 std::vector<CheckString> &CheckStrings) {
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000146 // Open the check file, and tell SourceMgr about it.
147 std::string ErrorStr;
148 MemoryBuffer *F =
149 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr);
150 if (F == 0) {
151 errs() << "Could not open check file '" << CheckFilename << "': "
152 << ErrorStr << '\n';
153 return true;
154 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000155
156 // If we want to canonicalize whitespace, strip excess whitespace from the
157 // buffer containing the CHECK lines.
158 if (!NoCanonicalizeWhiteSpace)
159 F = CanonicalizeInputFile(F);
160
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000161 SM.AddNewSourceBuffer(F, SMLoc());
162
Chris Lattnerd7e25052009-08-15 18:00:42 +0000163 // Find all instances of CheckPrefix followed by : in the file.
Chris Lattner96077032009-09-20 22:11:44 +0000164 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000165
Chris Lattnera29703e2009-09-24 20:39:13 +0000166 std::vector<std::pair<SMLoc, Pattern> > NotMatches;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000167
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000168 while (1) {
169 // See if Prefix occurs in the memory buffer.
Chris Lattner96077032009-09-20 22:11:44 +0000170 Buffer = Buffer.substr(Buffer.find(CheckPrefix));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000171
172 // If we didn't find a match, we're done.
Chris Lattner96077032009-09-20 22:11:44 +0000173 if (Buffer.empty())
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000174 break;
175
Chris Lattner96077032009-09-20 22:11:44 +0000176 const char *CheckPrefixStart = Buffer.data();
Chris Lattner5dafafd2009-08-15 18:32:21 +0000177
178 // When we find a check prefix, keep track of whether we find CHECK: or
179 // CHECK-NEXT:
Chris Lattnerf15380b2009-09-20 22:35:26 +0000180 bool IsCheckNext = false, IsCheckNot = false;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000181
Chris Lattnerd7e25052009-08-15 18:00:42 +0000182 // Verify that the : is present after the prefix.
Chris Lattner96077032009-09-20 22:11:44 +0000183 if (Buffer[CheckPrefix.size()] == ':') {
184 Buffer = Buffer.substr(CheckPrefix.size()+1);
Chris Lattner96077032009-09-20 22:11:44 +0000185 } else if (Buffer.size() > CheckPrefix.size()+6 &&
186 memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
187 Buffer = Buffer.substr(CheckPrefix.size()+7);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000188 IsCheckNext = true;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000189 } else if (Buffer.size() > CheckPrefix.size()+5 &&
190 memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) {
191 Buffer = Buffer.substr(CheckPrefix.size()+6);
192 IsCheckNot = true;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000193 } else {
Chris Lattner96077032009-09-20 22:11:44 +0000194 Buffer = Buffer.substr(1);
Chris Lattnerd7e25052009-08-15 18:00:42 +0000195 continue;
196 }
197
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000198 // Okay, we found the prefix, yay. Remember the rest of the line, but
199 // ignore leading and trailing whitespace.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000200 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000201
202 // Scan ahead to the end of line.
Chris Lattner96077032009-09-20 22:11:44 +0000203 size_t EOL = Buffer.find_first_of("\n\r");
Chris Lattnera29703e2009-09-24 20:39:13 +0000204
205 // Parse the pattern.
206 Pattern P;
207 if (P.ParsePattern(Buffer.substr(0, EOL), SM))
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000208 return true;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000209
Chris Lattnera29703e2009-09-24 20:39:13 +0000210 Buffer = Buffer.substr(EOL);
211
Chris Lattnerf15380b2009-09-20 22:35:26 +0000212
Chris Lattner5dafafd2009-08-15 18:32:21 +0000213 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
214 if (IsCheckNext && CheckStrings.empty()) {
215 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
216 "found '"+CheckPrefix+"-NEXT:' without previous '"+
217 CheckPrefix+ ": line", "error");
218 return true;
219 }
220
Chris Lattnera29703e2009-09-24 20:39:13 +0000221 // Handle CHECK-NOT.
222 if (IsCheckNot) {
223 NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
224 P));
225 continue;
226 }
227
Chris Lattner9fc66782009-09-24 20:25:55 +0000228
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000229 // Okay, add the string we captured to the output vector and move on.
Chris Lattner9fc66782009-09-24 20:25:55 +0000230 CheckStrings.push_back(CheckString(P,
Chris Lattner96077032009-09-20 22:11:44 +0000231 SMLoc::getFromPointer(Buffer.data()),
Chris Lattner5dafafd2009-08-15 18:32:21 +0000232 IsCheckNext));
Chris Lattnerf15380b2009-09-20 22:35:26 +0000233 std::swap(NotMatches, CheckStrings.back().NotStrings);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000234 }
235
236 if (CheckStrings.empty()) {
Chris Lattnerd7e25052009-08-15 18:00:42 +0000237 errs() << "error: no check strings found with prefix '" << CheckPrefix
238 << ":'\n";
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000239 return true;
240 }
241
Chris Lattnerf15380b2009-09-20 22:35:26 +0000242 if (!NotMatches.empty()) {
243 errs() << "error: '" << CheckPrefix
244 << "-NOT:' not supported after last check line.\n";
245 return true;
246 }
247
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000248 return false;
249}
250
Chris Lattner5dafafd2009-08-15 18:32:21 +0000251static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
Chris Lattner96077032009-09-20 22:11:44 +0000252 StringRef Buffer) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000253 // Otherwise, we have an error, emit an error message.
254 SM.PrintMessage(CheckStr.Loc, "expected string not found in input",
255 "error");
256
257 // Print the "scanning from here" line. If the current position is at the
258 // end of a line, advance to the start of the next line.
Chris Lattner96077032009-09-20 22:11:44 +0000259 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
Chris Lattner5dafafd2009-08-15 18:32:21 +0000260
Chris Lattner96077032009-09-20 22:11:44 +0000261 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here",
Chris Lattner5dafafd2009-08-15 18:32:21 +0000262 "note");
263}
264
Chris Lattner3711b7a2009-09-20 22:42:44 +0000265/// CountNumNewlinesBetween - Count the number of newlines in the specified
266/// range.
267static unsigned CountNumNewlinesBetween(StringRef Range) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000268 unsigned NumNewLines = 0;
Chris Lattner3711b7a2009-09-20 22:42:44 +0000269 while (1) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000270 // Scan for newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000271 Range = Range.substr(Range.find_first_of("\n\r"));
272 if (Range.empty()) return NumNewLines;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000273
274 ++NumNewLines;
275
276 // Handle \n\r and \r\n as a single newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000277 if (Range.size() > 1 &&
278 (Range[1] == '\n' || Range[1] == '\r') &&
279 (Range[0] != Range[1]))
280 Range = Range.substr(1);
281 Range = Range.substr(1);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000282 }
Chris Lattner5dafafd2009-08-15 18:32:21 +0000283}
284
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000285int main(int argc, char **argv) {
286 sys::PrintStackTraceOnErrorSignal();
287 PrettyStackTraceProgram X(argc, argv);
288 cl::ParseCommandLineOptions(argc, argv);
289
290 SourceMgr SM;
291
292 // Read the expected strings from the check file.
Chris Lattner207e1bc2009-08-15 17:41:04 +0000293 std::vector<CheckString> CheckStrings;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000294 if (ReadCheckFile(SM, CheckStrings))
295 return 2;
296
297 // Open the file to check and add it to SourceMgr.
298 std::string ErrorStr;
299 MemoryBuffer *F =
300 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
301 if (F == 0) {
302 errs() << "Could not open input file '" << InputFilename << "': "
303 << ErrorStr << '\n';
304 return true;
305 }
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000306
307 // Remove duplicate spaces in the input file if requested.
308 if (!NoCanonicalizeWhiteSpace)
309 F = CanonicalizeInputFile(F);
310
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000311 SM.AddNewSourceBuffer(F, SMLoc());
312
313 // Check that we have all of the expected strings, in order, in the input
314 // file.
Chris Lattner96077032009-09-20 22:11:44 +0000315 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000316
Chris Lattnerf15380b2009-09-20 22:35:26 +0000317 const char *LastMatch = Buffer.data();
318
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000319 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
Chris Lattner207e1bc2009-08-15 17:41:04 +0000320 const CheckString &CheckStr = CheckStrings[StrNo];
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000321
Chris Lattner96077032009-09-20 22:11:44 +0000322 StringRef SearchFrom = Buffer;
323
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000324 // Find StrNo in the file.
Chris Lattner9fc66782009-09-24 20:25:55 +0000325 size_t MatchLen = 0;
326 Buffer = Buffer.substr(CheckStr.Pat.Match(Buffer, MatchLen));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000327
Chris Lattner5dafafd2009-08-15 18:32:21 +0000328 // If we didn't find a match, reject the input.
Chris Lattner96077032009-09-20 22:11:44 +0000329 if (Buffer.empty()) {
330 PrintCheckFailed(SM, CheckStr, SearchFrom);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000331 return 1;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000332 }
Chris Lattner3711b7a2009-09-20 22:42:44 +0000333
334 StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
335
Chris Lattner5dafafd2009-08-15 18:32:21 +0000336 // If this check is a "CHECK-NEXT", verify that the previous match was on
337 // the previous line (i.e. that there is one newline between them).
338 if (CheckStr.IsCheckNext) {
339 // Count the number of newlines between the previous match and this one.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000340 assert(LastMatch != F->getBufferStart() &&
341 "CHECK-NEXT can't be the first check in a file");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000342
Chris Lattner3711b7a2009-09-20 22:42:44 +0000343 unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000344 if (NumNewLines == 0) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000345 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000346 CheckPrefix+"-NEXT: is on the same line as previous match",
347 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000348 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000349 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000350 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
351 "previous match was here", "note");
352 return 1;
353 }
354
355 if (NumNewLines != 1) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000356 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000357 CheckPrefix+
358 "-NEXT: is not on the line after the previous match",
359 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000360 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000361 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000362 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
363 "previous match was here", "note");
364 return 1;
365 }
366 }
Chris Lattnerf15380b2009-09-20 22:35:26 +0000367
368 // If this match had "not strings", verify that they don't exist in the
369 // skipped region.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000370 for (unsigned i = 0, e = CheckStr.NotStrings.size(); i != e; ++i) {
Chris Lattnera29703e2009-09-24 20:39:13 +0000371 size_t MatchLen = 0;
372 size_t Pos = CheckStr.NotStrings[i].second.Match(SkippedRegion, MatchLen);
Chris Lattnerf15380b2009-09-20 22:35:26 +0000373 if (Pos == StringRef::npos) continue;
374
375 SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos),
376 CheckPrefix+"-NOT: string occurred!", "error");
377 SM.PrintMessage(CheckStr.NotStrings[i].first,
378 CheckPrefix+"-NOT: pattern specified here", "note");
379 return 1;
380 }
381
Chris Lattner5dafafd2009-08-15 18:32:21 +0000382
Chris Lattner81115762009-09-21 02:30:42 +0000383 // Otherwise, everything is good. Step over the matched text and remember
384 // the position after the match as the end of the last match.
Chris Lattner9fc66782009-09-24 20:25:55 +0000385 Buffer = Buffer.substr(MatchLen);
Chris Lattner81115762009-09-21 02:30:42 +0000386 LastMatch = Buffer.data();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000387 }
388
389 return 0;
390}