blob: 77721f945158cb6784538fa7ac333950b4639a30 [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"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/ADT/StringMap.h"
Chris Lattner81cb8ca2009-07-08 18:44:05 +000023#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner52870082009-09-24 21:47:32 +000026#include "llvm/Support/Regex.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000027#include "llvm/Support/Signals.h"
Chris Lattner81cb8ca2009-07-08 18:44:05 +000028#include "llvm/Support/SourceMgr.h"
29#include "llvm/Support/raw_ostream.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000030#include "llvm/Support/system_error.h"
Chris Lattnereec96952009-09-27 07:56:52 +000031#include <algorithm>
Will Dietze3ba15c2013-10-12 00:55:57 +000032#include <cctype>
Eli Bendersky9756ca72012-12-01 21:54:48 +000033#include <map>
34#include <string>
35#include <vector>
Chris Lattner81cb8ca2009-07-08 18:44:05 +000036using namespace llvm;
37
38static cl::opt<std::string>
39CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
40
41static cl::opt<std::string>
42InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
43 cl::init("-"), cl::value_desc("filename"));
44
45static cl::opt<std::string>
46CheckPrefix("check-prefix", cl::init("CHECK"),
47 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
48
Chris Lattner88a7e9e2009-07-11 18:58:15 +000049static cl::opt<bool>
50NoCanonicalizeWhiteSpace("strict-whitespace",
51 cl::desc("Do not treat all horizontal whitespace as equivalent"));
52
Chris Lattnera29703e2009-09-24 20:39:13 +000053//===----------------------------------------------------------------------===//
54// Pattern Handling Code.
55//===----------------------------------------------------------------------===//
56
Matt Arsenault4f67afc2013-09-17 22:30:02 +000057namespace Check {
58 enum CheckType {
59 CheckNone = 0,
60 CheckPlain,
61 CheckNext,
62 CheckNot,
63 CheckDAG,
64 CheckLabel,
65
66 /// MatchEOF - When set, this pattern only matches the end of file. This is
67 /// used for trailing CHECK-NOTs.
68 CheckEOF
69 };
70}
71
Chris Lattner9fc66782009-09-24 20:25:55 +000072class Pattern {
Chris Lattner94638f02009-09-25 17:29:36 +000073 SMLoc PatternLoc;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000074
Matt Arsenault4f67afc2013-09-17 22:30:02 +000075 Check::CheckType CheckTy;
Michael Liao95ab3262013-05-14 20:34:12 +000076
Chris Lattner5d6a05f2009-09-25 17:23:43 +000077 /// FixedStr - If non-empty, this pattern is a fixed string match with the
78 /// specified fixed string.
Chris Lattner2702e6a2009-09-25 17:09:12 +000079 StringRef FixedStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000080
Chris Lattner5d6a05f2009-09-25 17:23:43 +000081 /// RegEx - If non-empty, this is a regex pattern.
82 std::string RegExStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000083
Alexander Kornienko70a870a2012-11-14 21:07:37 +000084 /// \brief Contains the number of line this pattern is in.
85 unsigned LineNumber;
86
Chris Lattnereec96952009-09-27 07:56:52 +000087 /// VariableUses - Entries in this vector map to uses of a variable in the
88 /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain
89 /// "foobaz" and we'll get an entry in this vector that tells us to insert the
90 /// value of bar at offset 3.
91 std::vector<std::pair<StringRef, unsigned> > VariableUses;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000092
Eli Bendersky9756ca72012-12-01 21:54:48 +000093 /// VariableDefs - Maps definitions of variables to their parenthesized
94 /// capture numbers.
95 /// E.g. for the pattern "foo[[bar:.*]]baz", VariableDefs will map "bar" to 1.
96 std::map<StringRef, unsigned> VariableDefs;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000097
Chris Lattner9fc66782009-09-24 20:25:55 +000098public:
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000099
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000100 Pattern(Check::CheckType Ty)
101 : CheckTy(Ty) { }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000102
Michael Liao0fc71372013-04-25 21:31:34 +0000103 /// getLoc - Return the location in source code.
104 SMLoc getLoc() const { return PatternLoc; }
105
Eli Bendersky1e5cbcb2012-11-30 14:22:14 +0000106 /// ParsePattern - Parse the given string into the Pattern. SM provides the
107 /// SourceMgr used for error reports, and LineNumber is the line number in
108 /// the input file from which the pattern string was read.
109 /// Returns true in case of an error, false otherwise.
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000110 bool ParsePattern(StringRef PatternStr, SourceMgr &SM, unsigned LineNumber);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000111
Chris Lattner9fc66782009-09-24 20:25:55 +0000112 /// Match - Match the pattern string against the input buffer Buffer. This
113 /// returns the position that is matched or npos if there is no match. If
114 /// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +0000115 ///
116 /// The VariableTable StringMap provides the current values of filecheck
117 /// variables and is updated if this match defines new values.
118 size_t Match(StringRef Buffer, size_t &MatchLen,
119 StringMap<StringRef> &VariableTable) const;
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000120
121 /// PrintFailureInfo - Print additional information about a failure to match
122 /// involving this pattern.
123 void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
124 const StringMap<StringRef> &VariableTable) const;
125
Stephen Lin178504b2013-07-12 14:51:05 +0000126 bool hasVariable() const { return !(VariableUses.empty() &&
127 VariableDefs.empty()); }
128
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000129 Check::CheckType getCheckTy() const { return CheckTy; }
Michael Liao95ab3262013-05-14 20:34:12 +0000130
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000131private:
Chris Lattnereec96952009-09-27 07:56:52 +0000132 static void AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr);
Eli Bendersky9756ca72012-12-01 21:54:48 +0000133 bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
134 void AddBackrefToRegEx(unsigned BackrefNum);
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000135
136 /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of
137 /// matching this pattern at the start of \arg Buffer; a distance of zero
138 /// should correspond to a perfect match.
139 unsigned ComputeMatchDistance(StringRef Buffer,
140 const StringMap<StringRef> &VariableTable) const;
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000141
142 /// \brief Evaluates expression and stores the result to \p Value.
143 /// \return true on success. false when the expression has invalid syntax.
144 bool EvaluateExpression(StringRef Expr, std::string &Value) const;
Eli Bendersky4db65112012-12-02 16:02:41 +0000145
146 /// \brief Finds the closing sequence of a regex variable usage or
147 /// definition. Str has to point in the beginning of the definition
148 /// (right after the opening sequence).
149 /// \return offset of the closing sequence within Str, or npos if it was not
150 /// found.
151 size_t FindRegexVarEnd(StringRef Str);
Chris Lattner9fc66782009-09-24 20:25:55 +0000152};
153
Chris Lattnereec96952009-09-27 07:56:52 +0000154
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000155bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM,
156 unsigned LineNumber) {
157 this->LineNumber = LineNumber;
Chris Lattner94638f02009-09-25 17:29:36 +0000158 PatternLoc = SMLoc::getFromPointer(PatternStr.data());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000159
Chris Lattnera29703e2009-09-24 20:39:13 +0000160 // Ignore trailing whitespace.
161 while (!PatternStr.empty() &&
162 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
163 PatternStr = PatternStr.substr(0, PatternStr.size()-1);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000164
Chris Lattnera29703e2009-09-24 20:39:13 +0000165 // Check that there is something on the line.
166 if (PatternStr.empty()) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000167 SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
168 "found empty check string with prefix '" +
169 CheckPrefix+":'");
Chris Lattnera29703e2009-09-24 20:39:13 +0000170 return true;
171 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000172
Chris Lattner2702e6a2009-09-25 17:09:12 +0000173 // Check to see if this is a fixed string, or if it has regex pieces.
Ted Kremenek4f505172012-09-08 04:32:13 +0000174 if (PatternStr.size() < 2 ||
Chris Lattnereec96952009-09-27 07:56:52 +0000175 (PatternStr.find("{{") == StringRef::npos &&
176 PatternStr.find("[[") == StringRef::npos)) {
Chris Lattner2702e6a2009-09-25 17:09:12 +0000177 FixedStr = PatternStr;
178 return false;
179 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000180
Chris Lattnereec96952009-09-27 07:56:52 +0000181 // Paren value #0 is for the fully matched string. Any new parenthesized
Chris Lattner13a38c42011-04-09 06:18:02 +0000182 // values add from there.
Chris Lattnereec96952009-09-27 07:56:52 +0000183 unsigned CurParen = 1;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000184
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000185 // Otherwise, there is at least one regex piece. Build up the regex pattern
186 // by escaping scary characters in fixed strings, building up one big regex.
Chris Lattner52870082009-09-24 21:47:32 +0000187 while (!PatternStr.empty()) {
Chris Lattnereec96952009-09-27 07:56:52 +0000188 // RegEx matches.
Chris Lattner13a38c42011-04-09 06:18:02 +0000189 if (PatternStr.startswith("{{")) {
Eli Bendersky1e5cbcb2012-11-30 14:22:14 +0000190 // This is the start of a regex match. Scan for the }}.
Chris Lattnereec96952009-09-27 07:56:52 +0000191 size_t End = PatternStr.find("}}");
192 if (End == StringRef::npos) {
193 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000194 SourceMgr::DK_Error,
195 "found start of regex string with no end '}}'");
Chris Lattnereec96952009-09-27 07:56:52 +0000196 return true;
197 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000198
Chris Lattner42e31df2011-04-09 06:37:03 +0000199 // Enclose {{}} patterns in parens just like [[]] even though we're not
200 // capturing the result for any purpose. This is required in case the
201 // expression contains an alternation like: CHECK: abc{{x|z}}def. We
202 // want this to turn into: "abc(x|z)def" not "abcx|zdef".
203 RegExStr += '(';
204 ++CurParen;
205
Chris Lattnereec96952009-09-27 07:56:52 +0000206 if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
207 return true;
Chris Lattner42e31df2011-04-09 06:37:03 +0000208 RegExStr += ')';
Chris Lattner13a38c42011-04-09 06:18:02 +0000209
Chris Lattnereec96952009-09-27 07:56:52 +0000210 PatternStr = PatternStr.substr(End+2);
Chris Lattner52870082009-09-24 21:47:32 +0000211 continue;
212 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000213
Chris Lattnereec96952009-09-27 07:56:52 +0000214 // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
215 // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
216 // second form is [[foo]] which is a reference to foo. The variable name
Daniel Dunbar964ac012009-11-22 22:07:50 +0000217 // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
Chris Lattnereec96952009-09-27 07:56:52 +0000218 // it. This is to catch some common errors.
Chris Lattner13a38c42011-04-09 06:18:02 +0000219 if (PatternStr.startswith("[[")) {
Eli Bendersky4db65112012-12-02 16:02:41 +0000220 // Find the closing bracket pair ending the match. End is going to be an
221 // offset relative to the beginning of the match string.
222 size_t End = FindRegexVarEnd(PatternStr.substr(2));
223
Chris Lattnereec96952009-09-27 07:56:52 +0000224 if (End == StringRef::npos) {
225 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000226 SourceMgr::DK_Error,
227 "invalid named regex reference, no ]] found");
Chris Lattnereec96952009-09-27 07:56:52 +0000228 return true;
229 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000230
Eli Bendersky4db65112012-12-02 16:02:41 +0000231 StringRef MatchStr = PatternStr.substr(2, End);
232 PatternStr = PatternStr.substr(End+4);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000233
Chris Lattnereec96952009-09-27 07:56:52 +0000234 // Get the regex name (e.g. "foo").
235 size_t NameEnd = MatchStr.find(':');
236 StringRef Name = MatchStr.substr(0, NameEnd);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000237
Chris Lattnereec96952009-09-27 07:56:52 +0000238 if (Name.empty()) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000239 SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
240 "invalid name in named regex: empty name");
Chris Lattnereec96952009-09-27 07:56:52 +0000241 return true;
242 }
243
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000244 // Verify that the name/expression is well formed. FileCheck currently
245 // supports @LINE, @LINE+number, @LINE-number expressions. The check here
246 // is relaxed, more strict check is performed in \c EvaluateExpression.
247 bool IsExpression = false;
248 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
249 if (i == 0 && Name[i] == '@') {
250 if (NameEnd != StringRef::npos) {
251 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
252 SourceMgr::DK_Error,
253 "invalid name in named regex definition");
254 return true;
255 }
256 IsExpression = true;
257 continue;
258 }
259 if (Name[i] != '_' && !isalnum(Name[i]) &&
260 (!IsExpression || (Name[i] != '+' && Name[i] != '-'))) {
Chris Lattnereec96952009-09-27 07:56:52 +0000261 SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000262 SourceMgr::DK_Error, "invalid name in named regex");
Chris Lattnereec96952009-09-27 07:56:52 +0000263 return true;
264 }
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000265 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000266
Chris Lattnereec96952009-09-27 07:56:52 +0000267 // Name can't start with a digit.
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000268 if (isdigit(static_cast<unsigned char>(Name[0]))) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000269 SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
270 "invalid name in named regex");
Chris Lattnereec96952009-09-27 07:56:52 +0000271 return true;
272 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000273
Chris Lattnereec96952009-09-27 07:56:52 +0000274 // Handle [[foo]].
275 if (NameEnd == StringRef::npos) {
Eli Bendersky9756ca72012-12-01 21:54:48 +0000276 // Handle variables that were defined earlier on the same line by
277 // emitting a backreference.
278 if (VariableDefs.find(Name) != VariableDefs.end()) {
279 unsigned VarParenNum = VariableDefs[Name];
280 if (VarParenNum < 1 || VarParenNum > 9) {
281 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
282 SourceMgr::DK_Error,
283 "Can't back-reference more than 9 variables");
284 return true;
285 }
286 AddBackrefToRegEx(VarParenNum);
287 } else {
288 VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
289 }
Chris Lattnereec96952009-09-27 07:56:52 +0000290 continue;
291 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000292
Chris Lattnereec96952009-09-27 07:56:52 +0000293 // Handle [[foo:.*]].
Eli Bendersky9756ca72012-12-01 21:54:48 +0000294 VariableDefs[Name] = CurParen;
Chris Lattnereec96952009-09-27 07:56:52 +0000295 RegExStr += '(';
296 ++CurParen;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000297
Chris Lattnereec96952009-09-27 07:56:52 +0000298 if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
299 return true;
300
301 RegExStr += ')';
Chris Lattner52870082009-09-24 21:47:32 +0000302 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000303
Chris Lattnereec96952009-09-27 07:56:52 +0000304 // Handle fixed string matches.
305 // Find the end, which is the start of the next regex.
306 size_t FixedMatchEnd = PatternStr.find("{{");
307 FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
308 AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr);
309 PatternStr = PatternStr.substr(FixedMatchEnd);
Chris Lattner52870082009-09-24 21:47:32 +0000310 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000311
Chris Lattnera29703e2009-09-24 20:39:13 +0000312 return false;
313}
314
Chris Lattnereec96952009-09-27 07:56:52 +0000315void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) {
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000316 // Add the characters from FixedStr to the regex, escaping as needed. This
317 // avoids "leaning toothpicks" in common patterns.
318 for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) {
319 switch (FixedStr[i]) {
320 // These are the special characters matched in "p_ere_exp".
321 case '(':
322 case ')':
323 case '^':
324 case '$':
325 case '|':
326 case '*':
327 case '+':
328 case '?':
329 case '.':
330 case '[':
331 case '\\':
332 case '{':
Chris Lattnereec96952009-09-27 07:56:52 +0000333 TheStr += '\\';
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000334 // FALL THROUGH.
335 default:
Chris Lattnereec96952009-09-27 07:56:52 +0000336 TheStr += FixedStr[i];
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000337 break;
338 }
339 }
340}
341
Eli Bendersky9756ca72012-12-01 21:54:48 +0000342bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen,
Chris Lattnereec96952009-09-27 07:56:52 +0000343 SourceMgr &SM) {
Eli Bendersky9756ca72012-12-01 21:54:48 +0000344 Regex R(RS);
Chris Lattnereec96952009-09-27 07:56:52 +0000345 std::string Error;
346 if (!R.isValid(Error)) {
Eli Bendersky9756ca72012-12-01 21:54:48 +0000347 SM.PrintMessage(SMLoc::getFromPointer(RS.data()), SourceMgr::DK_Error,
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000348 "invalid regex: " + Error);
Chris Lattnereec96952009-09-27 07:56:52 +0000349 return true;
350 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000351
Eli Bendersky9756ca72012-12-01 21:54:48 +0000352 RegExStr += RS.str();
Chris Lattnereec96952009-09-27 07:56:52 +0000353 CurParen += R.getNumMatches();
354 return false;
355}
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000356
Eli Bendersky9756ca72012-12-01 21:54:48 +0000357void Pattern::AddBackrefToRegEx(unsigned BackrefNum) {
358 assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number");
359 std::string Backref = std::string("\\") +
360 std::string(1, '0' + BackrefNum);
361 RegExStr += Backref;
362}
363
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000364bool Pattern::EvaluateExpression(StringRef Expr, std::string &Value) const {
365 // The only supported expression is @LINE([\+-]\d+)?
366 if (!Expr.startswith("@LINE"))
367 return false;
368 Expr = Expr.substr(StringRef("@LINE").size());
369 int Offset = 0;
370 if (!Expr.empty()) {
371 if (Expr[0] == '+')
372 Expr = Expr.substr(1);
373 else if (Expr[0] != '-')
374 return false;
375 if (Expr.getAsInteger(10, Offset))
376 return false;
377 }
378 Value = llvm::itostr(LineNumber + Offset);
379 return true;
380}
381
Chris Lattner52870082009-09-24 21:47:32 +0000382/// Match - Match the pattern string against the input buffer Buffer. This
383/// returns the position that is matched or npos if there is no match. If
384/// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +0000385size_t Pattern::Match(StringRef Buffer, size_t &MatchLen,
386 StringMap<StringRef> &VariableTable) const {
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000387 // If this is the EOF pattern, match it immediately.
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000388 if (CheckTy == Check::CheckEOF) {
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000389 MatchLen = 0;
390 return Buffer.size();
391 }
392
Chris Lattner2702e6a2009-09-25 17:09:12 +0000393 // If this is a fixed string pattern, just match it now.
394 if (!FixedStr.empty()) {
395 MatchLen = FixedStr.size();
396 return Buffer.find(FixedStr);
397 }
Chris Lattnereec96952009-09-27 07:56:52 +0000398
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000399 // Regex match.
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000400
Chris Lattnereec96952009-09-27 07:56:52 +0000401 // If there are variable uses, we need to create a temporary string with the
402 // actual value.
403 StringRef RegExToMatch = RegExStr;
404 std::string TmpStr;
405 if (!VariableUses.empty()) {
406 TmpStr = RegExStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000407
Chris Lattnereec96952009-09-27 07:56:52 +0000408 unsigned InsertOffset = 0;
409 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
Chris Lattnereec96952009-09-27 07:56:52 +0000410 std::string Value;
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000411
412 if (VariableUses[i].first[0] == '@') {
413 if (!EvaluateExpression(VariableUses[i].first, Value))
414 return StringRef::npos;
415 } else {
416 StringMap<StringRef>::iterator it =
417 VariableTable.find(VariableUses[i].first);
418 // If the variable is undefined, return an error.
419 if (it == VariableTable.end())
420 return StringRef::npos;
421
422 // Look up the value and escape it so that we can plop it into the regex.
423 AddFixedStringToRegEx(it->second, Value);
424 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000425
Chris Lattnereec96952009-09-27 07:56:52 +0000426 // Plop it into the regex at the adjusted offset.
427 TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset,
428 Value.begin(), Value.end());
429 InsertOffset += Value.size();
430 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000431
Chris Lattnereec96952009-09-27 07:56:52 +0000432 // Match the newly constructed regex.
433 RegExToMatch = TmpStr;
434 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000435
436
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000437 SmallVector<StringRef, 4> MatchInfo;
Chris Lattnereec96952009-09-27 07:56:52 +0000438 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000439 return StringRef::npos;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000440
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000441 // Successful regex match.
442 assert(!MatchInfo.empty() && "Didn't get any match");
443 StringRef FullMatch = MatchInfo[0];
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000444
Chris Lattnereec96952009-09-27 07:56:52 +0000445 // If this defines any variables, remember their values.
Eli Bendersky9756ca72012-12-01 21:54:48 +0000446 for (std::map<StringRef, unsigned>::const_iterator I = VariableDefs.begin(),
447 E = VariableDefs.end();
448 I != E; ++I) {
449 assert(I->second < MatchInfo.size() && "Internal paren error");
450 VariableTable[I->first] = MatchInfo[I->second];
Chris Lattner94638f02009-09-25 17:29:36 +0000451 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000452
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000453 MatchLen = FullMatch.size();
454 return FullMatch.data()-Buffer.data();
Chris Lattner52870082009-09-24 21:47:32 +0000455}
456
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000457unsigned Pattern::ComputeMatchDistance(StringRef Buffer,
458 const StringMap<StringRef> &VariableTable) const {
459 // Just compute the number of matching characters. For regular expressions, we
460 // just compare against the regex itself and hope for the best.
461 //
462 // FIXME: One easy improvement here is have the regex lib generate a single
463 // example regular expression which matches, and use that as the example
464 // string.
465 StringRef ExampleString(FixedStr);
466 if (ExampleString.empty())
467 ExampleString = RegExStr;
468
Daniel Dunbar0806f9f2010-01-30 00:24:06 +0000469 // Only compare up to the first line in the buffer, or the string size.
470 StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
471 BufferPrefix = BufferPrefix.split('\n').first;
472 return BufferPrefix.edit_distance(ExampleString);
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000473}
474
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000475void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
476 const StringMap<StringRef> &VariableTable) const{
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000477 // If this was a regular expression using variables, print the current
478 // variable values.
479 if (!VariableUses.empty()) {
480 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000481 SmallString<256> Msg;
482 raw_svector_ostream OS(Msg);
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000483 StringRef Var = VariableUses[i].first;
484 if (Var[0] == '@') {
485 std::string Value;
486 if (EvaluateExpression(Var, Value)) {
487 OS << "with expression \"";
488 OS.write_escaped(Var) << "\" equal to \"";
489 OS.write_escaped(Value) << "\"";
490 } else {
491 OS << "uses incorrect expression \"";
492 OS.write_escaped(Var) << "\"";
493 }
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000494 } else {
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000495 StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
496
497 // Check for undefined variable references.
498 if (it == VariableTable.end()) {
499 OS << "uses undefined variable \"";
500 OS.write_escaped(Var) << "\"";
501 } else {
502 OS << "with variable \"";
503 OS.write_escaped(Var) << "\" equal to \"";
504 OS.write_escaped(it->second) << "\"";
505 }
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000506 }
507
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000508 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
509 OS.str());
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000510 }
511 }
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000512
513 // Attempt to find the closest/best fuzzy match. Usually an error happens
514 // because some string in the output didn't exactly match. In these cases, we
515 // would like to show the user a best guess at what "should have" matched, to
516 // save them having to actually check the input manually.
517 size_t NumLinesForward = 0;
518 size_t Best = StringRef::npos;
519 double BestQuality = 0;
520
521 // Use an arbitrary 4k limit on how far we will search.
Dan Gohmane3a1e502010-01-29 21:57:46 +0000522 for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000523 if (Buffer[i] == '\n')
524 ++NumLinesForward;
525
Dan Gohmand8a55412010-01-29 21:55:16 +0000526 // Patterns have leading whitespace stripped, so skip whitespace when
527 // looking for something which looks like a pattern.
528 if (Buffer[i] == ' ' || Buffer[i] == '\t')
529 continue;
530
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000531 // Compute the "quality" of this match as an arbitrary combination of the
532 // match distance and the number of lines skipped to get to this match.
533 unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable);
534 double Quality = Distance + (NumLinesForward / 100.);
535
536 if (Quality < BestQuality || Best == StringRef::npos) {
537 Best = i;
538 BestQuality = Quality;
539 }
540 }
541
Daniel Dunbar7a68e0d2010-03-19 18:07:43 +0000542 // Print the "possible intended match here" line if we found something
543 // reasonable and not equal to what we showed in the "scanning from here"
544 // line.
545 if (Best && Best != StringRef::npos && BestQuality < 50) {
546 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000547 SourceMgr::DK_Note, "possible intended match here");
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000548
549 // FIXME: If we wanted to be really friendly we would show why the match
550 // failed, as it can be hard to spot simple one character differences.
551 }
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000552}
Chris Lattnera29703e2009-09-24 20:39:13 +0000553
Eli Bendersky4db65112012-12-02 16:02:41 +0000554size_t Pattern::FindRegexVarEnd(StringRef Str) {
555 // Offset keeps track of the current offset within the input Str
556 size_t Offset = 0;
557 // [...] Nesting depth
558 size_t BracketDepth = 0;
559
560 while (!Str.empty()) {
561 if (Str.startswith("]]") && BracketDepth == 0)
562 return Offset;
563 if (Str[0] == '\\') {
564 // Backslash escapes the next char within regexes, so skip them both.
565 Str = Str.substr(2);
566 Offset += 2;
567 } else {
568 switch (Str[0]) {
569 default:
570 break;
571 case '[':
572 BracketDepth++;
573 break;
574 case ']':
575 assert(BracketDepth > 0 && "Invalid regex");
576 BracketDepth--;
577 break;
578 }
579 Str = Str.substr(1);
580 Offset++;
581 }
582 }
583
584 return StringRef::npos;
585}
586
587
Chris Lattnera29703e2009-09-24 20:39:13 +0000588//===----------------------------------------------------------------------===//
589// Check Strings.
590//===----------------------------------------------------------------------===//
Chris Lattner9fc66782009-09-24 20:25:55 +0000591
592/// CheckString - This is a check that we found in the input file.
593struct CheckString {
594 /// Pat - The pattern to match.
595 Pattern Pat;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000596
Chris Lattner207e1bc2009-08-15 17:41:04 +0000597 /// Loc - The location in the match file that the check string was specified.
598 SMLoc Loc;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000599
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000600 /// CheckTy - Specify what kind of check this is. e.g. CHECK-NEXT: directive,
601 /// as opposed to a CHECK: directive.
602 Check::CheckType CheckTy;
Stephen Lin178504b2013-07-12 14:51:05 +0000603
Michael Liao95ab3262013-05-14 20:34:12 +0000604 /// DagNotStrings - These are all of the strings that are disallowed from
Chris Lattnerf15380b2009-09-20 22:35:26 +0000605 /// occurring between this match string and the previous one (or start of
606 /// file).
Michael Liao95ab3262013-05-14 20:34:12 +0000607 std::vector<Pattern> DagNotStrings;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000608
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000609 CheckString(const Pattern &P, SMLoc L, Check::CheckType Ty)
610 : Pat(P), Loc(L), CheckTy(Ty) {}
Michael Liao7efbbd62013-05-14 20:29:52 +0000611
Michael Liao95ab3262013-05-14 20:34:12 +0000612 /// Check - Match check string and its "not strings" and/or "dag strings".
Stephen Line5f740c2013-10-11 18:38:36 +0000613 size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
Stephen Lin178504b2013-07-12 14:51:05 +0000614 size_t &MatchLen, StringMap<StringRef> &VariableTable) const;
Michael Liao7efbbd62013-05-14 20:29:52 +0000615
616 /// CheckNext - Verify there is a single line in the given buffer.
617 bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
618
619 /// CheckNot - Verify there's no "not strings" in the given buffer.
620 bool CheckNot(const SourceMgr &SM, StringRef Buffer,
Michael Liao95ab3262013-05-14 20:34:12 +0000621 const std::vector<const Pattern *> &NotStrings,
Michael Liao7efbbd62013-05-14 20:29:52 +0000622 StringMap<StringRef> &VariableTable) const;
Michael Liao95ab3262013-05-14 20:34:12 +0000623
624 /// CheckDag - Match "dag strings" and their mixed "not strings".
625 size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
626 std::vector<const Pattern *> &NotStrings,
627 StringMap<StringRef> &VariableTable) const;
Chris Lattner207e1bc2009-08-15 17:41:04 +0000628};
629
Guy Benyei4cc74fc2013-02-06 20:40:38 +0000630/// Canonicalize whitespaces in the input file. Line endings are replaced
631/// with UNIX-style '\n'.
632///
633/// \param PreserveHorizontal Don't squash consecutive horizontal whitespace
634/// characters to a single space.
635static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB,
636 bool PreserveHorizontal) {
Chris Lattner4c842dd2010-04-05 22:42:30 +0000637 SmallString<128> NewFile;
Chris Lattneradea46e2009-09-24 20:45:07 +0000638 NewFile.reserve(MB->getBufferSize());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000639
Chris Lattneradea46e2009-09-24 20:45:07 +0000640 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
641 Ptr != End; ++Ptr) {
NAKAMURA Takumi9f6e03f2010-11-14 03:28:22 +0000642 // Eliminate trailing dosish \r.
643 if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
644 continue;
645 }
646
Michael Liaoc16f8c52013-04-25 18:54:02 +0000647 // If current char is not a horizontal whitespace or if horizontal
Guy Benyei4cc74fc2013-02-06 20:40:38 +0000648 // whitespace canonicalization is disabled, dump it to output as is.
649 if (PreserveHorizontal || (*Ptr != ' ' && *Ptr != '\t')) {
Chris Lattneradea46e2009-09-24 20:45:07 +0000650 NewFile.push_back(*Ptr);
651 continue;
652 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000653
Chris Lattneradea46e2009-09-24 20:45:07 +0000654 // Otherwise, add one space and advance over neighboring space.
655 NewFile.push_back(' ');
656 while (Ptr+1 != End &&
657 (Ptr[1] == ' ' || Ptr[1] == '\t'))
658 ++Ptr;
659 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000660
Chris Lattneradea46e2009-09-24 20:45:07 +0000661 // Free the old buffer and return a new one.
662 MemoryBuffer *MB2 =
Chris Lattner4c842dd2010-04-05 22:42:30 +0000663 MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000664
Chris Lattneradea46e2009-09-24 20:45:07 +0000665 delete MB;
666 return MB2;
667}
668
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000669static bool IsPartOfWord(char c) {
670 return (isalnum(c) || c == '-' || c == '_');
671}
672
673static Check::CheckType FindCheckType(StringRef &Buffer, StringRef Prefix) {
Matt Arsenault53bb26f2013-09-17 22:45:57 +0000674 char NextChar = Buffer[Prefix.size()];
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000675
676 // Verify that the : is present after the prefix.
677 if (NextChar == ':') {
Matt Arsenault53bb26f2013-09-17 22:45:57 +0000678 Buffer = Buffer.substr(Prefix.size() + 1);
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000679 return Check::CheckPlain;
680 }
681
682 if (NextChar != '-') {
683 Buffer = Buffer.drop_front(1);
684 return Check::CheckNone;
685 }
686
Matt Arsenault53bb26f2013-09-17 22:45:57 +0000687 StringRef Rest = Buffer.drop_front(Prefix.size() + 1);
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000688 if (Rest.startswith("NEXT:")) {
689 Buffer = Rest.drop_front(sizeof("NEXT:") - 1);
690 return Check::CheckNext;
691 }
692
693 if (Rest.startswith("NOT:")) {
694 Buffer = Rest.drop_front(sizeof("NOT:") - 1);
695 return Check::CheckNot;
696 }
697
698 if (Rest.startswith("DAG:")) {
699 Buffer = Rest.drop_front(sizeof("DAG:") - 1);
700 return Check::CheckDAG;
701 }
702
703 if (Rest.startswith("LABEL:")) {
704 Buffer = Rest.drop_front(sizeof("LABEL:") - 1);
705 return Check::CheckLabel;
706 }
707
708 Buffer = Buffer.drop_front(1);
709 return Check::CheckNone;
710}
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000711
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000712/// ReadCheckFile - Read the check file, which specifies the sequence of
713/// expected strings. The strings are added to the CheckStrings vector.
Eli Bendersky1e5cbcb2012-11-30 14:22:14 +0000714/// Returns true in case of an error, false otherwise.
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000715static bool ReadCheckFile(SourceMgr &SM,
Chris Lattner207e1bc2009-08-15 17:41:04 +0000716 std::vector<CheckString> &CheckStrings) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000717 OwningPtr<MemoryBuffer> File;
718 if (error_code ec =
Rafael Espindoladd5af272013-06-25 05:28:34 +0000719 MemoryBuffer::getFileOrSTDIN(CheckFilename, File)) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000720 errs() << "Could not open check file '" << CheckFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +0000721 << ec.message() << '\n';
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000722 return true;
723 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000724
Chris Lattneradea46e2009-09-24 20:45:07 +0000725 // If we want to canonicalize whitespace, strip excess whitespace from the
Guy Benyei4cc74fc2013-02-06 20:40:38 +0000726 // buffer containing the CHECK lines. Remove DOS style line endings.
Benjamin Kramer7cdba152013-03-23 13:56:23 +0000727 MemoryBuffer *F =
728 CanonicalizeInputFile(File.take(), NoCanonicalizeWhiteSpace);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000729
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000730 SM.AddNewSourceBuffer(F, SMLoc());
731
Chris Lattnerd7e25052009-08-15 18:00:42 +0000732 // Find all instances of CheckPrefix followed by : in the file.
Chris Lattner96077032009-09-20 22:11:44 +0000733 StringRef Buffer = F->getBuffer();
Michael Liao95ab3262013-05-14 20:34:12 +0000734 std::vector<Pattern> DagNotMatches;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000735
Eli Bendersky1e5cbcb2012-11-30 14:22:14 +0000736 // LineNumber keeps track of the line on which CheckPrefix instances are
737 // found.
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000738 unsigned LineNumber = 1;
739
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000740 while (1) {
741 // See if Prefix occurs in the memory buffer.
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000742 size_t PrefixLoc = Buffer.find(CheckPrefix);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000743 // If we didn't find a match, we're done.
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000744 if (PrefixLoc == StringRef::npos)
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000745 break;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000746
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000747 LineNumber += Buffer.substr(0, PrefixLoc).count('\n');
748
Rui Ueyamad9a84ef2013-08-12 23:05:59 +0000749 // Keep the charcter before our prefix so we can validate that we have
750 // found our prefix, and account for cases when PrefixLoc is 0.
751 Buffer = Buffer.substr(std::min(PrefixLoc-1, PrefixLoc));
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000752
Rui Ueyamad9a84ef2013-08-12 23:05:59 +0000753 const char *CheckPrefixStart = Buffer.data() + (PrefixLoc == 0 ? 0 : 1);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000754
Rui Ueyamad9a84ef2013-08-12 23:05:59 +0000755 // Make sure we have actually found our prefix, and not a word containing
756 // our prefix.
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000757 if (PrefixLoc != 0 && IsPartOfWord(Buffer[0])) {
Rui Ueyamad9a84ef2013-08-12 23:05:59 +0000758 Buffer = Buffer.substr(CheckPrefix.size());
759 continue;
760 }
761
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000762 // When we find a check prefix, keep track of what kind of type of CHECK we
763 // have.
764 Check::CheckType CheckTy = FindCheckType(Buffer, CheckPrefix);
765 if (CheckTy == Check::CheckNone)
Chris Lattnerd7e25052009-08-15 18:00:42 +0000766 continue;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000767
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000768 // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
769 // leading and trailing whitespace.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000770 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000771
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000772 // Scan ahead to the end of line.
Chris Lattner96077032009-09-20 22:11:44 +0000773 size_t EOL = Buffer.find_first_of("\n\r");
Chris Lattnera29703e2009-09-24 20:39:13 +0000774
Dan Gohmane5463432010-01-29 21:53:18 +0000775 // Remember the location of the start of the pattern, for diagnostics.
776 SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
777
Chris Lattnera29703e2009-09-24 20:39:13 +0000778 // Parse the pattern.
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000779 Pattern P(CheckTy);
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000780 if (P.ParsePattern(Buffer.substr(0, EOL), SM, LineNumber))
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000781 return true;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000782
Stephen Lin178504b2013-07-12 14:51:05 +0000783 // Verify that CHECK-LABEL lines do not define or use variables
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000784 if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
Stephen Lin178504b2013-07-12 14:51:05 +0000785 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
786 SourceMgr::DK_Error,
787 "found '"+CheckPrefix+"-LABEL:' with variable definition"
Stephen Lin6d3aa542013-08-16 17:29:01 +0000788 " or use");
Stephen Lin178504b2013-07-12 14:51:05 +0000789 return true;
790 }
791
Chris Lattnera29703e2009-09-24 20:39:13 +0000792 Buffer = Buffer.substr(EOL);
793
Chris Lattner5dafafd2009-08-15 18:32:21 +0000794 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000795 if ((CheckTy == Check::CheckNext) && CheckStrings.empty()) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000796 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000797 SourceMgr::DK_Error,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000798 "found '"+CheckPrefix+"-NEXT:' without previous '"+
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000799 CheckPrefix+ ": line");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000800 return true;
801 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000802
Michael Liao95ab3262013-05-14 20:34:12 +0000803 // Handle CHECK-DAG/-NOT.
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000804 if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
Michael Liao95ab3262013-05-14 20:34:12 +0000805 DagNotMatches.push_back(P);
Chris Lattnera29703e2009-09-24 20:39:13 +0000806 continue;
807 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000808
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000809 // Okay, add the string we captured to the output vector and move on.
Chris Lattner9fc66782009-09-24 20:25:55 +0000810 CheckStrings.push_back(CheckString(P,
Dan Gohmane5463432010-01-29 21:53:18 +0000811 PatternLoc,
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000812 CheckTy));
Michael Liao95ab3262013-05-14 20:34:12 +0000813 std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000814 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000815
Michael Liao95ab3262013-05-14 20:34:12 +0000816 // Add an EOF pattern for any trailing CHECK-DAG/-NOTs.
817 if (!DagNotMatches.empty()) {
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000818 CheckStrings.push_back(CheckString(Pattern(Check::CheckEOF),
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000819 SMLoc::getFromPointer(Buffer.data()),
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000820 Check::CheckEOF));
Michael Liao95ab3262013-05-14 20:34:12 +0000821 std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000822 }
823
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000824 if (CheckStrings.empty()) {
Chris Lattnerd7e25052009-08-15 18:00:42 +0000825 errs() << "error: no check strings found with prefix '" << CheckPrefix
826 << ":'\n";
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000827 return true;
828 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000829
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000830 return false;
831}
832
Michael Liao95ab3262013-05-14 20:34:12 +0000833static void PrintCheckFailed(const SourceMgr &SM, const SMLoc &Loc,
834 const Pattern &Pat, StringRef Buffer,
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000835 StringMap<StringRef> &VariableTable) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000836 // Otherwise, we have an error, emit an error message.
Michael Liao95ab3262013-05-14 20:34:12 +0000837 SM.PrintMessage(Loc, SourceMgr::DK_Error,
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000838 "expected string not found in input");
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000839
Chris Lattner5dafafd2009-08-15 18:32:21 +0000840 // Print the "scanning from here" line. If the current position is at the
841 // end of a line, advance to the start of the next line.
Chris Lattner96077032009-09-20 22:11:44 +0000842 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000843
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000844 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
845 "scanning from here");
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000846
847 // Allow the pattern to print additional information if desired.
Michael Liao95ab3262013-05-14 20:34:12 +0000848 Pat.PrintFailureInfo(SM, Buffer, VariableTable);
849}
850
851static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
852 StringRef Buffer,
853 StringMap<StringRef> &VariableTable) {
854 PrintCheckFailed(SM, CheckStr.Loc, CheckStr.Pat, Buffer, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000855}
856
Chris Lattner3711b7a2009-09-20 22:42:44 +0000857/// CountNumNewlinesBetween - Count the number of newlines in the specified
858/// range.
859static unsigned CountNumNewlinesBetween(StringRef Range) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000860 unsigned NumNewLines = 0;
Chris Lattner3711b7a2009-09-20 22:42:44 +0000861 while (1) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000862 // Scan for newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000863 Range = Range.substr(Range.find_first_of("\n\r"));
864 if (Range.empty()) return NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000865
Chris Lattner5dafafd2009-08-15 18:32:21 +0000866 ++NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000867
Chris Lattner5dafafd2009-08-15 18:32:21 +0000868 // Handle \n\r and \r\n as a single newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000869 if (Range.size() > 1 &&
870 (Range[1] == '\n' || Range[1] == '\r') &&
871 (Range[0] != Range[1]))
872 Range = Range.substr(1);
873 Range = Range.substr(1);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000874 }
Chris Lattner5dafafd2009-08-15 18:32:21 +0000875}
876
Michael Liao7efbbd62013-05-14 20:29:52 +0000877size_t CheckString::Check(const SourceMgr &SM, StringRef Buffer,
Stephen Line5f740c2013-10-11 18:38:36 +0000878 bool IsLabelScanMode, size_t &MatchLen,
Michael Liao7efbbd62013-05-14 20:29:52 +0000879 StringMap<StringRef> &VariableTable) const {
Michael Liao95ab3262013-05-14 20:34:12 +0000880 size_t LastPos = 0;
881 std::vector<const Pattern *> NotStrings;
882
Stephen Line5f740c2013-10-11 18:38:36 +0000883 // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
884 // bounds; we have not processed variable definitions within the bounded block
885 // yet so cannot handle any final CHECK-DAG yet; this is handled when going
886 // over the block again (including the last CHECK-LABEL) in normal mode.
887 if (!IsLabelScanMode) {
888 // Match "dag strings" (with mixed "not strings" if any).
889 LastPos = CheckDag(SM, Buffer, NotStrings, VariableTable);
890 if (LastPos == StringRef::npos)
891 return StringRef::npos;
892 }
Michael Liao95ab3262013-05-14 20:34:12 +0000893
894 // Match itself from the last position after matching CHECK-DAG.
895 StringRef MatchBuffer = Buffer.substr(LastPos);
896 size_t MatchPos = Pat.Match(MatchBuffer, MatchLen, VariableTable);
Michael Liao7efbbd62013-05-14 20:29:52 +0000897 if (MatchPos == StringRef::npos) {
Michael Liao95ab3262013-05-14 20:34:12 +0000898 PrintCheckFailed(SM, *this, MatchBuffer, VariableTable);
Michael Liao7efbbd62013-05-14 20:29:52 +0000899 return StringRef::npos;
900 }
Michael Liao95ab3262013-05-14 20:34:12 +0000901 MatchPos += LastPos;
Michael Liao7efbbd62013-05-14 20:29:52 +0000902
Stephen Line5f740c2013-10-11 18:38:36 +0000903 // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
904 // or CHECK-NOT
905 if (!IsLabelScanMode) {
Stephen Lin178504b2013-07-12 14:51:05 +0000906 StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
Michael Liao7efbbd62013-05-14 20:29:52 +0000907
Stephen Lin178504b2013-07-12 14:51:05 +0000908 // If this check is a "CHECK-NEXT", verify that the previous match was on
909 // the previous line (i.e. that there is one newline between them).
910 if (CheckNext(SM, SkippedRegion))
911 return StringRef::npos;
Michael Liao7efbbd62013-05-14 20:29:52 +0000912
Stephen Lin178504b2013-07-12 14:51:05 +0000913 // If this match had "not strings", verify that they don't exist in the
914 // skipped region.
915 if (CheckNot(SM, SkippedRegion, NotStrings, VariableTable))
916 return StringRef::npos;
917 }
Michael Liao7efbbd62013-05-14 20:29:52 +0000918
919 return MatchPos;
920}
921
922bool CheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const {
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000923 if (CheckTy != Check::CheckNext)
Michael Liao7efbbd62013-05-14 20:29:52 +0000924 return false;
925
926 // Count the number of newlines between the previous match and this one.
927 assert(Buffer.data() !=
928 SM.getMemoryBuffer(
929 SM.FindBufferContainingLoc(
930 SMLoc::getFromPointer(Buffer.data())))->getBufferStart() &&
931 "CHECK-NEXT can't be the first check in a file");
932
933 unsigned NumNewLines = CountNumNewlinesBetween(Buffer);
934
935 if (NumNewLines == 0) {
936 SM.PrintMessage(Loc, SourceMgr::DK_Error, CheckPrefix+
937 "-NEXT: is on the same line as previous match");
938 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()),
939 SourceMgr::DK_Note, "'next' match was here");
940 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
941 "previous match ended here");
942 return true;
943 }
944
945 if (NumNewLines != 1) {
946 SM.PrintMessage(Loc, SourceMgr::DK_Error, CheckPrefix+
947 "-NEXT: is not on the line after the previous match");
948 SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()),
949 SourceMgr::DK_Note, "'next' match was here");
950 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
951 "previous match ended here");
952 return true;
953 }
954
955 return false;
956}
957
958bool CheckString::CheckNot(const SourceMgr &SM, StringRef Buffer,
Michael Liao95ab3262013-05-14 20:34:12 +0000959 const std::vector<const Pattern *> &NotStrings,
Michael Liao7efbbd62013-05-14 20:29:52 +0000960 StringMap<StringRef> &VariableTable) const {
961 for (unsigned ChunkNo = 0, e = NotStrings.size();
962 ChunkNo != e; ++ChunkNo) {
Michael Liao95ab3262013-05-14 20:34:12 +0000963 const Pattern *Pat = NotStrings[ChunkNo];
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000964 assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!");
Michael Liao95ab3262013-05-14 20:34:12 +0000965
Michael Liao7efbbd62013-05-14 20:29:52 +0000966 size_t MatchLen = 0;
Michael Liao95ab3262013-05-14 20:34:12 +0000967 size_t Pos = Pat->Match(Buffer, MatchLen, VariableTable);
Michael Liao7efbbd62013-05-14 20:29:52 +0000968
969 if (Pos == StringRef::npos) continue;
970
971 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()+Pos),
972 SourceMgr::DK_Error,
973 CheckPrefix+"-NOT: string occurred!");
Michael Liao95ab3262013-05-14 20:34:12 +0000974 SM.PrintMessage(Pat->getLoc(), SourceMgr::DK_Note,
Michael Liao7efbbd62013-05-14 20:29:52 +0000975 CheckPrefix+"-NOT: pattern specified here");
976 return true;
977 }
978
979 return false;
980}
981
Michael Liao95ab3262013-05-14 20:34:12 +0000982size_t CheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
983 std::vector<const Pattern *> &NotStrings,
984 StringMap<StringRef> &VariableTable) const {
985 if (DagNotStrings.empty())
986 return 0;
987
988 size_t LastPos = 0;
989 size_t StartPos = LastPos;
990
991 for (unsigned ChunkNo = 0, e = DagNotStrings.size();
992 ChunkNo != e; ++ChunkNo) {
993 const Pattern &Pat = DagNotStrings[ChunkNo];
994
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000995 assert((Pat.getCheckTy() == Check::CheckDAG ||
996 Pat.getCheckTy() == Check::CheckNot) &&
Michael Liao95ab3262013-05-14 20:34:12 +0000997 "Invalid CHECK-DAG or CHECK-NOT!");
998
Matt Arsenault4f67afc2013-09-17 22:30:02 +0000999 if (Pat.getCheckTy() == Check::CheckNot) {
Michael Liao95ab3262013-05-14 20:34:12 +00001000 NotStrings.push_back(&Pat);
1001 continue;
1002 }
1003
Matt Arsenault4f67afc2013-09-17 22:30:02 +00001004 assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!");
Michael Liao95ab3262013-05-14 20:34:12 +00001005
1006 size_t MatchLen = 0, MatchPos;
1007
1008 // CHECK-DAG always matches from the start.
1009 StringRef MatchBuffer = Buffer.substr(StartPos);
1010 MatchPos = Pat.Match(MatchBuffer, MatchLen, VariableTable);
1011 // With a group of CHECK-DAGs, a single mismatching means the match on
1012 // that group of CHECK-DAGs fails immediately.
1013 if (MatchPos == StringRef::npos) {
1014 PrintCheckFailed(SM, Pat.getLoc(), Pat, MatchBuffer, VariableTable);
1015 return StringRef::npos;
1016 }
1017 // Re-calc it as the offset relative to the start of the original string.
1018 MatchPos += StartPos;
1019
1020 if (!NotStrings.empty()) {
1021 if (MatchPos < LastPos) {
1022 // Reordered?
1023 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + MatchPos),
1024 SourceMgr::DK_Error,
1025 CheckPrefix+"-DAG: found a match of CHECK-DAG"
1026 " reordering across a CHECK-NOT");
1027 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + LastPos),
1028 SourceMgr::DK_Note,
1029 CheckPrefix+"-DAG: the farthest match of CHECK-DAG"
1030 " is found here");
1031 SM.PrintMessage(NotStrings[0]->getLoc(), SourceMgr::DK_Note,
1032 CheckPrefix+"-NOT: the crossed pattern specified"
1033 " here");
1034 SM.PrintMessage(Pat.getLoc(), SourceMgr::DK_Note,
1035 CheckPrefix+"-DAG: the reordered pattern specified"
1036 " here");
1037 return StringRef::npos;
1038 }
1039 // All subsequent CHECK-DAGs should be matched from the farthest
1040 // position of all precedent CHECK-DAGs (including this one.)
1041 StartPos = LastPos;
1042 // If there's CHECK-NOTs between two CHECK-DAGs or from CHECK to
1043 // CHECK-DAG, verify that there's no 'not' strings occurred in that
1044 // region.
1045 StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
Tim Northovere57343b2013-08-02 11:32:50 +00001046 if (CheckNot(SM, SkippedRegion, NotStrings, VariableTable))
Michael Liao95ab3262013-05-14 20:34:12 +00001047 return StringRef::npos;
1048 // Clear "not strings".
1049 NotStrings.clear();
1050 }
1051
1052 // Update the last position with CHECK-DAG matches.
1053 LastPos = std::max(MatchPos + MatchLen, LastPos);
1054 }
1055
1056 return LastPos;
1057}
1058
Rui Ueyamad9a84ef2013-08-12 23:05:59 +00001059bool ValidateCheckPrefix() {
1060 // The check prefix must contain only alphanumeric, hyphens and underscores.
1061 Regex prefixValidator("^[a-zA-Z0-9_-]*$");
1062 return prefixValidator.match(CheckPrefix);
1063}
1064
Chris Lattner81cb8ca2009-07-08 18:44:05 +00001065int main(int argc, char **argv) {
1066 sys::PrintStackTraceOnErrorSignal();
1067 PrettyStackTraceProgram X(argc, argv);
1068 cl::ParseCommandLineOptions(argc, argv);
1069
Rui Ueyamad9a84ef2013-08-12 23:05:59 +00001070 if (!ValidateCheckPrefix()) {
1071 errs() << "Supplied check-prefix is invalid! Prefixes must start with a "
1072 "letter and contain only alphanumeric characters, hyphens and "
1073 "underscores\n";
1074 return 2;
1075 }
1076
Chris Lattner81cb8ca2009-07-08 18:44:05 +00001077 SourceMgr SM;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +00001078
Chris Lattner81cb8ca2009-07-08 18:44:05 +00001079 // Read the expected strings from the check file.
Chris Lattner207e1bc2009-08-15 17:41:04 +00001080 std::vector<CheckString> CheckStrings;
Chris Lattner81cb8ca2009-07-08 18:44:05 +00001081 if (ReadCheckFile(SM, CheckStrings))
1082 return 2;
1083
1084 // Open the file to check and add it to SourceMgr.
Michael J. Spencer3ff95632010-12-16 03:29:14 +00001085 OwningPtr<MemoryBuffer> File;
1086 if (error_code ec =
Rafael Espindoladd5af272013-06-25 05:28:34 +00001087 MemoryBuffer::getFileOrSTDIN(InputFilename, File)) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +00001088 errs() << "Could not open input file '" << InputFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +00001089 << ec.message() << '\n';
Eli Bendersky7f8e76f2012-11-30 13:51:33 +00001090 return 2;
Chris Lattner81cb8ca2009-07-08 18:44:05 +00001091 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +00001092
Benjamin Kramer7cdba152013-03-23 13:56:23 +00001093 if (File->getBufferSize() == 0) {
Chris Lattner1aac1862011-02-09 16:46:02 +00001094 errs() << "FileCheck error: '" << InputFilename << "' is empty.\n";
Eli Bendersky7f8e76f2012-11-30 13:51:33 +00001095 return 2;
Chris Lattner1aac1862011-02-09 16:46:02 +00001096 }
Benjamin Kramer7cdba152013-03-23 13:56:23 +00001097
Chris Lattner88a7e9e2009-07-11 18:58:15 +00001098 // Remove duplicate spaces in the input file if requested.
Guy Benyei4cc74fc2013-02-06 20:40:38 +00001099 // Remove DOS style line endings.
Benjamin Kramer7cdba152013-03-23 13:56:23 +00001100 MemoryBuffer *F =
1101 CanonicalizeInputFile(File.take(), NoCanonicalizeWhiteSpace);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +00001102
Chris Lattner81cb8ca2009-07-08 18:44:05 +00001103 SM.AddNewSourceBuffer(F, SMLoc());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +00001104
Chris Lattnereec96952009-09-27 07:56:52 +00001105 /// VariableTable - This holds all the current filecheck variables.
1106 StringMap<StringRef> VariableTable;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +00001107
Chris Lattner81cb8ca2009-07-08 18:44:05 +00001108 // Check that we have all of the expected strings, in order, in the input
1109 // file.
Chris Lattner96077032009-09-20 22:11:44 +00001110 StringRef Buffer = F->getBuffer();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +00001111
Stephen Lin178504b2013-07-12 14:51:05 +00001112 bool hasError = false;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +00001113
Stephen Lin178504b2013-07-12 14:51:05 +00001114 unsigned i = 0, j = 0, e = CheckStrings.size();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +00001115
Stephen Lin178504b2013-07-12 14:51:05 +00001116 while (true) {
1117 StringRef CheckRegion;
1118 if (j == e) {
1119 CheckRegion = Buffer;
1120 } else {
1121 const CheckString &CheckLabelStr = CheckStrings[j];
Matt Arsenault4f67afc2013-09-17 22:30:02 +00001122 if (CheckLabelStr.CheckTy != Check::CheckLabel) {
Stephen Lin178504b2013-07-12 14:51:05 +00001123 ++j;
1124 continue;
1125 }
Chris Lattner3711b7a2009-09-20 22:42:44 +00001126
Stephen Lin178504b2013-07-12 14:51:05 +00001127 // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
1128 size_t MatchLabelLen = 0;
Stephen Line5f740c2013-10-11 18:38:36 +00001129 size_t MatchLabelPos = CheckLabelStr.Check(SM, Buffer, true,
Stephen Lin178504b2013-07-12 14:51:05 +00001130 MatchLabelLen, VariableTable);
1131 if (MatchLabelPos == StringRef::npos) {
1132 hasError = true;
1133 break;
1134 }
1135
1136 CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen);
1137 Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen);
1138 ++j;
1139 }
1140
1141 for ( ; i != j; ++i) {
1142 const CheckString &CheckStr = CheckStrings[i];
1143
1144 // Check each string within the scanned region, including a second check
1145 // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
1146 size_t MatchLen = 0;
Stephen Line5f740c2013-10-11 18:38:36 +00001147 size_t MatchPos = CheckStr.Check(SM, CheckRegion, false, MatchLen,
Stephen Lin178504b2013-07-12 14:51:05 +00001148 VariableTable);
1149
1150 if (MatchPos == StringRef::npos) {
1151 hasError = true;
1152 i = j;
1153 break;
1154 }
1155
1156 CheckRegion = CheckRegion.substr(MatchPos + MatchLen);
1157 }
1158
1159 if (j == e)
1160 break;
Chris Lattner81cb8ca2009-07-08 18:44:05 +00001161 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +00001162
Stephen Lin178504b2013-07-12 14:51:05 +00001163 return hasError ? 1 : 0;
Chris Lattner81cb8ca2009-07-08 18:44:05 +00001164}