blob: c86993d0e7c7c1d8acb72f5b317076d73ed29595 [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);
Chris Lattner9fc66782009-09-24 20:25:55 +000095};
96
Chris Lattnereec96952009-09-27 07:56:52 +000097
Chris Lattnera29703e2009-09-24 20:39:13 +000098bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) {
Chris Lattner94638f02009-09-25 17:29:36 +000099 PatternLoc = SMLoc::getFromPointer(PatternStr.data());
100
Chris Lattnera29703e2009-09-24 20:39:13 +0000101 // Ignore trailing whitespace.
102 while (!PatternStr.empty() &&
103 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
104 PatternStr = PatternStr.substr(0, PatternStr.size()-1);
105
106 // Check that there is something on the line.
107 if (PatternStr.empty()) {
Chris Lattner94638f02009-09-25 17:29:36 +0000108 SM.PrintMessage(PatternLoc, "found empty check string with prefix '" +
109 CheckPrefix+":'", "error");
Chris Lattnera29703e2009-09-24 20:39:13 +0000110 return true;
111 }
Chris Lattner52870082009-09-24 21:47:32 +0000112
Chris Lattner2702e6a2009-09-25 17:09:12 +0000113 // Check to see if this is a fixed string, or if it has regex pieces.
Chris Lattnereec96952009-09-27 07:56:52 +0000114 if (PatternStr.size() < 2 ||
115 (PatternStr.find("{{") == StringRef::npos &&
116 PatternStr.find("[[") == StringRef::npos)) {
Chris Lattner2702e6a2009-09-25 17:09:12 +0000117 FixedStr = PatternStr;
118 return false;
119 }
120
Chris Lattnereec96952009-09-27 07:56:52 +0000121 // Paren value #0 is for the fully matched string. Any new parenthesized
122 // values add from their.
123 unsigned CurParen = 1;
124
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000125 // Otherwise, there is at least one regex piece. Build up the regex pattern
126 // by escaping scary characters in fixed strings, building up one big regex.
Chris Lattner52870082009-09-24 21:47:32 +0000127 while (!PatternStr.empty()) {
Chris Lattnereec96952009-09-27 07:56:52 +0000128 // RegEx matches.
129 if (PatternStr.size() >= 2 &&
130 PatternStr[0] == '{' && PatternStr[1] == '{') {
131
132 // Otherwise, this is the start of a regex match. Scan for the }}.
133 size_t End = PatternStr.find("}}");
134 if (End == StringRef::npos) {
135 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
136 "found start of regex string with no end '}}'", "error");
137 return true;
138 }
139
140 if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
141 return true;
142 PatternStr = PatternStr.substr(End+2);
Chris Lattner52870082009-09-24 21:47:32 +0000143 continue;
144 }
145
Chris Lattnereec96952009-09-27 07:56:52 +0000146 // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
147 // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
148 // second form is [[foo]] which is a reference to foo. The variable name
Daniel Dunbar964ac012009-11-22 22:07:50 +0000149 // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
Chris Lattnereec96952009-09-27 07:56:52 +0000150 // it. This is to catch some common errors.
151 if (PatternStr.size() >= 2 &&
152 PatternStr[0] == '[' && PatternStr[1] == '[') {
153 // Verify that it is terminated properly.
154 size_t End = PatternStr.find("]]");
155 if (End == StringRef::npos) {
156 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
157 "invalid named regex reference, no ]] found", "error");
158 return true;
159 }
160
161 StringRef MatchStr = PatternStr.substr(2, End-2);
162 PatternStr = PatternStr.substr(End+2);
163
164 // Get the regex name (e.g. "foo").
165 size_t NameEnd = MatchStr.find(':');
166 StringRef Name = MatchStr.substr(0, NameEnd);
167
168 if (Name.empty()) {
169 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
170 "invalid name in named regex: empty name", "error");
171 return true;
172 }
173
174 // Verify that the name is well formed.
175 for (unsigned i = 0, e = Name.size(); i != e; ++i)
Daniel Dunbar964ac012009-11-22 22:07:50 +0000176 if (Name[i] != '_' &&
177 (Name[i] < 'a' || Name[i] > 'z') &&
Chris Lattnereec96952009-09-27 07:56:52 +0000178 (Name[i] < 'A' || Name[i] > 'Z') &&
179 (Name[i] < '0' || Name[i] > '9')) {
180 SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i),
181 "invalid name in named regex", "error");
182 return true;
183 }
184
185 // Name can't start with a digit.
186 if (isdigit(Name[0])) {
187 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
188 "invalid name in named regex", "error");
189 return true;
190 }
191
192 // Handle [[foo]].
193 if (NameEnd == StringRef::npos) {
194 VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
195 continue;
196 }
197
198 // Handle [[foo:.*]].
199 VariableDefs.push_back(std::make_pair(Name, CurParen));
200 RegExStr += '(';
201 ++CurParen;
202
203 if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
204 return true;
205
206 RegExStr += ')';
Chris Lattner52870082009-09-24 21:47:32 +0000207 }
208
Chris Lattnereec96952009-09-27 07:56:52 +0000209 // Handle fixed string matches.
210 // Find the end, which is the start of the next regex.
211 size_t FixedMatchEnd = PatternStr.find("{{");
212 FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
213 AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr);
214 PatternStr = PatternStr.substr(FixedMatchEnd);
215 continue;
Chris Lattner52870082009-09-24 21:47:32 +0000216 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000217
Chris Lattnera29703e2009-09-24 20:39:13 +0000218 return false;
219}
220
Chris Lattnereec96952009-09-27 07:56:52 +0000221void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) {
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000222 // Add the characters from FixedStr to the regex, escaping as needed. This
223 // avoids "leaning toothpicks" in common patterns.
224 for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) {
225 switch (FixedStr[i]) {
226 // These are the special characters matched in "p_ere_exp".
227 case '(':
228 case ')':
229 case '^':
230 case '$':
231 case '|':
232 case '*':
233 case '+':
234 case '?':
235 case '.':
236 case '[':
237 case '\\':
238 case '{':
Chris Lattnereec96952009-09-27 07:56:52 +0000239 TheStr += '\\';
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000240 // FALL THROUGH.
241 default:
Chris Lattnereec96952009-09-27 07:56:52 +0000242 TheStr += FixedStr[i];
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000243 break;
244 }
245 }
246}
247
Chris Lattnereec96952009-09-27 07:56:52 +0000248bool Pattern::AddRegExToRegEx(StringRef RegexStr, unsigned &CurParen,
249 SourceMgr &SM) {
250 Regex R(RegexStr);
251 std::string Error;
252 if (!R.isValid(Error)) {
253 SM.PrintMessage(SMLoc::getFromPointer(RegexStr.data()),
254 "invalid regex: " + Error, "error");
255 return true;
256 }
257
258 RegExStr += RegexStr.str();
259 CurParen += R.getNumMatches();
260 return false;
261}
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000262
Chris Lattner52870082009-09-24 21:47:32 +0000263/// Match - Match the pattern string against the input buffer Buffer. This
264/// returns the position that is matched or npos if there is no match. If
265/// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +0000266size_t Pattern::Match(StringRef Buffer, size_t &MatchLen,
267 StringMap<StringRef> &VariableTable) const {
Chris Lattner2702e6a2009-09-25 17:09:12 +0000268 // If this is a fixed string pattern, just match it now.
269 if (!FixedStr.empty()) {
270 MatchLen = FixedStr.size();
271 return Buffer.find(FixedStr);
272 }
Chris Lattnereec96952009-09-27 07:56:52 +0000273
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000274 // Regex match.
Chris Lattnereec96952009-09-27 07:56:52 +0000275
276 // If there are variable uses, we need to create a temporary string with the
277 // actual value.
278 StringRef RegExToMatch = RegExStr;
279 std::string TmpStr;
280 if (!VariableUses.empty()) {
281 TmpStr = RegExStr;
282
283 unsigned InsertOffset = 0;
284 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000285 StringMap<StringRef>::iterator it =
286 VariableTable.find(VariableUses[i].first);
287 // If the variable is undefined, return an error.
288 if (it == VariableTable.end())
289 return StringRef::npos;
290
Chris Lattnereec96952009-09-27 07:56:52 +0000291 // Look up the value and escape it so that we can plop it into the regex.
292 std::string Value;
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000293 AddFixedStringToRegEx(it->second, Value);
Chris Lattnereec96952009-09-27 07:56:52 +0000294
295 // Plop it into the regex at the adjusted offset.
296 TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset,
297 Value.begin(), Value.end());
298 InsertOffset += Value.size();
299 }
300
301 // Match the newly constructed regex.
302 RegExToMatch = TmpStr;
303 }
304
305
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000306 SmallVector<StringRef, 4> MatchInfo;
Chris Lattnereec96952009-09-27 07:56:52 +0000307 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000308 return StringRef::npos;
Chris Lattner52870082009-09-24 21:47:32 +0000309
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000310 // Successful regex match.
311 assert(!MatchInfo.empty() && "Didn't get any match");
312 StringRef FullMatch = MatchInfo[0];
Chris Lattner52870082009-09-24 21:47:32 +0000313
Chris Lattnereec96952009-09-27 07:56:52 +0000314 // If this defines any variables, remember their values.
315 for (unsigned i = 0, e = VariableDefs.size(); i != e; ++i) {
316 assert(VariableDefs[i].second < MatchInfo.size() &&
317 "Internal paren error");
318 VariableTable[VariableDefs[i].first] = MatchInfo[VariableDefs[i].second];
Chris Lattner94638f02009-09-25 17:29:36 +0000319 }
Chris Lattner94638f02009-09-25 17:29:36 +0000320
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000321 MatchLen = FullMatch.size();
322 return FullMatch.data()-Buffer.data();
Chris Lattner52870082009-09-24 21:47:32 +0000323}
324
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000325void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
326 const StringMap<StringRef> &VariableTable) const{
327 // If this is a fixed string, do nothing.
328 if (!FixedStr.empty())
329 return;
330
331 // If this was a regular expression using variables, print the current
332 // variable values.
333 if (!VariableUses.empty()) {
334 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
335 StringRef Var = VariableUses[i].first;
336 StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
337 SmallString<256> Msg;
338 raw_svector_ostream OS(Msg);
339
340 // Check for undefined variable references.
341 if (it == VariableTable.end()) {
342 OS << "uses undefined variable \"";
343 OS.write_escaped(Var) << "\"";;
344 } else {
345 OS << "with variable \"";
346 OS.write_escaped(Var) << "\" equal to \"";
347 OS.write_escaped(it->second) << "\"";
348 }
349
350 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), OS.str(), "note",
351 /*ShowLine=*/false);
352 }
353 }
354}
Chris Lattnera29703e2009-09-24 20:39:13 +0000355
356//===----------------------------------------------------------------------===//
357// Check Strings.
358//===----------------------------------------------------------------------===//
Chris Lattner9fc66782009-09-24 20:25:55 +0000359
360/// CheckString - This is a check that we found in the input file.
361struct CheckString {
362 /// Pat - The pattern to match.
363 Pattern Pat;
Chris Lattner207e1bc2009-08-15 17:41:04 +0000364
365 /// Loc - The location in the match file that the check string was specified.
366 SMLoc Loc;
367
Chris Lattner5dafafd2009-08-15 18:32:21 +0000368 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
369 /// to a CHECK: directive.
370 bool IsCheckNext;
371
Chris Lattnerf15380b2009-09-20 22:35:26 +0000372 /// NotStrings - These are all of the strings that are disallowed from
373 /// occurring between this match string and the previous one (or start of
374 /// file).
Chris Lattnera29703e2009-09-24 20:39:13 +0000375 std::vector<std::pair<SMLoc, Pattern> > NotStrings;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000376
Chris Lattner9fc66782009-09-24 20:25:55 +0000377 CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
378 : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
Chris Lattner207e1bc2009-08-15 17:41:04 +0000379};
380
Chris Lattneradea46e2009-09-24 20:45:07 +0000381/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
382/// memory buffer, free it, and return a new one.
383static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
384 SmallVector<char, 16> NewFile;
385 NewFile.reserve(MB->getBufferSize());
386
387 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
388 Ptr != End; ++Ptr) {
389 // If C is not a horizontal whitespace, skip it.
390 if (*Ptr != ' ' && *Ptr != '\t') {
391 NewFile.push_back(*Ptr);
392 continue;
393 }
394
395 // Otherwise, add one space and advance over neighboring space.
396 NewFile.push_back(' ');
397 while (Ptr+1 != End &&
398 (Ptr[1] == ' ' || Ptr[1] == '\t'))
399 ++Ptr;
400 }
401
402 // Free the old buffer and return a new one.
403 MemoryBuffer *MB2 =
404 MemoryBuffer::getMemBufferCopy(NewFile.data(),
405 NewFile.data() + NewFile.size(),
406 MB->getBufferIdentifier());
407
408 delete MB;
409 return MB2;
410}
411
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000412
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000413/// ReadCheckFile - Read the check file, which specifies the sequence of
414/// expected strings. The strings are added to the CheckStrings vector.
415static bool ReadCheckFile(SourceMgr &SM,
Chris Lattner207e1bc2009-08-15 17:41:04 +0000416 std::vector<CheckString> &CheckStrings) {
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000417 // Open the check file, and tell SourceMgr about it.
418 std::string ErrorStr;
419 MemoryBuffer *F =
420 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr);
421 if (F == 0) {
422 errs() << "Could not open check file '" << CheckFilename << "': "
423 << ErrorStr << '\n';
424 return true;
425 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000426
427 // If we want to canonicalize whitespace, strip excess whitespace from the
428 // buffer containing the CHECK lines.
429 if (!NoCanonicalizeWhiteSpace)
430 F = CanonicalizeInputFile(F);
431
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000432 SM.AddNewSourceBuffer(F, SMLoc());
433
Chris Lattnerd7e25052009-08-15 18:00:42 +0000434 // Find all instances of CheckPrefix followed by : in the file.
Chris Lattner96077032009-09-20 22:11:44 +0000435 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000436
Chris Lattnera29703e2009-09-24 20:39:13 +0000437 std::vector<std::pair<SMLoc, Pattern> > NotMatches;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000438
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000439 while (1) {
440 // See if Prefix occurs in the memory buffer.
Chris Lattner96077032009-09-20 22:11:44 +0000441 Buffer = Buffer.substr(Buffer.find(CheckPrefix));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000442
443 // If we didn't find a match, we're done.
Chris Lattner96077032009-09-20 22:11:44 +0000444 if (Buffer.empty())
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000445 break;
446
Chris Lattner96077032009-09-20 22:11:44 +0000447 const char *CheckPrefixStart = Buffer.data();
Chris Lattner5dafafd2009-08-15 18:32:21 +0000448
449 // When we find a check prefix, keep track of whether we find CHECK: or
450 // CHECK-NEXT:
Chris Lattnerf15380b2009-09-20 22:35:26 +0000451 bool IsCheckNext = false, IsCheckNot = false;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000452
Chris Lattnerd7e25052009-08-15 18:00:42 +0000453 // Verify that the : is present after the prefix.
Chris Lattner96077032009-09-20 22:11:44 +0000454 if (Buffer[CheckPrefix.size()] == ':') {
455 Buffer = Buffer.substr(CheckPrefix.size()+1);
Chris Lattner96077032009-09-20 22:11:44 +0000456 } else if (Buffer.size() > CheckPrefix.size()+6 &&
457 memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
458 Buffer = Buffer.substr(CheckPrefix.size()+7);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000459 IsCheckNext = true;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000460 } else if (Buffer.size() > CheckPrefix.size()+5 &&
461 memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) {
462 Buffer = Buffer.substr(CheckPrefix.size()+6);
463 IsCheckNot = true;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000464 } else {
Chris Lattner96077032009-09-20 22:11:44 +0000465 Buffer = Buffer.substr(1);
Chris Lattnerd7e25052009-08-15 18:00:42 +0000466 continue;
467 }
468
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000469 // Okay, we found the prefix, yay. Remember the rest of the line, but
470 // ignore leading and trailing whitespace.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000471 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000472
473 // Scan ahead to the end of line.
Chris Lattner96077032009-09-20 22:11:44 +0000474 size_t EOL = Buffer.find_first_of("\n\r");
Chris Lattnera29703e2009-09-24 20:39:13 +0000475
476 // Parse the pattern.
477 Pattern P;
478 if (P.ParsePattern(Buffer.substr(0, EOL), SM))
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000479 return true;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000480
Chris Lattnera29703e2009-09-24 20:39:13 +0000481 Buffer = Buffer.substr(EOL);
482
Chris Lattnerf15380b2009-09-20 22:35:26 +0000483
Chris Lattner5dafafd2009-08-15 18:32:21 +0000484 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
485 if (IsCheckNext && CheckStrings.empty()) {
486 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
487 "found '"+CheckPrefix+"-NEXT:' without previous '"+
488 CheckPrefix+ ": line", "error");
489 return true;
490 }
491
Chris Lattnera29703e2009-09-24 20:39:13 +0000492 // Handle CHECK-NOT.
493 if (IsCheckNot) {
494 NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
495 P));
496 continue;
497 }
498
Chris Lattner9fc66782009-09-24 20:25:55 +0000499
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000500 // Okay, add the string we captured to the output vector and move on.
Chris Lattner9fc66782009-09-24 20:25:55 +0000501 CheckStrings.push_back(CheckString(P,
Chris Lattner96077032009-09-20 22:11:44 +0000502 SMLoc::getFromPointer(Buffer.data()),
Chris Lattner5dafafd2009-08-15 18:32:21 +0000503 IsCheckNext));
Chris Lattnerf15380b2009-09-20 22:35:26 +0000504 std::swap(NotMatches, CheckStrings.back().NotStrings);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000505 }
506
507 if (CheckStrings.empty()) {
Chris Lattnerd7e25052009-08-15 18:00:42 +0000508 errs() << "error: no check strings found with prefix '" << CheckPrefix
509 << ":'\n";
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000510 return true;
511 }
512
Chris Lattnerf15380b2009-09-20 22:35:26 +0000513 if (!NotMatches.empty()) {
514 errs() << "error: '" << CheckPrefix
515 << "-NOT:' not supported after last check line.\n";
516 return true;
517 }
518
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000519 return false;
520}
521
Chris Lattner5dafafd2009-08-15 18:32:21 +0000522static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000523 StringRef Buffer,
524 StringMap<StringRef> &VariableTable) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000525 // Otherwise, we have an error, emit an error message.
526 SM.PrintMessage(CheckStr.Loc, "expected string not found in input",
527 "error");
528
529 // Print the "scanning from here" line. If the current position is at the
530 // end of a line, advance to the start of the next line.
Chris Lattner96077032009-09-20 22:11:44 +0000531 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
Chris Lattner5dafafd2009-08-15 18:32:21 +0000532
Chris Lattner96077032009-09-20 22:11:44 +0000533 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here",
Chris Lattner5dafafd2009-08-15 18:32:21 +0000534 "note");
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000535
536 // Allow the pattern to print additional information if desired.
537 CheckStr.Pat.PrintFailureInfo(SM, Buffer, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000538}
539
Chris Lattner3711b7a2009-09-20 22:42:44 +0000540/// CountNumNewlinesBetween - Count the number of newlines in the specified
541/// range.
542static unsigned CountNumNewlinesBetween(StringRef Range) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000543 unsigned NumNewLines = 0;
Chris Lattner3711b7a2009-09-20 22:42:44 +0000544 while (1) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000545 // Scan for newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000546 Range = Range.substr(Range.find_first_of("\n\r"));
547 if (Range.empty()) return NumNewLines;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000548
549 ++NumNewLines;
550
551 // Handle \n\r and \r\n as a single newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000552 if (Range.size() > 1 &&
553 (Range[1] == '\n' || Range[1] == '\r') &&
554 (Range[0] != Range[1]))
555 Range = Range.substr(1);
556 Range = Range.substr(1);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000557 }
Chris Lattner5dafafd2009-08-15 18:32:21 +0000558}
559
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000560int main(int argc, char **argv) {
561 sys::PrintStackTraceOnErrorSignal();
562 PrettyStackTraceProgram X(argc, argv);
563 cl::ParseCommandLineOptions(argc, argv);
564
565 SourceMgr SM;
566
567 // Read the expected strings from the check file.
Chris Lattner207e1bc2009-08-15 17:41:04 +0000568 std::vector<CheckString> CheckStrings;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000569 if (ReadCheckFile(SM, CheckStrings))
570 return 2;
571
572 // Open the file to check and add it to SourceMgr.
573 std::string ErrorStr;
574 MemoryBuffer *F =
575 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
576 if (F == 0) {
577 errs() << "Could not open input file '" << InputFilename << "': "
578 << ErrorStr << '\n';
579 return true;
580 }
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000581
582 // Remove duplicate spaces in the input file if requested.
583 if (!NoCanonicalizeWhiteSpace)
584 F = CanonicalizeInputFile(F);
585
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000586 SM.AddNewSourceBuffer(F, SMLoc());
587
Chris Lattnereec96952009-09-27 07:56:52 +0000588 /// VariableTable - This holds all the current filecheck variables.
589 StringMap<StringRef> VariableTable;
590
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000591 // Check that we have all of the expected strings, in order, in the input
592 // file.
Chris Lattner96077032009-09-20 22:11:44 +0000593 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000594
Chris Lattnerf15380b2009-09-20 22:35:26 +0000595 const char *LastMatch = Buffer.data();
596
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000597 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
Chris Lattner207e1bc2009-08-15 17:41:04 +0000598 const CheckString &CheckStr = CheckStrings[StrNo];
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000599
Chris Lattner96077032009-09-20 22:11:44 +0000600 StringRef SearchFrom = Buffer;
601
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000602 // Find StrNo in the file.
Chris Lattner9fc66782009-09-24 20:25:55 +0000603 size_t MatchLen = 0;
Chris Lattnereec96952009-09-27 07:56:52 +0000604 Buffer = Buffer.substr(CheckStr.Pat.Match(Buffer, MatchLen, VariableTable));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000605
Chris Lattner5dafafd2009-08-15 18:32:21 +0000606 // If we didn't find a match, reject the input.
Chris Lattner96077032009-09-20 22:11:44 +0000607 if (Buffer.empty()) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000608 PrintCheckFailed(SM, CheckStr, SearchFrom, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000609 return 1;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000610 }
Chris Lattner3711b7a2009-09-20 22:42:44 +0000611
612 StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
613
Chris Lattner5dafafd2009-08-15 18:32:21 +0000614 // If this check is a "CHECK-NEXT", verify that the previous match was on
615 // the previous line (i.e. that there is one newline between them).
616 if (CheckStr.IsCheckNext) {
617 // Count the number of newlines between the previous match and this one.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000618 assert(LastMatch != F->getBufferStart() &&
619 "CHECK-NEXT can't be the first check in a file");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000620
Chris Lattner3711b7a2009-09-20 22:42:44 +0000621 unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000622 if (NumNewLines == 0) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000623 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000624 CheckPrefix+"-NEXT: is on the same line as previous match",
625 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000626 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000627 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000628 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
629 "previous match was here", "note");
630 return 1;
631 }
632
633 if (NumNewLines != 1) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000634 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000635 CheckPrefix+
636 "-NEXT: is not on the line after the previous match",
637 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000638 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000639 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000640 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
641 "previous match was here", "note");
642 return 1;
643 }
644 }
Chris Lattnerf15380b2009-09-20 22:35:26 +0000645
646 // If this match had "not strings", verify that they don't exist in the
647 // skipped region.
Chris Lattnereec96952009-09-27 07:56:52 +0000648 for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size();
649 ChunkNo != e; ++ChunkNo) {
Chris Lattnera29703e2009-09-24 20:39:13 +0000650 size_t MatchLen = 0;
Chris Lattnereec96952009-09-27 07:56:52 +0000651 size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion,
652 MatchLen,
653 VariableTable);
Chris Lattnerf15380b2009-09-20 22:35:26 +0000654 if (Pos == StringRef::npos) continue;
655
656 SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos),
657 CheckPrefix+"-NOT: string occurred!", "error");
Chris Lattner52870082009-09-24 21:47:32 +0000658 SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first,
Chris Lattnerf15380b2009-09-20 22:35:26 +0000659 CheckPrefix+"-NOT: pattern specified here", "note");
660 return 1;
661 }
662
Chris Lattner5dafafd2009-08-15 18:32:21 +0000663
Chris Lattner81115762009-09-21 02:30:42 +0000664 // Otherwise, everything is good. Step over the matched text and remember
665 // the position after the match as the end of the last match.
Chris Lattner9fc66782009-09-24 20:25:55 +0000666 Buffer = Buffer.substr(MatchLen);
Chris Lattner81115762009-09-21 02:30:42 +0000667 LastMatch = Buffer.data();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000668 }
669
670 return 0;
671}