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