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