1 //===- FileCheck.cpp - Check that File's Contents match what is expected --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
17 //===----------------------------------------------------------------------===//
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"
27 static cl::opt<std::string>
28 CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
30 static cl::opt<std::string>
31 InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
32 cl::init("-"), cl::value_desc("filename"));
34 static cl::opt<std::string>
35 CheckPrefix("check-prefix", cl::init("CHECK"),
36 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
39 NoCanonicalizeWhiteSpace("strict-whitespace",
40 cl::desc("Do not treat all horizontal whitespace as equivalent"));
43 /// Str - The string to match.
47 Pattern(StringRef S) : Str(S.str()) {
48 // Remove duplicate spaces in the check strings if requested.
49 if (!NoCanonicalizeWhiteSpace)
50 CanonicalizeCheckString();
53 /// Match - Match the pattern string against the input buffer Buffer. This
54 /// returns the position that is matched or npos if there is no match. If
55 /// there is a match, the size of the matched string is returned in MatchLen.
56 size_t Match(StringRef Buffer, size_t &MatchLen) const {
57 MatchLen = Str.size();
58 return Buffer.find(Str);
62 /// CanonicalizeCheckString - Replace all sequences of horizontal whitespace
63 /// in the check strings with a single space.
64 void CanonicalizeCheckString() {
65 for (unsigned C = 0; C != Str.size(); ++C) {
66 // If C is not a horizontal whitespace, skip it.
67 if (Str[C] != ' ' && Str[C] != '\t')
70 // Replace the character with space, then remove any other space
71 // characters after it.
74 while (C+1 != Str.size() &&
75 (Str[C+1] == ' ' || Str[C+1] == '\t'))
76 Str.erase(Str.begin()+C+1);
82 /// CheckString - This is a check that we found in the input file.
84 /// Pat - The pattern to match.
87 /// Loc - The location in the match file that the check string was specified.
90 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
91 /// to a CHECK: directive.
94 /// NotStrings - These are all of the strings that are disallowed from
95 /// occurring between this match string and the previous one (or start of
97 std::vector<std::pair<SMLoc, std::string> > NotStrings;
99 CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
100 : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
104 /// ReadCheckFile - Read the check file, which specifies the sequence of
105 /// expected strings. The strings are added to the CheckStrings vector.
106 static bool ReadCheckFile(SourceMgr &SM,
107 std::vector<CheckString> &CheckStrings) {
108 // Open the check file, and tell SourceMgr about it.
109 std::string ErrorStr;
111 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr);
113 errs() << "Could not open check file '" << CheckFilename << "': "
117 SM.AddNewSourceBuffer(F, SMLoc());
119 // Find all instances of CheckPrefix followed by : in the file.
120 StringRef Buffer = F->getBuffer();
122 std::vector<std::pair<SMLoc, std::string> > NotMatches;
125 // See if Prefix occurs in the memory buffer.
126 Buffer = Buffer.substr(Buffer.find(CheckPrefix));
128 // If we didn't find a match, we're done.
132 const char *CheckPrefixStart = Buffer.data();
134 // When we find a check prefix, keep track of whether we find CHECK: or
136 bool IsCheckNext = false, IsCheckNot = false;
138 // Verify that the : is present after the prefix.
139 if (Buffer[CheckPrefix.size()] == ':') {
140 Buffer = Buffer.substr(CheckPrefix.size()+1);
141 } else if (Buffer.size() > CheckPrefix.size()+6 &&
142 memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
143 Buffer = Buffer.substr(CheckPrefix.size()+7);
145 } else if (Buffer.size() > CheckPrefix.size()+5 &&
146 memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) {
147 Buffer = Buffer.substr(CheckPrefix.size()+6);
150 Buffer = Buffer.substr(1);
154 // Okay, we found the prefix, yay. Remember the rest of the line, but
155 // ignore leading and trailing whitespace.
156 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
158 // Scan ahead to the end of line.
159 size_t EOL = Buffer.find_first_of("\n\r");
160 if (EOL == StringRef::npos) EOL = Buffer.size();
162 // Ignore trailing whitespace.
163 while (EOL && (Buffer[EOL-1] == ' ' || Buffer[EOL-1] == '\t'))
166 // Check that there is something on the line.
168 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
169 "found empty check string with prefix '"+CheckPrefix+":'",
174 StringRef PatternStr = Buffer.substr(0, EOL);
178 NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
180 Buffer = Buffer.substr(EOL);
184 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
185 if (IsCheckNext && CheckStrings.empty()) {
186 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
187 "found '"+CheckPrefix+"-NEXT:' without previous '"+
188 CheckPrefix+ ": line", "error");
192 Pattern P(PatternStr);
194 // Okay, add the string we captured to the output vector and move on.
195 CheckStrings.push_back(CheckString(P,
196 SMLoc::getFromPointer(Buffer.data()),
198 std::swap(NotMatches, CheckStrings.back().NotStrings);
200 Buffer = Buffer.substr(EOL);
203 if (CheckStrings.empty()) {
204 errs() << "error: no check strings found with prefix '" << CheckPrefix
209 if (!NotMatches.empty()) {
210 errs() << "error: '" << CheckPrefix
211 << "-NOT:' not supported after last check line.\n";
218 /// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
219 /// memory buffer, free it, and return a new one.
220 static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
221 SmallVector<char, 16> NewFile;
222 NewFile.reserve(MB->getBufferSize());
224 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
226 // If C is not a horizontal whitespace, skip it.
227 if (*Ptr != ' ' && *Ptr != '\t') {
228 NewFile.push_back(*Ptr);
232 // Otherwise, add one space and advance over neighboring space.
233 NewFile.push_back(' ');
234 while (Ptr+1 != End &&
235 (Ptr[1] == ' ' || Ptr[1] == '\t'))
239 // Free the old buffer and return a new one.
241 MemoryBuffer::getMemBufferCopy(NewFile.data(),
242 NewFile.data() + NewFile.size(),
243 MB->getBufferIdentifier());
250 static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
252 // Otherwise, we have an error, emit an error message.
253 SM.PrintMessage(CheckStr.Loc, "expected string not found in input",
256 // Print the "scanning from here" line. If the current position is at the
257 // end of a line, advance to the start of the next line.
258 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
260 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here",
264 /// CountNumNewlinesBetween - Count the number of newlines in the specified
266 static unsigned CountNumNewlinesBetween(StringRef Range) {
267 unsigned NumNewLines = 0;
270 Range = Range.substr(Range.find_first_of("\n\r"));
271 if (Range.empty()) return NumNewLines;
275 // Handle \n\r and \r\n as a single newline.
276 if (Range.size() > 1 &&
277 (Range[1] == '\n' || Range[1] == '\r') &&
278 (Range[0] != Range[1]))
279 Range = Range.substr(1);
280 Range = Range.substr(1);
284 int main(int argc, char **argv) {
285 sys::PrintStackTraceOnErrorSignal();
286 PrettyStackTraceProgram X(argc, argv);
287 cl::ParseCommandLineOptions(argc, argv);
291 // Read the expected strings from the check file.
292 std::vector<CheckString> CheckStrings;
293 if (ReadCheckFile(SM, CheckStrings))
296 // Open the file to check and add it to SourceMgr.
297 std::string ErrorStr;
299 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
301 errs() << "Could not open input file '" << InputFilename << "': "
306 // Remove duplicate spaces in the input file if requested.
307 if (!NoCanonicalizeWhiteSpace)
308 F = CanonicalizeInputFile(F);
310 SM.AddNewSourceBuffer(F, SMLoc());
312 // Check that we have all of the expected strings, in order, in the input
314 StringRef Buffer = F->getBuffer();
316 const char *LastMatch = Buffer.data();
318 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
319 const CheckString &CheckStr = CheckStrings[StrNo];
321 StringRef SearchFrom = Buffer;
323 // Find StrNo in the file.
325 Buffer = Buffer.substr(CheckStr.Pat.Match(Buffer, MatchLen));
327 // If we didn't find a match, reject the input.
328 if (Buffer.empty()) {
329 PrintCheckFailed(SM, CheckStr, SearchFrom);
333 StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
335 // If this check is a "CHECK-NEXT", verify that the previous match was on
336 // the previous line (i.e. that there is one newline between them).
337 if (CheckStr.IsCheckNext) {
338 // Count the number of newlines between the previous match and this one.
339 assert(LastMatch != F->getBufferStart() &&
340 "CHECK-NEXT can't be the first check in a file");
342 unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
343 if (NumNewLines == 0) {
344 SM.PrintMessage(CheckStr.Loc,
345 CheckPrefix+"-NEXT: is on the same line as previous match",
347 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
348 "'next' match was here", "note");
349 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
350 "previous match was here", "note");
354 if (NumNewLines != 1) {
355 SM.PrintMessage(CheckStr.Loc,
357 "-NEXT: is not on the line after the previous match",
359 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
360 "'next' match was here", "note");
361 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
362 "previous match was here", "note");
367 // If this match had "not strings", verify that they don't exist in the
369 for (unsigned i = 0, e = CheckStr.NotStrings.size(); i != e; ++i) {
370 size_t Pos = SkippedRegion.find(CheckStr.NotStrings[i].second);
371 if (Pos == StringRef::npos) continue;
373 SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos),
374 CheckPrefix+"-NOT: string occurred!", "error");
375 SM.PrintMessage(CheckStr.NotStrings[i].first,
376 CheckPrefix+"-NOT: pattern specified here", "note");
381 // Otherwise, everything is good. Step over the matched text and remember
382 // the position after the match as the end of the last match.
383 Buffer = Buffer.substr(MatchLen);
384 LastMatch = Buffer.data();