blob: 1e6af371d8e8c33782f342ea0c99730d18e88b45 [file] [log] [blame]
Chris Lattner81cb8ca2009-07-08 18:44:05 +00001//===- FileCheck.cpp - Check that File's Contents match what is expected --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// FileCheck does a line-by line check of a file that validates whether it
11// contains the expected content. This is useful for regression tests etc.
12//
13// This program exits with an error status of 2 on error, exit status of 0 if
14// the file matched the expected contents, and exit status of 1 if it did not
15// contain the expected contents.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner52870082009-09-24 21:47:32 +000022#include "llvm/Support/Regex.h"
Chris Lattner81cb8ca2009-07-08 18:44:05 +000023#include "llvm/Support/SourceMgr.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/System/Signals.h"
26using namespace llvm;
27
28static cl::opt<std::string>
29CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
30
31static cl::opt<std::string>
32InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
33 cl::init("-"), cl::value_desc("filename"));
34
35static cl::opt<std::string>
36CheckPrefix("check-prefix", cl::init("CHECK"),
37 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
38
Chris Lattner88a7e9e2009-07-11 18:58:15 +000039static cl::opt<bool>
40NoCanonicalizeWhiteSpace("strict-whitespace",
41 cl::desc("Do not treat all horizontal whitespace as equivalent"));
42
Chris Lattnera29703e2009-09-24 20:39:13 +000043//===----------------------------------------------------------------------===//
44// Pattern Handling Code.
45//===----------------------------------------------------------------------===//
46
Chris Lattner9fc66782009-09-24 20:25:55 +000047class Pattern {
Chris Lattner5d6a05f2009-09-25 17:23:43 +000048 /// FixedStr - If non-empty, this pattern is a fixed string match with the
49 /// specified fixed string.
Chris Lattner2702e6a2009-09-25 17:09:12 +000050 StringRef FixedStr;
Chris Lattner5d6a05f2009-09-25 17:23:43 +000051
52 /// RegEx - If non-empty, this is a regex pattern.
53 std::string RegExStr;
Chris Lattner9fc66782009-09-24 20:25:55 +000054public:
55
Chris Lattnera29703e2009-09-24 20:39:13 +000056 Pattern() { }
57
58 bool ParsePattern(StringRef PatternStr, SourceMgr &SM);
Chris Lattner9fc66782009-09-24 20:25:55 +000059
60 /// Match - Match the pattern string against the input buffer Buffer. This
61 /// returns the position that is matched or npos if there is no match. If
62 /// there is a match, the size of the matched string is returned in MatchLen.
Chris Lattner52870082009-09-24 21:47:32 +000063 size_t Match(StringRef Buffer, size_t &MatchLen) const;
Chris Lattner5d6a05f2009-09-25 17:23:43 +000064
65private:
66 void AddFixedStringToRegEx(StringRef FixedStr);
Chris Lattner9fc66782009-09-24 20:25:55 +000067};
68
Chris Lattnera29703e2009-09-24 20:39:13 +000069bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) {
70 // Ignore trailing whitespace.
71 while (!PatternStr.empty() &&
72 (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
73 PatternStr = PatternStr.substr(0, PatternStr.size()-1);
74
75 // Check that there is something on the line.
76 if (PatternStr.empty()) {
77 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
78 "found empty check string with prefix '"+CheckPrefix+":'",
79 "error");
80 return true;
81 }
Chris Lattner52870082009-09-24 21:47:32 +000082
Chris Lattner2702e6a2009-09-25 17:09:12 +000083 // Check to see if this is a fixed string, or if it has regex pieces.
84 if (PatternStr.size() < 2 || PatternStr.find("{{") == StringRef::npos) {
85 FixedStr = PatternStr;
86 return false;
87 }
88
Chris Lattner5d6a05f2009-09-25 17:23:43 +000089 // Otherwise, there is at least one regex piece. Build up the regex pattern
90 // by escaping scary characters in fixed strings, building up one big regex.
Chris Lattner52870082009-09-24 21:47:32 +000091 while (!PatternStr.empty()) {
92 // Handle fixed string matches.
93 if (PatternStr.size() < 2 ||
94 PatternStr[0] != '{' || PatternStr[1] != '{') {
95 // Find the end, which is the start of the next regex.
96 size_t FixedMatchEnd = PatternStr.find("{{");
Chris Lattner5d6a05f2009-09-25 17:23:43 +000097 AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd));
Chris Lattner52870082009-09-24 21:47:32 +000098 PatternStr = PatternStr.substr(FixedMatchEnd);
99 continue;
100 }
101
102 // Otherwise, this is the start of a regex match. Scan for the }}.
103 size_t End = PatternStr.find("}}");
104 if (End == StringRef::npos) {
105 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
106 "found start of regex string with no end '}}'", "error");
107 return true;
108 }
109
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000110 StringRef RegexStr = PatternStr.substr(2, End-2);
111 Regex R(RegexStr);
Chris Lattner52870082009-09-24 21:47:32 +0000112 std::string Error;
113 if (!R.isValid(Error)) {
114 SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()+2),
115 "invalid regex: " + Error, "error");
116 return true;
117 }
118
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000119 RegExStr += RegexStr.str();
Chris Lattner52870082009-09-24 21:47:32 +0000120 PatternStr = PatternStr.substr(End+2);
121 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000122
Chris Lattnera29703e2009-09-24 20:39:13 +0000123 return false;
124}
125
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000126void Pattern::AddFixedStringToRegEx(StringRef FixedStr) {
127 // Add the characters from FixedStr to the regex, escaping as needed. This
128 // avoids "leaning toothpicks" in common patterns.
129 for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) {
130 switch (FixedStr[i]) {
131 // These are the special characters matched in "p_ere_exp".
132 case '(':
133 case ')':
134 case '^':
135 case '$':
136 case '|':
137 case '*':
138 case '+':
139 case '?':
140 case '.':
141 case '[':
142 case '\\':
143 case '{':
144 RegExStr += '\\';
145 // FALL THROUGH.
146 default:
147 RegExStr += FixedStr[i];
148 break;
149 }
150 }
151}
152
153
Chris Lattner52870082009-09-24 21:47:32 +0000154/// Match - Match the pattern string against the input buffer Buffer. This
155/// returns the position that is matched or npos if there is no match. If
156/// there is a match, the size of the matched string is returned in MatchLen.
157size_t Pattern::Match(StringRef Buffer, size_t &MatchLen) const {
Chris Lattner2702e6a2009-09-25 17:09:12 +0000158 // If this is a fixed string pattern, just match it now.
159 if (!FixedStr.empty()) {
160 MatchLen = FixedStr.size();
161 return Buffer.find(FixedStr);
162 }
163
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000164 // Regex match.
165 SmallVector<StringRef, 4> MatchInfo;
166 if (!Regex(RegExStr, Regex::Sub|Regex::Newline).match(Buffer, &MatchInfo))
167 return StringRef::npos;
Chris Lattner52870082009-09-24 21:47:32 +0000168
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000169 // Successful regex match.
170 assert(!MatchInfo.empty() && "Didn't get any match");
171 StringRef FullMatch = MatchInfo[0];
Chris Lattner52870082009-09-24 21:47:32 +0000172
Chris Lattner5d6a05f2009-09-25 17:23:43 +0000173 MatchLen = FullMatch.size();
174 return FullMatch.data()-Buffer.data();
Chris Lattner52870082009-09-24 21:47:32 +0000175}
176
Chris Lattnera29703e2009-09-24 20:39:13 +0000177
178//===----------------------------------------------------------------------===//
179// Check Strings.
180//===----------------------------------------------------------------------===//
Chris Lattner9fc66782009-09-24 20:25:55 +0000181
182/// CheckString - This is a check that we found in the input file.
183struct CheckString {
184 /// Pat - The pattern to match.
185 Pattern Pat;
Chris Lattner207e1bc2009-08-15 17:41:04 +0000186
187 /// Loc - The location in the match file that the check string was specified.
188 SMLoc Loc;
189
Chris Lattner5dafafd2009-08-15 18:32:21 +0000190 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
191 /// to a CHECK: directive.
192 bool IsCheckNext;
193
Chris Lattnerf15380b2009-09-20 22:35:26 +0000194 /// NotStrings - These are all of the strings that are disallowed from
195 /// occurring between this match string and the previous one (or start of
196 /// file).
Chris Lattnera29703e2009-09-24 20:39:13 +0000197 std::vector<std::pair<SMLoc, Pattern> > NotStrings;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000198
Chris Lattner9fc66782009-09-24 20:25:55 +0000199 CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
200 : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
Chris Lattner207e1bc2009-08-15 17:41:04 +0000201};
202
Chris Lattneradea46e2009-09-24 20:45:07 +0000203/// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
204/// memory buffer, free it, and return a new one.
205static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
206 SmallVector<char, 16> NewFile;
207 NewFile.reserve(MB->getBufferSize());
208
209 for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
210 Ptr != End; ++Ptr) {
211 // If C is not a horizontal whitespace, skip it.
212 if (*Ptr != ' ' && *Ptr != '\t') {
213 NewFile.push_back(*Ptr);
214 continue;
215 }
216
217 // Otherwise, add one space and advance over neighboring space.
218 NewFile.push_back(' ');
219 while (Ptr+1 != End &&
220 (Ptr[1] == ' ' || Ptr[1] == '\t'))
221 ++Ptr;
222 }
223
224 // Free the old buffer and return a new one.
225 MemoryBuffer *MB2 =
226 MemoryBuffer::getMemBufferCopy(NewFile.data(),
227 NewFile.data() + NewFile.size(),
228 MB->getBufferIdentifier());
229
230 delete MB;
231 return MB2;
232}
233
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000234
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000235/// ReadCheckFile - Read the check file, which specifies the sequence of
236/// expected strings. The strings are added to the CheckStrings vector.
237static bool ReadCheckFile(SourceMgr &SM,
Chris Lattner207e1bc2009-08-15 17:41:04 +0000238 std::vector<CheckString> &CheckStrings) {
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000239 // Open the check file, and tell SourceMgr about it.
240 std::string ErrorStr;
241 MemoryBuffer *F =
242 MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr);
243 if (F == 0) {
244 errs() << "Could not open check file '" << CheckFilename << "': "
245 << ErrorStr << '\n';
246 return true;
247 }
Chris Lattneradea46e2009-09-24 20:45:07 +0000248
249 // If we want to canonicalize whitespace, strip excess whitespace from the
250 // buffer containing the CHECK lines.
251 if (!NoCanonicalizeWhiteSpace)
252 F = CanonicalizeInputFile(F);
253
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000254 SM.AddNewSourceBuffer(F, SMLoc());
255
Chris Lattnerd7e25052009-08-15 18:00:42 +0000256 // Find all instances of CheckPrefix followed by : in the file.
Chris Lattner96077032009-09-20 22:11:44 +0000257 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000258
Chris Lattnera29703e2009-09-24 20:39:13 +0000259 std::vector<std::pair<SMLoc, Pattern> > NotMatches;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000260
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000261 while (1) {
262 // See if Prefix occurs in the memory buffer.
Chris Lattner96077032009-09-20 22:11:44 +0000263 Buffer = Buffer.substr(Buffer.find(CheckPrefix));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000264
265 // If we didn't find a match, we're done.
Chris Lattner96077032009-09-20 22:11:44 +0000266 if (Buffer.empty())
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000267 break;
268
Chris Lattner96077032009-09-20 22:11:44 +0000269 const char *CheckPrefixStart = Buffer.data();
Chris Lattner5dafafd2009-08-15 18:32:21 +0000270
271 // When we find a check prefix, keep track of whether we find CHECK: or
272 // CHECK-NEXT:
Chris Lattnerf15380b2009-09-20 22:35:26 +0000273 bool IsCheckNext = false, IsCheckNot = false;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000274
Chris Lattnerd7e25052009-08-15 18:00:42 +0000275 // Verify that the : is present after the prefix.
Chris Lattner96077032009-09-20 22:11:44 +0000276 if (Buffer[CheckPrefix.size()] == ':') {
277 Buffer = Buffer.substr(CheckPrefix.size()+1);
Chris Lattner96077032009-09-20 22:11:44 +0000278 } else if (Buffer.size() > CheckPrefix.size()+6 &&
279 memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
280 Buffer = Buffer.substr(CheckPrefix.size()+7);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000281 IsCheckNext = true;
Chris Lattnerf15380b2009-09-20 22:35:26 +0000282 } else if (Buffer.size() > CheckPrefix.size()+5 &&
283 memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) {
284 Buffer = Buffer.substr(CheckPrefix.size()+6);
285 IsCheckNot = true;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000286 } else {
Chris Lattner96077032009-09-20 22:11:44 +0000287 Buffer = Buffer.substr(1);
Chris Lattnerd7e25052009-08-15 18:00:42 +0000288 continue;
289 }
290
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000291 // Okay, we found the prefix, yay. Remember the rest of the line, but
292 // ignore leading and trailing whitespace.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000293 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000294
295 // Scan ahead to the end of line.
Chris Lattner96077032009-09-20 22:11:44 +0000296 size_t EOL = Buffer.find_first_of("\n\r");
Chris Lattnera29703e2009-09-24 20:39:13 +0000297
298 // Parse the pattern.
299 Pattern P;
300 if (P.ParsePattern(Buffer.substr(0, EOL), SM))
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000301 return true;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000302
Chris Lattnera29703e2009-09-24 20:39:13 +0000303 Buffer = Buffer.substr(EOL);
304
Chris Lattnerf15380b2009-09-20 22:35:26 +0000305
Chris Lattner5dafafd2009-08-15 18:32:21 +0000306 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
307 if (IsCheckNext && CheckStrings.empty()) {
308 SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
309 "found '"+CheckPrefix+"-NEXT:' without previous '"+
310 CheckPrefix+ ": line", "error");
311 return true;
312 }
313
Chris Lattnera29703e2009-09-24 20:39:13 +0000314 // Handle CHECK-NOT.
315 if (IsCheckNot) {
316 NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
317 P));
318 continue;
319 }
320
Chris Lattner9fc66782009-09-24 20:25:55 +0000321
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000322 // Okay, add the string we captured to the output vector and move on.
Chris Lattner9fc66782009-09-24 20:25:55 +0000323 CheckStrings.push_back(CheckString(P,
Chris Lattner96077032009-09-20 22:11:44 +0000324 SMLoc::getFromPointer(Buffer.data()),
Chris Lattner5dafafd2009-08-15 18:32:21 +0000325 IsCheckNext));
Chris Lattnerf15380b2009-09-20 22:35:26 +0000326 std::swap(NotMatches, CheckStrings.back().NotStrings);
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000327 }
328
329 if (CheckStrings.empty()) {
Chris Lattnerd7e25052009-08-15 18:00:42 +0000330 errs() << "error: no check strings found with prefix '" << CheckPrefix
331 << ":'\n";
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000332 return true;
333 }
334
Chris Lattnerf15380b2009-09-20 22:35:26 +0000335 if (!NotMatches.empty()) {
336 errs() << "error: '" << CheckPrefix
337 << "-NOT:' not supported after last check line.\n";
338 return true;
339 }
340
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000341 return false;
342}
343
Chris Lattner5dafafd2009-08-15 18:32:21 +0000344static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
Chris Lattner96077032009-09-20 22:11:44 +0000345 StringRef Buffer) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000346 // Otherwise, we have an error, emit an error message.
347 SM.PrintMessage(CheckStr.Loc, "expected string not found in input",
348 "error");
349
350 // Print the "scanning from here" line. If the current position is at the
351 // end of a line, advance to the start of the next line.
Chris Lattner96077032009-09-20 22:11:44 +0000352 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
Chris Lattner5dafafd2009-08-15 18:32:21 +0000353
Chris Lattner96077032009-09-20 22:11:44 +0000354 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here",
Chris Lattner5dafafd2009-08-15 18:32:21 +0000355 "note");
356}
357
Chris Lattner3711b7a2009-09-20 22:42:44 +0000358/// CountNumNewlinesBetween - Count the number of newlines in the specified
359/// range.
360static unsigned CountNumNewlinesBetween(StringRef Range) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000361 unsigned NumNewLines = 0;
Chris Lattner3711b7a2009-09-20 22:42:44 +0000362 while (1) {
Chris Lattner5dafafd2009-08-15 18:32:21 +0000363 // Scan for newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000364 Range = Range.substr(Range.find_first_of("\n\r"));
365 if (Range.empty()) return NumNewLines;
Chris Lattner5dafafd2009-08-15 18:32:21 +0000366
367 ++NumNewLines;
368
369 // Handle \n\r and \r\n as a single newline.
Chris Lattner3711b7a2009-09-20 22:42:44 +0000370 if (Range.size() > 1 &&
371 (Range[1] == '\n' || Range[1] == '\r') &&
372 (Range[0] != Range[1]))
373 Range = Range.substr(1);
374 Range = Range.substr(1);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000375 }
Chris Lattner5dafafd2009-08-15 18:32:21 +0000376}
377
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000378int main(int argc, char **argv) {
379 sys::PrintStackTraceOnErrorSignal();
380 PrettyStackTraceProgram X(argc, argv);
381 cl::ParseCommandLineOptions(argc, argv);
382
383 SourceMgr SM;
384
385 // Read the expected strings from the check file.
Chris Lattner207e1bc2009-08-15 17:41:04 +0000386 std::vector<CheckString> CheckStrings;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000387 if (ReadCheckFile(SM, CheckStrings))
388 return 2;
389
390 // Open the file to check and add it to SourceMgr.
391 std::string ErrorStr;
392 MemoryBuffer *F =
393 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
394 if (F == 0) {
395 errs() << "Could not open input file '" << InputFilename << "': "
396 << ErrorStr << '\n';
397 return true;
398 }
Chris Lattner88a7e9e2009-07-11 18:58:15 +0000399
400 // Remove duplicate spaces in the input file if requested.
401 if (!NoCanonicalizeWhiteSpace)
402 F = CanonicalizeInputFile(F);
403
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000404 SM.AddNewSourceBuffer(F, SMLoc());
405
406 // Check that we have all of the expected strings, in order, in the input
407 // file.
Chris Lattner96077032009-09-20 22:11:44 +0000408 StringRef Buffer = F->getBuffer();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000409
Chris Lattnerf15380b2009-09-20 22:35:26 +0000410 const char *LastMatch = Buffer.data();
411
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000412 for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
Chris Lattner207e1bc2009-08-15 17:41:04 +0000413 const CheckString &CheckStr = CheckStrings[StrNo];
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000414
Chris Lattner96077032009-09-20 22:11:44 +0000415 StringRef SearchFrom = Buffer;
416
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000417 // Find StrNo in the file.
Chris Lattner9fc66782009-09-24 20:25:55 +0000418 size_t MatchLen = 0;
419 Buffer = Buffer.substr(CheckStr.Pat.Match(Buffer, MatchLen));
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000420
Chris Lattner5dafafd2009-08-15 18:32:21 +0000421 // If we didn't find a match, reject the input.
Chris Lattner96077032009-09-20 22:11:44 +0000422 if (Buffer.empty()) {
423 PrintCheckFailed(SM, CheckStr, SearchFrom);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000424 return 1;
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000425 }
Chris Lattner3711b7a2009-09-20 22:42:44 +0000426
427 StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
428
Chris Lattner5dafafd2009-08-15 18:32:21 +0000429 // If this check is a "CHECK-NEXT", verify that the previous match was on
430 // the previous line (i.e. that there is one newline between them).
431 if (CheckStr.IsCheckNext) {
432 // Count the number of newlines between the previous match and this one.
Chris Lattnerf15380b2009-09-20 22:35:26 +0000433 assert(LastMatch != F->getBufferStart() &&
434 "CHECK-NEXT can't be the first check in a file");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000435
Chris Lattner3711b7a2009-09-20 22:42:44 +0000436 unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
Chris Lattner5dafafd2009-08-15 18:32:21 +0000437 if (NumNewLines == 0) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000438 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000439 CheckPrefix+"-NEXT: is on the same line as previous match",
440 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000441 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000442 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000443 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
444 "previous match was here", "note");
445 return 1;
446 }
447
448 if (NumNewLines != 1) {
Chris Lattner0b2353f2009-08-16 02:22:31 +0000449 SM.PrintMessage(CheckStr.Loc,
Chris Lattner5dafafd2009-08-15 18:32:21 +0000450 CheckPrefix+
451 "-NEXT: is not on the line after the previous match",
452 "error");
Chris Lattner96077032009-09-20 22:11:44 +0000453 SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
Chris Lattner0b2353f2009-08-16 02:22:31 +0000454 "'next' match was here", "note");
Chris Lattner5dafafd2009-08-15 18:32:21 +0000455 SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
456 "previous match was here", "note");
457 return 1;
458 }
459 }
Chris Lattnerf15380b2009-09-20 22:35:26 +0000460
461 // If this match had "not strings", verify that they don't exist in the
462 // skipped region.
Chris Lattner52870082009-09-24 21:47:32 +0000463 for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size(); ChunkNo != e; ++ChunkNo) {
Chris Lattnera29703e2009-09-24 20:39:13 +0000464 size_t MatchLen = 0;
Chris Lattner52870082009-09-24 21:47:32 +0000465 size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion, MatchLen);
Chris Lattnerf15380b2009-09-20 22:35:26 +0000466 if (Pos == StringRef::npos) continue;
467
468 SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos),
469 CheckPrefix+"-NOT: string occurred!", "error");
Chris Lattner52870082009-09-24 21:47:32 +0000470 SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first,
Chris Lattnerf15380b2009-09-20 22:35:26 +0000471 CheckPrefix+"-NOT: pattern specified here", "note");
472 return 1;
473 }
474
Chris Lattner5dafafd2009-08-15 18:32:21 +0000475
Chris Lattner81115762009-09-21 02:30:42 +0000476 // Otherwise, everything is good. Step over the matched text and remember
477 // the position after the match as the end of the last match.
Chris Lattner9fc66782009-09-24 20:25:55 +0000478 Buffer = Buffer.substr(MatchLen);
Chris Lattner81115762009-09-21 02:30:42 +0000479 LastMatch = Buffer.data();
Chris Lattner81cb8ca2009-07-08 18:44:05 +0000480 }
481
482 return 0;
483}