xref: /freebsd/contrib/llvm-project/llvm/lib/FileCheck/FileCheck.cpp (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
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/FileCheck/FileCheck.h"
17 #include "FileCheckImpl.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringSet.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/CheckedArithmetic.h"
23 #include "llvm/Support/FormatVariadic.h"
24 #include <cstdint>
25 #include <list>
26 #include <set>
27 #include <tuple>
28 #include <utility>
29 
30 using namespace llvm;
31 
32 StringRef ExpressionFormat::toString() const {
33   switch (Value) {
34   case Kind::NoFormat:
35     return StringRef("<none>");
36   case Kind::Unsigned:
37     return StringRef("%u");
38   case Kind::Signed:
39     return StringRef("%d");
40   case Kind::HexUpper:
41     return StringRef("%X");
42   case Kind::HexLower:
43     return StringRef("%x");
44   }
45   llvm_unreachable("unknown expression format");
46 }
47 
48 Expected<std::string> ExpressionFormat::getWildcardRegex() const {
49   StringRef AlternateFormPrefix = AlternateForm ? StringRef("0x") : StringRef();
50 
51   auto CreatePrecisionRegex = [&](StringRef S) {
52     return (Twine(AlternateFormPrefix) + S + Twine('{') + Twine(Precision) +
53             "}")
54         .str();
55   };
56 
57   switch (Value) {
58   case Kind::Unsigned:
59     if (Precision)
60       return CreatePrecisionRegex("([1-9][0-9]*)?[0-9]");
61     return std::string("[0-9]+");
62   case Kind::Signed:
63     if (Precision)
64       return CreatePrecisionRegex("-?([1-9][0-9]*)?[0-9]");
65     return std::string("-?[0-9]+");
66   case Kind::HexUpper:
67     if (Precision)
68       return CreatePrecisionRegex("([1-9A-F][0-9A-F]*)?[0-9A-F]");
69     return (Twine(AlternateFormPrefix) + Twine("[0-9A-F]+")).str();
70   case Kind::HexLower:
71     if (Precision)
72       return CreatePrecisionRegex("([1-9a-f][0-9a-f]*)?[0-9a-f]");
73     return (Twine(AlternateFormPrefix) + Twine("[0-9a-f]+")).str();
74   default:
75     return createStringError(std::errc::invalid_argument,
76                              "trying to match value with invalid format");
77   }
78 }
79 
80 Expected<std::string>
81 ExpressionFormat::getMatchingString(APInt IntValue) const {
82   if (Value != Kind::Signed && IntValue.isNegative())
83     return make_error<OverflowError>();
84 
85   unsigned Radix;
86   bool UpperCase = false;
87   SmallString<8> AbsoluteValueStr;
88   StringRef SignPrefix = IntValue.isNegative() ? "-" : "";
89   switch (Value) {
90   case Kind::Unsigned:
91   case Kind::Signed:
92     Radix = 10;
93     break;
94   case Kind::HexUpper:
95     UpperCase = true;
96     Radix = 16;
97     break;
98   case Kind::HexLower:
99     Radix = 16;
100     UpperCase = false;
101     break;
102   default:
103     return createStringError(std::errc::invalid_argument,
104                              "trying to match value with invalid format");
105   }
106   IntValue.abs().toString(AbsoluteValueStr, Radix, /*Signed=*/false,
107                           /*formatAsCLiteral=*/false,
108                           /*UpperCase=*/UpperCase);
109 
110   StringRef AlternateFormPrefix = AlternateForm ? StringRef("0x") : StringRef();
111 
112   if (Precision > AbsoluteValueStr.size()) {
113     unsigned LeadingZeros = Precision - AbsoluteValueStr.size();
114     return (Twine(SignPrefix) + Twine(AlternateFormPrefix) +
115             std::string(LeadingZeros, '0') + AbsoluteValueStr)
116         .str();
117   }
118 
119   return (Twine(SignPrefix) + Twine(AlternateFormPrefix) + AbsoluteValueStr)
120       .str();
121 }
122 
123 static unsigned nextAPIntBitWidth(unsigned BitWidth) {
124   return (BitWidth < APInt::APINT_BITS_PER_WORD) ? APInt::APINT_BITS_PER_WORD
125                                                  : BitWidth * 2;
126 }
127 
128 static APInt toSigned(APInt AbsVal, bool Negative) {
129   if (AbsVal.isSignBitSet())
130     AbsVal = AbsVal.zext(nextAPIntBitWidth(AbsVal.getBitWidth()));
131   APInt Result = AbsVal;
132   if (Negative)
133     Result.negate();
134   return Result;
135 }
136 
137 APInt ExpressionFormat::valueFromStringRepr(StringRef StrVal,
138                                             const SourceMgr &SM) const {
139   bool ValueIsSigned = Value == Kind::Signed;
140   bool Negative = StrVal.consume_front("-");
141   bool Hex = Value == Kind::HexUpper || Value == Kind::HexLower;
142   bool MissingFormPrefix =
143       !ValueIsSigned && AlternateForm && !StrVal.consume_front("0x");
144   (void)MissingFormPrefix;
145   assert(!MissingFormPrefix && "missing alternate form prefix");
146   APInt ResultValue;
147   [[maybe_unused]] bool ParseFailure =
148       StrVal.getAsInteger(Hex ? 16 : 10, ResultValue);
149   // Both the FileCheck utility and library only call this method with a valid
150   // value in StrVal. This is guaranteed by the regex returned by
151   // getWildcardRegex() above.
152   assert(!ParseFailure && "unable to represent numeric value");
153   return toSigned(ResultValue, Negative);
154 }
155 
156 Expected<APInt> llvm::exprAdd(const APInt &LeftOperand,
157                               const APInt &RightOperand, bool &Overflow) {
158   return LeftOperand.sadd_ov(RightOperand, Overflow);
159 }
160 
161 Expected<APInt> llvm::exprSub(const APInt &LeftOperand,
162                               const APInt &RightOperand, bool &Overflow) {
163   return LeftOperand.ssub_ov(RightOperand, Overflow);
164 }
165 
166 Expected<APInt> llvm::exprMul(const APInt &LeftOperand,
167                               const APInt &RightOperand, bool &Overflow) {
168   return LeftOperand.smul_ov(RightOperand, Overflow);
169 }
170 
171 Expected<APInt> llvm::exprDiv(const APInt &LeftOperand,
172                               const APInt &RightOperand, bool &Overflow) {
173   // Check for division by zero.
174   if (RightOperand.isZero())
175     return make_error<OverflowError>();
176 
177   return LeftOperand.sdiv_ov(RightOperand, Overflow);
178 }
179 
180 Expected<APInt> llvm::exprMax(const APInt &LeftOperand,
181                               const APInt &RightOperand, bool &Overflow) {
182   Overflow = false;
183   return LeftOperand.slt(RightOperand) ? RightOperand : LeftOperand;
184 }
185 
186 Expected<APInt> llvm::exprMin(const APInt &LeftOperand,
187                               const APInt &RightOperand, bool &Overflow) {
188   Overflow = false;
189   if (cantFail(exprMax(LeftOperand, RightOperand, Overflow)) == LeftOperand)
190     return RightOperand;
191 
192   return LeftOperand;
193 }
194 
195 Expected<APInt> NumericVariableUse::eval() const {
196   std::optional<APInt> Value = Variable->getValue();
197   if (Value)
198     return *Value;
199 
200   return make_error<UndefVarError>(getExpressionStr());
201 }
202 
203 Expected<APInt> BinaryOperation::eval() const {
204   Expected<APInt> MaybeLeftOp = LeftOperand->eval();
205   Expected<APInt> MaybeRightOp = RightOperand->eval();
206 
207   // Bubble up any error (e.g. undefined variables) in the recursive
208   // evaluation.
209   if (!MaybeLeftOp || !MaybeRightOp) {
210     Error Err = Error::success();
211     if (!MaybeLeftOp)
212       Err = joinErrors(std::move(Err), MaybeLeftOp.takeError());
213     if (!MaybeRightOp)
214       Err = joinErrors(std::move(Err), MaybeRightOp.takeError());
215     return std::move(Err);
216   }
217 
218   APInt LeftOp = *MaybeLeftOp;
219   APInt RightOp = *MaybeRightOp;
220   bool Overflow;
221   // Ensure both operands have the same bitwidth.
222   unsigned LeftBitWidth = LeftOp.getBitWidth();
223   unsigned RightBitWidth = RightOp.getBitWidth();
224   unsigned NewBitWidth = std::max(LeftBitWidth, RightBitWidth);
225   LeftOp = LeftOp.sext(NewBitWidth);
226   RightOp = RightOp.sext(NewBitWidth);
227   do {
228     Expected<APInt> MaybeResult = EvalBinop(LeftOp, RightOp, Overflow);
229     if (!MaybeResult)
230       return MaybeResult.takeError();
231 
232     if (!Overflow)
233       return MaybeResult;
234 
235     NewBitWidth = nextAPIntBitWidth(NewBitWidth);
236     LeftOp = LeftOp.sext(NewBitWidth);
237     RightOp = RightOp.sext(NewBitWidth);
238   } while (true);
239 }
240 
241 Expected<ExpressionFormat>
242 BinaryOperation::getImplicitFormat(const SourceMgr &SM) const {
243   Expected<ExpressionFormat> LeftFormat = LeftOperand->getImplicitFormat(SM);
244   Expected<ExpressionFormat> RightFormat = RightOperand->getImplicitFormat(SM);
245   if (!LeftFormat || !RightFormat) {
246     Error Err = Error::success();
247     if (!LeftFormat)
248       Err = joinErrors(std::move(Err), LeftFormat.takeError());
249     if (!RightFormat)
250       Err = joinErrors(std::move(Err), RightFormat.takeError());
251     return std::move(Err);
252   }
253 
254   if (*LeftFormat != ExpressionFormat::Kind::NoFormat &&
255       *RightFormat != ExpressionFormat::Kind::NoFormat &&
256       *LeftFormat != *RightFormat)
257     return ErrorDiagnostic::get(
258         SM, getExpressionStr(),
259         "implicit format conflict between '" + LeftOperand->getExpressionStr() +
260             "' (" + LeftFormat->toString() + ") and '" +
261             RightOperand->getExpressionStr() + "' (" + RightFormat->toString() +
262             "), need an explicit format specifier");
263 
264   return *LeftFormat != ExpressionFormat::Kind::NoFormat ? *LeftFormat
265                                                          : *RightFormat;
266 }
267 
268 Expected<std::string> NumericSubstitution::getResult() const {
269   assert(ExpressionPointer->getAST() != nullptr &&
270          "Substituting empty expression");
271   Expected<APInt> EvaluatedValue = ExpressionPointer->getAST()->eval();
272   if (!EvaluatedValue)
273     return EvaluatedValue.takeError();
274   ExpressionFormat Format = ExpressionPointer->getFormat();
275   return Format.getMatchingString(*EvaluatedValue);
276 }
277 
278 Expected<std::string> StringSubstitution::getResult() const {
279   // Look up the value and escape it so that we can put it into the regex.
280   Expected<StringRef> VarVal = Context->getPatternVarValue(FromStr);
281   if (!VarVal)
282     return VarVal.takeError();
283   return Regex::escape(*VarVal);
284 }
285 
286 bool Pattern::isValidVarNameStart(char C) { return C == '_' || isAlpha(C); }
287 
288 Expected<Pattern::VariableProperties>
289 Pattern::parseVariable(StringRef &Str, const SourceMgr &SM) {
290   if (Str.empty())
291     return ErrorDiagnostic::get(SM, Str, "empty variable name");
292 
293   size_t I = 0;
294   bool IsPseudo = Str[0] == '@';
295 
296   // Global vars start with '$'.
297   if (Str[0] == '$' || IsPseudo)
298     ++I;
299 
300   if (!isValidVarNameStart(Str[I++]))
301     return ErrorDiagnostic::get(SM, Str, "invalid variable name");
302 
303   for (size_t E = Str.size(); I != E; ++I)
304     // Variable names are composed of alphanumeric characters and underscores.
305     if (Str[I] != '_' && !isAlnum(Str[I]))
306       break;
307 
308   StringRef Name = Str.take_front(I);
309   Str = Str.substr(I);
310   return VariableProperties {Name, IsPseudo};
311 }
312 
313 // StringRef holding all characters considered as horizontal whitespaces by
314 // FileCheck input canonicalization.
315 constexpr StringLiteral SpaceChars = " \t";
316 
317 // Parsing helper function that strips the first character in S and returns it.
318 static char popFront(StringRef &S) {
319   char C = S.front();
320   S = S.drop_front();
321   return C;
322 }
323 
324 char OverflowError::ID = 0;
325 char UndefVarError::ID = 0;
326 char ErrorDiagnostic::ID = 0;
327 char NotFoundError::ID = 0;
328 char ErrorReported::ID = 0;
329 
330 Expected<NumericVariable *> Pattern::parseNumericVariableDefinition(
331     StringRef &Expr, FileCheckPatternContext *Context,
332     std::optional<size_t> LineNumber, ExpressionFormat ImplicitFormat,
333     const SourceMgr &SM) {
334   Expected<VariableProperties> ParseVarResult = parseVariable(Expr, SM);
335   if (!ParseVarResult)
336     return ParseVarResult.takeError();
337   StringRef Name = ParseVarResult->Name;
338 
339   if (ParseVarResult->IsPseudo)
340     return ErrorDiagnostic::get(
341         SM, Name, "definition of pseudo numeric variable unsupported");
342 
343   // Detect collisions between string and numeric variables when the latter
344   // is created later than the former.
345   if (Context->DefinedVariableTable.contains(Name))
346     return ErrorDiagnostic::get(
347         SM, Name, "string variable with name '" + Name + "' already exists");
348 
349   Expr = Expr.ltrim(SpaceChars);
350   if (!Expr.empty())
351     return ErrorDiagnostic::get(
352         SM, Expr, "unexpected characters after numeric variable name");
353 
354   NumericVariable *DefinedNumericVariable;
355   auto VarTableIter = Context->GlobalNumericVariableTable.find(Name);
356   if (VarTableIter != Context->GlobalNumericVariableTable.end()) {
357     DefinedNumericVariable = VarTableIter->second;
358     if (DefinedNumericVariable->getImplicitFormat() != ImplicitFormat)
359       return ErrorDiagnostic::get(
360           SM, Expr, "format different from previous variable definition");
361   } else
362     DefinedNumericVariable =
363         Context->makeNumericVariable(Name, ImplicitFormat, LineNumber);
364 
365   return DefinedNumericVariable;
366 }
367 
368 Expected<std::unique_ptr<NumericVariableUse>> Pattern::parseNumericVariableUse(
369     StringRef Name, bool IsPseudo, std::optional<size_t> LineNumber,
370     FileCheckPatternContext *Context, const SourceMgr &SM) {
371   if (IsPseudo && !Name.equals("@LINE"))
372     return ErrorDiagnostic::get(
373         SM, Name, "invalid pseudo numeric variable '" + Name + "'");
374 
375   // Numeric variable definitions and uses are parsed in the order in which
376   // they appear in the CHECK patterns. For each definition, the pointer to the
377   // class instance of the corresponding numeric variable definition is stored
378   // in GlobalNumericVariableTable in parsePattern. Therefore, if the pointer
379   // we get below is null, it means no such variable was defined before. When
380   // that happens, we create a dummy variable so that parsing can continue. All
381   // uses of undefined variables, whether string or numeric, are then diagnosed
382   // in printNoMatch() after failing to match.
383   auto VarTableIter = Context->GlobalNumericVariableTable.find(Name);
384   NumericVariable *NumericVariable;
385   if (VarTableIter != Context->GlobalNumericVariableTable.end())
386     NumericVariable = VarTableIter->second;
387   else {
388     NumericVariable = Context->makeNumericVariable(
389         Name, ExpressionFormat(ExpressionFormat::Kind::Unsigned));
390     Context->GlobalNumericVariableTable[Name] = NumericVariable;
391   }
392 
393   std::optional<size_t> DefLineNumber = NumericVariable->getDefLineNumber();
394   if (DefLineNumber && LineNumber && *DefLineNumber == *LineNumber)
395     return ErrorDiagnostic::get(
396         SM, Name,
397         "numeric variable '" + Name +
398             "' defined earlier in the same CHECK directive");
399 
400   return std::make_unique<NumericVariableUse>(Name, NumericVariable);
401 }
402 
403 Expected<std::unique_ptr<ExpressionAST>> Pattern::parseNumericOperand(
404     StringRef &Expr, AllowedOperand AO, bool MaybeInvalidConstraint,
405     std::optional<size_t> LineNumber, FileCheckPatternContext *Context,
406     const SourceMgr &SM) {
407   if (Expr.starts_with("(")) {
408     if (AO != AllowedOperand::Any)
409       return ErrorDiagnostic::get(
410           SM, Expr, "parenthesized expression not permitted here");
411     return parseParenExpr(Expr, LineNumber, Context, SM);
412   }
413 
414   if (AO == AllowedOperand::LineVar || AO == AllowedOperand::Any) {
415     // Try to parse as a numeric variable use.
416     Expected<Pattern::VariableProperties> ParseVarResult =
417         parseVariable(Expr, SM);
418     if (ParseVarResult) {
419       // Try to parse a function call.
420       if (Expr.ltrim(SpaceChars).starts_with("(")) {
421         if (AO != AllowedOperand::Any)
422           return ErrorDiagnostic::get(SM, ParseVarResult->Name,
423                                       "unexpected function call");
424 
425         return parseCallExpr(Expr, ParseVarResult->Name, LineNumber, Context,
426                              SM);
427       }
428 
429       return parseNumericVariableUse(ParseVarResult->Name,
430                                      ParseVarResult->IsPseudo, LineNumber,
431                                      Context, SM);
432     }
433 
434     if (AO == AllowedOperand::LineVar)
435       return ParseVarResult.takeError();
436     // Ignore the error and retry parsing as a literal.
437     consumeError(ParseVarResult.takeError());
438   }
439 
440   // Otherwise, parse it as a literal.
441   APInt LiteralValue;
442   StringRef SaveExpr = Expr;
443   bool Negative = Expr.consume_front("-");
444   if (!Expr.consumeInteger((AO == AllowedOperand::LegacyLiteral) ? 10 : 0,
445                            LiteralValue)) {
446     LiteralValue = toSigned(LiteralValue, Negative);
447     return std::make_unique<ExpressionLiteral>(SaveExpr.drop_back(Expr.size()),
448                                                LiteralValue);
449   }
450   return ErrorDiagnostic::get(
451       SM, SaveExpr,
452       Twine("invalid ") +
453           (MaybeInvalidConstraint ? "matching constraint or " : "") +
454           "operand format");
455 }
456 
457 Expected<std::unique_ptr<ExpressionAST>>
458 Pattern::parseParenExpr(StringRef &Expr, std::optional<size_t> LineNumber,
459                         FileCheckPatternContext *Context, const SourceMgr &SM) {
460   Expr = Expr.ltrim(SpaceChars);
461   assert(Expr.starts_with("("));
462 
463   // Parse right operand.
464   Expr.consume_front("(");
465   Expr = Expr.ltrim(SpaceChars);
466   if (Expr.empty())
467     return ErrorDiagnostic::get(SM, Expr, "missing operand in expression");
468 
469   // Note: parseNumericOperand handles nested opening parentheses.
470   Expected<std::unique_ptr<ExpressionAST>> SubExprResult = parseNumericOperand(
471       Expr, AllowedOperand::Any, /*MaybeInvalidConstraint=*/false, LineNumber,
472       Context, SM);
473   Expr = Expr.ltrim(SpaceChars);
474   while (SubExprResult && !Expr.empty() && !Expr.starts_with(")")) {
475     StringRef OrigExpr = Expr;
476     SubExprResult = parseBinop(OrigExpr, Expr, std::move(*SubExprResult), false,
477                                LineNumber, Context, SM);
478     Expr = Expr.ltrim(SpaceChars);
479   }
480   if (!SubExprResult)
481     return SubExprResult;
482 
483   if (!Expr.consume_front(")")) {
484     return ErrorDiagnostic::get(SM, Expr,
485                                 "missing ')' at end of nested expression");
486   }
487   return SubExprResult;
488 }
489 
490 Expected<std::unique_ptr<ExpressionAST>>
491 Pattern::parseBinop(StringRef Expr, StringRef &RemainingExpr,
492                     std::unique_ptr<ExpressionAST> LeftOp,
493                     bool IsLegacyLineExpr, std::optional<size_t> LineNumber,
494                     FileCheckPatternContext *Context, const SourceMgr &SM) {
495   RemainingExpr = RemainingExpr.ltrim(SpaceChars);
496   if (RemainingExpr.empty())
497     return std::move(LeftOp);
498 
499   // Check if this is a supported operation and select a function to perform
500   // it.
501   SMLoc OpLoc = SMLoc::getFromPointer(RemainingExpr.data());
502   char Operator = popFront(RemainingExpr);
503   binop_eval_t EvalBinop;
504   switch (Operator) {
505   case '+':
506     EvalBinop = exprAdd;
507     break;
508   case '-':
509     EvalBinop = exprSub;
510     break;
511   default:
512     return ErrorDiagnostic::get(
513         SM, OpLoc, Twine("unsupported operation '") + Twine(Operator) + "'");
514   }
515 
516   // Parse right operand.
517   RemainingExpr = RemainingExpr.ltrim(SpaceChars);
518   if (RemainingExpr.empty())
519     return ErrorDiagnostic::get(SM, RemainingExpr,
520                                 "missing operand in expression");
521   // The second operand in a legacy @LINE expression is always a literal.
522   AllowedOperand AO =
523       IsLegacyLineExpr ? AllowedOperand::LegacyLiteral : AllowedOperand::Any;
524   Expected<std::unique_ptr<ExpressionAST>> RightOpResult =
525       parseNumericOperand(RemainingExpr, AO, /*MaybeInvalidConstraint=*/false,
526                           LineNumber, Context, SM);
527   if (!RightOpResult)
528     return RightOpResult;
529 
530   Expr = Expr.drop_back(RemainingExpr.size());
531   return std::make_unique<BinaryOperation>(Expr, EvalBinop, std::move(LeftOp),
532                                            std::move(*RightOpResult));
533 }
534 
535 Expected<std::unique_ptr<ExpressionAST>>
536 Pattern::parseCallExpr(StringRef &Expr, StringRef FuncName,
537                        std::optional<size_t> LineNumber,
538                        FileCheckPatternContext *Context, const SourceMgr &SM) {
539   Expr = Expr.ltrim(SpaceChars);
540   assert(Expr.starts_with("("));
541 
542   auto OptFunc = StringSwitch<binop_eval_t>(FuncName)
543                      .Case("add", exprAdd)
544                      .Case("div", exprDiv)
545                      .Case("max", exprMax)
546                      .Case("min", exprMin)
547                      .Case("mul", exprMul)
548                      .Case("sub", exprSub)
549                      .Default(nullptr);
550 
551   if (!OptFunc)
552     return ErrorDiagnostic::get(
553         SM, FuncName, Twine("call to undefined function '") + FuncName + "'");
554 
555   Expr.consume_front("(");
556   Expr = Expr.ltrim(SpaceChars);
557 
558   // Parse call arguments, which are comma separated.
559   SmallVector<std::unique_ptr<ExpressionAST>, 4> Args;
560   while (!Expr.empty() && !Expr.starts_with(")")) {
561     if (Expr.starts_with(","))
562       return ErrorDiagnostic::get(SM, Expr, "missing argument");
563 
564     // Parse the argument, which is an arbitary expression.
565     StringRef OuterBinOpExpr = Expr;
566     Expected<std::unique_ptr<ExpressionAST>> Arg = parseNumericOperand(
567         Expr, AllowedOperand::Any, /*MaybeInvalidConstraint=*/false, LineNumber,
568         Context, SM);
569     while (Arg && !Expr.empty()) {
570       Expr = Expr.ltrim(SpaceChars);
571       // Have we reached an argument terminator?
572       if (Expr.starts_with(",") || Expr.starts_with(")"))
573         break;
574 
575       // Arg = Arg <op> <expr>
576       Arg = parseBinop(OuterBinOpExpr, Expr, std::move(*Arg), false, LineNumber,
577                        Context, SM);
578     }
579 
580     // Prefer an expression error over a generic invalid argument message.
581     if (!Arg)
582       return Arg.takeError();
583     Args.push_back(std::move(*Arg));
584 
585     // Have we parsed all available arguments?
586     Expr = Expr.ltrim(SpaceChars);
587     if (!Expr.consume_front(","))
588       break;
589 
590     Expr = Expr.ltrim(SpaceChars);
591     if (Expr.starts_with(")"))
592       return ErrorDiagnostic::get(SM, Expr, "missing argument");
593   }
594 
595   if (!Expr.consume_front(")"))
596     return ErrorDiagnostic::get(SM, Expr,
597                                 "missing ')' at end of call expression");
598 
599   const unsigned NumArgs = Args.size();
600   if (NumArgs == 2)
601     return std::make_unique<BinaryOperation>(Expr, *OptFunc, std::move(Args[0]),
602                                              std::move(Args[1]));
603 
604   // TODO: Support more than binop_eval_t.
605   return ErrorDiagnostic::get(SM, FuncName,
606                               Twine("function '") + FuncName +
607                                   Twine("' takes 2 arguments but ") +
608                                   Twine(NumArgs) + " given");
609 }
610 
611 Expected<std::unique_ptr<Expression>> Pattern::parseNumericSubstitutionBlock(
612     StringRef Expr, std::optional<NumericVariable *> &DefinedNumericVariable,
613     bool IsLegacyLineExpr, std::optional<size_t> LineNumber,
614     FileCheckPatternContext *Context, const SourceMgr &SM) {
615   std::unique_ptr<ExpressionAST> ExpressionASTPointer = nullptr;
616   StringRef DefExpr = StringRef();
617   DefinedNumericVariable = std::nullopt;
618   ExpressionFormat ExplicitFormat = ExpressionFormat();
619   unsigned Precision = 0;
620 
621   // Parse format specifier (NOTE: ',' is also an argument seperator).
622   size_t FormatSpecEnd = Expr.find(',');
623   size_t FunctionStart = Expr.find('(');
624   if (FormatSpecEnd != StringRef::npos && FormatSpecEnd < FunctionStart) {
625     StringRef FormatExpr = Expr.take_front(FormatSpecEnd);
626     Expr = Expr.drop_front(FormatSpecEnd + 1);
627     FormatExpr = FormatExpr.trim(SpaceChars);
628     if (!FormatExpr.consume_front("%"))
629       return ErrorDiagnostic::get(
630           SM, FormatExpr,
631           "invalid matching format specification in expression");
632 
633     // Parse alternate form flag.
634     SMLoc AlternateFormFlagLoc = SMLoc::getFromPointer(FormatExpr.data());
635     bool AlternateForm = FormatExpr.consume_front("#");
636 
637     // Parse precision.
638     if (FormatExpr.consume_front(".")) {
639       if (FormatExpr.consumeInteger(10, Precision))
640         return ErrorDiagnostic::get(SM, FormatExpr,
641                                     "invalid precision in format specifier");
642     }
643 
644     if (!FormatExpr.empty()) {
645       // Check for unknown matching format specifier and set matching format in
646       // class instance representing this expression.
647       SMLoc FmtLoc = SMLoc::getFromPointer(FormatExpr.data());
648       switch (popFront(FormatExpr)) {
649       case 'u':
650         ExplicitFormat =
651             ExpressionFormat(ExpressionFormat::Kind::Unsigned, Precision);
652         break;
653       case 'd':
654         ExplicitFormat =
655             ExpressionFormat(ExpressionFormat::Kind::Signed, Precision);
656         break;
657       case 'x':
658         ExplicitFormat = ExpressionFormat(ExpressionFormat::Kind::HexLower,
659                                           Precision, AlternateForm);
660         break;
661       case 'X':
662         ExplicitFormat = ExpressionFormat(ExpressionFormat::Kind::HexUpper,
663                                           Precision, AlternateForm);
664         break;
665       default:
666         return ErrorDiagnostic::get(SM, FmtLoc,
667                                     "invalid format specifier in expression");
668       }
669     }
670 
671     if (AlternateForm && ExplicitFormat != ExpressionFormat::Kind::HexLower &&
672         ExplicitFormat != ExpressionFormat::Kind::HexUpper)
673       return ErrorDiagnostic::get(
674           SM, AlternateFormFlagLoc,
675           "alternate form only supported for hex values");
676 
677     FormatExpr = FormatExpr.ltrim(SpaceChars);
678     if (!FormatExpr.empty())
679       return ErrorDiagnostic::get(
680           SM, FormatExpr,
681           "invalid matching format specification in expression");
682   }
683 
684   // Save variable definition expression if any.
685   size_t DefEnd = Expr.find(':');
686   if (DefEnd != StringRef::npos) {
687     DefExpr = Expr.substr(0, DefEnd);
688     Expr = Expr.substr(DefEnd + 1);
689   }
690 
691   // Parse matching constraint.
692   Expr = Expr.ltrim(SpaceChars);
693   bool HasParsedValidConstraint = false;
694   if (Expr.consume_front("=="))
695     HasParsedValidConstraint = true;
696 
697   // Parse the expression itself.
698   Expr = Expr.ltrim(SpaceChars);
699   if (Expr.empty()) {
700     if (HasParsedValidConstraint)
701       return ErrorDiagnostic::get(
702           SM, Expr, "empty numeric expression should not have a constraint");
703   } else {
704     Expr = Expr.rtrim(SpaceChars);
705     StringRef OuterBinOpExpr = Expr;
706     // The first operand in a legacy @LINE expression is always the @LINE
707     // pseudo variable.
708     AllowedOperand AO =
709         IsLegacyLineExpr ? AllowedOperand::LineVar : AllowedOperand::Any;
710     Expected<std::unique_ptr<ExpressionAST>> ParseResult = parseNumericOperand(
711         Expr, AO, !HasParsedValidConstraint, LineNumber, Context, SM);
712     while (ParseResult && !Expr.empty()) {
713       ParseResult = parseBinop(OuterBinOpExpr, Expr, std::move(*ParseResult),
714                                IsLegacyLineExpr, LineNumber, Context, SM);
715       // Legacy @LINE expressions only allow 2 operands.
716       if (ParseResult && IsLegacyLineExpr && !Expr.empty())
717         return ErrorDiagnostic::get(
718             SM, Expr,
719             "unexpected characters at end of expression '" + Expr + "'");
720     }
721     if (!ParseResult)
722       return ParseResult.takeError();
723     ExpressionASTPointer = std::move(*ParseResult);
724   }
725 
726   // Select format of the expression, i.e. (i) its explicit format, if any,
727   // otherwise (ii) its implicit format, if any, otherwise (iii) the default
728   // format (unsigned). Error out in case of conflicting implicit format
729   // without explicit format.
730   ExpressionFormat Format;
731   if (ExplicitFormat)
732     Format = ExplicitFormat;
733   else if (ExpressionASTPointer) {
734     Expected<ExpressionFormat> ImplicitFormat =
735         ExpressionASTPointer->getImplicitFormat(SM);
736     if (!ImplicitFormat)
737       return ImplicitFormat.takeError();
738     Format = *ImplicitFormat;
739   }
740   if (!Format)
741     Format = ExpressionFormat(ExpressionFormat::Kind::Unsigned, Precision);
742 
743   std::unique_ptr<Expression> ExpressionPointer =
744       std::make_unique<Expression>(std::move(ExpressionASTPointer), Format);
745 
746   // Parse the numeric variable definition.
747   if (DefEnd != StringRef::npos) {
748     DefExpr = DefExpr.ltrim(SpaceChars);
749     Expected<NumericVariable *> ParseResult = parseNumericVariableDefinition(
750         DefExpr, Context, LineNumber, ExpressionPointer->getFormat(), SM);
751 
752     if (!ParseResult)
753       return ParseResult.takeError();
754     DefinedNumericVariable = *ParseResult;
755   }
756 
757   return std::move(ExpressionPointer);
758 }
759 
760 bool Pattern::parsePattern(StringRef PatternStr, StringRef Prefix,
761                            SourceMgr &SM, const FileCheckRequest &Req) {
762   bool MatchFullLinesHere = Req.MatchFullLines && CheckTy != Check::CheckNot;
763   IgnoreCase = Req.IgnoreCase;
764 
765   PatternLoc = SMLoc::getFromPointer(PatternStr.data());
766 
767   if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
768     // Ignore trailing whitespace.
769     while (!PatternStr.empty() &&
770            (PatternStr.back() == ' ' || PatternStr.back() == '\t'))
771       PatternStr = PatternStr.substr(0, PatternStr.size() - 1);
772 
773   // Check that there is something on the line.
774   if (PatternStr.empty() && CheckTy != Check::CheckEmpty) {
775     SM.PrintMessage(PatternLoc, SourceMgr::DK_Error,
776                     "found empty check string with prefix '" + Prefix + ":'");
777     return true;
778   }
779 
780   if (!PatternStr.empty() && CheckTy == Check::CheckEmpty) {
781     SM.PrintMessage(
782         PatternLoc, SourceMgr::DK_Error,
783         "found non-empty check string for empty check with prefix '" + Prefix +
784             ":'");
785     return true;
786   }
787 
788   if (CheckTy == Check::CheckEmpty) {
789     RegExStr = "(\n$)";
790     return false;
791   }
792 
793   // If literal check, set fixed string.
794   if (CheckTy.isLiteralMatch()) {
795     FixedStr = PatternStr;
796     return false;
797   }
798 
799   // Check to see if this is a fixed string, or if it has regex pieces.
800   if (!MatchFullLinesHere &&
801       (PatternStr.size() < 2 ||
802        (!PatternStr.contains("{{") && !PatternStr.contains("[[")))) {
803     FixedStr = PatternStr;
804     return false;
805   }
806 
807   if (MatchFullLinesHere) {
808     RegExStr += '^';
809     if (!Req.NoCanonicalizeWhiteSpace)
810       RegExStr += " *";
811   }
812 
813   // Paren value #0 is for the fully matched string.  Any new parenthesized
814   // values add from there.
815   unsigned CurParen = 1;
816 
817   // Otherwise, there is at least one regex piece.  Build up the regex pattern
818   // by escaping scary characters in fixed strings, building up one big regex.
819   while (!PatternStr.empty()) {
820     // RegEx matches.
821     if (PatternStr.starts_with("{{")) {
822       // This is the start of a regex match.  Scan for the }}.
823       size_t End = PatternStr.find("}}");
824       if (End == StringRef::npos) {
825         SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
826                         SourceMgr::DK_Error,
827                         "found start of regex string with no end '}}'");
828         return true;
829       }
830 
831       // Enclose {{}} patterns in parens just like [[]] even though we're not
832       // capturing the result for any purpose.  This is required in case the
833       // expression contains an alternation like: CHECK:  abc{{x|z}}def.  We
834       // want this to turn into: "abc(x|z)def" not "abcx|zdef".
835       bool HasAlternation = PatternStr.contains('|');
836       if (HasAlternation) {
837         RegExStr += '(';
838         ++CurParen;
839       }
840 
841       if (AddRegExToRegEx(PatternStr.substr(2, End - 2), CurParen, SM))
842         return true;
843       if (HasAlternation)
844         RegExStr += ')';
845 
846       PatternStr = PatternStr.substr(End + 2);
847       continue;
848     }
849 
850     // String and numeric substitution blocks. Pattern substitution blocks come
851     // in two forms: [[foo:.*]] and [[foo]]. The former matches .* (or some
852     // other regex) and assigns it to the string variable 'foo'. The latter
853     // substitutes foo's value. Numeric substitution blocks recognize the same
854     // form as string ones, but start with a '#' sign after the double
855     // brackets. They also accept a combined form which sets a numeric variable
856     // to the evaluation of an expression. Both string and numeric variable
857     // names must satisfy the regular expression "[a-zA-Z_][0-9a-zA-Z_]*" to be
858     // valid, as this helps catch some common errors. If there are extra '['s
859     // before the "[[", treat them literally.
860     if (PatternStr.starts_with("[[") && !PatternStr.starts_with("[[[")) {
861       StringRef UnparsedPatternStr = PatternStr.substr(2);
862       // Find the closing bracket pair ending the match.  End is going to be an
863       // offset relative to the beginning of the match string.
864       size_t End = FindRegexVarEnd(UnparsedPatternStr, SM);
865       StringRef MatchStr = UnparsedPatternStr.substr(0, End);
866       bool IsNumBlock = MatchStr.consume_front("#");
867 
868       if (End == StringRef::npos) {
869         SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()),
870                         SourceMgr::DK_Error,
871                         "Invalid substitution block, no ]] found");
872         return true;
873       }
874       // Strip the substitution block we are parsing. End points to the start
875       // of the "]]" closing the expression so account for it in computing the
876       // index of the first unparsed character.
877       PatternStr = UnparsedPatternStr.substr(End + 2);
878 
879       bool IsDefinition = false;
880       bool SubstNeeded = false;
881       // Whether the substitution block is a legacy use of @LINE with string
882       // substitution block syntax.
883       bool IsLegacyLineExpr = false;
884       StringRef DefName;
885       StringRef SubstStr;
886       StringRef MatchRegexp;
887       std::string WildcardRegexp;
888       size_t SubstInsertIdx = RegExStr.size();
889 
890       // Parse string variable or legacy @LINE expression.
891       if (!IsNumBlock) {
892         size_t VarEndIdx = MatchStr.find(':');
893         size_t SpacePos = MatchStr.substr(0, VarEndIdx).find_first_of(" \t");
894         if (SpacePos != StringRef::npos) {
895           SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data() + SpacePos),
896                           SourceMgr::DK_Error, "unexpected whitespace");
897           return true;
898         }
899 
900         // Get the name (e.g. "foo") and verify it is well formed.
901         StringRef OrigMatchStr = MatchStr;
902         Expected<Pattern::VariableProperties> ParseVarResult =
903             parseVariable(MatchStr, SM);
904         if (!ParseVarResult) {
905           logAllUnhandledErrors(ParseVarResult.takeError(), errs());
906           return true;
907         }
908         StringRef Name = ParseVarResult->Name;
909         bool IsPseudo = ParseVarResult->IsPseudo;
910 
911         IsDefinition = (VarEndIdx != StringRef::npos);
912         SubstNeeded = !IsDefinition;
913         if (IsDefinition) {
914           if ((IsPseudo || !MatchStr.consume_front(":"))) {
915             SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
916                             SourceMgr::DK_Error,
917                             "invalid name in string variable definition");
918             return true;
919           }
920 
921           // Detect collisions between string and numeric variables when the
922           // former is created later than the latter.
923           if (Context->GlobalNumericVariableTable.contains(Name)) {
924             SM.PrintMessage(
925                 SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error,
926                 "numeric variable with name '" + Name + "' already exists");
927             return true;
928           }
929           DefName = Name;
930           MatchRegexp = MatchStr;
931         } else {
932           if (IsPseudo) {
933             MatchStr = OrigMatchStr;
934             IsLegacyLineExpr = IsNumBlock = true;
935           } else {
936             if (!MatchStr.empty()) {
937               SM.PrintMessage(SMLoc::getFromPointer(Name.data()),
938                               SourceMgr::DK_Error,
939                               "invalid name in string variable use");
940               return true;
941             }
942             SubstStr = Name;
943           }
944         }
945       }
946 
947       // Parse numeric substitution block.
948       std::unique_ptr<Expression> ExpressionPointer;
949       std::optional<NumericVariable *> DefinedNumericVariable;
950       if (IsNumBlock) {
951         Expected<std::unique_ptr<Expression>> ParseResult =
952             parseNumericSubstitutionBlock(MatchStr, DefinedNumericVariable,
953                                           IsLegacyLineExpr, LineNumber, Context,
954                                           SM);
955         if (!ParseResult) {
956           logAllUnhandledErrors(ParseResult.takeError(), errs());
957           return true;
958         }
959         ExpressionPointer = std::move(*ParseResult);
960         SubstNeeded = ExpressionPointer->getAST() != nullptr;
961         if (DefinedNumericVariable) {
962           IsDefinition = true;
963           DefName = (*DefinedNumericVariable)->getName();
964         }
965         if (SubstNeeded)
966           SubstStr = MatchStr;
967         else {
968           ExpressionFormat Format = ExpressionPointer->getFormat();
969           WildcardRegexp = cantFail(Format.getWildcardRegex());
970           MatchRegexp = WildcardRegexp;
971         }
972       }
973 
974       // Handle variable definition: [[<def>:(...)]] and [[#(...)<def>:(...)]].
975       if (IsDefinition) {
976         RegExStr += '(';
977         ++SubstInsertIdx;
978 
979         if (IsNumBlock) {
980           NumericVariableMatch NumericVariableDefinition = {
981               *DefinedNumericVariable, CurParen};
982           NumericVariableDefs[DefName] = NumericVariableDefinition;
983           // This store is done here rather than in match() to allow
984           // parseNumericVariableUse() to get the pointer to the class instance
985           // of the right variable definition corresponding to a given numeric
986           // variable use.
987           Context->GlobalNumericVariableTable[DefName] =
988               *DefinedNumericVariable;
989         } else {
990           VariableDefs[DefName] = CurParen;
991           // Mark string variable as defined to detect collisions between
992           // string and numeric variables in parseNumericVariableUse() and
993           // defineCmdlineVariables() when the latter is created later than the
994           // former. We cannot reuse GlobalVariableTable for this by populating
995           // it with an empty string since we would then lose the ability to
996           // detect the use of an undefined variable in match().
997           Context->DefinedVariableTable[DefName] = true;
998         }
999 
1000         ++CurParen;
1001       }
1002 
1003       if (!MatchRegexp.empty() && AddRegExToRegEx(MatchRegexp, CurParen, SM))
1004         return true;
1005 
1006       if (IsDefinition)
1007         RegExStr += ')';
1008 
1009       // Handle substitutions: [[foo]] and [[#<foo expr>]].
1010       if (SubstNeeded) {
1011         // Handle substitution of string variables that were defined earlier on
1012         // the same line by emitting a backreference. Expressions do not
1013         // support substituting a numeric variable defined on the same line.
1014         if (!IsNumBlock && VariableDefs.find(SubstStr) != VariableDefs.end()) {
1015           unsigned CaptureParenGroup = VariableDefs[SubstStr];
1016           if (CaptureParenGroup < 1 || CaptureParenGroup > 9) {
1017             SM.PrintMessage(SMLoc::getFromPointer(SubstStr.data()),
1018                             SourceMgr::DK_Error,
1019                             "Can't back-reference more than 9 variables");
1020             return true;
1021           }
1022           AddBackrefToRegEx(CaptureParenGroup);
1023         } else {
1024           // Handle substitution of string variables ([[<var>]]) defined in
1025           // previous CHECK patterns, and substitution of expressions.
1026           Substitution *Substitution =
1027               IsNumBlock
1028                   ? Context->makeNumericSubstitution(
1029                         SubstStr, std::move(ExpressionPointer), SubstInsertIdx)
1030                   : Context->makeStringSubstitution(SubstStr, SubstInsertIdx);
1031           Substitutions.push_back(Substitution);
1032         }
1033       }
1034 
1035       continue;
1036     }
1037 
1038     // Handle fixed string matches.
1039     // Find the end, which is the start of the next regex.
1040     size_t FixedMatchEnd =
1041         std::min(PatternStr.find("{{", 1), PatternStr.find("[[", 1));
1042     RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd));
1043     PatternStr = PatternStr.substr(FixedMatchEnd);
1044   }
1045 
1046   if (MatchFullLinesHere) {
1047     if (!Req.NoCanonicalizeWhiteSpace)
1048       RegExStr += " *";
1049     RegExStr += '$';
1050   }
1051 
1052   return false;
1053 }
1054 
1055 bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) {
1056   Regex R(RS);
1057   std::string Error;
1058   if (!R.isValid(Error)) {
1059     SM.PrintMessage(SMLoc::getFromPointer(RS.data()), SourceMgr::DK_Error,
1060                     "invalid regex: " + Error);
1061     return true;
1062   }
1063 
1064   RegExStr += RS.str();
1065   CurParen += R.getNumMatches();
1066   return false;
1067 }
1068 
1069 void Pattern::AddBackrefToRegEx(unsigned BackrefNum) {
1070   assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number");
1071   std::string Backref = std::string("\\") + std::string(1, '0' + BackrefNum);
1072   RegExStr += Backref;
1073 }
1074 
1075 Pattern::MatchResult Pattern::match(StringRef Buffer,
1076                                     const SourceMgr &SM) const {
1077   // If this is the EOF pattern, match it immediately.
1078   if (CheckTy == Check::CheckEOF)
1079     return MatchResult(Buffer.size(), 0, Error::success());
1080 
1081   // If this is a fixed string pattern, just match it now.
1082   if (!FixedStr.empty()) {
1083     size_t Pos =
1084         IgnoreCase ? Buffer.find_insensitive(FixedStr) : Buffer.find(FixedStr);
1085     if (Pos == StringRef::npos)
1086       return make_error<NotFoundError>();
1087     return MatchResult(Pos, /*MatchLen=*/FixedStr.size(), Error::success());
1088   }
1089 
1090   // Regex match.
1091 
1092   // If there are substitutions, we need to create a temporary string with the
1093   // actual value.
1094   StringRef RegExToMatch = RegExStr;
1095   std::string TmpStr;
1096   if (!Substitutions.empty()) {
1097     TmpStr = RegExStr;
1098     if (LineNumber)
1099       Context->LineVariable->setValue(
1100           APInt(sizeof(*LineNumber) * 8, *LineNumber));
1101 
1102     size_t InsertOffset = 0;
1103     // Substitute all string variables and expressions whose values are only
1104     // now known. Use of string variables defined on the same line are handled
1105     // by back-references.
1106     Error Errs = Error::success();
1107     for (const auto &Substitution : Substitutions) {
1108       // Substitute and check for failure (e.g. use of undefined variable).
1109       Expected<std::string> Value = Substitution->getResult();
1110       if (!Value) {
1111         // Convert to an ErrorDiagnostic to get location information. This is
1112         // done here rather than printMatch/printNoMatch since now we know which
1113         // substitution block caused the overflow.
1114         Errs = joinErrors(std::move(Errs),
1115                           handleErrors(
1116                               Value.takeError(),
1117                               [&](const OverflowError &E) {
1118                                 return ErrorDiagnostic::get(
1119                                     SM, Substitution->getFromString(),
1120                                     "unable to substitute variable or "
1121                                     "numeric expression: overflow error");
1122                               },
1123                               [&SM](const UndefVarError &E) {
1124                                 return ErrorDiagnostic::get(SM, E.getVarName(),
1125                                                             E.message());
1126                               }));
1127         continue;
1128       }
1129 
1130       // Plop it into the regex at the adjusted offset.
1131       TmpStr.insert(TmpStr.begin() + Substitution->getIndex() + InsertOffset,
1132                     Value->begin(), Value->end());
1133       InsertOffset += Value->size();
1134     }
1135     if (Errs)
1136       return std::move(Errs);
1137 
1138     // Match the newly constructed regex.
1139     RegExToMatch = TmpStr;
1140   }
1141 
1142   SmallVector<StringRef, 4> MatchInfo;
1143   unsigned int Flags = Regex::Newline;
1144   if (IgnoreCase)
1145     Flags |= Regex::IgnoreCase;
1146   if (!Regex(RegExToMatch, Flags).match(Buffer, &MatchInfo))
1147     return make_error<NotFoundError>();
1148 
1149   // Successful regex match.
1150   assert(!MatchInfo.empty() && "Didn't get any match");
1151   StringRef FullMatch = MatchInfo[0];
1152 
1153   // If this defines any string variables, remember their values.
1154   for (const auto &VariableDef : VariableDefs) {
1155     assert(VariableDef.second < MatchInfo.size() && "Internal paren error");
1156     Context->GlobalVariableTable[VariableDef.first] =
1157         MatchInfo[VariableDef.second];
1158   }
1159 
1160   // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after
1161   // the required preceding newline, which is consumed by the pattern in the
1162   // case of CHECK-EMPTY but not CHECK-NEXT.
1163   size_t MatchStartSkip = CheckTy == Check::CheckEmpty;
1164   Match TheMatch;
1165   TheMatch.Pos = FullMatch.data() - Buffer.data() + MatchStartSkip;
1166   TheMatch.Len = FullMatch.size() - MatchStartSkip;
1167 
1168   // If this defines any numeric variables, remember their values.
1169   for (const auto &NumericVariableDef : NumericVariableDefs) {
1170     const NumericVariableMatch &NumericVariableMatch =
1171         NumericVariableDef.getValue();
1172     unsigned CaptureParenGroup = NumericVariableMatch.CaptureParenGroup;
1173     assert(CaptureParenGroup < MatchInfo.size() && "Internal paren error");
1174     NumericVariable *DefinedNumericVariable =
1175         NumericVariableMatch.DefinedNumericVariable;
1176 
1177     StringRef MatchedValue = MatchInfo[CaptureParenGroup];
1178     ExpressionFormat Format = DefinedNumericVariable->getImplicitFormat();
1179     APInt Value = Format.valueFromStringRepr(MatchedValue, SM);
1180     DefinedNumericVariable->setValue(Value, MatchedValue);
1181   }
1182 
1183   return MatchResult(TheMatch, Error::success());
1184 }
1185 
1186 unsigned Pattern::computeMatchDistance(StringRef Buffer) const {
1187   // Just compute the number of matching characters. For regular expressions, we
1188   // just compare against the regex itself and hope for the best.
1189   //
1190   // FIXME: One easy improvement here is have the regex lib generate a single
1191   // example regular expression which matches, and use that as the example
1192   // string.
1193   StringRef ExampleString(FixedStr);
1194   if (ExampleString.empty())
1195     ExampleString = RegExStr;
1196 
1197   // Only compare up to the first line in the buffer, or the string size.
1198   StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
1199   BufferPrefix = BufferPrefix.split('\n').first;
1200   return BufferPrefix.edit_distance(ExampleString);
1201 }
1202 
1203 void Pattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer,
1204                                  SMRange Range,
1205                                  FileCheckDiag::MatchType MatchTy,
1206                                  std::vector<FileCheckDiag> *Diags) const {
1207   // Print what we know about substitutions.
1208   if (!Substitutions.empty()) {
1209     for (const auto &Substitution : Substitutions) {
1210       SmallString<256> Msg;
1211       raw_svector_ostream OS(Msg);
1212 
1213       Expected<std::string> MatchedValue = Substitution->getResult();
1214       // Substitution failures are handled in printNoMatch().
1215       if (!MatchedValue) {
1216         consumeError(MatchedValue.takeError());
1217         continue;
1218       }
1219 
1220       OS << "with \"";
1221       OS.write_escaped(Substitution->getFromString()) << "\" equal to \"";
1222       OS.write_escaped(*MatchedValue) << "\"";
1223 
1224       // We report only the start of the match/search range to suggest we are
1225       // reporting the substitutions as set at the start of the match/search.
1226       // Indicating a non-zero-length range might instead seem to imply that the
1227       // substitution matches or was captured from exactly that range.
1228       if (Diags)
1229         Diags->emplace_back(SM, CheckTy, getLoc(), MatchTy,
1230                             SMRange(Range.Start, Range.Start), OS.str());
1231       else
1232         SM.PrintMessage(Range.Start, SourceMgr::DK_Note, OS.str());
1233     }
1234   }
1235 }
1236 
1237 void Pattern::printVariableDefs(const SourceMgr &SM,
1238                                 FileCheckDiag::MatchType MatchTy,
1239                                 std::vector<FileCheckDiag> *Diags) const {
1240   if (VariableDefs.empty() && NumericVariableDefs.empty())
1241     return;
1242   // Build list of variable captures.
1243   struct VarCapture {
1244     StringRef Name;
1245     SMRange Range;
1246   };
1247   SmallVector<VarCapture, 2> VarCaptures;
1248   for (const auto &VariableDef : VariableDefs) {
1249     VarCapture VC;
1250     VC.Name = VariableDef.first;
1251     StringRef Value = Context->GlobalVariableTable[VC.Name];
1252     SMLoc Start = SMLoc::getFromPointer(Value.data());
1253     SMLoc End = SMLoc::getFromPointer(Value.data() + Value.size());
1254     VC.Range = SMRange(Start, End);
1255     VarCaptures.push_back(VC);
1256   }
1257   for (const auto &VariableDef : NumericVariableDefs) {
1258     VarCapture VC;
1259     VC.Name = VariableDef.getKey();
1260     std::optional<StringRef> StrValue =
1261         VariableDef.getValue().DefinedNumericVariable->getStringValue();
1262     if (!StrValue)
1263       continue;
1264     SMLoc Start = SMLoc::getFromPointer(StrValue->data());
1265     SMLoc End = SMLoc::getFromPointer(StrValue->data() + StrValue->size());
1266     VC.Range = SMRange(Start, End);
1267     VarCaptures.push_back(VC);
1268   }
1269   // Sort variable captures by the order in which they matched the input.
1270   // Ranges shouldn't be overlapping, so we can just compare the start.
1271   llvm::sort(VarCaptures, [](const VarCapture &A, const VarCapture &B) {
1272     if (&A == &B)
1273       return false;
1274     assert(A.Range.Start != B.Range.Start &&
1275            "unexpected overlapping variable captures");
1276     return A.Range.Start.getPointer() < B.Range.Start.getPointer();
1277   });
1278   // Create notes for the sorted captures.
1279   for (const VarCapture &VC : VarCaptures) {
1280     SmallString<256> Msg;
1281     raw_svector_ostream OS(Msg);
1282     OS << "captured var \"" << VC.Name << "\"";
1283     if (Diags)
1284       Diags->emplace_back(SM, CheckTy, getLoc(), MatchTy, VC.Range, OS.str());
1285     else
1286       SM.PrintMessage(VC.Range.Start, SourceMgr::DK_Note, OS.str(), VC.Range);
1287   }
1288 }
1289 
1290 static SMRange ProcessMatchResult(FileCheckDiag::MatchType MatchTy,
1291                                   const SourceMgr &SM, SMLoc Loc,
1292                                   Check::FileCheckType CheckTy,
1293                                   StringRef Buffer, size_t Pos, size_t Len,
1294                                   std::vector<FileCheckDiag> *Diags,
1295                                   bool AdjustPrevDiags = false) {
1296   SMLoc Start = SMLoc::getFromPointer(Buffer.data() + Pos);
1297   SMLoc End = SMLoc::getFromPointer(Buffer.data() + Pos + Len);
1298   SMRange Range(Start, End);
1299   if (Diags) {
1300     if (AdjustPrevDiags) {
1301       SMLoc CheckLoc = Diags->rbegin()->CheckLoc;
1302       for (auto I = Diags->rbegin(), E = Diags->rend();
1303            I != E && I->CheckLoc == CheckLoc; ++I)
1304         I->MatchTy = MatchTy;
1305     } else
1306       Diags->emplace_back(SM, CheckTy, Loc, MatchTy, Range);
1307   }
1308   return Range;
1309 }
1310 
1311 void Pattern::printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
1312                               std::vector<FileCheckDiag> *Diags) const {
1313   // Attempt to find the closest/best fuzzy match.  Usually an error happens
1314   // because some string in the output didn't exactly match. In these cases, we
1315   // would like to show the user a best guess at what "should have" matched, to
1316   // save them having to actually check the input manually.
1317   size_t NumLinesForward = 0;
1318   size_t Best = StringRef::npos;
1319   double BestQuality = 0;
1320 
1321   // Use an arbitrary 4k limit on how far we will search.
1322   for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) {
1323     if (Buffer[i] == '\n')
1324       ++NumLinesForward;
1325 
1326     // Patterns have leading whitespace stripped, so skip whitespace when
1327     // looking for something which looks like a pattern.
1328     if (Buffer[i] == ' ' || Buffer[i] == '\t')
1329       continue;
1330 
1331     // Compute the "quality" of this match as an arbitrary combination of the
1332     // match distance and the number of lines skipped to get to this match.
1333     unsigned Distance = computeMatchDistance(Buffer.substr(i));
1334     double Quality = Distance + (NumLinesForward / 100.);
1335 
1336     if (Quality < BestQuality || Best == StringRef::npos) {
1337       Best = i;
1338       BestQuality = Quality;
1339     }
1340   }
1341 
1342   // Print the "possible intended match here" line if we found something
1343   // reasonable and not equal to what we showed in the "scanning from here"
1344   // line.
1345   if (Best && Best != StringRef::npos && BestQuality < 50) {
1346     SMRange MatchRange =
1347         ProcessMatchResult(FileCheckDiag::MatchFuzzy, SM, getLoc(),
1348                            getCheckTy(), Buffer, Best, 0, Diags);
1349     SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note,
1350                     "possible intended match here");
1351 
1352     // FIXME: If we wanted to be really friendly we would show why the match
1353     // failed, as it can be hard to spot simple one character differences.
1354   }
1355 }
1356 
1357 Expected<StringRef>
1358 FileCheckPatternContext::getPatternVarValue(StringRef VarName) {
1359   auto VarIter = GlobalVariableTable.find(VarName);
1360   if (VarIter == GlobalVariableTable.end())
1361     return make_error<UndefVarError>(VarName);
1362 
1363   return VarIter->second;
1364 }
1365 
1366 template <class... Types>
1367 NumericVariable *FileCheckPatternContext::makeNumericVariable(Types... args) {
1368   NumericVariables.push_back(std::make_unique<NumericVariable>(args...));
1369   return NumericVariables.back().get();
1370 }
1371 
1372 Substitution *
1373 FileCheckPatternContext::makeStringSubstitution(StringRef VarName,
1374                                                 size_t InsertIdx) {
1375   Substitutions.push_back(
1376       std::make_unique<StringSubstitution>(this, VarName, InsertIdx));
1377   return Substitutions.back().get();
1378 }
1379 
1380 Substitution *FileCheckPatternContext::makeNumericSubstitution(
1381     StringRef ExpressionStr, std::unique_ptr<Expression> Expression,
1382     size_t InsertIdx) {
1383   Substitutions.push_back(std::make_unique<NumericSubstitution>(
1384       this, ExpressionStr, std::move(Expression), InsertIdx));
1385   return Substitutions.back().get();
1386 }
1387 
1388 size_t Pattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) {
1389   // Offset keeps track of the current offset within the input Str
1390   size_t Offset = 0;
1391   // [...] Nesting depth
1392   size_t BracketDepth = 0;
1393 
1394   while (!Str.empty()) {
1395     if (Str.starts_with("]]") && BracketDepth == 0)
1396       return Offset;
1397     if (Str[0] == '\\') {
1398       // Backslash escapes the next char within regexes, so skip them both.
1399       Str = Str.substr(2);
1400       Offset += 2;
1401     } else {
1402       switch (Str[0]) {
1403       default:
1404         break;
1405       case '[':
1406         BracketDepth++;
1407         break;
1408       case ']':
1409         if (BracketDepth == 0) {
1410           SM.PrintMessage(SMLoc::getFromPointer(Str.data()),
1411                           SourceMgr::DK_Error,
1412                           "missing closing \"]\" for regex variable");
1413           exit(1);
1414         }
1415         BracketDepth--;
1416         break;
1417       }
1418       Str = Str.substr(1);
1419       Offset++;
1420     }
1421   }
1422 
1423   return StringRef::npos;
1424 }
1425 
1426 StringRef FileCheck::CanonicalizeFile(MemoryBuffer &MB,
1427                                       SmallVectorImpl<char> &OutputBuffer) {
1428   OutputBuffer.reserve(MB.getBufferSize());
1429 
1430   for (const char *Ptr = MB.getBufferStart(), *End = MB.getBufferEnd();
1431        Ptr != End; ++Ptr) {
1432     // Eliminate trailing dosish \r.
1433     if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') {
1434       continue;
1435     }
1436 
1437     // If current char is not a horizontal whitespace or if horizontal
1438     // whitespace canonicalization is disabled, dump it to output as is.
1439     if (Req.NoCanonicalizeWhiteSpace || (*Ptr != ' ' && *Ptr != '\t')) {
1440       OutputBuffer.push_back(*Ptr);
1441       continue;
1442     }
1443 
1444     // Otherwise, add one space and advance over neighboring space.
1445     OutputBuffer.push_back(' ');
1446     while (Ptr + 1 != End && (Ptr[1] == ' ' || Ptr[1] == '\t'))
1447       ++Ptr;
1448   }
1449 
1450   // Add a null byte and then return all but that byte.
1451   OutputBuffer.push_back('\0');
1452   return StringRef(OutputBuffer.data(), OutputBuffer.size() - 1);
1453 }
1454 
1455 FileCheckDiag::FileCheckDiag(const SourceMgr &SM,
1456                              const Check::FileCheckType &CheckTy,
1457                              SMLoc CheckLoc, MatchType MatchTy,
1458                              SMRange InputRange, StringRef Note)
1459     : CheckTy(CheckTy), CheckLoc(CheckLoc), MatchTy(MatchTy), Note(Note) {
1460   auto Start = SM.getLineAndColumn(InputRange.Start);
1461   auto End = SM.getLineAndColumn(InputRange.End);
1462   InputStartLine = Start.first;
1463   InputStartCol = Start.second;
1464   InputEndLine = End.first;
1465   InputEndCol = End.second;
1466 }
1467 
1468 static bool IsPartOfWord(char c) {
1469   return (isAlnum(c) || c == '-' || c == '_');
1470 }
1471 
1472 Check::FileCheckType &Check::FileCheckType::setCount(int C) {
1473   assert(Count > 0 && "zero and negative counts are not supported");
1474   assert((C == 1 || Kind == CheckPlain) &&
1475          "count supported only for plain CHECK directives");
1476   Count = C;
1477   return *this;
1478 }
1479 
1480 std::string Check::FileCheckType::getModifiersDescription() const {
1481   if (Modifiers.none())
1482     return "";
1483   std::string Ret;
1484   raw_string_ostream OS(Ret);
1485   OS << '{';
1486   if (isLiteralMatch())
1487     OS << "LITERAL";
1488   OS << '}';
1489   return OS.str();
1490 }
1491 
1492 std::string Check::FileCheckType::getDescription(StringRef Prefix) const {
1493   // Append directive modifiers.
1494   auto WithModifiers = [this, Prefix](StringRef Str) -> std::string {
1495     return (Prefix + Str + getModifiersDescription()).str();
1496   };
1497 
1498   switch (Kind) {
1499   case Check::CheckNone:
1500     return "invalid";
1501   case Check::CheckMisspelled:
1502     return "misspelled";
1503   case Check::CheckPlain:
1504     if (Count > 1)
1505       return WithModifiers("-COUNT");
1506     return WithModifiers("");
1507   case Check::CheckNext:
1508     return WithModifiers("-NEXT");
1509   case Check::CheckSame:
1510     return WithModifiers("-SAME");
1511   case Check::CheckNot:
1512     return WithModifiers("-NOT");
1513   case Check::CheckDAG:
1514     return WithModifiers("-DAG");
1515   case Check::CheckLabel:
1516     return WithModifiers("-LABEL");
1517   case Check::CheckEmpty:
1518     return WithModifiers("-EMPTY");
1519   case Check::CheckComment:
1520     return std::string(Prefix);
1521   case Check::CheckEOF:
1522     return "implicit EOF";
1523   case Check::CheckBadNot:
1524     return "bad NOT";
1525   case Check::CheckBadCount:
1526     return "bad COUNT";
1527   }
1528   llvm_unreachable("unknown FileCheckType");
1529 }
1530 
1531 static std::pair<Check::FileCheckType, StringRef>
1532 FindCheckType(const FileCheckRequest &Req, StringRef Buffer, StringRef Prefix,
1533               bool &Misspelled) {
1534   if (Buffer.size() <= Prefix.size())
1535     return {Check::CheckNone, StringRef()};
1536 
1537   StringRef Rest = Buffer.drop_front(Prefix.size());
1538   // Check for comment.
1539   if (llvm::is_contained(Req.CommentPrefixes, Prefix)) {
1540     if (Rest.consume_front(":"))
1541       return {Check::CheckComment, Rest};
1542     // Ignore a comment prefix if it has a suffix like "-NOT".
1543     return {Check::CheckNone, StringRef()};
1544   }
1545 
1546   auto ConsumeModifiers = [&](Check::FileCheckType Ret)
1547       -> std::pair<Check::FileCheckType, StringRef> {
1548     if (Rest.consume_front(":"))
1549       return {Ret, Rest};
1550     if (!Rest.consume_front("{"))
1551       return {Check::CheckNone, StringRef()};
1552 
1553     // Parse the modifiers, speparated by commas.
1554     do {
1555       // Allow whitespace in modifiers list.
1556       Rest = Rest.ltrim();
1557       if (Rest.consume_front("LITERAL"))
1558         Ret.setLiteralMatch();
1559       else
1560         return {Check::CheckNone, Rest};
1561       // Allow whitespace in modifiers list.
1562       Rest = Rest.ltrim();
1563     } while (Rest.consume_front(","));
1564     if (!Rest.consume_front("}:"))
1565       return {Check::CheckNone, Rest};
1566     return {Ret, Rest};
1567   };
1568 
1569   // Verify that the prefix is followed by directive modifiers or a colon.
1570   if (Rest.consume_front(":"))
1571     return {Check::CheckPlain, Rest};
1572   if (Rest.front() == '{')
1573     return ConsumeModifiers(Check::CheckPlain);
1574 
1575   if (Rest.consume_front("_"))
1576     Misspelled = true;
1577   else if (!Rest.consume_front("-"))
1578     return {Check::CheckNone, StringRef()};
1579 
1580   if (Rest.consume_front("COUNT-")) {
1581     int64_t Count;
1582     if (Rest.consumeInteger(10, Count))
1583       // Error happened in parsing integer.
1584       return {Check::CheckBadCount, Rest};
1585     if (Count <= 0 || Count > INT32_MAX)
1586       return {Check::CheckBadCount, Rest};
1587     if (Rest.front() != ':' && Rest.front() != '{')
1588       return {Check::CheckBadCount, Rest};
1589     return ConsumeModifiers(
1590         Check::FileCheckType(Check::CheckPlain).setCount(Count));
1591   }
1592 
1593   // You can't combine -NOT with another suffix.
1594   if (Rest.starts_with("DAG-NOT:") || Rest.starts_with("NOT-DAG:") ||
1595       Rest.starts_with("NEXT-NOT:") || Rest.starts_with("NOT-NEXT:") ||
1596       Rest.starts_with("SAME-NOT:") || Rest.starts_with("NOT-SAME:") ||
1597       Rest.starts_with("EMPTY-NOT:") || Rest.starts_with("NOT-EMPTY:"))
1598     return {Check::CheckBadNot, Rest};
1599 
1600   if (Rest.consume_front("NEXT"))
1601     return ConsumeModifiers(Check::CheckNext);
1602 
1603   if (Rest.consume_front("SAME"))
1604     return ConsumeModifiers(Check::CheckSame);
1605 
1606   if (Rest.consume_front("NOT"))
1607     return ConsumeModifiers(Check::CheckNot);
1608 
1609   if (Rest.consume_front("DAG"))
1610     return ConsumeModifiers(Check::CheckDAG);
1611 
1612   if (Rest.consume_front("LABEL"))
1613     return ConsumeModifiers(Check::CheckLabel);
1614 
1615   if (Rest.consume_front("EMPTY"))
1616     return ConsumeModifiers(Check::CheckEmpty);
1617 
1618   return {Check::CheckNone, Rest};
1619 }
1620 
1621 static std::pair<Check::FileCheckType, StringRef>
1622 FindCheckType(const FileCheckRequest &Req, StringRef Buffer, StringRef Prefix) {
1623   bool Misspelled = false;
1624   auto Res = FindCheckType(Req, Buffer, Prefix, Misspelled);
1625   if (Res.first != Check::CheckNone && Misspelled)
1626     return {Check::CheckMisspelled, Res.second};
1627   return Res;
1628 }
1629 
1630 // From the given position, find the next character after the word.
1631 static size_t SkipWord(StringRef Str, size_t Loc) {
1632   while (Loc < Str.size() && IsPartOfWord(Str[Loc]))
1633     ++Loc;
1634   return Loc;
1635 }
1636 
1637 static const char *DefaultCheckPrefixes[] = {"CHECK"};
1638 static const char *DefaultCommentPrefixes[] = {"COM", "RUN"};
1639 
1640 static void addDefaultPrefixes(FileCheckRequest &Req) {
1641   if (Req.CheckPrefixes.empty()) {
1642     for (const char *Prefix : DefaultCheckPrefixes)
1643       Req.CheckPrefixes.push_back(Prefix);
1644     Req.IsDefaultCheckPrefix = true;
1645   }
1646   if (Req.CommentPrefixes.empty())
1647     for (const char *Prefix : DefaultCommentPrefixes)
1648       Req.CommentPrefixes.push_back(Prefix);
1649 }
1650 
1651 struct PrefixMatcher {
1652   /// Prefixes and their first occurrence past the current position.
1653   SmallVector<std::pair<StringRef, size_t>> Prefixes;
1654   StringRef Input;
1655 
1656   PrefixMatcher(ArrayRef<StringRef> CheckPrefixes,
1657                 ArrayRef<StringRef> CommentPrefixes, StringRef Input)
1658       : Input(Input) {
1659     for (StringRef Prefix : CheckPrefixes)
1660       Prefixes.push_back({Prefix, Input.find(Prefix)});
1661     for (StringRef Prefix : CommentPrefixes)
1662       Prefixes.push_back({Prefix, Input.find(Prefix)});
1663 
1664     // Sort by descending length.
1665     llvm::sort(Prefixes,
1666                [](auto A, auto B) { return A.first.size() > B.first.size(); });
1667   }
1668 
1669   /// Find the next match of a prefix in Buffer.
1670   /// Returns empty StringRef if not found.
1671   StringRef match(StringRef Buffer) {
1672     assert(Buffer.data() >= Input.data() &&
1673            Buffer.data() + Buffer.size() == Input.data() + Input.size() &&
1674            "Buffer must be suffix of Input");
1675 
1676     size_t From = Buffer.data() - Input.data();
1677     StringRef Match;
1678     for (auto &[Prefix, Pos] : Prefixes) {
1679       // If the last occurrence was before From, find the next one after From.
1680       if (Pos < From)
1681         Pos = Input.find(Prefix, From);
1682       // Find the first prefix with the lowest position.
1683       if (Pos != StringRef::npos &&
1684           (Match.empty() || size_t(Match.data() - Input.data()) > Pos))
1685         Match = StringRef(Input.substr(Pos, Prefix.size()));
1686     }
1687     return Match;
1688   }
1689 };
1690 
1691 /// Searches the buffer for the first prefix in the prefix regular expression.
1692 ///
1693 /// This searches the buffer using the provided regular expression, however it
1694 /// enforces constraints beyond that:
1695 /// 1) The found prefix must not be a suffix of something that looks like
1696 ///    a valid prefix.
1697 /// 2) The found prefix must be followed by a valid check type suffix using \c
1698 ///    FindCheckType above.
1699 ///
1700 /// \returns a pair of StringRefs into the Buffer, which combines:
1701 ///   - the first match of the regular expression to satisfy these two is
1702 ///   returned,
1703 ///     otherwise an empty StringRef is returned to indicate failure.
1704 ///   - buffer rewound to the location right after parsed suffix, for parsing
1705 ///     to continue from
1706 ///
1707 /// If this routine returns a valid prefix, it will also shrink \p Buffer to
1708 /// start at the beginning of the returned prefix, increment \p LineNumber for
1709 /// each new line consumed from \p Buffer, and set \p CheckTy to the type of
1710 /// check found by examining the suffix.
1711 ///
1712 /// If no valid prefix is found, the state of Buffer, LineNumber, and CheckTy
1713 /// is unspecified.
1714 static std::pair<StringRef, StringRef>
1715 FindFirstMatchingPrefix(const FileCheckRequest &Req, PrefixMatcher &Matcher,
1716                         StringRef &Buffer, unsigned &LineNumber,
1717                         Check::FileCheckType &CheckTy) {
1718   while (!Buffer.empty()) {
1719     // Find the first (longest) prefix match.
1720     StringRef Prefix = Matcher.match(Buffer);
1721     if (Prefix.empty())
1722       // No match at all, bail.
1723       return {StringRef(), StringRef()};
1724 
1725     assert(Prefix.data() >= Buffer.data() &&
1726            Prefix.data() < Buffer.data() + Buffer.size() &&
1727            "Prefix doesn't start inside of buffer!");
1728     size_t Loc = Prefix.data() - Buffer.data();
1729     StringRef Skipped = Buffer.substr(0, Loc);
1730     Buffer = Buffer.drop_front(Loc);
1731     LineNumber += Skipped.count('\n');
1732 
1733     // Check that the matched prefix isn't a suffix of some other check-like
1734     // word.
1735     // FIXME: This is a very ad-hoc check. it would be better handled in some
1736     // other way. Among other things it seems hard to distinguish between
1737     // intentional and unintentional uses of this feature.
1738     if (Skipped.empty() || !IsPartOfWord(Skipped.back())) {
1739       // Now extract the type.
1740       StringRef AfterSuffix;
1741       std::tie(CheckTy, AfterSuffix) = FindCheckType(Req, Buffer, Prefix);
1742 
1743       // If we've found a valid check type for this prefix, we're done.
1744       if (CheckTy != Check::CheckNone)
1745         return {Prefix, AfterSuffix};
1746     }
1747 
1748     // If we didn't successfully find a prefix, we need to skip this invalid
1749     // prefix and continue scanning. We directly skip the prefix that was
1750     // matched and any additional parts of that check-like word.
1751     Buffer = Buffer.drop_front(SkipWord(Buffer, Prefix.size()));
1752   }
1753 
1754   // We ran out of buffer while skipping partial matches so give up.
1755   return {StringRef(), StringRef()};
1756 }
1757 
1758 void FileCheckPatternContext::createLineVariable() {
1759   assert(!LineVariable && "@LINE pseudo numeric variable already created");
1760   StringRef LineName = "@LINE";
1761   LineVariable = makeNumericVariable(
1762       LineName, ExpressionFormat(ExpressionFormat::Kind::Unsigned));
1763   GlobalNumericVariableTable[LineName] = LineVariable;
1764 }
1765 
1766 FileCheck::FileCheck(FileCheckRequest Req)
1767     : Req(Req), PatternContext(std::make_unique<FileCheckPatternContext>()),
1768       CheckStrings(std::make_unique<std::vector<FileCheckString>>()) {}
1769 
1770 FileCheck::~FileCheck() = default;
1771 
1772 bool FileCheck::readCheckFile(
1773     SourceMgr &SM, StringRef Buffer,
1774     std::pair<unsigned, unsigned> *ImpPatBufferIDRange) {
1775   if (ImpPatBufferIDRange)
1776     ImpPatBufferIDRange->first = ImpPatBufferIDRange->second = 0;
1777 
1778   Error DefineError =
1779       PatternContext->defineCmdlineVariables(Req.GlobalDefines, SM);
1780   if (DefineError) {
1781     logAllUnhandledErrors(std::move(DefineError), errs());
1782     return true;
1783   }
1784 
1785   PatternContext->createLineVariable();
1786 
1787   std::vector<FileCheckString::DagNotPrefixInfo> ImplicitNegativeChecks;
1788   for (StringRef PatternString : Req.ImplicitCheckNot) {
1789     // Create a buffer with fake command line content in order to display the
1790     // command line option responsible for the specific implicit CHECK-NOT.
1791     std::string Prefix = "-implicit-check-not='";
1792     std::string Suffix = "'";
1793     std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
1794         (Prefix + PatternString + Suffix).str(), "command line");
1795 
1796     StringRef PatternInBuffer =
1797         CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
1798     unsigned BufferID = SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
1799     if (ImpPatBufferIDRange) {
1800       if (ImpPatBufferIDRange->first == ImpPatBufferIDRange->second) {
1801         ImpPatBufferIDRange->first = BufferID;
1802         ImpPatBufferIDRange->second = BufferID + 1;
1803       } else {
1804         assert(BufferID == ImpPatBufferIDRange->second &&
1805                "expected consecutive source buffer IDs");
1806         ++ImpPatBufferIDRange->second;
1807       }
1808     }
1809 
1810     ImplicitNegativeChecks.emplace_back(
1811         Pattern(Check::CheckNot, PatternContext.get()),
1812         StringRef("IMPLICIT-CHECK"));
1813     ImplicitNegativeChecks.back().DagNotPat.parsePattern(
1814         PatternInBuffer, "IMPLICIT-CHECK", SM, Req);
1815   }
1816 
1817   std::vector<FileCheckString::DagNotPrefixInfo> DagNotMatches =
1818       ImplicitNegativeChecks;
1819   // LineNumber keeps track of the line on which CheckPrefix instances are
1820   // found.
1821   unsigned LineNumber = 1;
1822 
1823   addDefaultPrefixes(Req);
1824   PrefixMatcher Matcher(Req.CheckPrefixes, Req.CommentPrefixes, Buffer);
1825   std::set<StringRef> PrefixesNotFound(Req.CheckPrefixes.begin(),
1826                                        Req.CheckPrefixes.end());
1827   const size_t DistinctPrefixes = PrefixesNotFound.size();
1828   while (true) {
1829     Check::FileCheckType CheckTy;
1830 
1831     // See if a prefix occurs in the memory buffer.
1832     StringRef UsedPrefix;
1833     StringRef AfterSuffix;
1834     std::tie(UsedPrefix, AfterSuffix) =
1835         FindFirstMatchingPrefix(Req, Matcher, Buffer, LineNumber, CheckTy);
1836     if (UsedPrefix.empty())
1837       break;
1838     if (CheckTy != Check::CheckComment)
1839       PrefixesNotFound.erase(UsedPrefix);
1840 
1841     assert(UsedPrefix.data() == Buffer.data() &&
1842            "Failed to move Buffer's start forward, or pointed prefix outside "
1843            "of the buffer!");
1844     assert(AfterSuffix.data() >= Buffer.data() &&
1845            AfterSuffix.data() < Buffer.data() + Buffer.size() &&
1846            "Parsing after suffix doesn't start inside of buffer!");
1847 
1848     // Location to use for error messages.
1849     const char *UsedPrefixStart = UsedPrefix.data();
1850 
1851     // Skip the buffer to the end of parsed suffix (or just prefix, if no good
1852     // suffix was processed).
1853     Buffer = AfterSuffix.empty() ? Buffer.drop_front(UsedPrefix.size())
1854                                  : AfterSuffix;
1855 
1856     // Complain about misspelled directives.
1857     if (CheckTy == Check::CheckMisspelled) {
1858       StringRef UsedDirective(UsedPrefix.data(),
1859                               AfterSuffix.data() - UsedPrefix.data());
1860       SM.PrintMessage(SMLoc::getFromPointer(UsedDirective.data()),
1861                       SourceMgr::DK_Error,
1862                       "misspelled directive '" + UsedDirective + "'");
1863       return true;
1864     }
1865 
1866     // Complain about useful-looking but unsupported suffixes.
1867     if (CheckTy == Check::CheckBadNot) {
1868       SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
1869                       "unsupported -NOT combo on prefix '" + UsedPrefix + "'");
1870       return true;
1871     }
1872 
1873     // Complain about invalid count specification.
1874     if (CheckTy == Check::CheckBadCount) {
1875       SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error,
1876                       "invalid count in -COUNT specification on prefix '" +
1877                           UsedPrefix + "'");
1878       return true;
1879     }
1880 
1881     // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
1882     // leading whitespace.
1883     if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
1884       Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
1885 
1886     // Scan ahead to the end of line.
1887     size_t EOL = Buffer.find_first_of("\n\r");
1888 
1889     // Remember the location of the start of the pattern, for diagnostics.
1890     SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
1891 
1892     // Extract the pattern from the buffer.
1893     StringRef PatternBuffer = Buffer.substr(0, EOL);
1894     Buffer = Buffer.substr(EOL);
1895 
1896     // If this is a comment, we're done.
1897     if (CheckTy == Check::CheckComment)
1898       continue;
1899 
1900     // Parse the pattern.
1901     Pattern P(CheckTy, PatternContext.get(), LineNumber);
1902     if (P.parsePattern(PatternBuffer, UsedPrefix, SM, Req))
1903       return true;
1904 
1905     // Verify that CHECK-LABEL lines do not define or use variables
1906     if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
1907       SM.PrintMessage(
1908           SMLoc::getFromPointer(UsedPrefixStart), SourceMgr::DK_Error,
1909           "found '" + UsedPrefix + "-LABEL:'"
1910                                    " with variable definition or use");
1911       return true;
1912     }
1913 
1914     // Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them.
1915     if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame ||
1916          CheckTy == Check::CheckEmpty) &&
1917         CheckStrings->empty()) {
1918       StringRef Type = CheckTy == Check::CheckNext
1919                            ? "NEXT"
1920                            : CheckTy == Check::CheckEmpty ? "EMPTY" : "SAME";
1921       SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
1922                       SourceMgr::DK_Error,
1923                       "found '" + UsedPrefix + "-" + Type +
1924                           "' without previous '" + UsedPrefix + ": line");
1925       return true;
1926     }
1927 
1928     // Handle CHECK-DAG/-NOT.
1929     if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
1930       DagNotMatches.emplace_back(P, UsedPrefix);
1931       continue;
1932     }
1933 
1934     // Okay, add the string we captured to the output vector and move on.
1935     CheckStrings->emplace_back(P, UsedPrefix, PatternLoc);
1936     std::swap(DagNotMatches, CheckStrings->back().DagNotStrings);
1937     DagNotMatches = ImplicitNegativeChecks;
1938   }
1939 
1940   // When there are no used prefixes we report an error except in the case that
1941   // no prefix is specified explicitly but -implicit-check-not is specified.
1942   const bool NoPrefixesFound = PrefixesNotFound.size() == DistinctPrefixes;
1943   const bool SomePrefixesUnexpectedlyNotUsed =
1944       !Req.AllowUnusedPrefixes && !PrefixesNotFound.empty();
1945   if ((NoPrefixesFound || SomePrefixesUnexpectedlyNotUsed) &&
1946       (ImplicitNegativeChecks.empty() || !Req.IsDefaultCheckPrefix)) {
1947     errs() << "error: no check strings found with prefix"
1948            << (PrefixesNotFound.size() > 1 ? "es " : " ");
1949     bool First = true;
1950     for (StringRef MissingPrefix : PrefixesNotFound) {
1951       if (!First)
1952         errs() << ", ";
1953       errs() << "\'" << MissingPrefix << ":'";
1954       First = false;
1955     }
1956     errs() << '\n';
1957     return true;
1958   }
1959 
1960   // Add an EOF pattern for any trailing --implicit-check-not/CHECK-DAG/-NOTs,
1961   // and use the first prefix as a filler for the error message.
1962   if (!DagNotMatches.empty()) {
1963     CheckStrings->emplace_back(
1964         Pattern(Check::CheckEOF, PatternContext.get(), LineNumber + 1),
1965         *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data()));
1966     std::swap(DagNotMatches, CheckStrings->back().DagNotStrings);
1967   }
1968 
1969   return false;
1970 }
1971 
1972 /// Returns either (1) \c ErrorSuccess if there was no error or (2)
1973 /// \c ErrorReported if an error was reported, such as an unexpected match.
1974 static Error printMatch(bool ExpectedMatch, const SourceMgr &SM,
1975                         StringRef Prefix, SMLoc Loc, const Pattern &Pat,
1976                         int MatchedCount, StringRef Buffer,
1977                         Pattern::MatchResult MatchResult,
1978                         const FileCheckRequest &Req,
1979                         std::vector<FileCheckDiag> *Diags) {
1980   // Suppress some verbosity if there's no error.
1981   bool HasError = !ExpectedMatch || MatchResult.TheError;
1982   bool PrintDiag = true;
1983   if (!HasError) {
1984     if (!Req.Verbose)
1985       return ErrorReported::reportedOrSuccess(HasError);
1986     if (!Req.VerboseVerbose && Pat.getCheckTy() == Check::CheckEOF)
1987       return ErrorReported::reportedOrSuccess(HasError);
1988     // Due to their verbosity, we don't print verbose diagnostics here if we're
1989     // gathering them for Diags to be rendered elsewhere, but we always print
1990     // other diagnostics.
1991     PrintDiag = !Diags;
1992   }
1993 
1994   // Add "found" diagnostic, substitutions, and variable definitions to Diags.
1995   FileCheckDiag::MatchType MatchTy = ExpectedMatch
1996                                          ? FileCheckDiag::MatchFoundAndExpected
1997                                          : FileCheckDiag::MatchFoundButExcluded;
1998   SMRange MatchRange = ProcessMatchResult(MatchTy, SM, Loc, Pat.getCheckTy(),
1999                                           Buffer, MatchResult.TheMatch->Pos,
2000                                           MatchResult.TheMatch->Len, Diags);
2001   if (Diags) {
2002     Pat.printSubstitutions(SM, Buffer, MatchRange, MatchTy, Diags);
2003     Pat.printVariableDefs(SM, MatchTy, Diags);
2004   }
2005   if (!PrintDiag) {
2006     assert(!HasError && "expected to report more diagnostics for error");
2007     return ErrorReported::reportedOrSuccess(HasError);
2008   }
2009 
2010   // Print the match.
2011   std::string Message = formatv("{0}: {1} string found in input",
2012                                 Pat.getCheckTy().getDescription(Prefix),
2013                                 (ExpectedMatch ? "expected" : "excluded"))
2014                             .str();
2015   if (Pat.getCount() > 1)
2016     Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
2017   SM.PrintMessage(
2018       Loc, ExpectedMatch ? SourceMgr::DK_Remark : SourceMgr::DK_Error, Message);
2019   SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, "found here",
2020                   {MatchRange});
2021 
2022   // Print additional information, which can be useful even if there are errors.
2023   Pat.printSubstitutions(SM, Buffer, MatchRange, MatchTy, nullptr);
2024   Pat.printVariableDefs(SM, MatchTy, nullptr);
2025 
2026   // Print errors and add them to Diags.  We report these errors after the match
2027   // itself because we found them after the match.  If we had found them before
2028   // the match, we'd be in printNoMatch.
2029   handleAllErrors(std::move(MatchResult.TheError),
2030                   [&](const ErrorDiagnostic &E) {
2031                     E.log(errs());
2032                     if (Diags) {
2033                       Diags->emplace_back(SM, Pat.getCheckTy(), Loc,
2034                                           FileCheckDiag::MatchFoundErrorNote,
2035                                           E.getRange(), E.getMessage().str());
2036                     }
2037                   });
2038   return ErrorReported::reportedOrSuccess(HasError);
2039 }
2040 
2041 /// Returns either (1) \c ErrorSuccess if there was no error, or (2)
2042 /// \c ErrorReported if an error was reported, such as an expected match not
2043 /// found.
2044 static Error printNoMatch(bool ExpectedMatch, const SourceMgr &SM,
2045                           StringRef Prefix, SMLoc Loc, const Pattern &Pat,
2046                           int MatchedCount, StringRef Buffer, Error MatchError,
2047                           bool VerboseVerbose,
2048                           std::vector<FileCheckDiag> *Diags) {
2049   // Print any pattern errors, and record them to be added to Diags later.
2050   bool HasError = ExpectedMatch;
2051   bool HasPatternError = false;
2052   FileCheckDiag::MatchType MatchTy = ExpectedMatch
2053                                          ? FileCheckDiag::MatchNoneButExpected
2054                                          : FileCheckDiag::MatchNoneAndExcluded;
2055   SmallVector<std::string, 4> ErrorMsgs;
2056   handleAllErrors(
2057       std::move(MatchError),
2058       [&](const ErrorDiagnostic &E) {
2059         HasError = HasPatternError = true;
2060         MatchTy = FileCheckDiag::MatchNoneForInvalidPattern;
2061         E.log(errs());
2062         if (Diags)
2063           ErrorMsgs.push_back(E.getMessage().str());
2064       },
2065       // NotFoundError is why printNoMatch was invoked.
2066       [](const NotFoundError &E) {});
2067 
2068   // Suppress some verbosity if there's no error.
2069   bool PrintDiag = true;
2070   if (!HasError) {
2071     if (!VerboseVerbose)
2072       return ErrorReported::reportedOrSuccess(HasError);
2073     // Due to their verbosity, we don't print verbose diagnostics here if we're
2074     // gathering them for Diags to be rendered elsewhere, but we always print
2075     // other diagnostics.
2076     PrintDiag = !Diags;
2077   }
2078 
2079   // Add "not found" diagnostic, substitutions, and pattern errors to Diags.
2080   //
2081   // We handle Diags a little differently than the errors we print directly:
2082   // we add the "not found" diagnostic to Diags even if there are pattern
2083   // errors.  The reason is that we need to attach pattern errors as notes
2084   // somewhere in the input, and the input search range from the "not found"
2085   // diagnostic is all we have to anchor them.
2086   SMRange SearchRange = ProcessMatchResult(MatchTy, SM, Loc, Pat.getCheckTy(),
2087                                            Buffer, 0, Buffer.size(), Diags);
2088   if (Diags) {
2089     SMRange NoteRange = SMRange(SearchRange.Start, SearchRange.Start);
2090     for (StringRef ErrorMsg : ErrorMsgs)
2091       Diags->emplace_back(SM, Pat.getCheckTy(), Loc, MatchTy, NoteRange,
2092                           ErrorMsg);
2093     Pat.printSubstitutions(SM, Buffer, SearchRange, MatchTy, Diags);
2094   }
2095   if (!PrintDiag) {
2096     assert(!HasError && "expected to report more diagnostics for error");
2097     return ErrorReported::reportedOrSuccess(HasError);
2098   }
2099 
2100   // Print "not found" diagnostic, except that's implied if we already printed a
2101   // pattern error.
2102   if (!HasPatternError) {
2103     std::string Message = formatv("{0}: {1} string not found in input",
2104                                   Pat.getCheckTy().getDescription(Prefix),
2105                                   (ExpectedMatch ? "expected" : "excluded"))
2106                               .str();
2107     if (Pat.getCount() > 1)
2108       Message +=
2109           formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
2110     SM.PrintMessage(Loc,
2111                     ExpectedMatch ? SourceMgr::DK_Error : SourceMgr::DK_Remark,
2112                     Message);
2113     SM.PrintMessage(SearchRange.Start, SourceMgr::DK_Note,
2114                     "scanning from here");
2115   }
2116 
2117   // Print additional information, which can be useful even after a pattern
2118   // error.
2119   Pat.printSubstitutions(SM, Buffer, SearchRange, MatchTy, nullptr);
2120   if (ExpectedMatch)
2121     Pat.printFuzzyMatch(SM, Buffer, Diags);
2122   return ErrorReported::reportedOrSuccess(HasError);
2123 }
2124 
2125 /// Returns either (1) \c ErrorSuccess if there was no error, or (2)
2126 /// \c ErrorReported if an error was reported.
2127 static Error reportMatchResult(bool ExpectedMatch, const SourceMgr &SM,
2128                                StringRef Prefix, SMLoc Loc, const Pattern &Pat,
2129                                int MatchedCount, StringRef Buffer,
2130                                Pattern::MatchResult MatchResult,
2131                                const FileCheckRequest &Req,
2132                                std::vector<FileCheckDiag> *Diags) {
2133   if (MatchResult.TheMatch)
2134     return printMatch(ExpectedMatch, SM, Prefix, Loc, Pat, MatchedCount, Buffer,
2135                       std::move(MatchResult), Req, Diags);
2136   return printNoMatch(ExpectedMatch, SM, Prefix, Loc, Pat, MatchedCount, Buffer,
2137                       std::move(MatchResult.TheError), Req.VerboseVerbose,
2138                       Diags);
2139 }
2140 
2141 /// Counts the number of newlines in the specified range.
2142 static unsigned CountNumNewlinesBetween(StringRef Range,
2143                                         const char *&FirstNewLine) {
2144   unsigned NumNewLines = 0;
2145   while (true) {
2146     // Scan for newline.
2147     Range = Range.substr(Range.find_first_of("\n\r"));
2148     if (Range.empty())
2149       return NumNewLines;
2150 
2151     ++NumNewLines;
2152 
2153     // Handle \n\r and \r\n as a single newline.
2154     if (Range.size() > 1 && (Range[1] == '\n' || Range[1] == '\r') &&
2155         (Range[0] != Range[1]))
2156       Range = Range.substr(1);
2157     Range = Range.substr(1);
2158 
2159     if (NumNewLines == 1)
2160       FirstNewLine = Range.begin();
2161   }
2162 }
2163 
2164 size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer,
2165                               bool IsLabelScanMode, size_t &MatchLen,
2166                               FileCheckRequest &Req,
2167                               std::vector<FileCheckDiag> *Diags) const {
2168   size_t LastPos = 0;
2169   std::vector<const DagNotPrefixInfo *> NotStrings;
2170 
2171   // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
2172   // bounds; we have not processed variable definitions within the bounded block
2173   // yet so cannot handle any final CHECK-DAG yet; this is handled when going
2174   // over the block again (including the last CHECK-LABEL) in normal mode.
2175   if (!IsLabelScanMode) {
2176     // Match "dag strings" (with mixed "not strings" if any).
2177     LastPos = CheckDag(SM, Buffer, NotStrings, Req, Diags);
2178     if (LastPos == StringRef::npos)
2179       return StringRef::npos;
2180   }
2181 
2182   // Match itself from the last position after matching CHECK-DAG.
2183   size_t LastMatchEnd = LastPos;
2184   size_t FirstMatchPos = 0;
2185   // Go match the pattern Count times. Majority of patterns only match with
2186   // count 1 though.
2187   assert(Pat.getCount() != 0 && "pattern count can not be zero");
2188   for (int i = 1; i <= Pat.getCount(); i++) {
2189     StringRef MatchBuffer = Buffer.substr(LastMatchEnd);
2190     // get a match at current start point
2191     Pattern::MatchResult MatchResult = Pat.match(MatchBuffer, SM);
2192 
2193     // report
2194     if (Error Err = reportMatchResult(/*ExpectedMatch=*/true, SM, Prefix, Loc,
2195                                       Pat, i, MatchBuffer,
2196                                       std::move(MatchResult), Req, Diags)) {
2197       cantFail(handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
2198       return StringRef::npos;
2199     }
2200 
2201     size_t MatchPos = MatchResult.TheMatch->Pos;
2202     if (i == 1)
2203       FirstMatchPos = LastPos + MatchPos;
2204 
2205     // move start point after the match
2206     LastMatchEnd += MatchPos + MatchResult.TheMatch->Len;
2207   }
2208   // Full match len counts from first match pos.
2209   MatchLen = LastMatchEnd - FirstMatchPos;
2210 
2211   // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
2212   // or CHECK-NOT
2213   if (!IsLabelScanMode) {
2214     size_t MatchPos = FirstMatchPos - LastPos;
2215     StringRef MatchBuffer = Buffer.substr(LastPos);
2216     StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
2217 
2218     // If this check is a "CHECK-NEXT", verify that the previous match was on
2219     // the previous line (i.e. that there is one newline between them).
2220     if (CheckNext(SM, SkippedRegion)) {
2221       ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
2222                          Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
2223                          Diags, Req.Verbose);
2224       return StringRef::npos;
2225     }
2226 
2227     // If this check is a "CHECK-SAME", verify that the previous match was on
2228     // the same line (i.e. that there is no newline between them).
2229     if (CheckSame(SM, SkippedRegion)) {
2230       ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc,
2231                          Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
2232                          Diags, Req.Verbose);
2233       return StringRef::npos;
2234     }
2235 
2236     // If this match had "not strings", verify that they don't exist in the
2237     // skipped region.
2238     if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
2239       return StringRef::npos;
2240   }
2241 
2242   return FirstMatchPos;
2243 }
2244 
2245 bool FileCheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const {
2246   if (Pat.getCheckTy() != Check::CheckNext &&
2247       Pat.getCheckTy() != Check::CheckEmpty)
2248     return false;
2249 
2250   Twine CheckName =
2251       Prefix +
2252       Twine(Pat.getCheckTy() == Check::CheckEmpty ? "-EMPTY" : "-NEXT");
2253 
2254   // Count the number of newlines between the previous match and this one.
2255   const char *FirstNewLine = nullptr;
2256   unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
2257 
2258   if (NumNewLines == 0) {
2259     SM.PrintMessage(Loc, SourceMgr::DK_Error,
2260                     CheckName + ": is on the same line as previous match");
2261     SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
2262                     "'next' match was here");
2263     SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
2264                     "previous match ended here");
2265     return true;
2266   }
2267 
2268   if (NumNewLines != 1) {
2269     SM.PrintMessage(Loc, SourceMgr::DK_Error,
2270                     CheckName +
2271                         ": is not on the line after the previous match");
2272     SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
2273                     "'next' match was here");
2274     SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
2275                     "previous match ended here");
2276     SM.PrintMessage(SMLoc::getFromPointer(FirstNewLine), SourceMgr::DK_Note,
2277                     "non-matching line after previous match is here");
2278     return true;
2279   }
2280 
2281   return false;
2282 }
2283 
2284 bool FileCheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const {
2285   if (Pat.getCheckTy() != Check::CheckSame)
2286     return false;
2287 
2288   // Count the number of newlines between the previous match and this one.
2289   const char *FirstNewLine = nullptr;
2290   unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
2291 
2292   if (NumNewLines != 0) {
2293     SM.PrintMessage(Loc, SourceMgr::DK_Error,
2294                     Prefix +
2295                         "-SAME: is not on the same line as the previous match");
2296     SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note,
2297                     "'next' match was here");
2298     SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note,
2299                     "previous match ended here");
2300     return true;
2301   }
2302 
2303   return false;
2304 }
2305 
2306 bool FileCheckString::CheckNot(
2307     const SourceMgr &SM, StringRef Buffer,
2308     const std::vector<const DagNotPrefixInfo *> &NotStrings,
2309     const FileCheckRequest &Req, std::vector<FileCheckDiag> *Diags) const {
2310   bool DirectiveFail = false;
2311   for (auto NotInfo : NotStrings) {
2312     assert((NotInfo->DagNotPat.getCheckTy() == Check::CheckNot) &&
2313            "Expect CHECK-NOT!");
2314     Pattern::MatchResult MatchResult = NotInfo->DagNotPat.match(Buffer, SM);
2315     if (Error Err = reportMatchResult(
2316             /*ExpectedMatch=*/false, SM, NotInfo->DagNotPrefix,
2317             NotInfo->DagNotPat.getLoc(), NotInfo->DagNotPat, 1, Buffer,
2318             std::move(MatchResult), Req, Diags)) {
2319       cantFail(handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
2320       DirectiveFail = true;
2321       continue;
2322     }
2323   }
2324   return DirectiveFail;
2325 }
2326 
2327 size_t
2328 FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer,
2329                           std::vector<const DagNotPrefixInfo *> &NotStrings,
2330                           const FileCheckRequest &Req,
2331                           std::vector<FileCheckDiag> *Diags) const {
2332   if (DagNotStrings.empty())
2333     return 0;
2334 
2335   // The start of the search range.
2336   size_t StartPos = 0;
2337 
2338   struct MatchRange {
2339     size_t Pos;
2340     size_t End;
2341   };
2342   // A sorted list of ranges for non-overlapping CHECK-DAG matches.  Match
2343   // ranges are erased from this list once they are no longer in the search
2344   // range.
2345   std::list<MatchRange> MatchRanges;
2346 
2347   // We need PatItr and PatEnd later for detecting the end of a CHECK-DAG
2348   // group, so we don't use a range-based for loop here.
2349   for (auto PatItr = DagNotStrings.begin(), PatEnd = DagNotStrings.end();
2350        PatItr != PatEnd; ++PatItr) {
2351     const Pattern &Pat = PatItr->DagNotPat;
2352     const StringRef DNPrefix = PatItr->DagNotPrefix;
2353     assert((Pat.getCheckTy() == Check::CheckDAG ||
2354             Pat.getCheckTy() == Check::CheckNot) &&
2355            "Invalid CHECK-DAG or CHECK-NOT!");
2356 
2357     if (Pat.getCheckTy() == Check::CheckNot) {
2358       NotStrings.push_back(&*PatItr);
2359       continue;
2360     }
2361 
2362     assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!");
2363 
2364     // CHECK-DAG always matches from the start.
2365     size_t MatchLen = 0, MatchPos = StartPos;
2366 
2367     // Search for a match that doesn't overlap a previous match in this
2368     // CHECK-DAG group.
2369     for (auto MI = MatchRanges.begin(), ME = MatchRanges.end(); true; ++MI) {
2370       StringRef MatchBuffer = Buffer.substr(MatchPos);
2371       Pattern::MatchResult MatchResult = Pat.match(MatchBuffer, SM);
2372       // With a group of CHECK-DAGs, a single mismatching means the match on
2373       // that group of CHECK-DAGs fails immediately.
2374       if (MatchResult.TheError || Req.VerboseVerbose) {
2375         if (Error Err = reportMatchResult(/*ExpectedMatch=*/true, SM, DNPrefix,
2376                                           Pat.getLoc(), Pat, 1, MatchBuffer,
2377                                           std::move(MatchResult), Req, Diags)) {
2378           cantFail(
2379               handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
2380           return StringRef::npos;
2381         }
2382       }
2383       MatchLen = MatchResult.TheMatch->Len;
2384       // Re-calc it as the offset relative to the start of the original
2385       // string.
2386       MatchPos += MatchResult.TheMatch->Pos;
2387       MatchRange M{MatchPos, MatchPos + MatchLen};
2388       if (Req.AllowDeprecatedDagOverlap) {
2389         // We don't need to track all matches in this mode, so we just maintain
2390         // one match range that encompasses the current CHECK-DAG group's
2391         // matches.
2392         if (MatchRanges.empty())
2393           MatchRanges.insert(MatchRanges.end(), M);
2394         else {
2395           auto Block = MatchRanges.begin();
2396           Block->Pos = std::min(Block->Pos, M.Pos);
2397           Block->End = std::max(Block->End, M.End);
2398         }
2399         break;
2400       }
2401       // Iterate previous matches until overlapping match or insertion point.
2402       bool Overlap = false;
2403       for (; MI != ME; ++MI) {
2404         if (M.Pos < MI->End) {
2405           // !Overlap => New match has no overlap and is before this old match.
2406           // Overlap => New match overlaps this old match.
2407           Overlap = MI->Pos < M.End;
2408           break;
2409         }
2410       }
2411       if (!Overlap) {
2412         // Insert non-overlapping match into list.
2413         MatchRanges.insert(MI, M);
2414         break;
2415       }
2416       if (Req.VerboseVerbose) {
2417         // Due to their verbosity, we don't print verbose diagnostics here if
2418         // we're gathering them for a different rendering, but we always print
2419         // other diagnostics.
2420         if (!Diags) {
2421           SMLoc OldStart = SMLoc::getFromPointer(Buffer.data() + MI->Pos);
2422           SMLoc OldEnd = SMLoc::getFromPointer(Buffer.data() + MI->End);
2423           SMRange OldRange(OldStart, OldEnd);
2424           SM.PrintMessage(OldStart, SourceMgr::DK_Note,
2425                           "match discarded, overlaps earlier DAG match here",
2426                           {OldRange});
2427         } else {
2428           SMLoc CheckLoc = Diags->rbegin()->CheckLoc;
2429           for (auto I = Diags->rbegin(), E = Diags->rend();
2430                I != E && I->CheckLoc == CheckLoc; ++I)
2431             I->MatchTy = FileCheckDiag::MatchFoundButDiscarded;
2432         }
2433       }
2434       MatchPos = MI->End;
2435     }
2436     if (!Req.VerboseVerbose)
2437       cantFail(printMatch(
2438           /*ExpectedMatch=*/true, SM, DNPrefix, Pat.getLoc(), Pat, 1, Buffer,
2439           Pattern::MatchResult(MatchPos, MatchLen, Error::success()), Req,
2440           Diags));
2441 
2442     // Handle the end of a CHECK-DAG group.
2443     if (std::next(PatItr) == PatEnd ||
2444         std::next(PatItr)->DagNotPat.getCheckTy() == Check::CheckNot) {
2445       if (!NotStrings.empty()) {
2446         // If there are CHECK-NOTs between two CHECK-DAGs or from CHECK to
2447         // CHECK-DAG, verify that there are no 'not' strings occurred in that
2448         // region.
2449         StringRef SkippedRegion =
2450             Buffer.slice(StartPos, MatchRanges.begin()->Pos);
2451         if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
2452           return StringRef::npos;
2453         // Clear "not strings".
2454         NotStrings.clear();
2455       }
2456       // All subsequent CHECK-DAGs and CHECK-NOTs should be matched from the
2457       // end of this CHECK-DAG group's match range.
2458       StartPos = MatchRanges.rbegin()->End;
2459       // Don't waste time checking for (impossible) overlaps before that.
2460       MatchRanges.clear();
2461     }
2462   }
2463 
2464   return StartPos;
2465 }
2466 
2467 static bool ValidatePrefixes(StringRef Kind, StringSet<> &UniquePrefixes,
2468                              ArrayRef<StringRef> SuppliedPrefixes) {
2469   for (StringRef Prefix : SuppliedPrefixes) {
2470     if (Prefix.empty()) {
2471       errs() << "error: supplied " << Kind << " prefix must not be the empty "
2472              << "string\n";
2473       return false;
2474     }
2475     static const Regex Validator("^[a-zA-Z0-9_-]*$");
2476     if (!Validator.match(Prefix)) {
2477       errs() << "error: supplied " << Kind << " prefix must start with a "
2478              << "letter and contain only alphanumeric characters, hyphens, and "
2479              << "underscores: '" << Prefix << "'\n";
2480       return false;
2481     }
2482     if (!UniquePrefixes.insert(Prefix).second) {
2483       errs() << "error: supplied " << Kind << " prefix must be unique among "
2484              << "check and comment prefixes: '" << Prefix << "'\n";
2485       return false;
2486     }
2487   }
2488   return true;
2489 }
2490 
2491 bool FileCheck::ValidateCheckPrefixes() {
2492   StringSet<> UniquePrefixes;
2493   // Add default prefixes to catch user-supplied duplicates of them below.
2494   if (Req.CheckPrefixes.empty()) {
2495     for (const char *Prefix : DefaultCheckPrefixes)
2496       UniquePrefixes.insert(Prefix);
2497   }
2498   if (Req.CommentPrefixes.empty()) {
2499     for (const char *Prefix : DefaultCommentPrefixes)
2500       UniquePrefixes.insert(Prefix);
2501   }
2502   // Do not validate the default prefixes, or diagnostics about duplicates might
2503   // incorrectly indicate that they were supplied by the user.
2504   if (!ValidatePrefixes("check", UniquePrefixes, Req.CheckPrefixes))
2505     return false;
2506   if (!ValidatePrefixes("comment", UniquePrefixes, Req.CommentPrefixes))
2507     return false;
2508   return true;
2509 }
2510 
2511 Error FileCheckPatternContext::defineCmdlineVariables(
2512     ArrayRef<StringRef> CmdlineDefines, SourceMgr &SM) {
2513   assert(GlobalVariableTable.empty() && GlobalNumericVariableTable.empty() &&
2514          "Overriding defined variable with command-line variable definitions");
2515 
2516   if (CmdlineDefines.empty())
2517     return Error::success();
2518 
2519   // Create a string representing the vector of command-line definitions. Each
2520   // definition is on its own line and prefixed with a definition number to
2521   // clarify which definition a given diagnostic corresponds to.
2522   unsigned I = 0;
2523   Error Errs = Error::success();
2524   std::string CmdlineDefsDiag;
2525   SmallVector<std::pair<size_t, size_t>, 4> CmdlineDefsIndices;
2526   for (StringRef CmdlineDef : CmdlineDefines) {
2527     std::string DefPrefix = ("Global define #" + Twine(++I) + ": ").str();
2528     size_t EqIdx = CmdlineDef.find('=');
2529     if (EqIdx == StringRef::npos) {
2530       CmdlineDefsIndices.push_back(std::make_pair(CmdlineDefsDiag.size(), 0));
2531       continue;
2532     }
2533     // Numeric variable definition.
2534     if (CmdlineDef[0] == '#') {
2535       // Append a copy of the command-line definition adapted to use the same
2536       // format as in the input file to be able to reuse
2537       // parseNumericSubstitutionBlock.
2538       CmdlineDefsDiag += (DefPrefix + CmdlineDef + " (parsed as: [[").str();
2539       std::string SubstitutionStr = std::string(CmdlineDef);
2540       SubstitutionStr[EqIdx] = ':';
2541       CmdlineDefsIndices.push_back(
2542           std::make_pair(CmdlineDefsDiag.size(), SubstitutionStr.size()));
2543       CmdlineDefsDiag += (SubstitutionStr + Twine("]])\n")).str();
2544     } else {
2545       CmdlineDefsDiag += DefPrefix;
2546       CmdlineDefsIndices.push_back(
2547           std::make_pair(CmdlineDefsDiag.size(), CmdlineDef.size()));
2548       CmdlineDefsDiag += (CmdlineDef + "\n").str();
2549     }
2550   }
2551 
2552   // Create a buffer with fake command line content in order to display
2553   // parsing diagnostic with location information and point to the
2554   // global definition with invalid syntax.
2555   std::unique_ptr<MemoryBuffer> CmdLineDefsDiagBuffer =
2556       MemoryBuffer::getMemBufferCopy(CmdlineDefsDiag, "Global defines");
2557   StringRef CmdlineDefsDiagRef = CmdLineDefsDiagBuffer->getBuffer();
2558   SM.AddNewSourceBuffer(std::move(CmdLineDefsDiagBuffer), SMLoc());
2559 
2560   for (std::pair<size_t, size_t> CmdlineDefIndices : CmdlineDefsIndices) {
2561     StringRef CmdlineDef = CmdlineDefsDiagRef.substr(CmdlineDefIndices.first,
2562                                                      CmdlineDefIndices.second);
2563     if (CmdlineDef.empty()) {
2564       Errs = joinErrors(
2565           std::move(Errs),
2566           ErrorDiagnostic::get(SM, CmdlineDef,
2567                                "missing equal sign in global definition"));
2568       continue;
2569     }
2570 
2571     // Numeric variable definition.
2572     if (CmdlineDef[0] == '#') {
2573       // Now parse the definition both to check that the syntax is correct and
2574       // to create the necessary class instance.
2575       StringRef CmdlineDefExpr = CmdlineDef.substr(1);
2576       std::optional<NumericVariable *> DefinedNumericVariable;
2577       Expected<std::unique_ptr<Expression>> ExpressionResult =
2578           Pattern::parseNumericSubstitutionBlock(CmdlineDefExpr,
2579                                                  DefinedNumericVariable, false,
2580                                                  std::nullopt, this, SM);
2581       if (!ExpressionResult) {
2582         Errs = joinErrors(std::move(Errs), ExpressionResult.takeError());
2583         continue;
2584       }
2585       std::unique_ptr<Expression> Expression = std::move(*ExpressionResult);
2586       // Now evaluate the expression whose value this variable should be set
2587       // to, since the expression of a command-line variable definition should
2588       // only use variables defined earlier on the command-line. If not, this
2589       // is an error and we report it.
2590       Expected<APInt> Value = Expression->getAST()->eval();
2591       if (!Value) {
2592         Errs = joinErrors(std::move(Errs), Value.takeError());
2593         continue;
2594       }
2595 
2596       assert(DefinedNumericVariable && "No variable defined");
2597       (*DefinedNumericVariable)->setValue(*Value);
2598 
2599       // Record this variable definition.
2600       GlobalNumericVariableTable[(*DefinedNumericVariable)->getName()] =
2601           *DefinedNumericVariable;
2602     } else {
2603       // String variable definition.
2604       std::pair<StringRef, StringRef> CmdlineNameVal = CmdlineDef.split('=');
2605       StringRef CmdlineName = CmdlineNameVal.first;
2606       StringRef OrigCmdlineName = CmdlineName;
2607       Expected<Pattern::VariableProperties> ParseVarResult =
2608           Pattern::parseVariable(CmdlineName, SM);
2609       if (!ParseVarResult) {
2610         Errs = joinErrors(std::move(Errs), ParseVarResult.takeError());
2611         continue;
2612       }
2613       // Check that CmdlineName does not denote a pseudo variable is only
2614       // composed of the parsed numeric variable. This catches cases like
2615       // "FOO+2" in a "FOO+2=10" definition.
2616       if (ParseVarResult->IsPseudo || !CmdlineName.empty()) {
2617         Errs = joinErrors(std::move(Errs),
2618                           ErrorDiagnostic::get(
2619                               SM, OrigCmdlineName,
2620                               "invalid name in string variable definition '" +
2621                                   OrigCmdlineName + "'"));
2622         continue;
2623       }
2624       StringRef Name = ParseVarResult->Name;
2625 
2626       // Detect collisions between string and numeric variables when the former
2627       // is created later than the latter.
2628       if (GlobalNumericVariableTable.contains(Name)) {
2629         Errs = joinErrors(std::move(Errs),
2630                           ErrorDiagnostic::get(SM, Name,
2631                                                "numeric variable with name '" +
2632                                                    Name + "' already exists"));
2633         continue;
2634       }
2635       GlobalVariableTable.insert(CmdlineNameVal);
2636       // Mark the string variable as defined to detect collisions between
2637       // string and numeric variables in defineCmdlineVariables when the latter
2638       // is created later than the former. We cannot reuse GlobalVariableTable
2639       // for this by populating it with an empty string since we would then
2640       // lose the ability to detect the use of an undefined variable in
2641       // match().
2642       DefinedVariableTable[Name] = true;
2643     }
2644   }
2645 
2646   return Errs;
2647 }
2648 
2649 void FileCheckPatternContext::clearLocalVars() {
2650   SmallVector<StringRef, 16> LocalPatternVars, LocalNumericVars;
2651   for (const StringMapEntry<StringRef> &Var : GlobalVariableTable)
2652     if (Var.first()[0] != '$')
2653       LocalPatternVars.push_back(Var.first());
2654 
2655   // Numeric substitution reads the value of a variable directly, not via
2656   // GlobalNumericVariableTable. Therefore, we clear local variables by
2657   // clearing their value which will lead to a numeric substitution failure. We
2658   // also mark the variable for removal from GlobalNumericVariableTable since
2659   // this is what defineCmdlineVariables checks to decide that no global
2660   // variable has been defined.
2661   for (const auto &Var : GlobalNumericVariableTable)
2662     if (Var.first()[0] != '$') {
2663       Var.getValue()->clearValue();
2664       LocalNumericVars.push_back(Var.first());
2665     }
2666 
2667   for (const auto &Var : LocalPatternVars)
2668     GlobalVariableTable.erase(Var);
2669   for (const auto &Var : LocalNumericVars)
2670     GlobalNumericVariableTable.erase(Var);
2671 }
2672 
2673 bool FileCheck::checkInput(SourceMgr &SM, StringRef Buffer,
2674                            std::vector<FileCheckDiag> *Diags) {
2675   bool ChecksFailed = false;
2676 
2677   unsigned i = 0, j = 0, e = CheckStrings->size();
2678   while (true) {
2679     StringRef CheckRegion;
2680     if (j == e) {
2681       CheckRegion = Buffer;
2682     } else {
2683       const FileCheckString &CheckLabelStr = (*CheckStrings)[j];
2684       if (CheckLabelStr.Pat.getCheckTy() != Check::CheckLabel) {
2685         ++j;
2686         continue;
2687       }
2688 
2689       // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
2690       size_t MatchLabelLen = 0;
2691       size_t MatchLabelPos =
2692           CheckLabelStr.Check(SM, Buffer, true, MatchLabelLen, Req, Diags);
2693       if (MatchLabelPos == StringRef::npos)
2694         // Immediately bail if CHECK-LABEL fails, nothing else we can do.
2695         return false;
2696 
2697       CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen);
2698       Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen);
2699       ++j;
2700     }
2701 
2702     // Do not clear the first region as it's the one before the first
2703     // CHECK-LABEL and it would clear variables defined on the command-line
2704     // before they get used.
2705     if (i != 0 && Req.EnableVarScope)
2706       PatternContext->clearLocalVars();
2707 
2708     for (; i != j; ++i) {
2709       const FileCheckString &CheckStr = (*CheckStrings)[i];
2710 
2711       // Check each string within the scanned region, including a second check
2712       // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
2713       size_t MatchLen = 0;
2714       size_t MatchPos =
2715           CheckStr.Check(SM, CheckRegion, false, MatchLen, Req, Diags);
2716 
2717       if (MatchPos == StringRef::npos) {
2718         ChecksFailed = true;
2719         i = j;
2720         break;
2721       }
2722 
2723       CheckRegion = CheckRegion.substr(MatchPos + MatchLen);
2724     }
2725 
2726     if (j == e)
2727       break;
2728   }
2729 
2730   // Success if no checks failed.
2731   return !ChecksFailed;
2732 }
2733