blob: 7b330d5ec751102e736efe1aaab9007ef931ba06 [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"
Alexander Kornienko70a870a2012-11-14 21:07:37 +000029#include "llvm/ADT/StringExtras.h"
Chris Lattnereec96952009-09-27 07:56:52 +000030#include "llvm/ADT/StringMap.h"
31#include <algorithm>
Eli Bendersky9756ca72012-12-01 21:54:48 +000032#include <map>
33#include <string>
34#include <vector>
Chris Lattner81cb8ca2009-07-08 18:44:05 +000035using namespace llvm;
36
37static cl::opt<std::string>
38CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
39
40static cl::opt<std::string>
41InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
42 cl::init("-"), cl::value_desc("filename"));
43
44static cl::opt<std::string>
45CheckPrefix("check-prefix", cl::init("CHECK"),
46 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
47
Chris Lattner88a7e9e2009-07-11 18:58:15 +000048static cl::opt<bool>
49NoCanonicalizeWhiteSpace("strict-whitespace",
50 cl::desc("Do not treat all horizontal whitespace as equivalent"));
51
Chris Lattnera29703e2009-09-24 20:39:13 +000052//===----------------------------------------------------------------------===//
53// Pattern Handling Code.
54//===----------------------------------------------------------------------===//
55
Chris Lattner9fc66782009-09-24 20:25:55 +000056class Pattern {
Chris Lattner94638f02009-09-25 17:29:36 +000057 SMLoc PatternLoc;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000058
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +000059 /// MatchEOF - When set, this pattern only matches the end of file. This is
60 /// used for trailing CHECK-NOTs.
61 bool MatchEOF;
62
Chris Lattner5d6a05f2009-09-25 17:23:43 +000063 /// FixedStr - If non-empty, this pattern is a fixed string match with the
64 /// specified fixed string.
Chris Lattner2702e6a2009-09-25 17:09:12 +000065 StringRef FixedStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000066
Chris Lattner5d6a05f2009-09-25 17:23:43 +000067 /// RegEx - If non-empty, this is a regex pattern.
68 std::string RegExStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000069
Alexander Kornienko70a870a2012-11-14 21:07:37 +000070 /// \brief Contains the number of line this pattern is in.
71 unsigned LineNumber;
72
Chris Lattnereec96952009-09-27 07:56:52 +000073 /// VariableUses - Entries in this vector map to uses of a variable in the
74 /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain
75 /// "foobaz" and we'll get an entry in this vector that tells us to insert the
76 /// value of bar at offset 3.
77 std::vector<std::pair<StringRef, unsigned> > VariableUses;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000078
Eli Bendersky9756ca72012-12-01 21:54:48 +000079 /// VariableDefs - Maps definitions of variables to their parenthesized
80 /// capture numbers.
81 /// E.g. for the pattern "foo[[bar:.*]]baz", VariableDefs will map "bar" to 1.
82 std::map<StringRef, unsigned> VariableDefs;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000083
Chris Lattner9fc66782009-09-24 20:25:55 +000084public:
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000085
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +000086 Pattern(bool matchEOF = false) : MatchEOF(matchEOF) { }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000087
Eli Bendersky1e5cbcb2012-11-30 14:22:14 +000088 /// ParsePattern - Parse the given string into the Pattern. SM provides the
89 /// SourceMgr used for error reports, and LineNumber is the line number in
90 /// the input file from which the pattern string was read.
91 /// Returns true in case of an error, false otherwise.
Alexander Kornienko70a870a2012-11-14 21:07:37 +000092 bool ParsePattern(StringRef PatternStr, SourceMgr &SM, unsigned LineNumber);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +000093
Chris Lattner9fc66782009-09-24 20:25:55 +000094 /// Match - Match the pattern string against the input buffer Buffer. This
95 /// returns the position that is matched or npos if there is no match. If
96 /// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +000097 ///
98 /// The VariableTable StringMap provides the current values of filecheck
99 /// variables and is updated if this match defines new values.
100 size_t Match(StringRef Buffer, size_t &MatchLen,
101 StringMap<StringRef> &VariableTable) const;
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000102
103 /// PrintFailureInfo - Print additional information about a failure to match
104 /// involving this pattern.
105 void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
106 const StringMap<StringRef> &VariableTable) const;
107
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000108private:
Chris Lattnereec96952009-09-27 07:56:52 +0000109 static void AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr);
Eli Bendersky9756ca72012-12-01 21:54:48 +0000110 bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
111 void AddBackrefToRegEx(unsigned BackrefNum);
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000112
113 /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of
114 /// matching this pattern at the start of \arg Buffer; a distance of zero
115 /// should correspond to a perfect match.
116 unsigned ComputeMatchDistance(StringRef Buffer,
117 const StringMap<StringRef> &VariableTable) const;
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000118
119 /// \brief Evaluates expression and stores the result to \p Value.
120 /// \return true on success. false when the expression has invalid syntax.
121 bool EvaluateExpression(StringRef Expr, std::string &Value) const;
Chris Lattner9fc66782009-09-24 20:25:55 +0000122};
123
Chris Lattnereec96952009-09-27 07:56:52 +0000124
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000125bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM,
126 unsigned LineNumber) {
127 this->LineNumber = LineNumber;
Chris Lattner94638f02009-09-25 17:29:36 +0000128 PatternLoc = SMLoc::getFromPointer(PatternStr.data());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000129
Chris Lattnera29703e2009-09-24 20:39:13 +0000130 // Ignore trailing whitespace.
131 while (!PatternStr.empty() &&
132 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
133 PatternStr = PatternStr.substr(0, PatternStr.size()-1);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000134
Chris Lattnera29703e2009-09-24 20:39:13 +0000135 // Check that there is something on the line.
136 if (PatternStr.empty()) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000137 SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
138 "found empty check string with prefix '" +
139 CheckPrefix+":'");
Chris Lattnera29703e2009-09-24 20:39:13 +0000140 return true;
141 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000142
Chris Lattner2702e6a2009-09-25 17:09:12 +0000143 // Check to see if this is a fixed string, or if it has regex pieces.
Ted Kremenek4f505172012-09-08 04:32:13 +0000144 if (PatternStr.size() < 2 ||
Chris Lattnereec96952009-09-27 07:56:52 +0000145 (PatternStr.find("{{") == StringRef::npos &&
146 PatternStr.find("[[") == StringRef::npos)) {
Chris Lattner2702e6a2009-09-25 17:09:12 +0000147 FixedStr = PatternStr;
148 return false;
149 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000150
Chris Lattnereec96952009-09-27 07:56:52 +0000151 // Paren value #0 is for the fully matched string. Any new parenthesized
Chris Lattner13a38c42011-04-09 06:18:02 +0000152 // values add from there.
Chris Lattnereec96952009-09-27 07:56:52 +0000153 unsigned CurParen = 1;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000154
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000155 // Otherwise, there is at least one regex piece. Build up the regex pattern
156 // by escaping scary characters in fixed strings, building up one big regex.
Chris Lattner52870082009-09-24 21:47:32 +0000157 while (!PatternStr.empty()) {
Chris Lattnereec96952009-09-27 07:56:52 +0000158 // RegEx matches.
Chris Lattner13a38c42011-04-09 06:18:02 +0000159 if (PatternStr.startswith("{{")) {
Eli Bendersky1e5cbcb2012-11-30 14:22:14 +0000160 // This is the start of a regex match. Scan for the }}.
Chris Lattnereec96952009-09-27 07:56:52 +0000161 size_t End = PatternStr.find("}}");
162 if (End == StringRef::npos) {
163 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000164 SourceMgr::DK_Error,
165 "found start of regex string with no end '}}'");
Chris Lattnereec96952009-09-27 07:56:52 +0000166 return true;
167 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000168
Chris Lattner42e31df2011-04-09 06:37:03 +0000169 // Enclose {{}} patterns in parens just like [[]] even though we're not
170 // capturing the result for any purpose. This is required in case the
171 // expression contains an alternation like: CHECK: abc{{x|z}}def. We
172 // want this to turn into: "abc(x|z)def" not "abcx|zdef".
173 RegExStr += '(';
174 ++CurParen;
175
Chris Lattnereec96952009-09-27 07:56:52 +0000176 if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
177 return true;
Chris Lattner42e31df2011-04-09 06:37:03 +0000178 RegExStr += ')';
Chris Lattner13a38c42011-04-09 06:18:02 +0000179
Chris Lattnereec96952009-09-27 07:56:52 +0000180 PatternStr = PatternStr.substr(End+2);
Chris Lattner52870082009-09-24 21:47:32 +0000181 continue;
182 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000183
Chris Lattnereec96952009-09-27 07:56:52 +0000184 // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
185 // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
186 // second form is [[foo]] which is a reference to foo. The variable name
Daniel Dunbar964ac012009-11-22 22:07:50 +0000187 // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
Chris Lattnereec96952009-09-27 07:56:52 +0000188 // it. This is to catch some common errors.
Chris Lattner13a38c42011-04-09 06:18:02 +0000189 if (PatternStr.startswith("[[")) {
Chris Lattnereec96952009-09-27 07:56:52 +0000190 // Verify that it is terminated properly.
191 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 "invalid named regex reference, no ]] found");
Chris Lattnereec96952009-09-27 07:56:52 +0000196 return true;
197 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000198
Chris Lattnereec96952009-09-27 07:56:52 +0000199 StringRef MatchStr = PatternStr.substr(2, End-2);
200 PatternStr = PatternStr.substr(End+2);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000201
Chris Lattnereec96952009-09-27 07:56:52 +0000202 // Get the regex name (e.g. "foo").
203 size_t NameEnd = MatchStr.find(':');
204 StringRef Name = MatchStr.substr(0, NameEnd);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000205
Chris Lattnereec96952009-09-27 07:56:52 +0000206 if (Name.empty()) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000207 SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
208 "invalid name in named regex: empty name");
Chris Lattnereec96952009-09-27 07:56:52 +0000209 return true;
210 }
211
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000212 // Verify that the name/expression is well formed. FileCheck currently
213 // supports @LINE, @LINE+number, @LINE-number expressions. The check here
214 // is relaxed, more strict check is performed in \c EvaluateExpression.
215 bool IsExpression = false;
216 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
217 if (i == 0 && Name[i] == '@') {
218 if (NameEnd != StringRef::npos) {
219 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
220 SourceMgr::DK_Error,
221 "invalid name in named regex definition");
222 return true;
223 }
224 IsExpression = true;
225 continue;
226 }
227 if (Name[i] != '_' && !isalnum(Name[i]) &&
228 (!IsExpression || (Name[i] != '+' && Name[i] != '-'))) {
Chris Lattnereec96952009-09-27 07:56:52 +0000229 SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000230 SourceMgr::DK_Error, "invalid name in named regex");
Chris Lattnereec96952009-09-27 07:56:52 +0000231 return true;
232 }
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000233 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000234
Chris Lattnereec96952009-09-27 07:56:52 +0000235 // Name can't start with a digit.
236 if (isdigit(Name[0])) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000237 SM.PrintMessage(SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
238 "invalid name in named regex");
Chris Lattnereec96952009-09-27 07:56:52 +0000239 return true;
240 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000241
Chris Lattnereec96952009-09-27 07:56:52 +0000242 // Handle [[foo]].
243 if (NameEnd == StringRef::npos) {
Eli Bendersky9756ca72012-12-01 21:54:48 +0000244 // Handle variables that were defined earlier on the same line by
245 // emitting a backreference.
246 if (VariableDefs.find(Name) != VariableDefs.end()) {
247 unsigned VarParenNum = VariableDefs[Name];
248 if (VarParenNum < 1 || VarParenNum > 9) {
249 SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
250 SourceMgr::DK_Error,
251 "Can't back-reference more than 9 variables");
252 return true;
253 }
254 AddBackrefToRegEx(VarParenNum);
255 } else {
256 VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
257 }
Chris Lattnereec96952009-09-27 07:56:52 +0000258 continue;
259 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000260
Chris Lattnereec96952009-09-27 07:56:52 +0000261 // Handle [[foo:.*]].
Eli Bendersky9756ca72012-12-01 21:54:48 +0000262 VariableDefs[Name] = CurParen;
Chris Lattnereec96952009-09-27 07:56:52 +0000263 RegExStr += '(';
264 ++CurParen;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000265
Chris Lattnereec96952009-09-27 07:56:52 +0000266 if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
267 return true;
268
269 RegExStr += ')';
Chris Lattner52870082009-09-24 21:47:32 +0000270 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000271
Chris Lattnereec96952009-09-27 07:56:52 +0000272 // Handle fixed string matches.
273 // Find the end, which is the start of the next regex.
274 size_t FixedMatchEnd = PatternStr.find("{{");
275 FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
276 AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr);
277 PatternStr = PatternStr.substr(FixedMatchEnd);
Chris Lattner52870082009-09-24 21:47:32 +0000278 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000279
Chris Lattnera29703e2009-09-24 20:39:13 +0000280 return false;
281}
282
Chris Lattnereec96952009-09-27 07:56:52 +0000283void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) {
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000284 // Add the characters from FixedStr to the regex, escaping as needed. This
285 // avoids "leaning toothpicks" in common patterns.
286 for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) {
287 switch (FixedStr[i]) {
288 // These are the special characters matched in "p_ere_exp".
289 case '(':
290 case ')':
291 case '^':
292 case '$':
293 case '|':
294 case '*':
295 case '+':
296 case '?':
297 case '.':
298 case '[':
299 case '\\':
300 case '{':
Chris Lattnereec96952009-09-27 07:56:52 +0000301 TheStr += '\\';
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000302 // FALL THROUGH.
303 default:
Chris Lattnereec96952009-09-27 07:56:52 +0000304 TheStr += FixedStr[i];
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000305 break;
306 }
307 }
308}
309
Eli Bendersky9756ca72012-12-01 21:54:48 +0000310bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen,
Chris Lattnereec96952009-09-27 07:56:52 +0000311 SourceMgr &SM) {
Eli Bendersky9756ca72012-12-01 21:54:48 +0000312 Regex R(RS);
Chris Lattnereec96952009-09-27 07:56:52 +0000313 std::string Error;
314 if (!R.isValid(Error)) {
Eli Bendersky9756ca72012-12-01 21:54:48 +0000315 SM.PrintMessage(SMLoc::getFromPointer(RS.data()), SourceMgr::DK_Error,
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000316 "invalid regex: " + Error);
Chris Lattnereec96952009-09-27 07:56:52 +0000317 return true;
318 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000319
Eli Bendersky9756ca72012-12-01 21:54:48 +0000320 RegExStr += RS.str();
Chris Lattnereec96952009-09-27 07:56:52 +0000321 CurParen += R.getNumMatches();
322 return false;
323}
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000324
Eli Bendersky9756ca72012-12-01 21:54:48 +0000325void Pattern::AddBackrefToRegEx(unsigned BackrefNum) {
326 assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number");
327 std::string Backref = std::string("\\") +
328 std::string(1, '0' + BackrefNum);
329 RegExStr += Backref;
330}
331
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000332bool Pattern::EvaluateExpression(StringRef Expr, std::string &Value) const {
333 // The only supported expression is @LINE([\+-]\d+)?
334 if (!Expr.startswith("@LINE"))
335 return false;
336 Expr = Expr.substr(StringRef("@LINE").size());
337 int Offset = 0;
338 if (!Expr.empty()) {
339 if (Expr[0] == '+')
340 Expr = Expr.substr(1);
341 else if (Expr[0] != '-')
342 return false;
343 if (Expr.getAsInteger(10, Offset))
344 return false;
345 }
346 Value = llvm::itostr(LineNumber + Offset);
347 return true;
348}
349
Chris Lattner52870082009-09-24 21:47:32 +0000350/// Match - Match the pattern string against the input buffer Buffer. This
351/// returns the position that is matched or npos if there is no match. If
352/// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattnereec96952009-09-27 07:56:52 +0000353size_t Pattern::Match(StringRef Buffer, size_t &MatchLen,
354 StringMap<StringRef> &VariableTable) const {
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000355 // If this is the EOF pattern, match it immediately.
356 if (MatchEOF) {
357 MatchLen = 0;
358 return Buffer.size();
359 }
360
Chris Lattner2702e6a2009-09-25 17:09:12 +0000361 // If this is a fixed string pattern, just match it now.
362 if (!FixedStr.empty()) {
363 MatchLen = FixedStr.size();
364 return Buffer.find(FixedStr);
365 }
Chris Lattnereec96952009-09-27 07:56:52 +0000366
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000367 // Regex match.
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000368
Chris Lattnereec96952009-09-27 07:56:52 +0000369 // If there are variable uses, we need to create a temporary string with the
370 // actual value.
371 StringRef RegExToMatch = RegExStr;
372 std::string TmpStr;
373 if (!VariableUses.empty()) {
374 TmpStr = RegExStr;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000375
Chris Lattnereec96952009-09-27 07:56:52 +0000376 unsigned InsertOffset = 0;
377 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
Chris Lattnereec96952009-09-27 07:56:52 +0000378 std::string Value;
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000379
380 if (VariableUses[i].first[0] == '@') {
381 if (!EvaluateExpression(VariableUses[i].first, Value))
382 return StringRef::npos;
383 } else {
384 StringMap<StringRef>::iterator it =
385 VariableTable.find(VariableUses[i].first);
386 // If the variable is undefined, return an error.
387 if (it == VariableTable.end())
388 return StringRef::npos;
389
390 // Look up the value and escape it so that we can plop it into the regex.
391 AddFixedStringToRegEx(it->second, Value);
392 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000393
Chris Lattnereec96952009-09-27 07:56:52 +0000394 // Plop it into the regex at the adjusted offset.
395 TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset,
396 Value.begin(), Value.end());
397 InsertOffset += Value.size();
398 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000399
Chris Lattnereec96952009-09-27 07:56:52 +0000400 // Match the newly constructed regex.
401 RegExToMatch = TmpStr;
402 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000403
404
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000405 SmallVector<StringRef, 4> MatchInfo;
Chris Lattnereec96952009-09-27 07:56:52 +0000406 if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000407 return StringRef::npos;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000408
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000409 // Successful regex match.
410 assert(!MatchInfo.empty() && "Didn't get any match");
411 StringRef FullMatch = MatchInfo[0];
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000412
Chris Lattnereec96952009-09-27 07:56:52 +0000413 // If this defines any variables, remember their values.
Eli Bendersky9756ca72012-12-01 21:54:48 +0000414 for (std::map<StringRef, unsigned>::const_iterator I = VariableDefs.begin(),
415 E = VariableDefs.end();
416 I != E; ++I) {
417 assert(I->second < MatchInfo.size() && "Internal paren error");
418 VariableTable[I->first] = MatchInfo[I->second];
Chris Lattner94638f02009-09-25 17:29:36 +0000419 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000420
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000421 MatchLen = FullMatch.size();
422 return FullMatch.data()-Buffer.data();
Chris Lattner52870082009-09-24 21:47:32 +0000423}
424
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000425unsigned Pattern::ComputeMatchDistance(StringRef Buffer,
426 const StringMap<StringRef> &VariableTable) const {
427 // Just compute the number of matching characters. For regular expressions, we
428 // just compare against the regex itself and hope for the best.
429 //
430 // FIXME: One easy improvement here is have the regex lib generate a single
431 // example regular expression which matches, and use that as the example
432 // string.
433 StringRef ExampleString(FixedStr);
434 if (ExampleString.empty())
435 ExampleString = RegExStr;
436
Daniel Dunbar0806f9f2010-01-30 00:24:06 +0000437 // Only compare up to the first line in the buffer, or the string size.
438 StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
439 BufferPrefix = BufferPrefix.split('\n').first;
440 return BufferPrefix.edit_distance(ExampleString);
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000441}
442
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000443void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
444 const StringMap<StringRef> &VariableTable) const{
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000445 // If this was a regular expression using variables, print the current
446 // variable values.
447 if (!VariableUses.empty()) {
448 for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000449 SmallString<256> Msg;
450 raw_svector_ostream OS(Msg);
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000451 StringRef Var = VariableUses[i].first;
452 if (Var[0] == '@') {
453 std::string Value;
454 if (EvaluateExpression(Var, Value)) {
455 OS << "with expression \"";
456 OS.write_escaped(Var) << "\" equal to \"";
457 OS.write_escaped(Value) << "\"";
458 } else {
459 OS << "uses incorrect expression \"";
460 OS.write_escaped(Var) << "\"";
461 }
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000462 } else {
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000463 StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
464
465 // Check for undefined variable references.
466 if (it == VariableTable.end()) {
467 OS << "uses undefined variable \"";
468 OS.write_escaped(Var) << "\"";
469 } else {
470 OS << "with variable \"";
471 OS.write_escaped(Var) << "\" equal to \"";
472 OS.write_escaped(it->second) << "\"";
473 }
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000474 }
475
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000476 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
477 OS.str());
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000478 }
479 }
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000480
481 // Attempt to find the closest/best fuzzy match. Usually an error happens
482 // because some string in the output didn't exactly match. In these cases, we
483 // would like to show the user a best guess at what "should have" matched, to
484 // save them having to actually check the input manually.
485 size_t NumLinesForward = 0;
486 size_t Best = StringRef::npos;
487 double BestQuality = 0;
488
489 // Use an arbitrary 4k limit on how far we will search.
Dan Gohmane3a1e502010-01-29 21:57:46 +0000490 for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000491 if (Buffer[i] == '\n')
492 ++NumLinesForward;
493
Dan Gohmand8a55412010-01-29 21:55:16 +0000494 // Patterns have leading whitespace stripped, so skip whitespace when
495 // looking for something which looks like a pattern.
496 if (Buffer[i] == ' ' || Buffer[i] == '\t')
497 continue;
498
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000499 // Compute the "quality" of this match as an arbitrary combination of the
500 // match distance and the number of lines skipped to get to this match.
501 unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable);
502 double Quality = Distance + (NumLinesForward / 100.);
503
504 if (Quality < BestQuality || Best == StringRef::npos) {
505 Best = i;
506 BestQuality = Quality;
507 }
508 }
509
Daniel Dunbar7a68e0d2010-03-19 18:07:43 +0000510 // Print the "possible intended match here" line if we found something
511 // reasonable and not equal to what we showed in the "scanning from here"
512 // line.
513 if (Best && Best != StringRef::npos && BestQuality < 50) {
514 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000515 SourceMgr::DK_Note, "possible intended match here");
Daniel Dunbaread2dac2009-11-22 22:59:26 +0000516
517 // FIXME: If we wanted to be really friendly we would show why the match
518 // failed, as it can be hard to spot simple one character differences.
519 }
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000520}
Chris Lattnera29703e2009-09-24 20:39:13 +0000521
522//===----------------------------------------------------------------------===//
523// Check Strings.
524//===----------------------------------------------------------------------===//
Chris Lattner9fc66782009-09-24 20:25:55 +0000525
526/// CheckString - This is a check that we found in the input file.
527struct CheckString {
528 /// Pat - The pattern to match.
529 Pattern Pat;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000530
Chris Lattner207e1bc2009-08-15 17:41:04 +0000531 /// Loc - The location in the match file that the check string was specified.
532 SMLoc Loc;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000533
Chris Lattner5dafafd2009-08-15 18:32:21 +0000534 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
535 /// to a CHECK: directive.
536 bool IsCheckNext;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000537
Chris Lattnerf15380b2009-09-20 22:35:26 +0000538 /// NotStrings - These are all of the strings that are disallowed from
539 /// occurring between this match string and the previous one (or start of
540 /// file).
Chris Lattnera29703e2009-09-24 20:39:13 +0000541 std::vector<std::pair<SMLoc, Pattern> > NotStrings;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000542
Chris Lattner9fc66782009-09-24 20:25:55 +0000543 CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
544 : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
Chris Lattner207e1bc2009-08-15 17:41:04 +0000545};
546
Chris Lattneradea46e2009-09-24 20:45:07 +0000547/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
548/// memory buffer, free it, and return a new one.
549static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
Chris Lattner4c842dd2010-04-05 22:42:30 +0000550 SmallString<128> NewFile;
Chris Lattneradea46e2009-09-24 20:45:07 +0000551 NewFile.reserve(MB->getBufferSize());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000552
Chris Lattneradea46e2009-09-24 20:45:07 +0000553 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
554 Ptr != End; ++Ptr) {
NAKAMURA Takumi9f6e03f2010-11-14 03:28:22 +0000555 // Eliminate trailing dosish \r.
556 if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
557 continue;
558 }
559
Dmitri Gribenko9d227af2012-09-21 15:26:34 +0000560 // If current char is not a horizontal whitespace, dump it to output as is.
Chris Lattneradea46e2009-09-24 20:45:07 +0000561 if (*Ptr != ' ' && *Ptr != '\t') {
562 NewFile.push_back(*Ptr);
563 continue;
564 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000565
Chris Lattneradea46e2009-09-24 20:45:07 +0000566 // Otherwise, add one space and advance over neighboring space.
567 NewFile.push_back(' ');
568 while (Ptr+1 != End &&
569 (Ptr[1] == ' ' || Ptr[1] == '\t'))
570 ++Ptr;
571 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000572
Chris Lattneradea46e2009-09-24 20:45:07 +0000573 // Free the old buffer and return a new one.
574 MemoryBuffer *MB2 =
Chris Lattner4c842dd2010-04-05 22:42:30 +0000575 MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000576
Chris Lattneradea46e2009-09-24 20:45:07 +0000577 delete MB;
578 return MB2;
579}
580
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000581
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000582/// ReadCheckFile - Read the check file, which specifies the sequence of
583/// expected strings. The strings are added to the CheckStrings vector.
Eli Bendersky1e5cbcb2012-11-30 14:22:14 +0000584/// Returns true in case of an error, false otherwise.
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000585static bool ReadCheckFile(SourceMgr &SM,
Chris Lattner207e1bc2009-08-15 17:41:04 +0000586 std::vector<CheckString> &CheckStrings) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000587 OwningPtr<MemoryBuffer> File;
588 if (error_code ec =
589 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), File)) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000590 errs() << "Could not open check file '" << CheckFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +0000591 << ec.message() << '\n';
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000592 return true;
593 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000594 MemoryBuffer *F = File.take();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000595
Chris Lattneradea46e2009-09-24 20:45:07 +0000596 // If we want to canonicalize whitespace, strip excess whitespace from the
597 // buffer containing the CHECK lines.
598 if (!NoCanonicalizeWhiteSpace)
599 F = CanonicalizeInputFile(F);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000600
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000601 SM.AddNewSourceBuffer(F, SMLoc());
602
Chris Lattnerd7e25052009-08-15 18:00:42 +0000603 // Find all instances of CheckPrefix followed by : in the file.
Chris Lattner96077032009-09-20 22:11:44 +0000604 StringRef Buffer = F->getBuffer();
Chris Lattnera29703e2009-09-24 20:39:13 +0000605 std::vector<std::pair<SMLoc, Pattern> > NotMatches;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000606
Eli Bendersky1e5cbcb2012-11-30 14:22:14 +0000607 // LineNumber keeps track of the line on which CheckPrefix instances are
608 // found.
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000609 unsigned LineNumber = 1;
610
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000611 while (1) {
612 // See if Prefix occurs in the memory buffer.
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000613 size_t PrefixLoc = Buffer.find(CheckPrefix);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000614 // If we didn't find a match, we're done.
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000615 if (PrefixLoc == StringRef::npos)
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000616 break;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000617
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000618 LineNumber += Buffer.substr(0, PrefixLoc).count('\n');
619
620 Buffer = Buffer.substr(PrefixLoc);
621
Chris Lattner96077032009-09-20 22:11:44 +0000622 const char *CheckPrefixStart = Buffer.data();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000623
Chris Lattner5dafafd2009-08-15 18:32:21 +0000624 // When we find a check prefix, keep track of whether we find CHECK: or
625 // CHECK-NEXT:
Chris Lattnerf15380b2009-09-20 22:35:26 +0000626 bool IsCheckNext = false, IsCheckNot = false;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000627
Chris Lattnerd7e25052009-08-15 18:00:42 +0000628 // Verify that the : is present after the prefix.
Chris Lattner96077032009-09-20 22:11:44 +0000629 if (Buffer[CheckPrefix.size()] == ':') {
630 Buffer = Buffer.substr(CheckPrefix.size()+1);
Chris Lattner96077032009-09-20 22:11:44 +0000631 } else if (Buffer.size() > CheckPrefix.size()+6 &&
632 memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
Benjamin Kramer30ce40e2012-09-18 20:51:39 +0000633 Buffer = Buffer.substr(CheckPrefix.size()+6);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000634 IsCheckNext = true;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000635 } else if (Buffer.size() > CheckPrefix.size()+5 &&
636 memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) {
Benjamin Kramer30ce40e2012-09-18 20:51:39 +0000637 Buffer = Buffer.substr(CheckPrefix.size()+5);
Chris Lattnerf15380b2009-09-20 22:35:26 +0000638 IsCheckNot = true;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000639 } else {
Chris Lattner96077032009-09-20 22:11:44 +0000640 Buffer = Buffer.substr(1);
Chris Lattnerd7e25052009-08-15 18:00:42 +0000641 continue;
642 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000643
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000644 // Okay, we found the prefix, yay. Remember the rest of the line, but
645 // ignore leading and trailing whitespace.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000646 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000647
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000648 // Scan ahead to the end of line.
Chris Lattner96077032009-09-20 22:11:44 +0000649 size_t EOL = Buffer.find_first_of("\n\r");
Chris Lattnera29703e2009-09-24 20:39:13 +0000650
Dan Gohmane5463432010-01-29 21:53:18 +0000651 // Remember the location of the start of the pattern, for diagnostics.
652 SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
653
Chris Lattnera29703e2009-09-24 20:39:13 +0000654 // Parse the pattern.
655 Pattern P;
Alexander Kornienko70a870a2012-11-14 21:07:37 +0000656 if (P.ParsePattern(Buffer.substr(0, EOL), SM, LineNumber))
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000657 return true;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000658
Chris Lattnera29703e2009-09-24 20:39:13 +0000659 Buffer = Buffer.substr(EOL);
660
Chris Lattner5dafafd2009-08-15 18:32:21 +0000661 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
662 if (IsCheckNext && CheckStrings.empty()) {
663 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000664 SourceMgr::DK_Error,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000665 "found '"+CheckPrefix+"-NEXT:' without previous '"+
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000666 CheckPrefix+ ": line");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000667 return true;
668 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000669
Chris Lattnera29703e2009-09-24 20:39:13 +0000670 // Handle CHECK-NOT.
671 if (IsCheckNot) {
672 NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
673 P));
674 continue;
675 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000676
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000677 // Okay, add the string we captured to the output vector and move on.
Chris Lattner9fc66782009-09-24 20:25:55 +0000678 CheckStrings.push_back(CheckString(P,
Dan Gohmane5463432010-01-29 21:53:18 +0000679 PatternLoc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000680 IsCheckNext));
Chris Lattnerf15380b2009-09-20 22:35:26 +0000681 std::swap(NotMatches, CheckStrings.back().NotStrings);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000682 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000683
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000684 // Add an EOF pattern for any trailing CHECK-NOTs.
685 if (!NotMatches.empty()) {
686 CheckStrings.push_back(CheckString(Pattern(true),
687 SMLoc::getFromPointer(Buffer.data()),
688 false));
689 std::swap(NotMatches, CheckStrings.back().NotStrings);
690 }
691
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000692 if (CheckStrings.empty()) {
Chris Lattnerd7e25052009-08-15 18:00:42 +0000693 errs() << "error: no check strings found with prefix '" << CheckPrefix
694 << ":'\n";
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000695 return true;
696 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000697
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000698 return false;
699}
700
Chris Lattner5dafafd2009-08-15 18:32:21 +0000701static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000702 StringRef Buffer,
703 StringMap<StringRef> &VariableTable) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000704 // Otherwise, we have an error, emit an error message.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000705 SM.PrintMessage(CheckStr.Loc, SourceMgr::DK_Error,
706 "expected string not found in input");
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000707
Chris Lattner5dafafd2009-08-15 18:32:21 +0000708 // Print the "scanning from here" line. If the current position is at the
709 // end of a line, advance to the start of the next line.
Chris Lattner96077032009-09-20 22:11:44 +0000710 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000711
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000712 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
713 "scanning from here");
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000714
715 // Allow the pattern to print additional information if desired.
716 CheckStr.Pat.PrintFailureInfo(SM, Buffer, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000717}
718
Chris Lattner3711b7a2009-09-20 22:42:44 +0000719/// CountNumNewlinesBetween - Count the number of newlines in the specified
720/// range.
721static unsigned CountNumNewlinesBetween(StringRef Range) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000722 unsigned NumNewLines = 0;
Chris Lattner3711b7a2009-09-20 22:42:44 +0000723 while (1) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000724 // Scan for newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000725 Range = Range.substr(Range.find_first_of("\n\r"));
726 if (Range.empty()) return NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000727
Chris Lattner5dafafd2009-08-15 18:32:21 +0000728 ++NumNewLines;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000729
Chris Lattner5dafafd2009-08-15 18:32:21 +0000730 // Handle \n\r and \r\n as a single newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000731 if (Range.size() > 1 &&
732 (Range[1] == '\n' || Range[1] == '\r') &&
733 (Range[0] != Range[1]))
734 Range = Range.substr(1);
735 Range = Range.substr(1);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000736 }
Chris Lattner5dafafd2009-08-15 18:32:21 +0000737}
738
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000739int main(int argc, char **argv) {
740 sys::PrintStackTraceOnErrorSignal();
741 PrettyStackTraceProgram X(argc, argv);
742 cl::ParseCommandLineOptions(argc, argv);
743
744 SourceMgr SM;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000745
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000746 // Read the expected strings from the check file.
Chris Lattner207e1bc2009-08-15 17:41:04 +0000747 std::vector<CheckString> CheckStrings;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000748 if (ReadCheckFile(SM, CheckStrings))
749 return 2;
750
751 // Open the file to check and add it to SourceMgr.
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000752 OwningPtr<MemoryBuffer> File;
753 if (error_code ec =
754 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000755 errs() << "Could not open input file '" << InputFilename << "': "
Michael J. Spencer333fb042010-12-09 17:36:48 +0000756 << ec.message() << '\n';
Eli Bendersky7f8e76f2012-11-30 13:51:33 +0000757 return 2;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000758 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000759 MemoryBuffer *F = File.take();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000760
Chris Lattner1aac1862011-02-09 16:46:02 +0000761 if (F->getBufferSize() == 0) {
762 errs() << "FileCheck error: '" << InputFilename << "' is empty.\n";
Eli Bendersky7f8e76f2012-11-30 13:51:33 +0000763 return 2;
Chris Lattner1aac1862011-02-09 16:46:02 +0000764 }
765
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000766 // Remove duplicate spaces in the input file if requested.
767 if (!NoCanonicalizeWhiteSpace)
768 F = CanonicalizeInputFile(F);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000769
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000770 SM.AddNewSourceBuffer(F, SMLoc());
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000771
Chris Lattnereec96952009-09-27 07:56:52 +0000772 /// VariableTable - This holds all the current filecheck variables.
773 StringMap<StringRef> VariableTable;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000774
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000775 // Check that we have all of the expected strings, in order, in the input
776 // file.
Chris Lattner96077032009-09-20 22:11:44 +0000777 StringRef Buffer = F->getBuffer();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000778
Chris Lattnerf15380b2009-09-20 22:35:26 +0000779 const char *LastMatch = Buffer.data();
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000780
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000781 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
Chris Lattner207e1bc2009-08-15 17:41:04 +0000782 const CheckString &CheckStr = CheckStrings[StrNo];
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000783
Chris Lattner96077032009-09-20 22:11:44 +0000784 StringRef SearchFrom = Buffer;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000785
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000786 // Find StrNo in the file.
Chris Lattner9fc66782009-09-24 20:25:55 +0000787 size_t MatchLen = 0;
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000788 size_t MatchPos = CheckStr.Pat.Match(Buffer, MatchLen, VariableTable);
789 Buffer = Buffer.substr(MatchPos);
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000790
Chris Lattner5dafafd2009-08-15 18:32:21 +0000791 // If we didn't find a match, reject the input.
Jakob Stoklund Olesen824c10e2010-10-15 17:47:12 +0000792 if (MatchPos == StringRef::npos) {
Daniel Dunbarfafe93c2009-11-22 22:08:06 +0000793 PrintCheckFailed(SM, CheckStr, SearchFrom, VariableTable);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000794 return 1;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000795 }
Chris Lattner3711b7a2009-09-20 22:42:44 +0000796
797 StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
798
Chris Lattner5dafafd2009-08-15 18:32:21 +0000799 // If this check is a "CHECK-NEXT", verify that the previous match was on
800 // the previous line (i.e. that there is one newline between them).
801 if (CheckStr.IsCheckNext) {
802 // Count the number of newlines between the previous match and this one.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000803 assert(LastMatch != F->getBufferStart() &&
804 "CHECK-NEXT can't be the first check in a file");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000805
Chris Lattner3711b7a2009-09-20 22:42:44 +0000806 unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000807 if (NumNewLines == 0) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000808 SM.PrintMessage(CheckStr.Loc, SourceMgr::DK_Error,
809 CheckPrefix+"-NEXT: is on the same line as previous match");
Chris Lattner96077032009-09-20 22:11:44 +0000810 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000811 SourceMgr::DK_Note, "'next' match was here");
812 SM.PrintMessage(SMLoc::getFromPointer(LastMatch), SourceMgr::DK_Note,
813 "previous match was here");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000814 return 1;
815 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000816
Chris Lattner5dafafd2009-08-15 18:32:21 +0000817 if (NumNewLines != 1) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000818 SM.PrintMessage(CheckStr.Loc, SourceMgr::DK_Error, CheckPrefix+
819 "-NEXT: is not on the line after the previous match");
Chris Lattner96077032009-09-20 22:11:44 +0000820 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000821 SourceMgr::DK_Note, "'next' match was here");
822 SM.PrintMessage(SMLoc::getFromPointer(LastMatch), SourceMgr::DK_Note,
823 "previous match was here");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000824 return 1;
825 }
826 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000827
Chris Lattnerf15380b2009-09-20 22:35:26 +0000828 // If this match had "not strings", verify that they don't exist in the
829 // skipped region.
Chris Lattnereec96952009-09-27 07:56:52 +0000830 for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size();
831 ChunkNo != e; ++ChunkNo) {
Chris Lattnera29703e2009-09-24 20:39:13 +0000832 size_t MatchLen = 0;
Chris Lattnereec96952009-09-27 07:56:52 +0000833 size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion,
834 MatchLen,
835 VariableTable);
Chris Lattnerf15380b2009-09-20 22:35:26 +0000836 if (Pos == StringRef::npos) continue;
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000837
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000838 SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos), SourceMgr::DK_Error,
839 CheckPrefix+"-NOT: string occurred!");
840 SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first, SourceMgr::DK_Note,
841 CheckPrefix+"-NOT: pattern specified here");
Chris Lattnerf15380b2009-09-20 22:35:26 +0000842 return 1;
843 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000844
Chris Lattner5dafafd2009-08-15 18:32:21 +0000845
Chris Lattner81115762009-09-21 02:30:42 +0000846 // Otherwise, everything is good. Step over the matched text and remember
847 // the position after the match as the end of the last match.
Chris Lattner9fc66782009-09-24 20:25:55 +0000848 Buffer = Buffer.substr(MatchLen);
Chris Lattner81115762009-09-21 02:30:42 +0000849 LastMatch = Buffer.data();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000850 }
Mikhail Glushenkov7112c862010-08-20 17:38:38 +0000851
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000852 return 0;
853}