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