blob: 9619f945aace711248657df63374d2e2f3db7901 [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"
Chris Lattner52870082009-09-24 21:47:32 +000022#include "llvm/Support/Regex.h"
Chris Lattner81cb8ca2009-07-08 18:44:05 +000023#include "llvm/Support/SourceMgr.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/System/Signals.h"
Daniel Dunbarfafe93c2009-11-22 22:08:06 +000026#include "llvm/ADT/SmallString.h"
Chris Lattnereec96952009-09-27 07:56:52 +000027#include "llvm/ADT/StringMap.h"
28#include <algorithm>
Chris Lattner81cb8ca2009-07-08 18:44:05 +000029using namespace llvm;
30
31static cl::opt<std::string>
32CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
33
34static cl::opt<std::string>
35InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
36 cl::init("-"), cl::value_desc("filename"));
37
38static cl::opt<std::string>
39CheckPrefix("check-prefix", cl::init("CHECK"),
40 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
41
Chris Lattner88a7e9e2009-07-11 18:58:15 +000042static cl::opt<bool>
43NoCanonicalizeWhiteSpace("strict-whitespace",
44 cl::desc("Do not treat all horizontal whitespace as equivalent"));
45
Chris Lattnera29703e2009-09-24 20:39:13 +000046//===----------------------------------------------------------------------===//
47// Pattern Handling Code.
48//===----------------------------------------------------------------------===//
49
Chris Lattner9fc66782009-09-24 20:25:55 +000050class Pattern {
Chris Lattner94638f02009-09-25 17:29:36 +000051 SMLoc PatternLoc;
52
Chris Lattner5d6a05f2009-09-25 17:23:43 +000053 /// FixedStr - If non-empty, this pattern is a fixed string match with the
54 /// specified fixed string.
Chris Lattner2702e6a2009-09-25 17:09:12 +000055 StringRef FixedStr;
Chris Lattner5d6a05f2009-09-25 17:23:43 +000056
57 /// RegEx - If non-empty, this is a regex pattern.
58 std::string RegExStr;
Chris Lattnereec96952009-09-27 07:56:52 +000059
60 /// VariableUses - Entries in this vector map to uses of a variable in the
61 /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain
62 /// "foobaz" and we'll get an entry in this vector that tells us to insert the
63 /// value of bar at offset 3.
64 std::vector<std::pair<StringRef, unsigned> > VariableUses;
65
66 /// VariableDefs - Entries in this vector map to definitions of a variable in
67 /// the pattern, e.g. "foo[[bar:.*]]baz". In this case, the RegExStr will
68 /// contain "foo(.*)baz" and VariableDefs will contain the pair "bar",1. The
69 /// index indicates what parenthesized value captures the variable value.
70 std::vector<std::pair<StringRef, unsigned> > VariableDefs;
71
Chris Lattner9fc66782009-09-24 20:25:55 +000072public:
73
Chris Lattnera29703e2009-09-24 20:39:13 +000074 Pattern() { }
75
76 bool ParsePattern(StringRef PatternStr, SourceMgr &SM);
Chris Lattner9fc66782009-09-24 20:25:55 +000077
78 /// Match - Match the pattern string against the input buffer Buffer. This
79 /// returns the position that is matched or npos if there is no match. If
80 /// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +000081 ///
82 /// The VariableTable StringMap provides the current values of filecheck
83 /// variables and is updated if this match defines new values.
84 size_t Match(StringRef Buffer, size_t &MatchLen,
85 StringMap<StringRef> &VariableTable) const;
Daniel Dunbarfafe93c2009-11-22 22:08:06 +000086
87 /// PrintFailureInfo - Print additional information about a failure to match
88 /// involving this pattern.
89 void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
90 const StringMap<StringRef> &VariableTable) const;
91
Chris Lattner5d6a05f2009-09-25 17:23:43 +000092private:
Chris Lattnereec96952009-09-27 07:56:52 +000093 static void AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr);
94 bool AddRegExToRegEx(StringRef RegExStr, unsigned &CurParen, SourceMgr &SM);
Daniel Dunbaread2dac2009-11-22 22:59:26 +000095
96 /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of
97 /// matching this pattern at the start of \arg Buffer; a distance of zero
98 /// should correspond to a perfect match.
99 unsigned ComputeMatchDistance(StringRef Buffer,
100 const StringMap<StringRef> &VariableTable) const;
Chris Lattner9fc66782009-09-24 20:25:55 +0000101};
102
Chris Lattnereec96952009-09-27 07:56:52 +0000103
Chris Lattnera29703e2009-09-24 20:39:13 +0000104bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) {
Chris Lattner94638f02009-09-25 17:29:36 +0000105 PatternLoc = SMLoc::getFromPointer(PatternStr.data());
106
Chris Lattnera29703e2009-09-24 20:39:13 +0000107 // Ignore trailing whitespace.
108 while (!PatternStr.empty() &&
109 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
110 PatternStr = PatternStr.substr(0, PatternStr.size()-1);
111
112 // Check that there is something on the line.
113 if (PatternStr.empty()) {
Chris Lattner94638f02009-09-25 17:29:36 +0000114 SM.PrintMessage(PatternLoc, "found empty check string with prefix '" +
115 CheckPrefix+":'", "error");
Chris Lattnera29703e2009-09-24 20:39:13 +0000116 return true;
117 }
Chris Lattner52870082009-09-24 21:47:32 +0000118
Chris Lattner2702e6a2009-09-25 17:09:12 +0000119 // Check to see if this is a fixed string, or if it has regex pieces.
Chris Lattnereec96952009-09-27 07:56:52 +0000120 if (PatternStr.size() < 2 ||
121 (PatternStr.find("{{") == StringRef::npos &&
122 PatternStr.find("[[") == StringRef::npos)) {
Chris Lattner2702e6a2009-09-25 17:09:12 +0000123 FixedStr = PatternStr;
124 return false;
125 }
126
Chris Lattnereec96952009-09-27 07:56:52 +0000127 // Paren value #0 is for the fully matched string. Any new parenthesized
128 // values add from their.
129 unsigned CurParen = 1;
130
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000131 // Otherwise, there is at least one regex piece. Build up the regex pattern
132 // by escaping scary characters in fixed strings, building up one big regex.
Chris Lattner52870082009-09-24 21:47:32 +0000133 while (!PatternStr.empty()) {
Chris Lattnereec96952009-09-27 07:56:52 +0000134 // RegEx matches.
135 if (PatternStr.size() >= 2 &&
136 PatternStr[0] == '{' && PatternStr[1] == '{') {
137
138 // Otherwise, this is the start of a regex match. Scan for the }}.
139 size_t End = PatternStr.find("}}");
140 if (End == StringRef::npos) {
141 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
142 "found start of regex string with no end '}}'", "error");
143 return true;
144 }
145
146 if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
147 return true;
148 PatternStr = PatternStr.substr(End+2);
Chris Lattner52870082009-09-24 21:47:32 +0000149 continue;
150 }
151
Chris Lattnereec96952009-09-27 07:56:52 +0000152 // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
153 // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
154 // second form is [[foo]] which is a reference to foo. The variable name
Daniel Dunbar964ac012009-11-22 22:07:50 +0000155 // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
Chris Lattnereec96952009-09-27 07:56:52 +0000156 // it. This is to catch some common errors.
157 if (PatternStr.size() >= 2 &&
158 PatternStr[0] == '[' && PatternStr[1] == '[') {
159 // Verify that it is terminated properly.
160 size_t End = PatternStr.find("]]");
161 if (End == StringRef::npos) {
162 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
163 "invalid named regex reference, no ]] found", "error");
164 return true;
165 }
166
167 StringRef MatchStr = PatternStr.substr(2, End-2);
168 PatternStr = PatternStr.substr(End+2);
169
170 // Get the regex name (e.g. "foo").
171 size_t NameEnd = MatchStr.find(':');
172 StringRef Name = MatchStr.substr(0, NameEnd);
173
174 if (Name.empty()) {
175 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
176 "invalid name in named regex: empty name", "error");
177 return true;
178 }
179
180 // Verify that the name is well formed.
181 for (unsigned i = 0, e = Name.size(); i != e; ++i)
Daniel Dunbar964ac012009-11-22 22:07:50 +0000182 if (Name[i] != '_' &&
183 (Name[i] < 'a' || Name[i] > 'z') &&
Chris Lattnereec96952009-09-27 07:56:52 +0000184 (Name[i] < 'A' || Name[i] > 'Z') &&
185 (Name[i] < '0' || Name[i] > '9')) {
186 SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i),
187 "invalid name in named regex", "error");
188 return true;
189 }
190
191 // Name can't start with a digit.
192 if (isdigit(Name[0])) {
193 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
194 "invalid name in named regex", "error");
195 return true;
196 }
197
198 // Handle [[foo]].
199 if (NameEnd == StringRef::npos) {
200 VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
201 continue;
202 }
203
204 // Handle [[foo:.*]].
205 VariableDefs.push_back(std::make_pair(Name, CurParen));
206 RegExStr += '(';
207 ++CurParen;
208
209 if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
210 return true;
211
212 RegExStr += ')';
Chris Lattner52870082009-09-24 21:47:32 +0000213 }
214
Chris Lattnereec96952009-09-27 07:56:52 +0000215 // Handle fixed string matches.
216 // Find the end, which is the start of the next regex.
217 size_t FixedMatchEnd = PatternStr.find("{{");
218 FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
219 AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr);
220 PatternStr = PatternStr.substr(FixedMatchEnd);
221 continue;
Chris Lattner52870082009-09-24 21:47:32 +0000222 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000223
Chris Lattnera29703e2009-09-24 20:39:13 +0000224 return false;
225}
226
Chris Lattnereec96952009-09-27 07:56:52 +0000227void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) {
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000228 // Add the characters from FixedStr to the regex, escaping as needed. This
229 // avoids "leaning toothpicks" in common patterns.
230 for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) {
231 switch (FixedStr[i]) {
232 // These are the special characters matched in "p_ere_exp".
233 case '(':
234 case ')':
235 case '^':
236 case '$':
237 case '|':
238 case '*':
239 case '+':
240 case '?':
241 case '.':
242 case '[':
243 case '\\':
244 case '{':
Chris Lattnereec96952009-09-27 07:56:52 +0000245 TheStr += '\\';
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000246 // FALL THROUGH.
247 default:
Chris Lattnereec96952009-09-27 07:56:52 +0000248 TheStr += FixedStr[i];
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000249 break;
250 }
251 }
252}
253
Chris Lattnereec96952009-09-27 07:56:52 +0000254bool Pattern::AddRegExToRegEx(StringRef RegexStr, unsigned &CurParen,
255 SourceMgr &SM) {
256 Regex R(RegexStr);
257 std::string Error;
258 if (!R.isValid(Error)) {
259 SM.PrintMessage(SMLoc::getFromPointer(RegexStr.data()),
260 "invalid regex: " + Error, "error");
261 return true;
262 }
263
264 RegExStr += RegexStr.str();
265 CurParen += R.getNumMatches();
266 return false;
267}
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000268
Chris Lattner52870082009-09-24 21:47:32 +0000269/// Match - Match the pattern string against the input buffer Buffer. This
270/// returns the position that is matched or npos if there is no match. If
271/// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +0000272size_t Pattern::Match(StringRef Buffer, size_t &MatchLen,
273 StringMap<StringRef> &VariableTable) const {
Chris Lattner2702e6a2009-09-25 17:09:12 +0000274 // If this is a fixed string pattern, just match it now.
275 if (!FixedStr.empty()) {
276 MatchLen = FixedStr.size();
277 return Buffer.find(FixedStr);
278 }
Chris Lattnereec96952009-09-27 07:56:52 +0000279
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000280 // Regex match.
Chris Lattnereec96952009-09-27 07:56:52 +0000281
282 // If there are variable uses, we need to create a temporary string with the
283 // actual value.
284 StringRef RegExToMatch = RegExStr;
285 std::string TmpStr;
286 if (!VariableUses.empty()) {
287 TmpStr = RegExStr;
288
289 unsigned InsertOffset = 0;
290 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000291 StringMap<StringRef>::iterator it =
292 VariableTable.find(VariableUses[i].first);
293 // If the variable is undefined, return an error.
294 if (it == VariableTable.end())
295 return StringRef::npos;
296
Chris Lattnereec96952009-09-27 07:56:52 +0000297 // Look up the value and escape it so that we can plop it into the regex.
298 std::string Value;
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000299 AddFixedStringToRegEx(it->second, Value);
Chris Lattnereec96952009-09-27 07:56:52 +0000300
301 // Plop it into the regex at the adjusted offset.
302 TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset,
303 Value.begin(), Value.end());
304 InsertOffset += Value.size();
305 }
306
307 // Match the newly constructed regex.
308 RegExToMatch = TmpStr;
309 }
310
311
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000312 SmallVector<StringRef, 4> MatchInfo;
Chris Lattnereec96952009-09-27 07:56:52 +0000313 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000314 return StringRef::npos;
Chris Lattner52870082009-09-24 21:47:32 +0000315
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000316 // Successful regex match.
317 assert(!MatchInfo.empty() && "Didn't get any match");
318 StringRef FullMatch = MatchInfo[0];
Chris Lattner52870082009-09-24 21:47:32 +0000319
Chris Lattnereec96952009-09-27 07:56:52 +0000320 // If this defines any variables, remember their values.
321 for (unsigned i = 0, e = VariableDefs.size(); i != e; ++i) {
322 assert(VariableDefs[i].second < MatchInfo.size() &&
323 "Internal paren error");
324 VariableTable[VariableDefs[i].first] = MatchInfo[VariableDefs[i].second];
Chris Lattner94638f02009-09-25 17:29:36 +0000325 }
Chris Lattner94638f02009-09-25 17:29:36 +0000326
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000327 MatchLen = FullMatch.size();
328 return FullMatch.data()-Buffer.data();
Chris Lattner52870082009-09-24 21:47:32 +0000329}
330
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000331unsigned Pattern::ComputeMatchDistance(StringRef Buffer,
332 const StringMap<StringRef> &VariableTable) const {
333 // Just compute the number of matching characters. For regular expressions, we
334 // just compare against the regex itself and hope for the best.
335 //
336 // FIXME: One easy improvement here is have the regex lib generate a single
337 // example regular expression which matches, and use that as the example
338 // string.
339 StringRef ExampleString(FixedStr);
340 if (ExampleString.empty())
341 ExampleString = RegExStr;
342
Daniel Dunbar8cdf54c2010-01-29 03:22:19 +0000343 return Buffer.substr(0, ExampleString.size()).edit_distance(ExampleString);
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000344}
345
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000346void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
347 const StringMap<StringRef> &VariableTable) const{
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000348 // If this was a regular expression using variables, print the current
349 // variable values.
350 if (!VariableUses.empty()) {
351 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
352 StringRef Var = VariableUses[i].first;
353 StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
354 SmallString<256> Msg;
355 raw_svector_ostream OS(Msg);
356
357 // Check for undefined variable references.
358 if (it == VariableTable.end()) {
359 OS << "uses undefined variable \"";
360 OS.write_escaped(Var) << "\"";;
361 } else {
362 OS << "with variable \"";
363 OS.write_escaped(Var) << "\" equal to \"";
364 OS.write_escaped(it->second) << "\"";
365 }
366
367 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), OS.str(), "note",
368 /*ShowLine=*/false);
369 }
370 }
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000371
372 // Attempt to find the closest/best fuzzy match. Usually an error happens
373 // because some string in the output didn't exactly match. In these cases, we
374 // would like to show the user a best guess at what "should have" matched, to
375 // save them having to actually check the input manually.
376 size_t NumLinesForward = 0;
377 size_t Best = StringRef::npos;
378 double BestQuality = 0;
379
380 // Use an arbitrary 4k limit on how far we will search.
Dan Gohmane3a1e502010-01-29 21:57:46 +0000381 for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000382 if (Buffer[i] == '\n')
383 ++NumLinesForward;
384
Dan Gohmand8a55412010-01-29 21:55:16 +0000385 // Patterns have leading whitespace stripped, so skip whitespace when
386 // looking for something which looks like a pattern.
387 if (Buffer[i] == ' ' || Buffer[i] == '\t')
388 continue;
389
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000390 // Compute the "quality" of this match as an arbitrary combination of the
391 // match distance and the number of lines skipped to get to this match.
392 unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable);
393 double Quality = Distance + (NumLinesForward / 100.);
394
395 if (Quality < BestQuality || Best == StringRef::npos) {
396 Best = i;
397 BestQuality = Quality;
398 }
399 }
400
Daniel Dunbara76a7882009-11-29 08:30:24 +0000401 if (Best != StringRef::npos && BestQuality < 50) {
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000402 // Print the "possible intended match here" line if we found something
403 // reasonable.
404 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
405 "possible intended match here", "note");
406
407 // FIXME: If we wanted to be really friendly we would show why the match
408 // failed, as it can be hard to spot simple one character differences.
409 }
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000410}
Chris Lattnera29703e2009-09-24 20:39:13 +0000411
412//===----------------------------------------------------------------------===//
413// Check Strings.
414//===----------------------------------------------------------------------===//
Chris Lattner9fc66782009-09-24 20:25:55 +0000415
416/// CheckString - This is a check that we found in the input file.
417struct CheckString {
418 /// Pat - The pattern to match.
419 Pattern Pat;
Chris Lattner207e1bc2009-08-15 17:41:04 +0000420
421 /// Loc - The location in the match file that the check string was specified.
422 SMLoc Loc;
423
Chris Lattner5dafafd2009-08-15 18:32:21 +0000424 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
425 /// to a CHECK: directive.
426 bool IsCheckNext;
427
Chris Lattnerf15380b2009-09-20 22:35:26 +0000428 /// NotStrings - These are all of the strings that are disallowed from
429 /// occurring between this match string and the previous one (or start of
430 /// file).
Chris Lattnera29703e2009-09-24 20:39:13 +0000431 std::vector<std::pair<SMLoc, Pattern> > NotStrings;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000432
Chris Lattner9fc66782009-09-24 20:25:55 +0000433 CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
434 : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
Chris Lattner207e1bc2009-08-15 17:41:04 +0000435};
436
Chris Lattneradea46e2009-09-24 20:45:07 +0000437/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
438/// memory buffer, free it, and return a new one.
439static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
440 SmallVector<char, 16> NewFile;
441 NewFile.reserve(MB->getBufferSize());
442
443 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
444 Ptr != End; ++Ptr) {
445 // If C is not a horizontal whitespace, skip it.
446 if (*Ptr != ' ' && *Ptr != '\t') {
447 NewFile.push_back(*Ptr);
448 continue;
449 }
450
451 // Otherwise, add one space and advance over neighboring space.
452 NewFile.push_back(' ');
453 while (Ptr+1 != End &&
454 (Ptr[1] == ' ' || Ptr[1] == '\t'))
455 ++Ptr;
456 }
457
458 // Free the old buffer and return a new one.
459 MemoryBuffer *MB2 =
460 MemoryBuffer::getMemBufferCopy(NewFile.data(),
461 NewFile.data() + NewFile.size(),
462 MB->getBufferIdentifier());
463
464 delete MB;
465 return MB2;
466}
467
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000468
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000469/// ReadCheckFile - Read the check file, which specifies the sequence of
470/// expected strings. The strings are added to the CheckStrings vector.
471static bool ReadCheckFile(SourceMgr &SM,
Chris Lattner207e1bc2009-08-15 17:41:04 +0000472 std::vector<CheckString> &CheckStrings) {
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000473 // Open the check file, and tell SourceMgr about it.
474 std::string ErrorStr;
475 MemoryBuffer *F =
476 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr);
477 if (F == 0) {
478 errs() << "Could not open check file '" << CheckFilename << "': "
479 << ErrorStr << '\n';
480 return true;
481 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000482
483 // If we want to canonicalize whitespace, strip excess whitespace from the
484 // buffer containing the CHECK lines.
485 if (!NoCanonicalizeWhiteSpace)
486 F = CanonicalizeInputFile(F);
487
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000488 SM.AddNewSourceBuffer(F, SMLoc());
489
Chris Lattnerd7e25052009-08-15 18:00:42 +0000490 // Find all instances of CheckPrefix followed by : in the file.
Chris Lattner96077032009-09-20 22:11:44 +0000491 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000492
Chris Lattnera29703e2009-09-24 20:39:13 +0000493 std::vector<std::pair<SMLoc, Pattern> > NotMatches;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000494
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000495 while (1) {
496 // See if Prefix occurs in the memory buffer.
Chris Lattner96077032009-09-20 22:11:44 +0000497 Buffer = Buffer.substr(Buffer.find(CheckPrefix));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000498
499 // If we didn't find a match, we're done.
Chris Lattner96077032009-09-20 22:11:44 +0000500 if (Buffer.empty())
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000501 break;
502
Chris Lattner96077032009-09-20 22:11:44 +0000503 const char *CheckPrefixStart = Buffer.data();
Chris Lattner5dafafd2009-08-15 18:32:21 +0000504
505 // When we find a check prefix, keep track of whether we find CHECK: or
506 // CHECK-NEXT:
Chris Lattnerf15380b2009-09-20 22:35:26 +0000507 bool IsCheckNext = false, IsCheckNot = false;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000508
Chris Lattnerd7e25052009-08-15 18:00:42 +0000509 // Verify that the : is present after the prefix.
Chris Lattner96077032009-09-20 22:11:44 +0000510 if (Buffer[CheckPrefix.size()] == ':') {
511 Buffer = Buffer.substr(CheckPrefix.size()+1);
Chris Lattner96077032009-09-20 22:11:44 +0000512 } else if (Buffer.size() > CheckPrefix.size()+6 &&
513 memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
514 Buffer = Buffer.substr(CheckPrefix.size()+7);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000515 IsCheckNext = true;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000516 } else if (Buffer.size() > CheckPrefix.size()+5 &&
517 memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) {
518 Buffer = Buffer.substr(CheckPrefix.size()+6);
519 IsCheckNot = true;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000520 } else {
Chris Lattner96077032009-09-20 22:11:44 +0000521 Buffer = Buffer.substr(1);
Chris Lattnerd7e25052009-08-15 18:00:42 +0000522 continue;
523 }
524
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000525 // Okay, we found the prefix, yay. Remember the rest of the line, but
526 // ignore leading and trailing whitespace.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000527 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000528
529 // Scan ahead to the end of line.
Chris Lattner96077032009-09-20 22:11:44 +0000530 size_t EOL = Buffer.find_first_of("\n\r");
Chris Lattnera29703e2009-09-24 20:39:13 +0000531
Dan Gohmane5463432010-01-29 21:53:18 +0000532 // Remember the location of the start of the pattern, for diagnostics.
533 SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
534
Chris Lattnera29703e2009-09-24 20:39:13 +0000535 // Parse the pattern.
536 Pattern P;
537 if (P.ParsePattern(Buffer.substr(0, EOL), SM))
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000538 return true;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000539
Chris Lattnera29703e2009-09-24 20:39:13 +0000540 Buffer = Buffer.substr(EOL);
541
Chris Lattnerf15380b2009-09-20 22:35:26 +0000542
Chris Lattner5dafafd2009-08-15 18:32:21 +0000543 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
544 if (IsCheckNext && CheckStrings.empty()) {
545 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
546 "found '"+CheckPrefix+"-NEXT:' without previous '"+
547 CheckPrefix+ ": line", "error");
548 return true;
549 }
550
Chris Lattnera29703e2009-09-24 20:39:13 +0000551 // Handle CHECK-NOT.
552 if (IsCheckNot) {
553 NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
554 P));
555 continue;
556 }
557
Chris Lattner9fc66782009-09-24 20:25:55 +0000558
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000559 // Okay, add the string we captured to the output vector and move on.
Chris Lattner9fc66782009-09-24 20:25:55 +0000560 CheckStrings.push_back(CheckString(P,
Dan Gohmane5463432010-01-29 21:53:18 +0000561 PatternLoc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000562 IsCheckNext));
Chris Lattnerf15380b2009-09-20 22:35:26 +0000563 std::swap(NotMatches, CheckStrings.back().NotStrings);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000564 }
565
566 if (CheckStrings.empty()) {
Chris Lattnerd7e25052009-08-15 18:00:42 +0000567 errs() << "error: no check strings found with prefix '" << CheckPrefix
568 << ":'\n";
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000569 return true;
570 }
571
Chris Lattnerf15380b2009-09-20 22:35:26 +0000572 if (!NotMatches.empty()) {
573 errs() << "error: '" << CheckPrefix
574 << "-NOT:' not supported after last check line.\n";
575 return true;
576 }
577
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000578 return false;
579}
580
Chris Lattner5dafafd2009-08-15 18:32:21 +0000581static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000582 StringRef Buffer,
583 StringMap<StringRef> &VariableTable) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000584 // Otherwise, we have an error, emit an error message.
585 SM.PrintMessage(CheckStr.Loc, "expected string not found in input",
586 "error");
587
588 // Print the "scanning from here" line. If the current position is at the
589 // end of a line, advance to the start of the next line.
Chris Lattner96077032009-09-20 22:11:44 +0000590 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
Chris Lattner5dafafd2009-08-15 18:32:21 +0000591
Chris Lattner96077032009-09-20 22:11:44 +0000592 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here",
Chris Lattner5dafafd2009-08-15 18:32:21 +0000593 "note");
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000594
595 // Allow the pattern to print additional information if desired.
596 CheckStr.Pat.PrintFailureInfo(SM, Buffer, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000597}
598
Chris Lattner3711b7a2009-09-20 22:42:44 +0000599/// CountNumNewlinesBetween - Count the number of newlines in the specified
600/// range.
601static unsigned CountNumNewlinesBetween(StringRef Range) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000602 unsigned NumNewLines = 0;
Chris Lattner3711b7a2009-09-20 22:42:44 +0000603 while (1) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000604 // Scan for newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000605 Range = Range.substr(Range.find_first_of("\n\r"));
606 if (Range.empty()) return NumNewLines;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000607
608 ++NumNewLines;
609
610 // Handle \n\r and \r\n as a single newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000611 if (Range.size() > 1 &&
612 (Range[1] == '\n' || Range[1] == '\r') &&
613 (Range[0] != Range[1]))
614 Range = Range.substr(1);
615 Range = Range.substr(1);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000616 }
Chris Lattner5dafafd2009-08-15 18:32:21 +0000617}
618
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000619int main(int argc, char **argv) {
620 sys::PrintStackTraceOnErrorSignal();
621 PrettyStackTraceProgram X(argc, argv);
622 cl::ParseCommandLineOptions(argc, argv);
623
624 SourceMgr SM;
625
626 // Read the expected strings from the check file.
Chris Lattner207e1bc2009-08-15 17:41:04 +0000627 std::vector<CheckString> CheckStrings;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000628 if (ReadCheckFile(SM, CheckStrings))
629 return 2;
630
631 // Open the file to check and add it to SourceMgr.
632 std::string ErrorStr;
633 MemoryBuffer *F =
634 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
635 if (F == 0) {
636 errs() << "Could not open input file '" << InputFilename << "': "
637 << ErrorStr << '\n';
638 return true;
639 }
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000640
641 // Remove duplicate spaces in the input file if requested.
642 if (!NoCanonicalizeWhiteSpace)
643 F = CanonicalizeInputFile(F);
644
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000645 SM.AddNewSourceBuffer(F, SMLoc());
646
Chris Lattnereec96952009-09-27 07:56:52 +0000647 /// VariableTable - This holds all the current filecheck variables.
648 StringMap<StringRef> VariableTable;
649
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000650 // Check that we have all of the expected strings, in order, in the input
651 // file.
Chris Lattner96077032009-09-20 22:11:44 +0000652 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000653
Chris Lattnerf15380b2009-09-20 22:35:26 +0000654 const char *LastMatch = Buffer.data();
655
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000656 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
Chris Lattner207e1bc2009-08-15 17:41:04 +0000657 const CheckString &CheckStr = CheckStrings[StrNo];
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000658
Chris Lattner96077032009-09-20 22:11:44 +0000659 StringRef SearchFrom = Buffer;
660
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000661 // Find StrNo in the file.
Chris Lattner9fc66782009-09-24 20:25:55 +0000662 size_t MatchLen = 0;
Chris Lattnereec96952009-09-27 07:56:52 +0000663 Buffer = Buffer.substr(CheckStr.Pat.Match(Buffer, MatchLen, VariableTable));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000664
Chris Lattner5dafafd2009-08-15 18:32:21 +0000665 // If we didn't find a match, reject the input.
Chris Lattner96077032009-09-20 22:11:44 +0000666 if (Buffer.empty()) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000667 PrintCheckFailed(SM, CheckStr, SearchFrom, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000668 return 1;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000669 }
Chris Lattner3711b7a2009-09-20 22:42:44 +0000670
671 StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
672
Chris Lattner5dafafd2009-08-15 18:32:21 +0000673 // If this check is a "CHECK-NEXT", verify that the previous match was on
674 // the previous line (i.e. that there is one newline between them).
675 if (CheckStr.IsCheckNext) {
676 // Count the number of newlines between the previous match and this one.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000677 assert(LastMatch != F->getBufferStart() &&
678 "CHECK-NEXT can't be the first check in a file");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000679
Chris Lattner3711b7a2009-09-20 22:42:44 +0000680 unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000681 if (NumNewLines == 0) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000682 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000683 CheckPrefix+"-NEXT: is on the same line as previous match",
684 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000685 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000686 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000687 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
688 "previous match was here", "note");
689 return 1;
690 }
691
692 if (NumNewLines != 1) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000693 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000694 CheckPrefix+
695 "-NEXT: is not on the line after the previous match",
696 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000697 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000698 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000699 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
700 "previous match was here", "note");
701 return 1;
702 }
703 }
Chris Lattnerf15380b2009-09-20 22:35:26 +0000704
705 // If this match had "not strings", verify that they don't exist in the
706 // skipped region.
Chris Lattnereec96952009-09-27 07:56:52 +0000707 for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size();
708 ChunkNo != e; ++ChunkNo) {
Chris Lattnera29703e2009-09-24 20:39:13 +0000709 size_t MatchLen = 0;
Chris Lattnereec96952009-09-27 07:56:52 +0000710 size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion,
711 MatchLen,
712 VariableTable);
Chris Lattnerf15380b2009-09-20 22:35:26 +0000713 if (Pos == StringRef::npos) continue;
714
715 SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos),
716 CheckPrefix+"-NOT: string occurred!", "error");
Chris Lattner52870082009-09-24 21:47:32 +0000717 SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first,
Chris Lattnerf15380b2009-09-20 22:35:26 +0000718 CheckPrefix+"-NOT: pattern specified here", "note");
719 return 1;
720 }
721
Chris Lattner5dafafd2009-08-15 18:32:21 +0000722
Chris Lattner81115762009-09-21 02:30:42 +0000723 // Otherwise, everything is good. Step over the matched text and remember
724 // the position after the match as the end of the last match.
Chris Lattner9fc66782009-09-24 20:25:55 +0000725 Buffer = Buffer.substr(MatchLen);
Chris Lattner81115762009-09-21 02:30:42 +0000726 LastMatch = Buffer.data();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000727 }
728
729 return 0;
730}