blob: a4aa693448ba6ad489250c4ad4ec4a7c2e50dc74 [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
134 // values add from their.
135 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.
141 if (PatternStr.size() >= 2 &&
142 PatternStr[0] == '{' && PatternStr[1] == '{') {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000143
Chris Lattnereec96952009-09-27 07:56:52 +0000144 // Otherwise, this is the start of a regex match. Scan for the }}.
145 size_t End = PatternStr.find("}}");
146 if (End == StringRef::npos) {
147 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
148 "found start of regex string with no end '}}'", "error");
149 return true;
150 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000151
Chris Lattnereec96952009-09-27 07:56:52 +0000152 if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
153 return true;
154 PatternStr = PatternStr.substr(End+2);
Chris Lattner52870082009-09-24 21:47:32 +0000155 continue;
156 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000157
Chris Lattnereec96952009-09-27 07:56:52 +0000158 // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
159 // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
160 // second form is [[foo]] which is a reference to foo. The variable name
Daniel Dunbar964ac012009-11-22 22:07:50 +0000161 // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
Chris Lattnereec96952009-09-27 07:56:52 +0000162 // it. This is to catch some common errors.
163 if (PatternStr.size() >= 2 &&
164 PatternStr[0] == '[' && PatternStr[1] == '[') {
165 // Verify that it is terminated properly.
166 size_t End = PatternStr.find("]]");
167 if (End == StringRef::npos) {
168 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
169 "invalid named regex reference, no ]] found", "error");
170 return true;
171 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000172
Chris Lattnereec96952009-09-27 07:56:52 +0000173 StringRef MatchStr = PatternStr.substr(2, End-2);
174 PatternStr = PatternStr.substr(End+2);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000175
Chris Lattnereec96952009-09-27 07:56:52 +0000176 // Get the regex name (e.g. "foo").
177 size_t NameEnd = MatchStr.find(':');
178 StringRef Name = MatchStr.substr(0, NameEnd);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000179
Chris Lattnereec96952009-09-27 07:56:52 +0000180 if (Name.empty()) {
181 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
182 "invalid name in named regex: empty name", "error");
183 return true;
184 }
185
186 // Verify that the name is well formed.
187 for (unsigned i = 0, e = Name.size(); i != e; ++i)
Daniel Dunbar964ac012009-11-22 22:07:50 +0000188 if (Name[i] != '_' &&
189 (Name[i] < 'a' || Name[i] > 'z') &&
Chris Lattnereec96952009-09-27 07:56:52 +0000190 (Name[i] < 'A' || Name[i] > 'Z') &&
191 (Name[i] < '0' || Name[i] > '9')) {
192 SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i),
193 "invalid name in named regex", "error");
194 return true;
195 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000196
Chris Lattnereec96952009-09-27 07:56:52 +0000197 // Name can't start with a digit.
198 if (isdigit(Name[0])) {
199 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
200 "invalid name in named regex", "error");
201 return true;
202 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000203
Chris Lattnereec96952009-09-27 07:56:52 +0000204 // Handle [[foo]].
205 if (NameEnd == StringRef::npos) {
206 VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
207 continue;
208 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000209
Chris Lattnereec96952009-09-27 07:56:52 +0000210 // Handle [[foo:.*]].
211 VariableDefs.push_back(std::make_pair(Name, CurParen));
212 RegExStr += '(';
213 ++CurParen;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000214
Chris Lattnereec96952009-09-27 07:56:52 +0000215 if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
216 return true;
217
218 RegExStr += ')';
Chris Lattner52870082009-09-24 21:47:32 +0000219 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000220
Chris Lattnereec96952009-09-27 07:56:52 +0000221 // Handle fixed string matches.
222 // Find the end, which is the start of the next regex.
223 size_t FixedMatchEnd = PatternStr.find("{{");
224 FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
225 AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr);
226 PatternStr = PatternStr.substr(FixedMatchEnd);
227 continue;
Chris Lattner52870082009-09-24 21:47:32 +0000228 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000229
Chris Lattnera29703e2009-09-24 20:39:13 +0000230 return false;
231}
232
Chris Lattnereec96952009-09-27 07:56:52 +0000233void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) {
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000234 // Add the characters from FixedStr to the regex, escaping as needed. This
235 // avoids "leaning toothpicks" in common patterns.
236 for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) {
237 switch (FixedStr[i]) {
238 // These are the special characters matched in "p_ere_exp".
239 case '(':
240 case ')':
241 case '^':
242 case '$':
243 case '|':
244 case '*':
245 case '+':
246 case '?':
247 case '.':
248 case '[':
249 case '\\':
250 case '{':
Chris Lattnereec96952009-09-27 07:56:52 +0000251 TheStr += '\\';
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000252 // FALL THROUGH.
253 default:
Chris Lattnereec96952009-09-27 07:56:52 +0000254 TheStr += FixedStr[i];
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000255 break;
256 }
257 }
258}
259
Chris Lattnereec96952009-09-27 07:56:52 +0000260bool Pattern::AddRegExToRegEx(StringRef RegexStr, unsigned &CurParen,
261 SourceMgr &SM) {
262 Regex R(RegexStr);
263 std::string Error;
264 if (!R.isValid(Error)) {
265 SM.PrintMessage(SMLoc::getFromPointer(RegexStr.data()),
266 "invalid regex: " + Error, "error");
267 return true;
268 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000269
Chris Lattnereec96952009-09-27 07:56:52 +0000270 RegExStr += RegexStr.str();
271 CurParen += R.getNumMatches();
272 return false;
273}
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000274
Chris Lattner52870082009-09-24 21:47:32 +0000275/// Match - Match the pattern string against the input buffer Buffer. This
276/// returns the position that is matched or npos if there is no match. If
277/// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +0000278size_t Pattern::Match(StringRef Buffer, size_t &MatchLen,
279 StringMap<StringRef> &VariableTable) const {
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000280 // If this is the EOF pattern, match it immediately.
281 if (MatchEOF) {
282 MatchLen = 0;
283 return Buffer.size();
284 }
285
Chris Lattner2702e6a2009-09-25 17:09:12 +0000286 // If this is a fixed string pattern, just match it now.
287 if (!FixedStr.empty()) {
288 MatchLen = FixedStr.size();
289 return Buffer.find(FixedStr);
290 }
Chris Lattnereec96952009-09-27 07:56:52 +0000291
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000292 // Regex match.
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000293
Chris Lattnereec96952009-09-27 07:56:52 +0000294 // If there are variable uses, we need to create a temporary string with the
295 // actual value.
296 StringRef RegExToMatch = RegExStr;
297 std::string TmpStr;
298 if (!VariableUses.empty()) {
299 TmpStr = RegExStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000300
Chris Lattnereec96952009-09-27 07:56:52 +0000301 unsigned InsertOffset = 0;
302 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000303 StringMap<StringRef>::iterator it =
304 VariableTable.find(VariableUses[i].first);
305 // If the variable is undefined, return an error.
306 if (it == VariableTable.end())
307 return StringRef::npos;
308
Chris Lattnereec96952009-09-27 07:56:52 +0000309 // Look up the value and escape it so that we can plop it into the regex.
310 std::string Value;
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000311 AddFixedStringToRegEx(it->second, Value);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000312
Chris Lattnereec96952009-09-27 07:56:52 +0000313 // Plop it into the regex at the adjusted offset.
314 TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset,
315 Value.begin(), Value.end());
316 InsertOffset += Value.size();
317 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000318
Chris Lattnereec96952009-09-27 07:56:52 +0000319 // Match the newly constructed regex.
320 RegExToMatch = TmpStr;
321 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000322
323
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000324 SmallVector<StringRef, 4> MatchInfo;
Chris Lattnereec96952009-09-27 07:56:52 +0000325 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000326 return StringRef::npos;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000327
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000328 // Successful regex match.
329 assert(!MatchInfo.empty() && "Didn't get any match");
330 StringRef FullMatch = MatchInfo[0];
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000331
Chris Lattnereec96952009-09-27 07:56:52 +0000332 // If this defines any variables, remember their values.
333 for (unsigned i = 0, e = VariableDefs.size(); i != e; ++i) {
334 assert(VariableDefs[i].second < MatchInfo.size() &&
335 "Internal paren error");
336 VariableTable[VariableDefs[i].first] = MatchInfo[VariableDefs[i].second];
Chris Lattner94638f02009-09-25 17:29:36 +0000337 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000338
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000339 MatchLen = FullMatch.size();
340 return FullMatch.data()-Buffer.data();
Chris Lattner52870082009-09-24 21:47:32 +0000341}
342
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000343unsigned Pattern::ComputeMatchDistance(StringRef Buffer,
344 const StringMap<StringRef> &VariableTable) const {
345 // Just compute the number of matching characters. For regular expressions, we
346 // just compare against the regex itself and hope for the best.
347 //
348 // FIXME: One easy improvement here is have the regex lib generate a single
349 // example regular expression which matches, and use that as the example
350 // string.
351 StringRef ExampleString(FixedStr);
352 if (ExampleString.empty())
353 ExampleString = RegExStr;
354
Daniel Dunbar0806f9f2010-01-30 00:24:06 +0000355 // Only compare up to the first line in the buffer, or the string size.
356 StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
357 BufferPrefix = BufferPrefix.split('\n').first;
358 return BufferPrefix.edit_distance(ExampleString);
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000359}
360
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000361void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
362 const StringMap<StringRef> &VariableTable) const{
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000363 // If this was a regular expression using variables, print the current
364 // variable values.
365 if (!VariableUses.empty()) {
366 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
367 StringRef Var = VariableUses[i].first;
368 StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
369 SmallString<256> Msg;
370 raw_svector_ostream OS(Msg);
371
372 // Check for undefined variable references.
373 if (it == VariableTable.end()) {
374 OS << "uses undefined variable \"";
375 OS.write_escaped(Var) << "\"";;
376 } else {
377 OS << "with variable \"";
378 OS.write_escaped(Var) << "\" equal to \"";
379 OS.write_escaped(it->second) << "\"";
380 }
381
382 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), OS.str(), "note",
383 /*ShowLine=*/false);
384 }
385 }
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000386
387 // Attempt to find the closest/best fuzzy match. Usually an error happens
388 // because some string in the output didn't exactly match. In these cases, we
389 // would like to show the user a best guess at what "should have" matched, to
390 // save them having to actually check the input manually.
391 size_t NumLinesForward = 0;
392 size_t Best = StringRef::npos;
393 double BestQuality = 0;
394
395 // Use an arbitrary 4k limit on how far we will search.
Dan Gohmane3a1e502010-01-29 21:57:46 +0000396 for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000397 if (Buffer[i] == '\n')
398 ++NumLinesForward;
399
Dan Gohmand8a55412010-01-29 21:55:16 +0000400 // Patterns have leading whitespace stripped, so skip whitespace when
401 // looking for something which looks like a pattern.
402 if (Buffer[i] == ' ' || Buffer[i] == '\t')
403 continue;
404
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000405 // Compute the "quality" of this match as an arbitrary combination of the
406 // match distance and the number of lines skipped to get to this match.
407 unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable);
408 double Quality = Distance + (NumLinesForward / 100.);
409
410 if (Quality < BestQuality || Best == StringRef::npos) {
411 Best = i;
412 BestQuality = Quality;
413 }
414 }
415
Daniel Dunbar7a68e0d2010-03-19 18:07:43 +0000416 // Print the "possible intended match here" line if we found something
417 // reasonable and not equal to what we showed in the "scanning from here"
418 // line.
419 if (Best && Best != StringRef::npos && BestQuality < 50) {
420 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
421 "possible intended match here", "note");
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000422
423 // FIXME: If we wanted to be really friendly we would show why the match
424 // failed, as it can be hard to spot simple one character differences.
425 }
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000426}
Chris Lattnera29703e2009-09-24 20:39:13 +0000427
428//===----------------------------------------------------------------------===//
429// Check Strings.
430//===----------------------------------------------------------------------===//
Chris Lattner9fc66782009-09-24 20:25:55 +0000431
432/// CheckString - This is a check that we found in the input file.
433struct CheckString {
434 /// Pat - The pattern to match.
435 Pattern Pat;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000436
Chris Lattner207e1bc2009-08-15 17:41:04 +0000437 /// Loc - The location in the match file that the check string was specified.
438 SMLoc Loc;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000439
Chris Lattner5dafafd2009-08-15 18:32:21 +0000440 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
441 /// to a CHECK: directive.
442 bool IsCheckNext;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000443
Chris Lattnerf15380b2009-09-20 22:35:26 +0000444 /// NotStrings - These are all of the strings that are disallowed from
445 /// occurring between this match string and the previous one (or start of
446 /// file).
Chris Lattnera29703e2009-09-24 20:39:13 +0000447 std::vector<std::pair<SMLoc, Pattern> > NotStrings;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000448
Chris Lattner9fc66782009-09-24 20:25:55 +0000449 CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
450 : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
Chris Lattner207e1bc2009-08-15 17:41:04 +0000451};
452
Chris Lattneradea46e2009-09-24 20:45:07 +0000453/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
454/// memory buffer, free it, and return a new one.
455static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
Chris Lattner4c842dd2010-04-05 22:42:30 +0000456 SmallString<128> NewFile;
Chris Lattneradea46e2009-09-24 20:45:07 +0000457 NewFile.reserve(MB->getBufferSize());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000458
Chris Lattneradea46e2009-09-24 20:45:07 +0000459 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
460 Ptr != End; ++Ptr) {
NAKAMURA Takumi9f6e03f2010-11-14 03:28:22 +0000461 // Eliminate trailing dosish \r.
462 if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
463 continue;
464 }
465
Chris Lattneradea46e2009-09-24 20:45:07 +0000466 // If C is not a horizontal whitespace, skip it.
467 if (*Ptr != ' ' && *Ptr != '\t') {
468 NewFile.push_back(*Ptr);
469 continue;
470 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000471
Chris Lattneradea46e2009-09-24 20:45:07 +0000472 // Otherwise, add one space and advance over neighboring space.
473 NewFile.push_back(' ');
474 while (Ptr+1 != End &&
475 (Ptr[1] == ' ' || Ptr[1] == '\t'))
476 ++Ptr;
477 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000478
Chris Lattneradea46e2009-09-24 20:45:07 +0000479 // Free the old buffer and return a new one.
480 MemoryBuffer *MB2 =
Chris Lattner4c842dd2010-04-05 22:42:30 +0000481 MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000482
Chris Lattneradea46e2009-09-24 20:45:07 +0000483 delete MB;
484 return MB2;
485}
486
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000487
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000488/// ReadCheckFile - Read the check file, which specifies the sequence of
489/// expected strings. The strings are added to the CheckStrings vector.
490static bool ReadCheckFile(SourceMgr &SM,
Chris Lattner207e1bc2009-08-15 17:41:04 +0000491 std::vector<CheckString> &CheckStrings) {
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000492 // Open the check file, and tell SourceMgr about it.
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000493 OwningPtr<MemoryBuffer> File;
494 if (error_code ec =
495 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), File)) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000496 errs() << "Could not open check file '" << CheckFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +0000497 << ec.message() << '\n';
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000498 return true;
499 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000500 MemoryBuffer *F = File.take();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000501
Chris Lattneradea46e2009-09-24 20:45:07 +0000502 // If we want to canonicalize whitespace, strip excess whitespace from the
503 // buffer containing the CHECK lines.
504 if (!NoCanonicalizeWhiteSpace)
505 F = CanonicalizeInputFile(F);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000506
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000507 SM.AddNewSourceBuffer(F, SMLoc());
508
Chris Lattnerd7e25052009-08-15 18:00:42 +0000509 // Find all instances of CheckPrefix followed by : in the file.
Chris Lattner96077032009-09-20 22:11:44 +0000510 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000511
Chris Lattnera29703e2009-09-24 20:39:13 +0000512 std::vector<std::pair<SMLoc, Pattern> > NotMatches;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000513
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000514 while (1) {
515 // See if Prefix occurs in the memory buffer.
Chris Lattner96077032009-09-20 22:11:44 +0000516 Buffer = Buffer.substr(Buffer.find(CheckPrefix));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000517
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000518 // If we didn't find a match, we're done.
Chris Lattner96077032009-09-20 22:11:44 +0000519 if (Buffer.empty())
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000520 break;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000521
Chris Lattner96077032009-09-20 22:11:44 +0000522 const char *CheckPrefixStart = Buffer.data();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000523
Chris Lattner5dafafd2009-08-15 18:32:21 +0000524 // When we find a check prefix, keep track of whether we find CHECK: or
525 // CHECK-NEXT:
Chris Lattnerf15380b2009-09-20 22:35:26 +0000526 bool IsCheckNext = false, IsCheckNot = false;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000527
Chris Lattnerd7e25052009-08-15 18:00:42 +0000528 // Verify that the : is present after the prefix.
Chris Lattner96077032009-09-20 22:11:44 +0000529 if (Buffer[CheckPrefix.size()] == ':') {
530 Buffer = Buffer.substr(CheckPrefix.size()+1);
Chris Lattner96077032009-09-20 22:11:44 +0000531 } else if (Buffer.size() > CheckPrefix.size()+6 &&
532 memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
533 Buffer = Buffer.substr(CheckPrefix.size()+7);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000534 IsCheckNext = true;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000535 } else if (Buffer.size() > CheckPrefix.size()+5 &&
536 memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) {
537 Buffer = Buffer.substr(CheckPrefix.size()+6);
538 IsCheckNot = true;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000539 } else {
Chris Lattner96077032009-09-20 22:11:44 +0000540 Buffer = Buffer.substr(1);
Chris Lattnerd7e25052009-08-15 18:00:42 +0000541 continue;
542 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000543
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000544 // Okay, we found the prefix, yay. Remember the rest of the line, but
545 // ignore leading and trailing whitespace.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000546 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000547
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000548 // Scan ahead to the end of line.
Chris Lattner96077032009-09-20 22:11:44 +0000549 size_t EOL = Buffer.find_first_of("\n\r");
Chris Lattnera29703e2009-09-24 20:39:13 +0000550
Dan Gohmane5463432010-01-29 21:53:18 +0000551 // Remember the location of the start of the pattern, for diagnostics.
552 SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
553
Chris Lattnera29703e2009-09-24 20:39:13 +0000554 // Parse the pattern.
555 Pattern P;
556 if (P.ParsePattern(Buffer.substr(0, EOL), SM))
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000557 return true;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000558
Chris Lattnera29703e2009-09-24 20:39:13 +0000559 Buffer = Buffer.substr(EOL);
560
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000561
Chris Lattner5dafafd2009-08-15 18:32:21 +0000562 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
563 if (IsCheckNext && CheckStrings.empty()) {
564 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
565 "found '"+CheckPrefix+"-NEXT:' without previous '"+
566 CheckPrefix+ ": line", "error");
567 return true;
568 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000569
Chris Lattnera29703e2009-09-24 20:39:13 +0000570 // Handle CHECK-NOT.
571 if (IsCheckNot) {
572 NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
573 P));
574 continue;
575 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000576
577
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000578 // Okay, add the string we captured to the output vector and move on.
Chris Lattner9fc66782009-09-24 20:25:55 +0000579 CheckStrings.push_back(CheckString(P,
Dan Gohmane5463432010-01-29 21:53:18 +0000580 PatternLoc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000581 IsCheckNext));
Chris Lattnerf15380b2009-09-20 22:35:26 +0000582 std::swap(NotMatches, CheckStrings.back().NotStrings);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000583 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000584
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000585 // Add an EOF pattern for any trailing CHECK-NOTs.
586 if (!NotMatches.empty()) {
587 CheckStrings.push_back(CheckString(Pattern(true),
588 SMLoc::getFromPointer(Buffer.data()),
589 false));
590 std::swap(NotMatches, CheckStrings.back().NotStrings);
591 }
592
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000593 if (CheckStrings.empty()) {
Chris Lattnerd7e25052009-08-15 18:00:42 +0000594 errs() << "error: no check strings found with prefix '" << CheckPrefix
595 << ":'\n";
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000596 return true;
597 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000598
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000599 return false;
600}
601
Chris Lattner5dafafd2009-08-15 18:32:21 +0000602static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000603 StringRef Buffer,
604 StringMap<StringRef> &VariableTable) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000605 // Otherwise, we have an error, emit an error message.
606 SM.PrintMessage(CheckStr.Loc, "expected string not found in input",
607 "error");
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000608
Chris Lattner5dafafd2009-08-15 18:32:21 +0000609 // Print the "scanning from here" line. If the current position is at the
610 // end of a line, advance to the start of the next line.
Chris Lattner96077032009-09-20 22:11:44 +0000611 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000612
Chris Lattner96077032009-09-20 22:11:44 +0000613 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here",
Chris Lattner5dafafd2009-08-15 18:32:21 +0000614 "note");
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000615
616 // Allow the pattern to print additional information if desired.
617 CheckStr.Pat.PrintFailureInfo(SM, Buffer, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000618}
619
Chris Lattner3711b7a2009-09-20 22:42:44 +0000620/// CountNumNewlinesBetween - Count the number of newlines in the specified
621/// range.
622static unsigned CountNumNewlinesBetween(StringRef Range) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000623 unsigned NumNewLines = 0;
Chris Lattner3711b7a2009-09-20 22:42:44 +0000624 while (1) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000625 // Scan for newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000626 Range = Range.substr(Range.find_first_of("\n\r"));
627 if (Range.empty()) return NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000628
Chris Lattner5dafafd2009-08-15 18:32:21 +0000629 ++NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000630
Chris Lattner5dafafd2009-08-15 18:32:21 +0000631 // Handle \n\r and \r\n as a single newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000632 if (Range.size() > 1 &&
633 (Range[1] == '\n' || Range[1] == '\r') &&
634 (Range[0] != Range[1]))
635 Range = Range.substr(1);
636 Range = Range.substr(1);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000637 }
Chris Lattner5dafafd2009-08-15 18:32:21 +0000638}
639
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000640int main(int argc, char **argv) {
641 sys::PrintStackTraceOnErrorSignal();
642 PrettyStackTraceProgram X(argc, argv);
643 cl::ParseCommandLineOptions(argc, argv);
644
645 SourceMgr SM;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000646
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000647 // Read the expected strings from the check file.
Chris Lattner207e1bc2009-08-15 17:41:04 +0000648 std::vector<CheckString> CheckStrings;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000649 if (ReadCheckFile(SM, CheckStrings))
650 return 2;
651
652 // Open the file to check and add it to SourceMgr.
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000653 OwningPtr<MemoryBuffer> File;
654 if (error_code ec =
655 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000656 errs() << "Could not open input file '" << InputFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +0000657 << ec.message() << '\n';
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000658 return true;
659 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000660 MemoryBuffer *F = File.take();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000661
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000662 // Remove duplicate spaces in the input file if requested.
663 if (!NoCanonicalizeWhiteSpace)
664 F = CanonicalizeInputFile(F);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000665
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000666 SM.AddNewSourceBuffer(F, SMLoc());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000667
Chris Lattnereec96952009-09-27 07:56:52 +0000668 /// VariableTable - This holds all the current filecheck variables.
669 StringMap<StringRef> VariableTable;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000670
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000671 // Check that we have all of the expected strings, in order, in the input
672 // file.
Chris Lattner96077032009-09-20 22:11:44 +0000673 StringRef Buffer = F->getBuffer();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000674
Chris Lattnerf15380b2009-09-20 22:35:26 +0000675 const char *LastMatch = Buffer.data();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000676
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000677 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
Chris Lattner207e1bc2009-08-15 17:41:04 +0000678 const CheckString &CheckStr = CheckStrings[StrNo];
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000679
Chris Lattner96077032009-09-20 22:11:44 +0000680 StringRef SearchFrom = Buffer;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000681
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000682 // Find StrNo in the file.
Chris Lattner9fc66782009-09-24 20:25:55 +0000683 size_t MatchLen = 0;
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000684 size_t MatchPos = CheckStr.Pat.Match(Buffer, MatchLen, VariableTable);
685 Buffer = Buffer.substr(MatchPos);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000686
Chris Lattner5dafafd2009-08-15 18:32:21 +0000687 // If we didn't find a match, reject the input.
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000688 if (MatchPos == StringRef::npos) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000689 PrintCheckFailed(SM, CheckStr, SearchFrom, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000690 return 1;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000691 }
Chris Lattner3711b7a2009-09-20 22:42:44 +0000692
693 StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
694
Chris Lattner5dafafd2009-08-15 18:32:21 +0000695 // If this check is a "CHECK-NEXT", verify that the previous match was on
696 // the previous line (i.e. that there is one newline between them).
697 if (CheckStr.IsCheckNext) {
698 // Count the number of newlines between the previous match and this one.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000699 assert(LastMatch != F->getBufferStart() &&
700 "CHECK-NEXT can't be the first check in a file");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000701
Chris Lattner3711b7a2009-09-20 22:42:44 +0000702 unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000703 if (NumNewLines == 0) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000704 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000705 CheckPrefix+"-NEXT: is on the same line as previous match",
706 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000707 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000708 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000709 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
710 "previous match was here", "note");
711 return 1;
712 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000713
Chris Lattner5dafafd2009-08-15 18:32:21 +0000714 if (NumNewLines != 1) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000715 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000716 CheckPrefix+
717 "-NEXT: is not on the line after the previous match",
718 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000719 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000720 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000721 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
722 "previous match was here", "note");
723 return 1;
724 }
725 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000726
Chris Lattnerf15380b2009-09-20 22:35:26 +0000727 // If this match had "not strings", verify that they don't exist in the
728 // skipped region.
Chris Lattnereec96952009-09-27 07:56:52 +0000729 for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size();
730 ChunkNo != e; ++ChunkNo) {
Chris Lattnera29703e2009-09-24 20:39:13 +0000731 size_t MatchLen = 0;
Chris Lattnereec96952009-09-27 07:56:52 +0000732 size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion,
733 MatchLen,
734 VariableTable);
Chris Lattnerf15380b2009-09-20 22:35:26 +0000735 if (Pos == StringRef::npos) continue;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000736
Chris Lattnerf15380b2009-09-20 22:35:26 +0000737 SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos),
738 CheckPrefix+"-NOT: string occurred!", "error");
Chris Lattner52870082009-09-24 21:47:32 +0000739 SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first,
Chris Lattnerf15380b2009-09-20 22:35:26 +0000740 CheckPrefix+"-NOT: pattern specified here", "note");
741 return 1;
742 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000743
Chris Lattner5dafafd2009-08-15 18:32:21 +0000744
Chris Lattner81115762009-09-21 02:30:42 +0000745 // Otherwise, everything is good. Step over the matched text and remember
746 // the position after the match as the end of the last match.
Chris Lattner9fc66782009-09-24 20:25:55 +0000747 Buffer = Buffer.substr(MatchLen);
Chris Lattner81115762009-09-21 02:30:42 +0000748 LastMatch = Buffer.data();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000749 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000750
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000751 return 0;
752}