blob: 8de2612f88d1884f7185fa916d13f2c6577b5b00 [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 Lattner207e1bc2009-08-15 17:41:04 +000042/// CheckString - This is a check that we found in the input file.
43struct CheckString {
44 /// Str - The string to match.
45 std::string Str;
46
47 /// Loc - The location in the match file that the check string was specified.
48 SMLoc Loc;
49
Chris Lattner5dafafd2009-08-15 18:32:21 +000050 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
51 /// to a CHECK: directive.
52 bool IsCheckNext;
53
54 CheckString(const std::string &S, SMLoc L, bool isCheckNext)
55 : Str(S), Loc(L), IsCheckNext(isCheckNext) {}
Chris Lattner207e1bc2009-08-15 17:41:04 +000056};
57
Chris Lattner81cb8ca2009-07-08 18:44:05 +000058
Chris Lattner81cb8ca2009-07-08 18:44:05 +000059/// ReadCheckFile - Read the check file, which specifies the sequence of
60/// expected strings. The strings are added to the CheckStrings vector.
61static bool ReadCheckFile(SourceMgr &SM,
Chris Lattner207e1bc2009-08-15 17:41:04 +000062 std::vector<CheckString> &CheckStrings) {
Chris Lattner81cb8ca2009-07-08 18:44:05 +000063 // Open the check file, and tell SourceMgr about it.
64 std::string ErrorStr;
65 MemoryBuffer *F =
66 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr);
67 if (F == 0) {
68 errs() << "Could not open check file '" << CheckFilename << "': "
69 << ErrorStr << '\n';
70 return true;
71 }
72 SM.AddNewSourceBuffer(F, SMLoc());
73
Chris Lattnerd7e25052009-08-15 18:00:42 +000074 // Find all instances of CheckPrefix followed by : in the file.
Chris Lattner96077032009-09-20 22:11:44 +000075 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +000076
77 while (1) {
78 // See if Prefix occurs in the memory buffer.
Chris Lattner96077032009-09-20 22:11:44 +000079 Buffer = Buffer.substr(Buffer.find(CheckPrefix));
Chris Lattner81cb8ca2009-07-08 18:44:05 +000080
81 // If we didn't find a match, we're done.
Chris Lattner96077032009-09-20 22:11:44 +000082 if (Buffer.empty())
Chris Lattner81cb8ca2009-07-08 18:44:05 +000083 break;
84
Chris Lattner96077032009-09-20 22:11:44 +000085 const char *CheckPrefixStart = Buffer.data();
Chris Lattner5dafafd2009-08-15 18:32:21 +000086
87 // When we find a check prefix, keep track of whether we find CHECK: or
88 // CHECK-NEXT:
89 bool IsCheckNext;
90
Chris Lattnerd7e25052009-08-15 18:00:42 +000091 // Verify that the : is present after the prefix.
Chris Lattner96077032009-09-20 22:11:44 +000092 if (Buffer[CheckPrefix.size()] == ':') {
93 Buffer = Buffer.substr(CheckPrefix.size()+1);
Chris Lattner5dafafd2009-08-15 18:32:21 +000094 IsCheckNext = false;
Chris Lattner96077032009-09-20 22:11:44 +000095 } else if (Buffer.size() > CheckPrefix.size()+6 &&
96 memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
97 Buffer = Buffer.substr(CheckPrefix.size()+7);
Chris Lattner5dafafd2009-08-15 18:32:21 +000098 IsCheckNext = true;
99 } else {
Chris Lattner96077032009-09-20 22:11:44 +0000100 Buffer = Buffer.substr(1);
Chris Lattnerd7e25052009-08-15 18:00:42 +0000101 continue;
102 }
103
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000104 // Okay, we found the prefix, yay. Remember the rest of the line, but
105 // ignore leading and trailing whitespace.
Chris Lattner96077032009-09-20 22:11:44 +0000106 while (!Buffer.empty() && (Buffer[0] == ' ' || Buffer[0] == '\t'))
107 Buffer = Buffer.substr(1);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000108
109 // Scan ahead to the end of line.
Chris Lattner96077032009-09-20 22:11:44 +0000110 size_t EOL = Buffer.find_first_of("\n\r");
111 if (EOL == StringRef::npos) EOL = Buffer.size();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000112
113 // Ignore trailing whitespace.
Chris Lattner96077032009-09-20 22:11:44 +0000114 while (EOL && (Buffer[EOL-1] == ' ' || Buffer[EOL-1] == '\t'))
115 --EOL;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000116
117 // Check that there is something on the line.
Chris Lattner96077032009-09-20 22:11:44 +0000118 if (EOL == 0) {
119 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattnerd7e25052009-08-15 18:00:42 +0000120 "found empty check string with prefix '"+CheckPrefix+":'",
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000121 "error");
122 return true;
123 }
124
Chris Lattner5dafafd2009-08-15 18:32:21 +0000125 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
126 if (IsCheckNext && CheckStrings.empty()) {
127 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
128 "found '"+CheckPrefix+"-NEXT:' without previous '"+
129 CheckPrefix+ ": line", "error");
130 return true;
131 }
132
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000133 // Okay, add the string we captured to the output vector and move on.
Chris Lattner96077032009-09-20 22:11:44 +0000134 CheckStrings.push_back(CheckString(std::string(Buffer.data(),
135 Buffer.data()+EOL),
136 SMLoc::getFromPointer(Buffer.data()),
Chris Lattner5dafafd2009-08-15 18:32:21 +0000137 IsCheckNext));
Chris Lattner96077032009-09-20 22:11:44 +0000138
139 Buffer = Buffer.substr(EOL);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000140 }
141
142 if (CheckStrings.empty()) {
Chris Lattnerd7e25052009-08-15 18:00:42 +0000143 errs() << "error: no check strings found with prefix '" << CheckPrefix
144 << ":'\n";
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000145 return true;
146 }
147
148 return false;
149}
150
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000151// CanonicalizeCheckStrings - Replace all sequences of horizontal whitespace in
152// the check strings with a single space.
Chris Lattner207e1bc2009-08-15 17:41:04 +0000153static void CanonicalizeCheckStrings(std::vector<CheckString> &CheckStrings) {
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000154 for (unsigned i = 0, e = CheckStrings.size(); i != e; ++i) {
Chris Lattner207e1bc2009-08-15 17:41:04 +0000155 std::string &Str = CheckStrings[i].Str;
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000156
157 for (unsigned C = 0; C != Str.size(); ++C) {
158 // If C is not a horizontal whitespace, skip it.
159 if (Str[C] != ' ' && Str[C] != '\t')
160 continue;
161
162 // Replace the character with space, then remove any other space
163 // characters after it.
164 Str[C] = ' ';
165
166 while (C+1 != Str.size() &&
167 (Str[C+1] == ' ' || Str[C+1] == '\t'))
168 Str.erase(Str.begin()+C+1);
169 }
170 }
171}
172
173/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
174/// memory buffer, free it, and return a new one.
175static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
Daniel Dunbar6f69aa32009-08-02 01:21:22 +0000176 SmallVector<char, 16> NewFile;
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000177 NewFile.reserve(MB->getBufferSize());
178
179 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
180 Ptr != End; ++Ptr) {
181 // If C is not a horizontal whitespace, skip it.
182 if (*Ptr != ' ' && *Ptr != '\t') {
183 NewFile.push_back(*Ptr);
184 continue;
185 }
186
187 // Otherwise, add one space and advance over neighboring space.
188 NewFile.push_back(' ');
189 while (Ptr+1 != End &&
190 (Ptr[1] == ' ' || Ptr[1] == '\t'))
191 ++Ptr;
192 }
193
194 // Free the old buffer and return a new one.
195 MemoryBuffer *MB2 =
Daniel Dunbar6f69aa32009-08-02 01:21:22 +0000196 MemoryBuffer::getMemBufferCopy(NewFile.data(),
197 NewFile.data() + NewFile.size(),
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000198 MB->getBufferIdentifier());
199
200 delete MB;
201 return MB2;
202}
203
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000204
Chris Lattner5dafafd2009-08-15 18:32:21 +0000205static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
Chris Lattner96077032009-09-20 22:11:44 +0000206 StringRef Buffer) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000207 // Otherwise, we have an error, emit an error message.
208 SM.PrintMessage(CheckStr.Loc, "expected string not found in input",
209 "error");
210
211 // Print the "scanning from here" line. If the current position is at the
212 // end of a line, advance to the start of the next line.
Chris Lattner96077032009-09-20 22:11:44 +0000213 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
Chris Lattner5dafafd2009-08-15 18:32:21 +0000214
Chris Lattner96077032009-09-20 22:11:44 +0000215 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here",
Chris Lattner5dafafd2009-08-15 18:32:21 +0000216 "note");
217}
218
219static unsigned CountNumNewlinesBetween(const char *Start, const char *End) {
220 unsigned NumNewLines = 0;
221 for (; Start != End; ++Start) {
222 // Scan for newline.
223 if (Start[0] != '\n' && Start[0] != '\r')
224 continue;
225
226 ++NumNewLines;
227
228 // Handle \n\r and \r\n as a single newline.
229 if (Start+1 != End &&
230 (Start[0] == '\n' || Start[0] == '\r') &&
231 (Start[0] != Start[1]))
232 ++Start;
233 }
234
235 return NumNewLines;
236}
237
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000238int main(int argc, char **argv) {
239 sys::PrintStackTraceOnErrorSignal();
240 PrettyStackTraceProgram X(argc, argv);
241 cl::ParseCommandLineOptions(argc, argv);
242
243 SourceMgr SM;
244
245 // Read the expected strings from the check file.
Chris Lattner207e1bc2009-08-15 17:41:04 +0000246 std::vector<CheckString> CheckStrings;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000247 if (ReadCheckFile(SM, CheckStrings))
248 return 2;
249
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000250 // Remove duplicate spaces in the check strings if requested.
251 if (!NoCanonicalizeWhiteSpace)
252 CanonicalizeCheckStrings(CheckStrings);
253
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000254 // Open the file to check and add it to SourceMgr.
255 std::string ErrorStr;
256 MemoryBuffer *F =
257 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
258 if (F == 0) {
259 errs() << "Could not open input file '" << InputFilename << "': "
260 << ErrorStr << '\n';
261 return true;
262 }
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000263
264 // Remove duplicate spaces in the input file if requested.
265 if (!NoCanonicalizeWhiteSpace)
266 F = CanonicalizeInputFile(F);
267
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000268 SM.AddNewSourceBuffer(F, SMLoc());
269
270 // Check that we have all of the expected strings, in order, in the input
271 // file.
Chris Lattner96077032009-09-20 22:11:44 +0000272 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000273
Chris Lattner5dafafd2009-08-15 18:32:21 +0000274 const char *LastMatch = 0;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000275 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
Chris Lattner207e1bc2009-08-15 17:41:04 +0000276 const CheckString &CheckStr = CheckStrings[StrNo];
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000277
Chris Lattner96077032009-09-20 22:11:44 +0000278 StringRef SearchFrom = Buffer;
279
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000280 // Find StrNo in the file.
Chris Lattner96077032009-09-20 22:11:44 +0000281 Buffer = Buffer.substr(Buffer.find(CheckStr.Str));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000282
Chris Lattner5dafafd2009-08-15 18:32:21 +0000283 // If we didn't find a match, reject the input.
Chris Lattner96077032009-09-20 22:11:44 +0000284 if (Buffer.empty()) {
285 PrintCheckFailed(SM, CheckStr, SearchFrom);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000286 return 1;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000287 }
288
Chris Lattner5dafafd2009-08-15 18:32:21 +0000289 // If this check is a "CHECK-NEXT", verify that the previous match was on
290 // the previous line (i.e. that there is one newline between them).
291 if (CheckStr.IsCheckNext) {
292 // Count the number of newlines between the previous match and this one.
293 assert(LastMatch && "CHECK-NEXT can't be the first check in a file");
294
Chris Lattner96077032009-09-20 22:11:44 +0000295 unsigned NumNewLines = CountNumNewlinesBetween(LastMatch, Buffer.data());
Chris Lattner5dafafd2009-08-15 18:32:21 +0000296 if (NumNewLines == 0) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000297 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000298 CheckPrefix+"-NEXT: is on the same line as previous match",
299 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000300 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000301 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000302 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
303 "previous match was here", "note");
304 return 1;
305 }
306
307 if (NumNewLines != 1) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000308 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000309 CheckPrefix+
310 "-NEXT: is not on the line after the previous match",
311 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000312 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000313 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000314 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
315 "previous match was here", "note");
316 return 1;
317 }
318 }
319
320 // Otherwise, everything is good. Remember this as the last match and move
321 // on to the next one.
Chris Lattner96077032009-09-20 22:11:44 +0000322 LastMatch = Buffer.data();
323 Buffer = Buffer.substr(CheckStr.Str.size());
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000324 }
325
326 return 0;
327}