]>
git.proxmox.com Git - rustc.git/blob - src/llvm/utils/FileCheck/FileCheck.cpp
1 //===- FileCheck.cpp - Check that File's Contents match what is expected --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/ADT/OwningPtr.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/PrettyStackTrace.h"
23 #include "llvm/Support/Regex.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/Support/system_error.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/StringMap.h"
33 static cl::opt
<std::string
>
34 CheckFilename(cl::Positional
, cl::desc("<check-file>"), cl::Required
);
36 static cl::opt
<std::string
>
37 InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
38 cl::init("-"), cl::value_desc("filename"));
40 static cl::opt
<std::string
>
41 CheckPrefix("check-prefix", cl::init("CHECK"),
42 cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
45 NoCanonicalizeWhiteSpace("strict-whitespace",
46 cl::desc("Do not treat all horizontal whitespace as equivalent"));
48 //===----------------------------------------------------------------------===//
49 // Pattern Handling Code.
50 //===----------------------------------------------------------------------===//
55 /// MatchEOF - When set, this pattern only matches the end of file. This is
56 /// used for trailing CHECK-NOTs.
59 /// FixedStr - If non-empty, this pattern is a fixed string match with the
60 /// specified fixed string.
63 /// RegEx - If non-empty, this is a regex pattern.
66 /// VariableUses - Entries in this vector map to uses of a variable in the
67 /// pattern, e.g. "foo[[bar]]baz". In this case, the RegExStr will contain
68 /// "foobaz" and we'll get an entry in this vector that tells us to insert the
69 /// value of bar at offset 3.
70 std::vector
<std::pair
<StringRef
, unsigned> > VariableUses
;
72 /// VariableDefs - Entries in this vector map to definitions of a variable in
73 /// the pattern, e.g. "foo[[bar:.*]]baz". In this case, the RegExStr will
74 /// contain "foo(.*)baz" and VariableDefs will contain the pair "bar",1. The
75 /// index indicates what parenthesized value captures the variable value.
76 std::vector
<std::pair
<StringRef
, unsigned> > VariableDefs
;
80 Pattern(bool matchEOF
= false) : MatchEOF(matchEOF
) { }
82 bool ParsePattern(StringRef PatternStr
, SourceMgr
&SM
);
84 /// Match - Match the pattern string against the input buffer Buffer. This
85 /// returns the position that is matched or npos if there is no match. If
86 /// there is a match, the size of the matched string is returned in MatchLen.
88 /// The VariableTable StringMap provides the current values of filecheck
89 /// variables and is updated if this match defines new values.
90 size_t Match(StringRef Buffer
, size_t &MatchLen
,
91 StringMap
<StringRef
> &VariableTable
) const;
93 /// PrintFailureInfo - Print additional information about a failure to match
94 /// involving this pattern.
95 void PrintFailureInfo(const SourceMgr
&SM
, StringRef Buffer
,
96 const StringMap
<StringRef
> &VariableTable
) const;
99 static void AddFixedStringToRegEx(StringRef FixedStr
, std::string
&TheStr
);
100 bool AddRegExToRegEx(StringRef RegExStr
, unsigned &CurParen
, SourceMgr
&SM
);
102 /// ComputeMatchDistance - Compute an arbitrary estimate for the quality of
103 /// matching this pattern at the start of \arg Buffer; a distance of zero
104 /// should correspond to a perfect match.
105 unsigned ComputeMatchDistance(StringRef Buffer
,
106 const StringMap
<StringRef
> &VariableTable
) const;
110 bool Pattern::ParsePattern(StringRef PatternStr
, SourceMgr
&SM
) {
111 PatternLoc
= SMLoc::getFromPointer(PatternStr
.data());
113 // Ignore trailing whitespace.
114 while (!PatternStr
.empty() &&
115 (PatternStr
.back() == ' ' || PatternStr
.back() == '\t'))
116 PatternStr
= PatternStr
.substr(0, PatternStr
.size()-1);
118 // Check that there is something on the line.
119 if (PatternStr
.empty()) {
120 SM
.PrintMessage(PatternLoc
, SourceMgr::DK_Error
,
121 "found empty check string with prefix '" +
126 // Check to see if this is a fixed string, or if it has regex pieces.
127 if (PatternStr
.size() < 2 ||
128 (PatternStr
.find("{{") == StringRef::npos
&&
129 PatternStr
.find("[[") == StringRef::npos
)) {
130 FixedStr
= PatternStr
;
134 // Paren value #0 is for the fully matched string. Any new parenthesized
135 // values add from there.
136 unsigned CurParen
= 1;
138 // Otherwise, there is at least one regex piece. Build up the regex pattern
139 // by escaping scary characters in fixed strings, building up one big regex.
140 while (!PatternStr
.empty()) {
142 if (PatternStr
.startswith("{{")) {
144 // Otherwise, this is the start of a regex match. Scan for the }}.
145 size_t End
= PatternStr
.find("}}");
146 if (End
== StringRef::npos
) {
147 SM
.PrintMessage(SMLoc::getFromPointer(PatternStr
.data()),
149 "found start of regex string with no end '}}'");
153 // Enclose {{}} patterns in parens just like [[]] even though we're not
154 // capturing the result for any purpose. This is required in case the
155 // expression contains an alternation like: CHECK: abc{{x|z}}def. We
156 // want this to turn into: "abc(x|z)def" not "abcx|zdef".
160 if (AddRegExToRegEx(PatternStr
.substr(2, End
-2), CurParen
, SM
))
164 PatternStr
= PatternStr
.substr(End
+2);
168 // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
169 // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
170 // second form is [[foo]] which is a reference to foo. The variable name
171 // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
172 // it. This is to catch some common errors.
173 if (PatternStr
.startswith("[[")) {
174 // Verify that it is terminated properly.
175 size_t End
= PatternStr
.find("]]");
176 if (End
== StringRef::npos
) {
177 SM
.PrintMessage(SMLoc::getFromPointer(PatternStr
.data()),
179 "invalid named regex reference, no ]] found");
183 StringRef MatchStr
= PatternStr
.substr(2, End
-2);
184 PatternStr
= PatternStr
.substr(End
+2);
186 // Get the regex name (e.g. "foo").
187 size_t NameEnd
= MatchStr
.find(':');
188 StringRef Name
= MatchStr
.substr(0, NameEnd
);
191 SM
.PrintMessage(SMLoc::getFromPointer(Name
.data()), SourceMgr::DK_Error
,
192 "invalid name in named regex: empty name");
196 // Verify that the name is well formed.
197 for (unsigned i
= 0, e
= Name
.size(); i
!= e
; ++i
)
198 if (Name
[i
] != '_' && !isalnum(Name
[i
])) {
199 SM
.PrintMessage(SMLoc::getFromPointer(Name
.data()+i
),
200 SourceMgr::DK_Error
, "invalid name in named regex");
204 // Name can't start with a digit.
205 if (isdigit(Name
[0])) {
206 SM
.PrintMessage(SMLoc::getFromPointer(Name
.data()), SourceMgr::DK_Error
,
207 "invalid name in named regex");
212 if (NameEnd
== StringRef::npos
) {
213 VariableUses
.push_back(std::make_pair(Name
, RegExStr
.size()));
217 // Handle [[foo:.*]].
218 VariableDefs
.push_back(std::make_pair(Name
, CurParen
));
222 if (AddRegExToRegEx(MatchStr
.substr(NameEnd
+1), CurParen
, SM
))
228 // Handle fixed string matches.
229 // Find the end, which is the start of the next regex.
230 size_t FixedMatchEnd
= PatternStr
.find("{{");
231 FixedMatchEnd
= std::min(FixedMatchEnd
, PatternStr
.find("[["));
232 AddFixedStringToRegEx(PatternStr
.substr(0, FixedMatchEnd
), RegExStr
);
233 PatternStr
= PatternStr
.substr(FixedMatchEnd
);
240 void Pattern::AddFixedStringToRegEx(StringRef FixedStr
, std::string
&TheStr
) {
241 // Add the characters from FixedStr to the regex, escaping as needed. This
242 // avoids "leaning toothpicks" in common patterns.
243 for (unsigned i
= 0, e
= FixedStr
.size(); i
!= e
; ++i
) {
244 switch (FixedStr
[i
]) {
245 // These are the special characters matched in "p_ere_exp".
261 TheStr
+= FixedStr
[i
];
267 bool Pattern::AddRegExToRegEx(StringRef RegexStr
, unsigned &CurParen
,
271 if (!R
.isValid(Error
)) {
272 SM
.PrintMessage(SMLoc::getFromPointer(RegexStr
.data()), SourceMgr::DK_Error
,
273 "invalid regex: " + Error
);
277 RegExStr
+= RegexStr
.str();
278 CurParen
+= R
.getNumMatches();
282 /// Match - Match the pattern string against the input buffer Buffer. This
283 /// returns the position that is matched or npos if there is no match. If
284 /// there is a match, the size of the matched string is returned in MatchLen.
285 size_t Pattern::Match(StringRef Buffer
, size_t &MatchLen
,
286 StringMap
<StringRef
> &VariableTable
) const {
287 // If this is the EOF pattern, match it immediately.
290 return Buffer
.size();
293 // If this is a fixed string pattern, just match it now.
294 if (!FixedStr
.empty()) {
295 MatchLen
= FixedStr
.size();
296 return Buffer
.find(FixedStr
);
301 // If there are variable uses, we need to create a temporary string with the
303 StringRef RegExToMatch
= RegExStr
;
305 if (!VariableUses
.empty()) {
308 unsigned InsertOffset
= 0;
309 for (unsigned i
= 0, e
= VariableUses
.size(); i
!= e
; ++i
) {
310 StringMap
<StringRef
>::iterator it
=
311 VariableTable
.find(VariableUses
[i
].first
);
312 // If the variable is undefined, return an error.
313 if (it
== VariableTable
.end())
314 return StringRef::npos
;
316 // Look up the value and escape it so that we can plop it into the regex.
318 AddFixedStringToRegEx(it
->second
, Value
);
320 // Plop it into the regex at the adjusted offset.
321 TmpStr
.insert(TmpStr
.begin()+VariableUses
[i
].second
+InsertOffset
,
322 Value
.begin(), Value
.end());
323 InsertOffset
+= Value
.size();
326 // Match the newly constructed regex.
327 RegExToMatch
= TmpStr
;
331 SmallVector
<StringRef
, 4> MatchInfo
;
332 if (!Regex(RegExToMatch
, Regex::Newline
).match(Buffer
, &MatchInfo
))
333 return StringRef::npos
;
335 // Successful regex match.
336 assert(!MatchInfo
.empty() && "Didn't get any match");
337 StringRef FullMatch
= MatchInfo
[0];
339 // If this defines any variables, remember their values.
340 for (unsigned i
= 0, e
= VariableDefs
.size(); i
!= e
; ++i
) {
341 assert(VariableDefs
[i
].second
< MatchInfo
.size() &&
342 "Internal paren error");
343 VariableTable
[VariableDefs
[i
].first
] = MatchInfo
[VariableDefs
[i
].second
];
346 MatchLen
= FullMatch
.size();
347 return FullMatch
.data()-Buffer
.data();
350 unsigned Pattern::ComputeMatchDistance(StringRef Buffer
,
351 const StringMap
<StringRef
> &VariableTable
) const {
352 // Just compute the number of matching characters. For regular expressions, we
353 // just compare against the regex itself and hope for the best.
355 // FIXME: One easy improvement here is have the regex lib generate a single
356 // example regular expression which matches, and use that as the example
358 StringRef
ExampleString(FixedStr
);
359 if (ExampleString
.empty())
360 ExampleString
= RegExStr
;
362 // Only compare up to the first line in the buffer, or the string size.
363 StringRef BufferPrefix
= Buffer
.substr(0, ExampleString
.size());
364 BufferPrefix
= BufferPrefix
.split('\n').first
;
365 return BufferPrefix
.edit_distance(ExampleString
);
368 void Pattern::PrintFailureInfo(const SourceMgr
&SM
, StringRef Buffer
,
369 const StringMap
<StringRef
> &VariableTable
) const{
370 // If this was a regular expression using variables, print the current
372 if (!VariableUses
.empty()) {
373 for (unsigned i
= 0, e
= VariableUses
.size(); i
!= e
; ++i
) {
374 StringRef Var
= VariableUses
[i
].first
;
375 StringMap
<StringRef
>::const_iterator it
= VariableTable
.find(Var
);
376 SmallString
<256> Msg
;
377 raw_svector_ostream
OS(Msg
);
379 // Check for undefined variable references.
380 if (it
== VariableTable
.end()) {
381 OS
<< "uses undefined variable \"";
382 OS
.write_escaped(Var
) << "\"";;
384 OS
<< "with variable \"";
385 OS
.write_escaped(Var
) << "\" equal to \"";
386 OS
.write_escaped(it
->second
) << "\"";
389 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.data()), SourceMgr::DK_Note
,
394 // Attempt to find the closest/best fuzzy match. Usually an error happens
395 // because some string in the output didn't exactly match. In these cases, we
396 // would like to show the user a best guess at what "should have" matched, to
397 // save them having to actually check the input manually.
398 size_t NumLinesForward
= 0;
399 size_t Best
= StringRef::npos
;
400 double BestQuality
= 0;
402 // Use an arbitrary 4k limit on how far we will search.
403 for (size_t i
= 0, e
= std::min(size_t(4096), Buffer
.size()); i
!= e
; ++i
) {
404 if (Buffer
[i
] == '\n')
407 // Patterns have leading whitespace stripped, so skip whitespace when
408 // looking for something which looks like a pattern.
409 if (Buffer
[i
] == ' ' || Buffer
[i
] == '\t')
412 // Compute the "quality" of this match as an arbitrary combination of the
413 // match distance and the number of lines skipped to get to this match.
414 unsigned Distance
= ComputeMatchDistance(Buffer
.substr(i
), VariableTable
);
415 double Quality
= Distance
+ (NumLinesForward
/ 100.);
417 if (Quality
< BestQuality
|| Best
== StringRef::npos
) {
419 BestQuality
= Quality
;
423 // Print the "possible intended match here" line if we found something
424 // reasonable and not equal to what we showed in the "scanning from here"
426 if (Best
&& Best
!= StringRef::npos
&& BestQuality
< 50) {
427 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.data() + Best
),
428 SourceMgr::DK_Note
, "possible intended match here");
430 // FIXME: If we wanted to be really friendly we would show why the match
431 // failed, as it can be hard to spot simple one character differences.
435 //===----------------------------------------------------------------------===//
437 //===----------------------------------------------------------------------===//
439 /// CheckString - This is a check that we found in the input file.
441 /// Pat - The pattern to match.
444 /// Loc - The location in the match file that the check string was specified.
447 /// IsCheckNext - This is true if this is a CHECK-NEXT: directive (as opposed
448 /// to a CHECK: directive.
451 /// NotStrings - These are all of the strings that are disallowed from
452 /// occurring between this match string and the previous one (or start of
454 std::vector
<std::pair
<SMLoc
, Pattern
> > NotStrings
;
456 CheckString(const Pattern
&P
, SMLoc L
, bool isCheckNext
)
457 : Pat(P
), Loc(L
), IsCheckNext(isCheckNext
) {}
460 /// CanonicalizeInputFile - Remove duplicate horizontal space from the specified
461 /// memory buffer, free it, and return a new one.
462 static MemoryBuffer
*CanonicalizeInputFile(MemoryBuffer
*MB
) {
463 SmallString
<128> NewFile
;
464 NewFile
.reserve(MB
->getBufferSize());
466 for (const char *Ptr
= MB
->getBufferStart(), *End
= MB
->getBufferEnd();
468 // Eliminate trailing dosish \r.
469 if (Ptr
<= End
- 2 && Ptr
[0] == '\r' && Ptr
[1] == '\n') {
473 // If current char is not a horizontal whitespace, dump it to output as is.
474 if (*Ptr
!= ' ' && *Ptr
!= '\t') {
475 NewFile
.push_back(*Ptr
);
479 // Otherwise, add one space and advance over neighboring space.
480 NewFile
.push_back(' ');
481 while (Ptr
+1 != End
&&
482 (Ptr
[1] == ' ' || Ptr
[1] == '\t'))
486 // Free the old buffer and return a new one.
488 MemoryBuffer::getMemBufferCopy(NewFile
.str(), MB
->getBufferIdentifier());
495 /// ReadCheckFile - Read the check file, which specifies the sequence of
496 /// expected strings. The strings are added to the CheckStrings vector.
497 static bool ReadCheckFile(SourceMgr
&SM
,
498 std::vector
<CheckString
> &CheckStrings
) {
499 // Open the check file, and tell SourceMgr about it.
500 OwningPtr
<MemoryBuffer
> File
;
502 MemoryBuffer::getFileOrSTDIN(CheckFilename
.c_str(), File
)) {
503 errs() << "Could not open check file '" << CheckFilename
<< "': "
504 << ec
.message() << '\n';
507 MemoryBuffer
*F
= File
.take();
509 // If we want to canonicalize whitespace, strip excess whitespace from the
510 // buffer containing the CHECK lines.
511 if (!NoCanonicalizeWhiteSpace
)
512 F
= CanonicalizeInputFile(F
);
514 SM
.AddNewSourceBuffer(F
, SMLoc());
516 // Find all instances of CheckPrefix followed by : in the file.
517 StringRef Buffer
= F
->getBuffer();
519 std::vector
<std::pair
<SMLoc
, Pattern
> > NotMatches
;
522 // See if Prefix occurs in the memory buffer.
523 Buffer
= Buffer
.substr(Buffer
.find(CheckPrefix
));
525 // If we didn't find a match, we're done.
529 const char *CheckPrefixStart
= Buffer
.data();
531 // When we find a check prefix, keep track of whether we find CHECK: or
533 bool IsCheckNext
= false, IsCheckNot
= false;
535 // Verify that the : is present after the prefix.
536 if (Buffer
[CheckPrefix
.size()] == ':') {
537 Buffer
= Buffer
.substr(CheckPrefix
.size()+1);
538 } else if (Buffer
.size() > CheckPrefix
.size()+6 &&
539 memcmp(Buffer
.data()+CheckPrefix
.size(), "-NEXT:", 6) == 0) {
540 Buffer
= Buffer
.substr(CheckPrefix
.size()+6);
542 } else if (Buffer
.size() > CheckPrefix
.size()+5 &&
543 memcmp(Buffer
.data()+CheckPrefix
.size(), "-NOT:", 5) == 0) {
544 Buffer
= Buffer
.substr(CheckPrefix
.size()+5);
547 Buffer
= Buffer
.substr(1);
551 // Okay, we found the prefix, yay. Remember the rest of the line, but
552 // ignore leading and trailing whitespace.
553 Buffer
= Buffer
.substr(Buffer
.find_first_not_of(" \t"));
555 // Scan ahead to the end of line.
556 size_t EOL
= Buffer
.find_first_of("\n\r");
558 // Remember the location of the start of the pattern, for diagnostics.
559 SMLoc PatternLoc
= SMLoc::getFromPointer(Buffer
.data());
561 // Parse the pattern.
563 if (P
.ParsePattern(Buffer
.substr(0, EOL
), SM
))
566 Buffer
= Buffer
.substr(EOL
);
569 // Verify that CHECK-NEXT lines have at least one CHECK line before them.
570 if (IsCheckNext
&& CheckStrings
.empty()) {
571 SM
.PrintMessage(SMLoc::getFromPointer(CheckPrefixStart
),
573 "found '"+CheckPrefix
+"-NEXT:' without previous '"+
574 CheckPrefix
+ ": line");
580 NotMatches
.push_back(std::make_pair(SMLoc::getFromPointer(Buffer
.data()),
586 // Okay, add the string we captured to the output vector and move on.
587 CheckStrings
.push_back(CheckString(P
,
590 std::swap(NotMatches
, CheckStrings
.back().NotStrings
);
593 // Add an EOF pattern for any trailing CHECK-NOTs.
594 if (!NotMatches
.empty()) {
595 CheckStrings
.push_back(CheckString(Pattern(true),
596 SMLoc::getFromPointer(Buffer
.data()),
598 std::swap(NotMatches
, CheckStrings
.back().NotStrings
);
601 if (CheckStrings
.empty()) {
602 errs() << "error: no check strings found with prefix '" << CheckPrefix
610 static void PrintCheckFailed(const SourceMgr
&SM
, const CheckString
&CheckStr
,
612 StringMap
<StringRef
> &VariableTable
) {
613 // Otherwise, we have an error, emit an error message.
614 SM
.PrintMessage(CheckStr
.Loc
, SourceMgr::DK_Error
,
615 "expected string not found in input");
617 // Print the "scanning from here" line. If the current position is at the
618 // end of a line, advance to the start of the next line.
619 Buffer
= Buffer
.substr(Buffer
.find_first_not_of(" \t\n\r"));
621 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.data()), SourceMgr::DK_Note
,
622 "scanning from here");
624 // Allow the pattern to print additional information if desired.
625 CheckStr
.Pat
.PrintFailureInfo(SM
, Buffer
, VariableTable
);
628 /// CountNumNewlinesBetween - Count the number of newlines in the specified
630 static unsigned CountNumNewlinesBetween(StringRef Range
) {
631 unsigned NumNewLines
= 0;
634 Range
= Range
.substr(Range
.find_first_of("\n\r"));
635 if (Range
.empty()) return NumNewLines
;
639 // Handle \n\r and \r\n as a single newline.
640 if (Range
.size() > 1 &&
641 (Range
[1] == '\n' || Range
[1] == '\r') &&
642 (Range
[0] != Range
[1]))
643 Range
= Range
.substr(1);
644 Range
= Range
.substr(1);
648 int main(int argc
, char **argv
) {
649 sys::PrintStackTraceOnErrorSignal();
650 PrettyStackTraceProgram
X(argc
, argv
);
651 cl::ParseCommandLineOptions(argc
, argv
);
655 // Read the expected strings from the check file.
656 std::vector
<CheckString
> CheckStrings
;
657 if (ReadCheckFile(SM
, CheckStrings
))
660 // Open the file to check and add it to SourceMgr.
661 OwningPtr
<MemoryBuffer
> File
;
663 MemoryBuffer::getFileOrSTDIN(InputFilename
.c_str(), File
)) {
664 errs() << "Could not open input file '" << InputFilename
<< "': "
665 << ec
.message() << '\n';
668 MemoryBuffer
*F
= File
.take();
670 if (F
->getBufferSize() == 0) {
671 errs() << "FileCheck error: '" << InputFilename
<< "' is empty.\n";
675 // Remove duplicate spaces in the input file if requested.
676 if (!NoCanonicalizeWhiteSpace
)
677 F
= CanonicalizeInputFile(F
);
679 SM
.AddNewSourceBuffer(F
, SMLoc());
681 /// VariableTable - This holds all the current filecheck variables.
682 StringMap
<StringRef
> VariableTable
;
684 // Check that we have all of the expected strings, in order, in the input
686 StringRef Buffer
= F
->getBuffer();
688 const char *LastMatch
= Buffer
.data();
690 for (unsigned StrNo
= 0, e
= CheckStrings
.size(); StrNo
!= e
; ++StrNo
) {
691 const CheckString
&CheckStr
= CheckStrings
[StrNo
];
693 StringRef SearchFrom
= Buffer
;
695 // Find StrNo in the file.
697 size_t MatchPos
= CheckStr
.Pat
.Match(Buffer
, MatchLen
, VariableTable
);
698 Buffer
= Buffer
.substr(MatchPos
);
700 // If we didn't find a match, reject the input.
701 if (MatchPos
== StringRef::npos
) {
702 PrintCheckFailed(SM
, CheckStr
, SearchFrom
, VariableTable
);
706 StringRef
SkippedRegion(LastMatch
, Buffer
.data()-LastMatch
);
708 // If this check is a "CHECK-NEXT", verify that the previous match was on
709 // the previous line (i.e. that there is one newline between them).
710 if (CheckStr
.IsCheckNext
) {
711 // Count the number of newlines between the previous match and this one.
712 assert(LastMatch
!= F
->getBufferStart() &&
713 "CHECK-NEXT can't be the first check in a file");
715 unsigned NumNewLines
= CountNumNewlinesBetween(SkippedRegion
);
716 if (NumNewLines
== 0) {
717 SM
.PrintMessage(CheckStr
.Loc
, SourceMgr::DK_Error
,
718 CheckPrefix
+"-NEXT: is on the same line as previous match");
719 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.data()),
720 SourceMgr::DK_Note
, "'next' match was here");
721 SM
.PrintMessage(SMLoc::getFromPointer(LastMatch
), SourceMgr::DK_Note
,
722 "previous match was here");
726 if (NumNewLines
!= 1) {
727 SM
.PrintMessage(CheckStr
.Loc
, SourceMgr::DK_Error
, CheckPrefix
+
728 "-NEXT: is not on the line after the previous match");
729 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.data()),
730 SourceMgr::DK_Note
, "'next' match was here");
731 SM
.PrintMessage(SMLoc::getFromPointer(LastMatch
), SourceMgr::DK_Note
,
732 "previous match was here");
737 // If this match had "not strings", verify that they don't exist in the
739 for (unsigned ChunkNo
= 0, e
= CheckStr
.NotStrings
.size();
740 ChunkNo
!= e
; ++ChunkNo
) {
742 size_t Pos
= CheckStr
.NotStrings
[ChunkNo
].second
.Match(SkippedRegion
,
745 if (Pos
== StringRef::npos
) continue;
747 SM
.PrintMessage(SMLoc::getFromPointer(LastMatch
+Pos
), SourceMgr::DK_Error
,
748 CheckPrefix
+"-NOT: string occurred!");
749 SM
.PrintMessage(CheckStr
.NotStrings
[ChunkNo
].first
, SourceMgr::DK_Note
,
750 CheckPrefix
+"-NOT: pattern specified here");
755 // Otherwise, everything is good. Step over the matched text and remember
756 // the position after the match as the end of the last match.
757 Buffer
= Buffer
.substr(MatchLen
);
758 LastMatch
= Buffer
.data();