blob: f2255948658e8847a3c2220cb3baa579c3e7abb4 [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
Michael J. Spencer3ff95632010-12-16 03:29:14 +000019#include "llvm/ADT/OwningPtr.h"
Chris Lattner81cb8ca2009-07-08 18:44:05 +000020#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner52870082009-09-24 21:47:32 +000023#include "llvm/Support/Regex.h"
Chris Lattner81cb8ca2009-07-08 18:44:05 +000024#include "llvm/Support/SourceMgr.h"
25#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000026#include "llvm/Support/Signals.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000027#include "llvm/Support/system_error.h"
Daniel Dunbarfafe93c2009-11-22 22:08:06 +000028#include "llvm/ADT/SmallString.h"
Chris Lattnereec96952009-09-27 07:56:52 +000029#include "llvm/ADT/StringMap.h"
30#include <algorithm>
Chris Lattner81cb8ca2009-07-08 18:44:05 +000031using namespace llvm;
32
33static cl::opt<std::string>
34CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
35
36static cl::opt<std::string>
37InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
38 cl::init("-"), cl::value_desc("filename"));
39
40static cl::opt<std::string>
41CheckPrefix("check-prefix", cl::init("CHECK"),
42 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
43
Chris Lattner88a7e9e2009-07-11 18:58:15 +000044static cl::opt<bool>
45NoCanonicalizeWhiteSpace("strict-whitespace",
46 cl::desc("Do not treat all horizontal whitespace as equivalent"));
47
Chris Lattnera29703e2009-09-24 20:39:13 +000048//===----------------------------------------------------------------------===//
49// Pattern Handling Code.
50//===----------------------------------------------------------------------===//
51
Chris Lattner9fc66782009-09-24 20:25:55 +000052class Pattern {
Chris Lattner94638f02009-09-25 17:29:36 +000053 SMLoc PatternLoc;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000054
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +000055 /// MatchEOF - When set, this pattern only matches the end of file. This is
56 /// used for trailing CHECK-NOTs.
57 bool MatchEOF;
58
Chris Lattner5d6a05f2009-09-25 17:23:43 +000059 /// FixedStr - If non-empty, this pattern is a fixed string match with the
60 /// specified fixed string.
Chris Lattner2702e6a2009-09-25 17:09:12 +000061 StringRef FixedStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000062
Chris Lattner5d6a05f2009-09-25 17:23:43 +000063 /// RegEx - If non-empty, this is a regex pattern.
64 std::string RegExStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000065
Chris Lattnereec96952009-09-27 07:56:52 +000066 /// VariableUses - Entries in this vector map to uses of a variable in the
67 /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain
68 /// "foobaz" and we'll get an entry in this vector that tells us to insert the
69 /// value of bar at offset 3.
70 std::vector<std::pair<StringRef, unsigned> > VariableUses;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000071
Chris Lattnereec96952009-09-27 07:56:52 +000072 /// VariableDefs - Entries in this vector map to definitions of a variable in
73 /// the pattern, e.g. "foo[[bar:.*]]baz". In this case, the RegExStr will
74 /// contain "foo(.*)baz" and VariableDefs will contain the pair "bar",1. The
75 /// index indicates what parenthesized value captures the variable value.
76 std::vector<std::pair<StringRef, unsigned> > VariableDefs;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000077
Chris Lattner9fc66782009-09-24 20:25:55 +000078public:
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000079
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +000080 Pattern(bool matchEOF = false) : MatchEOF(matchEOF) { }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000081
Chris Lattnera29703e2009-09-24 20:39:13 +000082 bool ParsePattern(StringRef PatternStr, SourceMgr &SM);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000083
Chris Lattner9fc66782009-09-24 20:25:55 +000084 /// Match - Match the pattern string against the input buffer Buffer. This
85 /// returns the position that is matched or npos if there is no match. If
86 /// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +000087 ///
88 /// The VariableTable StringMap provides the current values of filecheck
89 /// variables and is updated if this match defines new values.
90 size_t Match(StringRef Buffer, size_t &MatchLen,
91 StringMap<StringRef> &VariableTable) const;
Daniel Dunbarfafe93c2009-11-22 22:08:06 +000092
93 /// PrintFailureInfo - Print additional information about a failure to match
94 /// involving this pattern.
95 void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
96 const StringMap<StringRef> &VariableTable) const;
97
Chris Lattner5d6a05f2009-09-25 17:23:43 +000098private:
Chris Lattnereec96952009-09-27 07:56:52 +000099 static void AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr);
100 bool AddRegExToRegEx(StringRef RegExStr, unsigned &CurParen, SourceMgr &SM);
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000101
102 /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of
103 /// matching this pattern at the start of \arg Buffer; a distance of zero
104 /// should correspond to a perfect match.
105 unsigned ComputeMatchDistance(StringRef Buffer,
106 const StringMap<StringRef> &VariableTable) const;
Chris Lattner9fc66782009-09-24 20:25:55 +0000107};
108
Chris Lattnereec96952009-09-27 07:56:52 +0000109
Chris Lattnera29703e2009-09-24 20:39:13 +0000110bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) {
Chris Lattner94638f02009-09-25 17:29:36 +0000111 PatternLoc = SMLoc::getFromPointer(PatternStr.data());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000112
Chris Lattnera29703e2009-09-24 20:39:13 +0000113 // Ignore trailing whitespace.
114 while (!PatternStr.empty() &&
115 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
116 PatternStr = PatternStr.substr(0, PatternStr.size()-1);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000117
Chris Lattnera29703e2009-09-24 20:39:13 +0000118 // Check that there is something on the line.
119 if (PatternStr.empty()) {
Chris Lattner94638f02009-09-25 17:29:36 +0000120 SM.PrintMessage(PatternLoc, "found empty check string with prefix '" +
121 CheckPrefix+":'", "error");
Chris Lattnera29703e2009-09-24 20:39:13 +0000122 return true;
123 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000124
Chris Lattner2702e6a2009-09-25 17:09:12 +0000125 // Check to see if this is a fixed string, or if it has regex pieces.
Chris Lattnereec96952009-09-27 07:56:52 +0000126 if (PatternStr.size() < 2 ||
127 (PatternStr.find("{{") == StringRef::npos &&
128 PatternStr.find("[[") == StringRef::npos)) {
Chris Lattner2702e6a2009-09-25 17:09:12 +0000129 FixedStr = PatternStr;
130 return false;
131 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000132
Chris Lattnereec96952009-09-27 07:56:52 +0000133 // Paren value #0 is for the fully matched string. Any new parenthesized
Chris Lattner13a38c42011-04-09 06:18:02 +0000134 // values add from there.
Chris Lattnereec96952009-09-27 07:56:52 +0000135 unsigned CurParen = 1;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000136
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000137 // Otherwise, there is at least one regex piece. Build up the regex pattern
138 // by escaping scary characters in fixed strings, building up one big regex.
Chris Lattner52870082009-09-24 21:47:32 +0000139 while (!PatternStr.empty()) {
Chris Lattnereec96952009-09-27 07:56:52 +0000140 // RegEx matches.
Chris Lattner13a38c42011-04-09 06:18:02 +0000141 if (PatternStr.startswith("{{")) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000142
Chris Lattnereec96952009-09-27 07:56:52 +0000143 // Otherwise, this is the start of a regex match. Scan for the }}.
144 size_t End = PatternStr.find("}}");
145 if (End == StringRef::npos) {
146 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
Chris Lattner13a38c42011-04-09 06:18:02 +0000147 "found start of regex string with no end '}}'","error");
Chris Lattnereec96952009-09-27 07:56:52 +0000148 return true;
149 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000150
Chris Lattner42e31df2011-04-09 06:37:03 +0000151 // Enclose {{}} patterns in parens just like [[]] even though we're not
152 // capturing the result for any purpose. This is required in case the
153 // expression contains an alternation like: CHECK: abc{{x|z}}def. We
154 // want this to turn into: "abc(x|z)def" not "abcx|zdef".
155 RegExStr += '(';
156 ++CurParen;
157
Chris Lattnereec96952009-09-27 07:56:52 +0000158 if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
159 return true;
Chris Lattner42e31df2011-04-09 06:37:03 +0000160 RegExStr += ')';
Chris Lattner13a38c42011-04-09 06:18:02 +0000161
Chris Lattnereec96952009-09-27 07:56:52 +0000162 PatternStr = PatternStr.substr(End+2);
Chris Lattner52870082009-09-24 21:47:32 +0000163 continue;
164 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000165
Chris Lattnereec96952009-09-27 07:56:52 +0000166 // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
167 // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
168 // second form is [[foo]] which is a reference to foo. The variable name
Daniel Dunbar964ac012009-11-22 22:07:50 +0000169 // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
Chris Lattnereec96952009-09-27 07:56:52 +0000170 // it. This is to catch some common errors.
Chris Lattner13a38c42011-04-09 06:18:02 +0000171 if (PatternStr.startswith("[[")) {
Chris Lattnereec96952009-09-27 07:56:52 +0000172 // Verify that it is terminated properly.
173 size_t End = PatternStr.find("]]");
174 if (End == StringRef::npos) {
175 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
176 "invalid named regex reference, no ]] found", "error");
177 return true;
178 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000179
Chris Lattnereec96952009-09-27 07:56:52 +0000180 StringRef MatchStr = PatternStr.substr(2, End-2);
181 PatternStr = PatternStr.substr(End+2);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000182
Chris Lattnereec96952009-09-27 07:56:52 +0000183 // Get the regex name (e.g. "foo").
184 size_t NameEnd = MatchStr.find(':');
185 StringRef Name = MatchStr.substr(0, NameEnd);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000186
Chris Lattnereec96952009-09-27 07:56:52 +0000187 if (Name.empty()) {
188 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
189 "invalid name in named regex: empty name", "error");
190 return true;
191 }
192
193 // Verify that the name is well formed.
194 for (unsigned i = 0, e = Name.size(); i != e; ++i)
Chris Lattner13a38c42011-04-09 06:18:02 +0000195 if (Name[i] != '_' && !isalnum(Name[i])) {
Chris Lattnereec96952009-09-27 07:56:52 +0000196 SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i),
197 "invalid name in named regex", "error");
198 return true;
199 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000200
Chris Lattnereec96952009-09-27 07:56:52 +0000201 // Name can't start with a digit.
202 if (isdigit(Name[0])) {
203 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
204 "invalid name in named regex", "error");
205 return true;
206 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000207
Chris Lattnereec96952009-09-27 07:56:52 +0000208 // Handle [[foo]].
209 if (NameEnd == StringRef::npos) {
210 VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
211 continue;
212 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000213
Chris Lattnereec96952009-09-27 07:56:52 +0000214 // Handle [[foo:.*]].
215 VariableDefs.push_back(std::make_pair(Name, CurParen));
216 RegExStr += '(';
217 ++CurParen;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000218
Chris Lattnereec96952009-09-27 07:56:52 +0000219 if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
220 return true;
221
222 RegExStr += ')';
Chris Lattner52870082009-09-24 21:47:32 +0000223 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000224
Chris Lattnereec96952009-09-27 07:56:52 +0000225 // Handle fixed string matches.
226 // Find the end, which is the start of the next regex.
227 size_t FixedMatchEnd = PatternStr.find("{{");
228 FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
229 AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr);
230 PatternStr = PatternStr.substr(FixedMatchEnd);
231 continue;
Chris Lattner52870082009-09-24 21:47:32 +0000232 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000233
Chris Lattnera29703e2009-09-24 20:39:13 +0000234 return false;
235}
236
Chris Lattnereec96952009-09-27 07:56:52 +0000237void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) {
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000238 // Add the characters from FixedStr to the regex, escaping as needed. This
239 // avoids "leaning toothpicks" in common patterns.
240 for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) {
241 switch (FixedStr[i]) {
242 // These are the special characters matched in "p_ere_exp".
243 case '(':
244 case ')':
245 case '^':
246 case '$':
247 case '|':
248 case '*':
249 case '+':
250 case '?':
251 case '.':
252 case '[':
253 case '\\':
254 case '{':
Chris Lattnereec96952009-09-27 07:56:52 +0000255 TheStr += '\\';
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000256 // FALL THROUGH.
257 default:
Chris Lattnereec96952009-09-27 07:56:52 +0000258 TheStr += FixedStr[i];
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000259 break;
260 }
261 }
262}
263
Chris Lattnereec96952009-09-27 07:56:52 +0000264bool Pattern::AddRegExToRegEx(StringRef RegexStr, unsigned &CurParen,
265 SourceMgr &SM) {
266 Regex R(RegexStr);
267 std::string Error;
268 if (!R.isValid(Error)) {
269 SM.PrintMessage(SMLoc::getFromPointer(RegexStr.data()),
270 "invalid regex: " + Error, "error");
271 return true;
272 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000273
Chris Lattnereec96952009-09-27 07:56:52 +0000274 RegExStr += RegexStr.str();
275 CurParen += R.getNumMatches();
276 return false;
277}
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000278
Chris Lattner52870082009-09-24 21:47:32 +0000279/// Match - Match the pattern string against the input buffer Buffer. This
280/// returns the position that is matched or npos if there is no match. If
281/// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +0000282size_t Pattern::Match(StringRef Buffer, size_t &MatchLen,
283 StringMap<StringRef> &VariableTable) const {
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000284 // If this is the EOF pattern, match it immediately.
285 if (MatchEOF) {
286 MatchLen = 0;
287 return Buffer.size();
288 }
289
Chris Lattner2702e6a2009-09-25 17:09:12 +0000290 // If this is a fixed string pattern, just match it now.
291 if (!FixedStr.empty()) {
292 MatchLen = FixedStr.size();
293 return Buffer.find(FixedStr);
294 }
Chris Lattnereec96952009-09-27 07:56:52 +0000295
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000296 // Regex match.
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000297
Chris Lattnereec96952009-09-27 07:56:52 +0000298 // If there are variable uses, we need to create a temporary string with the
299 // actual value.
300 StringRef RegExToMatch = RegExStr;
301 std::string TmpStr;
302 if (!VariableUses.empty()) {
303 TmpStr = RegExStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000304
Chris Lattnereec96952009-09-27 07:56:52 +0000305 unsigned InsertOffset = 0;
306 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000307 StringMap<StringRef>::iterator it =
308 VariableTable.find(VariableUses[i].first);
309 // If the variable is undefined, return an error.
310 if (it == VariableTable.end())
311 return StringRef::npos;
312
Chris Lattnereec96952009-09-27 07:56:52 +0000313 // Look up the value and escape it so that we can plop it into the regex.
314 std::string Value;
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000315 AddFixedStringToRegEx(it->second, Value);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000316
Chris Lattnereec96952009-09-27 07:56:52 +0000317 // Plop it into the regex at the adjusted offset.
318 TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset,
319 Value.begin(), Value.end());
320 InsertOffset += Value.size();
321 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000322
Chris Lattnereec96952009-09-27 07:56:52 +0000323 // Match the newly constructed regex.
324 RegExToMatch = TmpStr;
325 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000326
327
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000328 SmallVector<StringRef, 4> MatchInfo;
Chris Lattnereec96952009-09-27 07:56:52 +0000329 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000330 return StringRef::npos;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000331
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000332 // Successful regex match.
333 assert(!MatchInfo.empty() && "Didn't get any match");
334 StringRef FullMatch = MatchInfo[0];
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000335
Chris Lattnereec96952009-09-27 07:56:52 +0000336 // If this defines any variables, remember their values.
337 for (unsigned i = 0, e = VariableDefs.size(); i != e; ++i) {
338 assert(VariableDefs[i].second < MatchInfo.size() &&
339 "Internal paren error");
340 VariableTable[VariableDefs[i].first] = MatchInfo[VariableDefs[i].second];
Chris Lattner94638f02009-09-25 17:29:36 +0000341 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000342
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000343 MatchLen = FullMatch.size();
344 return FullMatch.data()-Buffer.data();
Chris Lattner52870082009-09-24 21:47:32 +0000345}
346
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000347unsigned Pattern::ComputeMatchDistance(StringRef Buffer,
348 const StringMap<StringRef> &VariableTable) const {
349 // Just compute the number of matching characters. For regular expressions, we
350 // just compare against the regex itself and hope for the best.
351 //
352 // FIXME: One easy improvement here is have the regex lib generate a single
353 // example regular expression which matches, and use that as the example
354 // string.
355 StringRef ExampleString(FixedStr);
356 if (ExampleString.empty())
357 ExampleString = RegExStr;
358
Daniel Dunbar0806f9f2010-01-30 00:24:06 +0000359 // Only compare up to the first line in the buffer, or the string size.
360 StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
361 BufferPrefix = BufferPrefix.split('\n').first;
362 return BufferPrefix.edit_distance(ExampleString);
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000363}
364
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000365void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
366 const StringMap<StringRef> &VariableTable) const{
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000367 // If this was a regular expression using variables, print the current
368 // variable values.
369 if (!VariableUses.empty()) {
370 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
371 StringRef Var = VariableUses[i].first;
372 StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
373 SmallString<256> Msg;
374 raw_svector_ostream OS(Msg);
375
376 // Check for undefined variable references.
377 if (it == VariableTable.end()) {
378 OS << "uses undefined variable \"";
379 OS.write_escaped(Var) << "\"";;
380 } else {
381 OS << "with variable \"";
382 OS.write_escaped(Var) << "\" equal to \"";
383 OS.write_escaped(it->second) << "\"";
384 }
385
386 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), OS.str(), "note",
387 /*ShowLine=*/false);
388 }
389 }
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000390
391 // Attempt to find the closest/best fuzzy match. Usually an error happens
392 // because some string in the output didn't exactly match. In these cases, we
393 // would like to show the user a best guess at what "should have" matched, to
394 // save them having to actually check the input manually.
395 size_t NumLinesForward = 0;
396 size_t Best = StringRef::npos;
397 double BestQuality = 0;
398
399 // Use an arbitrary 4k limit on how far we will search.
Dan Gohmane3a1e502010-01-29 21:57:46 +0000400 for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000401 if (Buffer[i] == '\n')
402 ++NumLinesForward;
403
Dan Gohmand8a55412010-01-29 21:55:16 +0000404 // Patterns have leading whitespace stripped, so skip whitespace when
405 // looking for something which looks like a pattern.
406 if (Buffer[i] == ' ' || Buffer[i] == '\t')
407 continue;
408
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000409 // Compute the "quality" of this match as an arbitrary combination of the
410 // match distance and the number of lines skipped to get to this match.
411 unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable);
412 double Quality = Distance + (NumLinesForward / 100.);
413
414 if (Quality < BestQuality || Best == StringRef::npos) {
415 Best = i;
416 BestQuality = Quality;
417 }
418 }
419
Daniel Dunbar7a68e0d2010-03-19 18:07:43 +0000420 // Print the "possible intended match here" line if we found something
421 // reasonable and not equal to what we showed in the "scanning from here"
422 // line.
423 if (Best && Best != StringRef::npos && BestQuality < 50) {
424 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
425 "possible intended match here", "note");
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000426
427 // FIXME: If we wanted to be really friendly we would show why the match
428 // failed, as it can be hard to spot simple one character differences.
429 }
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000430}
Chris Lattnera29703e2009-09-24 20:39:13 +0000431
432//===----------------------------------------------------------------------===//
433// Check Strings.
434//===----------------------------------------------------------------------===//
Chris Lattner9fc66782009-09-24 20:25:55 +0000435
436/// CheckString - This is a check that we found in the input file.
437struct CheckString {
438 /// Pat - The pattern to match.
439 Pattern Pat;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000440
Chris Lattner207e1bc2009-08-15 17:41:04 +0000441 /// Loc - The location in the match file that the check string was specified.
442 SMLoc Loc;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000443
Chris Lattner5dafafd2009-08-15 18:32:21 +0000444 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
445 /// to a CHECK: directive.
446 bool IsCheckNext;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000447
Chris Lattnerf15380b2009-09-20 22:35:26 +0000448 /// NotStrings - These are all of the strings that are disallowed from
449 /// occurring between this match string and the previous one (or start of
450 /// file).
Chris Lattnera29703e2009-09-24 20:39:13 +0000451 std::vector<std::pair<SMLoc, Pattern> > NotStrings;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000452
Chris Lattner9fc66782009-09-24 20:25:55 +0000453 CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
454 : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
Chris Lattner207e1bc2009-08-15 17:41:04 +0000455};
456
Chris Lattneradea46e2009-09-24 20:45:07 +0000457/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
458/// memory buffer, free it, and return a new one.
459static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
Chris Lattner4c842dd2010-04-05 22:42:30 +0000460 SmallString<128> NewFile;
Chris Lattneradea46e2009-09-24 20:45:07 +0000461 NewFile.reserve(MB->getBufferSize());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000462
Chris Lattneradea46e2009-09-24 20:45:07 +0000463 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
464 Ptr != End; ++Ptr) {
NAKAMURA Takumi9f6e03f2010-11-14 03:28:22 +0000465 // Eliminate trailing dosish \r.
466 if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
467 continue;
468 }
469
Chris Lattneradea46e2009-09-24 20:45:07 +0000470 // If C is not a horizontal whitespace, skip it.
471 if (*Ptr != ' ' && *Ptr != '\t') {
472 NewFile.push_back(*Ptr);
473 continue;
474 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000475
Chris Lattneradea46e2009-09-24 20:45:07 +0000476 // Otherwise, add one space and advance over neighboring space.
477 NewFile.push_back(' ');
478 while (Ptr+1 != End &&
479 (Ptr[1] == ' ' || Ptr[1] == '\t'))
480 ++Ptr;
481 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000482
Chris Lattneradea46e2009-09-24 20:45:07 +0000483 // Free the old buffer and return a new one.
484 MemoryBuffer *MB2 =
Chris Lattner4c842dd2010-04-05 22:42:30 +0000485 MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000486
Chris Lattneradea46e2009-09-24 20:45:07 +0000487 delete MB;
488 return MB2;
489}
490
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000491
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000492/// ReadCheckFile - Read the check file, which specifies the sequence of
493/// expected strings. The strings are added to the CheckStrings vector.
494static bool ReadCheckFile(SourceMgr &SM,
Chris Lattner207e1bc2009-08-15 17:41:04 +0000495 std::vector<CheckString> &CheckStrings) {
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000496 // Open the check file, and tell SourceMgr about it.
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000497 OwningPtr<MemoryBuffer> File;
498 if (error_code ec =
499 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), File)) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000500 errs() << "Could not open check file '" << CheckFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +0000501 << ec.message() << '\n';
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000502 return true;
503 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000504 MemoryBuffer *F = File.take();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000505
Chris Lattneradea46e2009-09-24 20:45:07 +0000506 // If we want to canonicalize whitespace, strip excess whitespace from the
507 // buffer containing the CHECK lines.
508 if (!NoCanonicalizeWhiteSpace)
509 F = CanonicalizeInputFile(F);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000510
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000511 SM.AddNewSourceBuffer(F, SMLoc());
512
Chris Lattnerd7e25052009-08-15 18:00:42 +0000513 // Find all instances of CheckPrefix followed by : in the file.
Chris Lattner96077032009-09-20 22:11:44 +0000514 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000515
Chris Lattnera29703e2009-09-24 20:39:13 +0000516 std::vector<std::pair<SMLoc, Pattern> > NotMatches;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000517
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000518 while (1) {
519 // See if Prefix occurs in the memory buffer.
Chris Lattner96077032009-09-20 22:11:44 +0000520 Buffer = Buffer.substr(Buffer.find(CheckPrefix));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000521
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000522 // If we didn't find a match, we're done.
Chris Lattner96077032009-09-20 22:11:44 +0000523 if (Buffer.empty())
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000524 break;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000525
Chris Lattner96077032009-09-20 22:11:44 +0000526 const char *CheckPrefixStart = Buffer.data();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000527
Chris Lattner5dafafd2009-08-15 18:32:21 +0000528 // When we find a check prefix, keep track of whether we find CHECK: or
529 // CHECK-NEXT:
Chris Lattnerf15380b2009-09-20 22:35:26 +0000530 bool IsCheckNext = false, IsCheckNot = false;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000531
Chris Lattnerd7e25052009-08-15 18:00:42 +0000532 // Verify that the : is present after the prefix.
Chris Lattner96077032009-09-20 22:11:44 +0000533 if (Buffer[CheckPrefix.size()] == ':') {
534 Buffer = Buffer.substr(CheckPrefix.size()+1);
Chris Lattner96077032009-09-20 22:11:44 +0000535 } else if (Buffer.size() > CheckPrefix.size()+6 &&
536 memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
537 Buffer = Buffer.substr(CheckPrefix.size()+7);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000538 IsCheckNext = true;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000539 } else if (Buffer.size() > CheckPrefix.size()+5 &&
540 memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) {
541 Buffer = Buffer.substr(CheckPrefix.size()+6);
542 IsCheckNot = true;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000543 } else {
Chris Lattner96077032009-09-20 22:11:44 +0000544 Buffer = Buffer.substr(1);
Chris Lattnerd7e25052009-08-15 18:00:42 +0000545 continue;
546 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000547
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000548 // Okay, we found the prefix, yay. Remember the rest of the line, but
549 // ignore leading and trailing whitespace.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000550 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000551
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000552 // Scan ahead to the end of line.
Chris Lattner96077032009-09-20 22:11:44 +0000553 size_t EOL = Buffer.find_first_of("\n\r");
Chris Lattnera29703e2009-09-24 20:39:13 +0000554
Dan Gohmane5463432010-01-29 21:53:18 +0000555 // Remember the location of the start of the pattern, for diagnostics.
556 SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
557
Chris Lattnera29703e2009-09-24 20:39:13 +0000558 // Parse the pattern.
559 Pattern P;
560 if (P.ParsePattern(Buffer.substr(0, EOL), SM))
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000561 return true;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000562
Chris Lattnera29703e2009-09-24 20:39:13 +0000563 Buffer = Buffer.substr(EOL);
564
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000565
Chris Lattner5dafafd2009-08-15 18:32:21 +0000566 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
567 if (IsCheckNext && CheckStrings.empty()) {
568 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
569 "found '"+CheckPrefix+"-NEXT:' without previous '"+
570 CheckPrefix+ ": line", "error");
571 return true;
572 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000573
Chris Lattnera29703e2009-09-24 20:39:13 +0000574 // Handle CHECK-NOT.
575 if (IsCheckNot) {
576 NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
577 P));
578 continue;
579 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000580
581
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000582 // Okay, add the string we captured to the output vector and move on.
Chris Lattner9fc66782009-09-24 20:25:55 +0000583 CheckStrings.push_back(CheckString(P,
Dan Gohmane5463432010-01-29 21:53:18 +0000584 PatternLoc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000585 IsCheckNext));
Chris Lattnerf15380b2009-09-20 22:35:26 +0000586 std::swap(NotMatches, CheckStrings.back().NotStrings);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000587 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000588
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000589 // Add an EOF pattern for any trailing CHECK-NOTs.
590 if (!NotMatches.empty()) {
591 CheckStrings.push_back(CheckString(Pattern(true),
592 SMLoc::getFromPointer(Buffer.data()),
593 false));
594 std::swap(NotMatches, CheckStrings.back().NotStrings);
595 }
596
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000597 if (CheckStrings.empty()) {
Chris Lattnerd7e25052009-08-15 18:00:42 +0000598 errs() << "error: no check strings found with prefix '" << CheckPrefix
599 << ":'\n";
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000600 return true;
601 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000602
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000603 return false;
604}
605
Chris Lattner5dafafd2009-08-15 18:32:21 +0000606static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000607 StringRef Buffer,
608 StringMap<StringRef> &VariableTable) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000609 // Otherwise, we have an error, emit an error message.
610 SM.PrintMessage(CheckStr.Loc, "expected string not found in input",
611 "error");
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000612
Chris Lattner5dafafd2009-08-15 18:32:21 +0000613 // Print the "scanning from here" line. If the current position is at the
614 // end of a line, advance to the start of the next line.
Chris Lattner96077032009-09-20 22:11:44 +0000615 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000616
Chris Lattner96077032009-09-20 22:11:44 +0000617 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here",
Chris Lattner5dafafd2009-08-15 18:32:21 +0000618 "note");
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000619
620 // Allow the pattern to print additional information if desired.
621 CheckStr.Pat.PrintFailureInfo(SM, Buffer, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000622}
623
Chris Lattner3711b7a2009-09-20 22:42:44 +0000624/// CountNumNewlinesBetween - Count the number of newlines in the specified
625/// range.
626static unsigned CountNumNewlinesBetween(StringRef Range) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000627 unsigned NumNewLines = 0;
Chris Lattner3711b7a2009-09-20 22:42:44 +0000628 while (1) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000629 // Scan for newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000630 Range = Range.substr(Range.find_first_of("\n\r"));
631 if (Range.empty()) return NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000632
Chris Lattner5dafafd2009-08-15 18:32:21 +0000633 ++NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000634
Chris Lattner5dafafd2009-08-15 18:32:21 +0000635 // Handle \n\r and \r\n as a single newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000636 if (Range.size() > 1 &&
637 (Range[1] == '\n' || Range[1] == '\r') &&
638 (Range[0] != Range[1]))
639 Range = Range.substr(1);
640 Range = Range.substr(1);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000641 }
Chris Lattner5dafafd2009-08-15 18:32:21 +0000642}
643
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000644int main(int argc, char **argv) {
645 sys::PrintStackTraceOnErrorSignal();
646 PrettyStackTraceProgram X(argc, argv);
647 cl::ParseCommandLineOptions(argc, argv);
648
649 SourceMgr SM;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000650
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000651 // Read the expected strings from the check file.
Chris Lattner207e1bc2009-08-15 17:41:04 +0000652 std::vector<CheckString> CheckStrings;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000653 if (ReadCheckFile(SM, CheckStrings))
654 return 2;
655
656 // Open the file to check and add it to SourceMgr.
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000657 OwningPtr<MemoryBuffer> File;
658 if (error_code ec =
659 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000660 errs() << "Could not open input file '" << InputFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +0000661 << ec.message() << '\n';
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000662 return true;
663 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000664 MemoryBuffer *F = File.take();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000665
Chris Lattner1aac1862011-02-09 16:46:02 +0000666 if (F->getBufferSize() == 0) {
667 errs() << "FileCheck error: '" << InputFilename << "' is empty.\n";
668 return 1;
669 }
670
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000671 // Remove duplicate spaces in the input file if requested.
672 if (!NoCanonicalizeWhiteSpace)
673 F = CanonicalizeInputFile(F);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000674
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000675 SM.AddNewSourceBuffer(F, SMLoc());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000676
Chris Lattnereec96952009-09-27 07:56:52 +0000677 /// VariableTable - This holds all the current filecheck variables.
678 StringMap<StringRef> VariableTable;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000679
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000680 // Check that we have all of the expected strings, in order, in the input
681 // file.
Chris Lattner96077032009-09-20 22:11:44 +0000682 StringRef Buffer = F->getBuffer();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000683
Chris Lattnerf15380b2009-09-20 22:35:26 +0000684 const char *LastMatch = Buffer.data();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000685
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000686 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
Chris Lattner207e1bc2009-08-15 17:41:04 +0000687 const CheckString &CheckStr = CheckStrings[StrNo];
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000688
Chris Lattner96077032009-09-20 22:11:44 +0000689 StringRef SearchFrom = Buffer;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000690
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000691 // Find StrNo in the file.
Chris Lattner9fc66782009-09-24 20:25:55 +0000692 size_t MatchLen = 0;
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000693 size_t MatchPos = CheckStr.Pat.Match(Buffer, MatchLen, VariableTable);
694 Buffer = Buffer.substr(MatchPos);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000695
Chris Lattner5dafafd2009-08-15 18:32:21 +0000696 // If we didn't find a match, reject the input.
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000697 if (MatchPos == StringRef::npos) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000698 PrintCheckFailed(SM, CheckStr, SearchFrom, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000699 return 1;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000700 }
Chris Lattner3711b7a2009-09-20 22:42:44 +0000701
702 StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
703
Chris Lattner5dafafd2009-08-15 18:32:21 +0000704 // If this check is a "CHECK-NEXT", verify that the previous match was on
705 // the previous line (i.e. that there is one newline between them).
706 if (CheckStr.IsCheckNext) {
707 // Count the number of newlines between the previous match and this one.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000708 assert(LastMatch != F->getBufferStart() &&
709 "CHECK-NEXT can't be the first check in a file");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000710
Chris Lattner3711b7a2009-09-20 22:42:44 +0000711 unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000712 if (NumNewLines == 0) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000713 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000714 CheckPrefix+"-NEXT: is on the same line as previous match",
715 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000716 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000717 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000718 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
719 "previous match was here", "note");
720 return 1;
721 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000722
Chris Lattner5dafafd2009-08-15 18:32:21 +0000723 if (NumNewLines != 1) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000724 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000725 CheckPrefix+
726 "-NEXT: is not on the line after the previous match",
727 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000728 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000729 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000730 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
731 "previous match was here", "note");
732 return 1;
733 }
734 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000735
Chris Lattnerf15380b2009-09-20 22:35:26 +0000736 // If this match had "not strings", verify that they don't exist in the
737 // skipped region.
Chris Lattnereec96952009-09-27 07:56:52 +0000738 for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size();
739 ChunkNo != e; ++ChunkNo) {
Chris Lattnera29703e2009-09-24 20:39:13 +0000740 size_t MatchLen = 0;
Chris Lattnereec96952009-09-27 07:56:52 +0000741 size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion,
742 MatchLen,
743 VariableTable);
Chris Lattnerf15380b2009-09-20 22:35:26 +0000744 if (Pos == StringRef::npos) continue;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000745
Chris Lattnerf15380b2009-09-20 22:35:26 +0000746 SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos),
747 CheckPrefix+"-NOT: string occurred!", "error");
Chris Lattner52870082009-09-24 21:47:32 +0000748 SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first,
Chris Lattnerf15380b2009-09-20 22:35:26 +0000749 CheckPrefix+"-NOT: pattern specified here", "note");
750 return 1;
751 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000752
Chris Lattner5dafafd2009-08-15 18:32:21 +0000753
Chris Lattner81115762009-09-21 02:30:42 +0000754 // Otherwise, everything is good. Step over the matched text and remember
755 // the position after the match as the end of the last match.
Chris Lattner9fc66782009-09-24 20:25:55 +0000756 Buffer = Buffer.substr(MatchLen);
Chris Lattner81115762009-09-21 02:30:42 +0000757 LastMatch = Buffer.data();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000758 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000759
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000760 return 0;
761}