LLVM  9.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 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // FileCheck does a line-by line check of a file that validates whether it
10 // contains the expected content. This is useful for regression tests etc.
11 //
12 // This file implements most of the API that will be used by the FileCheck utility
13 // as well as various unittests.
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Support/FileCheck.h"
17 #include "llvm/ADT/StringSet.h"
19 #include <cstdint>
20 #include <list>
21 #include <map>
22 #include <tuple>
23 #include <utility>
24 
25 using namespace llvm;
26 
27 bool FileCheckNumericVariable::setValue(uint64_t NewValue) {
28  if (Value)
29  return true;
30  Value = NewValue;
31  return false;
32 }
33 
35  if (!Value)
36  return true;
37  Value = None;
38  return false;
39 }
40 
42  Optional<uint64_t> LeftOp = this->LeftOp->getValue();
43  // Variable is undefined.
44  if (!LeftOp)
45  return None;
46  return EvalBinop(*LeftOp, RightOp);
47 }
48 
50  if (!LeftOp->getValue())
51  return LeftOp->getName();
52  return StringRef();
53 }
54 
56  Optional<uint64_t> EvaluatedValue = NumExpr->eval();
57  if (!EvaluatedValue)
58  return None;
59  return utostr(*EvaluatedValue);
60 }
61 
63  // Look up the value and escape it so that we can put it into the regex.
64  Optional<StringRef> VarVal = Context->getPatternVarValue(FromStr);
65  if (!VarVal)
66  return None;
67  return Regex::escape(*VarVal);
68 }
69 
71  // Although a use of an undefined numeric variable is detected at parse
72  // time, a numeric variable can be undefined later by ClearLocalVariables.
73  return NumExpr->getUndefVarName();
74 }
75 
77  if (!Context->getPatternVarValue(FromStr))
78  return FromStr;
79 
80  return StringRef();
81 }
82 
84  return C == '_' || isalpha(C);
85 }
86 
87 bool FileCheckPattern::parseVariable(StringRef Str, bool &IsPseudo,
88  unsigned &TrailIdx) {
89  if (Str.empty())
90  return true;
91 
92  bool ParsedOneChar = false;
93  unsigned I = 0;
94  IsPseudo = Str[0] == '@';
95 
96  // Global vars start with '$'.
97  if (Str[0] == '$' || IsPseudo)
98  ++I;
99 
100  for (unsigned E = Str.size(); I != E; ++I) {
101  if (!ParsedOneChar && !isValidVarNameStart(Str[I]))
102  return true;
103 
104  // Variable names are composed of alphanumeric characters and underscores.
105  if (Str[I] != '_' && !isalnum(Str[I]))
106  break;
107  ParsedOneChar = true;
108  }
109 
110  TrailIdx = I;
111  return false;
112 }
113 
114 // StringRef holding all characters considered as horizontal whitespaces by
115 // FileCheck input canonicalization.
117 
118 // Parsing helper function that strips the first character in S and returns it.
119 static char popFront(StringRef &S) {
120  char C = S.front();
121  S = S.drop_front();
122  return C;
123 }
124 
125 static uint64_t add(uint64_t LeftOp, uint64_t RightOp) {
126  return LeftOp + RightOp;
127 }
128 static uint64_t sub(uint64_t LeftOp, uint64_t RightOp) {
129  return LeftOp - RightOp;
130 }
131 
134  StringRef Trailer,
135  const SourceMgr &SM) const {
136  if (IsPseudo && !Name.equals("@LINE")) {
138  "invalid pseudo numeric variable '" + Name + "'");
139  return nullptr;
140  }
141 
142  // This method is indirectly called from ParsePattern for all numeric
143  // variable definitions and uses in the order in which they appear in the
144  // CHECK pattern. For each definition, the pointer to the class instance of
145  // the corresponding numeric variable definition is stored in
146  // GlobalNumericVariableTable. Therefore, the pointer we get below is for the
147  // class instance corresponding to the last definition of this variable use.
148  auto VarTableIter = Context->GlobalNumericVariableTable.find(Name);
149  if (VarTableIter == Context->GlobalNumericVariableTable.end()) {
151  "using undefined numeric variable '" + Name + "'");
152  return nullptr;
153  }
154 
155  FileCheckNumericVariable *LeftOp = VarTableIter->second;
156 
157  // Check if this is a supported operation and select a function to perform
158  // it.
159  Trailer = Trailer.ltrim(SpaceChars);
160  if (Trailer.empty()) {
161  return Context->makeNumExpr(add, LeftOp, 0);
162  }
163  SMLoc OpLoc = SMLoc::getFromPointer(Trailer.data());
164  char Operator = popFront(Trailer);
165  binop_eval_t EvalBinop;
166  switch (Operator) {
167  case '+':
168  EvalBinop = add;
169  break;
170  case '-':
171  EvalBinop = sub;
172  break;
173  default:
175  Twine("unsupported numeric operation '") + Twine(Operator) +
176  "'");
177  return nullptr;
178  }
179 
180  // Parse right operand.
181  Trailer = Trailer.ltrim(SpaceChars);
182  if (Trailer.empty()) {
184  "missing operand in numeric expression");
185  return nullptr;
186  }
187  uint64_t RightOp;
188  if (Trailer.consumeInteger(10, RightOp)) {
190  "invalid offset in numeric expression '" + Trailer + "'");
191  return nullptr;
192  }
193  Trailer = Trailer.ltrim(SpaceChars);
194  if (!Trailer.empty()) {
196  "unexpected characters at end of numeric expression '" +
197  Trailer + "'");
198  return nullptr;
199  }
200 
201  return Context->makeNumExpr(EvalBinop, LeftOp, RightOp);
202 }
203 
205  SourceMgr &SM, unsigned LineNumber,
206  const FileCheckRequest &Req) {
207  bool MatchFullLinesHere = Req.MatchFullLines && CheckTy != Check::CheckNot;
208 
209  this->LineNumber = LineNumber;
210  PatternLoc = SMLoc::getFromPointer(PatternStr.data());
211 
212  // Create fake @LINE pseudo variable definition.
213  StringRef LinePseudo = "@LINE";
214  uint64_t LineNumber64 = LineNumber;
215  FileCheckNumericVariable *LinePseudoVar =
216  Context->makeNumericVariable(LinePseudo, LineNumber64);
217  Context->GlobalNumericVariableTable[LinePseudo] = LinePseudoVar;
218 
219  if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
220  // Ignore trailing whitespace.
221  while (!PatternStr.empty() &&
222  (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
223  PatternStr = PatternStr.substr(0, PatternStr.size() - 1);
224 
225  // Check that there is something on the line.
226  if (PatternStr.empty() && CheckTy != Check::CheckEmpty) {
227  SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
228  "found empty check string with prefix '" + Prefix + ":'");
229  return true;
230  }
231 
232  if (!PatternStr.empty() && CheckTy == Check::CheckEmpty) {
233  SM.PrintMessage(
234  PatternLoc, SourceMgr::DK_Error,
235  "found non-empty check string for empty check with prefix '" + Prefix +
236  ":'");
237  return true;
238  }
239 
240  if (CheckTy == Check::CheckEmpty) {
241  RegExStr = "(\n$)";
242  return false;
243  }
244 
245  // Check to see if this is a fixed string, or if it has regex pieces.
246  if (!MatchFullLinesHere &&
247  (PatternStr.size() < 2 || (PatternStr.find("{{") == StringRef::npos &&
248  PatternStr.find("[[") == StringRef::npos))) {
249  FixedStr = PatternStr;
250  return false;
251  }
252 
253  if (MatchFullLinesHere) {
254  RegExStr += '^';
255  if (!Req.NoCanonicalizeWhiteSpace)
256  RegExStr += " *";
257  }
258 
259  // Paren value #0 is for the fully matched string. Any new parenthesized
260  // values add from there.
261  unsigned CurParen = 1;
262 
263  // Otherwise, there is at least one regex piece. Build up the regex pattern
264  // by escaping scary characters in fixed strings, building up one big regex.
265  while (!PatternStr.empty()) {
266  // RegEx matches.
267  if (PatternStr.startswith("{{")) {
268  // This is the start of a regex match. Scan for the }}.
269  size_t End = PatternStr.find("}}");
270  if (End == StringRef::npos) {
271  SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
273  "found start of regex string with no end '}}'");
274  return true;
275  }
276 
277  // Enclose {{}} patterns in parens just like [[]] even though we're not
278  // capturing the result for any purpose. This is required in case the
279  // expression contains an alternation like: CHECK: abc{{x|z}}def. We
280  // want this to turn into: "abc(x|z)def" not "abcx|zdef".
281  RegExStr += '(';
282  ++CurParen;
283 
284  if (AddRegExToRegEx(PatternStr.substr(2, End - 2), CurParen, SM))
285  return true;
286  RegExStr += ')';
287 
288  PatternStr = PatternStr.substr(End + 2);
289  continue;
290  }
291 
292  // String and numeric substitution blocks. String substitution blocks come
293  // in two forms: [[foo:.*]] and [[foo]]. The former matches .* (or some
294  // other regex) and assigns it to the string variable 'foo'. The latter
295  // substitutes foo's value. Numeric substitution blocks start with a
296  // '#' sign after the double brackets and only have the substitution form.
297  // Both string and numeric variables must satisfy the regular expression
298  // "[a-zA-Z_][0-9a-zA-Z_]*" to be valid, as this helps catch some common
299  // errors.
300  if (PatternStr.startswith("[[")) {
301  StringRef UnparsedPatternStr = PatternStr.substr(2);
302  // Find the closing bracket pair ending the match. End is going to be an
303  // offset relative to the beginning of the match string.
304  size_t End = FindRegexVarEnd(UnparsedPatternStr, SM);
305  StringRef MatchStr = UnparsedPatternStr.substr(0, End);
306  bool IsNumBlock = MatchStr.consume_front("#");
307 
308  if (End == StringRef::npos) {
309  SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
311  "Invalid substitution block, no ]] found");
312  return true;
313  }
314  // Strip the substitution block we are parsing. End points to the start
315  // of the "]]" closing the expression so account for it in computing the
316  // index of the first unparsed character.
317  PatternStr = UnparsedPatternStr.substr(End + 2);
318 
319  size_t VarEndIdx = MatchStr.find(":");
320  if (IsNumBlock)
321  MatchStr = MatchStr.ltrim(SpaceChars);
322  else {
323  size_t SpacePos = MatchStr.substr(0, VarEndIdx).find_first_of(" \t");
324  if (SpacePos != StringRef::npos) {
325  SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data() + SpacePos),
326  SourceMgr::DK_Error, "unexpected whitespace");
327  return true;
328  }
329  }
330 
331  // Get the variable name (e.g. "foo") and verify it is well formed.
332  bool IsPseudo;
333  unsigned TrailIdx;
334  if (parseVariable(MatchStr, IsPseudo, TrailIdx)) {
335  SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data()),
336  SourceMgr::DK_Error, "invalid variable name");
337  return true;
338  }
339 
340  size_t SubstInsertIdx = RegExStr.size();
341  FileCheckNumExpr *NumExpr;
342 
343  StringRef Name = MatchStr.substr(0, TrailIdx);
344  StringRef Trailer = MatchStr.substr(TrailIdx);
345  bool IsVarDef = (VarEndIdx != StringRef::npos);
346 
347  if (IsVarDef) {
348  if (IsPseudo || !Trailer.consume_front(":")) {
349  SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data()),
351  "invalid name in string variable definition");
352  return true;
353  }
354 
355  // Detect collisions between string and numeric variables when the
356  // former is created later than the latter.
357  if (Context->GlobalNumericVariableTable.find(Name) !=
358  Context->GlobalNumericVariableTable.end()) {
359  SM.PrintMessage(
361  "numeric variable with name '" + Name + "' already exists");
362  return true;
363  }
364  }
365 
366  if (IsNumBlock || (!IsVarDef && IsPseudo)) {
367  NumExpr = parseNumericSubstitution(Name, IsPseudo, Trailer, SM);
368  if (NumExpr == nullptr)
369  return true;
370  IsNumBlock = true;
371  }
372 
373  // Handle substitutions: [[foo]] and [[#<foo expr>]].
374  if (!IsVarDef) {
375  // Handle substitution of string variables that were defined earlier on
376  // the same line by emitting a backreference.
377  if (!IsNumBlock && VariableDefs.find(Name) != VariableDefs.end()) {
378  unsigned CaptureParen = VariableDefs[Name];
379  if (CaptureParen < 1 || CaptureParen > 9) {
382  "Can't back-reference more than 9 variables");
383  return true;
384  }
385  AddBackrefToRegEx(CaptureParen);
386  } else {
387  // Handle substitution of string variables ([[<var>]]) defined in
388  // previous CHECK patterns, and substitution of numeric expressions.
389  FileCheckSubstitution *Substitution =
390  IsNumBlock
391  ? Context->makeNumericSubstitution(MatchStr, NumExpr,
392  SubstInsertIdx)
393  : Context->makeStringSubstitution(MatchStr, SubstInsertIdx);
394  Substitutions.push_back(Substitution);
395  }
396  continue;
397  }
398 
399  // Handle variable definitions: [[foo:.*]].
400  VariableDefs[Name] = CurParen;
401  RegExStr += '(';
402  ++CurParen;
403 
404  if (AddRegExToRegEx(Trailer, CurParen, SM))
405  return true;
406 
407  RegExStr += ')';
408  }
409 
410  // Handle fixed string matches.
411  // Find the end, which is the start of the next regex.
412  size_t FixedMatchEnd = PatternStr.find("{{");
413  FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[["));
414  RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd));
415  PatternStr = PatternStr.substr(FixedMatchEnd);
416  }
417 
418  if (MatchFullLinesHere) {
419  if (!Req.NoCanonicalizeWhiteSpace)
420  RegExStr += " *";
421  RegExStr += '$';
422  }
423 
424  return false;
425 }
426 
427 bool FileCheckPattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) {
428  Regex R(RS);
429  std::string Error;
430  if (!R.isValid(Error)) {
432  "invalid regex: " + Error);
433  return true;
434  }
435 
436  RegExStr += RS.str();
437  CurParen += R.getNumMatches();
438  return false;
439 }
440 
441 void FileCheckPattern::AddBackrefToRegEx(unsigned BackrefNum) {
442  assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number");
443  std::string Backref = std::string("\\") + std::string(1, '0' + BackrefNum);
444  RegExStr += Backref;
445 }
446 
447 size_t FileCheckPattern::match(StringRef Buffer, size_t &MatchLen) const {
448  // If this is the EOF pattern, match it immediately.
449  if (CheckTy == Check::CheckEOF) {
450  MatchLen = 0;
451  return Buffer.size();
452  }
453 
454  // If this is a fixed string pattern, just match it now.
455  if (!FixedStr.empty()) {
456  MatchLen = FixedStr.size();
457  return Buffer.find(FixedStr);
458  }
459 
460  // Regex match.
461 
462  // If there are substitutions, we need to create a temporary string with the
463  // actual value.
464  StringRef RegExToMatch = RegExStr;
465  std::string TmpStr;
466  if (!Substitutions.empty()) {
467  TmpStr = RegExStr;
468 
469  size_t InsertOffset = 0;
470  // Substitute all string variables and numeric expressions whose values are
471  // only now known. Use of string variables defined on the same line are
472  // handled by back-references.
473  for (const auto &Substitution : Substitutions) {
474  // Substitute and check for failure (e.g. use of undefined variable).
475  Optional<std::string> Value = Substitution->getResult();
476  if (!Value)
477  return StringRef::npos;
478 
479  // Plop it into the regex at the adjusted offset.
480  TmpStr.insert(TmpStr.begin() + Substitution->getIndex() + InsertOffset,
481  Value->begin(), Value->end());
482  InsertOffset += Value->size();
483  }
484 
485  // Match the newly constructed regex.
486  RegExToMatch = TmpStr;
487  }
488 
489  SmallVector<StringRef, 4> MatchInfo;
490  if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo))
491  return StringRef::npos;
492 
493  // Successful regex match.
494  assert(!MatchInfo.empty() && "Didn't get any match");
495  StringRef FullMatch = MatchInfo[0];
496 
497  // If this defines any string variables, remember their values.
498  for (const auto &VariableDef : VariableDefs) {
499  assert(VariableDef.second < MatchInfo.size() && "Internal paren error");
500  Context->GlobalVariableTable[VariableDef.first] =
501  MatchInfo[VariableDef.second];
502  }
503 
504  // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after
505  // the required preceding newline, which is consumed by the pattern in the
506  // case of CHECK-EMPTY but not CHECK-NEXT.
507  size_t MatchStartSkip = CheckTy == Check::CheckEmpty;
508  MatchLen = FullMatch.size() - MatchStartSkip;
509  return FullMatch.data() - Buffer.data() + MatchStartSkip;
510 }
511 
512 unsigned FileCheckPattern::computeMatchDistance(StringRef Buffer) const {
513  // Just compute the number of matching characters. For regular expressions, we
514  // just compare against the regex itself and hope for the best.
515  //
516  // FIXME: One easy improvement here is have the regex lib generate a single
517  // example regular expression which matches, and use that as the example
518  // string.
519  StringRef ExampleString(FixedStr);
520  if (ExampleString.empty())
521  ExampleString = RegExStr;
522 
523  // Only compare up to the first line in the buffer, or the string size.
524  StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
525  BufferPrefix = BufferPrefix.split('\n').first;
526  return BufferPrefix.edit_distance(ExampleString);
527 }
528 
530  SMRange MatchRange) const {
531  // Print what we know about substitutions.
532  if (!Substitutions.empty()) {
533  for (const auto &Substitution : Substitutions) {
534  SmallString<256> Msg;
535  raw_svector_ostream OS(Msg);
536  Optional<std::string> MatchedValue = Substitution->getResult();
537 
538  // Substitution failed or is not known at match time, print the undefined
539  // variable it uses.
540  if (!MatchedValue) {
541  StringRef UndefVarName = Substitution->getUndefVarName();
542  if (UndefVarName.empty())
543  continue;
544  OS << "uses undefined variable \"";
545  OS.write_escaped(UndefVarName) << "\"";
546  } else {
547  // Substitution succeeded. Print substituted value.
548  OS << "with \"";
549  OS.write_escaped(Substitution->getFromString()) << "\" equal to \"";
550  OS.write_escaped(*MatchedValue) << "\"";
551  }
552 
553  if (MatchRange.isValid())
554  SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, OS.str(),
555  {MatchRange});
556  else
558  SourceMgr::DK_Note, OS.str());
559  }
560  }
561 }
562 
564  const SourceMgr &SM, SMLoc Loc,
565  Check::FileCheckType CheckTy,
566  StringRef Buffer, size_t Pos, size_t Len,
567  std::vector<FileCheckDiag> *Diags,
568  bool AdjustPrevDiag = false) {
569  SMLoc Start = SMLoc::getFromPointer(Buffer.data() + Pos);
570  SMLoc End = SMLoc::getFromPointer(Buffer.data() + Pos + Len);
571  SMRange Range(Start, End);
572  if (Diags) {
573  if (AdjustPrevDiag)
574  Diags->rbegin()->MatchTy = MatchTy;
575  else
576  Diags->emplace_back(SM, CheckTy, Loc, MatchTy, Range);
577  }
578  return Range;
579 }
580 
582  const SourceMgr &SM, StringRef Buffer,
583  std::vector<FileCheckDiag> *Diags) const {
584  // Attempt to find the closest/best fuzzy match. Usually an error happens
585  // because some string in the output didn't exactly match. In these cases, we
586  // would like to show the user a best guess at what "should have" matched, to
587  // save them having to actually check the input manually.
588  size_t NumLinesForward = 0;
589  size_t Best = StringRef::npos;
590  double BestQuality = 0;
591 
592  // Use an arbitrary 4k limit on how far we will search.
593  for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
594  if (Buffer[i] == '\n')
595  ++NumLinesForward;
596 
597  // Patterns have leading whitespace stripped, so skip whitespace when
598  // looking for something which looks like a pattern.
599  if (Buffer[i] == ' ' || Buffer[i] == '\t')
600  continue;
601 
602  // Compute the "quality" of this match as an arbitrary combination of the
603  // match distance and the number of lines skipped to get to this match.
604  unsigned Distance = computeMatchDistance(Buffer.substr(i));
605  double Quality = Distance + (NumLinesForward / 100.);
606 
607  if (Quality < BestQuality || Best == StringRef::npos) {
608  Best = i;
609  BestQuality = Quality;
610  }
611  }
612 
613  // Print the "possible intended match here" line if we found something
614  // reasonable and not equal to what we showed in the "scanning from here"
615  // line.
616  if (Best && Best != StringRef::npos && BestQuality < 50) {
617  SMRange MatchRange =
619  getCheckTy(), Buffer, Best, 0, Diags);
620  SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note,
621  "possible intended match here");
622 
623  // FIXME: If we wanted to be really friendly we would show why the match
624  // failed, as it can be hard to spot simple one character differences.
625  }
626 }
627 
630  auto VarIter = GlobalVariableTable.find(VarName);
631  if (VarIter == GlobalVariableTable.end())
632  return None;
633 
634  return VarIter->second;
635 }
636 
638 FileCheckPatternContext::makeNumExpr(binop_eval_t EvalBinop,
639  FileCheckNumericVariable *OperandLeft,
640  uint64_t OperandRight) {
641  NumExprs.push_back(llvm::make_unique<FileCheckNumExpr>(EvalBinop, OperandLeft,
642  OperandRight));
643  return NumExprs.back().get();
644 }
645 
647 FileCheckPatternContext::makeNumericVariable(StringRef Name, uint64_t Value) {
648  NumericVariables.push_back(
649  llvm::make_unique<FileCheckNumericVariable>(Name, Value));
650  return NumericVariables.back().get();
651 }
652 
654 FileCheckPatternContext::makeStringSubstitution(StringRef VarName,
655  size_t InsertIdx) {
656  Substitutions.push_back(
657  llvm::make_unique<FileCheckStringSubstitution>(this, VarName, InsertIdx));
658  return Substitutions.back().get();
659 }
660 
661 FileCheckSubstitution *FileCheckPatternContext::makeNumericSubstitution(
662  StringRef Expr, FileCheckNumExpr *NumExpr, size_t InsertIdx) {
663  Substitutions.push_back(llvm::make_unique<FileCheckNumericSubstitution>(
664  this, Expr, NumExpr, InsertIdx));
665  return Substitutions.back().get();
666 }
667 
668 size_t FileCheckPattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) {
669  // Offset keeps track of the current offset within the input Str
670  size_t Offset = 0;
671  // [...] Nesting depth
672  size_t BracketDepth = 0;
673 
674  while (!Str.empty()) {
675  if (Str.startswith("]]") && BracketDepth == 0)
676  return Offset;
677  if (Str[0] == '\\') {
678  // Backslash escapes the next char within regexes, so skip them both.
679  Str = Str.substr(2);
680  Offset += 2;
681  } else {
682  switch (Str[0]) {
683  default:
684  break;
685  case '[':
686  BracketDepth++;
687  break;
688  case ']':
689  if (BracketDepth == 0) {
692  "missing closing \"]\" for regex variable");
693  exit(1);
694  }
695  BracketDepth--;
696  break;
697  }
698  Str = Str.substr(1);
699  Offset++;
700  }
701  }
702 
703  return StringRef::npos;
704 }
705 
707  SmallVectorImpl<char> &OutputBuffer) {
708  OutputBuffer.reserve(MB.getBufferSize());
709 
710  for (const char *Ptr = MB.getBufferStart(), *End = MB.getBufferEnd();
711  Ptr != End; ++Ptr) {
712  // Eliminate trailing dosish \r.
713  if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
714  continue;
715  }
716 
717  // If current char is not a horizontal whitespace or if horizontal
718  // whitespace canonicalization is disabled, dump it to output as is.
719  if (Req.NoCanonicalizeWhiteSpace || (*Ptr != ' ' && *Ptr != '\t')) {
720  OutputBuffer.push_back(*Ptr);
721  continue;
722  }
723 
724  // Otherwise, add one space and advance over neighboring space.
725  OutputBuffer.push_back(' ');
726  while (Ptr + 1 != End && (Ptr[1] == ' ' || Ptr[1] == '\t'))
727  ++Ptr;
728  }
729 
730  // Add a null byte and then return all but that byte.
731  OutputBuffer.push_back('\0');
732  return StringRef(OutputBuffer.data(), OutputBuffer.size() - 1);
733 }
734 
736  const Check::FileCheckType &CheckTy,
737  SMLoc CheckLoc, MatchType MatchTy,
738  SMRange InputRange)
739  : CheckTy(CheckTy), MatchTy(MatchTy) {
740  auto Start = SM.getLineAndColumn(InputRange.Start);
741  auto End = SM.getLineAndColumn(InputRange.End);
742  InputStartLine = Start.first;
743  InputStartCol = Start.second;
744  InputEndLine = End.first;
745  InputEndCol = End.second;
746  Start = SM.getLineAndColumn(CheckLoc);
747  CheckLine = Start.first;
748  CheckCol = Start.second;
749 }
750 
751 static bool IsPartOfWord(char c) {
752  return (isalnum(c) || c == '-' || c == '_');
753 }
754 
756  assert(Count > 0 && "zero and negative counts are not supported");
757  assert((C == 1 || Kind == CheckPlain) &&
758  "count supported only for plain CHECK directives");
759  Count = C;
760  return *this;
761 }
762 
764  switch (Kind) {
765  case Check::CheckNone:
766  return "invalid";
767  case Check::CheckPlain:
768  if (Count > 1)
769  return Prefix.str() + "-COUNT";
770  return Prefix;
771  case Check::CheckNext:
772  return Prefix.str() + "-NEXT";
773  case Check::CheckSame:
774  return Prefix.str() + "-SAME";
775  case Check::CheckNot:
776  return Prefix.str() + "-NOT";
777  case Check::CheckDAG:
778  return Prefix.str() + "-DAG";
779  case Check::CheckLabel:
780  return Prefix.str() + "-LABEL";
781  case Check::CheckEmpty:
782  return Prefix.str() + "-EMPTY";
783  case Check::CheckEOF:
784  return "implicit EOF";
785  case Check::CheckBadNot:
786  return "bad NOT";
788  return "bad COUNT";
789  }
790  llvm_unreachable("unknown FileCheckType");
791 }
792 
793 static std::pair<Check::FileCheckType, StringRef>
795  if (Buffer.size() <= Prefix.size())
796  return {Check::CheckNone, StringRef()};
797 
798  char NextChar = Buffer[Prefix.size()];
799 
800  StringRef Rest = Buffer.drop_front(Prefix.size() + 1);
801  // Verify that the : is present after the prefix.
802  if (NextChar == ':')
803  return {Check::CheckPlain, Rest};
804 
805  if (NextChar != '-')
806  return {Check::CheckNone, StringRef()};
807 
808  if (Rest.consume_front("COUNT-")) {
809  int64_t Count;
810  if (Rest.consumeInteger(10, Count))
811  // Error happened in parsing integer.
812  return {Check::CheckBadCount, Rest};
813  if (Count <= 0 || Count > INT32_MAX)
814  return {Check::CheckBadCount, Rest};
815  if (!Rest.consume_front(":"))
816  return {Check::CheckBadCount, Rest};
817  return {Check::FileCheckType(Check::CheckPlain).setCount(Count), Rest};
818  }
819 
820  if (Rest.consume_front("NEXT:"))
821  return {Check::CheckNext, Rest};
822 
823  if (Rest.consume_front("SAME:"))
824  return {Check::CheckSame, Rest};
825 
826  if (Rest.consume_front("NOT:"))
827  return {Check::CheckNot, Rest};
828 
829  if (Rest.consume_front("DAG:"))
830  return {Check::CheckDAG, Rest};
831 
832  if (Rest.consume_front("LABEL:"))
833  return {Check::CheckLabel, Rest};
834 
835  if (Rest.consume_front("EMPTY:"))
836  return {Check::CheckEmpty, Rest};
837 
838  // You can't combine -NOT with another suffix.
839  if (Rest.startswith("DAG-NOT:") || Rest.startswith("NOT-DAG:") ||
840  Rest.startswith("NEXT-NOT:") || Rest.startswith("NOT-NEXT:") ||
841  Rest.startswith("SAME-NOT:") || Rest.startswith("NOT-SAME:") ||
842  Rest.startswith("EMPTY-NOT:") || Rest.startswith("NOT-EMPTY:"))
843  return {Check::CheckBadNot, Rest};
844 
845  return {Check::CheckNone, Rest};
846 }
847 
848 // From the given position, find the next character after the word.
849 static size_t SkipWord(StringRef Str, size_t Loc) {
850  while (Loc < Str.size() && IsPartOfWord(Str[Loc]))
851  ++Loc;
852  return Loc;
853 }
854 
855 /// Searches the buffer for the first prefix in the prefix regular expression.
856 ///
857 /// This searches the buffer using the provided regular expression, however it
858 /// enforces constraints beyond that:
859 /// 1) The found prefix must not be a suffix of something that looks like
860 /// a valid prefix.
861 /// 2) The found prefix must be followed by a valid check type suffix using \c
862 /// FindCheckType above.
863 ///
864 /// \returns a pair of StringRefs into the Buffer, which combines:
865 /// - the first match of the regular expression to satisfy these two is
866 /// returned,
867 /// otherwise an empty StringRef is returned to indicate failure.
868 /// - buffer rewound to the location right after parsed suffix, for parsing
869 /// to continue from
870 ///
871 /// If this routine returns a valid prefix, it will also shrink \p Buffer to
872 /// start at the beginning of the returned prefix, increment \p LineNumber for
873 /// each new line consumed from \p Buffer, and set \p CheckTy to the type of
874 /// check found by examining the suffix.
875 ///
876 /// If no valid prefix is found, the state of Buffer, LineNumber, and CheckTy
877 /// is unspecified.
878 static std::pair<StringRef, StringRef>
880  unsigned &LineNumber, Check::FileCheckType &CheckTy) {
882 
883  while (!Buffer.empty()) {
884  // Find the first (longest) match using the RE.
885  if (!PrefixRE.match(Buffer, &Matches))
886  // No match at all, bail.
887  return {StringRef(), StringRef()};
888 
889  StringRef Prefix = Matches[0];
890  Matches.clear();
891 
892  assert(Prefix.data() >= Buffer.data() &&
893  Prefix.data() < Buffer.data() + Buffer.size() &&
894  "Prefix doesn't start inside of buffer!");
895  size_t Loc = Prefix.data() - Buffer.data();
896  StringRef Skipped = Buffer.substr(0, Loc);
897  Buffer = Buffer.drop_front(Loc);
898  LineNumber += Skipped.count('\n');
899 
900  // Check that the matched prefix isn't a suffix of some other check-like
901  // word.
902  // FIXME: This is a very ad-hoc check. it would be better handled in some
903  // other way. Among other things it seems hard to distinguish between
904  // intentional and unintentional uses of this feature.
905  if (Skipped.empty() || !IsPartOfWord(Skipped.back())) {
906  // Now extract the type.
907  StringRef AfterSuffix;
908  std::tie(CheckTy, AfterSuffix) = FindCheckType(Buffer, Prefix);
909 
910  // If we've found a valid check type for this prefix, we're done.
911  if (CheckTy != Check::CheckNone)
912  return {Prefix, AfterSuffix};
913  }
914 
915  // If we didn't successfully find a prefix, we need to skip this invalid
916  // prefix and continue scanning. We directly skip the prefix that was
917  // matched and any additional parts of that check-like word.
918  Buffer = Buffer.drop_front(SkipWord(Buffer, Prefix.size()));
919  }
920 
921  // We ran out of buffer while skipping partial matches so give up.
922  return {StringRef(), StringRef()};
923 }
924 
925 bool FileCheck::ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
926  std::vector<FileCheckString> &CheckStrings) {
927  if (PatternContext.defineCmdlineVariables(Req.GlobalDefines, SM))
928  return true;
929 
930  std::vector<FileCheckPattern> ImplicitNegativeChecks;
931  for (const auto &PatternString : Req.ImplicitCheckNot) {
932  // Create a buffer with fake command line content in order to display the
933  // command line option responsible for the specific implicit CHECK-NOT.
934  std::string Prefix = "-implicit-check-not='";
935  std::string Suffix = "'";
936  std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
937  Prefix + PatternString + Suffix, "command line");
938 
939  StringRef PatternInBuffer =
940  CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
941  SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
942 
943  ImplicitNegativeChecks.push_back(
944  FileCheckPattern(Check::CheckNot, &PatternContext));
945  ImplicitNegativeChecks.back().ParsePattern(PatternInBuffer,
946  "IMPLICIT-CHECK", SM, 0, Req);
947  }
948 
949  std::vector<FileCheckPattern> DagNotMatches = ImplicitNegativeChecks;
950 
951  // LineNumber keeps track of the line on which CheckPrefix instances are
952  // found.
953  unsigned LineNumber = 1;
954 
955  while (1) {
957 
958  // See if a prefix occurs in the memory buffer.
959  StringRef UsedPrefix;
960  StringRef AfterSuffix;
961  std::tie(UsedPrefix, AfterSuffix) =
962  FindFirstMatchingPrefix(PrefixRE, Buffer, LineNumber, CheckTy);
963  if (UsedPrefix.empty())
964  break;
965  assert(UsedPrefix.data() == Buffer.data() &&
966  "Failed to move Buffer's start forward, or pointed prefix outside "
967  "of the buffer!");
968  assert(AfterSuffix.data() >= Buffer.data() &&
969  AfterSuffix.data() < Buffer.data() + Buffer.size() &&
970  "Parsing after suffix doesn't start inside of buffer!");
971 
972  // Location to use for error messages.
973  const char *UsedPrefixStart = UsedPrefix.data();
974 
975  // Skip the buffer to the end of parsed suffix (or just prefix, if no good
976  // suffix was processed).
977  Buffer = AfterSuffix.empty() ? Buffer.drop_front(UsedPrefix.size())
978  : AfterSuffix;
979 
980  // Complain about useful-looking but unsupported suffixes.
981  if (CheckTy == Check::CheckBadNot) {
983  "unsupported -NOT combo on prefix '" + UsedPrefix + "'");
984  return true;
985  }
986 
987  // Complain about invalid count specification.
988  if (CheckTy == Check::CheckBadCount) {
990  "invalid count in -COUNT specification on prefix '" +
991  UsedPrefix + "'");
992  return true;
993  }
994 
995  // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
996  // leading whitespace.
997  if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
998  Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
999 
1000  // Scan ahead to the end of line.
1001  size_t EOL = Buffer.find_first_of("\n\r");
1002 
1003  // Remember the location of the start of the pattern, for diagnostics.
1004  SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
1005 
1006  // Parse the pattern.
1007  FileCheckPattern P(CheckTy, &PatternContext);
1008  if (P.ParsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, LineNumber, Req))
1009  return true;
1010 
1011  // Verify that CHECK-LABEL lines do not define or use variables
1012  if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
1013  SM.PrintMessage(
1014  SMLoc::getFromPointer(UsedPrefixStart), SourceMgr::DK_Error,
1015  "found '" + UsedPrefix + "-LABEL:'"
1016  " with variable definition or use");
1017  return true;
1018  }
1019 
1020  Buffer = Buffer.substr(EOL);
1021 
1022  // Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them.
1023  if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame ||
1024  CheckTy == Check::CheckEmpty) &&
1025  CheckStrings.empty()) {
1026  StringRef Type = CheckTy == Check::CheckNext
1027  ? "NEXT"
1028  : CheckTy == Check::CheckEmpty ? "EMPTY" : "SAME";
1029  SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
1031  "found '" + UsedPrefix + "-" + Type +
1032  "' without previous '" + UsedPrefix + ": line");
1033  return true;
1034  }
1035 
1036  // Handle CHECK-DAG/-NOT.
1037  if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
1038  DagNotMatches.push_back(P);
1039  continue;
1040  }
1041 
1042  // Okay, add the string we captured to the output vector and move on.
1043  CheckStrings.emplace_back(P, UsedPrefix, PatternLoc);
1044  std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
1045  DagNotMatches = ImplicitNegativeChecks;
1046  }
1047 
1048  // Add an EOF pattern for any trailing CHECK-DAG/-NOTs, and use the first
1049  // prefix as a filler for the error message.
1050  if (!DagNotMatches.empty()) {
1051  CheckStrings.emplace_back(
1052  FileCheckPattern(Check::CheckEOF, &PatternContext),
1053  *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data()));
1054  std::swap(DagNotMatches, CheckStrings.back().DagNotStrings);
1055  }
1056 
1057  if (CheckStrings.empty()) {
1058  errs() << "error: no check strings found with prefix"
1059  << (Req.CheckPrefixes.size() > 1 ? "es " : " ");
1060  auto I = Req.CheckPrefixes.begin();
1061  auto E = Req.CheckPrefixes.end();
1062  if (I != E) {
1063  errs() << "\'" << *I << ":'";
1064  ++I;
1065  }
1066  for (; I != E; ++I)
1067  errs() << ", \'" << *I << ":'";
1068 
1069  errs() << '\n';
1070  return true;
1071  }
1072 
1073  return false;
1074 }
1075 
1076 static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM,
1077  StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat,
1078  int MatchedCount, StringRef Buffer, size_t MatchPos,
1079  size_t MatchLen, const FileCheckRequest &Req,
1080  std::vector<FileCheckDiag> *Diags) {
1081  bool PrintDiag = true;
1082  if (ExpectedMatch) {
1083  if (!Req.Verbose)
1084  return;
1085  if (!Req.VerboseVerbose && Pat.getCheckTy() == Check::CheckEOF)
1086  return;
1087  // Due to their verbosity, we don't print verbose diagnostics here if we're
1088  // gathering them for a different rendering, but we always print other
1089  // diagnostics.
1090  PrintDiag = !Diags;
1091  }
1092  SMRange MatchRange = ProcessMatchResult(
1093  ExpectedMatch ? FileCheckDiag::MatchFoundAndExpected
1095  SM, Loc, Pat.getCheckTy(), Buffer, MatchPos, MatchLen, Diags);
1096  if (!PrintDiag)
1097  return;
1098 
1099  std::string Message = formatv("{0}: {1} string found in input",
1100  Pat.getCheckTy().getDescription(Prefix),
1101  (ExpectedMatch ? "expected" : "excluded"))
1102  .str();
1103  if (Pat.getCount() > 1)
1104  Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
1105 
1106  SM.PrintMessage(
1107  Loc, ExpectedMatch ? SourceMgr::DK_Remark : SourceMgr::DK_Error, Message);
1108  SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, "found here",
1109  {MatchRange});
1110  Pat.printSubstitutions(SM, Buffer, MatchRange);
1111 }
1112 
1113 static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM,
1114  const FileCheckString &CheckStr, int MatchedCount,
1115  StringRef Buffer, size_t MatchPos, size_t MatchLen,
1116  FileCheckRequest &Req,
1117  std::vector<FileCheckDiag> *Diags) {
1118  PrintMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat,
1119  MatchedCount, Buffer, MatchPos, MatchLen, Req, Diags);
1120 }
1121 
1122 static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM,
1123  StringRef Prefix, SMLoc Loc,
1124  const FileCheckPattern &Pat, int MatchedCount,
1125  StringRef Buffer, bool VerboseVerbose,
1126  std::vector<FileCheckDiag> *Diags) {
1127  bool PrintDiag = true;
1128  if (!ExpectedMatch) {
1129  if (!VerboseVerbose)
1130  return;
1131  // Due to their verbosity, we don't print verbose diagnostics here if we're
1132  // gathering them for a different rendering, but we always print other
1133  // diagnostics.
1134  PrintDiag = !Diags;
1135  }
1136 
1137  // If the current position is at the end of a line, advance to the start of
1138  // the next line.
1139  Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r"));
1140  SMRange SearchRange = ProcessMatchResult(
1141  ExpectedMatch ? FileCheckDiag::MatchNoneButExpected
1143  SM, Loc, Pat.getCheckTy(), Buffer, 0, Buffer.size(), Diags);
1144  if (!PrintDiag)
1145  return;
1146 
1147  // Print "not found" diagnostic.
1148  std::string Message = formatv("{0}: {1} string not found in input",
1149  Pat.getCheckTy().getDescription(Prefix),
1150  (ExpectedMatch ? "expected" : "excluded"))
1151  .str();
1152  if (Pat.getCount() > 1)
1153  Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
1154  SM.PrintMessage(
1155  Loc, ExpectedMatch ? SourceMgr::DK_Error : SourceMgr::DK_Remark, Message);
1156 
1157  // Print the "scanning from here" line.
1158  SM.PrintMessage(SearchRange.Start, SourceMgr::DK_Note, "scanning from here");
1159 
1160  // Allow the pattern to print additional information if desired.
1161  Pat.printSubstitutions(SM, Buffer);
1162 
1163  if (ExpectedMatch)
1164  Pat.printFuzzyMatch(SM, Buffer, Diags);
1165 }
1166 
1167 static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM,
1168  const FileCheckString &CheckStr, int MatchedCount,
1169  StringRef Buffer, bool VerboseVerbose,
1170  std::vector<FileCheckDiag> *Diags) {
1171  PrintNoMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat,
1172  MatchedCount, Buffer, VerboseVerbose, Diags);
1173 }
1174 
1175 /// Counts the number of newlines in the specified range.
1176 static unsigned CountNumNewlinesBetween(StringRef Range,
1177  const char *&FirstNewLine) {
1178  unsigned NumNewLines = 0;
1179  while (1) {
1180  // Scan for newline.
1181  Range = Range.substr(Range.find_first_of("\n\r"));
1182  if (Range.empty())
1183  return NumNewLines;
1184 
1185  ++NumNewLines;
1186 
1187  // Handle \n\r and \r\n as a single newline.
1188  if (Range.size() > 1 && (Range[1] == '\n' || Range[1] == '\r') &&
1189  (Range[0] != Range[1]))
1190  Range = Range.substr(1);
1191  Range = Range.substr(1);
1192 
1193  if (NumNewLines == 1)
1194  FirstNewLine = Range.begin();
1195  }
1196 }
1197 
1198 size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
1199  bool IsLabelScanMode, size_t &MatchLen,
1200  FileCheckRequest &Req,
1201  std::vector<FileCheckDiag> *Diags) const {
1202  size_t LastPos = 0;
1203  std::vector<const FileCheckPattern *> NotStrings;
1204 
1205  // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
1206  // bounds; we have not processed variable definitions within the bounded block
1207  // yet so cannot handle any final CHECK-DAG yet; this is handled when going
1208  // over the block again (including the last CHECK-LABEL) in normal mode.
1209  if (!IsLabelScanMode) {
1210  // Match "dag strings" (with mixed "not strings" if any).
1211  LastPos = CheckDag(SM, Buffer, NotStrings, Req, Diags);
1212  if (LastPos == StringRef::npos)
1213  return StringRef::npos;
1214  }
1215 
1216  // Match itself from the last position after matching CHECK-DAG.
1217  size_t LastMatchEnd = LastPos;
1218  size_t FirstMatchPos = 0;
1219  // Go match the pattern Count times. Majority of patterns only match with
1220  // count 1 though.
1221  assert(Pat.getCount() != 0 && "pattern count can not be zero");
1222  for (int i = 1; i <= Pat.getCount(); i++) {
1223  StringRef MatchBuffer = Buffer.substr(LastMatchEnd);
1224  size_t CurrentMatchLen;
1225  // get a match at current start point
1226  size_t MatchPos = Pat.match(MatchBuffer, CurrentMatchLen);
1227  if (i == 1)
1228  FirstMatchPos = LastPos + MatchPos;
1229 
1230  // report
1231  if (MatchPos == StringRef::npos) {
1232  PrintNoMatch(true, SM, *this, i, MatchBuffer, Req.VerboseVerbose, Diags);
1233  return StringRef::npos;
1234  }
1235  PrintMatch(true, SM, *this, i, MatchBuffer, MatchPos, CurrentMatchLen, Req,
1236  Diags);
1237 
1238  // move start point after the match
1239  LastMatchEnd += MatchPos + CurrentMatchLen;
1240  }
1241  // Full match len counts from first match pos.
1242  MatchLen = LastMatchEnd - FirstMatchPos;
1243 
1244  // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
1245  // or CHECK-NOT
1246  if (!IsLabelScanMode) {
1247  size_t MatchPos = FirstMatchPos - LastPos;
1248  StringRef MatchBuffer = Buffer.substr(LastPos);
1249  StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
1250 
1251  // If this check is a "CHECK-NEXT", verify that the previous match was on
1252  // the previous line (i.e. that there is one newline between them).
1253  if (CheckNext(SM, SkippedRegion)) {
1255  Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
1256  Diags, Req.Verbose);
1257  return StringRef::npos;
1258  }
1259 
1260  // If this check is a "CHECK-SAME", verify that the previous match was on
1261  // the same line (i.e. that there is no newline between them).
1262  if (CheckSame(SM, SkippedRegion)) {
1264  Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
1265  Diags, Req.Verbose);
1266  return StringRef::npos;
1267  }
1268 
1269  // If this match had "not strings", verify that they don't exist in the
1270  // skipped region.
1271  if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
1272  return StringRef::npos;
1273  }
1274 
1275  return FirstMatchPos;
1276 }
1277 
1278 bool FileCheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const {
1279  if (Pat.getCheckTy() != Check::CheckNext &&
1280  Pat.getCheckTy() != Check::CheckEmpty)
1281  return false;
1282 
1283  Twine CheckName =
1284  Prefix +
1285  Twine(Pat.getCheckTy() == Check::CheckEmpty ? "-EMPTY" : "-NEXT");
1286 
1287  // Count the number of newlines between the previous match and this one.
1288  const char *FirstNewLine = nullptr;
1289  unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
1290 
1291  if (NumNewLines == 0) {
1293  CheckName + ": is on the same line as previous match");
1295  "'next' match was here");
1297  "previous match ended here");
1298  return true;
1299  }
1300 
1301  if (NumNewLines != 1) {
1303  CheckName +
1304  ": is not on the line after the previous match");
1306  "'next' match was here");
1308  "previous match ended here");
1310  "non-matching line after previous match is here");
1311  return true;
1312  }
1313 
1314  return false;
1315 }
1316 
1317 bool FileCheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const {
1318  if (Pat.getCheckTy() != Check::CheckSame)
1319  return false;
1320 
1321  // Count the number of newlines between the previous match and this one.
1322  const char *FirstNewLine = nullptr;
1323  unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
1324 
1325  if (NumNewLines != 0) {
1327  Prefix +
1328  "-SAME: is not on the same line as the previous match");
1330  "'next' match was here");
1332  "previous match ended here");
1333  return true;
1334  }
1335 
1336  return false;
1337 }
1338 
1340  const SourceMgr &SM, StringRef Buffer,
1341  const std::vector<const FileCheckPattern *> &NotStrings,
1342  const FileCheckRequest &Req, std::vector<FileCheckDiag> *Diags) const {
1343  for (const FileCheckPattern *Pat : NotStrings) {
1344  assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!");
1345 
1346  size_t MatchLen = 0;
1347  size_t Pos = Pat->match(Buffer, MatchLen);
1348 
1349  if (Pos == StringRef::npos) {
1350  PrintNoMatch(false, SM, Prefix, Pat->getLoc(), *Pat, 1, Buffer,
1351  Req.VerboseVerbose, Diags);
1352  continue;
1353  }
1354 
1355  PrintMatch(false, SM, Prefix, Pat->getLoc(), *Pat, 1, Buffer, Pos, MatchLen,
1356  Req, Diags);
1357 
1358  return true;
1359  }
1360 
1361  return false;
1362 }
1363 
1364 size_t
1366  std::vector<const FileCheckPattern *> &NotStrings,
1367  const FileCheckRequest &Req,
1368  std::vector<FileCheckDiag> *Diags) const {
1369  if (DagNotStrings.empty())
1370  return 0;
1371 
1372  // The start of the search range.
1373  size_t StartPos = 0;
1374 
1375  struct MatchRange {
1376  size_t Pos;
1377  size_t End;
1378  };
1379  // A sorted list of ranges for non-overlapping CHECK-DAG matches. Match
1380  // ranges are erased from this list once they are no longer in the search
1381  // range.
1382  std::list<MatchRange> MatchRanges;
1383 
1384  // We need PatItr and PatEnd later for detecting the end of a CHECK-DAG
1385  // group, so we don't use a range-based for loop here.
1386  for (auto PatItr = DagNotStrings.begin(), PatEnd = DagNotStrings.end();
1387  PatItr != PatEnd; ++PatItr) {
1388  const FileCheckPattern &Pat = *PatItr;
1389  assert((Pat.getCheckTy() == Check::CheckDAG ||
1390  Pat.getCheckTy() == Check::CheckNot) &&
1391  "Invalid CHECK-DAG or CHECK-NOT!");
1392 
1393  if (Pat.getCheckTy() == Check::CheckNot) {
1394  NotStrings.push_back(&Pat);
1395  continue;
1396  }
1397 
1398  assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!");
1399 
1400  // CHECK-DAG always matches from the start.
1401  size_t MatchLen = 0, MatchPos = StartPos;
1402 
1403  // Search for a match that doesn't overlap a previous match in this
1404  // CHECK-DAG group.
1405  for (auto MI = MatchRanges.begin(), ME = MatchRanges.end(); true; ++MI) {
1406  StringRef MatchBuffer = Buffer.substr(MatchPos);
1407  size_t MatchPosBuf = Pat.match(MatchBuffer, MatchLen);
1408  // With a group of CHECK-DAGs, a single mismatching means the match on
1409  // that group of CHECK-DAGs fails immediately.
1410  if (MatchPosBuf == StringRef::npos) {
1411  PrintNoMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, MatchBuffer,
1412  Req.VerboseVerbose, Diags);
1413  return StringRef::npos;
1414  }
1415  // Re-calc it as the offset relative to the start of the original string.
1416  MatchPos += MatchPosBuf;
1417  if (Req.VerboseVerbose)
1418  PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer, MatchPos,
1419  MatchLen, Req, Diags);
1420  MatchRange M{MatchPos, MatchPos + MatchLen};
1421  if (Req.AllowDeprecatedDagOverlap) {
1422  // We don't need to track all matches in this mode, so we just maintain
1423  // one match range that encompasses the current CHECK-DAG group's
1424  // matches.
1425  if (MatchRanges.empty())
1426  MatchRanges.insert(MatchRanges.end(), M);
1427  else {
1428  auto Block = MatchRanges.begin();
1429  Block->Pos = std::min(Block->Pos, M.Pos);
1430  Block->End = std::max(Block->End, M.End);
1431  }
1432  break;
1433  }
1434  // Iterate previous matches until overlapping match or insertion point.
1435  bool Overlap = false;
1436  for (; MI != ME; ++MI) {
1437  if (M.Pos < MI->End) {
1438  // !Overlap => New match has no overlap and is before this old match.
1439  // Overlap => New match overlaps this old match.
1440  Overlap = MI->Pos < M.End;
1441  break;
1442  }
1443  }
1444  if (!Overlap) {
1445  // Insert non-overlapping match into list.
1446  MatchRanges.insert(MI, M);
1447  break;
1448  }
1449  if (Req.VerboseVerbose) {
1450  // Due to their verbosity, we don't print verbose diagnostics here if
1451  // we're gathering them for a different rendering, but we always print
1452  // other diagnostics.
1453  if (!Diags) {
1454  SMLoc OldStart = SMLoc::getFromPointer(Buffer.data() + MI->Pos);
1455  SMLoc OldEnd = SMLoc::getFromPointer(Buffer.data() + MI->End);
1456  SMRange OldRange(OldStart, OldEnd);
1457  SM.PrintMessage(OldStart, SourceMgr::DK_Note,
1458  "match discarded, overlaps earlier DAG match here",
1459  {OldRange});
1460  } else
1461  Diags->rbegin()->MatchTy = FileCheckDiag::MatchFoundButDiscarded;
1462  }
1463  MatchPos = MI->End;
1464  }
1465  if (!Req.VerboseVerbose)
1466  PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer, MatchPos,
1467  MatchLen, Req, Diags);
1468 
1469  // Handle the end of a CHECK-DAG group.
1470  if (std::next(PatItr) == PatEnd ||
1471  std::next(PatItr)->getCheckTy() == Check::CheckNot) {
1472  if (!NotStrings.empty()) {
1473  // If there are CHECK-NOTs between two CHECK-DAGs or from CHECK to
1474  // CHECK-DAG, verify that there are no 'not' strings occurred in that
1475  // region.
1476  StringRef SkippedRegion =
1477  Buffer.slice(StartPos, MatchRanges.begin()->Pos);
1478  if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
1479  return StringRef::npos;
1480  // Clear "not strings".
1481  NotStrings.clear();
1482  }
1483  // All subsequent CHECK-DAGs and CHECK-NOTs should be matched from the
1484  // end of this CHECK-DAG group's match range.
1485  StartPos = MatchRanges.rbegin()->End;
1486  // Don't waste time checking for (impossible) overlaps before that.
1487  MatchRanges.clear();
1488  }
1489  }
1490 
1491  return StartPos;
1492 }
1493 
1494 // A check prefix must contain only alphanumeric, hyphens and underscores.
1495 static bool ValidateCheckPrefix(StringRef CheckPrefix) {
1496  Regex Validator("^[a-zA-Z0-9_-]*$");
1497  return Validator.match(CheckPrefix);
1498 }
1499 
1501  StringSet<> PrefixSet;
1502 
1503  for (StringRef Prefix : Req.CheckPrefixes) {
1504  // Reject empty prefixes.
1505  if (Prefix == "")
1506  return false;
1507 
1508  if (!PrefixSet.insert(Prefix).second)
1509  return false;
1510 
1511  if (!ValidateCheckPrefix(Prefix))
1512  return false;
1513  }
1514 
1515  return true;
1516 }
1517 
1519  // I don't think there's a way to specify an initial value for cl::list,
1520  // so if nothing was specified, add the default
1521  if (Req.CheckPrefixes.empty())
1522  Req.CheckPrefixes.push_back("CHECK");
1523 
1524  // We already validated the contents of CheckPrefixes so just concatenate
1525  // them as alternatives.
1526  SmallString<32> PrefixRegexStr;
1527  for (StringRef Prefix : Req.CheckPrefixes) {
1528  if (Prefix != Req.CheckPrefixes.front())
1529  PrefixRegexStr.push_back('|');
1530 
1531  PrefixRegexStr.append(Prefix);
1532  }
1533 
1534  return Regex(PrefixRegexStr);
1535 }
1536 
1538  std::vector<std::string> &CmdlineDefines, SourceMgr &SM) {
1539  assert(GlobalVariableTable.empty() && GlobalNumericVariableTable.empty() &&
1540  "Overriding defined variable with command-line variable definitions");
1541 
1542  if (CmdlineDefines.empty())
1543  return false;
1544 
1545  // Create a string representing the vector of command-line definitions. Each
1546  // definition is on its own line and prefixed with a definition number to
1547  // clarify which definition a given diagnostic corresponds to.
1548  unsigned I = 0;
1549  bool ErrorFound = false;
1550  std::string CmdlineDefsDiag;
1551  StringRef Prefix1 = "Global define #";
1552  StringRef Prefix2 = ": ";
1553  for (StringRef CmdlineDef : CmdlineDefines)
1554  CmdlineDefsDiag +=
1555  (Prefix1 + Twine(++I) + Prefix2 + CmdlineDef + "\n").str();
1556 
1557  // Create a buffer with fake command line content in order to display
1558  // parsing diagnostic with location information and point to the
1559  // global definition with invalid syntax.
1560  std::unique_ptr<MemoryBuffer> CmdLineDefsDiagBuffer =
1561  MemoryBuffer::getMemBufferCopy(CmdlineDefsDiag, "Global defines");
1562  StringRef CmdlineDefsDiagRef = CmdLineDefsDiagBuffer->getBuffer();
1563  SM.AddNewSourceBuffer(std::move(CmdLineDefsDiagBuffer), SMLoc());
1564 
1565  SmallVector<StringRef, 4> CmdlineDefsDiagVec;
1566  CmdlineDefsDiagRef.split(CmdlineDefsDiagVec, '\n', -1 /*MaxSplit*/,
1567  false /*KeepEmpty*/);
1568  for (StringRef CmdlineDefDiag : CmdlineDefsDiagVec) {
1569  unsigned DefStart = CmdlineDefDiag.find(Prefix2) + Prefix2.size();
1570  StringRef CmdlineDef = CmdlineDefDiag.substr(DefStart);
1571  if (CmdlineDef.find('=') == StringRef::npos) {
1572  SM.PrintMessage(SMLoc::getFromPointer(CmdlineDef.data()),
1574  "Missing equal sign in global definition");
1575  ErrorFound = true;
1576  continue;
1577  }
1578 
1579  // Numeric variable definition.
1580  if (CmdlineDef[0] == '#') {
1581  bool IsPseudo;
1582  unsigned TrailIdx;
1583  size_t EqIdx = CmdlineDef.find('=');
1584  StringRef CmdlineName = CmdlineDef.substr(1, EqIdx - 1);
1585  if (FileCheckPattern::parseVariable(CmdlineName, IsPseudo, TrailIdx) ||
1586  IsPseudo || TrailIdx != CmdlineName.size() || CmdlineName.empty()) {
1587  SM.PrintMessage(SMLoc::getFromPointer(CmdlineName.data()),
1589  "invalid name in numeric variable definition '" +
1590  CmdlineName + "'");
1591  ErrorFound = true;
1592  continue;
1593  }
1594 
1595  // Detect collisions between string and numeric variables when the latter
1596  // is created later than the former.
1597  if (DefinedVariableTable.find(CmdlineName) !=
1598  DefinedVariableTable.end()) {
1599  SM.PrintMessage(
1601  "string variable with name '" + CmdlineName + "' already exists");
1602  ErrorFound = true;
1603  continue;
1604  }
1605 
1606  StringRef CmdlineVal = CmdlineDef.substr(EqIdx + 1);
1607  uint64_t Val;
1608  if (CmdlineVal.getAsInteger(10, Val)) {
1609  SM.PrintMessage(SMLoc::getFromPointer(CmdlineVal.data()),
1611  "invalid value in numeric variable definition '" +
1612  CmdlineVal + "'");
1613  ErrorFound = true;
1614  continue;
1615  }
1616  auto DefinedNumericVariable = makeNumericVariable(CmdlineName, Val);
1617 
1618  // Record this variable definition.
1619  GlobalNumericVariableTable[CmdlineName] = DefinedNumericVariable;
1620  } else {
1621  // String variable definition.
1622  std::pair<StringRef, StringRef> CmdlineNameVal = CmdlineDef.split('=');
1623  StringRef Name = CmdlineNameVal.first;
1624  bool IsPseudo;
1625  unsigned TrailIdx;
1626  if (FileCheckPattern::parseVariable(Name, IsPseudo, TrailIdx) ||
1627  IsPseudo || TrailIdx != Name.size() || Name.empty()) {
1629  "invalid name in string variable definition '" + Name +
1630  "'");
1631  ErrorFound = true;
1632  continue;
1633  }
1634 
1635  // Detect collisions between string and numeric variables when the former
1636  // is created later than the latter.
1637  if (GlobalNumericVariableTable.find(Name) !=
1638  GlobalNumericVariableTable.end()) {
1640  "numeric variable with name '" + Name +
1641  "' already exists");
1642  ErrorFound = true;
1643  continue;
1644  }
1645  GlobalVariableTable.insert(CmdlineNameVal);
1646  // Mark the string variable as defined to detect collisions between
1647  // string and numeric variables in DefineCmdlineVariables when the latter
1648  // is created later than the former. We cannot reuse GlobalVariableTable
1649  // for that by populating it with an empty string since we would then
1650  // lose the ability to detect the use of an undefined variable in
1651  // match().
1652  DefinedVariableTable[Name] = true;
1653  }
1654  }
1655 
1656  return ErrorFound;
1657 }
1658 
1660  SmallVector<StringRef, 16> LocalPatternVars, LocalNumericVars;
1661  for (const StringMapEntry<StringRef> &Var : GlobalVariableTable)
1662  if (Var.first()[0] != '$')
1663  LocalPatternVars.push_back(Var.first());
1664 
1665  // Numeric substitution reads the value of a variable directly, not via
1666  // GlobalNumericVariableTable. Therefore, we clear local variables by
1667  // clearing their value which will lead to a numeric substitution failure. We
1668  // also mark the variable for removal from GlobalNumericVariableTable since
1669  // this is what defineCmdlineVariables checks to decide that no global
1670  // variable has been defined.
1671  for (const auto &Var : GlobalNumericVariableTable)
1672  if (Var.first()[0] != '$') {
1673  Var.getValue()->clearValue();
1674  LocalNumericVars.push_back(Var.first());
1675  }
1676 
1677  for (const auto &Var : LocalPatternVars)
1678  GlobalVariableTable.erase(Var);
1679  for (const auto &Var : LocalNumericVars)
1680  GlobalNumericVariableTable.erase(Var);
1681 }
1682 
1684  ArrayRef<FileCheckString> CheckStrings,
1685  std::vector<FileCheckDiag> *Diags) {
1686  bool ChecksFailed = false;
1687 
1688  unsigned i = 0, j = 0, e = CheckStrings.size();
1689  while (true) {
1690  StringRef CheckRegion;
1691  if (j == e) {
1692  CheckRegion = Buffer;
1693  } else {
1694  const FileCheckString &CheckLabelStr = CheckStrings[j];
1695  if (CheckLabelStr.Pat.getCheckTy() != Check::CheckLabel) {
1696  ++j;
1697  continue;
1698  }
1699 
1700  // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
1701  size_t MatchLabelLen = 0;
1702  size_t MatchLabelPos =
1703  CheckLabelStr.Check(SM, Buffer, true, MatchLabelLen, Req, Diags);
1704  if (MatchLabelPos == StringRef::npos)
1705  // Immediately bail if CHECK-LABEL fails, nothing else we can do.
1706  return false;
1707 
1708  CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen);
1709  Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen);
1710  ++j;
1711  }
1712 
1713  // Do not clear the first region as it's the one before the first
1714  // CHECK-LABEL and it would clear variables defined on the command-line
1715  // before they get used.
1716  if (i != 0 && Req.EnableVarScope)
1717  PatternContext.clearLocalVars();
1718 
1719  for (; i != j; ++i) {
1720  const FileCheckString &CheckStr = CheckStrings[i];
1721 
1722  // Check each string within the scanned region, including a second check
1723  // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
1724  size_t MatchLen = 0;
1725  size_t MatchPos =
1726  CheckStr.Check(SM, CheckRegion, false, MatchLen, Req, Diags);
1727 
1728  if (MatchPos == StringRef::npos) {
1729  ChecksFailed = true;
1730  i = j;
1731  break;
1732  }
1733 
1734  CheckRegion = CheckRegion.substr(MatchPos + MatchLen);
1735  }
1736 
1737  if (j == e)
1738  break;
1739  }
1740 
1741  // Success if no checks failed.
1742  return !ChecksFailed;
1743 }
uint64_t CallInst * C
Represents a range in source code.
Definition: SMLoc.h:48
Indicates a good match for an expected pattern.
Definition: FileCheck.h:457
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Indicates no match for an excluded pattern.
Definition: FileCheck.h:466
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:218
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:125
Indicates a discarded match for an expected pattern.
Definition: FileCheck.h:464
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:256
void push_back(const T &Elt)
Definition: SmallVector.h:211
FileCheckNumExpr * parseNumericSubstitution(StringRef Name, bool IsPseudo, StringRef Trailer, const SourceMgr &SM) const
Parses a numeric substitution involving (pseudo if IsPseudo is true) variable Name with the string co...
Definition: FileCheck.cpp:133
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:649
bool CheckSame(const SourceMgr &SM, StringRef Buffer) const
Verifies that there is no newline in the given Buffer.
Definition: FileCheck.cpp:1317
static std::string escape(StringRef String)
Turn String into a regex by escaping its special characters.
Definition: Regex.cpp:200
StringRef getUndefVarName() const
Definition: FileCheck.cpp:49
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:509
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:763
Marks when parsing found a -NOT check combined with another CHECK suffix.
Definition: FileCheck.h:205
size_t getBufferSize() const
Definition: MemoryBuffer.h:61
Regex buildCheckPrefixRegex()
Definition: FileCheck.cpp:1518
LLVM_NODISCARD StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
Definition: StringRef.h:789
void printSubstitutions(const SourceMgr &SM, StringRef Buffer, SMRange MatchRange=None) const
Prints the value of successful substitutions or the name of the undefined string or numeric variable ...
Definition: FileCheck.cpp:529
void reserve(size_type N)
Definition: SmallVector.h:369
Optional< std::string > getResult() const override
Definition: FileCheck.cpp:62
std::pair< unsigned, unsigned > getLineAndColumn(SMLoc Loc, unsigned BufferID=0) const
Find the line and column number for the specified location in the specified file. ...
Definition: SourceMgr.cpp:130
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:94
size_t CheckDag(const SourceMgr &SM, StringRef Buffer, std::vector< const FileCheckPattern *> &NotStrings, const FileCheckRequest &Req, std::vector< FileCheckDiag > *Diags) const
Matches "dag strings" and their mixed "not strings".
Definition: FileCheck.cpp:1365
bool CheckNext(const SourceMgr &SM, StringRef Buffer) const
Verifies that there is a single line in the given Buffer.
Definition: FileCheck.cpp:1278
SMLoc Start
Definition: SMLoc.h:50
Class representing a numeric variable with a given value in a numeric expression. ...
Definition: FileCheck.h:45
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:495
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:47
static bool parseVariable(StringRef Str, bool &IsPseudo, unsigned &TrailIdx)
Verifies that the string at the start of Str is a well formed variable.
Definition: FileCheck.cpp:87
FileCheckPattern Pat
The pattern to match.
Definition: FileCheck.h:492
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:247
bool ParsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM, unsigned LineNumber, const FileCheckRequest &Req)
Parses the pattern in PatternStr and initializes this FileCheckPattern instance accordingly.
Definition: FileCheck.cpp:204
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
LLVM_NODISCARD 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:620
LLVM_NODISCARD size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:457
SMLoc Loc
The location in the match file that the check string was specified.
Definition: FileCheck.h:498
Check::FileCheckType CheckTy
What is the FileCheck directive for this diagnostic?
Definition: FileCheck.h:438
Indicates a match for an expected pattern, but the match is on the wrong line.
Definition: FileCheck.h:462
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:679
Class representing a substitution to perform in the RegExStr string.
Definition: FileCheck.h:109
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:578
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
Marks when parsing found a -COUNT directive with invalid count value.
Definition: FileCheck.h:208
static std::pair< Check::FileCheckType, StringRef > FindCheckType(StringRef Buffer, StringRef Prefix)
Definition: FileCheck.cpp:794
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:255
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:512
static unsigned CountNumNewlinesBetween(StringRef Range, const char *&FirstNewLine)
Counts the number of newlines in the specified range.
Definition: FileCheck.cpp:1176
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:151
size_t match(StringRef Buffer, size_t &MatchLen) const
Matches the pattern string against the input buffer Buffer.
Definition: FileCheck.cpp:447
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
Contains info about various FileCheck options.
Definition: FileCheck.h:26
static bool ValidateCheckPrefix(StringRef CheckPrefix)
Definition: FileCheck.cpp:1495
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode, size_t &MatchLen, FileCheckRequest &Req, std::vector< FileCheckDiag > *Diags) const
Matches check string and its "not strings" and/or "dag strings".
Definition: FileCheck.cpp:1198
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:74
bool ValidateCheckPrefixes()
Definition: FileCheck.cpp:1500
unsigned InputEndLine
Definition: FileCheck.h:479
StringRef getUndefVarName() const override
Definition: FileCheck.cpp:76
#define P(N)
A check that we found in the input file.
Definition: FileCheck.h:490
void clearLocalVars()
Undefines local variables (variables whose name does not start with a &#39;$&#39; sign), i.e.
Definition: FileCheck.cpp:1659
FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy, SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange)
Definition: FileCheck.cpp:735
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
StringRef CanonicalizeFile(MemoryBuffer &MB, SmallVectorImpl< char > &OutputBuffer)
Canonicalizes whitespaces in the file.
Definition: FileCheck.cpp:706
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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:249
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:285
unsigned CheckLine
Where is the FileCheck directive for this diagnostic?
Definition: FileCheck.h:440
Optional< uint64_t > eval() const
Evaluates the value of this numeric expression, using EvalBinop to perform the binary operation it co...
Definition: FileCheck.cpp:41
SMLoc getLoc() const
Definition: FileCheck.h:358
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:41
Check::FileCheckType getCheckTy() const
Definition: FileCheck.h:412
size_t size() const
Definition: SmallVector.h:52
LLVM_NODISCARD char back() const
back - Get the last character in the string.
Definition: StringRef.h:141
#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
bool ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE, std::vector< FileCheckString > &CheckStrings)
Reads the check file from Buffer and records the expected strings it contains in the CheckStrings vec...
Definition: FileCheck.cpp:925
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:478
Indicates the pattern only matches the end of file.
Definition: FileCheck.h:202
bool isValid() const
Definition: SMLoc.h:59
static size_t SkipWord(StringRef Str, size_t Loc)
Definition: FileCheck.cpp:849
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...
Indicates a fuzzy match that serves as a suggestion for the next intended match for an expected patte...
Definition: FileCheck.h:473
static std::pair< StringRef, StringRef > FindFirstMatchingPrefix(Regex &PrefixRE, StringRef &Buffer, unsigned &LineNumber, Check::FileCheckType &CheckTy)
Searches the buffer for the first prefix in the prefix regular expression.
Definition: FileCheck.cpp:879
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
Definition: FileCheck.cpp:125
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:30
bool setValue(uint64_t Value)
Sets value of this numeric variable if not defined.
Definition: FileCheck.cpp:27
Indicates no match for an expected pattern, but this might follow good matches when multiple matches ...
Definition: FileCheck.h:470
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:696
bool isValid(std::string &Error) const
isValid - returns the error encountered during regex compilation, or matching, if any...
Definition: Regex.cpp:55
bool defineCmdlineVariables(std::vector< std::string > &CmdlineDefines, SourceMgr &SM)
Defines string and numeric variables from definitions given on the command line, passed as a vector o...
Definition: FileCheck.cpp:1537
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:534
unsigned InputEndCol
Definition: FileCheck.h:480
uint64_t(*)(uint64_t, uint64_t) binop_eval_t
Type of functions evaluating a given binary operation.
Definition: FileCheck.h:75
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:223
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM, StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat, int MatchedCount, StringRef Buffer, size_t MatchPos, size_t MatchLen, const FileCheckRequest &Req, std::vector< FileCheckDiag > *Diags)
Definition: FileCheck.cpp:1076
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
Optional< std::string > getResult() const override
Definition: FileCheck.cpp:55
StringRef SpaceChars
Definition: FileCheck.cpp:116
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...
static SMRange ProcessMatchResult(FileCheckDiag::MatchType MatchTy, const SourceMgr &SM, SMLoc Loc, Check::FileCheckType CheckTy, StringRef Buffer, size_t Pos, size_t Len, std::vector< FileCheckDiag > *Diags, bool AdjustPrevDiag=false)
Definition: FileCheck.cpp:563
bool CheckNot(const SourceMgr &SM, StringRef Buffer, const std::vector< const FileCheckPattern *> &NotStrings, const FileCheckRequest &Req, std::vector< FileCheckDiag > *Diags) const
Verifies that none of the strings in NotStrings are found in the given Buffer.
Definition: FileCheck.cpp:1339
SMLoc End
Definition: SMLoc.h:50
bool clearValue()
Clears value of this numeric variable.
Definition: FileCheck.cpp:34
void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer, std::vector< FileCheckDiag > *Diags) const
Definition: FileCheck.cpp:581
unsigned getNumMatches() const
getNumMatches - In a valid regex, return the number of parenthesized matches it contains.
Definition: Regex.cpp:68
iterator begin() const
Definition: StringRef.h:101
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:36
unsigned InputStartLine
The search range if MatchTy is MatchNoneAndExcluded or MatchNoneButExpected, or the match range other...
Definition: FileCheck.h:477
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:160
bool CheckInput(SourceMgr &SM, StringRef Buffer, ArrayRef< FileCheckString > CheckStrings, std::vector< FileCheckDiag > *Diags=nullptr)
Checks the input to FileCheck provided in the Buffer against the CheckStrings read from the check fil...
Definition: FileCheck.cpp:1683
Indicates a match for an excluded pattern.
Definition: FileCheck.h:459
const char * getBufferEnd() const
Definition: MemoryBuffer.h:60
FileCheckType & setCount(int C)
Definition: FileCheck.cpp:755
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:144
static const size_t npos
Definition: StringRef.h:50
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
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:380
#define I(x, y, z)
Definition: MD5.cpp:58
StringRef getUndefVarName() const override
Definition: FileCheck.cpp:70
Optional< StringRef > getPatternVarValue(StringRef VarName)
Definition: FileCheck.cpp:629
const char * getBufferStart() const
Definition: MemoryBuffer.h:59
static bool IsPartOfWord(char c)
Definition: FileCheck.cpp:751
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:122
unsigned InputStartCol
Definition: FileCheck.h:478
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM_NODISCARD char front() const
front - Get the first character in the string.
Definition: StringRef.h:134
Compile for newline-sensitive matching.
Definition: Regex.h:38
LLVM Value Representation.
Definition: Value.h:72
static char popFront(StringRef &S)
Definition: FileCheck.cpp:119
static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM, StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat, int MatchedCount, StringRef Buffer, bool VerboseVerbose, std::vector< FileCheckDiag > *Diags)
Definition: FileCheck.cpp:1122
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:27
static uint64_t sub(uint64_t LeftOp, uint64_t RightOp)
Definition: FileCheck.cpp:128
bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr)
matches - Match the regex against a given String.
Definition: Regex.cpp:72
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
MatchType
What type of match result does this diagnostic describe?
Definition: FileCheck.h:455
int getCount() const
Definition: FileCheck.h:414
Represents a location in source code.
Definition: SMLoc.h:23
iterator end() const
Definition: StringRef.h:103
static bool isValidVarNameStart(char C)
Definition: FileCheck.cpp:83
Class representing a numeric expression consisting of either a single numeric variable or a binary op...
Definition: FileCheck.h:80