blob: b8c14f0e9b5eca5cb98cfe7618b0ec1efe6f32b0 [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"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000025#include "llvm/Support/Signals.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000026#include "llvm/Support/system_error.h"
Daniel Dunbarfafe93c2009-11-22 22:08:06 +000027#include "llvm/ADT/SmallString.h"
Chris Lattnereec96952009-09-27 07:56:52 +000028#include "llvm/ADT/StringMap.h"
29#include <algorithm>
Chris Lattner81cb8ca2009-07-08 18:44:05 +000030using namespace llvm;
31
32static cl::opt<std::string>
33CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
34
35static cl::opt<std::string>
36InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
37 cl::init("-"), cl::value_desc("filename"));
38
39static cl::opt<std::string>
40CheckPrefix("check-prefix", cl::init("CHECK"),
41 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
42
Chris Lattner88a7e9e2009-07-11 18:58:15 +000043static cl::opt<bool>
44NoCanonicalizeWhiteSpace("strict-whitespace",
45 cl::desc("Do not treat all horizontal whitespace as equivalent"));
46
Chris Lattnera29703e2009-09-24 20:39:13 +000047//===----------------------------------------------------------------------===//
48// Pattern Handling Code.
49//===----------------------------------------------------------------------===//
50
Chris Lattner9fc66782009-09-24 20:25:55 +000051class Pattern {
Chris Lattner94638f02009-09-25 17:29:36 +000052 SMLoc PatternLoc;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000053
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +000054 /// MatchEOF - When set, this pattern only matches the end of file. This is
55 /// used for trailing CHECK-NOTs.
56 bool MatchEOF;
57
Chris Lattner5d6a05f2009-09-25 17:23:43 +000058 /// FixedStr - If non-empty, this pattern is a fixed string match with the
59 /// specified fixed string.
Chris Lattner2702e6a2009-09-25 17:09:12 +000060 StringRef FixedStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000061
Chris Lattner5d6a05f2009-09-25 17:23:43 +000062 /// RegEx - If non-empty, this is a regex pattern.
63 std::string RegExStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000064
Chris Lattnereec96952009-09-27 07:56:52 +000065 /// VariableUses - Entries in this vector map to uses of a variable in the
66 /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain
67 /// "foobaz" and we'll get an entry in this vector that tells us to insert the
68 /// value of bar at offset 3.
69 std::vector<std::pair<StringRef, unsigned> > VariableUses;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000070
Chris Lattnereec96952009-09-27 07:56:52 +000071 /// VariableDefs - Entries in this vector map to definitions of a variable in
72 /// the pattern, e.g. "foo[[bar:.*]]baz". In this case, the RegExStr will
73 /// contain "foo(.*)baz" and VariableDefs will contain the pair "bar",1. The
74 /// index indicates what parenthesized value captures the variable value.
75 std::vector<std::pair<StringRef, unsigned> > VariableDefs;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000076
Chris Lattner9fc66782009-09-24 20:25:55 +000077public:
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000078
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +000079 Pattern(bool matchEOF = false) : MatchEOF(matchEOF) { }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000080
Chris Lattnera29703e2009-09-24 20:39:13 +000081 bool ParsePattern(StringRef PatternStr, SourceMgr &SM);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000082
Chris Lattner9fc66782009-09-24 20:25:55 +000083 /// Match - Match the pattern string against the input buffer Buffer. This
84 /// returns the position that is matched or npos if there is no match. If
85 /// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +000086 ///
87 /// The VariableTable StringMap provides the current values of filecheck
88 /// variables and is updated if this match defines new values.
89 size_t Match(StringRef Buffer, size_t &MatchLen,
90 StringMap<StringRef> &VariableTable) const;
Daniel Dunbarfafe93c2009-11-22 22:08:06 +000091
92 /// PrintFailureInfo - Print additional information about a failure to match
93 /// involving this pattern.
94 void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
95 const StringMap<StringRef> &VariableTable) const;
96
Chris Lattner5d6a05f2009-09-25 17:23:43 +000097private:
Chris Lattnereec96952009-09-27 07:56:52 +000098 static void AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr);
99 bool AddRegExToRegEx(StringRef RegExStr, unsigned &CurParen, SourceMgr &SM);
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000100
101 /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of
102 /// matching this pattern at the start of \arg Buffer; a distance of zero
103 /// should correspond to a perfect match.
104 unsigned ComputeMatchDistance(StringRef Buffer,
105 const StringMap<StringRef> &VariableTable) const;
Chris Lattner9fc66782009-09-24 20:25:55 +0000106};
107
Chris Lattnereec96952009-09-27 07:56:52 +0000108
Chris Lattnera29703e2009-09-24 20:39:13 +0000109bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) {
Chris Lattner94638f02009-09-25 17:29:36 +0000110 PatternLoc = SMLoc::getFromPointer(PatternStr.data());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000111
Chris Lattnera29703e2009-09-24 20:39:13 +0000112 // Ignore trailing whitespace.
113 while (!PatternStr.empty() &&
114 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
115 PatternStr = PatternStr.substr(0, PatternStr.size()-1);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000116
Chris Lattnera29703e2009-09-24 20:39:13 +0000117 // Check that there is something on the line.
118 if (PatternStr.empty()) {
Chris Lattner94638f02009-09-25 17:29:36 +0000119 SM.PrintMessage(PatternLoc, "found empty check string with prefix '" +
120 CheckPrefix+":'", "error");
Chris Lattnera29703e2009-09-24 20:39:13 +0000121 return true;
122 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000123
Chris Lattner2702e6a2009-09-25 17:09:12 +0000124 // Check to see if this is a fixed string, or if it has regex pieces.
Chris Lattnereec96952009-09-27 07:56:52 +0000125 if (PatternStr.size() < 2 ||
126 (PatternStr.find("{{") == StringRef::npos &&
127 PatternStr.find("[[") == StringRef::npos)) {
Chris Lattner2702e6a2009-09-25 17:09:12 +0000128 FixedStr = PatternStr;
129 return false;
130 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000131
Chris Lattnereec96952009-09-27 07:56:52 +0000132 // Paren value #0 is for the fully matched string. Any new parenthesized
133 // values add from their.
134 unsigned CurParen = 1;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000135
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000136 // Otherwise, there is at least one regex piece. Build up the regex pattern
137 // by escaping scary characters in fixed strings, building up one big regex.
Chris Lattner52870082009-09-24 21:47:32 +0000138 while (!PatternStr.empty()) {
Chris Lattnereec96952009-09-27 07:56:52 +0000139 // RegEx matches.
140 if (PatternStr.size() >= 2 &&
141 PatternStr[0] == '{' && PatternStr[1] == '{') {
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()),
147 "found start of regex string with no end '}}'", "error");
148 return true;
149 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000150
Chris Lattnereec96952009-09-27 07:56:52 +0000151 if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
152 return true;
153 PatternStr = PatternStr.substr(End+2);
Chris Lattner52870082009-09-24 21:47:32 +0000154 continue;
155 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000156
Chris Lattnereec96952009-09-27 07:56:52 +0000157 // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
158 // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
159 // second form is [[foo]] which is a reference to foo. The variable name
Daniel Dunbar964ac012009-11-22 22:07:50 +0000160 // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
Chris Lattnereec96952009-09-27 07:56:52 +0000161 // it. This is to catch some common errors.
162 if (PatternStr.size() >= 2 &&
163 PatternStr[0] == '[' && PatternStr[1] == '[') {
164 // Verify that it is terminated properly.
165 size_t End = PatternStr.find("]]");
166 if (End == StringRef::npos) {
167 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
168 "invalid named regex reference, no ]] found", "error");
169 return true;
170 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000171
Chris Lattnereec96952009-09-27 07:56:52 +0000172 StringRef MatchStr = PatternStr.substr(2, End-2);
173 PatternStr = PatternStr.substr(End+2);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000174
Chris Lattnereec96952009-09-27 07:56:52 +0000175 // Get the regex name (e.g. "foo").
176 size_t NameEnd = MatchStr.find(':');
177 StringRef Name = MatchStr.substr(0, NameEnd);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000178
Chris Lattnereec96952009-09-27 07:56:52 +0000179 if (Name.empty()) {
180 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
181 "invalid name in named regex: empty name", "error");
182 return true;
183 }
184
185 // Verify that the name is well formed.
186 for (unsigned i = 0, e = Name.size(); i != e; ++i)
Daniel Dunbar964ac012009-11-22 22:07:50 +0000187 if (Name[i] != '_' &&
188 (Name[i] < 'a' || Name[i] > 'z') &&
Chris Lattnereec96952009-09-27 07:56:52 +0000189 (Name[i] < 'A' || Name[i] > 'Z') &&
190 (Name[i] < '0' || Name[i] > '9')) {
191 SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i),
192 "invalid name in named regex", "error");
193 return true;
194 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000195
Chris Lattnereec96952009-09-27 07:56:52 +0000196 // Name can't start with a digit.
197 if (isdigit(Name[0])) {
198 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
199 "invalid name in named regex", "error");
200 return true;
201 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000202
Chris Lattnereec96952009-09-27 07:56:52 +0000203 // Handle [[foo]].
204 if (NameEnd == StringRef::npos) {
205 VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
206 continue;
207 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000208
Chris Lattnereec96952009-09-27 07:56:52 +0000209 // Handle [[foo:.*]].
210 VariableDefs.push_back(std::make_pair(Name, CurParen));
211 RegExStr += '(';
212 ++CurParen;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000213
Chris Lattnereec96952009-09-27 07:56:52 +0000214 if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
215 return true;
216
217 RegExStr += ')';
Chris Lattner52870082009-09-24 21:47:32 +0000218 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000219
Chris Lattnereec96952009-09-27 07:56:52 +0000220 // Handle fixed string matches.
221 // Find the end, which is the start of the next regex.
222 size_t FixedMatchEnd = PatternStr.find("{{");
223 FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
224 AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr);
225 PatternStr = PatternStr.substr(FixedMatchEnd);
226 continue;
Chris Lattner52870082009-09-24 21:47:32 +0000227 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000228
Chris Lattnera29703e2009-09-24 20:39:13 +0000229 return false;
230}
231
Chris Lattnereec96952009-09-27 07:56:52 +0000232void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) {
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000233 // Add the characters from FixedStr to the regex, escaping as needed. This
234 // avoids "leaning toothpicks" in common patterns.
235 for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) {
236 switch (FixedStr[i]) {
237 // These are the special characters matched in "p_ere_exp".
238 case '(':
239 case ')':
240 case '^':
241 case '$':
242 case '|':
243 case '*':
244 case '+':
245 case '?':
246 case '.':
247 case '[':
248 case '\\':
249 case '{':
Chris Lattnereec96952009-09-27 07:56:52 +0000250 TheStr += '\\';
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000251 // FALL THROUGH.
252 default:
Chris Lattnereec96952009-09-27 07:56:52 +0000253 TheStr += FixedStr[i];
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000254 break;
255 }
256 }
257}
258
Chris Lattnereec96952009-09-27 07:56:52 +0000259bool Pattern::AddRegExToRegEx(StringRef RegexStr, unsigned &CurParen,
260 SourceMgr &SM) {
261 Regex R(RegexStr);
262 std::string Error;
263 if (!R.isValid(Error)) {
264 SM.PrintMessage(SMLoc::getFromPointer(RegexStr.data()),
265 "invalid regex: " + Error, "error");
266 return true;
267 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000268
Chris Lattnereec96952009-09-27 07:56:52 +0000269 RegExStr += RegexStr.str();
270 CurParen += R.getNumMatches();
271 return false;
272}
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000273
Chris Lattner52870082009-09-24 21:47:32 +0000274/// Match - Match the pattern string against the input buffer Buffer. This
275/// returns the position that is matched or npos if there is no match. If
276/// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +0000277size_t Pattern::Match(StringRef Buffer, size_t &MatchLen,
278 StringMap<StringRef> &VariableTable) const {
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000279 // If this is the EOF pattern, match it immediately.
280 if (MatchEOF) {
281 MatchLen = 0;
282 return Buffer.size();
283 }
284
Chris Lattner2702e6a2009-09-25 17:09:12 +0000285 // If this is a fixed string pattern, just match it now.
286 if (!FixedStr.empty()) {
287 MatchLen = FixedStr.size();
288 return Buffer.find(FixedStr);
289 }
Chris Lattnereec96952009-09-27 07:56:52 +0000290
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000291 // Regex match.
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000292
Chris Lattnereec96952009-09-27 07:56:52 +0000293 // If there are variable uses, we need to create a temporary string with the
294 // actual value.
295 StringRef RegExToMatch = RegExStr;
296 std::string TmpStr;
297 if (!VariableUses.empty()) {
298 TmpStr = RegExStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000299
Chris Lattnereec96952009-09-27 07:56:52 +0000300 unsigned InsertOffset = 0;
301 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000302 StringMap<StringRef>::iterator it =
303 VariableTable.find(VariableUses[i].first);
304 // If the variable is undefined, return an error.
305 if (it == VariableTable.end())
306 return StringRef::npos;
307
Chris Lattnereec96952009-09-27 07:56:52 +0000308 // Look up the value and escape it so that we can plop it into the regex.
309 std::string Value;
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000310 AddFixedStringToRegEx(it->second, Value);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000311
Chris Lattnereec96952009-09-27 07:56:52 +0000312 // Plop it into the regex at the adjusted offset.
313 TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset,
314 Value.begin(), Value.end());
315 InsertOffset += Value.size();
316 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000317
Chris Lattnereec96952009-09-27 07:56:52 +0000318 // Match the newly constructed regex.
319 RegExToMatch = TmpStr;
320 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000321
322
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000323 SmallVector<StringRef, 4> MatchInfo;
Chris Lattnereec96952009-09-27 07:56:52 +0000324 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000325 return StringRef::npos;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000326
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000327 // Successful regex match.
328 assert(!MatchInfo.empty() && "Didn't get any match");
329 StringRef FullMatch = MatchInfo[0];
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000330
Chris Lattnereec96952009-09-27 07:56:52 +0000331 // If this defines any variables, remember their values.
332 for (unsigned i = 0, e = VariableDefs.size(); i != e; ++i) {
333 assert(VariableDefs[i].second < MatchInfo.size() &&
334 "Internal paren error");
335 VariableTable[VariableDefs[i].first] = MatchInfo[VariableDefs[i].second];
Chris Lattner94638f02009-09-25 17:29:36 +0000336 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000337
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000338 MatchLen = FullMatch.size();
339 return FullMatch.data()-Buffer.data();
Chris Lattner52870082009-09-24 21:47:32 +0000340}
341
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000342unsigned Pattern::ComputeMatchDistance(StringRef Buffer,
343 const StringMap<StringRef> &VariableTable) const {
344 // Just compute the number of matching characters. For regular expressions, we
345 // just compare against the regex itself and hope for the best.
346 //
347 // FIXME: One easy improvement here is have the regex lib generate a single
348 // example regular expression which matches, and use that as the example
349 // string.
350 StringRef ExampleString(FixedStr);
351 if (ExampleString.empty())
352 ExampleString = RegExStr;
353
Daniel Dunbar0806f9f2010-01-30 00:24:06 +0000354 // Only compare up to the first line in the buffer, or the string size.
355 StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
356 BufferPrefix = BufferPrefix.split('\n').first;
357 return BufferPrefix.edit_distance(ExampleString);
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000358}
359
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000360void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
361 const StringMap<StringRef> &VariableTable) const{
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000362 // If this was a regular expression using variables, print the current
363 // variable values.
364 if (!VariableUses.empty()) {
365 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
366 StringRef Var = VariableUses[i].first;
367 StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
368 SmallString<256> Msg;
369 raw_svector_ostream OS(Msg);
370
371 // Check for undefined variable references.
372 if (it == VariableTable.end()) {
373 OS << "uses undefined variable \"";
374 OS.write_escaped(Var) << "\"";;
375 } else {
376 OS << "with variable \"";
377 OS.write_escaped(Var) << "\" equal to \"";
378 OS.write_escaped(it->second) << "\"";
379 }
380
381 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), OS.str(), "note",
382 /*ShowLine=*/false);
383 }
384 }
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000385
386 // Attempt to find the closest/best fuzzy match. Usually an error happens
387 // because some string in the output didn't exactly match. In these cases, we
388 // would like to show the user a best guess at what "should have" matched, to
389 // save them having to actually check the input manually.
390 size_t NumLinesForward = 0;
391 size_t Best = StringRef::npos;
392 double BestQuality = 0;
393
394 // Use an arbitrary 4k limit on how far we will search.
Dan Gohmane3a1e502010-01-29 21:57:46 +0000395 for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000396 if (Buffer[i] == '\n')
397 ++NumLinesForward;
398
Dan Gohmand8a55412010-01-29 21:55:16 +0000399 // Patterns have leading whitespace stripped, so skip whitespace when
400 // looking for something which looks like a pattern.
401 if (Buffer[i] == ' ' || Buffer[i] == '\t')
402 continue;
403
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000404 // Compute the "quality" of this match as an arbitrary combination of the
405 // match distance and the number of lines skipped to get to this match.
406 unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable);
407 double Quality = Distance + (NumLinesForward / 100.);
408
409 if (Quality < BestQuality || Best == StringRef::npos) {
410 Best = i;
411 BestQuality = Quality;
412 }
413 }
414
Daniel Dunbar7a68e0d2010-03-19 18:07:43 +0000415 // Print the "possible intended match here" line if we found something
416 // reasonable and not equal to what we showed in the "scanning from here"
417 // line.
418 if (Best && Best != StringRef::npos && BestQuality < 50) {
419 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
420 "possible intended match here", "note");
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000421
422 // FIXME: If we wanted to be really friendly we would show why the match
423 // failed, as it can be hard to spot simple one character differences.
424 }
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000425}
Chris Lattnera29703e2009-09-24 20:39:13 +0000426
427//===----------------------------------------------------------------------===//
428// Check Strings.
429//===----------------------------------------------------------------------===//
Chris Lattner9fc66782009-09-24 20:25:55 +0000430
431/// CheckString - This is a check that we found in the input file.
432struct CheckString {
433 /// Pat - The pattern to match.
434 Pattern Pat;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000435
Chris Lattner207e1bc2009-08-15 17:41:04 +0000436 /// Loc - The location in the match file that the check string was specified.
437 SMLoc Loc;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000438
Chris Lattner5dafafd2009-08-15 18:32:21 +0000439 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
440 /// to a CHECK: directive.
441 bool IsCheckNext;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000442
Chris Lattnerf15380b2009-09-20 22:35:26 +0000443 /// NotStrings - These are all of the strings that are disallowed from
444 /// occurring between this match string and the previous one (or start of
445 /// file).
Chris Lattnera29703e2009-09-24 20:39:13 +0000446 std::vector<std::pair<SMLoc, Pattern> > NotStrings;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000447
Chris Lattner9fc66782009-09-24 20:25:55 +0000448 CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
449 : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
Chris Lattner207e1bc2009-08-15 17:41:04 +0000450};
451
Chris Lattneradea46e2009-09-24 20:45:07 +0000452/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
453/// memory buffer, free it, and return a new one.
454static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
Chris Lattner4c842dd2010-04-05 22:42:30 +0000455 SmallString<128> NewFile;
Chris Lattneradea46e2009-09-24 20:45:07 +0000456 NewFile.reserve(MB->getBufferSize());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000457
Chris Lattneradea46e2009-09-24 20:45:07 +0000458 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
459 Ptr != End; ++Ptr) {
NAKAMURA Takumi9f6e03f2010-11-14 03:28:22 +0000460 // Eliminate trailing dosish \r.
461 if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
462 continue;
463 }
464
Chris Lattneradea46e2009-09-24 20:45:07 +0000465 // If C is not a horizontal whitespace, skip it.
466 if (*Ptr != ' ' && *Ptr != '\t') {
467 NewFile.push_back(*Ptr);
468 continue;
469 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000470
Chris Lattneradea46e2009-09-24 20:45:07 +0000471 // Otherwise, add one space and advance over neighboring space.
472 NewFile.push_back(' ');
473 while (Ptr+1 != End &&
474 (Ptr[1] == ' ' || Ptr[1] == '\t'))
475 ++Ptr;
476 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000477
Chris Lattneradea46e2009-09-24 20:45:07 +0000478 // Free the old buffer and return a new one.
479 MemoryBuffer *MB2 =
Chris Lattner4c842dd2010-04-05 22:42:30 +0000480 MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000481
Chris Lattneradea46e2009-09-24 20:45:07 +0000482 delete MB;
483 return MB2;
484}
485
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000486
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000487/// ReadCheckFile - Read the check file, which specifies the sequence of
488/// expected strings. The strings are added to the CheckStrings vector.
489static bool ReadCheckFile(SourceMgr &SM,
Chris Lattner207e1bc2009-08-15 17:41:04 +0000490 std::vector<CheckString> &CheckStrings) {
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000491 // Open the check file, and tell SourceMgr about it.
Michael J. Spencer333fb042010-12-09 17:36:48 +0000492 error_code ec;
493 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), ec);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000494 if (F == 0) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000495 errs() << "Could not open check file '" << CheckFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +0000496 << ec.message() << '\n';
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000497 return true;
498 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000499
Chris Lattneradea46e2009-09-24 20:45:07 +0000500 // If we want to canonicalize whitespace, strip excess whitespace from the
501 // buffer containing the CHECK lines.
502 if (!NoCanonicalizeWhiteSpace)
503 F = CanonicalizeInputFile(F);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000504
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000505 SM.AddNewSourceBuffer(F, SMLoc());
506
Chris Lattnerd7e25052009-08-15 18:00:42 +0000507 // Find all instances of CheckPrefix followed by : in the file.
Chris Lattner96077032009-09-20 22:11:44 +0000508 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000509
Chris Lattnera29703e2009-09-24 20:39:13 +0000510 std::vector<std::pair<SMLoc, Pattern> > NotMatches;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000511
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000512 while (1) {
513 // See if Prefix occurs in the memory buffer.
Chris Lattner96077032009-09-20 22:11:44 +0000514 Buffer = Buffer.substr(Buffer.find(CheckPrefix));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000515
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000516 // If we didn't find a match, we're done.
Chris Lattner96077032009-09-20 22:11:44 +0000517 if (Buffer.empty())
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000518 break;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000519
Chris Lattner96077032009-09-20 22:11:44 +0000520 const char *CheckPrefixStart = Buffer.data();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000521
Chris Lattner5dafafd2009-08-15 18:32:21 +0000522 // When we find a check prefix, keep track of whether we find CHECK: or
523 // CHECK-NEXT:
Chris Lattnerf15380b2009-09-20 22:35:26 +0000524 bool IsCheckNext = false, IsCheckNot = false;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000525
Chris Lattnerd7e25052009-08-15 18:00:42 +0000526 // Verify that the : is present after the prefix.
Chris Lattner96077032009-09-20 22:11:44 +0000527 if (Buffer[CheckPrefix.size()] == ':') {
528 Buffer = Buffer.substr(CheckPrefix.size()+1);
Chris Lattner96077032009-09-20 22:11:44 +0000529 } else if (Buffer.size() > CheckPrefix.size()+6 &&
530 memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
531 Buffer = Buffer.substr(CheckPrefix.size()+7);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000532 IsCheckNext = true;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000533 } else if (Buffer.size() > CheckPrefix.size()+5 &&
534 memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) {
535 Buffer = Buffer.substr(CheckPrefix.size()+6);
536 IsCheckNot = true;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000537 } else {
Chris Lattner96077032009-09-20 22:11:44 +0000538 Buffer = Buffer.substr(1);
Chris Lattnerd7e25052009-08-15 18:00:42 +0000539 continue;
540 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000541
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000542 // Okay, we found the prefix, yay. Remember the rest of the line, but
543 // ignore leading and trailing whitespace.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000544 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000545
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000546 // Scan ahead to the end of line.
Chris Lattner96077032009-09-20 22:11:44 +0000547 size_t EOL = Buffer.find_first_of("\n\r");
Chris Lattnera29703e2009-09-24 20:39:13 +0000548
Dan Gohmane5463432010-01-29 21:53:18 +0000549 // Remember the location of the start of the pattern, for diagnostics.
550 SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
551
Chris Lattnera29703e2009-09-24 20:39:13 +0000552 // Parse the pattern.
553 Pattern P;
554 if (P.ParsePattern(Buffer.substr(0, EOL), SM))
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000555 return true;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000556
Chris Lattnera29703e2009-09-24 20:39:13 +0000557 Buffer = Buffer.substr(EOL);
558
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000559
Chris Lattner5dafafd2009-08-15 18:32:21 +0000560 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
561 if (IsCheckNext && CheckStrings.empty()) {
562 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
563 "found '"+CheckPrefix+"-NEXT:' without previous '"+
564 CheckPrefix+ ": line", "error");
565 return true;
566 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000567
Chris Lattnera29703e2009-09-24 20:39:13 +0000568 // Handle CHECK-NOT.
569 if (IsCheckNot) {
570 NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
571 P));
572 continue;
573 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000574
575
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000576 // Okay, add the string we captured to the output vector and move on.
Chris Lattner9fc66782009-09-24 20:25:55 +0000577 CheckStrings.push_back(CheckString(P,
Dan Gohmane5463432010-01-29 21:53:18 +0000578 PatternLoc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000579 IsCheckNext));
Chris Lattnerf15380b2009-09-20 22:35:26 +0000580 std::swap(NotMatches, CheckStrings.back().NotStrings);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000581 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000582
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000583 // Add an EOF pattern for any trailing CHECK-NOTs.
584 if (!NotMatches.empty()) {
585 CheckStrings.push_back(CheckString(Pattern(true),
586 SMLoc::getFromPointer(Buffer.data()),
587 false));
588 std::swap(NotMatches, CheckStrings.back().NotStrings);
589 }
590
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000591 if (CheckStrings.empty()) {
Chris Lattnerd7e25052009-08-15 18:00:42 +0000592 errs() << "error: no check strings found with prefix '" << CheckPrefix
593 << ":'\n";
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000594 return true;
595 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000596
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000597 return false;
598}
599
Chris Lattner5dafafd2009-08-15 18:32:21 +0000600static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000601 StringRef Buffer,
602 StringMap<StringRef> &VariableTable) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000603 // Otherwise, we have an error, emit an error message.
604 SM.PrintMessage(CheckStr.Loc, "expected string not found in input",
605 "error");
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000606
Chris Lattner5dafafd2009-08-15 18:32:21 +0000607 // Print the "scanning from here" line. If the current position is at the
608 // end of a line, advance to the start of the next line.
Chris Lattner96077032009-09-20 22:11:44 +0000609 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000610
Chris Lattner96077032009-09-20 22:11:44 +0000611 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here",
Chris Lattner5dafafd2009-08-15 18:32:21 +0000612 "note");
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000613
614 // Allow the pattern to print additional information if desired.
615 CheckStr.Pat.PrintFailureInfo(SM, Buffer, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000616}
617
Chris Lattner3711b7a2009-09-20 22:42:44 +0000618/// CountNumNewlinesBetween - Count the number of newlines in the specified
619/// range.
620static unsigned CountNumNewlinesBetween(StringRef Range) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000621 unsigned NumNewLines = 0;
Chris Lattner3711b7a2009-09-20 22:42:44 +0000622 while (1) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000623 // Scan for newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000624 Range = Range.substr(Range.find_first_of("\n\r"));
625 if (Range.empty()) return NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000626
Chris Lattner5dafafd2009-08-15 18:32:21 +0000627 ++NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000628
Chris Lattner5dafafd2009-08-15 18:32:21 +0000629 // Handle \n\r and \r\n as a single newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000630 if (Range.size() > 1 &&
631 (Range[1] == '\n' || Range[1] == '\r') &&
632 (Range[0] != Range[1]))
633 Range = Range.substr(1);
634 Range = Range.substr(1);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000635 }
Chris Lattner5dafafd2009-08-15 18:32:21 +0000636}
637
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000638int main(int argc, char **argv) {
639 sys::PrintStackTraceOnErrorSignal();
640 PrettyStackTraceProgram X(argc, argv);
641 cl::ParseCommandLineOptions(argc, argv);
642
643 SourceMgr SM;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000644
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000645 // Read the expected strings from the check file.
Chris Lattner207e1bc2009-08-15 17:41:04 +0000646 std::vector<CheckString> CheckStrings;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000647 if (ReadCheckFile(SM, CheckStrings))
648 return 2;
649
650 // Open the file to check and add it to SourceMgr.
Michael J. Spencer333fb042010-12-09 17:36:48 +0000651 error_code ec;
652 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000653 if (F == 0) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000654 errs() << "Could not open input file '" << InputFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +0000655 << ec.message() << '\n';
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000656 return true;
657 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000658
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000659 // Remove duplicate spaces in the input file if requested.
660 if (!NoCanonicalizeWhiteSpace)
661 F = CanonicalizeInputFile(F);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000662
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000663 SM.AddNewSourceBuffer(F, SMLoc());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000664
Chris Lattnereec96952009-09-27 07:56:52 +0000665 /// VariableTable - This holds all the current filecheck variables.
666 StringMap<StringRef> VariableTable;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000667
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000668 // Check that we have all of the expected strings, in order, in the input
669 // file.
Chris Lattner96077032009-09-20 22:11:44 +0000670 StringRef Buffer = F->getBuffer();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000671
Chris Lattnerf15380b2009-09-20 22:35:26 +0000672 const char *LastMatch = Buffer.data();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000673
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000674 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
Chris Lattner207e1bc2009-08-15 17:41:04 +0000675 const CheckString &CheckStr = CheckStrings[StrNo];
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000676
Chris Lattner96077032009-09-20 22:11:44 +0000677 StringRef SearchFrom = Buffer;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000678
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000679 // Find StrNo in the file.
Chris Lattner9fc66782009-09-24 20:25:55 +0000680 size_t MatchLen = 0;
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000681 size_t MatchPos = CheckStr.Pat.Match(Buffer, MatchLen, VariableTable);
682 Buffer = Buffer.substr(MatchPos);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000683
Chris Lattner5dafafd2009-08-15 18:32:21 +0000684 // If we didn't find a match, reject the input.
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000685 if (MatchPos == StringRef::npos) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000686 PrintCheckFailed(SM, CheckStr, SearchFrom, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000687 return 1;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000688 }
Chris Lattner3711b7a2009-09-20 22:42:44 +0000689
690 StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
691
Chris Lattner5dafafd2009-08-15 18:32:21 +0000692 // If this check is a "CHECK-NEXT", verify that the previous match was on
693 // the previous line (i.e. that there is one newline between them).
694 if (CheckStr.IsCheckNext) {
695 // Count the number of newlines between the previous match and this one.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000696 assert(LastMatch != F->getBufferStart() &&
697 "CHECK-NEXT can't be the first check in a file");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000698
Chris Lattner3711b7a2009-09-20 22:42:44 +0000699 unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000700 if (NumNewLines == 0) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000701 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000702 CheckPrefix+"-NEXT: is on the same line as previous match",
703 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000704 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000705 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000706 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
707 "previous match was here", "note");
708 return 1;
709 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000710
Chris Lattner5dafafd2009-08-15 18:32:21 +0000711 if (NumNewLines != 1) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000712 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000713 CheckPrefix+
714 "-NEXT: is not on the line after the 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 }
722 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000723
Chris Lattnerf15380b2009-09-20 22:35:26 +0000724 // If this match had "not strings", verify that they don't exist in the
725 // skipped region.
Chris Lattnereec96952009-09-27 07:56:52 +0000726 for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size();
727 ChunkNo != e; ++ChunkNo) {
Chris Lattnera29703e2009-09-24 20:39:13 +0000728 size_t MatchLen = 0;
Chris Lattnereec96952009-09-27 07:56:52 +0000729 size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion,
730 MatchLen,
731 VariableTable);
Chris Lattnerf15380b2009-09-20 22:35:26 +0000732 if (Pos == StringRef::npos) continue;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000733
Chris Lattnerf15380b2009-09-20 22:35:26 +0000734 SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos),
735 CheckPrefix+"-NOT: string occurred!", "error");
Chris Lattner52870082009-09-24 21:47:32 +0000736 SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first,
Chris Lattnerf15380b2009-09-20 22:35:26 +0000737 CheckPrefix+"-NOT: pattern specified here", "note");
738 return 1;
739 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000740
Chris Lattner5dafafd2009-08-15 18:32:21 +0000741
Chris Lattner81115762009-09-21 02:30:42 +0000742 // Otherwise, everything is good. Step over the matched text and remember
743 // the position after the match as the end of the last match.
Chris Lattner9fc66782009-09-24 20:25:55 +0000744 Buffer = Buffer.substr(MatchLen);
Chris Lattner81115762009-09-21 02:30:42 +0000745 LastMatch = Buffer.data();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000746 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000747
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000748 return 0;
749}