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