1 //===- FileCheck.cpp - Check that File's Contents match what is expected --===//
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
7 //===----------------------------------------------------------------------===//
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.
12 // This file implements most of the API that will be used by the FileCheck utility
13 // as well as various unittests.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Support/FileCheck.h"
17 #include "llvm/ADT/StringSet.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/FormatVariadic.h"
27 Expected
<uint64_t> FileCheckNumericVariableUse::eval() const {
28 Optional
<uint64_t> Value
= NumericVariable
->getValue();
32 return make_error
<FileCheckUndefVarError
>(Name
);
35 Expected
<uint64_t> FileCheckASTBinop::eval() const {
36 Expected
<uint64_t> LeftOp
= LeftOperand
->eval();
37 Expected
<uint64_t> RightOp
= RightOperand
->eval();
39 // Bubble up any error (e.g. undefined variables) in the recursive
41 if (!LeftOp
|| !RightOp
) {
42 Error Err
= Error::success();
44 Err
= joinErrors(std::move(Err
), LeftOp
.takeError());
46 Err
= joinErrors(std::move(Err
), RightOp
.takeError());
47 return std::move(Err
);
50 return EvalBinop(*LeftOp
, *RightOp
);
53 Expected
<std::string
> FileCheckNumericSubstitution::getResult() const {
54 Expected
<uint64_t> EvaluatedValue
= ExpressionAST
->eval();
56 return EvaluatedValue
.takeError();
57 return utostr(*EvaluatedValue
);
60 Expected
<std::string
> FileCheckStringSubstitution::getResult() const {
61 // Look up the value and escape it so that we can put it into the regex.
62 Expected
<StringRef
> VarVal
= Context
->getPatternVarValue(FromStr
);
64 return VarVal
.takeError();
65 return Regex::escape(*VarVal
);
68 bool FileCheckPattern::isValidVarNameStart(char C
) {
69 return C
== '_' || isalpha(C
);
72 Expected
<FileCheckPattern::VariableProperties
>
73 FileCheckPattern::parseVariable(StringRef
&Str
, const SourceMgr
&SM
) {
75 return FileCheckErrorDiagnostic::get(SM
, Str
, "empty variable name");
77 bool ParsedOneChar
= false;
79 bool IsPseudo
= Str
[0] == '@';
81 // Global vars start with '$'.
82 if (Str
[0] == '$' || IsPseudo
)
85 for (unsigned E
= Str
.size(); I
!= E
; ++I
) {
86 if (!ParsedOneChar
&& !isValidVarNameStart(Str
[I
]))
87 return FileCheckErrorDiagnostic::get(SM
, Str
, "invalid variable name");
89 // Variable names are composed of alphanumeric characters and underscores.
90 if (Str
[I
] != '_' && !isalnum(Str
[I
]))
95 StringRef Name
= Str
.take_front(I
);
97 return VariableProperties
{Name
, IsPseudo
};
100 // StringRef holding all characters considered as horizontal whitespaces by
101 // FileCheck input canonicalization.
102 constexpr StringLiteral SpaceChars
= " \t";
104 // Parsing helper function that strips the first character in S and returns it.
105 static char popFront(StringRef
&S
) {
111 char FileCheckUndefVarError::ID
= 0;
112 char FileCheckErrorDiagnostic::ID
= 0;
113 char FileCheckNotFoundError::ID
= 0;
115 Expected
<FileCheckNumericVariable
*>
116 FileCheckPattern::parseNumericVariableDefinition(
117 StringRef
&Expr
, FileCheckPatternContext
*Context
,
118 Optional
<size_t> LineNumber
, const SourceMgr
&SM
) {
119 Expected
<VariableProperties
> ParseVarResult
= parseVariable(Expr
, SM
);
121 return ParseVarResult
.takeError();
122 StringRef Name
= ParseVarResult
->Name
;
124 if (ParseVarResult
->IsPseudo
)
125 return FileCheckErrorDiagnostic::get(
126 SM
, Name
, "definition of pseudo numeric variable unsupported");
128 // Detect collisions between string and numeric variables when the latter
129 // is created later than the former.
130 if (Context
->DefinedVariableTable
.find(Name
) !=
131 Context
->DefinedVariableTable
.end())
132 return FileCheckErrorDiagnostic::get(
133 SM
, Name
, "string variable with name '" + Name
+ "' already exists");
135 Expr
= Expr
.ltrim(SpaceChars
);
137 return FileCheckErrorDiagnostic::get(
138 SM
, Expr
, "unexpected characters after numeric variable name");
140 FileCheckNumericVariable
*DefinedNumericVariable
;
141 auto VarTableIter
= Context
->GlobalNumericVariableTable
.find(Name
);
142 if (VarTableIter
!= Context
->GlobalNumericVariableTable
.end())
143 DefinedNumericVariable
= VarTableIter
->second
;
145 DefinedNumericVariable
= Context
->makeNumericVariable(Name
, LineNumber
);
147 return DefinedNumericVariable
;
150 Expected
<std::unique_ptr
<FileCheckNumericVariableUse
>>
151 FileCheckPattern::parseNumericVariableUse(StringRef Name
, bool IsPseudo
,
152 Optional
<size_t> LineNumber
,
153 FileCheckPatternContext
*Context
,
154 const SourceMgr
&SM
) {
155 if (IsPseudo
&& !Name
.equals("@LINE"))
156 return FileCheckErrorDiagnostic::get(
157 SM
, Name
, "invalid pseudo numeric variable '" + Name
+ "'");
159 // Numeric variable definitions and uses are parsed in the order in which
160 // they appear in the CHECK patterns. For each definition, the pointer to the
161 // class instance of the corresponding numeric variable definition is stored
162 // in GlobalNumericVariableTable in parsePattern. Therefore, if the pointer
163 // we get below is null, it means no such variable was defined before. When
164 // that happens, we create a dummy variable so that parsing can continue. All
165 // uses of undefined variables, whether string or numeric, are then diagnosed
166 // in printSubstitutions() after failing to match.
167 auto VarTableIter
= Context
->GlobalNumericVariableTable
.find(Name
);
168 FileCheckNumericVariable
*NumericVariable
;
169 if (VarTableIter
!= Context
->GlobalNumericVariableTable
.end())
170 NumericVariable
= VarTableIter
->second
;
172 NumericVariable
= Context
->makeNumericVariable(Name
);
173 Context
->GlobalNumericVariableTable
[Name
] = NumericVariable
;
176 Optional
<size_t> DefLineNumber
= NumericVariable
->getDefLineNumber();
177 if (DefLineNumber
&& LineNumber
&& *DefLineNumber
== *LineNumber
)
178 return FileCheckErrorDiagnostic::get(
180 "numeric variable '" + Name
+
181 "' defined earlier in the same CHECK directive");
183 return std::make_unique
<FileCheckNumericVariableUse
>(Name
, NumericVariable
);
186 Expected
<std::unique_ptr
<FileCheckExpressionAST
>>
187 FileCheckPattern::parseNumericOperand(StringRef
&Expr
, AllowedOperand AO
,
188 Optional
<size_t> LineNumber
,
189 FileCheckPatternContext
*Context
,
190 const SourceMgr
&SM
) {
191 if (AO
== AllowedOperand::LineVar
|| AO
== AllowedOperand::Any
) {
192 // Try to parse as a numeric variable use.
193 Expected
<FileCheckPattern::VariableProperties
> ParseVarResult
=
194 parseVariable(Expr
, SM
);
196 return parseNumericVariableUse(ParseVarResult
->Name
,
197 ParseVarResult
->IsPseudo
, LineNumber
,
199 if (AO
== AllowedOperand::LineVar
)
200 return ParseVarResult
.takeError();
201 // Ignore the error and retry parsing as a literal.
202 consumeError(ParseVarResult
.takeError());
205 // Otherwise, parse it as a literal.
206 uint64_t LiteralValue
;
207 if (!Expr
.consumeInteger(/*Radix=*/10, LiteralValue
))
208 return std::make_unique
<FileCheckExpressionLiteral
>(LiteralValue
);
210 return FileCheckErrorDiagnostic::get(SM
, Expr
,
211 "invalid operand format '" + Expr
+ "'");
214 static uint64_t add(uint64_t LeftOp
, uint64_t RightOp
) {
215 return LeftOp
+ RightOp
;
218 static uint64_t sub(uint64_t LeftOp
, uint64_t RightOp
) {
219 return LeftOp
- RightOp
;
222 Expected
<std::unique_ptr
<FileCheckExpressionAST
>> FileCheckPattern::parseBinop(
223 StringRef
&Expr
, std::unique_ptr
<FileCheckExpressionAST
> LeftOp
,
224 bool IsLegacyLineExpr
, Optional
<size_t> LineNumber
,
225 FileCheckPatternContext
*Context
, const SourceMgr
&SM
) {
226 Expr
= Expr
.ltrim(SpaceChars
);
228 return std::move(LeftOp
);
230 // Check if this is a supported operation and select a function to perform
232 SMLoc OpLoc
= SMLoc::getFromPointer(Expr
.data());
233 char Operator
= popFront(Expr
);
234 binop_eval_t EvalBinop
;
243 return FileCheckErrorDiagnostic::get(
244 SM
, OpLoc
, Twine("unsupported operation '") + Twine(Operator
) + "'");
247 // Parse right operand.
248 Expr
= Expr
.ltrim(SpaceChars
);
250 return FileCheckErrorDiagnostic::get(SM
, Expr
,
251 "missing operand in expression");
252 // The second operand in a legacy @LINE expression is always a literal.
254 IsLegacyLineExpr
? AllowedOperand::Literal
: AllowedOperand::Any
;
255 Expected
<std::unique_ptr
<FileCheckExpressionAST
>> RightOpResult
=
256 parseNumericOperand(Expr
, AO
, LineNumber
, Context
, SM
);
258 return RightOpResult
;
260 Expr
= Expr
.ltrim(SpaceChars
);
261 return std::make_unique
<FileCheckASTBinop
>(EvalBinop
, std::move(LeftOp
),
262 std::move(*RightOpResult
));
265 Expected
<std::unique_ptr
<FileCheckExpressionAST
>>
266 FileCheckPattern::parseNumericSubstitutionBlock(
268 Optional
<FileCheckNumericVariable
*> &DefinedNumericVariable
,
269 bool IsLegacyLineExpr
, Optional
<size_t> LineNumber
,
270 FileCheckPatternContext
*Context
, const SourceMgr
&SM
) {
271 std::unique_ptr
<FileCheckExpressionAST
> ExpressionAST
= nullptr;
272 StringRef DefExpr
= StringRef();
273 DefinedNumericVariable
= None
;
274 // Save variable definition expression if any.
275 size_t DefEnd
= Expr
.find(':');
276 if (DefEnd
!= StringRef::npos
) {
277 DefExpr
= Expr
.substr(0, DefEnd
);
278 Expr
= Expr
.substr(DefEnd
+ 1);
281 // Parse the expression itself.
282 Expr
= Expr
.ltrim(SpaceChars
);
284 // The first operand in a legacy @LINE expression is always the @LINE
287 IsLegacyLineExpr
? AllowedOperand::LineVar
: AllowedOperand::Any
;
288 Expected
<std::unique_ptr
<FileCheckExpressionAST
>> ParseResult
=
289 parseNumericOperand(Expr
, AO
, LineNumber
, Context
, SM
);
290 while (ParseResult
&& !Expr
.empty()) {
291 ParseResult
= parseBinop(Expr
, std::move(*ParseResult
), IsLegacyLineExpr
,
292 LineNumber
, Context
, SM
);
293 // Legacy @LINE expressions only allow 2 operands.
294 if (ParseResult
&& IsLegacyLineExpr
&& !Expr
.empty())
295 return FileCheckErrorDiagnostic::get(
297 "unexpected characters at end of expression '" + Expr
+ "'");
301 ExpressionAST
= std::move(*ParseResult
);
304 // Parse the numeric variable definition.
305 if (DefEnd
!= StringRef::npos
) {
306 DefExpr
= DefExpr
.ltrim(SpaceChars
);
307 Expected
<FileCheckNumericVariable
*> ParseResult
=
308 parseNumericVariableDefinition(DefExpr
, Context
, LineNumber
, SM
);
311 return ParseResult
.takeError();
312 DefinedNumericVariable
= *ParseResult
;
315 return std::move(ExpressionAST
);
318 bool FileCheckPattern::parsePattern(StringRef PatternStr
, StringRef Prefix
,
320 const FileCheckRequest
&Req
) {
321 bool MatchFullLinesHere
= Req
.MatchFullLines
&& CheckTy
!= Check::CheckNot
;
323 PatternLoc
= SMLoc::getFromPointer(PatternStr
.data());
325 if (!(Req
.NoCanonicalizeWhiteSpace
&& Req
.MatchFullLines
))
326 // Ignore trailing whitespace.
327 while (!PatternStr
.empty() &&
328 (PatternStr
.back() == ' ' || PatternStr
.back() == '\t'))
329 PatternStr
= PatternStr
.substr(0, PatternStr
.size() - 1);
331 // Check that there is something on the line.
332 if (PatternStr
.empty() && CheckTy
!= Check::CheckEmpty
) {
333 SM
.PrintMessage(PatternLoc
, SourceMgr::DK_Error
,
334 "found empty check string with prefix '" + Prefix
+ ":'");
338 if (!PatternStr
.empty() && CheckTy
== Check::CheckEmpty
) {
340 PatternLoc
, SourceMgr::DK_Error
,
341 "found non-empty check string for empty check with prefix '" + Prefix
+
346 if (CheckTy
== Check::CheckEmpty
) {
351 // Check to see if this is a fixed string, or if it has regex pieces.
352 if (!MatchFullLinesHere
&&
353 (PatternStr
.size() < 2 || (PatternStr
.find("{{") == StringRef::npos
&&
354 PatternStr
.find("[[") == StringRef::npos
))) {
355 FixedStr
= PatternStr
;
359 if (MatchFullLinesHere
) {
361 if (!Req
.NoCanonicalizeWhiteSpace
)
365 // Paren value #0 is for the fully matched string. Any new parenthesized
366 // values add from there.
367 unsigned CurParen
= 1;
369 // Otherwise, there is at least one regex piece. Build up the regex pattern
370 // by escaping scary characters in fixed strings, building up one big regex.
371 while (!PatternStr
.empty()) {
373 if (PatternStr
.startswith("{{")) {
374 // This is the start of a regex match. Scan for the }}.
375 size_t End
= PatternStr
.find("}}");
376 if (End
== StringRef::npos
) {
377 SM
.PrintMessage(SMLoc::getFromPointer(PatternStr
.data()),
379 "found start of regex string with no end '}}'");
383 // Enclose {{}} patterns in parens just like [[]] even though we're not
384 // capturing the result for any purpose. This is required in case the
385 // expression contains an alternation like: CHECK: abc{{x|z}}def. We
386 // want this to turn into: "abc(x|z)def" not "abcx|zdef".
390 if (AddRegExToRegEx(PatternStr
.substr(2, End
- 2), CurParen
, SM
))
394 PatternStr
= PatternStr
.substr(End
+ 2);
398 // String and numeric substitution blocks. Pattern substitution blocks come
399 // in two forms: [[foo:.*]] and [[foo]]. The former matches .* (or some
400 // other regex) and assigns it to the string variable 'foo'. The latter
401 // substitutes foo's value. Numeric substitution blocks recognize the same
402 // form as string ones, but start with a '#' sign after the double
403 // brackets. They also accept a combined form which sets a numeric variable
404 // to the evaluation of an expression. Both string and numeric variable
405 // names must satisfy the regular expression "[a-zA-Z_][0-9a-zA-Z_]*" to be
406 // valid, as this helps catch some common errors.
407 if (PatternStr
.startswith("[[")) {
408 StringRef UnparsedPatternStr
= PatternStr
.substr(2);
409 // Find the closing bracket pair ending the match. End is going to be an
410 // offset relative to the beginning of the match string.
411 size_t End
= FindRegexVarEnd(UnparsedPatternStr
, SM
);
412 StringRef MatchStr
= UnparsedPatternStr
.substr(0, End
);
413 bool IsNumBlock
= MatchStr
.consume_front("#");
415 if (End
== StringRef::npos
) {
416 SM
.PrintMessage(SMLoc::getFromPointer(PatternStr
.data()),
418 "Invalid substitution block, no ]] found");
421 // Strip the substitution block we are parsing. End points to the start
422 // of the "]]" closing the expression so account for it in computing the
423 // index of the first unparsed character.
424 PatternStr
= UnparsedPatternStr
.substr(End
+ 2);
426 bool IsDefinition
= false;
427 bool SubstNeeded
= false;
428 // Whether the substitution block is a legacy use of @LINE with string
429 // substitution block syntax.
430 bool IsLegacyLineExpr
= false;
433 StringRef MatchRegexp
;
434 size_t SubstInsertIdx
= RegExStr
.size();
436 // Parse string variable or legacy @LINE expression.
438 size_t VarEndIdx
= MatchStr
.find(":");
439 size_t SpacePos
= MatchStr
.substr(0, VarEndIdx
).find_first_of(" \t");
440 if (SpacePos
!= StringRef::npos
) {
441 SM
.PrintMessage(SMLoc::getFromPointer(MatchStr
.data() + SpacePos
),
442 SourceMgr::DK_Error
, "unexpected whitespace");
446 // Get the name (e.g. "foo") and verify it is well formed.
447 StringRef OrigMatchStr
= MatchStr
;
448 Expected
<FileCheckPattern::VariableProperties
> ParseVarResult
=
449 parseVariable(MatchStr
, SM
);
450 if (!ParseVarResult
) {
451 logAllUnhandledErrors(ParseVarResult
.takeError(), errs());
454 StringRef Name
= ParseVarResult
->Name
;
455 bool IsPseudo
= ParseVarResult
->IsPseudo
;
457 IsDefinition
= (VarEndIdx
!= StringRef::npos
);
458 SubstNeeded
= !IsDefinition
;
460 if ((IsPseudo
|| !MatchStr
.consume_front(":"))) {
461 SM
.PrintMessage(SMLoc::getFromPointer(Name
.data()),
463 "invalid name in string variable definition");
467 // Detect collisions between string and numeric variables when the
468 // former is created later than the latter.
469 if (Context
->GlobalNumericVariableTable
.find(Name
) !=
470 Context
->GlobalNumericVariableTable
.end()) {
472 SMLoc::getFromPointer(Name
.data()), SourceMgr::DK_Error
,
473 "numeric variable with name '" + Name
+ "' already exists");
477 MatchRegexp
= MatchStr
;
480 MatchStr
= OrigMatchStr
;
481 IsLegacyLineExpr
= IsNumBlock
= true;
487 // Parse numeric substitution block.
488 std::unique_ptr
<FileCheckExpressionAST
> ExpressionAST
;
489 Optional
<FileCheckNumericVariable
*> DefinedNumericVariable
;
491 Expected
<std::unique_ptr
<FileCheckExpressionAST
>> ParseResult
=
492 parseNumericSubstitutionBlock(MatchStr
, DefinedNumericVariable
,
493 IsLegacyLineExpr
, LineNumber
, Context
,
496 logAllUnhandledErrors(ParseResult
.takeError(), errs());
499 ExpressionAST
= std::move(*ParseResult
);
500 SubstNeeded
= ExpressionAST
!= nullptr;
501 if (DefinedNumericVariable
) {
503 DefName
= (*DefinedNumericVariable
)->getName();
508 MatchRegexp
= "[0-9]+";
511 // Handle variable definition: [[<def>:(...)]] and [[#(...)<def>:(...)]].
517 FileCheckNumericVariableMatch NumericVariableDefinition
= {
518 *DefinedNumericVariable
, CurParen
};
519 NumericVariableDefs
[DefName
] = NumericVariableDefinition
;
520 // This store is done here rather than in match() to allow
521 // parseNumericVariableUse() to get the pointer to the class instance
522 // of the right variable definition corresponding to a given numeric
524 Context
->GlobalNumericVariableTable
[DefName
] =
525 *DefinedNumericVariable
;
527 VariableDefs
[DefName
] = CurParen
;
528 // Mark string variable as defined to detect collisions between
529 // string and numeric variables in parseNumericVariableUse() and
530 // defineCmdlineVariables() when the latter is created later than the
531 // former. We cannot reuse GlobalVariableTable for this by populating
532 // it with an empty string since we would then lose the ability to
533 // detect the use of an undefined variable in match().
534 Context
->DefinedVariableTable
[DefName
] = true;
540 if (!MatchRegexp
.empty() && AddRegExToRegEx(MatchRegexp
, CurParen
, SM
))
546 // Handle substitutions: [[foo]] and [[#<foo expr>]].
548 // Handle substitution of string variables that were defined earlier on
549 // the same line by emitting a backreference. Expressions do not
550 // support substituting a numeric variable defined on the same line.
551 if (!IsNumBlock
&& VariableDefs
.find(SubstStr
) != VariableDefs
.end()) {
552 unsigned CaptureParenGroup
= VariableDefs
[SubstStr
];
553 if (CaptureParenGroup
< 1 || CaptureParenGroup
> 9) {
554 SM
.PrintMessage(SMLoc::getFromPointer(SubstStr
.data()),
556 "Can't back-reference more than 9 variables");
559 AddBackrefToRegEx(CaptureParenGroup
);
561 // Handle substitution of string variables ([[<var>]]) defined in
562 // previous CHECK patterns, and substitution of expressions.
563 FileCheckSubstitution
*Substitution
=
565 ? Context
->makeNumericSubstitution(
566 SubstStr
, std::move(ExpressionAST
), SubstInsertIdx
)
567 : Context
->makeStringSubstitution(SubstStr
, SubstInsertIdx
);
568 Substitutions
.push_back(Substitution
);
573 // Handle fixed string matches.
574 // Find the end, which is the start of the next regex.
575 size_t FixedMatchEnd
= PatternStr
.find("{{");
576 FixedMatchEnd
= std::min(FixedMatchEnd
, PatternStr
.find("[["));
577 RegExStr
+= Regex::escape(PatternStr
.substr(0, FixedMatchEnd
));
578 PatternStr
= PatternStr
.substr(FixedMatchEnd
);
581 if (MatchFullLinesHere
) {
582 if (!Req
.NoCanonicalizeWhiteSpace
)
590 bool FileCheckPattern::AddRegExToRegEx(StringRef RS
, unsigned &CurParen
, SourceMgr
&SM
) {
593 if (!R
.isValid(Error
)) {
594 SM
.PrintMessage(SMLoc::getFromPointer(RS
.data()), SourceMgr::DK_Error
,
595 "invalid regex: " + Error
);
599 RegExStr
+= RS
.str();
600 CurParen
+= R
.getNumMatches();
604 void FileCheckPattern::AddBackrefToRegEx(unsigned BackrefNum
) {
605 assert(BackrefNum
>= 1 && BackrefNum
<= 9 && "Invalid backref number");
606 std::string Backref
= std::string("\\") + std::string(1, '0' + BackrefNum
);
610 Expected
<size_t> FileCheckPattern::match(StringRef Buffer
, size_t &MatchLen
,
611 const SourceMgr
&SM
) const {
612 // If this is the EOF pattern, match it immediately.
613 if (CheckTy
== Check::CheckEOF
) {
615 return Buffer
.size();
618 // If this is a fixed string pattern, just match it now.
619 if (!FixedStr
.empty()) {
620 MatchLen
= FixedStr
.size();
621 size_t Pos
= Buffer
.find(FixedStr
);
622 if (Pos
== StringRef::npos
)
623 return make_error
<FileCheckNotFoundError
>();
629 // If there are substitutions, we need to create a temporary string with the
631 StringRef RegExToMatch
= RegExStr
;
633 if (!Substitutions
.empty()) {
636 Context
->LineVariable
->setValue(*LineNumber
);
638 size_t InsertOffset
= 0;
639 // Substitute all string variables and expressions whose values are only
640 // now known. Use of string variables defined on the same line are handled
641 // by back-references.
642 for (const auto &Substitution
: Substitutions
) {
643 // Substitute and check for failure (e.g. use of undefined variable).
644 Expected
<std::string
> Value
= Substitution
->getResult();
646 return Value
.takeError();
648 // Plop it into the regex at the adjusted offset.
649 TmpStr
.insert(TmpStr
.begin() + Substitution
->getIndex() + InsertOffset
,
650 Value
->begin(), Value
->end());
651 InsertOffset
+= Value
->size();
654 // Match the newly constructed regex.
655 RegExToMatch
= TmpStr
;
658 SmallVector
<StringRef
, 4> MatchInfo
;
659 if (!Regex(RegExToMatch
, Regex::Newline
).match(Buffer
, &MatchInfo
))
660 return make_error
<FileCheckNotFoundError
>();
662 // Successful regex match.
663 assert(!MatchInfo
.empty() && "Didn't get any match");
664 StringRef FullMatch
= MatchInfo
[0];
666 // If this defines any string variables, remember their values.
667 for (const auto &VariableDef
: VariableDefs
) {
668 assert(VariableDef
.second
< MatchInfo
.size() && "Internal paren error");
669 Context
->GlobalVariableTable
[VariableDef
.first
] =
670 MatchInfo
[VariableDef
.second
];
673 // If this defines any numeric variables, remember their values.
674 for (const auto &NumericVariableDef
: NumericVariableDefs
) {
675 const FileCheckNumericVariableMatch
&NumericVariableMatch
=
676 NumericVariableDef
.getValue();
677 unsigned CaptureParenGroup
= NumericVariableMatch
.CaptureParenGroup
;
678 assert(CaptureParenGroup
< MatchInfo
.size() && "Internal paren error");
679 FileCheckNumericVariable
*DefinedNumericVariable
=
680 NumericVariableMatch
.DefinedNumericVariable
;
682 StringRef MatchedValue
= MatchInfo
[CaptureParenGroup
];
684 if (MatchedValue
.getAsInteger(10, Val
))
685 return FileCheckErrorDiagnostic::get(SM
, MatchedValue
,
686 "Unable to represent numeric value");
687 DefinedNumericVariable
->setValue(Val
);
690 // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after
691 // the required preceding newline, which is consumed by the pattern in the
692 // case of CHECK-EMPTY but not CHECK-NEXT.
693 size_t MatchStartSkip
= CheckTy
== Check::CheckEmpty
;
694 MatchLen
= FullMatch
.size() - MatchStartSkip
;
695 return FullMatch
.data() - Buffer
.data() + MatchStartSkip
;
698 unsigned FileCheckPattern::computeMatchDistance(StringRef Buffer
) const {
699 // Just compute the number of matching characters. For regular expressions, we
700 // just compare against the regex itself and hope for the best.
702 // FIXME: One easy improvement here is have the regex lib generate a single
703 // example regular expression which matches, and use that as the example
705 StringRef
ExampleString(FixedStr
);
706 if (ExampleString
.empty())
707 ExampleString
= RegExStr
;
709 // Only compare up to the first line in the buffer, or the string size.
710 StringRef BufferPrefix
= Buffer
.substr(0, ExampleString
.size());
711 BufferPrefix
= BufferPrefix
.split('\n').first
;
712 return BufferPrefix
.edit_distance(ExampleString
);
715 void FileCheckPattern::printSubstitutions(const SourceMgr
&SM
, StringRef Buffer
,
716 SMRange MatchRange
) const {
717 // Print what we know about substitutions.
718 if (!Substitutions
.empty()) {
719 for (const auto &Substitution
: Substitutions
) {
720 SmallString
<256> Msg
;
721 raw_svector_ostream
OS(Msg
);
722 Expected
<std::string
> MatchedValue
= Substitution
->getResult();
724 // Substitution failed or is not known at match time, print the undefined
725 // variables it uses.
727 bool UndefSeen
= false;
728 handleAllErrors(MatchedValue
.takeError(),
729 [](const FileCheckNotFoundError
&E
) {},
730 // Handled in PrintNoMatch().
731 [](const FileCheckErrorDiagnostic
&E
) {},
732 [&](const FileCheckUndefVarError
&E
) {
734 OS
<< "uses undefined variable(s):";
741 // Substitution succeeded. Print substituted value.
743 OS
.write_escaped(Substitution
->getFromString()) << "\" equal to \"";
744 OS
.write_escaped(*MatchedValue
) << "\"";
747 if (MatchRange
.isValid())
748 SM
.PrintMessage(MatchRange
.Start
, SourceMgr::DK_Note
, OS
.str(),
751 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.data()),
752 SourceMgr::DK_Note
, OS
.str());
757 static SMRange
ProcessMatchResult(FileCheckDiag::MatchType MatchTy
,
758 const SourceMgr
&SM
, SMLoc Loc
,
759 Check::FileCheckType CheckTy
,
760 StringRef Buffer
, size_t Pos
, size_t Len
,
761 std::vector
<FileCheckDiag
> *Diags
,
762 bool AdjustPrevDiag
= false) {
763 SMLoc Start
= SMLoc::getFromPointer(Buffer
.data() + Pos
);
764 SMLoc End
= SMLoc::getFromPointer(Buffer
.data() + Pos
+ Len
);
765 SMRange
Range(Start
, End
);
768 Diags
->rbegin()->MatchTy
= MatchTy
;
770 Diags
->emplace_back(SM
, CheckTy
, Loc
, MatchTy
, Range
);
775 void FileCheckPattern::printFuzzyMatch(
776 const SourceMgr
&SM
, StringRef Buffer
,
777 std::vector
<FileCheckDiag
> *Diags
) const {
778 // Attempt to find the closest/best fuzzy match. Usually an error happens
779 // because some string in the output didn't exactly match. In these cases, we
780 // would like to show the user a best guess at what "should have" matched, to
781 // save them having to actually check the input manually.
782 size_t NumLinesForward
= 0;
783 size_t Best
= StringRef::npos
;
784 double BestQuality
= 0;
786 // Use an arbitrary 4k limit on how far we will search.
787 for (size_t i
= 0, e
= std::min(size_t(4096), Buffer
.size()); i
!= e
; ++i
) {
788 if (Buffer
[i
] == '\n')
791 // Patterns have leading whitespace stripped, so skip whitespace when
792 // looking for something which looks like a pattern.
793 if (Buffer
[i
] == ' ' || Buffer
[i
] == '\t')
796 // Compute the "quality" of this match as an arbitrary combination of the
797 // match distance and the number of lines skipped to get to this match.
798 unsigned Distance
= computeMatchDistance(Buffer
.substr(i
));
799 double Quality
= Distance
+ (NumLinesForward
/ 100.);
801 if (Quality
< BestQuality
|| Best
== StringRef::npos
) {
803 BestQuality
= Quality
;
807 // Print the "possible intended match here" line if we found something
808 // reasonable and not equal to what we showed in the "scanning from here"
810 if (Best
&& Best
!= StringRef::npos
&& BestQuality
< 50) {
812 ProcessMatchResult(FileCheckDiag::MatchFuzzy
, SM
, getLoc(),
813 getCheckTy(), Buffer
, Best
, 0, Diags
);
814 SM
.PrintMessage(MatchRange
.Start
, SourceMgr::DK_Note
,
815 "possible intended match here");
817 // FIXME: If we wanted to be really friendly we would show why the match
818 // failed, as it can be hard to spot simple one character differences.
823 FileCheckPatternContext::getPatternVarValue(StringRef VarName
) {
824 auto VarIter
= GlobalVariableTable
.find(VarName
);
825 if (VarIter
== GlobalVariableTable
.end())
826 return make_error
<FileCheckUndefVarError
>(VarName
);
828 return VarIter
->second
;
831 template <class... Types
>
832 FileCheckNumericVariable
*
833 FileCheckPatternContext::makeNumericVariable(Types
... args
) {
834 NumericVariables
.push_back(
835 std::make_unique
<FileCheckNumericVariable
>(args
...));
836 return NumericVariables
.back().get();
839 FileCheckSubstitution
*
840 FileCheckPatternContext::makeStringSubstitution(StringRef VarName
,
842 Substitutions
.push_back(
843 std::make_unique
<FileCheckStringSubstitution
>(this, VarName
, InsertIdx
));
844 return Substitutions
.back().get();
847 FileCheckSubstitution
*FileCheckPatternContext::makeNumericSubstitution(
848 StringRef ExpressionStr
,
849 std::unique_ptr
<FileCheckExpressionAST
> ExpressionAST
, size_t InsertIdx
) {
850 Substitutions
.push_back(std::make_unique
<FileCheckNumericSubstitution
>(
851 this, ExpressionStr
, std::move(ExpressionAST
), InsertIdx
));
852 return Substitutions
.back().get();
855 size_t FileCheckPattern::FindRegexVarEnd(StringRef Str
, SourceMgr
&SM
) {
856 // Offset keeps track of the current offset within the input Str
858 // [...] Nesting depth
859 size_t BracketDepth
= 0;
861 while (!Str
.empty()) {
862 if (Str
.startswith("]]") && BracketDepth
== 0)
864 if (Str
[0] == '\\') {
865 // Backslash escapes the next char within regexes, so skip them both.
876 if (BracketDepth
== 0) {
877 SM
.PrintMessage(SMLoc::getFromPointer(Str
.data()),
879 "missing closing \"]\" for regex variable");
890 return StringRef::npos
;
893 StringRef
FileCheck::CanonicalizeFile(MemoryBuffer
&MB
,
894 SmallVectorImpl
<char> &OutputBuffer
) {
895 OutputBuffer
.reserve(MB
.getBufferSize());
897 for (const char *Ptr
= MB
.getBufferStart(), *End
= MB
.getBufferEnd();
899 // Eliminate trailing dosish \r.
900 if (Ptr
<= End
- 2 && Ptr
[0] == '\r' && Ptr
[1] == '\n') {
904 // If current char is not a horizontal whitespace or if horizontal
905 // whitespace canonicalization is disabled, dump it to output as is.
906 if (Req
.NoCanonicalizeWhiteSpace
|| (*Ptr
!= ' ' && *Ptr
!= '\t')) {
907 OutputBuffer
.push_back(*Ptr
);
911 // Otherwise, add one space and advance over neighboring space.
912 OutputBuffer
.push_back(' ');
913 while (Ptr
+ 1 != End
&& (Ptr
[1] == ' ' || Ptr
[1] == '\t'))
917 // Add a null byte and then return all but that byte.
918 OutputBuffer
.push_back('\0');
919 return StringRef(OutputBuffer
.data(), OutputBuffer
.size() - 1);
922 FileCheckDiag::FileCheckDiag(const SourceMgr
&SM
,
923 const Check::FileCheckType
&CheckTy
,
924 SMLoc CheckLoc
, MatchType MatchTy
,
926 : CheckTy(CheckTy
), MatchTy(MatchTy
) {
927 auto Start
= SM
.getLineAndColumn(InputRange
.Start
);
928 auto End
= SM
.getLineAndColumn(InputRange
.End
);
929 InputStartLine
= Start
.first
;
930 InputStartCol
= Start
.second
;
931 InputEndLine
= End
.first
;
932 InputEndCol
= End
.second
;
933 Start
= SM
.getLineAndColumn(CheckLoc
);
934 CheckLine
= Start
.first
;
935 CheckCol
= Start
.second
;
938 static bool IsPartOfWord(char c
) {
939 return (isalnum(c
) || c
== '-' || c
== '_');
942 Check::FileCheckType
&Check::FileCheckType::setCount(int C
) {
943 assert(Count
> 0 && "zero and negative counts are not supported");
944 assert((C
== 1 || Kind
== CheckPlain
) &&
945 "count supported only for plain CHECK directives");
950 std::string
Check::FileCheckType::getDescription(StringRef Prefix
) const {
952 case Check::CheckNone
:
954 case Check::CheckPlain
:
956 return Prefix
.str() + "-COUNT";
958 case Check::CheckNext
:
959 return Prefix
.str() + "-NEXT";
960 case Check::CheckSame
:
961 return Prefix
.str() + "-SAME";
962 case Check::CheckNot
:
963 return Prefix
.str() + "-NOT";
964 case Check::CheckDAG
:
965 return Prefix
.str() + "-DAG";
966 case Check::CheckLabel
:
967 return Prefix
.str() + "-LABEL";
968 case Check::CheckEmpty
:
969 return Prefix
.str() + "-EMPTY";
970 case Check::CheckEOF
:
971 return "implicit EOF";
972 case Check::CheckBadNot
:
974 case Check::CheckBadCount
:
977 llvm_unreachable("unknown FileCheckType");
980 static std::pair
<Check::FileCheckType
, StringRef
>
981 FindCheckType(StringRef Buffer
, StringRef Prefix
) {
982 if (Buffer
.size() <= Prefix
.size())
983 return {Check::CheckNone
, StringRef()};
985 char NextChar
= Buffer
[Prefix
.size()];
987 StringRef Rest
= Buffer
.drop_front(Prefix
.size() + 1);
988 // Verify that the : is present after the prefix.
990 return {Check::CheckPlain
, Rest
};
993 return {Check::CheckNone
, StringRef()};
995 if (Rest
.consume_front("COUNT-")) {
997 if (Rest
.consumeInteger(10, Count
))
998 // Error happened in parsing integer.
999 return {Check::CheckBadCount
, Rest
};
1000 if (Count
<= 0 || Count
> INT32_MAX
)
1001 return {Check::CheckBadCount
, Rest
};
1002 if (!Rest
.consume_front(":"))
1003 return {Check::CheckBadCount
, Rest
};
1004 return {Check::FileCheckType(Check::CheckPlain
).setCount(Count
), Rest
};
1007 if (Rest
.consume_front("NEXT:"))
1008 return {Check::CheckNext
, Rest
};
1010 if (Rest
.consume_front("SAME:"))
1011 return {Check::CheckSame
, Rest
};
1013 if (Rest
.consume_front("NOT:"))
1014 return {Check::CheckNot
, Rest
};
1016 if (Rest
.consume_front("DAG:"))
1017 return {Check::CheckDAG
, Rest
};
1019 if (Rest
.consume_front("LABEL:"))
1020 return {Check::CheckLabel
, Rest
};
1022 if (Rest
.consume_front("EMPTY:"))
1023 return {Check::CheckEmpty
, Rest
};
1025 // You can't combine -NOT with another suffix.
1026 if (Rest
.startswith("DAG-NOT:") || Rest
.startswith("NOT-DAG:") ||
1027 Rest
.startswith("NEXT-NOT:") || Rest
.startswith("NOT-NEXT:") ||
1028 Rest
.startswith("SAME-NOT:") || Rest
.startswith("NOT-SAME:") ||
1029 Rest
.startswith("EMPTY-NOT:") || Rest
.startswith("NOT-EMPTY:"))
1030 return {Check::CheckBadNot
, Rest
};
1032 return {Check::CheckNone
, Rest
};
1035 // From the given position, find the next character after the word.
1036 static size_t SkipWord(StringRef Str
, size_t Loc
) {
1037 while (Loc
< Str
.size() && IsPartOfWord(Str
[Loc
]))
1042 /// Searches the buffer for the first prefix in the prefix regular expression.
1044 /// This searches the buffer using the provided regular expression, however it
1045 /// enforces constraints beyond that:
1046 /// 1) The found prefix must not be a suffix of something that looks like
1048 /// 2) The found prefix must be followed by a valid check type suffix using \c
1049 /// FindCheckType above.
1051 /// \returns a pair of StringRefs into the Buffer, which combines:
1052 /// - the first match of the regular expression to satisfy these two is
1054 /// otherwise an empty StringRef is returned to indicate failure.
1055 /// - buffer rewound to the location right after parsed suffix, for parsing
1056 /// to continue from
1058 /// If this routine returns a valid prefix, it will also shrink \p Buffer to
1059 /// start at the beginning of the returned prefix, increment \p LineNumber for
1060 /// each new line consumed from \p Buffer, and set \p CheckTy to the type of
1061 /// check found by examining the suffix.
1063 /// If no valid prefix is found, the state of Buffer, LineNumber, and CheckTy
1065 static std::pair
<StringRef
, StringRef
>
1066 FindFirstMatchingPrefix(Regex
&PrefixRE
, StringRef
&Buffer
,
1067 unsigned &LineNumber
, Check::FileCheckType
&CheckTy
) {
1068 SmallVector
<StringRef
, 2> Matches
;
1070 while (!Buffer
.empty()) {
1071 // Find the first (longest) match using the RE.
1072 if (!PrefixRE
.match(Buffer
, &Matches
))
1073 // No match at all, bail.
1074 return {StringRef(), StringRef()};
1076 StringRef Prefix
= Matches
[0];
1079 assert(Prefix
.data() >= Buffer
.data() &&
1080 Prefix
.data() < Buffer
.data() + Buffer
.size() &&
1081 "Prefix doesn't start inside of buffer!");
1082 size_t Loc
= Prefix
.data() - Buffer
.data();
1083 StringRef Skipped
= Buffer
.substr(0, Loc
);
1084 Buffer
= Buffer
.drop_front(Loc
);
1085 LineNumber
+= Skipped
.count('\n');
1087 // Check that the matched prefix isn't a suffix of some other check-like
1089 // FIXME: This is a very ad-hoc check. it would be better handled in some
1090 // other way. Among other things it seems hard to distinguish between
1091 // intentional and unintentional uses of this feature.
1092 if (Skipped
.empty() || !IsPartOfWord(Skipped
.back())) {
1093 // Now extract the type.
1094 StringRef AfterSuffix
;
1095 std::tie(CheckTy
, AfterSuffix
) = FindCheckType(Buffer
, Prefix
);
1097 // If we've found a valid check type for this prefix, we're done.
1098 if (CheckTy
!= Check::CheckNone
)
1099 return {Prefix
, AfterSuffix
};
1102 // If we didn't successfully find a prefix, we need to skip this invalid
1103 // prefix and continue scanning. We directly skip the prefix that was
1104 // matched and any additional parts of that check-like word.
1105 Buffer
= Buffer
.drop_front(SkipWord(Buffer
, Prefix
.size()));
1108 // We ran out of buffer while skipping partial matches so give up.
1109 return {StringRef(), StringRef()};
1112 void FileCheckPatternContext::createLineVariable() {
1113 assert(!LineVariable
&& "@LINE pseudo numeric variable already created");
1114 StringRef LineName
= "@LINE";
1115 LineVariable
= makeNumericVariable(LineName
);
1116 GlobalNumericVariableTable
[LineName
] = LineVariable
;
1119 bool FileCheck::ReadCheckFile(SourceMgr
&SM
, StringRef Buffer
, Regex
&PrefixRE
,
1120 std::vector
<FileCheckString
> &CheckStrings
) {
1122 PatternContext
.defineCmdlineVariables(Req
.GlobalDefines
, SM
);
1124 logAllUnhandledErrors(std::move(DefineError
), errs());
1128 PatternContext
.createLineVariable();
1130 std::vector
<FileCheckPattern
> ImplicitNegativeChecks
;
1131 for (const auto &PatternString
: Req
.ImplicitCheckNot
) {
1132 // Create a buffer with fake command line content in order to display the
1133 // command line option responsible for the specific implicit CHECK-NOT.
1134 std::string Prefix
= "-implicit-check-not='";
1135 std::string Suffix
= "'";
1136 std::unique_ptr
<MemoryBuffer
> CmdLine
= MemoryBuffer::getMemBufferCopy(
1137 Prefix
+ PatternString
+ Suffix
, "command line");
1139 StringRef PatternInBuffer
=
1140 CmdLine
->getBuffer().substr(Prefix
.size(), PatternString
.size());
1141 SM
.AddNewSourceBuffer(std::move(CmdLine
), SMLoc());
1143 ImplicitNegativeChecks
.push_back(
1144 FileCheckPattern(Check::CheckNot
, &PatternContext
));
1145 ImplicitNegativeChecks
.back().parsePattern(PatternInBuffer
,
1146 "IMPLICIT-CHECK", SM
, Req
);
1149 std::vector
<FileCheckPattern
> DagNotMatches
= ImplicitNegativeChecks
;
1151 // LineNumber keeps track of the line on which CheckPrefix instances are
1153 unsigned LineNumber
= 1;
1156 Check::FileCheckType CheckTy
;
1158 // See if a prefix occurs in the memory buffer.
1159 StringRef UsedPrefix
;
1160 StringRef AfterSuffix
;
1161 std::tie(UsedPrefix
, AfterSuffix
) =
1162 FindFirstMatchingPrefix(PrefixRE
, Buffer
, LineNumber
, CheckTy
);
1163 if (UsedPrefix
.empty())
1165 assert(UsedPrefix
.data() == Buffer
.data() &&
1166 "Failed to move Buffer's start forward, or pointed prefix outside "
1168 assert(AfterSuffix
.data() >= Buffer
.data() &&
1169 AfterSuffix
.data() < Buffer
.data() + Buffer
.size() &&
1170 "Parsing after suffix doesn't start inside of buffer!");
1172 // Location to use for error messages.
1173 const char *UsedPrefixStart
= UsedPrefix
.data();
1175 // Skip the buffer to the end of parsed suffix (or just prefix, if no good
1176 // suffix was processed).
1177 Buffer
= AfterSuffix
.empty() ? Buffer
.drop_front(UsedPrefix
.size())
1180 // Complain about useful-looking but unsupported suffixes.
1181 if (CheckTy
== Check::CheckBadNot
) {
1182 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.data()), SourceMgr::DK_Error
,
1183 "unsupported -NOT combo on prefix '" + UsedPrefix
+ "'");
1187 // Complain about invalid count specification.
1188 if (CheckTy
== Check::CheckBadCount
) {
1189 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.data()), SourceMgr::DK_Error
,
1190 "invalid count in -COUNT specification on prefix '" +
1195 // Okay, we found the prefix, yay. Remember the rest of the line, but ignore
1196 // leading whitespace.
1197 if (!(Req
.NoCanonicalizeWhiteSpace
&& Req
.MatchFullLines
))
1198 Buffer
= Buffer
.substr(Buffer
.find_first_not_of(" \t"));
1200 // Scan ahead to the end of line.
1201 size_t EOL
= Buffer
.find_first_of("\n\r");
1203 // Remember the location of the start of the pattern, for diagnostics.
1204 SMLoc PatternLoc
= SMLoc::getFromPointer(Buffer
.data());
1206 // Parse the pattern.
1207 FileCheckPattern
P(CheckTy
, &PatternContext
, LineNumber
);
1208 if (P
.parsePattern(Buffer
.substr(0, EOL
), UsedPrefix
, SM
, Req
))
1211 // Verify that CHECK-LABEL lines do not define or use variables
1212 if ((CheckTy
== Check::CheckLabel
) && P
.hasVariable()) {
1214 SMLoc::getFromPointer(UsedPrefixStart
), SourceMgr::DK_Error
,
1215 "found '" + UsedPrefix
+ "-LABEL:'"
1216 " with variable definition or use");
1220 Buffer
= Buffer
.substr(EOL
);
1222 // Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them.
1223 if ((CheckTy
== Check::CheckNext
|| CheckTy
== Check::CheckSame
||
1224 CheckTy
== Check::CheckEmpty
) &&
1225 CheckStrings
.empty()) {
1226 StringRef Type
= CheckTy
== Check::CheckNext
1228 : CheckTy
== Check::CheckEmpty
? "EMPTY" : "SAME";
1229 SM
.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart
),
1230 SourceMgr::DK_Error
,
1231 "found '" + UsedPrefix
+ "-" + Type
+
1232 "' without previous '" + UsedPrefix
+ ": line");
1236 // Handle CHECK-DAG/-NOT.
1237 if (CheckTy
== Check::CheckDAG
|| CheckTy
== Check::CheckNot
) {
1238 DagNotMatches
.push_back(P
);
1242 // Okay, add the string we captured to the output vector and move on.
1243 CheckStrings
.emplace_back(P
, UsedPrefix
, PatternLoc
);
1244 std::swap(DagNotMatches
, CheckStrings
.back().DagNotStrings
);
1245 DagNotMatches
= ImplicitNegativeChecks
;
1248 // Add an EOF pattern for any trailing CHECK-DAG/-NOTs, and use the first
1249 // prefix as a filler for the error message.
1250 if (!DagNotMatches
.empty()) {
1251 CheckStrings
.emplace_back(
1252 FileCheckPattern(Check::CheckEOF
, &PatternContext
, LineNumber
+ 1),
1253 *Req
.CheckPrefixes
.begin(), SMLoc::getFromPointer(Buffer
.data()));
1254 std::swap(DagNotMatches
, CheckStrings
.back().DagNotStrings
);
1257 if (CheckStrings
.empty()) {
1258 errs() << "error: no check strings found with prefix"
1259 << (Req
.CheckPrefixes
.size() > 1 ? "es " : " ");
1260 auto I
= Req
.CheckPrefixes
.begin();
1261 auto E
= Req
.CheckPrefixes
.end();
1263 errs() << "\'" << *I
<< ":'";
1267 errs() << ", \'" << *I
<< ":'";
1276 static void PrintMatch(bool ExpectedMatch
, const SourceMgr
&SM
,
1277 StringRef Prefix
, SMLoc Loc
, const FileCheckPattern
&Pat
,
1278 int MatchedCount
, StringRef Buffer
, size_t MatchPos
,
1279 size_t MatchLen
, const FileCheckRequest
&Req
,
1280 std::vector
<FileCheckDiag
> *Diags
) {
1281 bool PrintDiag
= true;
1282 if (ExpectedMatch
) {
1285 if (!Req
.VerboseVerbose
&& Pat
.getCheckTy() == Check::CheckEOF
)
1287 // Due to their verbosity, we don't print verbose diagnostics here if we're
1288 // gathering them for a different rendering, but we always print other
1292 SMRange MatchRange
= ProcessMatchResult(
1293 ExpectedMatch
? FileCheckDiag::MatchFoundAndExpected
1294 : FileCheckDiag::MatchFoundButExcluded
,
1295 SM
, Loc
, Pat
.getCheckTy(), Buffer
, MatchPos
, MatchLen
, Diags
);
1299 std::string Message
= formatv("{0}: {1} string found in input",
1300 Pat
.getCheckTy().getDescription(Prefix
),
1301 (ExpectedMatch
? "expected" : "excluded"))
1303 if (Pat
.getCount() > 1)
1304 Message
+= formatv(" ({0} out of {1})", MatchedCount
, Pat
.getCount()).str();
1307 Loc
, ExpectedMatch
? SourceMgr::DK_Remark
: SourceMgr::DK_Error
, Message
);
1308 SM
.PrintMessage(MatchRange
.Start
, SourceMgr::DK_Note
, "found here",
1310 Pat
.printSubstitutions(SM
, Buffer
, MatchRange
);
1313 static void PrintMatch(bool ExpectedMatch
, const SourceMgr
&SM
,
1314 const FileCheckString
&CheckStr
, int MatchedCount
,
1315 StringRef Buffer
, size_t MatchPos
, size_t MatchLen
,
1316 FileCheckRequest
&Req
,
1317 std::vector
<FileCheckDiag
> *Diags
) {
1318 PrintMatch(ExpectedMatch
, SM
, CheckStr
.Prefix
, CheckStr
.Loc
, CheckStr
.Pat
,
1319 MatchedCount
, Buffer
, MatchPos
, MatchLen
, Req
, Diags
);
1322 static void PrintNoMatch(bool ExpectedMatch
, const SourceMgr
&SM
,
1323 StringRef Prefix
, SMLoc Loc
,
1324 const FileCheckPattern
&Pat
, int MatchedCount
,
1325 StringRef Buffer
, bool VerboseVerbose
,
1326 std::vector
<FileCheckDiag
> *Diags
, Error MatchErrors
) {
1327 assert(MatchErrors
&& "Called on successful match");
1328 bool PrintDiag
= true;
1329 if (!ExpectedMatch
) {
1330 if (!VerboseVerbose
) {
1331 consumeError(std::move(MatchErrors
));
1334 // Due to their verbosity, we don't print verbose diagnostics here if we're
1335 // gathering them for a different rendering, but we always print other
1340 // If the current position is at the end of a line, advance to the start of
1342 Buffer
= Buffer
.substr(Buffer
.find_first_not_of(" \t\n\r"));
1343 SMRange SearchRange
= ProcessMatchResult(
1344 ExpectedMatch
? FileCheckDiag::MatchNoneButExpected
1345 : FileCheckDiag::MatchNoneAndExcluded
,
1346 SM
, Loc
, Pat
.getCheckTy(), Buffer
, 0, Buffer
.size(), Diags
);
1348 consumeError(std::move(MatchErrors
));
1353 handleErrors(std::move(MatchErrors
),
1354 [](const FileCheckErrorDiagnostic
&E
) { E
.log(errs()); });
1356 // No problem matching the string per se.
1359 consumeError(std::move(MatchErrors
));
1361 // Print "not found" diagnostic.
1362 std::string Message
= formatv("{0}: {1} string not found in input",
1363 Pat
.getCheckTy().getDescription(Prefix
),
1364 (ExpectedMatch
? "expected" : "excluded"))
1366 if (Pat
.getCount() > 1)
1367 Message
+= formatv(" ({0} out of {1})", MatchedCount
, Pat
.getCount()).str();
1369 Loc
, ExpectedMatch
? SourceMgr::DK_Error
: SourceMgr::DK_Remark
, Message
);
1371 // Print the "scanning from here" line.
1372 SM
.PrintMessage(SearchRange
.Start
, SourceMgr::DK_Note
, "scanning from here");
1374 // Allow the pattern to print additional information if desired.
1375 Pat
.printSubstitutions(SM
, Buffer
);
1378 Pat
.printFuzzyMatch(SM
, Buffer
, Diags
);
1381 static void PrintNoMatch(bool ExpectedMatch
, const SourceMgr
&SM
,
1382 const FileCheckString
&CheckStr
, int MatchedCount
,
1383 StringRef Buffer
, bool VerboseVerbose
,
1384 std::vector
<FileCheckDiag
> *Diags
, Error MatchErrors
) {
1385 PrintNoMatch(ExpectedMatch
, SM
, CheckStr
.Prefix
, CheckStr
.Loc
, CheckStr
.Pat
,
1386 MatchedCount
, Buffer
, VerboseVerbose
, Diags
,
1387 std::move(MatchErrors
));
1390 /// Counts the number of newlines in the specified range.
1391 static unsigned CountNumNewlinesBetween(StringRef Range
,
1392 const char *&FirstNewLine
) {
1393 unsigned NumNewLines
= 0;
1395 // Scan for newline.
1396 Range
= Range
.substr(Range
.find_first_of("\n\r"));
1402 // Handle \n\r and \r\n as a single newline.
1403 if (Range
.size() > 1 && (Range
[1] == '\n' || Range
[1] == '\r') &&
1404 (Range
[0] != Range
[1]))
1405 Range
= Range
.substr(1);
1406 Range
= Range
.substr(1);
1408 if (NumNewLines
== 1)
1409 FirstNewLine
= Range
.begin();
1413 size_t FileCheckString::Check(const SourceMgr
&SM
, StringRef Buffer
,
1414 bool IsLabelScanMode
, size_t &MatchLen
,
1415 FileCheckRequest
&Req
,
1416 std::vector
<FileCheckDiag
> *Diags
) const {
1418 std::vector
<const FileCheckPattern
*> NotStrings
;
1420 // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL
1421 // bounds; we have not processed variable definitions within the bounded block
1422 // yet so cannot handle any final CHECK-DAG yet; this is handled when going
1423 // over the block again (including the last CHECK-LABEL) in normal mode.
1424 if (!IsLabelScanMode
) {
1425 // Match "dag strings" (with mixed "not strings" if any).
1426 LastPos
= CheckDag(SM
, Buffer
, NotStrings
, Req
, Diags
);
1427 if (LastPos
== StringRef::npos
)
1428 return StringRef::npos
;
1431 // Match itself from the last position after matching CHECK-DAG.
1432 size_t LastMatchEnd
= LastPos
;
1433 size_t FirstMatchPos
= 0;
1434 // Go match the pattern Count times. Majority of patterns only match with
1436 assert(Pat
.getCount() != 0 && "pattern count can not be zero");
1437 for (int i
= 1; i
<= Pat
.getCount(); i
++) {
1438 StringRef MatchBuffer
= Buffer
.substr(LastMatchEnd
);
1439 size_t CurrentMatchLen
;
1440 // get a match at current start point
1441 Expected
<size_t> MatchResult
= Pat
.match(MatchBuffer
, CurrentMatchLen
, SM
);
1445 PrintNoMatch(true, SM
, *this, i
, MatchBuffer
, Req
.VerboseVerbose
, Diags
,
1446 MatchResult
.takeError());
1447 return StringRef::npos
;
1449 size_t MatchPos
= *MatchResult
;
1450 PrintMatch(true, SM
, *this, i
, MatchBuffer
, MatchPos
, CurrentMatchLen
, Req
,
1453 FirstMatchPos
= LastPos
+ MatchPos
;
1455 // move start point after the match
1456 LastMatchEnd
+= MatchPos
+ CurrentMatchLen
;
1458 // Full match len counts from first match pos.
1459 MatchLen
= LastMatchEnd
- FirstMatchPos
;
1461 // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT
1463 if (!IsLabelScanMode
) {
1464 size_t MatchPos
= FirstMatchPos
- LastPos
;
1465 StringRef MatchBuffer
= Buffer
.substr(LastPos
);
1466 StringRef SkippedRegion
= Buffer
.substr(LastPos
, MatchPos
);
1468 // If this check is a "CHECK-NEXT", verify that the previous match was on
1469 // the previous line (i.e. that there is one newline between them).
1470 if (CheckNext(SM
, SkippedRegion
)) {
1471 ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine
, SM
, Loc
,
1472 Pat
.getCheckTy(), MatchBuffer
, MatchPos
, MatchLen
,
1473 Diags
, Req
.Verbose
);
1474 return StringRef::npos
;
1477 // If this check is a "CHECK-SAME", verify that the previous match was on
1478 // the same line (i.e. that there is no newline between them).
1479 if (CheckSame(SM
, SkippedRegion
)) {
1480 ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine
, SM
, Loc
,
1481 Pat
.getCheckTy(), MatchBuffer
, MatchPos
, MatchLen
,
1482 Diags
, Req
.Verbose
);
1483 return StringRef::npos
;
1486 // If this match had "not strings", verify that they don't exist in the
1488 if (CheckNot(SM
, SkippedRegion
, NotStrings
, Req
, Diags
))
1489 return StringRef::npos
;
1492 return FirstMatchPos
;
1495 bool FileCheckString::CheckNext(const SourceMgr
&SM
, StringRef Buffer
) const {
1496 if (Pat
.getCheckTy() != Check::CheckNext
&&
1497 Pat
.getCheckTy() != Check::CheckEmpty
)
1502 Twine(Pat
.getCheckTy() == Check::CheckEmpty
? "-EMPTY" : "-NEXT");
1504 // Count the number of newlines between the previous match and this one.
1505 const char *FirstNewLine
= nullptr;
1506 unsigned NumNewLines
= CountNumNewlinesBetween(Buffer
, FirstNewLine
);
1508 if (NumNewLines
== 0) {
1509 SM
.PrintMessage(Loc
, SourceMgr::DK_Error
,
1510 CheckName
+ ": is on the same line as previous match");
1511 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.end()), SourceMgr::DK_Note
,
1512 "'next' match was here");
1513 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.data()), SourceMgr::DK_Note
,
1514 "previous match ended here");
1518 if (NumNewLines
!= 1) {
1519 SM
.PrintMessage(Loc
, SourceMgr::DK_Error
,
1521 ": is not on the line after the previous match");
1522 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.end()), SourceMgr::DK_Note
,
1523 "'next' match was here");
1524 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.data()), SourceMgr::DK_Note
,
1525 "previous match ended here");
1526 SM
.PrintMessage(SMLoc::getFromPointer(FirstNewLine
), SourceMgr::DK_Note
,
1527 "non-matching line after previous match is here");
1534 bool FileCheckString::CheckSame(const SourceMgr
&SM
, StringRef Buffer
) const {
1535 if (Pat
.getCheckTy() != Check::CheckSame
)
1538 // Count the number of newlines between the previous match and this one.
1539 const char *FirstNewLine
= nullptr;
1540 unsigned NumNewLines
= CountNumNewlinesBetween(Buffer
, FirstNewLine
);
1542 if (NumNewLines
!= 0) {
1543 SM
.PrintMessage(Loc
, SourceMgr::DK_Error
,
1545 "-SAME: is not on the same line as the previous match");
1546 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.end()), SourceMgr::DK_Note
,
1547 "'next' match was here");
1548 SM
.PrintMessage(SMLoc::getFromPointer(Buffer
.data()), SourceMgr::DK_Note
,
1549 "previous match ended here");
1556 bool FileCheckString::CheckNot(
1557 const SourceMgr
&SM
, StringRef Buffer
,
1558 const std::vector
<const FileCheckPattern
*> &NotStrings
,
1559 const FileCheckRequest
&Req
, std::vector
<FileCheckDiag
> *Diags
) const {
1560 for (const FileCheckPattern
*Pat
: NotStrings
) {
1561 assert((Pat
->getCheckTy() == Check::CheckNot
) && "Expect CHECK-NOT!");
1563 size_t MatchLen
= 0;
1564 Expected
<size_t> MatchResult
= Pat
->match(Buffer
, MatchLen
, SM
);
1567 PrintNoMatch(false, SM
, Prefix
, Pat
->getLoc(), *Pat
, 1, Buffer
,
1568 Req
.VerboseVerbose
, Diags
, MatchResult
.takeError());
1571 size_t Pos
= *MatchResult
;
1573 PrintMatch(false, SM
, Prefix
, Pat
->getLoc(), *Pat
, 1, Buffer
, Pos
, MatchLen
,
1583 FileCheckString::CheckDag(const SourceMgr
&SM
, StringRef Buffer
,
1584 std::vector
<const FileCheckPattern
*> &NotStrings
,
1585 const FileCheckRequest
&Req
,
1586 std::vector
<FileCheckDiag
> *Diags
) const {
1587 if (DagNotStrings
.empty())
1590 // The start of the search range.
1591 size_t StartPos
= 0;
1597 // A sorted list of ranges for non-overlapping CHECK-DAG matches. Match
1598 // ranges are erased from this list once they are no longer in the search
1600 std::list
<MatchRange
> MatchRanges
;
1602 // We need PatItr and PatEnd later for detecting the end of a CHECK-DAG
1603 // group, so we don't use a range-based for loop here.
1604 for (auto PatItr
= DagNotStrings
.begin(), PatEnd
= DagNotStrings
.end();
1605 PatItr
!= PatEnd
; ++PatItr
) {
1606 const FileCheckPattern
&Pat
= *PatItr
;
1607 assert((Pat
.getCheckTy() == Check::CheckDAG
||
1608 Pat
.getCheckTy() == Check::CheckNot
) &&
1609 "Invalid CHECK-DAG or CHECK-NOT!");
1611 if (Pat
.getCheckTy() == Check::CheckNot
) {
1612 NotStrings
.push_back(&Pat
);
1616 assert((Pat
.getCheckTy() == Check::CheckDAG
) && "Expect CHECK-DAG!");
1618 // CHECK-DAG always matches from the start.
1619 size_t MatchLen
= 0, MatchPos
= StartPos
;
1621 // Search for a match that doesn't overlap a previous match in this
1623 for (auto MI
= MatchRanges
.begin(), ME
= MatchRanges
.end(); true; ++MI
) {
1624 StringRef MatchBuffer
= Buffer
.substr(MatchPos
);
1625 Expected
<size_t> MatchResult
= Pat
.match(MatchBuffer
, MatchLen
, SM
);
1626 // With a group of CHECK-DAGs, a single mismatching means the match on
1627 // that group of CHECK-DAGs fails immediately.
1629 PrintNoMatch(true, SM
, Prefix
, Pat
.getLoc(), Pat
, 1, MatchBuffer
,
1630 Req
.VerboseVerbose
, Diags
, MatchResult
.takeError());
1631 return StringRef::npos
;
1633 size_t MatchPosBuf
= *MatchResult
;
1634 // Re-calc it as the offset relative to the start of the original string.
1635 MatchPos
+= MatchPosBuf
;
1636 if (Req
.VerboseVerbose
)
1637 PrintMatch(true, SM
, Prefix
, Pat
.getLoc(), Pat
, 1, Buffer
, MatchPos
,
1638 MatchLen
, Req
, Diags
);
1639 MatchRange M
{MatchPos
, MatchPos
+ MatchLen
};
1640 if (Req
.AllowDeprecatedDagOverlap
) {
1641 // We don't need to track all matches in this mode, so we just maintain
1642 // one match range that encompasses the current CHECK-DAG group's
1644 if (MatchRanges
.empty())
1645 MatchRanges
.insert(MatchRanges
.end(), M
);
1647 auto Block
= MatchRanges
.begin();
1648 Block
->Pos
= std::min(Block
->Pos
, M
.Pos
);
1649 Block
->End
= std::max(Block
->End
, M
.End
);
1653 // Iterate previous matches until overlapping match or insertion point.
1654 bool Overlap
= false;
1655 for (; MI
!= ME
; ++MI
) {
1656 if (M
.Pos
< MI
->End
) {
1657 // !Overlap => New match has no overlap and is before this old match.
1658 // Overlap => New match overlaps this old match.
1659 Overlap
= MI
->Pos
< M
.End
;
1664 // Insert non-overlapping match into list.
1665 MatchRanges
.insert(MI
, M
);
1668 if (Req
.VerboseVerbose
) {
1669 // Due to their verbosity, we don't print verbose diagnostics here if
1670 // we're gathering them for a different rendering, but we always print
1671 // other diagnostics.
1673 SMLoc OldStart
= SMLoc::getFromPointer(Buffer
.data() + MI
->Pos
);
1674 SMLoc OldEnd
= SMLoc::getFromPointer(Buffer
.data() + MI
->End
);
1675 SMRange
OldRange(OldStart
, OldEnd
);
1676 SM
.PrintMessage(OldStart
, SourceMgr::DK_Note
,
1677 "match discarded, overlaps earlier DAG match here",
1680 Diags
->rbegin()->MatchTy
= FileCheckDiag::MatchFoundButDiscarded
;
1684 if (!Req
.VerboseVerbose
)
1685 PrintMatch(true, SM
, Prefix
, Pat
.getLoc(), Pat
, 1, Buffer
, MatchPos
,
1686 MatchLen
, Req
, Diags
);
1688 // Handle the end of a CHECK-DAG group.
1689 if (std::next(PatItr
) == PatEnd
||
1690 std::next(PatItr
)->getCheckTy() == Check::CheckNot
) {
1691 if (!NotStrings
.empty()) {
1692 // If there are CHECK-NOTs between two CHECK-DAGs or from CHECK to
1693 // CHECK-DAG, verify that there are no 'not' strings occurred in that
1695 StringRef SkippedRegion
=
1696 Buffer
.slice(StartPos
, MatchRanges
.begin()->Pos
);
1697 if (CheckNot(SM
, SkippedRegion
, NotStrings
, Req
, Diags
))
1698 return StringRef::npos
;
1699 // Clear "not strings".
1702 // All subsequent CHECK-DAGs and CHECK-NOTs should be matched from the
1703 // end of this CHECK-DAG group's match range.
1704 StartPos
= MatchRanges
.rbegin()->End
;
1705 // Don't waste time checking for (impossible) overlaps before that.
1706 MatchRanges
.clear();
1713 // A check prefix must contain only alphanumeric, hyphens and underscores.
1714 static bool ValidateCheckPrefix(StringRef CheckPrefix
) {
1715 Regex
Validator("^[a-zA-Z0-9_-]*$");
1716 return Validator
.match(CheckPrefix
);
1719 bool FileCheck::ValidateCheckPrefixes() {
1720 StringSet
<> PrefixSet
;
1722 for (StringRef Prefix
: Req
.CheckPrefixes
) {
1723 // Reject empty prefixes.
1727 if (!PrefixSet
.insert(Prefix
).second
)
1730 if (!ValidateCheckPrefix(Prefix
))
1737 Regex
FileCheck::buildCheckPrefixRegex() {
1738 // I don't think there's a way to specify an initial value for cl::list,
1739 // so if nothing was specified, add the default
1740 if (Req
.CheckPrefixes
.empty())
1741 Req
.CheckPrefixes
.push_back("CHECK");
1743 // We already validated the contents of CheckPrefixes so just concatenate
1744 // them as alternatives.
1745 SmallString
<32> PrefixRegexStr
;
1746 for (StringRef Prefix
: Req
.CheckPrefixes
) {
1747 if (Prefix
!= Req
.CheckPrefixes
.front())
1748 PrefixRegexStr
.push_back('|');
1750 PrefixRegexStr
.append(Prefix
);
1753 return Regex(PrefixRegexStr
);
1756 Error
FileCheckPatternContext::defineCmdlineVariables(
1757 std::vector
<std::string
> &CmdlineDefines
, SourceMgr
&SM
) {
1758 assert(GlobalVariableTable
.empty() && GlobalNumericVariableTable
.empty() &&
1759 "Overriding defined variable with command-line variable definitions");
1761 if (CmdlineDefines
.empty())
1762 return Error::success();
1764 // Create a string representing the vector of command-line definitions. Each
1765 // definition is on its own line and prefixed with a definition number to
1766 // clarify which definition a given diagnostic corresponds to.
1768 Error Errs
= Error::success();
1769 std::string CmdlineDefsDiag
;
1770 SmallVector
<std::pair
<size_t, size_t>, 4> CmdlineDefsIndices
;
1771 for (StringRef CmdlineDef
: CmdlineDefines
) {
1772 std::string DefPrefix
= ("Global define #" + Twine(++I
) + ": ").str();
1773 size_t EqIdx
= CmdlineDef
.find('=');
1774 if (EqIdx
== StringRef::npos
) {
1775 CmdlineDefsIndices
.push_back(std::make_pair(CmdlineDefsDiag
.size(), 0));
1778 // Numeric variable definition.
1779 if (CmdlineDef
[0] == '#') {
1780 // Append a copy of the command-line definition adapted to use the same
1781 // format as in the input file to be able to reuse
1782 // parseNumericSubstitutionBlock.
1783 CmdlineDefsDiag
+= (DefPrefix
+ CmdlineDef
+ " (parsed as: [[").str();
1784 std::string SubstitutionStr
= CmdlineDef
;
1785 SubstitutionStr
[EqIdx
] = ':';
1786 CmdlineDefsIndices
.push_back(
1787 std::make_pair(CmdlineDefsDiag
.size(), SubstitutionStr
.size()));
1788 CmdlineDefsDiag
+= (SubstitutionStr
+ Twine("]])\n")).str();
1790 CmdlineDefsDiag
+= DefPrefix
;
1791 CmdlineDefsIndices
.push_back(
1792 std::make_pair(CmdlineDefsDiag
.size(), CmdlineDef
.size()));
1793 CmdlineDefsDiag
+= (CmdlineDef
+ "\n").str();
1797 // Create a buffer with fake command line content in order to display
1798 // parsing diagnostic with location information and point to the
1799 // global definition with invalid syntax.
1800 std::unique_ptr
<MemoryBuffer
> CmdLineDefsDiagBuffer
=
1801 MemoryBuffer::getMemBufferCopy(CmdlineDefsDiag
, "Global defines");
1802 StringRef CmdlineDefsDiagRef
= CmdLineDefsDiagBuffer
->getBuffer();
1803 SM
.AddNewSourceBuffer(std::move(CmdLineDefsDiagBuffer
), SMLoc());
1805 for (std::pair
<size_t, size_t> CmdlineDefIndices
: CmdlineDefsIndices
) {
1806 StringRef CmdlineDef
= CmdlineDefsDiagRef
.substr(CmdlineDefIndices
.first
,
1807 CmdlineDefIndices
.second
);
1808 if (CmdlineDef
.empty()) {
1811 FileCheckErrorDiagnostic::get(
1812 SM
, CmdlineDef
, "missing equal sign in global definition"));
1816 // Numeric variable definition.
1817 if (CmdlineDef
[0] == '#') {
1818 // Now parse the definition both to check that the syntax is correct and
1819 // to create the necessary class instance.
1820 StringRef CmdlineDefExpr
= CmdlineDef
.substr(1);
1821 Optional
<FileCheckNumericVariable
*> DefinedNumericVariable
;
1822 Expected
<std::unique_ptr
<FileCheckExpressionAST
>> ExpressionASTResult
=
1823 FileCheckPattern::parseNumericSubstitutionBlock(
1824 CmdlineDefExpr
, DefinedNumericVariable
, false, None
, this, SM
);
1825 if (!ExpressionASTResult
) {
1826 Errs
= joinErrors(std::move(Errs
), ExpressionASTResult
.takeError());
1829 std::unique_ptr
<FileCheckExpressionAST
> ExpressionAST
=
1830 std::move(*ExpressionASTResult
);
1831 // Now evaluate the expression whose value this variable should be set
1832 // to, since the expression of a command-line variable definition should
1833 // only use variables defined earlier on the command-line. If not, this
1834 // is an error and we report it.
1835 Expected
<uint64_t> Value
= ExpressionAST
->eval();
1837 Errs
= joinErrors(std::move(Errs
), Value
.takeError());
1841 assert(DefinedNumericVariable
&& "No variable defined");
1842 (*DefinedNumericVariable
)->setValue(*Value
);
1844 // Record this variable definition.
1845 GlobalNumericVariableTable
[(*DefinedNumericVariable
)->getName()] =
1846 *DefinedNumericVariable
;
1848 // String variable definition.
1849 std::pair
<StringRef
, StringRef
> CmdlineNameVal
= CmdlineDef
.split('=');
1850 StringRef CmdlineName
= CmdlineNameVal
.first
;
1851 StringRef OrigCmdlineName
= CmdlineName
;
1852 Expected
<FileCheckPattern::VariableProperties
> ParseVarResult
=
1853 FileCheckPattern::parseVariable(CmdlineName
, SM
);
1854 if (!ParseVarResult
) {
1855 Errs
= joinErrors(std::move(Errs
), ParseVarResult
.takeError());
1858 // Check that CmdlineName does not denote a pseudo variable is only
1859 // composed of the parsed numeric variable. This catches cases like
1860 // "FOO+2" in a "FOO+2=10" definition.
1861 if (ParseVarResult
->IsPseudo
|| !CmdlineName
.empty()) {
1862 Errs
= joinErrors(std::move(Errs
),
1863 FileCheckErrorDiagnostic::get(
1864 SM
, OrigCmdlineName
,
1865 "invalid name in string variable definition '" +
1866 OrigCmdlineName
+ "'"));
1869 StringRef Name
= ParseVarResult
->Name
;
1871 // Detect collisions between string and numeric variables when the former
1872 // is created later than the latter.
1873 if (GlobalNumericVariableTable
.find(Name
) !=
1874 GlobalNumericVariableTable
.end()) {
1875 Errs
= joinErrors(std::move(Errs
), FileCheckErrorDiagnostic::get(
1877 "numeric variable with name '" +
1878 Name
+ "' already exists"));
1881 GlobalVariableTable
.insert(CmdlineNameVal
);
1882 // Mark the string variable as defined to detect collisions between
1883 // string and numeric variables in defineCmdlineVariables when the latter
1884 // is created later than the former. We cannot reuse GlobalVariableTable
1885 // for this by populating it with an empty string since we would then
1886 // lose the ability to detect the use of an undefined variable in
1888 DefinedVariableTable
[Name
] = true;
1895 void FileCheckPatternContext::clearLocalVars() {
1896 SmallVector
<StringRef
, 16> LocalPatternVars
, LocalNumericVars
;
1897 for (const StringMapEntry
<StringRef
> &Var
: GlobalVariableTable
)
1898 if (Var
.first()[0] != '$')
1899 LocalPatternVars
.push_back(Var
.first());
1901 // Numeric substitution reads the value of a variable directly, not via
1902 // GlobalNumericVariableTable. Therefore, we clear local variables by
1903 // clearing their value which will lead to a numeric substitution failure. We
1904 // also mark the variable for removal from GlobalNumericVariableTable since
1905 // this is what defineCmdlineVariables checks to decide that no global
1906 // variable has been defined.
1907 for (const auto &Var
: GlobalNumericVariableTable
)
1908 if (Var
.first()[0] != '$') {
1909 Var
.getValue()->clearValue();
1910 LocalNumericVars
.push_back(Var
.first());
1913 for (const auto &Var
: LocalPatternVars
)
1914 GlobalVariableTable
.erase(Var
);
1915 for (const auto &Var
: LocalNumericVars
)
1916 GlobalNumericVariableTable
.erase(Var
);
1919 bool FileCheck::CheckInput(SourceMgr
&SM
, StringRef Buffer
,
1920 ArrayRef
<FileCheckString
> CheckStrings
,
1921 std::vector
<FileCheckDiag
> *Diags
) {
1922 bool ChecksFailed
= false;
1924 unsigned i
= 0, j
= 0, e
= CheckStrings
.size();
1926 StringRef CheckRegion
;
1928 CheckRegion
= Buffer
;
1930 const FileCheckString
&CheckLabelStr
= CheckStrings
[j
];
1931 if (CheckLabelStr
.Pat
.getCheckTy() != Check::CheckLabel
) {
1936 // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG
1937 size_t MatchLabelLen
= 0;
1938 size_t MatchLabelPos
=
1939 CheckLabelStr
.Check(SM
, Buffer
, true, MatchLabelLen
, Req
, Diags
);
1940 if (MatchLabelPos
== StringRef::npos
)
1941 // Immediately bail if CHECK-LABEL fails, nothing else we can do.
1944 CheckRegion
= Buffer
.substr(0, MatchLabelPos
+ MatchLabelLen
);
1945 Buffer
= Buffer
.substr(MatchLabelPos
+ MatchLabelLen
);
1949 // Do not clear the first region as it's the one before the first
1950 // CHECK-LABEL and it would clear variables defined on the command-line
1951 // before they get used.
1952 if (i
!= 0 && Req
.EnableVarScope
)
1953 PatternContext
.clearLocalVars();
1955 for (; i
!= j
; ++i
) {
1956 const FileCheckString
&CheckStr
= CheckStrings
[i
];
1958 // Check each string within the scanned region, including a second check
1959 // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG)
1960 size_t MatchLen
= 0;
1962 CheckStr
.Check(SM
, CheckRegion
, false, MatchLen
, Req
, Diags
);
1964 if (MatchPos
== StringRef::npos
) {
1965 ChecksFailed
= true;
1970 CheckRegion
= CheckRegion
.substr(MatchPos
+ MatchLen
);
1977 // Success if no checks failed.
1978 return !ChecksFailed
;