FileCheck: Switch "possible match" calculation to use StringRef::edit_distance.
[oota-llvm.git] / utils / FileCheck / FileCheck.cpp
1 //===- 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"
22 #include "llvm/Support/Regex.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/System/Signals.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/StringMap.h"
28 #include <algorithm>
29 using namespace llvm;
30
31 static cl::opt<std::string>
32 CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
33
34 static cl::opt<std::string>
35 InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
36               cl::init("-"), cl::value_desc("filename"));
37
38 static cl::opt<std::string>
39 CheckPrefix("check-prefix", cl::init("CHECK"),
40             cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
41
42 static cl::opt<bool>
43 NoCanonicalizeWhiteSpace("strict-whitespace",
44               cl::desc("Do not treat all horizontal whitespace as equivalent"));
45
46 //===----------------------------------------------------------------------===//
47 // Pattern Handling Code.
48 //===----------------------------------------------------------------------===//
49
50 class Pattern {
51   SMLoc PatternLoc;
52   
53   /// FixedStr - If non-empty, this pattern is a fixed string match with the
54   /// specified fixed string.
55   StringRef FixedStr;
56   
57   /// RegEx - If non-empty, this is a regex pattern.
58   std::string RegExStr;
59   
60   /// VariableUses - Entries in this vector map to uses of a variable in the
61   /// pattern, e.g. "foo[[bar]]baz".  In this case, the RegExStr will contain
62   /// "foobaz" and we'll get an entry in this vector that tells us to insert the
63   /// value of bar at offset 3.
64   std::vector<std::pair<StringRef, unsigned> > VariableUses;
65   
66   /// VariableDefs - Entries in this vector map to definitions of a variable in
67   /// the pattern, e.g. "foo[[bar:.*]]baz".  In this case, the RegExStr will
68   /// contain "foo(.*)baz" and VariableDefs will contain the pair "bar",1.  The
69   /// index indicates what parenthesized value captures the variable value.
70   std::vector<std::pair<StringRef, unsigned> > VariableDefs;
71   
72 public:
73   
74   Pattern() { }
75   
76   bool ParsePattern(StringRef PatternStr, SourceMgr &SM);
77   
78   /// Match - Match the pattern string against the input buffer Buffer.  This
79   /// returns the position that is matched or npos if there is no match.  If
80   /// there is a match, the size of the matched string is returned in MatchLen.
81   ///
82   /// The VariableTable StringMap provides the current values of filecheck
83   /// variables and is updated if this match defines new values.
84   size_t Match(StringRef Buffer, size_t &MatchLen,
85                StringMap<StringRef> &VariableTable) const;
86
87   /// PrintFailureInfo - Print additional information about a failure to match
88   /// involving this pattern.
89   void PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
90                         const StringMap<StringRef> &VariableTable) const;
91
92 private:
93   static void AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr);
94   bool AddRegExToRegEx(StringRef RegExStr, unsigned &CurParen, SourceMgr &SM);
95
96   /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of
97   /// matching this pattern at the start of \arg Buffer; a distance of zero
98   /// should correspond to a perfect match.
99   unsigned ComputeMatchDistance(StringRef Buffer,
100                                const StringMap<StringRef> &VariableTable) const;
101 };
102
103
104 bool Pattern::ParsePattern(StringRef PatternStr, SourceMgr &SM) {
105   PatternLoc = SMLoc::getFromPointer(PatternStr.data());
106   
107   // Ignore trailing whitespace.
108   while (!PatternStr.empty() &&
109          (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
110     PatternStr = PatternStr.substr(0, PatternStr.size()-1);
111   
112   // Check that there is something on the line.
113   if (PatternStr.empty()) {
114     SM.PrintMessage(PatternLoc, "found empty check string with prefix '" +
115                     CheckPrefix+":'", "error");
116     return true;
117   }
118   
119   // Check to see if this is a fixed string, or if it has regex pieces.
120   if (PatternStr.size() < 2 ||
121       (PatternStr.find("{{") == StringRef::npos &&
122        PatternStr.find("[[") == StringRef::npos)) {
123     FixedStr = PatternStr;
124     return false;
125   }
126   
127   // Paren value #0 is for the fully matched string.  Any new parenthesized
128   // values add from their.
129   unsigned CurParen = 1;
130   
131   // Otherwise, there is at least one regex piece.  Build up the regex pattern
132   // by escaping scary characters in fixed strings, building up one big regex.
133   while (!PatternStr.empty()) {
134     // RegEx matches.
135     if (PatternStr.size() >= 2 &&
136         PatternStr[0] == '{' && PatternStr[1] == '{') {
137      
138       // Otherwise, this is the start of a regex match.  Scan for the }}.
139       size_t End = PatternStr.find("}}");
140       if (End == StringRef::npos) {
141         SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
142                         "found start of regex string with no end '}}'", "error");
143         return true;
144       }
145       
146       if (AddRegExToRegEx(PatternStr.substr(2, End-2), CurParen, SM))
147         return true;
148       PatternStr = PatternStr.substr(End+2);
149       continue;
150     }
151     
152     // Named RegEx matches.  These are of two forms: [[foo:.*]] which matches .*
153     // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
154     // second form is [[foo]] which is a reference to foo.  The variable name
155     // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
156     // it.  This is to catch some common errors.
157     if (PatternStr.size() >= 2 &&
158         PatternStr[0] == '[' && PatternStr[1] == '[') {
159       // Verify that it is terminated properly.
160       size_t End = PatternStr.find("]]");
161       if (End == StringRef::npos) {
162         SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
163                         "invalid named regex reference, no ]] found", "error");
164         return true;
165       }
166       
167       StringRef MatchStr = PatternStr.substr(2, End-2);
168       PatternStr = PatternStr.substr(End+2);
169       
170       // Get the regex name (e.g. "foo").
171       size_t NameEnd = MatchStr.find(':');
172       StringRef Name = MatchStr.substr(0, NameEnd);
173       
174       if (Name.empty()) {
175         SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
176                         "invalid name in named regex: empty name", "error");
177         return true;
178       }
179
180       // Verify that the name is well formed.
181       for (unsigned i = 0, e = Name.size(); i != e; ++i)
182         if (Name[i] != '_' &&
183             (Name[i] < 'a' || Name[i] > 'z') &&
184             (Name[i] < 'A' || Name[i] > 'Z') &&
185             (Name[i] < '0' || Name[i] > '9')) {
186           SM.PrintMessage(SMLoc::getFromPointer(Name.data()+i),
187                           "invalid name in named regex", "error");
188           return true;
189         }
190       
191       // Name can't start with a digit.
192       if (isdigit(Name[0])) {
193         SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
194                         "invalid name in named regex", "error");
195         return true;
196       }
197       
198       // Handle [[foo]].
199       if (NameEnd == StringRef::npos) {
200         VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
201         continue;
202       }
203       
204       // Handle [[foo:.*]].
205       VariableDefs.push_back(std::make_pair(Name, CurParen));
206       RegExStr += '(';
207       ++CurParen;
208       
209       if (AddRegExToRegEx(MatchStr.substr(NameEnd+1), CurParen, SM))
210         return true;
211
212       RegExStr += ')';
213     }
214     
215     // Handle fixed string matches.
216     // Find the end, which is the start of the next regex.
217     size_t FixedMatchEnd = PatternStr.find("{{");
218     FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
219     AddFixedStringToRegEx(PatternStr.substr(0, FixedMatchEnd), RegExStr);
220     PatternStr = PatternStr.substr(FixedMatchEnd);
221     continue;
222   }
223
224   return false;
225 }
226
227 void Pattern::AddFixedStringToRegEx(StringRef FixedStr, std::string &TheStr) {
228   // Add the characters from FixedStr to the regex, escaping as needed.  This
229   // avoids "leaning toothpicks" in common patterns.
230   for (unsigned i = 0, e = FixedStr.size(); i != e; ++i) {
231     switch (FixedStr[i]) {
232     // These are the special characters matched in "p_ere_exp".
233     case '(':
234     case ')':
235     case '^':
236     case '$':
237     case '|':
238     case '*':
239     case '+':
240     case '?':
241     case '.':
242     case '[':
243     case '\\':
244     case '{':
245       TheStr += '\\';
246       // FALL THROUGH.
247     default:
248       TheStr += FixedStr[i];
249       break;
250     }
251   }
252 }
253
254 bool Pattern::AddRegExToRegEx(StringRef RegexStr, unsigned &CurParen,
255                               SourceMgr &SM) {
256   Regex R(RegexStr);
257   std::string Error;
258   if (!R.isValid(Error)) {
259     SM.PrintMessage(SMLoc::getFromPointer(RegexStr.data()),
260                     "invalid regex: " + Error, "error");
261     return true;
262   }
263   
264   RegExStr += RegexStr.str();
265   CurParen += R.getNumMatches();
266   return false;
267 }
268
269 /// Match - Match the pattern string against the input buffer Buffer.  This
270 /// returns the position that is matched or npos if there is no match.  If
271 /// there is a match, the size of the matched string is returned in MatchLen.
272 size_t Pattern::Match(StringRef Buffer, size_t &MatchLen,
273                       StringMap<StringRef> &VariableTable) const {
274   // If this is a fixed string pattern, just match it now.
275   if (!FixedStr.empty()) {
276     MatchLen = FixedStr.size();
277     return Buffer.find(FixedStr);
278   }
279
280   // Regex match.
281   
282   // If there are variable uses, we need to create a temporary string with the
283   // actual value.
284   StringRef RegExToMatch = RegExStr;
285   std::string TmpStr;
286   if (!VariableUses.empty()) {
287     TmpStr = RegExStr;
288     
289     unsigned InsertOffset = 0;
290     for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
291       StringMap<StringRef>::iterator it =
292         VariableTable.find(VariableUses[i].first);
293       // If the variable is undefined, return an error.
294       if (it == VariableTable.end())
295         return StringRef::npos;
296
297       // Look up the value and escape it so that we can plop it into the regex.
298       std::string Value;
299       AddFixedStringToRegEx(it->second, Value);
300       
301       // Plop it into the regex at the adjusted offset.
302       TmpStr.insert(TmpStr.begin()+VariableUses[i].second+InsertOffset,
303                     Value.begin(), Value.end());
304       InsertOffset += Value.size();
305     }
306     
307     // Match the newly constructed regex.
308     RegExToMatch = TmpStr;
309   }
310   
311   
312   SmallVector<StringRef, 4> MatchInfo;
313   if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
314     return StringRef::npos;
315   
316   // Successful regex match.
317   assert(!MatchInfo.empty() && "Didn't get any match");
318   StringRef FullMatch = MatchInfo[0];
319   
320   // If this defines any variables, remember their values.
321   for (unsigned i = 0, e = VariableDefs.size(); i != e; ++i) {
322     assert(VariableDefs[i].second < MatchInfo.size() &&
323            "Internal paren error");
324     VariableTable[VariableDefs[i].first] = MatchInfo[VariableDefs[i].second];
325   }
326   
327   MatchLen = FullMatch.size();
328   return FullMatch.data()-Buffer.data();
329 }
330
331 unsigned Pattern::ComputeMatchDistance(StringRef Buffer,
332                               const StringMap<StringRef> &VariableTable) const {
333   // Just compute the number of matching characters. For regular expressions, we
334   // just compare against the regex itself and hope for the best.
335   //
336   // FIXME: One easy improvement here is have the regex lib generate a single
337   // example regular expression which matches, and use that as the example
338   // string.
339   StringRef ExampleString(FixedStr);
340   if (ExampleString.empty())
341     ExampleString = RegExStr;
342
343   return Buffer.substr(0, ExampleString.size()).edit_distance(ExampleString);
344 }
345
346 void Pattern::PrintFailureInfo(const SourceMgr &SM, StringRef Buffer,
347                                const StringMap<StringRef> &VariableTable) const{
348   // If this was a regular expression using variables, print the current
349   // variable values.
350   if (!VariableUses.empty()) {
351     for (unsigned i = 0, e = VariableUses.size(); i != e; ++i) {
352       StringRef Var = VariableUses[i].first;
353       StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
354       SmallString<256> Msg;
355       raw_svector_ostream OS(Msg);
356
357       // Check for undefined variable references.
358       if (it == VariableTable.end()) {
359         OS << "uses undefined variable \"";
360         OS.write_escaped(Var) << "\"";;
361       } else {
362         OS << "with variable \"";
363         OS.write_escaped(Var) << "\" equal to \"";
364         OS.write_escaped(it->second) << "\"";
365       }
366
367       SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), OS.str(), "note",
368                       /*ShowLine=*/false);
369     }
370   }
371
372   // Attempt to find the closest/best fuzzy match.  Usually an error happens
373   // because some string in the output didn't exactly match. In these cases, we
374   // would like to show the user a best guess at what "should have" matched, to
375   // save them having to actually check the input manually.
376   size_t NumLinesForward = 0;
377   size_t Best = StringRef::npos;
378   double BestQuality = 0;
379
380   // Use an arbitrary 4k limit on how far we will search.
381   for (size_t i = 0, e = std::min(4096, int(Buffer.size())); i != e; ++i) {
382     if (Buffer[i] == '\n')
383       ++NumLinesForward;
384
385     // Compute the "quality" of this match as an arbitrary combination of the
386     // match distance and the number of lines skipped to get to this match.
387     unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable);
388     double Quality = Distance + (NumLinesForward / 100.);
389
390     if (Quality < BestQuality || Best == StringRef::npos) {
391       Best = i;
392       BestQuality = Quality;
393     }
394   }
395
396   if (Best != StringRef::npos && BestQuality < 50) {
397     // Print the "possible intended match here" line if we found something
398     // reasonable.
399     SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
400                     "possible intended match here", "note");
401
402     // FIXME: If we wanted to be really friendly we would show why the match
403     // failed, as it can be hard to spot simple one character differences.
404   }
405 }
406
407 //===----------------------------------------------------------------------===//
408 // Check Strings.
409 //===----------------------------------------------------------------------===//
410
411 /// CheckString - This is a check that we found in the input file.
412 struct CheckString {
413   /// Pat - The pattern to match.
414   Pattern Pat;
415   
416   /// Loc - The location in the match file that the check string was specified.
417   SMLoc Loc;
418   
419   /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
420   /// to a CHECK: directive.
421   bool IsCheckNext;
422   
423   /// NotStrings - These are all of the strings that are disallowed from
424   /// occurring between this match string and the previous one (or start of
425   /// file).
426   std::vector<std::pair<SMLoc, Pattern> > NotStrings;
427   
428   CheckString(const Pattern &P, SMLoc L, bool isCheckNext)
429     : Pat(P), Loc(L), IsCheckNext(isCheckNext) {}
430 };
431
432 /// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
433 /// memory buffer, free it, and return a new one.
434 static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
435   SmallVector<char, 16> NewFile;
436   NewFile.reserve(MB->getBufferSize());
437   
438   for (const char *Ptr = MB->getBufferStart(), *End = MB->getBufferEnd();
439        Ptr != End; ++Ptr) {
440     // If C is not a horizontal whitespace, skip it.
441     if (*Ptr != ' ' && *Ptr != '\t') {
442       NewFile.push_back(*Ptr);
443       continue;
444     }
445     
446     // Otherwise, add one space and advance over neighboring space.
447     NewFile.push_back(' ');
448     while (Ptr+1 != End &&
449            (Ptr[1] == ' ' || Ptr[1] == '\t'))
450       ++Ptr;
451   }
452   
453   // Free the old buffer and return a new one.
454   MemoryBuffer *MB2 =
455     MemoryBuffer::getMemBufferCopy(NewFile.data(), 
456                                    NewFile.data() + NewFile.size(),
457                                    MB->getBufferIdentifier());
458   
459   delete MB;
460   return MB2;
461 }
462
463
464 /// ReadCheckFile - Read the check file, which specifies the sequence of
465 /// expected strings.  The strings are added to the CheckStrings vector.
466 static bool ReadCheckFile(SourceMgr &SM,
467                           std::vector<CheckString> &CheckStrings) {
468   // Open the check file, and tell SourceMgr about it.
469   std::string ErrorStr;
470   MemoryBuffer *F =
471     MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), &ErrorStr);
472   if (F == 0) {
473     errs() << "Could not open check file '" << CheckFilename << "': " 
474            << ErrorStr << '\n';
475     return true;
476   }
477   
478   // If we want to canonicalize whitespace, strip excess whitespace from the
479   // buffer containing the CHECK lines.
480   if (!NoCanonicalizeWhiteSpace)
481     F = CanonicalizeInputFile(F);
482   
483   SM.AddNewSourceBuffer(F, SMLoc());
484
485   // Find all instances of CheckPrefix followed by : in the file.
486   StringRef Buffer = F->getBuffer();
487
488   std::vector<std::pair<SMLoc, Pattern> > NotMatches;
489   
490   while (1) {
491     // See if Prefix occurs in the memory buffer.
492     Buffer = Buffer.substr(Buffer.find(CheckPrefix));
493     
494     // If we didn't find a match, we're done.
495     if (Buffer.empty())
496       break;
497     
498     const char *CheckPrefixStart = Buffer.data();
499     
500     // When we find a check prefix, keep track of whether we find CHECK: or
501     // CHECK-NEXT:
502     bool IsCheckNext = false, IsCheckNot = false;
503     
504     // Verify that the : is present after the prefix.
505     if (Buffer[CheckPrefix.size()] == ':') {
506       Buffer = Buffer.substr(CheckPrefix.size()+1);
507     } else if (Buffer.size() > CheckPrefix.size()+6 &&
508                memcmp(Buffer.data()+CheckPrefix.size(), "-NEXT:", 6) == 0) {
509       Buffer = Buffer.substr(CheckPrefix.size()+7);
510       IsCheckNext = true;
511     } else if (Buffer.size() > CheckPrefix.size()+5 &&
512                memcmp(Buffer.data()+CheckPrefix.size(), "-NOT:", 5) == 0) {
513       Buffer = Buffer.substr(CheckPrefix.size()+6);
514       IsCheckNot = true;
515     } else {
516       Buffer = Buffer.substr(1);
517       continue;
518     }
519     
520     // Okay, we found the prefix, yay.  Remember the rest of the line, but
521     // ignore leading and trailing whitespace.
522     Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
523     
524     // Scan ahead to the end of line.
525     size_t EOL = Buffer.find_first_of("\n\r");
526
527     // Parse the pattern.
528     Pattern P;
529     if (P.ParsePattern(Buffer.substr(0, EOL), SM))
530       return true;
531     
532     Buffer = Buffer.substr(EOL);
533
534     
535     // Verify that CHECK-NEXT lines have at least one CHECK line before them.
536     if (IsCheckNext && CheckStrings.empty()) {
537       SM.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart),
538                       "found '"+CheckPrefix+"-NEXT:' without previous '"+
539                       CheckPrefix+ ": line", "error");
540       return true;
541     }
542     
543     // Handle CHECK-NOT.
544     if (IsCheckNot) {
545       NotMatches.push_back(std::make_pair(SMLoc::getFromPointer(Buffer.data()),
546                                           P));
547       continue;
548     }
549     
550     
551     // Okay, add the string we captured to the output vector and move on.
552     CheckStrings.push_back(CheckString(P,
553                                        SMLoc::getFromPointer(Buffer.data()),
554                                        IsCheckNext));
555     std::swap(NotMatches, CheckStrings.back().NotStrings);
556   }
557   
558   if (CheckStrings.empty()) {
559     errs() << "error: no check strings found with prefix '" << CheckPrefix
560            << ":'\n";
561     return true;
562   }
563   
564   if (!NotMatches.empty()) {
565     errs() << "error: '" << CheckPrefix
566            << "-NOT:' not supported after last check line.\n";
567     return true;
568   }
569   
570   return false;
571 }
572
573 static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr,
574                              StringRef Buffer,
575                              StringMap<StringRef> &VariableTable) {
576   // Otherwise, we have an error, emit an error message.
577   SM.PrintMessage(CheckStr.Loc, "expected string not found in input",
578                   "error");
579   
580   // Print the "scanning from here" line.  If the current position is at the
581   // end of a line, advance to the start of the next line.
582   Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
583   
584   SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), "scanning from here",
585                   "note");
586
587   // Allow the pattern to print additional information if desired.
588   CheckStr.Pat.PrintFailureInfo(SM, Buffer, VariableTable);
589 }
590
591 /// CountNumNewlinesBetween - Count the number of newlines in the specified
592 /// range.
593 static unsigned CountNumNewlinesBetween(StringRef Range) {
594   unsigned NumNewLines = 0;
595   while (1) {
596     // Scan for newline.
597     Range = Range.substr(Range.find_first_of("\n\r"));
598     if (Range.empty()) return NumNewLines;
599     
600     ++NumNewLines;
601     
602     // Handle \n\r and \r\n as a single newline.
603     if (Range.size() > 1 &&
604         (Range[1] == '\n' || Range[1] == '\r') &&
605         (Range[0] != Range[1]))
606       Range = Range.substr(1);
607     Range = Range.substr(1);
608   }
609 }
610
611 int main(int argc, char **argv) {
612   sys::PrintStackTraceOnErrorSignal();
613   PrettyStackTraceProgram X(argc, argv);
614   cl::ParseCommandLineOptions(argc, argv);
615
616   SourceMgr SM;
617   
618   // Read the expected strings from the check file.
619   std::vector<CheckString> CheckStrings;
620   if (ReadCheckFile(SM, CheckStrings))
621     return 2;
622
623   // Open the file to check and add it to SourceMgr.
624   std::string ErrorStr;
625   MemoryBuffer *F =
626     MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
627   if (F == 0) {
628     errs() << "Could not open input file '" << InputFilename << "': " 
629            << ErrorStr << '\n';
630     return true;
631   }
632   
633   // Remove duplicate spaces in the input file if requested.
634   if (!NoCanonicalizeWhiteSpace)
635     F = CanonicalizeInputFile(F);
636   
637   SM.AddNewSourceBuffer(F, SMLoc());
638   
639   /// VariableTable - This holds all the current filecheck variables.
640   StringMap<StringRef> VariableTable;
641   
642   // Check that we have all of the expected strings, in order, in the input
643   // file.
644   StringRef Buffer = F->getBuffer();
645   
646   const char *LastMatch = Buffer.data();
647   
648   for (unsigned StrNo = 0, e = CheckStrings.size(); StrNo != e; ++StrNo) {
649     const CheckString &CheckStr = CheckStrings[StrNo];
650     
651     StringRef SearchFrom = Buffer;
652     
653     // Find StrNo in the file.
654     size_t MatchLen = 0;
655     Buffer = Buffer.substr(CheckStr.Pat.Match(Buffer, MatchLen, VariableTable));
656     
657     // If we didn't find a match, reject the input.
658     if (Buffer.empty()) {
659       PrintCheckFailed(SM, CheckStr, SearchFrom, VariableTable);
660       return 1;
661     }
662
663     StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch);
664
665     // If this check is a "CHECK-NEXT", verify that the previous match was on
666     // the previous line (i.e. that there is one newline between them).
667     if (CheckStr.IsCheckNext) {
668       // Count the number of newlines between the previous match and this one.
669       assert(LastMatch != F->getBufferStart() &&
670              "CHECK-NEXT can't be the first check in a file");
671
672       unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion);
673       if (NumNewLines == 0) {
674         SM.PrintMessage(CheckStr.Loc,
675                     CheckPrefix+"-NEXT: is on the same line as previous match",
676                         "error");
677         SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
678                         "'next' match was here", "note");
679         SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
680                         "previous match was here", "note");
681         return 1;
682       }
683       
684       if (NumNewLines != 1) {
685         SM.PrintMessage(CheckStr.Loc,
686                         CheckPrefix+
687                         "-NEXT: is not on the line after the previous match",
688                         "error");
689         SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()),
690                         "'next' match was here", "note");
691         SM.PrintMessage(SMLoc::getFromPointer(LastMatch),
692                         "previous match was here", "note");
693         return 1;
694       }
695     }
696     
697     // If this match had "not strings", verify that they don't exist in the
698     // skipped region.
699     for (unsigned ChunkNo = 0, e = CheckStr.NotStrings.size();
700          ChunkNo != e; ++ChunkNo) {
701       size_t MatchLen = 0;
702       size_t Pos = CheckStr.NotStrings[ChunkNo].second.Match(SkippedRegion,
703                                                              MatchLen,
704                                                              VariableTable);
705       if (Pos == StringRef::npos) continue;
706      
707       SM.PrintMessage(SMLoc::getFromPointer(LastMatch+Pos),
708                       CheckPrefix+"-NOT: string occurred!", "error");
709       SM.PrintMessage(CheckStr.NotStrings[ChunkNo].first,
710                       CheckPrefix+"-NOT: pattern specified here", "note");
711       return 1;
712     }
713     
714
715     // Otherwise, everything is good.  Step over the matched text and remember
716     // the position after the match as the end of the last match.
717     Buffer = Buffer.substr(MatchLen);
718     LastMatch = Buffer.data();
719   }
720   
721   return 0;
722 }