LLVM 20.0.0git
FileCheck.cpp
Go to the documentation of this file.
1//===- FileCheck.cpp - Check that File's Contents match what is expected --===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// FileCheck does a line-by line check of a file that validates whether it
10// contains the expected content. This is useful for regression tests etc.
11//
12// This file implements most of the API that will be used by the FileCheck utility
13// as well as various unittests.
14//===----------------------------------------------------------------------===//
15
17#include "FileCheckImpl.h"
18#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/StringSet.h"
21#include "llvm/ADT/Twine.h"
24#include <cstdint>
25#include <list>
26#include <set>
27#include <tuple>
28#include <utility>
29
30using namespace llvm;
31
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
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
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
123static unsigned nextAPIntBitWidth(unsigned BitWidth) {
125 : BitWidth * 2;
126}
127
128static 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
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
157 const APInt &RightOperand, bool &Overflow) {
158 return LeftOperand.sadd_ov(RightOperand, Overflow);
159}
160
162 const APInt &RightOperand, bool &Overflow) {
163 return LeftOperand.ssub_ov(RightOperand, Overflow);
164}
165
167 const APInt &RightOperand, bool &Overflow) {
168 return LeftOperand.smul_ov(RightOperand, Overflow);
169}
170
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
181 const APInt &RightOperand, bool &Overflow) {
182 Overflow = false;
183 return LeftOperand.slt(RightOperand) ? RightOperand : LeftOperand;
184}
185
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
196 std::optional<APInt> Value = Variable->getValue();
197 if (Value)
198 return *Value;
199
200 return make_error<UndefVarError>(getExpressionStr());
201}
202
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
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)
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
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
279 // Look up the value and escape it so that we can put it into the regex.
281 if (!VarVal)
282 return VarVal.takeError();
283 return Regex::escape(*VarVal);
284}
285
286bool Pattern::isValidVarNameStart(char C) { return C == '_' || isAlpha(C); }
287
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.slice(I, StringRef::npos),
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.
321constexpr StringLiteral SpaceChars = " \t";
322
323// Parsing helper function that strips the first character in S and returns it.
324static char popFront(StringRef &S) {
325 char C = S.front();
326 S = S.drop_front();
327 return C;
328}
329
330char OverflowError::ID = 0;
331char UndefVarError::ID = 0;
332char ErrorDiagnostic::ID = 0;
333char NotFoundError::ID = 0;
334char ErrorReported::ID = 0;
335
336Expected<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)
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))
353 SM, Name, "string variable with name '" + Name + "' already exists");
354
355 Expr = Expr.ltrim(SpaceChars);
356 if (!Expr.empty())
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)
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
374Expected<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")
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);
391 if (VarTableIter != Context->GlobalNumericVariableTable.end())
392 NumericVariable = VarTableIter->second;
393 else {
394 NumericVariable = Context->makeNumericVariable(
396 Context->GlobalNumericVariableTable[Name] = NumericVariable;
397 }
398
399 std::optional<size_t> DefLineNumber = NumericVariable->getDefLineNumber();
400 if (DefLineNumber && LineNumber && *DefLineNumber == *LineNumber)
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
409Expected<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)
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.
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.
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 }
457 SM, SaveExpr,
458 Twine("invalid ") +
459 (MaybeInvalidConstraint ? "matching constraint or " : "") +
460 "operand format");
461}
462
464Pattern::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
497Pattern::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:
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;
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
542Pattern::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)
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.
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
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("%"))
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 =
658 break;
659 case 'd':
660 ExplicitFormat =
662 break;
663 case 'x':
665 Precision, AlternateForm);
666 break;
667 case 'X':
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)
680 SM, AlternateFormFlagLoc,
681 "alternate form only supported for hex values");
682
683 FormatExpr = FormatExpr.ltrim(SpaceChars);
684 if (!FormatExpr.empty())
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)
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;
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())
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.
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)
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
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
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 += '^';
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()),
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()),
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;
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(":"))) {
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(
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()) {
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) {
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) {
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.
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
1057bool Pattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) {
1058 Regex R(RS);
1059 std::string Error;
1060 if (!R.isValid(Error)) {
1062 "invalid regex: " + Error);
1063 return true;
1064 }
1065
1066 RegExStr += RS.str();
1067 CurParen += R.getNumMatches();
1068 return false;
1069}
1070
1071void 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
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).
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),
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
1188unsigned 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
1206 SMRange Range,
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;
1214
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
1241 std::vector<FileCheckDiag> *Diags) const {
1242 if (VariableDefs.empty() && NumericVariableDefs.empty())
1243 return;
1244 // Build list of variable captures.
1245 struct VarCapture {
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;
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
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
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 =
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
1361 auto VarIter = GlobalVariableTable.find(VarName);
1362 if (VarIter == GlobalVariableTable.end())
1363 return make_error<UndefVarError>(VarName);
1364
1365 return VarIter->second;
1366}
1367
1368template <class... Types>
1369NumericVariable *FileCheckPatternContext::makeNumericVariable(Types... args) {
1370 NumericVariables.push_back(std::make_unique<NumericVariable>(args...));
1371 return NumericVariables.back().get();
1372}
1373
1375FileCheckPatternContext::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
1382Substitution *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
1390size_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()),
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
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
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
1470static bool IsPartOfWord(char c) {
1471 return (isAlnum(c) || c == '-' || c == '_');
1472}
1473
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
1483 if (Modifiers.none())
1484 return "";
1485 std::string Ret;
1487 OS << '{';
1488 if (isLiteralMatch())
1489 OS << "LITERAL";
1490 OS << '}';
1491 return Ret;
1492}
1493
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";
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");
1522 return std::string(Prefix);
1523 case Check::CheckEOF:
1524 return "implicit EOF";
1525 case Check::CheckBadNot:
1526 return "bad NOT";
1528 return "bad COUNT";
1529 }
1530 llvm_unreachable("unknown FileCheckType");
1531}
1532
1533static std::pair<Check::FileCheckType, StringRef>
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
1623static std::pair<Check::FileCheckType, StringRef>
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.
1633static size_t SkipWord(StringRef Str, size_t Loc) {
1634 while (Loc < Str.size() && IsPartOfWord(Str[Loc]))
1635 ++Loc;
1636 return Loc;
1637}
1638
1639static const char *DefaultCheckPrefixes[] = {"CHECK"};
1640static const char *DefaultCommentPrefixes[] = {"COM", "RUN"};
1641
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
1654 /// Prefixes and their first occurrence past the current position.
1657
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.
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();
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.
1716static std::pair<StringRef, StringRef>
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
1761 assert(!LineVariable && "@LINE pseudo numeric variable already created");
1762 StringRef LineName = "@LINE";
1763 LineVariable = makeNumericVariable(
1765 GlobalNumericVariableTable[LineName] = LineVariable;
1766}
1767
1769 : Req(Req), PatternContext(std::make_unique<FileCheckPatternContext>()),
1770 CheckStrings(std::make_unique<std::vector<FileCheckString>>()) {}
1771
1772FileCheck::~FileCheck() = default;
1773
1775 SourceMgr &SM, StringRef Buffer,
1776 std::pair<unsigned, unsigned> *ImpPatBufferIDRange) {
1777 if (ImpPatBufferIDRange)
1778 ImpPatBufferIDRange->first = ImpPatBufferIDRange->second = 0;
1779
1780 Error DefineError =
1781 PatternContext->defineCmdlineVariables(Req.GlobalDefines, SM);
1782 if (DefineError) {
1783 logAllUnhandledErrors(std::move(DefineError), errs());
1784 return true;
1785 }
1786
1787 PatternContext->createLineVariable();
1788
1789 std::vector<FileCheckString::DagNotPrefixInfo> ImplicitNegativeChecks;
1790 for (StringRef PatternString : Req.ImplicitCheckNot) {
1791 // Create a buffer with fake command line content in order to display the
1792 // command line option responsible for the specific implicit CHECK-NOT.
1793 std::string Prefix = "-implicit-check-not='";
1794 std::string Suffix = "'";
1795 std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
1796 (Prefix + PatternString + Suffix).str(), "command line");
1797
1798 StringRef PatternInBuffer =
1799 CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
1800 unsigned BufferID = SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
1801 if (ImpPatBufferIDRange) {
1802 if (ImpPatBufferIDRange->first == ImpPatBufferIDRange->second) {
1803 ImpPatBufferIDRange->first = BufferID;
1804 ImpPatBufferIDRange->second = BufferID + 1;
1805 } else {
1806 assert(BufferID == ImpPatBufferIDRange->second &&
1807 "expected consecutive source buffer IDs");
1808 ++ImpPatBufferIDRange->second;
1809 }
1810 }
1811
1812 ImplicitNegativeChecks.emplace_back(
1813 Pattern(Check::CheckNot, PatternContext.get()),
1814 StringRef("IMPLICIT-CHECK"));
1815 ImplicitNegativeChecks.back().DagNotPat.parsePattern(
1816 PatternInBuffer, "IMPLICIT-CHECK", SM, Req);
1817 }
1818
1819 std::vector<FileCheckString::DagNotPrefixInfo> DagNotMatches =
1820 ImplicitNegativeChecks;
1821 // LineNumber keeps track of the line on which CheckPrefix instances are
1822 // found.
1823 unsigned LineNumber = 1;
1824
1825 addDefaultPrefixes(Req);
1826 PrefixMatcher Matcher(Req.CheckPrefixes, Req.CommentPrefixes, Buffer);
1827 std::set<StringRef> PrefixesNotFound(Req.CheckPrefixes.begin(),
1828 Req.CheckPrefixes.end());
1829 const size_t DistinctPrefixes = PrefixesNotFound.size();
1830 while (true) {
1831 Check::FileCheckType CheckTy;
1832
1833 // See if a prefix occurs in the memory buffer.
1834 StringRef UsedPrefix;
1835 StringRef AfterSuffix;
1836 std::tie(UsedPrefix, AfterSuffix) =
1837 FindFirstMatchingPrefix(Req, Matcher, Buffer, LineNumber, CheckTy);
1838 if (UsedPrefix.empty())
1839 break;
1840 if (CheckTy != Check::CheckComment)
1841 PrefixesNotFound.erase(UsedPrefix);
1842
1843 assert(UsedPrefix.data() == Buffer.data() &&
1844 "Failed to move Buffer's start forward, or pointed prefix outside "
1845 "of the buffer!");
1846 assert(AfterSuffix.data() >= Buffer.data() &&
1847 AfterSuffix.data() < Buffer.data() + Buffer.size() &&
1848 "Parsing after suffix doesn't start inside of buffer!");
1849
1850 // Location to use for error messages.
1851 const char *UsedPrefixStart = UsedPrefix.data();
1852
1853 // Skip the buffer to the end of parsed suffix (or just prefix, if no good
1854 // suffix was processed).
1855 Buffer = AfterSuffix.empty() ? Buffer.drop_front(UsedPrefix.size())
1856 : AfterSuffix;
1857
1858 // Complain about misspelled directives.
1859 if (CheckTy == Check::CheckMisspelled) {
1860 StringRef UsedDirective(UsedPrefix.data(),
1861 AfterSuffix.data() - UsedPrefix.data());
1862 SM.PrintMessage(SMLoc::getFromPointer(UsedDirective.data()),
1864 "misspelled directive '" + UsedDirective + "'");
1865 return true;
1866 }
1867
1868 // Complain about useful-looking but unsupported suffixes.
1869 if (CheckTy == Check::CheckBadNot) {
1871 "unsupported -NOT combo on prefix '" + UsedPrefix + "'");
1872 return true;
1873 }
1874
1875 // Complain about invalid count specification.
1876 if (CheckTy == Check::CheckBadCount) {
1878 "invalid count in -COUNT specification on prefix '" +
1879 UsedPrefix + "'");
1880 return true;
1881 }
1882
1883 // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
1884 // leading whitespace.
1885 if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines))
1886 Buffer = Buffer.substr(Buffer.find_first_not_of(" \t"));
1887
1888 // Scan ahead to the end of line.
1889 size_t EOL = Buffer.find_first_of("\n\r");
1890
1891 // Remember the location of the start of the pattern, for diagnostics.
1892 SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data());
1893
1894 // Extract the pattern from the buffer.
1895 StringRef PatternBuffer = Buffer.substr(0, EOL);
1896 Buffer = Buffer.substr(EOL);
1897
1898 // If this is a comment, we're done.
1899 if (CheckTy == Check::CheckComment)
1900 continue;
1901
1902 // Parse the pattern.
1903 Pattern P(CheckTy, PatternContext.get(), LineNumber);
1904 if (P.parsePattern(PatternBuffer, UsedPrefix, SM, Req))
1905 return true;
1906
1907 // Verify that CHECK-LABEL lines do not define or use variables
1908 if ((CheckTy == Check::CheckLabel) && P.hasVariable()) {
1909 SM.PrintMessage(
1911 "found '" + UsedPrefix + "-LABEL:'"
1912 " with variable definition or use");
1913 return true;
1914 }
1915
1916 // Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them.
1917 if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame ||
1918 CheckTy == Check::CheckEmpty) &&
1919 CheckStrings->empty()) {
1920 StringRef Type = CheckTy == Check::CheckNext
1921 ? "NEXT"
1922 : CheckTy == Check::CheckEmpty ? "EMPTY" : "SAME";
1923 SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart),
1925 "found '" + UsedPrefix + "-" + Type +
1926 "' without previous '" + UsedPrefix + ": line");
1927 return true;
1928 }
1929
1930 // Handle CHECK-DAG/-NOT.
1931 if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) {
1932 DagNotMatches.emplace_back(P, UsedPrefix);
1933 continue;
1934 }
1935
1936 // Okay, add the string we captured to the output vector and move on.
1937 CheckStrings->emplace_back(P, UsedPrefix, PatternLoc);
1938 std::swap(DagNotMatches, CheckStrings->back().DagNotStrings);
1939 DagNotMatches = ImplicitNegativeChecks;
1940 }
1941
1942 // When there are no used prefixes we report an error except in the case that
1943 // no prefix is specified explicitly but -implicit-check-not is specified.
1944 const bool NoPrefixesFound = PrefixesNotFound.size() == DistinctPrefixes;
1945 const bool SomePrefixesUnexpectedlyNotUsed =
1946 !Req.AllowUnusedPrefixes && !PrefixesNotFound.empty();
1947 if ((NoPrefixesFound || SomePrefixesUnexpectedlyNotUsed) &&
1948 (ImplicitNegativeChecks.empty() || !Req.IsDefaultCheckPrefix)) {
1949 errs() << "error: no check strings found with prefix"
1950 << (PrefixesNotFound.size() > 1 ? "es " : " ");
1951 bool First = true;
1952 for (StringRef MissingPrefix : PrefixesNotFound) {
1953 if (!First)
1954 errs() << ", ";
1955 errs() << "\'" << MissingPrefix << ":'";
1956 First = false;
1957 }
1958 errs() << '\n';
1959 return true;
1960 }
1961
1962 // Add an EOF pattern for any trailing --implicit-check-not/CHECK-DAG/-NOTs,
1963 // and use the first prefix as a filler for the error message.
1964 if (!DagNotMatches.empty()) {
1965 CheckStrings->emplace_back(
1966 Pattern(Check::CheckEOF, PatternContext.get(), LineNumber + 1),
1967 *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data()));
1968 std::swap(DagNotMatches, CheckStrings->back().DagNotStrings);
1969 }
1970
1971 return false;
1972}
1973
1974/// Returns either (1) \c ErrorSuccess if there was no error or (2)
1975/// \c ErrorReported if an error was reported, such as an unexpected match.
1976static Error printMatch(bool ExpectedMatch, const SourceMgr &SM,
1977 StringRef Prefix, SMLoc Loc, const Pattern &Pat,
1978 int MatchedCount, StringRef Buffer,
1979 Pattern::MatchResult MatchResult,
1980 const FileCheckRequest &Req,
1981 std::vector<FileCheckDiag> *Diags) {
1982 // Suppress some verbosity if there's no error.
1983 bool HasError = !ExpectedMatch || MatchResult.TheError;
1984 bool PrintDiag = true;
1985 if (!HasError) {
1986 if (!Req.Verbose)
1987 return ErrorReported::reportedOrSuccess(HasError);
1988 if (!Req.VerboseVerbose && Pat.getCheckTy() == Check::CheckEOF)
1989 return ErrorReported::reportedOrSuccess(HasError);
1990 // Due to their verbosity, we don't print verbose diagnostics here if we're
1991 // gathering them for Diags to be rendered elsewhere, but we always print
1992 // other diagnostics.
1993 PrintDiag = !Diags;
1994 }
1995
1996 // Add "found" diagnostic, substitutions, and variable definitions to Diags.
1997 FileCheckDiag::MatchType MatchTy = ExpectedMatch
2000 SMRange MatchRange = ProcessMatchResult(MatchTy, SM, Loc, Pat.getCheckTy(),
2001 Buffer, MatchResult.TheMatch->Pos,
2002 MatchResult.TheMatch->Len, Diags);
2003 if (Diags) {
2004 Pat.printSubstitutions(SM, Buffer, MatchRange, MatchTy, Diags);
2005 Pat.printVariableDefs(SM, MatchTy, Diags);
2006 }
2007 if (!PrintDiag) {
2008 assert(!HasError && "expected to report more diagnostics for error");
2009 return ErrorReported::reportedOrSuccess(HasError);
2010 }
2011
2012 // Print the match.
2013 std::string Message = formatv("{0}: {1} string found in input",
2014 Pat.getCheckTy().getDescription(Prefix),
2015 (ExpectedMatch ? "expected" : "excluded"))
2016 .str();
2017 if (Pat.getCount() > 1)
2018 Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
2019 SM.PrintMessage(
2020 Loc, ExpectedMatch ? SourceMgr::DK_Remark : SourceMgr::DK_Error, Message);
2021 SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, "found here",
2022 {MatchRange});
2023
2024 // Print additional information, which can be useful even if there are errors.
2025 Pat.printSubstitutions(SM, Buffer, MatchRange, MatchTy, nullptr);
2026 Pat.printVariableDefs(SM, MatchTy, nullptr);
2027
2028 // Print errors and add them to Diags. We report these errors after the match
2029 // itself because we found them after the match. If we had found them before
2030 // the match, we'd be in printNoMatch.
2031 handleAllErrors(std::move(MatchResult.TheError),
2032 [&](const ErrorDiagnostic &E) {
2033 E.log(errs());
2034 if (Diags) {
2035 Diags->emplace_back(SM, Pat.getCheckTy(), Loc,
2036 FileCheckDiag::MatchFoundErrorNote,
2037 E.getRange(), E.getMessage().str());
2038 }
2039 });
2040 return ErrorReported::reportedOrSuccess(HasError);
2041}
2042
2043/// Returns either (1) \c ErrorSuccess if there was no error, or (2)
2044/// \c ErrorReported if an error was reported, such as an expected match not
2045/// found.
2046static Error printNoMatch(bool ExpectedMatch, const SourceMgr &SM,
2047 StringRef Prefix, SMLoc Loc, const Pattern &Pat,
2048 int MatchedCount, StringRef Buffer, Error MatchError,
2049 bool VerboseVerbose,
2050 std::vector<FileCheckDiag> *Diags) {
2051 // Print any pattern errors, and record them to be added to Diags later.
2052 bool HasError = ExpectedMatch;
2053 bool HasPatternError = false;
2054 FileCheckDiag::MatchType MatchTy = ExpectedMatch
2059 std::move(MatchError),
2060 [&](const ErrorDiagnostic &E) {
2061 HasError = HasPatternError = true;
2063 E.log(errs());
2064 if (Diags)
2065 ErrorMsgs.push_back(E.getMessage().str());
2066 },
2067 // NotFoundError is why printNoMatch was invoked.
2068 [](const NotFoundError &E) {});
2069
2070 // Suppress some verbosity if there's no error.
2071 bool PrintDiag = true;
2072 if (!HasError) {
2073 if (!VerboseVerbose)
2074 return ErrorReported::reportedOrSuccess(HasError);
2075 // Due to their verbosity, we don't print verbose diagnostics here if we're
2076 // gathering them for Diags to be rendered elsewhere, but we always print
2077 // other diagnostics.
2078 PrintDiag = !Diags;
2079 }
2080
2081 // Add "not found" diagnostic, substitutions, and pattern errors to Diags.
2082 //
2083 // We handle Diags a little differently than the errors we print directly:
2084 // we add the "not found" diagnostic to Diags even if there are pattern
2085 // errors. The reason is that we need to attach pattern errors as notes
2086 // somewhere in the input, and the input search range from the "not found"
2087 // diagnostic is all we have to anchor them.
2088 SMRange SearchRange = ProcessMatchResult(MatchTy, SM, Loc, Pat.getCheckTy(),
2089 Buffer, 0, Buffer.size(), Diags);
2090 if (Diags) {
2091 SMRange NoteRange = SMRange(SearchRange.Start, SearchRange.Start);
2092 for (StringRef ErrorMsg : ErrorMsgs)
2093 Diags->emplace_back(SM, Pat.getCheckTy(), Loc, MatchTy, NoteRange,
2094 ErrorMsg);
2095 Pat.printSubstitutions(SM, Buffer, SearchRange, MatchTy, Diags);
2096 }
2097 if (!PrintDiag) {
2098 assert(!HasError && "expected to report more diagnostics for error");
2099 return ErrorReported::reportedOrSuccess(HasError);
2100 }
2101
2102 // Print "not found" diagnostic, except that's implied if we already printed a
2103 // pattern error.
2104 if (!HasPatternError) {
2105 std::string Message = formatv("{0}: {1} string not found in input",
2106 Pat.getCheckTy().getDescription(Prefix),
2107 (ExpectedMatch ? "expected" : "excluded"))
2108 .str();
2109 if (Pat.getCount() > 1)
2110 Message +=
2111 formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str();
2112 SM.PrintMessage(Loc,
2113 ExpectedMatch ? SourceMgr::DK_Error : SourceMgr::DK_Remark,
2114 Message);
2115 SM.PrintMessage(SearchRange.Start, SourceMgr::DK_Note,
2116 "scanning from here");
2117 }
2118
2119 // Print additional information, which can be useful even after a pattern
2120 // error.
2121 Pat.printSubstitutions(SM, Buffer, SearchRange, MatchTy, nullptr);
2122 if (ExpectedMatch)
2123 Pat.printFuzzyMatch(SM, Buffer, Diags);
2124 return ErrorReported::reportedOrSuccess(HasError);
2125}
2126
2127/// Returns either (1) \c ErrorSuccess if there was no error, or (2)
2128/// \c ErrorReported if an error was reported.
2129static Error reportMatchResult(bool ExpectedMatch, const SourceMgr &SM,
2130 StringRef Prefix, SMLoc Loc, const Pattern &Pat,
2131 int MatchedCount, StringRef Buffer,
2132 Pattern::MatchResult MatchResult,
2133 const FileCheckRequest &Req,
2134 std::vector<FileCheckDiag> *Diags) {
2135 if (MatchResult.TheMatch)
2136 return printMatch(ExpectedMatch, SM, Prefix, Loc, Pat, MatchedCount, Buffer,
2137 std::move(MatchResult), Req, Diags);
2138 return printNoMatch(ExpectedMatch, SM, Prefix, Loc, Pat, MatchedCount, Buffer,
2139 std::move(MatchResult.TheError), Req.VerboseVerbose,
2140 Diags);
2141}
2142
2143/// Counts the number of newlines in the specified range.
2145 const char *&FirstNewLine) {
2146 unsigned NumNewLines = 0;
2147 while (true) {
2148 // Scan for newline.
2149 Range = Range.substr(Range.find_first_of("\n\r"));
2150 if (Range.empty())
2151 return NumNewLines;
2152
2153 ++NumNewLines;
2154
2155 // Handle \n\r and \r\n as a single newline.
2156 if (Range.size() > 1 && (Range[1] == '\n' || Range[1] == '\r') &&
2157 (Range[0] != Range[1]))
2158 Range = Range.substr(1);
2159 Range = Range.substr(1);
2160
2161 if (NumNewLines == 1)
2162 FirstNewLine = Range.begin();
2163 }
2164}
2165
2167 bool IsLabelScanMode, size_t &MatchLen,
2168 FileCheckRequest &Req,
2169 std::vector<FileCheckDiag> *Diags) const {
2170 size_t LastPos = 0;
2171 std::vector<const DagNotPrefixInfo *> NotStrings;
2172
2173 // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
2174 // bounds; we have not processed variable definitions within the bounded block
2175 // yet so cannot handle any final CHECK-DAG yet; this is handled when going
2176 // over the block again (including the last CHECK-LABEL) in normal mode.
2177 if (!IsLabelScanMode) {
2178 // Match "dag strings" (with mixed "not strings" if any).
2179 LastPos = CheckDag(SM, Buffer, NotStrings, Req, Diags);
2180 if (LastPos == StringRef::npos)
2181 return StringRef::npos;
2182 }
2183
2184 // Match itself from the last position after matching CHECK-DAG.
2185 size_t LastMatchEnd = LastPos;
2186 size_t FirstMatchPos = 0;
2187 // Go match the pattern Count times. Majority of patterns only match with
2188 // count 1 though.
2189 assert(Pat.getCount() != 0 && "pattern count can not be zero");
2190 for (int i = 1; i <= Pat.getCount(); i++) {
2191 StringRef MatchBuffer = Buffer.substr(LastMatchEnd);
2192 // get a match at current start point
2193 Pattern::MatchResult MatchResult = Pat.match(MatchBuffer, SM);
2194
2195 // report
2196 if (Error Err = reportMatchResult(/*ExpectedMatch=*/true, SM, Prefix, Loc,
2197 Pat, i, MatchBuffer,
2198 std::move(MatchResult), Req, Diags)) {
2199 cantFail(handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
2200 return StringRef::npos;
2201 }
2202
2203 size_t MatchPos = MatchResult.TheMatch->Pos;
2204 if (i == 1)
2205 FirstMatchPos = LastPos + MatchPos;
2206
2207 // move start point after the match
2208 LastMatchEnd += MatchPos + MatchResult.TheMatch->Len;
2209 }
2210 // Full match len counts from first match pos.
2211 MatchLen = LastMatchEnd - FirstMatchPos;
2212
2213 // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
2214 // or CHECK-NOT
2215 if (!IsLabelScanMode) {
2216 size_t MatchPos = FirstMatchPos - LastPos;
2217 StringRef MatchBuffer = Buffer.substr(LastPos);
2218 StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos);
2219
2220 // If this check is a "CHECK-NEXT", verify that the previous match was on
2221 // the previous line (i.e. that there is one newline between them).
2222 if (CheckNext(SM, SkippedRegion)) {
2224 Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
2225 Diags, Req.Verbose);
2226 return StringRef::npos;
2227 }
2228
2229 // If this check is a "CHECK-SAME", verify that the previous match was on
2230 // the same line (i.e. that there is no newline between them).
2231 if (CheckSame(SM, SkippedRegion)) {
2233 Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen,
2234 Diags, Req.Verbose);
2235 return StringRef::npos;
2236 }
2237
2238 // If this match had "not strings", verify that they don't exist in the
2239 // skipped region.
2240 if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
2241 return StringRef::npos;
2242 }
2243
2244 return FirstMatchPos;
2245}
2246
2247bool FileCheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const {
2248 if (Pat.getCheckTy() != Check::CheckNext &&
2250 return false;
2251
2252 Twine CheckName =
2253 Prefix +
2254 Twine(Pat.getCheckTy() == Check::CheckEmpty ? "-EMPTY" : "-NEXT");
2255
2256 // Count the number of newlines between the previous match and this one.
2257 const char *FirstNewLine = nullptr;
2258 unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
2259
2260 if (NumNewLines == 0) {
2262 CheckName + ": is on the same line as previous match");
2264 "'next' match was here");
2266 "previous match ended here");
2267 return true;
2268 }
2269
2270 if (NumNewLines != 1) {
2272 CheckName +
2273 ": is not on the line after the previous match");
2275 "'next' match was here");
2277 "previous match ended here");
2279 "non-matching line after previous match is here");
2280 return true;
2281 }
2282
2283 return false;
2284}
2285
2286bool FileCheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const {
2288 return false;
2289
2290 // Count the number of newlines between the previous match and this one.
2291 const char *FirstNewLine = nullptr;
2292 unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine);
2293
2294 if (NumNewLines != 0) {
2296 Prefix +
2297 "-SAME: is not on the same line as the previous match");
2299 "'next' match was here");
2301 "previous match ended here");
2302 return true;
2303 }
2304
2305 return false;
2306}
2307
2309 const SourceMgr &SM, StringRef Buffer,
2310 const std::vector<const DagNotPrefixInfo *> &NotStrings,
2311 const FileCheckRequest &Req, std::vector<FileCheckDiag> *Diags) const {
2312 bool DirectiveFail = false;
2313 for (auto NotInfo : NotStrings) {
2314 assert((NotInfo->DagNotPat.getCheckTy() == Check::CheckNot) &&
2315 "Expect CHECK-NOT!");
2316 Pattern::MatchResult MatchResult = NotInfo->DagNotPat.match(Buffer, SM);
2317 if (Error Err = reportMatchResult(
2318 /*ExpectedMatch=*/false, SM, NotInfo->DagNotPrefix,
2319 NotInfo->DagNotPat.getLoc(), NotInfo->DagNotPat, 1, Buffer,
2320 std::move(MatchResult), Req, Diags)) {
2321 cantFail(handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
2322 DirectiveFail = true;
2323 continue;
2324 }
2325 }
2326 return DirectiveFail;
2327}
2328
2329size_t
2331 std::vector<const DagNotPrefixInfo *> &NotStrings,
2332 const FileCheckRequest &Req,
2333 std::vector<FileCheckDiag> *Diags) const {
2334 if (DagNotStrings.empty())
2335 return 0;
2336
2337 // The start of the search range.
2338 size_t StartPos = 0;
2339
2340 struct MatchRange {
2341 size_t Pos;
2342 size_t End;
2343 };
2344 // A sorted list of ranges for non-overlapping CHECK-DAG matches. Match
2345 // ranges are erased from this list once they are no longer in the search
2346 // range.
2347 std::list<MatchRange> MatchRanges;
2348
2349 // We need PatItr and PatEnd later for detecting the end of a CHECK-DAG
2350 // group, so we don't use a range-based for loop here.
2351 for (auto PatItr = DagNotStrings.begin(), PatEnd = DagNotStrings.end();
2352 PatItr != PatEnd; ++PatItr) {
2353 const Pattern &Pat = PatItr->DagNotPat;
2354 const StringRef DNPrefix = PatItr->DagNotPrefix;
2357 "Invalid CHECK-DAG or CHECK-NOT!");
2358
2359 if (Pat.getCheckTy() == Check::CheckNot) {
2360 NotStrings.push_back(&*PatItr);
2361 continue;
2362 }
2363
2364 assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!");
2365
2366 // CHECK-DAG always matches from the start.
2367 size_t MatchLen = 0, MatchPos = StartPos;
2368
2369 // Search for a match that doesn't overlap a previous match in this
2370 // CHECK-DAG group.
2371 for (auto MI = MatchRanges.begin(), ME = MatchRanges.end(); true; ++MI) {
2372 StringRef MatchBuffer = Buffer.substr(MatchPos);
2373 Pattern::MatchResult MatchResult = Pat.match(MatchBuffer, SM);
2374 // With a group of CHECK-DAGs, a single mismatching means the match on
2375 // that group of CHECK-DAGs fails immediately.
2376 if (MatchResult.TheError || Req.VerboseVerbose) {
2377 if (Error Err = reportMatchResult(/*ExpectedMatch=*/true, SM, DNPrefix,
2378 Pat.getLoc(), Pat, 1, MatchBuffer,
2379 std::move(MatchResult), Req, Diags)) {
2380 cantFail(
2381 handleErrors(std::move(Err), [&](const ErrorReported &E) {}));
2382 return StringRef::npos;
2383 }
2384 }
2385 MatchLen = MatchResult.TheMatch->Len;
2386 // Re-calc it as the offset relative to the start of the original
2387 // string.
2388 MatchPos += MatchResult.TheMatch->Pos;
2389 MatchRange M{MatchPos, MatchPos + MatchLen};
2390 if (Req.AllowDeprecatedDagOverlap) {
2391 // We don't need to track all matches in this mode, so we just maintain
2392 // one match range that encompasses the current CHECK-DAG group's
2393 // matches.
2394 if (MatchRanges.empty())
2395 MatchRanges.insert(MatchRanges.end(), M);
2396 else {
2397 auto Block = MatchRanges.begin();
2398 Block->Pos = std::min(Block->Pos, M.Pos);
2399 Block->End = std::max(Block->End, M.End);
2400 }
2401 break;
2402 }
2403 // Iterate previous matches until overlapping match or insertion point.
2404 bool Overlap = false;
2405 for (; MI != ME; ++MI) {
2406 if (M.Pos < MI->End) {
2407 // !Overlap => New match has no overlap and is before this old match.
2408 // Overlap => New match overlaps this old match.
2409 Overlap = MI->Pos < M.End;
2410 break;
2411 }
2412 }
2413 if (!Overlap) {
2414 // Insert non-overlapping match into list.
2415 MatchRanges.insert(MI, M);
2416 break;
2417 }
2418 if (Req.VerboseVerbose) {
2419 // Due to their verbosity, we don't print verbose diagnostics here if
2420 // we're gathering them for a different rendering, but we always print
2421 // other diagnostics.
2422 if (!Diags) {
2423 SMLoc OldStart = SMLoc::getFromPointer(Buffer.data() + MI->Pos);
2424 SMLoc OldEnd = SMLoc::getFromPointer(Buffer.data() + MI->End);
2425 SMRange OldRange(OldStart, OldEnd);
2426 SM.PrintMessage(OldStart, SourceMgr::DK_Note,
2427 "match discarded, overlaps earlier DAG match here",
2428 {OldRange});
2429 } else {
2430 SMLoc CheckLoc = Diags->rbegin()->CheckLoc;
2431 for (auto I = Diags->rbegin(), E = Diags->rend();
2432 I != E && I->CheckLoc == CheckLoc; ++I)
2434 }
2435 }
2436 MatchPos = MI->End;
2437 }
2438 if (!Req.VerboseVerbose)
2440 /*ExpectedMatch=*/true, SM, DNPrefix, Pat.getLoc(), Pat, 1, Buffer,
2441 Pattern::MatchResult(MatchPos, MatchLen, Error::success()), Req,
2442 Diags));
2443
2444 // Handle the end of a CHECK-DAG group.
2445 if (std::next(PatItr) == PatEnd ||
2446 std::next(PatItr)->DagNotPat.getCheckTy() == Check::CheckNot) {
2447 if (!NotStrings.empty()) {
2448 // If there are CHECK-NOTs between two CHECK-DAGs or from CHECK to
2449 // CHECK-DAG, verify that there are no 'not' strings occurred in that
2450 // region.
2451 StringRef SkippedRegion =
2452 Buffer.slice(StartPos, MatchRanges.begin()->Pos);
2453 if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags))
2454 return StringRef::npos;
2455 // Clear "not strings".
2456 NotStrings.clear();
2457 }
2458 // All subsequent CHECK-DAGs and CHECK-NOTs should be matched from the
2459 // end of this CHECK-DAG group's match range.
2460 StartPos = MatchRanges.rbegin()->End;
2461 // Don't waste time checking for (impossible) overlaps before that.
2462 MatchRanges.clear();
2463 }
2464 }
2465
2466 return StartPos;
2467}
2468
2469static bool ValidatePrefixes(StringRef Kind, StringSet<> &UniquePrefixes,
2470 ArrayRef<StringRef> SuppliedPrefixes) {
2471 for (StringRef Prefix : SuppliedPrefixes) {
2472 if (Prefix.empty()) {
2473 errs() << "error: supplied " << Kind << " prefix must not be the empty "
2474 << "string\n";
2475 return false;
2476 }
2477 static const Regex Validator("^[a-zA-Z0-9_-]*$");
2478 if (!Validator.match(Prefix)) {
2479 errs() << "error: supplied " << Kind << " prefix must start with a "
2480 << "letter and contain only alphanumeric characters, hyphens, and "
2481 << "underscores: '" << Prefix << "'\n";
2482 return false;
2483 }
2484 if (!UniquePrefixes.insert(Prefix).second) {
2485 errs() << "error: supplied " << Kind << " prefix must be unique among "
2486 << "check and comment prefixes: '" << Prefix << "'\n";
2487 return false;
2488 }
2489 }
2490 return true;
2491}
2492
2494 StringSet<> UniquePrefixes;
2495 // Add default prefixes to catch user-supplied duplicates of them below.
2496 if (Req.CheckPrefixes.empty()) {
2497 for (const char *Prefix : DefaultCheckPrefixes)
2498 UniquePrefixes.insert(Prefix);
2499 }
2500 if (Req.CommentPrefixes.empty()) {
2501 for (const char *Prefix : DefaultCommentPrefixes)
2502 UniquePrefixes.insert(Prefix);
2503 }
2504 // Do not validate the default prefixes, or diagnostics about duplicates might
2505 // incorrectly indicate that they were supplied by the user.
2506 if (!ValidatePrefixes("check", UniquePrefixes, Req.CheckPrefixes))
2507 return false;
2508 if (!ValidatePrefixes("comment", UniquePrefixes, Req.CommentPrefixes))
2509 return false;
2510 return true;
2511}
2512
2514 ArrayRef<StringRef> CmdlineDefines, SourceMgr &SM) {
2515 assert(GlobalVariableTable.empty() && GlobalNumericVariableTable.empty() &&
2516 "Overriding defined variable with command-line variable definitions");
2517
2518 if (CmdlineDefines.empty())
2519 return Error::success();
2520
2521 // Create a string representing the vector of command-line definitions. Each
2522 // definition is on its own line and prefixed with a definition number to
2523 // clarify which definition a given diagnostic corresponds to.
2524 unsigned I = 0;
2525 Error Errs = Error::success();
2526 std::string CmdlineDefsDiag;
2527 SmallVector<std::pair<size_t, size_t>, 4> CmdlineDefsIndices;
2528 for (StringRef CmdlineDef : CmdlineDefines) {
2529 std::string DefPrefix = ("Global define #" + Twine(++I) + ": ").str();
2530 size_t EqIdx = CmdlineDef.find('=');
2531 if (EqIdx == StringRef::npos) {
2532 CmdlineDefsIndices.push_back(std::make_pair(CmdlineDefsDiag.size(), 0));
2533 continue;
2534 }
2535 // Numeric variable definition.
2536 if (CmdlineDef[0] == '#') {
2537 // Append a copy of the command-line definition adapted to use the same
2538 // format as in the input file to be able to reuse
2539 // parseNumericSubstitutionBlock.
2540 CmdlineDefsDiag += (DefPrefix + CmdlineDef + " (parsed as: [[").str();
2541 std::string SubstitutionStr = std::string(CmdlineDef);
2542 SubstitutionStr[EqIdx] = ':';
2543 CmdlineDefsIndices.push_back(
2544 std::make_pair(CmdlineDefsDiag.size(), SubstitutionStr.size()));
2545 CmdlineDefsDiag += (SubstitutionStr + Twine("]])\n")).str();
2546 } else {
2547 CmdlineDefsDiag += DefPrefix;
2548 CmdlineDefsIndices.push_back(
2549 std::make_pair(CmdlineDefsDiag.size(), CmdlineDef.size()));
2550 CmdlineDefsDiag += (CmdlineDef + "\n").str();
2551 }
2552 }
2553
2554 // Create a buffer with fake command line content in order to display
2555 // parsing diagnostic with location information and point to the
2556 // global definition with invalid syntax.
2557 std::unique_ptr<MemoryBuffer> CmdLineDefsDiagBuffer =
2558 MemoryBuffer::getMemBufferCopy(CmdlineDefsDiag, "Global defines");
2559 StringRef CmdlineDefsDiagRef = CmdLineDefsDiagBuffer->getBuffer();
2560 SM.AddNewSourceBuffer(std::move(CmdLineDefsDiagBuffer), SMLoc());
2561
2562 for (std::pair<size_t, size_t> CmdlineDefIndices : CmdlineDefsIndices) {
2563 StringRef CmdlineDef = CmdlineDefsDiagRef.substr(CmdlineDefIndices.first,
2564 CmdlineDefIndices.second);
2565 if (CmdlineDef.empty()) {
2566 Errs = joinErrors(
2567 std::move(Errs),
2568 ErrorDiagnostic::get(SM, CmdlineDef,
2569 "missing equal sign in global definition"));
2570 continue;
2571 }
2572
2573 // Numeric variable definition.
2574 if (CmdlineDef[0] == '#') {
2575 // Now parse the definition both to check that the syntax is correct and
2576 // to create the necessary class instance.
2577 StringRef CmdlineDefExpr = CmdlineDef.substr(1);
2578 std::optional<NumericVariable *> DefinedNumericVariable;
2579 Expected<std::unique_ptr<Expression>> ExpressionResult =
2581 DefinedNumericVariable, false,
2582 std::nullopt, this, SM);
2583 if (!ExpressionResult) {
2584 Errs = joinErrors(std::move(Errs), ExpressionResult.takeError());
2585 continue;
2586 }
2587 std::unique_ptr<Expression> Expression = std::move(*ExpressionResult);
2588 // Now evaluate the expression whose value this variable should be set
2589 // to, since the expression of a command-line variable definition should
2590 // only use variables defined earlier on the command-line. If not, this
2591 // is an error and we report it.
2593 if (!Value) {
2594 Errs = joinErrors(std::move(Errs), Value.takeError());
2595 continue;
2596 }
2597
2598 assert(DefinedNumericVariable && "No variable defined");
2599 (*DefinedNumericVariable)->setValue(*Value);
2600
2601 // Record this variable definition.
2602 GlobalNumericVariableTable[(*DefinedNumericVariable)->getName()] =
2603 *DefinedNumericVariable;
2604 } else {
2605 // String variable definition.
2606 std::pair<StringRef, StringRef> CmdlineNameVal = CmdlineDef.split('=');
2607 StringRef CmdlineName = CmdlineNameVal.first;
2608 StringRef OrigCmdlineName = CmdlineName;
2610 Pattern::parseVariable(CmdlineName, SM);
2611 if (!ParseVarResult) {
2612 Errs = joinErrors(std::move(Errs), ParseVarResult.takeError());
2613 continue;
2614 }
2615 // Check that CmdlineName does not denote a pseudo variable is only
2616 // composed of the parsed numeric variable. This catches cases like
2617 // "FOO+2" in a "FOO+2=10" definition.
2618 if (ParseVarResult->IsPseudo || !CmdlineName.empty()) {
2619 Errs = joinErrors(std::move(Errs),
2621 SM, OrigCmdlineName,
2622 "invalid name in string variable definition '" +
2623 OrigCmdlineName + "'"));
2624 continue;
2625 }
2626 StringRef Name = ParseVarResult->Name;
2627
2628 // Detect collisions between string and numeric variables when the former
2629 // is created later than the latter.
2630 if (GlobalNumericVariableTable.contains(Name)) {
2631 Errs = joinErrors(std::move(Errs),
2633 "numeric variable with name '" +
2634 Name + "' already exists"));
2635 continue;
2636 }
2637 GlobalVariableTable.insert(CmdlineNameVal);
2638 // Mark the string variable as defined to detect collisions between
2639 // string and numeric variables in defineCmdlineVariables when the latter
2640 // is created later than the former. We cannot reuse GlobalVariableTable
2641 // for this by populating it with an empty string since we would then
2642 // lose the ability to detect the use of an undefined variable in
2643 // match().
2644 DefinedVariableTable[Name] = true;
2645 }
2646 }
2647
2648 return Errs;
2649}
2650
2652 SmallVector<StringRef, 16> LocalPatternVars, LocalNumericVars;
2653 for (const StringMapEntry<StringRef> &Var : GlobalVariableTable)
2654 if (Var.first()[0] != '$')
2655 LocalPatternVars.push_back(Var.first());
2656
2657 // Numeric substitution reads the value of a variable directly, not via
2658 // GlobalNumericVariableTable. Therefore, we clear local variables by
2659 // clearing their value which will lead to a numeric substitution failure. We
2660 // also mark the variable for removal from GlobalNumericVariableTable since
2661 // this is what defineCmdlineVariables checks to decide that no global
2662 // variable has been defined.
2663 for (const auto &Var : GlobalNumericVariableTable)
2664 if (Var.first()[0] != '$') {
2665 Var.getValue()->clearValue();
2666 LocalNumericVars.push_back(Var.first());
2667 }
2668
2669 for (const auto &Var : LocalPatternVars)
2670 GlobalVariableTable.erase(Var);
2671 for (const auto &Var : LocalNumericVars)
2672 GlobalNumericVariableTable.erase(Var);
2673}
2674
2676 std::vector<FileCheckDiag> *Diags) {
2677 bool ChecksFailed = false;
2678
2679 unsigned i = 0, j = 0, e = CheckStrings->size();
2680 while (true) {
2681 StringRef CheckRegion;
2682 if (j == e) {
2683 CheckRegion = Buffer;
2684 } else {
2685 const FileCheckString &CheckLabelStr = (*CheckStrings)[j];
2686 if (CheckLabelStr.Pat.getCheckTy() != Check::CheckLabel) {
2687 ++j;
2688 continue;
2689 }
2690
2691 // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
2692 size_t MatchLabelLen = 0;
2693 size_t MatchLabelPos =
2694 CheckLabelStr.Check(SM, Buffer, true, MatchLabelLen, Req, Diags);
2695 if (MatchLabelPos == StringRef::npos)
2696 // Immediately bail if CHECK-LABEL fails, nothing else we can do.
2697 return false;
2698
2699 CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen);
2700 Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen);
2701 ++j;
2702 }
2703
2704 // Do not clear the first region as it's the one before the first
2705 // CHECK-LABEL and it would clear variables defined on the command-line
2706 // before they get used.
2707 if (i != 0 && Req.EnableVarScope)
2708 PatternContext->clearLocalVars();
2709
2710 for (; i != j; ++i) {
2711 const FileCheckString &CheckStr = (*CheckStrings)[i];
2712
2713 // Check each string within the scanned region, including a second check
2714 // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
2715 size_t MatchLen = 0;
2716 size_t MatchPos =
2717 CheckStr.Check(SM, CheckRegion, false, MatchLen, Req, Diags);
2718
2719 if (MatchPos == StringRef::npos) {
2720 ChecksFailed = true;
2721 i = j;
2722 break;
2723 }
2724
2725 CheckRegion = CheckRegion.substr(MatchPos + MatchLen);
2726 }
2727
2728 if (j == e)
2729 break;
2730 }
2731
2732 // Success if no checks failed.
2733 return !ChecksFailed;
2734}
AMDGPU promote alloca to vector or false DEBUG_TYPE to vector
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
std::string Name
bool End
Definition: ELF_riscv.cpp:480
static std::pair< StringRef, StringRef > FindFirstMatchingPrefix(const FileCheckRequest &Req, PrefixMatcher &Matcher, StringRef &Buffer, unsigned &LineNumber, Check::FileCheckType &CheckTy)
Searches the buffer for the first prefix in the prefix regular expression.
Definition: FileCheck.cpp:1717
static size_t SkipWord(StringRef Str, size_t Loc)
Definition: FileCheck.cpp:1633
static char popFront(StringRef &S)
Definition: FileCheck.cpp:324
constexpr StringLiteral SpaceChars
Definition: FileCheck.cpp:321
static Error reportMatchResult(bool ExpectedMatch, const SourceMgr &SM, StringRef Prefix, SMLoc Loc, const Pattern &Pat, int MatchedCount, StringRef Buffer, Pattern::MatchResult MatchResult, const FileCheckRequest &Req, std::vector< FileCheckDiag > *Diags)
Returns either (1) ErrorSuccess if there was no error, or (2) ErrorReported if an error was reported.
Definition: FileCheck.cpp:2129
static Error printNoMatch(bool ExpectedMatch, const SourceMgr &SM, StringRef Prefix, SMLoc Loc, const Pattern &Pat, int MatchedCount, StringRef Buffer, Error MatchError, bool VerboseVerbose, std::vector< FileCheckDiag > *Diags)
Returns either (1) ErrorSuccess if there was no error, or (2) ErrorReported if an error was reported,...
Definition: FileCheck.cpp:2046
static std::pair< Check::FileCheckType, StringRef > FindCheckType(const FileCheckRequest &Req, StringRef Buffer, StringRef Prefix, bool &Misspelled)
Definition: FileCheck.cpp:1534
static const char * DefaultCheckPrefixes[]
Definition: FileCheck.cpp:1639
static const char * DefaultCommentPrefixes[]
Definition: FileCheck.cpp:1640
static SMRange ProcessMatchResult(FileCheckDiag::MatchType MatchTy, const SourceMgr &SM, SMLoc Loc, Check::FileCheckType CheckTy, StringRef Buffer, size_t Pos, size_t Len, std::vector< FileCheckDiag > *Diags, bool AdjustPrevDiags=false)
Definition: FileCheck.cpp:1292
static unsigned CountNumNewlinesBetween(StringRef Range, const char *&FirstNewLine)
Counts the number of newlines in the specified range.
Definition: FileCheck.cpp:2144
static Error printMatch(bool ExpectedMatch, const SourceMgr &SM, StringRef Prefix, SMLoc Loc, const Pattern &Pat, int MatchedCount, StringRef Buffer, Pattern::MatchResult MatchResult, const FileCheckRequest &Req, std::vector< FileCheckDiag > *Diags)
Returns either (1) ErrorSuccess if there was no error or (2) ErrorReported if an error was reported,...
Definition: FileCheck.cpp:1976
static bool ValidatePrefixes(StringRef Kind, StringSet<> &UniquePrefixes, ArrayRef< StringRef > SuppliedPrefixes)
Definition: FileCheck.cpp:2469
static void addDefaultPrefixes(FileCheckRequest &Req)
Definition: FileCheck.cpp:1642
static unsigned nextAPIntBitWidth(unsigned BitWidth)
Definition: FileCheck.cpp:123
static bool IsPartOfWord(char c)
Definition: FileCheck.cpp:1470
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
nvptx lower args
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
This file contains some functions that are useful when dealing with strings.
StringSet - A set-like wrapper for the StringMap.
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:981
APInt abs() const
Get the absolute value.
Definition: APInt.h:1751
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1446
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:307
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1898
APInt sdiv_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1924
static constexpr unsigned APINT_BITS_PER_WORD
Bits in a word.
Definition: APInt.h:86
APInt smul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1930
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:954
bool isSignBitSet() const
Determine if sign bit of this APInt is set.
Definition: APInt.h:319
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1108
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1911
void toString(SmallVectorImpl< char > &Str, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false) const
Converts an APInt to a string and append it to Str.
Definition: APInt.cpp:2134
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
Expected< ExpressionFormat > getImplicitFormat(const SourceMgr &SM) const override
Definition: FileCheck.cpp:242
Expected< APInt > eval() const override
Evaluates the value of the binary operation represented by this AST, using EvalBinop on the result of...
Definition: FileCheck.cpp:203
std::string getDescription(StringRef Prefix) const
Definition: FileCheck.cpp:1494
bool isLiteralMatch() const
Definition: FileCheck.h:96
std::string getModifiersDescription() const
Definition: FileCheck.cpp:1482
FileCheckType & setCount(int C)
Definition: FileCheck.cpp:1474
Class to represent an error holding a diagnostic with location information used when printing it.
StringRef getMessage() const
void log(raw_ostream &OS) const override
Print diagnostic associated with this error when printing the error.
static Error get(const SourceMgr &SM, SMLoc Loc, const Twine &ErrMsg, SMRange Range=std::nullopt)
An error that has already been reported.
static Error reportedOrSuccess(bool HasErrorReported)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
virtual Expected< APInt > eval() const =0
Evaluates and.
StringRef getExpressionStr() const
Class representing an expression and its matching format.
ExpressionAST * getAST() const
Class holding the Pattern global state, shared by all patterns: tables holding values of variables an...
Error defineCmdlineVariables(ArrayRef< StringRef > CmdlineDefines, SourceMgr &SM)
Defines string and numeric variables from definitions given on the command line, passed as a vector o...
Definition: FileCheck.cpp:2513
void createLineVariable()
Create @LINE pseudo variable.
Definition: FileCheck.cpp:1760
Expected< StringRef > getPatternVarValue(StringRef VarName)
Definition: FileCheck.cpp:1360
void clearLocalVars()
Undefines local variables (variables whose name does not start with a '$' sign), i....
Definition: FileCheck.cpp:2651
bool readCheckFile(SourceMgr &SM, StringRef Buffer, std::pair< unsigned, unsigned > *ImpPatBufferIDRange=nullptr)
Reads the check file from Buffer and records the expected strings it contains.
Definition: FileCheck.cpp:1774
StringRef CanonicalizeFile(MemoryBuffer &MB, SmallVectorImpl< char > &OutputBuffer)
Canonicalizes whitespaces in the file.
Definition: FileCheck.cpp:1428
FileCheck(FileCheckRequest Req)
Definition: FileCheck.cpp:1768
bool checkInput(SourceMgr &SM, StringRef Buffer, std::vector< FileCheckDiag > *Diags=nullptr)
Checks the input to FileCheck provided in the Buffer against the expected strings read from the check...
Definition: FileCheck.cpp:2675
bool ValidateCheckPrefixes()
Definition: FileCheck.cpp:2493
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
size_t getBufferSize() const
Definition: MemoryBuffer.h:68
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
const char * getBufferEnd() const
Definition: MemoryBuffer.h:67
const char * getBufferStart() const
Definition: MemoryBuffer.h:66
Expected< std::string > getResult() const override
Definition: FileCheck.cpp:268
Expected< APInt > eval() const override
Definition: FileCheck.cpp:195
Class representing a numeric variable and its associated current value.
void setValue(APInt NewValue, std::optional< StringRef > NewStrValue=std::nullopt)
Sets value of this numeric variable to NewValue, and sets the input buffer string from which it was p...
ExpressionFormat getImplicitFormat() const
std::optional< APInt > getValue() const
std::optional< size_t > getDefLineNumber() const
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:32
Class to represent an overflow error that might result when manipulating a value.
This class represents success/failure for parsing-like operations that find it important to chain tog...
static Expected< VariableProperties > parseVariable(StringRef &Str, const SourceMgr &SM)
Parses the string at the start of Str for a variable name.
Definition: FileCheck.cpp:289
MatchResult match(StringRef Buffer, const SourceMgr &SM) const
Matches the pattern string against the input buffer Buffer.
Definition: FileCheck.cpp:1077
void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer, std::vector< FileCheckDiag > *Diags) const
Definition: FileCheck.cpp:1313
void printSubstitutions(const SourceMgr &SM, StringRef Buffer, SMRange MatchRange, FileCheckDiag::MatchType MatchTy, std::vector< FileCheckDiag > *Diags) const
Prints the value of successful substitutions.
Definition: FileCheck.cpp:1205
SMLoc getLoc() const
static Expected< std::unique_ptr< Expression > > parseNumericSubstitutionBlock(StringRef Expr, std::optional< NumericVariable * > &DefinedNumericVariable, bool IsLegacyLineExpr, std::optional< size_t > LineNumber, FileCheckPatternContext *Context, const SourceMgr &SM)
Parses Expr for a numeric substitution block at line LineNumber, or before input is parsed if LineNum...
Definition: FileCheck.cpp:617
void printVariableDefs(const SourceMgr &SM, FileCheckDiag::MatchType MatchTy, std::vector< FileCheckDiag > *Diags) const
Definition: FileCheck.cpp:1239
static bool isValidVarNameStart(char C)
Definition: FileCheck.cpp:286
int getCount() const
Check::FileCheckType getCheckTy() const
bool parsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM, const FileCheckRequest &Req)
Parses the pattern in PatternStr and initializes this Pattern instance accordingly.
Definition: FileCheck.cpp:764
@ Newline
Compile for newline-sensitive matching.
Definition: Regex.h:39
@ IgnoreCase
Compile for matching that ignores upper/lower case distinctions.
Definition: Regex.h:33
static std::string escape(StringRef String)
Turn String into a regex by escaping its special characters.
Definition: Regex.cpp:239
bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr, std::string *Error=nullptr) const
matches - Match the regex against a given String.
Definition: Regex.cpp:83
Represents a location in source code.
Definition: SMLoc.h:23
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:36
Represents a range in source code.
Definition: SMLoc.h:48
SMLoc Start
Definition: SMLoc.h:50
SMLoc End
Definition: SMLoc.h:50
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void reserve(size_type N)
Definition: SmallVector.h:676
void push_back(const T &Elt)
Definition: SmallVector.h:426
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:299
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
std::pair< unsigned, unsigned > getLineAndColumn(SMLoc Loc, unsigned BufferID=0) const
Find the line and column number for the specified location in the specified file.
Definition: SourceMgr.cpp:192
void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
Definition: SourceMgr.cpp:352
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:144
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:838
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
bool empty() const
Definition: StringMap.h:103
bool contains(StringRef Key) const
contains - Return true if the element is in the map, false otherwise.
Definition: StringMap.h:273
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:685
bool consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:484
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:215
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:556
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:250
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:594
unsigned edit_distance(StringRef Other, bool AllowReplacements=true, unsigned MaxEditDistance=0) const
Determine the edit distance between this string and another string.
Definition: StringRef.cpp:92
char back() const
back - Get the last character in the string.
Definition: StringRef.h:146
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:669
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
char front() const
front - Get the first character in the string.
Definition: StringRef.h:140
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
Definition: StringRef.h:776
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:409
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:620
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:362
iterator end() const
Definition: StringRef.h:113
StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
Definition: StringRef.h:788
StringRef take_front(size_t N=1) const
Return a StringRef equal to 'this' but with only the first N elements remaining.
Definition: StringRef.h:565
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:282
StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition: StringRef.h:800
size_t find_insensitive(char C, size_t From=0) const
Search for the first character C in the string, ignoring case.
Definition: StringRef.cpp:55
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:436
static constexpr size_t npos
Definition: StringRef.h:52
StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Definition: StringRef.h:601
size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition: StringRef.cpp:251
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:38
Expected< std::string > getResult() const override
Definition: FileCheck.cpp:278
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
Class representing a substitution to perform in the RegExStr string.
StringRef getFromString() const
size_t getIndex() const
FileCheckPatternContext * Context
Pointer to a class instance holding, among other things, the table with the values of live string var...
StringRef FromStr
The string that needs to be substituted for something else.
virtual Expected< std::string > getResult() const =0
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Class to represent an undefined variable error, which quotes that variable's name when printed.
LLVM Value Representation.
Definition: Value.h:74
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ CheckBadNot
Marks when parsing found a -NOT check combined with another CHECK suffix.
Definition: FileCheck.h:66
@ CheckBadCount
Marks when parsing found a -COUNT directive with invalid count value.
Definition: FileCheck.h:69
@ CheckEOF
Indicates the pattern only matches the end of file.
Definition: FileCheck.h:63
@ CheckMisspelled
Definition: FileCheck.h:51
@ CheckComment
Definition: FileCheck.h:59
std::optional< int64_t > toSigned(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an signed constant.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:65
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:977
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Definition: Error.h:954
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Expected< APInt > exprAdd(const APInt &Lhs, const APInt &Rhs, bool &Overflow)
Performs operation and.
Definition: FileCheck.cpp:156
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1286
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:438
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
Expected< APInt > exprMul(const APInt &Lhs, const APInt &Rhs, bool &Overflow)
Definition: FileCheck.cpp:166
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:756
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1886
Expected< APInt > exprMax(const APInt &Lhs, const APInt &Rhs, bool &Overflow)
Definition: FileCheck.cpp:180
Expected< APInt > exprDiv(const APInt &Lhs, const APInt &Rhs, bool &Overflow)
Definition: FileCheck.cpp:171
Expected< APInt > exprMin(const APInt &Lhs, const APInt &Rhs, bool &Overflow)
Definition: FileCheck.cpp:186
Expected< APInt > exprSub(const APInt &Lhs, const APInt &Rhs, bool &Overflow)
Definition: FileCheck.cpp:161
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
StringRef match(StringRef Buffer)
Find the next match of a prefix in Buffer.
Definition: FileCheck.cpp:1673
StringRef Input
Definition: FileCheck.cpp:1656
SmallVector< std::pair< StringRef, size_t > > Prefixes
Prefixes and their first occurrence past the current position.
Definition: FileCheck.cpp:1655
PrefixMatcher(ArrayRef< StringRef > CheckPrefixes, ArrayRef< StringRef > CommentPrefixes, StringRef Input)
Definition: FileCheck.cpp:1658
Type representing the format an expression value should be textualized into for matching.
Definition: FileCheckImpl.h:38
APInt valueFromStringRepr(StringRef StrVal, const SourceMgr &SM) const
Definition: FileCheck.cpp:137
StringRef toString() const
Definition: FileCheck.cpp:32
Expected< std::string > getMatchingString(APInt Value) const
Definition: FileCheck.cpp:81
Expected< std::string > getWildcardRegex() const
Definition: FileCheck.cpp:48
@ HexLower
Value should be printed as a lowercase hex number.
@ HexUpper
Value should be printed as an uppercase hex number.
@ Signed
Value is a signed integer and should be printed as a decimal number.
@ Unsigned
Value is an unsigned integer and should be printed as a decimal number.
@ NoFormat
Denote absence of format.
unsigned InputStartCol
Definition: FileCheck.h:164
unsigned InputStartLine
The search range if MatchTy starts with MatchNone, or the match range otherwise.
Definition: FileCheck.h:163
unsigned InputEndLine
Definition: FileCheck.h:165
FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy, SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange, StringRef Note="")
Definition: FileCheck.cpp:1457
unsigned InputEndCol
Definition: FileCheck.h:166
MatchType
What type of match result does this diagnostic describe?
Definition: FileCheck.h:130
@ MatchFoundButWrongLine
Indicates a match for an expected pattern, but the match is on the wrong line.
Definition: FileCheck.h:137
@ MatchNoneAndExcluded
Indicates no match for an excluded pattern.
Definition: FileCheck.h:147
@ MatchFoundButExcluded
Indicates a match for an excluded pattern.
Definition: FileCheck.h:134
@ MatchFuzzy
Indicates a fuzzy match that serves as a suggestion for the next intended match for an expected patte...
Definition: FileCheck.h:159
@ MatchFoundButDiscarded
Indicates a discarded match for an expected pattern.
Definition: FileCheck.h:139
@ MatchNoneForInvalidPattern
Indicates no match due to an expected or excluded pattern that has proven to be invalid at match time...
Definition: FileCheck.h:156
@ MatchFoundAndExpected
Indicates a good match for an expected pattern.
Definition: FileCheck.h:132
@ MatchNoneButExpected
Indicates no match for an expected pattern, but this might follow good matches when multiple matches ...
Definition: FileCheck.h:151
Contains info about various FileCheck options.
Definition: FileCheck.h:30
std::vector< StringRef > GlobalDefines
Definition: FileCheck.h:35
std::vector< StringRef > ImplicitCheckNot
Definition: FileCheck.h:34
std::vector< StringRef > CommentPrefixes
Definition: FileCheck.h:32
std::vector< StringRef > CheckPrefixes
Definition: FileCheck.h:31
bool AllowDeprecatedDagOverlap
Definition: FileCheck.h:42
A check that we found in the input file.
bool CheckNext(const SourceMgr &SM, StringRef Buffer) const
Verifies that there is a single line in the given Buffer.
Definition: FileCheck.cpp:2247
Pattern Pat
The pattern to match.
bool CheckSame(const SourceMgr &SM, StringRef Buffer) const
Verifies that there is no newline in the given Buffer.
Definition: FileCheck.cpp:2286
std::vector< DagNotPrefixInfo > DagNotStrings
Hold the DAG/NOT strings occurring in the input file.
SMLoc Loc
The location in the match file that the check string was specified.
StringRef Prefix
Which prefix name this check matched.
size_t CheckDag(const SourceMgr &SM, StringRef Buffer, std::vector< const DagNotPrefixInfo * > &NotStrings, const FileCheckRequest &Req, std::vector< FileCheckDiag > *Diags) const
Matches "dag strings" and their mixed "not strings".
Definition: FileCheck.cpp:2330
size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode, size_t &MatchLen, FileCheckRequest &Req, std::vector< FileCheckDiag > *Diags) const
Matches check string and its "not strings" and/or "dag strings".
Definition: FileCheck.cpp:2166
bool CheckNot(const SourceMgr &SM, StringRef Buffer, const std::vector< const DagNotPrefixInfo * > &NotStrings, const FileCheckRequest &Req, std::vector< FileCheckDiag > *Diags) const
Verifies that none of the strings in NotStrings are found in the given Buffer.
Definition: FileCheck.cpp:2308
std::optional< Match > TheMatch
Parsing information about a variable.