LLVM  8.0.0svn
FileCheck.cpp
Go to the documentation of this file.
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 file implements most of the API that will be used by the FileCheck utility
14 // as well as various unittests.
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/Support/FileCheck.h"
18 #include "llvm/ADT/StringSet.h"
20 #include <cstdint>
21 #include <list>
22 #include <map>
23 #include <tuple>
24 #include <utility>
25 
26 using namespace llvm;
27 
28 /// Parses the given string into the Pattern.
29 ///
30 /// \p Prefix provides which prefix is being matched, \p SM provides the
31 /// SourceMgr used for error reports, and \p LineNumber is the line number in
32 /// the input file from which the pattern string was read. Returns true in
33 /// case of an error, false otherwise.
35  SourceMgr &SM, unsigned LineNumber,
36  const FileCheckRequest &Req) {
37  bool MatchFullLinesHere = Req.MatchFullLines && CheckTy != Check::CheckNot;
38 
39  this->LineNumber = LineNumber;
40  PatternLoc = SMLoc::getFromPointer(PatternStr.data());
41 
42  if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
43  // Ignore trailing whitespace.
44  while (!PatternStr.empty() &&
45  (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
46  PatternStr = PatternStr.substr(0, PatternStr.size() - 1);
47 
48  // Check that there is something on the line.
49  if (PatternStr.empty() && CheckTy != Check::CheckEmpty) {
50  SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
51  "found empty check string with prefix '" + Prefix + ":'");
52  return true;
53  }
54 
55  if (!PatternStr.empty() && CheckTy == Check::CheckEmpty) {
56  SM.PrintMessage(
57  PatternLoc, SourceMgr::DK_Error,
58  "found non-empty check string for empty check with prefix '" + Prefix +
59  ":'");
60  return true;
61  }
62 
63  if (CheckTy == Check::CheckEmpty) {
64  RegExStr = "(\n$)";
65  return false;
66  }
67 
68  // Check to see if this is a fixed string, or if it has regex pieces.
69  if (!MatchFullLinesHere &&
70  (PatternStr.size() < 2 || (PatternStr.find("{{") == StringRef::npos &&
71  PatternStr.find("[[") == StringRef::npos))) {
72  FixedStr = PatternStr;
73  return false;
74  }
75 
76  if (MatchFullLinesHere) {
77  RegExStr += '^';
78  if (!Req.NoCanonicalizeWhiteSpace)
79  RegExStr += " *";
80  }
81 
82  // Paren value #0 is for the fully matched string. Any new parenthesized
83  // values add from there.
84  unsigned CurParen = 1;
85 
86  // Otherwise, there is at least one regex piece. Build up the regex pattern
87  // by escaping scary characters in fixed strings, building up one big regex.
88  while (!PatternStr.empty()) {
89  // RegEx matches.
90  if (PatternStr.startswith("{{")) {
91  // This is the start of a regex match. Scan for the }}.
92  size_t End = PatternStr.find("}}");
93  if (End == StringRef::npos) {
94  SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
96  "found start of regex string with no end '}}'");
97  return true;
98  }
99 
100  // Enclose {{}} patterns in parens just like [[]] even though we're not
101  // capturing the result for any purpose. This is required in case the
102  // expression contains an alternation like: CHECK: abc{{x|z}}def. We
103  // want this to turn into: "abc(x|z)def" not "abcx|zdef".
104  RegExStr += '(';
105  ++CurParen;
106 
107  if (AddRegExToRegEx(PatternStr.substr(2, End - 2), CurParen, SM))
108  return true;
109  RegExStr += ')';
110 
111  PatternStr = PatternStr.substr(End + 2);
112  continue;
113  }
114 
115  // Named RegEx matches. These are of two forms: [[foo:.*]] which matches .*
116  // (or some other regex) and assigns it to the FileCheck variable 'foo'. The
117  // second form is [[foo]] which is a reference to foo. The variable name
118  // itself must be of the form "[a-zA-Z_][0-9a-zA-Z_]*", otherwise we reject
119  // it. This is to catch some common errors.
120  if (PatternStr.startswith("[[")) {
121  // Find the closing bracket pair ending the match. End is going to be an
122  // offset relative to the beginning of the match string.
123  size_t End = FindRegexVarEnd(PatternStr.substr(2), SM);
124 
125  if (End == StringRef::npos) {
126  SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
128  "invalid named regex reference, no ]] found");
129  return true;
130  }
131 
132  StringRef MatchStr = PatternStr.substr(2, End);
133  PatternStr = PatternStr.substr(End + 4);
134 
135  // Get the regex name (e.g. "foo").
136  size_t NameEnd = MatchStr.find(':');
137  StringRef Name = MatchStr.substr(0, NameEnd);
138 
139  if (Name.empty()) {
141  "invalid name in named regex: empty name");
142  return true;
143  }
144 
145  // Verify that the name/expression is well formed. FileCheck currently
146  // supports @LINE, @LINE+number, @LINE-number expressions. The check here
147  // is relaxed, more strict check is performed in \c EvaluateExpression.
148  bool IsExpression = false;
149  for (unsigned i = 0, e = Name.size(); i != e; ++i) {
150  if (i == 0) {
151  if (Name[i] == '$') // Global vars start with '$'
152  continue;
153  if (Name[i] == '@') {
154  if (NameEnd != StringRef::npos) {
157  "invalid name in named regex definition");
158  return true;
159  }
160  IsExpression = true;
161  continue;
162  }
163  }
164  if (Name[i] != '_' && !isalnum(Name[i]) &&
165  (!IsExpression || (Name[i] != '+' && Name[i] != '-'))) {
166  SM.PrintMessage(SMLoc::getFromPointer(Name.data() + i),
167  SourceMgr::DK_Error, "invalid name in named regex");
168  return true;
169  }
170  }
171 
172  // Name can't start with a digit.
173  if (isdigit(static_cast<unsigned char>(Name[0]))) {
175  "invalid name in named regex");
176  return true;
177  }
178 
179  // Handle [[foo]].
180  if (NameEnd == StringRef::npos) {
181  // Handle variables that were defined earlier on the same line by
182  // emitting a backreference.
183  if (VariableDefs.find(Name) != VariableDefs.end()) {
184  unsigned VarParenNum = VariableDefs[Name];
185  if (VarParenNum < 1 || VarParenNum > 9) {
188  "Can't back-reference more than 9 variables");
189  return true;
190  }
191  AddBackrefToRegEx(VarParenNum);
192  } else {
193  VariableUses.push_back(std::make_pair(Name, RegExStr.size()));
194  }
195  continue;
196  }
197 
198  // Handle [[foo:.*]].
199  VariableDefs[Name] = CurParen;
200  RegExStr += '(';
201  ++CurParen;
202 
203  if (AddRegExToRegEx(MatchStr.substr(NameEnd + 1), CurParen, SM))
204  return true;
205 
206  RegExStr += ')';
207  }
208 
209  // Handle fixed string matches.
210  // Find the end, which is the start of the next regex.
211  size_t FixedMatchEnd = PatternStr.find("{{");
212  FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
213  RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd));
214  PatternStr = PatternStr.substr(FixedMatchEnd);
215  }
216 
217  if (MatchFullLinesHere) {
218  if (!Req.NoCanonicalizeWhiteSpace)
219  RegExStr += " *";
220  RegExStr += '$';
221  }
222 
223  return false;
224 }
225 
226 bool FileCheckPattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) {
227  Regex R(RS);
228  std::string Error;
229  if (!R.isValid(Error)) {
231  "invalid regex: " + Error);
232  return true;
233  }
234 
235  RegExStr += RS.str();
236  CurParen += R.getNumMatches();
237  return false;
238 }
239 
240 void FileCheckPattern::AddBackrefToRegEx(unsigned BackrefNum) {
241  assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number");
242  std::string Backref = std::string("\\") + std::string(1, '0' + BackrefNum);
243  RegExStr += Backref;
244 }
245 
246 /// Evaluates expression and stores the result to \p Value.
247 ///
248 /// Returns true on success and false when the expression has invalid syntax.
249 bool FileCheckPattern::EvaluateExpression(StringRef Expr, std::string &Value) const {
250  // The only supported expression is @LINE([\+-]\d+)?
251  if (!Expr.startswith("@LINE"))
252  return false;
253  Expr = Expr.substr(StringRef("@LINE").size());
254  int Offset = 0;
255  if (!Expr.empty()) {
256  if (Expr[0] == '+')
257  Expr = Expr.substr(1);
258  else if (Expr[0] != '-')
259  return false;
260  if (Expr.getAsInteger(10, Offset))
261  return false;
262  }
263  Value = llvm::itostr(LineNumber + Offset);
264  return true;
265 }
266 
267 /// Matches the pattern string against the input buffer \p Buffer
268 ///
269 /// This returns the position that is matched or npos if there is no match. If
270 /// there is a match, the size of the matched string is returned in \p
271 /// MatchLen.
272 ///
273 /// The \p VariableTable StringMap provides the current values of filecheck
274 /// variables and is updated if this match defines new values.
275 size_t FileCheckPattern::Match(StringRef Buffer, size_t &MatchLen,
276  StringMap<StringRef> &VariableTable) const {
277  // If this is the EOF pattern, match it immediately.
278  if (CheckTy == Check::CheckEOF) {
279  MatchLen = 0;
280  return Buffer.size();
281  }
282 
283  // If this is a fixed string pattern, just match it now.
284  if (!FixedStr.empty()) {
285  MatchLen = FixedStr.size();
286  return Buffer.find(FixedStr);
287  }
288 
289  // Regex match.
290 
291  // If there are variable uses, we need to create a temporary string with the
292  // actual value.
293  StringRef RegExToMatch = RegExStr;
294  std::string TmpStr;
295  if (!VariableUses.empty()) {
296  TmpStr = RegExStr;
297 
298  unsigned InsertOffset = 0;
299  for (const auto &VariableUse : VariableUses) {
300  std::string Value;
301 
302  if (VariableUse.first[0] == '@') {
303  if (!EvaluateExpression(VariableUse.first, Value))
304  return StringRef::npos;
305  } else {
307  VariableTable.find(VariableUse.first);
308  // If the variable is undefined, return an error.
309  if (it == VariableTable.end())
310  return StringRef::npos;
311 
312  // Look up the value and escape it so that we can put it into the regex.
313  Value += Regex::escape(it->second);
314  }
315 
316  // Plop it into the regex at the adjusted offset.
317  TmpStr.insert(TmpStr.begin() + VariableUse.second + InsertOffset,
318  Value.begin(), Value.end());
319  InsertOffset += Value.size();
320  }
321 
322  // Match the newly constructed regex.
323  RegExToMatch = TmpStr;
324  }
325 
326  SmallVector<StringRef, 4> MatchInfo;
327  if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
328  return StringRef::npos;
329 
330  // Successful regex match.
331  assert(!MatchInfo.empty() && "Didn't get any match");
332  StringRef FullMatch = MatchInfo[0];
333 
334  // If this defines any variables, remember their values.
335  for (const auto &VariableDef : VariableDefs) {
336  assert(VariableDef.second < MatchInfo.size() && "Internal paren error");
337  VariableTable[VariableDef.first] = MatchInfo[VariableDef.second];
338  }
339 
340  // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after
341  // the required preceding newline, which is consumed by the pattern in the
342  // case of CHECK-EMPTY but not CHECK-NEXT.
343  size_t MatchStartSkip = CheckTy == Check::CheckEmpty;
344  MatchLen = FullMatch.size() - MatchStartSkip;
345  return FullMatch.data() - Buffer.data() + MatchStartSkip;
346 }
347 
348 
349 /// Computes an arbitrary estimate for the quality of matching this pattern at
350 /// the start of \p Buffer; a distance of zero should correspond to a perfect
351 /// match.
352 unsigned
353 FileCheckPattern::ComputeMatchDistance(StringRef Buffer,
354  const StringMap<StringRef> &VariableTable) const {
355  // Just compute the number of matching characters. For regular expressions, we
356  // just compare against the regex itself and hope for the best.
357  //
358  // FIXME: One easy improvement here is have the regex lib generate a single
359  // example regular expression which matches, and use that as the example
360  // string.
361  StringRef ExampleString(FixedStr);
362  if (ExampleString.empty())
363  ExampleString = RegExStr;
364 
365  // Only compare up to the first line in the buffer, or the string size.
366  StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
367  BufferPrefix = BufferPrefix.split('\n').first;
368  return BufferPrefix.edit_distance(ExampleString);
369 }
370 
372  const StringMap<StringRef> &VariableTable,
373  SMRange MatchRange) const {
374  // If this was a regular expression using variables, print the current
375  // variable values.
376  if (!VariableUses.empty()) {
377  for (const auto &VariableUse : VariableUses) {
378  SmallString<256> Msg;
379  raw_svector_ostream OS(Msg);
380  StringRef Var = VariableUse.first;
381  if (Var[0] == '@') {
382  std::string Value;
383  if (EvaluateExpression(Var, Value)) {
384  OS << "with expression \"";
385  OS.write_escaped(Var) << "\" equal to \"";
386  OS.write_escaped(Value) << "\"";
387  } else {
388  OS << "uses incorrect expression \"";
389  OS.write_escaped(Var) << "\"";
390  }
391  } else {
392  StringMap<StringRef>::const_iterator it = VariableTable.find(Var);
393 
394  // Check for undefined variable references.
395  if (it == VariableTable.end()) {
396  OS << "uses undefined variable \"";
397  OS.write_escaped(Var) << "\"";
398  } else {
399  OS << "with variable \"";
400  OS.write_escaped(Var) << "\" equal to \"";
401  OS.write_escaped(it->second) << "\"";
402  }
403  }
404 
405  if (MatchRange.isValid())
406  SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, OS.str(),
407  {MatchRange});
408  else
410  SourceMgr::DK_Note, OS.str());
411  }
412  }
413 }
414 
416  const SourceMgr &SM, StringRef Buffer,
417  const StringMap<StringRef> &VariableTable) const {
418  // Attempt to find the closest/best fuzzy match. Usually an error happens
419  // because some string in the output didn't exactly match. In these cases, we
420  // would like to show the user a best guess at what "should have" matched, to
421  // save them having to actually check the input manually.
422  size_t NumLinesForward = 0;
423  size_t Best = StringRef::npos;
424  double BestQuality = 0;
425 
426  // Use an arbitrary 4k limit on how far we will search.
427  for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
428  if (Buffer[i] == '\n')
429  ++NumLinesForward;
430 
431  // Patterns have leading whitespace stripped, so skip whitespace when
432  // looking for something which looks like a pattern.
433  if (Buffer[i] == ' ' || Buffer[i] == '\t')
434  continue;
435 
436  // Compute the "quality" of this match as an arbitrary combination of the
437  // match distance and the number of lines skipped to get to this match.
438  unsigned Distance = ComputeMatchDistance(Buffer.substr(i), VariableTable);
439  double Quality = Distance + (NumLinesForward / 100.);
440 
441  if (Quality < BestQuality || Best == StringRef::npos) {
442  Best = i;
443  BestQuality = Quality;
444  }
445  }
446 
447  // Print the "possible intended match here" line if we found something
448  // reasonable and not equal to what we showed in the "scanning from here"
449  // line.
450  if (Best && Best != StringRef::npos && BestQuality < 50) {
451  SM.PrintMessage(SMLoc::getFromPointer(Buffer.data() + Best),
452  SourceMgr::DK_Note, "possible intended match here");
453 
454  // FIXME: If we wanted to be really friendly we would show why the match
455  // failed, as it can be hard to spot simple one character differences.
456  }
457 }
458 
459 /// Finds the closing sequence of a regex variable usage or definition.
460 ///
461 /// \p Str has to point in the beginning of the definition (right after the
462 /// opening sequence). Returns the offset of the closing sequence within Str,
463 /// or npos if it was not found.
464 size_t FileCheckPattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) {
465  // Offset keeps track of the current offset within the input Str
466  size_t Offset = 0;
467  // [...] Nesting depth
468  size_t BracketDepth = 0;
469 
470  while (!Str.empty()) {
471  if (Str.startswith("]]") && BracketDepth == 0)
472  return Offset;
473  if (Str[0] == '\\') {
474  // Backslash escapes the next char within regexes, so skip them both.
475  Str = Str.substr(2);
476  Offset += 2;
477  } else {
478  switch (Str[0]) {
479  default:
480  break;
481  case '[':
482  BracketDepth++;
483  break;
484  case ']':
485  if (BracketDepth == 0) {
488  "missing closing \"]\" for regex variable");
489  exit(1);
490  }
491  BracketDepth--;
492  break;
493  }
494  Str = Str.substr(1);
495  Offset++;
496  }
497  }
498 
499  return StringRef::npos;
500 }
501 
502 /// Canonicalize whitespaces in the file. Line endings are replaced with
503 /// UNIX-style '\n'.
504 StringRef
506  SmallVectorImpl<char> &OutputBuffer) {
507  OutputBuffer.reserve(MB.getBufferSize());
508 
509  for (const char *Ptr = MB.getBufferStart(), *End = MB.getBufferEnd();
510  Ptr != End; ++Ptr) {
511  // Eliminate trailing dosish \r.
512  if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
513  continue;
514  }
515 
516  // If current char is not a horizontal whitespace or if horizontal
517  // whitespace canonicalization is disabled, dump it to output as is.
518  if (Req.NoCanonicalizeWhiteSpace || (*Ptr != ' ' && *Ptr != '\t')) {
519  OutputBuffer.push_back(*Ptr);
520  continue;
521  }
522 
523  // Otherwise, add one space and advance over neighboring space.
524  OutputBuffer.push_back(' ');
525  while (Ptr + 1 != End && (Ptr[1] == ' ' || Ptr[1] == '\t'))
526  ++Ptr;
527  }
528 
529  // Add a null byte and then return all but that byte.
530  OutputBuffer.push_back('\0');
531  return StringRef(OutputBuffer.data(), OutputBuffer.size() - 1);
532 }
533 
534 static bool IsPartOfWord(char c) {
535  return (isalnum(c) || c == '-' || c == '_');
536 }
537 
539  assert(Count > 0 && "zero and negative counts are not supported");
540  assert((C == 1 || Kind == CheckPlain) &&
541  "count supported only for plain CHECK directives");
542  Count = C;
543  return *this;
544 }
545 
546 // Get a description of the type.
548  switch (Kind) {
549  case Check::CheckNone:
550  return "invalid";
551  case Check::CheckPlain:
552  if (Count > 1)
553  return Prefix.str() + "-COUNT";
554  return Prefix;
555  case Check::CheckNext:
556  return Prefix.str() + "-NEXT";
557  case Check::CheckSame:
558  return Prefix.str() + "-SAME";
559  case Check::CheckNot:
560  return Prefix.str() + "-NOT";
561  case Check::CheckDAG:
562  return Prefix.str() + "-DAG";
563  case Check::CheckLabel:
564  return Prefix.str() + "-LABEL";
565  case Check::CheckEmpty:
566  return Prefix.str() + "-EMPTY";
567  case Check::CheckEOF:
568  return "implicit EOF";
569  case Check::CheckBadNot:
570  return "bad NOT";
572  return "bad COUNT";
573  }
574  llvm_unreachable("unknown FileCheckType");
575 }
576 
577 static std::pair<Check::FileCheckType, StringRef>
579  if (Buffer.size() <= Prefix.size())
580  return {Check::CheckNone, StringRef()};
581 
582  char NextChar = Buffer[Prefix.size()];
583 
584  StringRef Rest = Buffer.drop_front(Prefix.size() + 1);
585  // Verify that the : is present after the prefix.
586  if (NextChar == ':')
587  return {Check::CheckPlain, Rest};
588 
589  if (NextChar != '-')
590  return {Check::CheckNone, StringRef()};
591 
592  if (Rest.consume_front("COUNT-")) {
593  int64_t Count;
594  if (Rest.consumeInteger(10, Count))
595  // Error happened in parsing integer.
596  return {Check::CheckBadCount, Rest};
597  if (Count <= 0 || Count > INT32_MAX)
598  return {Check::CheckBadCount, Rest};
599  if (!Rest.consume_front(":"))
600  return {Check::CheckBadCount, Rest};
601  return {Check::FileCheckType(Check::CheckPlain).setCount(Count), Rest};
602  }
603 
604  if (Rest.consume_front("NEXT:"))
605  return {Check::CheckNext, Rest};
606 
607  if (Rest.consume_front("SAME:"))
608  return {Check::CheckSame, Rest};
609 
610  if (Rest.consume_front("NOT:"))
611  return {Check::CheckNot, Rest};
612 
613  if (Rest.consume_front("DAG:"))
614  return {Check::CheckDAG, Rest};
615 
616  if (Rest.consume_front("LABEL:"))
617  return {Check::CheckLabel, Rest};
618 
619  if (Rest.consume_front("EMPTY:"))
620  return {Check::CheckEmpty, Rest};
621 
622  // You can't combine -NOT with another suffix.
623  if (Rest.startswith("DAG-NOT:") || Rest.startswith("NOT-DAG:") ||
624  Rest.startswith("NEXT-NOT:") || Rest.startswith("NOT-NEXT:") ||
625  Rest.startswith("SAME-NOT:") || Rest.startswith("NOT-SAME:") ||
626  Rest.startswith("EMPTY-NOT:") || Rest.startswith("NOT-EMPTY:"))
627  return {Check::CheckBadNot, Rest};
628 
629  return {Check::CheckNone, Rest};
630 }
631 
632 // From the given position, find the next character after the word.
633 static size_t SkipWord(StringRef Str, size_t Loc) {
634  while (Loc < Str.size() && IsPartOfWord(Str[Loc]))
635  ++Loc;
636  return Loc;
637 }
638 
639 /// Search the buffer for the first prefix in the prefix regular expression.
640 ///
641 /// This searches the buffer using the provided regular expression, however it
642 /// enforces constraints beyond that:
643 /// 1) The found prefix must not be a suffix of something that looks like
644 /// a valid prefix.
645 /// 2) The found prefix must be followed by a valid check type suffix using \c
646 /// FindCheckType above.
647 ///
648 /// Returns a pair of StringRefs into the Buffer, which combines:
649 /// - the first match of the regular expression to satisfy these two is
650 /// returned,
651 /// otherwise an empty StringRef is returned to indicate failure.
652 /// - buffer rewound to the location right after parsed suffix, for parsing
653 /// to continue from
654 ///
655 /// If this routine returns a valid prefix, it will also shrink \p Buffer to
656 /// start at the beginning of the returned prefix, increment \p LineNumber for
657 /// each new line consumed from \p Buffer, and set \p CheckTy to the type of
658 /// check found by examining the suffix.
659 ///
660 /// If no valid prefix is found, the state of Buffer, LineNumber, and CheckTy
661 /// is unspecified.
662 static std::pair<StringRef, StringRef>
664  unsigned &LineNumber, Check::FileCheckType &CheckTy) {
666 
667  while (!Buffer.empty()) {
668  // Find the first (longest) match using the RE.
669  if (!PrefixRE.match(Buffer, &Matches))
670  // No match at all, bail.
671  return {StringRef(), StringRef()};
672 
673  StringRef Prefix = Matches[0];
674  Matches.clear();
675 
676  assert(Prefix.data() >= Buffer.data() &&
677  Prefix.data() < Buffer.data() + Buffer.size() &&
678  "Prefix doesn't start inside of buffer!");
679  size_t Loc = Prefix.data() - Buffer.data();
680  StringRef Skipped = Buffer.substr(0, Loc);
681  Buffer = Buffer.drop_front(Loc);
682  LineNumber += Skipped.count('\n');
683 
684  // Check that the matched prefix isn't a suffix of some other check-like
685  // word.
686  // FIXME: This is a very ad-hoc check. it would be better handled in some
687  // other way. Among other things it seems hard to distinguish between
688  // intentional and unintentional uses of this feature.
689  if (Skipped.empty() || !IsPartOfWord(Skipped.back())) {
690  // Now extract the type.
691  StringRef AfterSuffix;
692  std::tie(CheckTy, AfterSuffix) = FindCheckType(Buffer, Prefix);
693 
694  // If we've found a valid check type for this prefix, we're done.
695  if (CheckTy != Check::CheckNone)
696  return {Prefix, AfterSuffix};
697  }
698 
699  // If we didn't successfully find a prefix, we need to skip this invalid
700  // prefix and continue scanning. We directly skip the prefix that was
701  // matched and any additional parts of that check-like word.
702  Buffer = Buffer.drop_front(SkipWord(Buffer, Prefix.size()));
703  }
704 
705  // We ran out of buffer while skipping partial matches so give up.
706  return {StringRef(), StringRef()};
707 }
708 
709 /// Read the check file, which specifies the sequence of expected strings.
710 ///
711 /// The strings are added to the CheckStrings vector. Returns true in case of
712 /// an error, false otherwise.
714  Regex &PrefixRE,
715  std::vector<FileCheckString> &CheckStrings) {
716  std::vector<FileCheckPattern> ImplicitNegativeChecks;
717  for (const auto &PatternString : Req.ImplicitCheckNot) {
718  // Create a buffer with fake command line content in order to display the
719  // command line option responsible for the specific implicit CHECK-NOT.
720  std::string Prefix = "-implicit-check-not='";
721  std::string Suffix = "'";
722  std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
723  Prefix + PatternString + Suffix, "command line");
724 
725  StringRef PatternInBuffer =
726  CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
727  SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
728 
729  ImplicitNegativeChecks.push_back(FileCheckPattern(Check::CheckNot));
730  ImplicitNegativeChecks.back().ParsePattern(PatternInBuffer,
731  "IMPLICIT-CHECK", SM, 0, Req);
732  }
733 
734  std::vector<FileCheckPattern> DagNotMatches = ImplicitNegativeChecks;
735 
736  // LineNumber keeps track of the line on which CheckPrefix instances are
737  // found.
738  unsigned LineNumber = 1;
739 
740  while (1) {
741  Check::FileCheckType CheckTy;
742 
743  // See if a prefix occurs in the memory buffer.
744  StringRef UsedPrefix;
745  StringRef AfterSuffix;
746  std::tie(UsedPrefix, AfterSuffix) =
747  FindFirstMatchingPrefix(PrefixRE, Buffer, LineNumber, CheckTy);
748  if (UsedPrefix.empty())
749  break;
750  assert(UsedPrefix.data() == Buffer.data() &&
751  "Failed to move Buffer's start forward, or pointed prefix outside "
752  "of the buffer!");
753  assert(AfterSuffix.data() >= Buffer.data() &&
754  AfterSuffix.data() < Buffer.data() + Buffer.size() &&
755  "Parsing after suffix doesn't start inside of buffer!");
756 
757  // Location to use for error messages.
758  const char *UsedPrefixStart = UsedPrefix.data();
759 
760  // Skip the buffer to the end of parsed suffix (or just prefix, if no good
761  // suffix was processed).
762  Buffer = AfterSuffix.empty() ? Buffer.drop_front(UsedPrefix.size())
763  : AfterSuffix;
764 
765  // Complain about useful-looking but unsupported suffixes.
766  if (CheckTy == Check::CheckBadNot) {
768  "unsupported -NOT combo on prefix '" + UsedPrefix + "'");
769  return true;
770  }
771 
772  // Complain about invalid count specification.
773  if (CheckTy == Check::CheckBadCount) {
775  "invalid count in -COUNT specification on prefix '" +
776  UsedPrefix + "'");
777  return true;
778  }
779 
780  // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
781  // leading whitespace.
782  if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
783  Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
784 
785  // Scan ahead to the end of line.
786  size_t EOL = Buffer.find_first_of("\n\r");
787 
788  // Remember the location of the start of the pattern, for diagnostics.
789  SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
790 
791  // Parse the pattern.
792  FileCheckPattern P(CheckTy);
793  if (P.ParsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, LineNumber, Req))
794  return true;
795 
796  // Verify that CHECK-LABEL lines do not define or use variables
797  if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
798  SM.PrintMessage(
799  SMLoc::getFromPointer(UsedPrefixStart), SourceMgr::DK_Error,
800  "found '" + UsedPrefix + "-LABEL:'"
801  " with variable definition or use");
802  return true;
803  }
804 
805  Buffer = Buffer.substr(EOL);
806 
807  // Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them.
808  if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame ||
809  CheckTy == Check::CheckEmpty) &&
810  CheckStrings.empty()) {
811  StringRef Type = CheckTy == Check::CheckNext
812  ? "NEXT"
813  : CheckTy == Check::CheckEmpty ? "EMPTY" : "SAME";
814  SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
816  "found '" + UsedPrefix + "-" + Type +
817  "' without previous '" + UsedPrefix + ": line");
818  return true;
819  }
820 
821  // Handle CHECK-DAG/-NOT.
822  if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
823  DagNotMatches.push_back(P);
824  continue;
825  }
826 
827  // Okay, add the string we captured to the output vector and move on.
828  CheckStrings.emplace_back(P, UsedPrefix, PatternLoc);
829  std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
830  DagNotMatches = ImplicitNegativeChecks;
831  }
832 
833  // Add an EOF pattern for any trailing CHECK-DAG/-NOTs, and use the first
834  // prefix as a filler for the error message.
835  if (!DagNotMatches.empty()) {
836  CheckStrings.emplace_back(FileCheckPattern(Check::CheckEOF), *Req.CheckPrefixes.begin(),
837  SMLoc::getFromPointer(Buffer.data()));
838  std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
839  }
840 
841  if (CheckStrings.empty()) {
842  errs() << "error: no check strings found with prefix"
843  << (Req.CheckPrefixes.size() > 1 ? "es " : " ");
844  auto I = Req.CheckPrefixes.begin();
845  auto E = Req.CheckPrefixes.end();
846  if (I != E) {
847  errs() << "\'" << *I << ":'";
848  ++I;
849  }
850  for (; I != E; ++I)
851  errs() << ", \'" << *I << ":'";
852 
853  errs() << '\n';
854  return true;
855  }
856 
857  return false;
858 }
859 
860 static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM,
861  StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat,
862  int MatchedCount, StringRef Buffer,
863  StringMap<StringRef> &VariableTable, size_t MatchPos,
864  size_t MatchLen, const FileCheckRequest &Req) {
865  if (ExpectedMatch) {
866  if (!Req.Verbose)
867  return;
868  if (!Req.VerboseVerbose && Pat.getCheckTy() == Check::CheckEOF)
869  return;
870  }
871  SMLoc MatchStart = SMLoc::getFromPointer(Buffer.data() + MatchPos);
872  SMLoc MatchEnd = SMLoc::getFromPointer(Buffer.data() + MatchPos + MatchLen);
873  SMRange MatchRange(MatchStart, MatchEnd);
874  std::string Message = formatv("{0}: {1} string found in input",
875  Pat.getCheckTy().getDescription(Prefix),
876  (ExpectedMatch ? "expected" : "excluded"))
877  .str();
878  if (Pat.getCount() > 1)
879  Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
880 
881  SM.PrintMessage(
882  Loc, ExpectedMatch ? SourceMgr::DK_Remark : SourceMgr::DK_Error, Message);
883  SM.PrintMessage(MatchStart, SourceMgr::DK_Note, "found here", {MatchRange});
884  Pat.PrintVariableUses(SM, Buffer, VariableTable, MatchRange);
885 }
886 
887 static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM,
888  const FileCheckString &CheckStr, int MatchedCount,
889  StringRef Buffer, StringMap<StringRef> &VariableTable,
890  size_t MatchPos, size_t MatchLen,
891  FileCheckRequest &Req) {
892  PrintMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat,
893  MatchedCount, Buffer, VariableTable, MatchPos, MatchLen, Req);
894 }
895 
896 static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM,
897  StringRef Prefix, SMLoc Loc,
898  const FileCheckPattern &Pat, int MatchedCount,
899  StringRef Buffer, StringMap<StringRef> &VariableTable,
900  bool VerboseVerbose) {
901  if (!ExpectedMatch && !VerboseVerbose)
902  return;
903 
904  // Otherwise, we have an error, emit an error message.
905  std::string Message = formatv("{0}: {1} string not found in input",
906  Pat.getCheckTy().getDescription(Prefix),
907  (ExpectedMatch ? "expected" : "excluded"))
908  .str();
909  if (Pat.getCount() > 1)
910  Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
911 
912  SM.PrintMessage(
913  Loc, ExpectedMatch ? SourceMgr::DK_Error : SourceMgr::DK_Remark, Message);
914 
915  // Print the "scanning from here" line. If the current position is at the
916  // end of a line, advance to the start of the next line.
917  Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
918 
920  "scanning from here");
921 
922  // Allow the pattern to print additional information if desired.
923  Pat.PrintVariableUses(SM, Buffer, VariableTable);
924  if (ExpectedMatch)
925  Pat.PrintFuzzyMatch(SM, Buffer, VariableTable);
926 }
927 
928 static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM,
929  const FileCheckString &CheckStr, int MatchedCount,
930  StringRef Buffer, StringMap<StringRef> &VariableTable,
931  bool VerboseVerbose) {
932  PrintNoMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat,
933  MatchedCount, Buffer, VariableTable, VerboseVerbose);
934 }
935 
936 /// Count the number of newlines in the specified range.
937 static unsigned CountNumNewlinesBetween(StringRef Range,
938  const char *&FirstNewLine) {
939  unsigned NumNewLines = 0;
940  while (1) {
941  // Scan for newline.
942  Range = Range.substr(Range.find_first_of("\n\r"));
943  if (Range.empty())
944  return NumNewLines;
945 
946  ++NumNewLines;
947 
948  // Handle \n\r and \r\n as a single newline.
949  if (Range.size() > 1 && (Range[1] == '\n' || Range[1] == '\r') &&
950  (Range[0] != Range[1]))
951  Range = Range.substr(1);
952  Range = Range.substr(1);
953 
954  if (NumNewLines == 1)
955  FirstNewLine = Range.begin();
956  }
957 }
958 
959 /// Match check string and its "not strings" and/or "dag strings".
960 size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
961  bool IsLabelScanMode, size_t &MatchLen,
962  StringMap<StringRef> &VariableTable,
963  FileCheckRequest &Req) const {
964  size_t LastPos = 0;
965  std::vector<const FileCheckPattern *> NotStrings;
966 
967  // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
968  // bounds; we have not processed variable definitions within the bounded block
969  // yet so cannot handle any final CHECK-DAG yet; this is handled when going
970  // over the block again (including the last CHECK-LABEL) in normal mode.
971  if (!IsLabelScanMode) {
972  // Match "dag strings" (with mixed "not strings" if any).
973  LastPos = CheckDag(SM, Buffer, NotStrings, VariableTable, Req);
974  if (LastPos == StringRef::npos)
975  return StringRef::npos;
976  }
977 
978  // Match itself from the last position after matching CHECK-DAG.
979  size_t LastMatchEnd = LastPos;
980  size_t FirstMatchPos = 0;
981  // Go match the pattern Count times. Majority of patterns only match with
982  // count 1 though.
983  assert(Pat.getCount() != 0 && "pattern count can not be zero");
984  for (int i = 1; i <= Pat.getCount(); i++) {
985  StringRef MatchBuffer = Buffer.substr(LastMatchEnd);
986  size_t CurrentMatchLen;
987  // get a match at current start point
988  size_t MatchPos = Pat.Match(MatchBuffer, CurrentMatchLen, VariableTable);
989  if (i == 1)
990  FirstMatchPos = LastPos + MatchPos;
991 
992  // report
993  if (MatchPos == StringRef::npos) {
994  PrintNoMatch(true, SM, *this, i, MatchBuffer, VariableTable,
995  Req.VerboseVerbose);
996  return StringRef::npos;
997  }
998  PrintMatch(true, SM, *this, i, MatchBuffer, VariableTable, MatchPos,
999  CurrentMatchLen, Req);
1000 
1001  // move start point after the match
1002  LastMatchEnd += MatchPos + CurrentMatchLen;
1003  }
1004  // Full match len counts from first match pos.
1005  MatchLen = LastMatchEnd - FirstMatchPos;
1006 
1007  // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
1008  // or CHECK-NOT
1009  if (!IsLabelScanMode) {
1010  StringRef SkippedRegion = Buffer.substr(LastPos, FirstMatchPos - LastPos);
1011 
1012  // If this check is a "CHECK-NEXT", verify that the previous match was on
1013  // the previous line (i.e. that there is one newline between them).
1014  if (CheckNext(SM, SkippedRegion))
1015  return StringRef::npos;
1016 
1017  // If this check is a "CHECK-SAME", verify that the previous match was on
1018  // the same line (i.e. that there is no newline between them).
1019  if (CheckSame(SM, SkippedRegion))
1020  return StringRef::npos;
1021 
1022  // If this match had "not strings", verify that they don't exist in the
1023  // skipped region.
1024  if (CheckNot(SM, SkippedRegion, NotStrings, VariableTable, Req))
1025  return StringRef::npos;
1026  }
1027 
1028  return FirstMatchPos;
1029 }
1030 
1031 /// Verify there is a single line in the given buffer.
1032 bool FileCheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const {
1033  if (Pat.getCheckTy() != Check::CheckNext &&
1034  Pat.getCheckTy() != Check::CheckEmpty)
1035  return false;
1036 
1037  Twine CheckName =
1038  Prefix +
1039  Twine(Pat.getCheckTy() == Check::CheckEmpty ? "-EMPTY" : "-NEXT");
1040 
1041  // Count the number of newlines between the previous match and this one.
1042  assert(Buffer.data() !=
1044  SMLoc::getFromPointer(Buffer.data())))
1045  ->getBufferStart() &&
1046  "CHECK-NEXT and CHECK-EMPTY can't be the first check in a file");
1047 
1048  const char *FirstNewLine = nullptr;
1049  unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
1050 
1051  if (NumNewLines == 0) {
1053  CheckName + ": is on the same line as previous match");
1055  "'next' match was here");
1057  "previous match ended here");
1058  return true;
1059  }
1060 
1061  if (NumNewLines != 1) {
1063  CheckName +
1064  ": is not on the line after the previous match");
1066  "'next' match was here");
1068  "previous match ended here");
1070  "non-matching line after previous match is here");
1071  return true;
1072  }
1073 
1074  return false;
1075 }
1076 
1077 /// Verify there is no newline in the given buffer.
1078 bool FileCheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const {
1079  if (Pat.getCheckTy() != Check::CheckSame)
1080  return false;
1081 
1082  // Count the number of newlines between the previous match and this one.
1083  assert(Buffer.data() !=
1085  SMLoc::getFromPointer(Buffer.data())))
1086  ->getBufferStart() &&
1087  "CHECK-SAME can't be the first check in a file");
1088 
1089  const char *FirstNewLine = nullptr;
1090  unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
1091 
1092  if (NumNewLines != 0) {
1094  Prefix +
1095  "-SAME: is not on the same line as the previous match");
1097  "'next' match was here");
1099  "previous match ended here");
1100  return true;
1101  }
1102 
1103  return false;
1104 }
1105 
1106 /// Verify there's no "not strings" in the given buffer.
1108  const std::vector<const FileCheckPattern *> &NotStrings,
1109  StringMap<StringRef> &VariableTable,
1110  const FileCheckRequest &Req) const {
1111  for (const FileCheckPattern *Pat : NotStrings) {
1112  assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!");
1113 
1114  size_t MatchLen = 0;
1115  size_t Pos = Pat->Match(Buffer, MatchLen, VariableTable);
1116 
1117  if (Pos == StringRef::npos) {
1118  PrintNoMatch(false, SM, Prefix, Pat->getLoc(), *Pat, 1, Buffer,
1119  VariableTable, Req.VerboseVerbose);
1120  continue;
1121  }
1122 
1123  PrintMatch(false, SM, Prefix, Pat->getLoc(), *Pat, 1, Buffer, VariableTable,
1124  Pos, MatchLen, Req);
1125 
1126  return true;
1127  }
1128 
1129  return false;
1130 }
1131 
1132 /// Match "dag strings" and their mixed "not strings".
1134  std::vector<const FileCheckPattern *> &NotStrings,
1135  StringMap<StringRef> &VariableTable,
1136  const FileCheckRequest &Req) const {
1137  if (DagNotStrings.empty())
1138  return 0;
1139 
1140  // The start of the search range.
1141  size_t StartPos = 0;
1142 
1143  struct MatchRange {
1144  size_t Pos;
1145  size_t End;
1146  };
1147  // A sorted list of ranges for non-overlapping CHECK-DAG matches. Match
1148  // ranges are erased from this list once they are no longer in the search
1149  // range.
1150  std::list<MatchRange> MatchRanges;
1151 
1152  // We need PatItr and PatEnd later for detecting the end of a CHECK-DAG
1153  // group, so we don't use a range-based for loop here.
1154  for (auto PatItr = DagNotStrings.begin(), PatEnd = DagNotStrings.end();
1155  PatItr != PatEnd; ++PatItr) {
1156  const FileCheckPattern &Pat = *PatItr;
1157  assert((Pat.getCheckTy() == Check::CheckDAG ||
1158  Pat.getCheckTy() == Check::CheckNot) &&
1159  "Invalid CHECK-DAG or CHECK-NOT!");
1160 
1161  if (Pat.getCheckTy() == Check::CheckNot) {
1162  NotStrings.push_back(&Pat);
1163  continue;
1164  }
1165 
1166  assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!");
1167 
1168  // CHECK-DAG always matches from the start.
1169  size_t MatchLen = 0, MatchPos = StartPos;
1170 
1171  // Search for a match that doesn't overlap a previous match in this
1172  // CHECK-DAG group.
1173  for (auto MI = MatchRanges.begin(), ME = MatchRanges.end(); true; ++MI) {
1174  StringRef MatchBuffer = Buffer.substr(MatchPos);
1175  size_t MatchPosBuf = Pat.Match(MatchBuffer, MatchLen, VariableTable);
1176  // With a group of CHECK-DAGs, a single mismatching means the match on
1177  // that group of CHECK-DAGs fails immediately.
1178  if (MatchPosBuf == StringRef::npos) {
1179  PrintNoMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, MatchBuffer,
1180  VariableTable, Req.VerboseVerbose);
1181  return StringRef::npos;
1182  }
1183  // Re-calc it as the offset relative to the start of the original string.
1184  MatchPos += MatchPosBuf;
1185  if (Req.VerboseVerbose)
1186  PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer,
1187  VariableTable, MatchPos, MatchLen, Req);
1188  MatchRange M{MatchPos, MatchPos + MatchLen};
1189  if (Req.AllowDeprecatedDagOverlap) {
1190  // We don't need to track all matches in this mode, so we just maintain
1191  // one match range that encompasses the current CHECK-DAG group's
1192  // matches.
1193  if (MatchRanges.empty())
1194  MatchRanges.insert(MatchRanges.end(), M);
1195  else {
1196  auto Block = MatchRanges.begin();
1197  Block->Pos = std::min(Block->Pos, M.Pos);
1198  Block->End = std::max(Block->End, M.End);
1199  }
1200  break;
1201  }
1202  // Iterate previous matches until overlapping match or insertion point.
1203  bool Overlap = false;
1204  for (; MI != ME; ++MI) {
1205  if (M.Pos < MI->End) {
1206  // !Overlap => New match has no overlap and is before this old match.
1207  // Overlap => New match overlaps this old match.
1208  Overlap = MI->Pos < M.End;
1209  break;
1210  }
1211  }
1212  if (!Overlap) {
1213  // Insert non-overlapping match into list.
1214  MatchRanges.insert(MI, M);
1215  break;
1216  }
1217  if (Req.VerboseVerbose) {
1218  SMLoc OldStart = SMLoc::getFromPointer(Buffer.data() + MI->Pos);
1219  SMLoc OldEnd = SMLoc::getFromPointer(Buffer.data() + MI->End);
1220  SMRange OldRange(OldStart, OldEnd);
1221  SM.PrintMessage(OldStart, SourceMgr::DK_Note,
1222  "match discarded, overlaps earlier DAG match here",
1223  {OldRange});
1224  }
1225  MatchPos = MI->End;
1226  }
1227  if (!Req.VerboseVerbose)
1228  PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer, VariableTable,
1229  MatchPos, MatchLen, Req);
1230 
1231  // Handle the end of a CHECK-DAG group.
1232  if (std::next(PatItr) == PatEnd ||
1233  std::next(PatItr)->getCheckTy() == Check::CheckNot) {
1234  if (!NotStrings.empty()) {
1235  // If there are CHECK-NOTs between two CHECK-DAGs or from CHECK to
1236  // CHECK-DAG, verify that there are no 'not' strings occurred in that
1237  // region.
1238  StringRef SkippedRegion =
1239  Buffer.slice(StartPos, MatchRanges.begin()->Pos);
1240  if (CheckNot(SM, SkippedRegion, NotStrings, VariableTable, Req))
1241  return StringRef::npos;
1242  // Clear "not strings".
1243  NotStrings.clear();
1244  }
1245  // All subsequent CHECK-DAGs and CHECK-NOTs should be matched from the
1246  // end of this CHECK-DAG group's match range.
1247  StartPos = MatchRanges.rbegin()->End;
1248  // Don't waste time checking for (impossible) overlaps before that.
1249  MatchRanges.clear();
1250  }
1251  }
1252 
1253  return StartPos;
1254 }
1255 
1256 // A check prefix must contain only alphanumeric, hyphens and underscores.
1257 static bool ValidateCheckPrefix(StringRef CheckPrefix) {
1258  Regex Validator("^[a-zA-Z0-9_-]*$");
1259  return Validator.match(CheckPrefix);
1260 }
1261 
1263  StringSet<> PrefixSet;
1264 
1265  for (StringRef Prefix : Req.CheckPrefixes) {
1266  // Reject empty prefixes.
1267  if (Prefix == "")
1268  return false;
1269 
1270  if (!PrefixSet.insert(Prefix).second)
1271  return false;
1272 
1273  if (!ValidateCheckPrefix(Prefix))
1274  return false;
1275  }
1276 
1277  return true;
1278 }
1279 
1280 // Combines the check prefixes into a single regex so that we can efficiently
1281 // scan for any of the set.
1282 //
1283 // The semantics are that the longest-match wins which matches our regex
1284 // library.
1286  // I don't think there's a way to specify an initial value for cl::list,
1287  // so if nothing was specified, add the default
1288  if (Req.CheckPrefixes.empty())
1289  Req.CheckPrefixes.push_back("CHECK");
1290 
1291  // We already validated the contents of CheckPrefixes so just concatenate
1292  // them as alternatives.
1293  SmallString<32> PrefixRegexStr;
1294  for (StringRef Prefix : Req.CheckPrefixes) {
1295  if (Prefix != Req.CheckPrefixes.front())
1296  PrefixRegexStr.push_back('|');
1297 
1298  PrefixRegexStr.append(Prefix);
1299  }
1300 
1301  return Regex(PrefixRegexStr);
1302 }
1303 
1304 // Remove local variables from \p VariableTable. Global variables
1305 // (start with '$') are preserved.
1306 static void ClearLocalVars(StringMap<StringRef> &VariableTable) {
1307  SmallVector<StringRef, 16> LocalVars;
1308  for (const auto &Var : VariableTable)
1309  if (Var.first()[0] != '$')
1310  LocalVars.push_back(Var.first());
1311 
1312  for (const auto &Var : LocalVars)
1313  VariableTable.erase(Var);
1314 }
1315 
1316 /// Check the input to FileCheck provided in the \p Buffer against the \p
1317 /// CheckStrings read from the check file.
1318 ///
1319 /// Returns false if the input fails to satisfy the checks.
1321  ArrayRef<FileCheckString> CheckStrings) {
1322  bool ChecksFailed = false;
1323 
1324  /// VariableTable - This holds all the current filecheck variables.
1325  StringMap<StringRef> VariableTable;
1326 
1327  for (const auto& Def : Req.GlobalDefines)
1328  VariableTable.insert(StringRef(Def).split('='));
1329 
1330  unsigned i = 0, j = 0, e = CheckStrings.size();
1331  while (true) {
1332  StringRef CheckRegion;
1333  if (j == e) {
1334  CheckRegion = Buffer;
1335  } else {
1336  const FileCheckString &CheckLabelStr = CheckStrings[j];
1337  if (CheckLabelStr.Pat.getCheckTy() != Check::CheckLabel) {
1338  ++j;
1339  continue;
1340  }
1341 
1342  // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
1343  size_t MatchLabelLen = 0;
1344  size_t MatchLabelPos =
1345  CheckLabelStr.Check(SM, Buffer, true, MatchLabelLen, VariableTable,
1346  Req);
1347  if (MatchLabelPos == StringRef::npos)
1348  // Immediately bail of CHECK-LABEL fails, nothing else we can do.
1349  return false;
1350 
1351  CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen);
1352  Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen);
1353  ++j;
1354  }
1355 
1356  if (Req.EnableVarScope)
1357  ClearLocalVars(VariableTable);
1358 
1359  for (; i != j; ++i) {
1360  const FileCheckString &CheckStr = CheckStrings[i];
1361 
1362  // Check each string within the scanned region, including a second check
1363  // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
1364  size_t MatchLen = 0;
1365  size_t MatchPos =
1366  CheckStr.Check(SM, CheckRegion, false, MatchLen, VariableTable, Req);
1367 
1368  if (MatchPos == StringRef::npos) {
1369  ChecksFailed = true;
1370  i = j;
1371  break;
1372  }
1373 
1374  CheckRegion = CheckRegion.substr(MatchPos + MatchLen);
1375  }
1376 
1377  if (j == e)
1378  break;
1379  }
1380 
1381  // Success if no checks failed.
1382  return !ChecksFailed;
1383 }
uint64_t CallInst * C
Represents a range in source code.
Definition: SMLoc.h:49
void push_back(const T &Elt)
Definition: SmallVector.h:218
bool ValidateCheckPrefixes()
Definition: FileCheck.cpp:1262
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
Definition: SourceMgr.cpp:62
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
size_t CheckDag(const SourceMgr &SM, StringRef Buffer, std::vector< const FileCheckPattern *> &NotStrings, StringMap< StringRef > &VariableTable, const FileCheckRequest &Req) const
Match "dag strings" and their mixed "not strings".
Definition: FileCheck.cpp:1133
bool CheckNot(const SourceMgr &SM, StringRef Buffer, const std::vector< const FileCheckPattern *> &NotStrings, StringMap< StringRef > &VariableTable, const FileCheckRequest &Req) const
Verify there&#39;s no "not strings" in the given buffer.
Definition: FileCheck.cpp:1107
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
bool CheckSame(const SourceMgr &SM, StringRef Buffer) const
Verify there is no newline in the given buffer.
Definition: FileCheck.cpp:1078
static std::string escape(StringRef String)
Turn String into a regex by escaping its special characters.
Definition: Regex.cpp:201
iterator find(StringRef Key)
Definition: StringMap.h:333
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:510
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
std::string getDescription(StringRef Prefix) const
Definition: FileCheck.cpp:547
static std::pair< StringRef, StringRef > split(StringRef Str, char Separator)
Checked version of split, to ensure mandatory subparts.
Definition: DataLayout.cpp:202
Marks when parsing found a -NOT check combined with another CHECK suffix.
Definition: FileCheck.h:62
size_t getBufferSize() const
Definition: MemoryBuffer.h:62
void reserve(size_type N)
Definition: SmallVector.h:376
LLVM_NODISCARD unsigned edit_distance(StringRef Other, bool AllowReplacements=true, unsigned MaxEditDistance=0) const
Determine the edit distance between this string and another string.
Definition: StringRef.cpp:95
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
bool CheckNext(const SourceMgr &SM, StringRef Buffer) const
Verify there is a single line in the given buffer.
Definition: FileCheck.cpp:1032
bool AllowDeprecatedDagOverlap
Definition: FileCheck.h:35
SMLoc Start
Definition: SMLoc.h:51
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckSame(const unsigned char *MatcherTable, unsigned &MatcherIndex, SDValue N, const SmallVectorImpl< std::pair< SDValue, SDNode *>> &RecordedNodes)
CheckSame - Implements OP_CheckSame.
StringRef Prefix
Which prefix name this check matched.
Definition: FileCheck.h:158
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
FileCheckPattern Pat
The pattern to match.
Definition: FileCheck.h:155
void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges=None, ArrayRef< SMFixIt > FixIts=None, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
Definition: SourceMgr.cpp:248
amdgpu Simplify well known AMD library false Value Value const Twine & Name
size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode, size_t &MatchLen, StringMap< StringRef > &VariableTable, FileCheckRequest &Req) const
Match check string and its "not strings" and/or "dag strings".
Definition: FileCheck.cpp:960
bool ParsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM, unsigned LineNumber, const FileCheckRequest &Req)
Parses the given string into the Pattern.
Definition: FileCheck.cpp:34
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LLVM_NODISCARD size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:476
SMLoc Loc
The location in the match file that the check string was specified.
Definition: FileCheck.h:161
Marks when parsing found a -COUNT directive with invalid count value.
Definition: FileCheck.h:65
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition: SourceMgr.h:131
std::string itostr(int64_t X)
Definition: StringExtras.h:239
static std::pair< Check::FileCheckType, StringRef > FindCheckType(StringRef Buffer, StringRef Prefix)
Definition: FileCheck.cpp:578
static unsigned CountNumNewlinesBetween(StringRef Range, const char *&FirstNewLine)
Count the number of newlines in the specified range.
Definition: FileCheck.cpp:937
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:152
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
StringRef CanonicalizeFile(MemoryBuffer &MB, SmallVectorImpl< char > &OutputBuffer)
Canonicalize whitespaces in the file.
Definition: FileCheck.cpp:505
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
Contains info about various FileCheck options.
Definition: FileCheck.h:27
static bool ValidateCheckPrefix(StringRef CheckPrefix)
Definition: FileCheck.cpp:1257
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
void PrintVariableUses(const SourceMgr &SM, StringRef Buffer, const StringMap< StringRef > &VariableTable, SMRange MatchRange=None) const
Definition: FileCheck.cpp:371
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:75
#define P(N)
bool CheckInput(SourceMgr &SM, StringRef Buffer, ArrayRef< FileCheckString > CheckStrings)
Check the input to FileCheck provided in the Buffer against the CheckStrings read from the check file...
Definition: FileCheck.cpp:1320
A check that we found in the input file.
Definition: FileCheck.h:153
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
bool ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE, std::vector< FileCheckString > &CheckStrings)
Read the check file, which specifies the sequence of expected strings.
Definition: FileCheck.cpp:713
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM, StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat, int MatchedCount, StringRef Buffer, StringMap< StringRef > &VariableTable, bool VerboseVerbose)
Definition: FileCheck.cpp:896
LLVM_NODISCARD size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition: StringRef.cpp:250
SMLoc getLoc() const
Returns the location in source code.
Definition: FileCheck.h:118
static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM, StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat, int MatchedCount, StringRef Buffer, StringMap< StringRef > &VariableTable, size_t MatchPos, size_t MatchLen, const FileCheckRequest &Req)
Definition: FileCheck.cpp:860
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:42
Check::FileCheckType getCheckTy() const
Definition: FileCheck.h:134
size_t size() const
Definition: SmallVector.h:53
LLVM_NODISCARD char back() const
back - Get the last character in the string.
Definition: StringRef.h:149
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::pair< typename base::iterator, bool > insert(StringRef Key)
Definition: StringSet.h:38
size_t Match(StringRef Buffer, size_t &MatchLen, StringMap< StringRef > &VariableTable) const
Matches the pattern string against the input buffer Buffer.
Definition: FileCheck.cpp:275
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:497
Indicates the pattern only matches the end of file.
Definition: FileCheck.h:59
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_front(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with the first N elements dropped.
Definition: StringRef.h:645
bool isValid() const
Definition: SMLoc.h:60
static size_t SkipWord(StringRef Str, size_t Loc)
Definition: FileCheck.cpp:633
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning &#39;\&#39;, &#39;&#39;, &#39; &#39;, &#39;"&#39;, and anything that doesn&#39;t satisfy llvm::isPrint into an escape...
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1029
static std::pair< StringRef, StringRef > FindFirstMatchingPrefix(Regex &PrefixRE, StringRef &Buffer, unsigned &LineNumber, Check::FileCheckType &CheckTy)
Search the buffer for the first prefix in the prefix regular expression.
Definition: FileCheck.cpp:663
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:710
bool isValid(std::string &Error) const
isValid - returns the error encountered during regex compilation, or matching, if any...
Definition: Regex.cpp:56
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:535
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:366
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:42
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:941
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it...
unsigned getNumMatches() const
getNumMatches - In a valid regex, return the number of parenthesized matches it contains.
Definition: Regex.cpp:69
iterator begin() const
Definition: StringRef.h:106
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:37
const char * getBufferEnd() const
Definition: MemoryBuffer.h:61
void PrintFuzzyMatch(const SourceMgr &SM, StringRef Buffer, const StringMap< StringRef > &VariableTable) const
Definition: FileCheck.cpp:415
FileCheckType & setCount(int C)
Definition: FileCheck.cpp:538
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:149
static const size_t npos
Definition: StringRef.h:51
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
Regex buildCheckPrefixRegex()
Definition: FileCheck.cpp:1285
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:395
#define I(x, y, z)
Definition: MD5.cpp:58
const char * getBufferStart() const
Definition: MemoryBuffer.h:60
static bool IsPartOfWord(char c)
Definition: FileCheck.cpp:534
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM_NODISCARD char front() const
front - Get the first character in the string.
Definition: StringRef.h:142
LLVM Value Representation.
Definition: Value.h:73
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:28
bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr)
matches - Match the regex against a given String.
Definition: Regex.cpp:73
FileCheckPattern(Check::FileCheckType Ty)
Definition: FileCheck.h:114
IRTranslator LLVM IR MI
Compile for newline-sensitive matching.
Definition: Regex.h:39
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static void ClearLocalVars(StringMap< StringRef > &VariableTable)
Definition: FileCheck.cpp:1306
int getCount() const
Definition: FileCheck.h:136
Represents a location in source code.
Definition: SMLoc.h:24
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:298
iterator end() const
Definition: StringRef.h:108
iterator end()
Definition: StringMap.h:318