blob: afbce35af5b07414c342423e069744aba02c2e2a [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 Lattner3f2d5f62011-10-16 05:43:57 +0000120 SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
121 "found empty check string with prefix '" +
122 CheckPrefix+":'");
Chris Lattnera29703e2009-09-24 20:39:13 +0000123 return true;
124 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000125
Chris Lattner2702e6a2009-09-25 17:09:12 +0000126 // Check to see if this is a fixed string, or if it has regex pieces.
Ted Kremenek4f505172012-09-08 04:32:13 +0000127 if (PatternStr.size() < 2 ||
Chris Lattnereec96952009-09-27 07:56:52 +0000128 (PatternStr.find("{{") == StringRef::npos &&
129 PatternStr.find("[[") == StringRef::npos)) {
Chris Lattner2702e6a2009-09-25 17:09:12 +0000130 FixedStr = PatternStr;
131 return false;
132 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000133
Chris Lattnereec96952009-09-27 07:56:52 +0000134 // Paren value #0 is for the fully matched string. Any new parenthesized
Chris Lattner13a38c42011-04-09 06:18:02 +0000135 // values add from there.
Chris Lattnereec96952009-09-27 07:56:52 +0000136 unsigned CurParen = 1;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000137
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000138 // Otherwise, there is at least one regex piece. Build up the regex pattern
139 // by escaping scary characters in fixed strings, building up one big regex.
Chris Lattner52870082009-09-24 21:47:32 +0000140 while (!PatternStr.empty()) {
Chris Lattnereec96952009-09-27 07:56:52 +0000141 // RegEx matches.
Chris Lattner13a38c42011-04-09 06:18:02 +0000142 if (PatternStr.startswith("{{")) {
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()),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000148 SourceMgr::DK_Error,
149 "found start of regex string with no end '}}'");
Chris Lattnereec96952009-09-27 07:56:52 +0000150 return true;
151 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000152
Chris Lattner42e31df2011-04-09 06:37:03 +0000153 // Enclose {{}} patterns in parens just like [[]] even though we're not
154 // capturing the result for any purpose. This is required in case the
155 // expression contains an alternation like: CHECK: abc{{x|z}}def. We
156 // want this to turn into: "abc(x|z)def" not "abcx|zdef".
157 RegExStr += '(';
158 ++CurParen;
159
Chris Lattnereec96952009-09-27 07:56:52 +0000160 if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
161 return true;
Chris Lattner42e31df2011-04-09 06:37:03 +0000162 RegExStr += ')';
Chris Lattner13a38c42011-04-09 06:18:02 +0000163
Chris Lattnereec96952009-09-27 07:56:52 +0000164 PatternStr = PatternStr.substr(End+2);
Chris Lattner52870082009-09-24 21:47:32 +0000165 continue;
166 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000167
Chris Lattnereec96952009-09-27 07:56:52 +0000168 // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
169 // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
170 // second form is [[foo]] which is a reference to foo. The variable name
Daniel Dunbar964ac012009-11-22 22:07:50 +0000171 // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
Chris Lattnereec96952009-09-27 07:56:52 +0000172 // it. This is to catch some common errors.
Chris Lattner13a38c42011-04-09 06:18:02 +0000173 if (PatternStr.startswith("[[")) {
Chris Lattnereec96952009-09-27 07:56:52 +0000174 // Verify that it is terminated properly.
175 size_t End = PatternStr.find("]]");
176 if (End == StringRef::npos) {
177 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000178 SourceMgr::DK_Error,
179 "invalid named regex reference, no ]] found");
Chris Lattnereec96952009-09-27 07:56:52 +0000180 return true;
181 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000182
Chris Lattnereec96952009-09-27 07:56:52 +0000183 StringRef MatchStr = PatternStr.substr(2, End-2);
184 PatternStr = PatternStr.substr(End+2);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000185
Chris Lattnereec96952009-09-27 07:56:52 +0000186 // Get the regex name (e.g. "foo").
187 size_t NameEnd = MatchStr.find(':');
188 StringRef Name = MatchStr.substr(0, NameEnd);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000189
Chris Lattnereec96952009-09-27 07:56:52 +0000190 if (Name.empty()) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000191 SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
192 "invalid name in named regex: empty name");
Chris Lattnereec96952009-09-27 07:56:52 +0000193 return true;
194 }
195
196 // Verify that the name is well formed.
197 for (unsigned i = 0, e = Name.size(); i != e; ++i)
Chris Lattner13a38c42011-04-09 06:18:02 +0000198 if (Name[i] != '_' && !isalnum(Name[i])) {
Chris Lattnereec96952009-09-27 07:56:52 +0000199 SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000200 SourceMgr::DK_Error, "invalid name in named regex");
Chris Lattnereec96952009-09-27 07:56:52 +0000201 return true;
202 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000203
Chris Lattnereec96952009-09-27 07:56:52 +0000204 // Name can't start with a digit.
205 if (isdigit(Name[0])) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000206 SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
207 "invalid name in named regex");
Chris Lattnereec96952009-09-27 07:56:52 +0000208 return true;
209 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000210
Chris Lattnereec96952009-09-27 07:56:52 +0000211 // Handle [[foo]].
212 if (NameEnd == StringRef::npos) {
213 VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
214 continue;
215 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000216
Chris Lattnereec96952009-09-27 07:56:52 +0000217 // Handle [[foo:.*]].
218 VariableDefs.push_back(std::make_pair(Name, CurParen));
219 RegExStr += '(';
220 ++CurParen;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000221
Chris Lattnereec96952009-09-27 07:56:52 +0000222 if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
223 return true;
224
225 RegExStr += ')';
Chris Lattner52870082009-09-24 21:47:32 +0000226 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000227
Chris Lattnereec96952009-09-27 07:56:52 +0000228 // Handle fixed string matches.
229 // Find the end, which is the start of the next regex.
230 size_t FixedMatchEnd = PatternStr.find("{{");
231 FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
232 AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr);
233 PatternStr = PatternStr.substr(FixedMatchEnd);
234 continue;
Chris Lattner52870082009-09-24 21:47:32 +0000235 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000236
Chris Lattnera29703e2009-09-24 20:39:13 +0000237 return false;
238}
239
Chris Lattnereec96952009-09-27 07:56:52 +0000240void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) {
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000241 // Add the characters from FixedStr to the regex, escaping as needed. This
242 // avoids "leaning toothpicks" in common patterns.
243 for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) {
244 switch (FixedStr[i]) {
245 // These are the special characters matched in "p_ere_exp".
246 case '(':
247 case ')':
248 case '^':
249 case '$':
250 case '|':
251 case '*':
252 case '+':
253 case '?':
254 case '.':
255 case '[':
256 case '\\':
257 case '{':
Chris Lattnereec96952009-09-27 07:56:52 +0000258 TheStr += '\\';
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000259 // FALL THROUGH.
260 default:
Chris Lattnereec96952009-09-27 07:56:52 +0000261 TheStr += FixedStr[i];
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000262 break;
263 }
264 }
265}
266
Chris Lattnereec96952009-09-27 07:56:52 +0000267bool Pattern::AddRegExToRegEx(StringRef RegexStr, unsigned &CurParen,
268 SourceMgr &SM) {
269 Regex R(RegexStr);
270 std::string Error;
271 if (!R.isValid(Error)) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000272 SM.PrintMessage(SMLoc::getFromPointer(RegexStr.data()), SourceMgr::DK_Error,
273 "invalid regex: " + Error);
Chris Lattnereec96952009-09-27 07:56:52 +0000274 return true;
275 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000276
Chris Lattnereec96952009-09-27 07:56:52 +0000277 RegExStr += RegexStr.str();
278 CurParen += R.getNumMatches();
279 return false;
280}
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000281
Chris Lattner52870082009-09-24 21:47:32 +0000282/// Match - Match the pattern string against the input buffer Buffer. This
283/// returns the position that is matched or npos if there is no match. If
284/// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +0000285size_t Pattern::Match(StringRef Buffer, size_t &MatchLen,
286 StringMap<StringRef> &VariableTable) const {
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000287 // If this is the EOF pattern, match it immediately.
288 if (MatchEOF) {
289 MatchLen = 0;
290 return Buffer.size();
291 }
292
Chris Lattner2702e6a2009-09-25 17:09:12 +0000293 // If this is a fixed string pattern, just match it now.
294 if (!FixedStr.empty()) {
295 MatchLen = FixedStr.size();
296 return Buffer.find(FixedStr);
297 }
Chris Lattnereec96952009-09-27 07:56:52 +0000298
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000299 // Regex match.
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000300
Chris Lattnereec96952009-09-27 07:56:52 +0000301 // If there are variable uses, we need to create a temporary string with the
302 // actual value.
303 StringRef RegExToMatch = RegExStr;
304 std::string TmpStr;
305 if (!VariableUses.empty()) {
306 TmpStr = RegExStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000307
Chris Lattnereec96952009-09-27 07:56:52 +0000308 unsigned InsertOffset = 0;
309 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000310 StringMap<StringRef>::iterator it =
311 VariableTable.find(VariableUses[i].first);
312 // If the variable is undefined, return an error.
313 if (it == VariableTable.end())
314 return StringRef::npos;
315
Chris Lattnereec96952009-09-27 07:56:52 +0000316 // Look up the value and escape it so that we can plop it into the regex.
317 std::string Value;
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000318 AddFixedStringToRegEx(it->second, Value);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000319
Chris Lattnereec96952009-09-27 07:56:52 +0000320 // Plop it into the regex at the adjusted offset.
321 TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset,
322 Value.begin(), Value.end());
323 InsertOffset += Value.size();
324 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000325
Chris Lattnereec96952009-09-27 07:56:52 +0000326 // Match the newly constructed regex.
327 RegExToMatch = TmpStr;
328 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000329
330
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000331 SmallVector<StringRef, 4> MatchInfo;
Chris Lattnereec96952009-09-27 07:56:52 +0000332 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000333 return StringRef::npos;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000334
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000335 // Successful regex match.
336 assert(!MatchInfo.empty() && "Didn't get any match");
337 StringRef FullMatch = MatchInfo[0];
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000338
Chris Lattnereec96952009-09-27 07:56:52 +0000339 // If this defines any variables, remember their values.
340 for (unsigned i = 0, e = VariableDefs.size(); i != e; ++i) {
341 assert(VariableDefs[i].second < MatchInfo.size() &&
342 "Internal paren error");
343 VariableTable[VariableDefs[i].first] = MatchInfo[VariableDefs[i].second];
Chris Lattner94638f02009-09-25 17:29:36 +0000344 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000345
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000346 MatchLen = FullMatch.size();
347 return FullMatch.data()-Buffer.data();
Chris Lattner52870082009-09-24 21:47:32 +0000348}
349
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000350unsigned Pattern::ComputeMatchDistance(StringRef Buffer,
351 const StringMap<StringRef> &VariableTable) const {
352 // Just compute the number of matching characters. For regular expressions, we
353 // just compare against the regex itself and hope for the best.
354 //
355 // FIXME: One easy improvement here is have the regex lib generate a single
356 // example regular expression which matches, and use that as the example
357 // string.
358 StringRef ExampleString(FixedStr);
359 if (ExampleString.empty())
360 ExampleString = RegExStr;
361
Daniel Dunbar0806f9f2010-01-30 00:24:06 +0000362 // Only compare up to the first line in the buffer, or the string size.
363 StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
364 BufferPrefix = BufferPrefix.split('\n').first;
365 return BufferPrefix.edit_distance(ExampleString);
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000366}
367
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000368void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
369 const StringMap<StringRef> &VariableTable) const{
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000370 // If this was a regular expression using variables, print the current
371 // variable values.
372 if (!VariableUses.empty()) {
373 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
374 StringRef Var = VariableUses[i].first;
375 StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
376 SmallString<256> Msg;
377 raw_svector_ostream OS(Msg);
378
379 // Check for undefined variable references.
380 if (it == VariableTable.end()) {
381 OS << "uses undefined variable \"";
382 OS.write_escaped(Var) << "\"";;
383 } else {
384 OS << "with variable \"";
385 OS.write_escaped(Var) << "\" equal to \"";
386 OS.write_escaped(it->second) << "\"";
387 }
388
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000389 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
390 OS.str());
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000391 }
392 }
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000393
394 // Attempt to find the closest/best fuzzy match. Usually an error happens
395 // because some string in the output didn't exactly match. In these cases, we
396 // would like to show the user a best guess at what "should have" matched, to
397 // save them having to actually check the input manually.
398 size_t NumLinesForward = 0;
399 size_t Best = StringRef::npos;
400 double BestQuality = 0;
401
402 // Use an arbitrary 4k limit on how far we will search.
Dan Gohmane3a1e502010-01-29 21:57:46 +0000403 for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000404 if (Buffer[i] == '\n')
405 ++NumLinesForward;
406
Dan Gohmand8a55412010-01-29 21:55:16 +0000407 // Patterns have leading whitespace stripped, so skip whitespace when
408 // looking for something which looks like a pattern.
409 if (Buffer[i] == ' ' || Buffer[i] == '\t')
410 continue;
411
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000412 // Compute the "quality" of this match as an arbitrary combination of the
413 // match distance and the number of lines skipped to get to this match.
414 unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable);
415 double Quality = Distance + (NumLinesForward / 100.);
416
417 if (Quality < BestQuality || Best == StringRef::npos) {
418 Best = i;
419 BestQuality = Quality;
420 }
421 }
422
Daniel Dunbar7a68e0d2010-03-19 18:07:43 +0000423 // Print the "possible intended match here" line if we found something
424 // reasonable and not equal to what we showed in the "scanning from here"
425 // line.
426 if (Best && Best != StringRef::npos && BestQuality < 50) {
427 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000428 SourceMgr::DK_Note, "possible intended match here");
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000429
430 // FIXME: If we wanted to be really friendly we would show why the match
431 // failed, as it can be hard to spot simple one character differences.
432 }
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000433}
Chris Lattnera29703e2009-09-24 20:39:13 +0000434
435//===----------------------------------------------------------------------===//
436// Check Strings.
437//===----------------------------------------------------------------------===//
Chris Lattner9fc66782009-09-24 20:25:55 +0000438
439/// CheckString - This is a check that we found in the input file.
440struct CheckString {
441 /// Pat - The pattern to match.
442 Pattern Pat;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000443
Chris Lattner207e1bc2009-08-15 17:41:04 +0000444 /// Loc - The location in the match file that the check string was specified.
445 SMLoc Loc;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000446
Chris Lattner5dafafd2009-08-15 18:32:21 +0000447 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
448 /// to a CHECK: directive.
449 bool IsCheckNext;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000450
Chris Lattnerf15380b2009-09-20 22:35:26 +0000451 /// NotStrings - These are all of the strings that are disallowed from
452 /// occurring between this match string and the previous one (or start of
453 /// file).
Chris Lattnera29703e2009-09-24 20:39:13 +0000454 std::vector<std::pair<SMLoc, Pattern> > NotStrings;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000455
Chris Lattner9fc66782009-09-24 20:25:55 +0000456 CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
457 : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
Chris Lattner207e1bc2009-08-15 17:41:04 +0000458};
459
Chris Lattneradea46e2009-09-24 20:45:07 +0000460/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
461/// memory buffer, free it, and return a new one.
462static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
Chris Lattner4c842dd2010-04-05 22:42:30 +0000463 SmallString<128> NewFile;
Chris Lattneradea46e2009-09-24 20:45:07 +0000464 NewFile.reserve(MB->getBufferSize());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000465
Chris Lattneradea46e2009-09-24 20:45:07 +0000466 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
467 Ptr != End; ++Ptr) {
NAKAMURA Takumi9f6e03f2010-11-14 03:28:22 +0000468 // Eliminate trailing dosish \r.
469 if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
470 continue;
471 }
472
Chris Lattneradea46e2009-09-24 20:45:07 +0000473 // If C is not a horizontal whitespace, skip it.
474 if (*Ptr != ' ' && *Ptr != '\t') {
475 NewFile.push_back(*Ptr);
476 continue;
477 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000478
Chris Lattneradea46e2009-09-24 20:45:07 +0000479 // Otherwise, add one space and advance over neighboring space.
480 NewFile.push_back(' ');
481 while (Ptr+1 != End &&
482 (Ptr[1] == ' ' || Ptr[1] == '\t'))
483 ++Ptr;
484 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000485
Chris Lattneradea46e2009-09-24 20:45:07 +0000486 // Free the old buffer and return a new one.
487 MemoryBuffer *MB2 =
Chris Lattner4c842dd2010-04-05 22:42:30 +0000488 MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000489
Chris Lattneradea46e2009-09-24 20:45:07 +0000490 delete MB;
491 return MB2;
492}
493
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000494
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000495/// ReadCheckFile - Read the check file, which specifies the sequence of
496/// expected strings. The strings are added to the CheckStrings vector.
497static bool ReadCheckFile(SourceMgr &SM,
Chris Lattner207e1bc2009-08-15 17:41:04 +0000498 std::vector<CheckString> &CheckStrings) {
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000499 // Open the check file, and tell SourceMgr about it.
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000500 OwningPtr<MemoryBuffer> File;
501 if (error_code ec =
502 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), File)) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000503 errs() << "Could not open check file '" << CheckFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +0000504 << ec.message() << '\n';
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000505 return true;
506 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000507 MemoryBuffer *F = File.take();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000508
Chris Lattneradea46e2009-09-24 20:45:07 +0000509 // If we want to canonicalize whitespace, strip excess whitespace from the
510 // buffer containing the CHECK lines.
511 if (!NoCanonicalizeWhiteSpace)
512 F = CanonicalizeInputFile(F);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000513
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000514 SM.AddNewSourceBuffer(F, SMLoc());
515
Chris Lattnerd7e25052009-08-15 18:00:42 +0000516 // Find all instances of CheckPrefix followed by : in the file.
Chris Lattner96077032009-09-20 22:11:44 +0000517 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000518
Chris Lattnera29703e2009-09-24 20:39:13 +0000519 std::vector<std::pair<SMLoc, Pattern> > NotMatches;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000520
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000521 while (1) {
522 // See if Prefix occurs in the memory buffer.
Chris Lattner96077032009-09-20 22:11:44 +0000523 Buffer = Buffer.substr(Buffer.find(CheckPrefix));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000524
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000525 // If we didn't find a match, we're done.
Chris Lattner96077032009-09-20 22:11:44 +0000526 if (Buffer.empty())
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000527 break;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000528
Chris Lattner96077032009-09-20 22:11:44 +0000529 const char *CheckPrefixStart = Buffer.data();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000530
Chris Lattner5dafafd2009-08-15 18:32:21 +0000531 // When we find a check prefix, keep track of whether we find CHECK: or
532 // CHECK-NEXT:
Chris Lattnerf15380b2009-09-20 22:35:26 +0000533 bool IsCheckNext = false, IsCheckNot = false;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000534
Chris Lattnerd7e25052009-08-15 18:00:42 +0000535 // Verify that the : is present after the prefix.
Chris Lattner96077032009-09-20 22:11:44 +0000536 if (Buffer[CheckPrefix.size()] == ':') {
537 Buffer = Buffer.substr(CheckPrefix.size()+1);
Chris Lattner96077032009-09-20 22:11:44 +0000538 } else if (Buffer.size() > CheckPrefix.size()+6 &&
539 memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
Benjamin Kramer30ce40e2012-09-18 20:51:39 +0000540 Buffer = Buffer.substr(CheckPrefix.size()+6);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000541 IsCheckNext = true;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000542 } else if (Buffer.size() > CheckPrefix.size()+5 &&
543 memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) {
Benjamin Kramer30ce40e2012-09-18 20:51:39 +0000544 Buffer = Buffer.substr(CheckPrefix.size()+5);
Chris Lattnerf15380b2009-09-20 22:35:26 +0000545 IsCheckNot = true;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000546 } else {
Chris Lattner96077032009-09-20 22:11:44 +0000547 Buffer = Buffer.substr(1);
Chris Lattnerd7e25052009-08-15 18:00:42 +0000548 continue;
549 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000550
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000551 // Okay, we found the prefix, yay. Remember the rest of the line, but
552 // ignore leading and trailing whitespace.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000553 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000554
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000555 // Scan ahead to the end of line.
Chris Lattner96077032009-09-20 22:11:44 +0000556 size_t EOL = Buffer.find_first_of("\n\r");
Chris Lattnera29703e2009-09-24 20:39:13 +0000557
Dan Gohmane5463432010-01-29 21:53:18 +0000558 // Remember the location of the start of the pattern, for diagnostics.
559 SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
560
Chris Lattnera29703e2009-09-24 20:39:13 +0000561 // Parse the pattern.
562 Pattern P;
563 if (P.ParsePattern(Buffer.substr(0, EOL), SM))
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000564 return true;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000565
Chris Lattnera29703e2009-09-24 20:39:13 +0000566 Buffer = Buffer.substr(EOL);
567
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000568
Chris Lattner5dafafd2009-08-15 18:32:21 +0000569 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
570 if (IsCheckNext && CheckStrings.empty()) {
571 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000572 SourceMgr::DK_Error,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000573 "found '"+CheckPrefix+"-NEXT:' without previous '"+
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000574 CheckPrefix+ ": line");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000575 return true;
576 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000577
Chris Lattnera29703e2009-09-24 20:39:13 +0000578 // Handle CHECK-NOT.
579 if (IsCheckNot) {
580 NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
581 P));
582 continue;
583 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000584
585
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000586 // Okay, add the string we captured to the output vector and move on.
Chris Lattner9fc66782009-09-24 20:25:55 +0000587 CheckStrings.push_back(CheckString(P,
Dan Gohmane5463432010-01-29 21:53:18 +0000588 PatternLoc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000589 IsCheckNext));
Chris Lattnerf15380b2009-09-20 22:35:26 +0000590 std::swap(NotMatches, CheckStrings.back().NotStrings);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000591 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000592
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000593 // Add an EOF pattern for any trailing CHECK-NOTs.
594 if (!NotMatches.empty()) {
595 CheckStrings.push_back(CheckString(Pattern(true),
596 SMLoc::getFromPointer(Buffer.data()),
597 false));
598 std::swap(NotMatches, CheckStrings.back().NotStrings);
599 }
600
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000601 if (CheckStrings.empty()) {
Chris Lattnerd7e25052009-08-15 18:00:42 +0000602 errs() << "error: no check strings found with prefix '" << CheckPrefix
603 << ":'\n";
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000604 return true;
605 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000606
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000607 return false;
608}
609
Chris Lattner5dafafd2009-08-15 18:32:21 +0000610static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000611 StringRef Buffer,
612 StringMap<StringRef> &VariableTable) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000613 // Otherwise, we have an error, emit an error message.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000614 SM.PrintMessage(CheckStr.Loc, SourceMgr::DK_Error,
615 "expected string not found in input");
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000616
Chris Lattner5dafafd2009-08-15 18:32:21 +0000617 // Print the "scanning from here" line. If the current position is at the
618 // end of a line, advance to the start of the next line.
Chris Lattner96077032009-09-20 22:11:44 +0000619 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000620
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000621 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
622 "scanning from here");
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000623
624 // Allow the pattern to print additional information if desired.
625 CheckStr.Pat.PrintFailureInfo(SM, Buffer, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000626}
627
Chris Lattner3711b7a2009-09-20 22:42:44 +0000628/// CountNumNewlinesBetween - Count the number of newlines in the specified
629/// range.
630static unsigned CountNumNewlinesBetween(StringRef Range) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000631 unsigned NumNewLines = 0;
Chris Lattner3711b7a2009-09-20 22:42:44 +0000632 while (1) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000633 // Scan for newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000634 Range = Range.substr(Range.find_first_of("\n\r"));
635 if (Range.empty()) return NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000636
Chris Lattner5dafafd2009-08-15 18:32:21 +0000637 ++NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000638
Chris Lattner5dafafd2009-08-15 18:32:21 +0000639 // Handle \n\r and \r\n as a single newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000640 if (Range.size() > 1 &&
641 (Range[1] == '\n' || Range[1] == '\r') &&
642 (Range[0] != Range[1]))
643 Range = Range.substr(1);
644 Range = Range.substr(1);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000645 }
Chris Lattner5dafafd2009-08-15 18:32:21 +0000646}
647
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000648int main(int argc, char **argv) {
649 sys::PrintStackTraceOnErrorSignal();
650 PrettyStackTraceProgram X(argc, argv);
651 cl::ParseCommandLineOptions(argc, argv);
652
653 SourceMgr SM;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000654
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000655 // Read the expected strings from the check file.
Chris Lattner207e1bc2009-08-15 17:41:04 +0000656 std::vector<CheckString> CheckStrings;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000657 if (ReadCheckFile(SM, CheckStrings))
658 return 2;
659
660 // Open the file to check and add it to SourceMgr.
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000661 OwningPtr<MemoryBuffer> File;
662 if (error_code ec =
663 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000664 errs() << "Could not open input file '" << InputFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +0000665 << ec.message() << '\n';
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000666 return true;
667 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000668 MemoryBuffer *F = File.take();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000669
Chris Lattner1aac1862011-02-09 16:46:02 +0000670 if (F->getBufferSize() == 0) {
671 errs() << "FileCheck error: '" << InputFilename << "' is empty.\n";
672 return 1;
673 }
674
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000675 // Remove duplicate spaces in the input file if requested.
676 if (!NoCanonicalizeWhiteSpace)
677 F = CanonicalizeInputFile(F);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000678
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000679 SM.AddNewSourceBuffer(F, SMLoc());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000680
Chris Lattnereec96952009-09-27 07:56:52 +0000681 /// VariableTable - This holds all the current filecheck variables.
682 StringMap<StringRef> VariableTable;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000683
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000684 // Check that we have all of the expected strings, in order, in the input
685 // file.
Chris Lattner96077032009-09-20 22:11:44 +0000686 StringRef Buffer = F->getBuffer();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000687
Chris Lattnerf15380b2009-09-20 22:35:26 +0000688 const char *LastMatch = Buffer.data();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000689
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000690 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
Chris Lattner207e1bc2009-08-15 17:41:04 +0000691 const CheckString &CheckStr = CheckStrings[StrNo];
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000692
Chris Lattner96077032009-09-20 22:11:44 +0000693 StringRef SearchFrom = Buffer;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000694
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000695 // Find StrNo in the file.
Chris Lattner9fc66782009-09-24 20:25:55 +0000696 size_t MatchLen = 0;
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000697 size_t MatchPos = CheckStr.Pat.Match(Buffer, MatchLen, VariableTable);
698 Buffer = Buffer.substr(MatchPos);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000699
Chris Lattner5dafafd2009-08-15 18:32:21 +0000700 // If we didn't find a match, reject the input.
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000701 if (MatchPos == StringRef::npos) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000702 PrintCheckFailed(SM, CheckStr, SearchFrom, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000703 return 1;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000704 }
Chris Lattner3711b7a2009-09-20 22:42:44 +0000705
706 StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
707
Chris Lattner5dafafd2009-08-15 18:32:21 +0000708 // If this check is a "CHECK-NEXT", verify that the previous match was on
709 // the previous line (i.e. that there is one newline between them).
710 if (CheckStr.IsCheckNext) {
711 // Count the number of newlines between the previous match and this one.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000712 assert(LastMatch != F->getBufferStart() &&
713 "CHECK-NEXT can't be the first check in a file");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000714
Chris Lattner3711b7a2009-09-20 22:42:44 +0000715 unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000716 if (NumNewLines == 0) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000717 SM.PrintMessage(CheckStr.Loc, SourceMgr::DK_Error,
718 CheckPrefix+"-NEXT: is on the same line as previous match");
Chris Lattner96077032009-09-20 22:11:44 +0000719 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000720 SourceMgr::DK_Note, "'next' match was here");
721 SM.PrintMessage(SMLoc::getFromPointer(LastMatch), SourceMgr::DK_Note,
722 "previous match was here");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000723 return 1;
724 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000725
Chris Lattner5dafafd2009-08-15 18:32:21 +0000726 if (NumNewLines != 1) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000727 SM.PrintMessage(CheckStr.Loc, SourceMgr::DK_Error, CheckPrefix+
728 "-NEXT: is not on the line after the previous match");
Chris Lattner96077032009-09-20 22:11:44 +0000729 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000730 SourceMgr::DK_Note, "'next' match was here");
731 SM.PrintMessage(SMLoc::getFromPointer(LastMatch), SourceMgr::DK_Note,
732 "previous match was here");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000733 return 1;
734 }
735 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000736
Chris Lattnerf15380b2009-09-20 22:35:26 +0000737 // If this match had "not strings", verify that they don't exist in the
738 // skipped region.
Chris Lattnereec96952009-09-27 07:56:52 +0000739 for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size();
740 ChunkNo != e; ++ChunkNo) {
Chris Lattnera29703e2009-09-24 20:39:13 +0000741 size_t MatchLen = 0;
Chris Lattnereec96952009-09-27 07:56:52 +0000742 size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion,
743 MatchLen,
744 VariableTable);
Chris Lattnerf15380b2009-09-20 22:35:26 +0000745 if (Pos == StringRef::npos) continue;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000746
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000747 SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos), SourceMgr::DK_Error,
748 CheckPrefix+"-NOT: string occurred!");
749 SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first, SourceMgr::DK_Note,
750 CheckPrefix+"-NOT: pattern specified here");
Chris Lattnerf15380b2009-09-20 22:35:26 +0000751 return 1;
752 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000753
Chris Lattner5dafafd2009-08-15 18:32:21 +0000754
Chris Lattner81115762009-09-21 02:30:42 +0000755 // Otherwise, everything is good. Step over the matched text and remember
756 // the position after the match as the end of the last match.
Chris Lattner9fc66782009-09-24 20:25:55 +0000757 Buffer = Buffer.substr(MatchLen);
Chris Lattner81115762009-09-21 02:30:42 +0000758 LastMatch = Buffer.data();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000759 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000760
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000761 return 0;
762}