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