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