1 //===-- FileCheckImpl.h - Private FileCheck Interface ------------*- C++ -*-==//
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 // This file defines the private interfaces of FileCheck. Its purpose is to
10 // allow unit testing of FileCheck and to separate the interface from the
11 // implementation. It is only meant to be used by FileCheck.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_FILECHECK_FILECHECKIMPL_H
16 #define LLVM_LIB_FILECHECK_FILECHECKIMPL_H
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/FileCheck/FileCheck.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/SourceMgr.h"
30 //===----------------------------------------------------------------------===//
31 // Numeric substitution handling code.
32 //===----------------------------------------------------------------------===//
34 class ExpressionValue
;
36 /// Type representing the format an expression value should be textualized into
37 /// for matching. Used to represent both explicit format specifiers as well as
38 /// implicit format from using numeric variables.
39 struct ExpressionFormat
{
41 /// Denote absence of format. Used for implicit format of literals and
42 /// empty expressions.
44 /// Value is an unsigned integer and should be printed as a decimal number.
46 /// Value is a signed integer and should be printed as a decimal number.
48 /// Value should be printed as an uppercase hex number.
50 /// Value should be printed as a lowercase hex number.
56 unsigned Precision
= 0;
57 /// printf-like "alternate form" selected.
58 bool AlternateForm
= false;
61 /// Evaluates a format to true if it can be used in a match.
62 explicit operator bool() const { return Value
!= Kind::NoFormat
; }
64 /// Define format equality: formats are equal if neither is NoFormat and
65 /// their kinds and precision are the same.
66 bool operator==(const ExpressionFormat
&Other
) const {
67 return Value
!= Kind::NoFormat
&& Value
== Other
.Value
&&
68 Precision
== Other
.Precision
&& AlternateForm
== Other
.AlternateForm
;
71 bool operator!=(const ExpressionFormat
&Other
) const {
72 return !(*this == Other
);
75 bool operator==(Kind OtherValue
) const { return Value
== OtherValue
; }
77 bool operator!=(Kind OtherValue
) const { return !(*this == OtherValue
); }
79 /// \returns the format specifier corresponding to this format as a string.
80 StringRef
toString() const;
82 ExpressionFormat() : Value(Kind::NoFormat
){};
83 explicit ExpressionFormat(Kind Value
) : Value(Value
), Precision(0){};
84 explicit ExpressionFormat(Kind Value
, unsigned Precision
)
85 : Value(Value
), Precision(Precision
){};
86 explicit ExpressionFormat(Kind Value
, unsigned Precision
, bool AlternateForm
)
87 : Value(Value
), Precision(Precision
), AlternateForm(AlternateForm
){};
89 /// \returns a wildcard regular expression string that matches any value in
90 /// the format represented by this instance and no other value, or an error
91 /// if the format is NoFormat.
92 Expected
<std::string
> getWildcardRegex() const;
94 /// \returns the string representation of \p Value in the format represented
95 /// by this instance, or an error if conversion to this format failed or the
96 /// format is NoFormat.
97 Expected
<std::string
> getMatchingString(ExpressionValue Value
) const;
99 /// \returns the value corresponding to string representation \p StrVal
100 /// according to the matching format represented by this instance or an error
101 /// with diagnostic against \p SM if \p StrVal does not correspond to a valid
102 /// and representable value.
103 Expected
<ExpressionValue
> valueFromStringRepr(StringRef StrVal
,
104 const SourceMgr
&SM
) const;
107 /// Class to represent an overflow error that might result when manipulating a
109 class OverflowError
: public ErrorInfo
<OverflowError
> {
113 std::error_code
convertToErrorCode() const override
{
114 return std::make_error_code(std::errc::value_too_large
);
117 void log(raw_ostream
&OS
) const override
{ OS
<< "overflow error"; }
120 /// Class representing a numeric value.
121 class ExpressionValue
{
128 explicit ExpressionValue(T Val
) : Value(Val
), Negative(Val
< 0) {}
130 bool operator==(const ExpressionValue
&Other
) const {
131 return Value
== Other
.Value
&& isNegative() == Other
.isNegative();
134 bool operator!=(const ExpressionValue
&Other
) const {
135 return !(*this == Other
);
138 /// Returns true if value is signed and negative, false otherwise.
139 bool isNegative() const {
140 assert((Value
!= 0 || !Negative
) && "Unexpected negative zero!");
144 /// \returns the value as a signed integer or an error if the value is out of
146 Expected
<int64_t> getSignedValue() const;
148 /// \returns the value as an unsigned integer or an error if the value is out
150 Expected
<uint64_t> getUnsignedValue() const;
152 /// \returns an unsigned ExpressionValue instance whose value is the absolute
153 /// value to this object's value.
154 ExpressionValue
getAbsolute() const;
157 /// Performs operation and \returns its result or an error in case of failure,
158 /// such as if an overflow occurs.
159 Expected
<ExpressionValue
> operator+(const ExpressionValue
&Lhs
,
160 const ExpressionValue
&Rhs
);
161 Expected
<ExpressionValue
> operator-(const ExpressionValue
&Lhs
,
162 const ExpressionValue
&Rhs
);
163 Expected
<ExpressionValue
> operator*(const ExpressionValue
&Lhs
,
164 const ExpressionValue
&Rhs
);
165 Expected
<ExpressionValue
> operator/(const ExpressionValue
&Lhs
,
166 const ExpressionValue
&Rhs
);
167 Expected
<ExpressionValue
> max(const ExpressionValue
&Lhs
,
168 const ExpressionValue
&Rhs
);
169 Expected
<ExpressionValue
> min(const ExpressionValue
&Lhs
,
170 const ExpressionValue
&Rhs
);
172 /// Base class representing the AST of a given expression.
173 class ExpressionAST
{
175 StringRef ExpressionStr
;
178 ExpressionAST(StringRef ExpressionStr
) : ExpressionStr(ExpressionStr
) {}
180 virtual ~ExpressionAST() = default;
182 StringRef
getExpressionStr() const { return ExpressionStr
; }
184 /// Evaluates and \returns the value of the expression represented by this
185 /// AST or an error if evaluation fails.
186 virtual Expected
<ExpressionValue
> eval() const = 0;
188 /// \returns either the implicit format of this AST, a diagnostic against
189 /// \p SM if implicit formats of the AST's components conflict, or NoFormat
190 /// if the AST has no implicit format (e.g. AST is made up of a single
192 virtual Expected
<ExpressionFormat
>
193 getImplicitFormat(const SourceMgr
&SM
) const {
194 return ExpressionFormat();
198 /// Class representing an unsigned literal in the AST of an expression.
199 class ExpressionLiteral
: public ExpressionAST
{
201 /// Actual value of the literal.
202 ExpressionValue Value
;
206 explicit ExpressionLiteral(StringRef ExpressionStr
, T Val
)
207 : ExpressionAST(ExpressionStr
), Value(Val
) {}
209 /// \returns the literal's value.
210 Expected
<ExpressionValue
> eval() const override
{ return Value
; }
213 /// Class to represent an undefined variable error, which quotes that
214 /// variable's name when printed.
215 class UndefVarError
: public ErrorInfo
<UndefVarError
> {
222 UndefVarError(StringRef VarName
) : VarName(VarName
) {}
224 StringRef
getVarName() const { return VarName
; }
226 std::error_code
convertToErrorCode() const override
{
227 return inconvertibleErrorCode();
230 /// Print name of variable associated with this error.
231 void log(raw_ostream
&OS
) const override
{
232 OS
<< "undefined variable: " << VarName
;
236 /// Class representing an expression and its matching format.
239 /// Pointer to AST of the expression.
240 std::unique_ptr
<ExpressionAST
> AST
;
242 /// Format to use (e.g. hex upper case letters) when matching the value.
243 ExpressionFormat Format
;
246 /// Generic constructor for an expression represented by the given \p AST and
247 /// whose matching format is \p Format.
248 Expression(std::unique_ptr
<ExpressionAST
> AST
, ExpressionFormat Format
)
249 : AST(std::move(AST
)), Format(Format
) {}
251 /// \returns pointer to AST of the expression. Pointer is guaranteed to be
252 /// valid as long as this object is.
253 ExpressionAST
*getAST() const { return AST
.get(); }
255 ExpressionFormat
getFormat() const { return Format
; }
258 /// Class representing a numeric variable and its associated current value.
259 class NumericVariable
{
261 /// Name of the numeric variable.
264 /// Format to use for expressions using this variable without an explicit
266 ExpressionFormat ImplicitFormat
;
268 /// Value of numeric variable, if defined, or None otherwise.
269 Optional
<ExpressionValue
> Value
;
271 /// The input buffer's string from which Value was parsed, or None. See
272 /// comments on getStringValue for a discussion of the None case.
273 Optional
<StringRef
> StrValue
;
275 /// Line number where this variable is defined, or None if defined before
276 /// input is parsed. Used to determine whether a variable is defined on the
277 /// same line as a given use.
278 Optional
<size_t> DefLineNumber
;
281 /// Constructor for a variable \p Name with implicit format \p ImplicitFormat
282 /// defined at line \p DefLineNumber or defined before input is parsed if
283 /// \p DefLineNumber is None.
284 explicit NumericVariable(StringRef Name
, ExpressionFormat ImplicitFormat
,
285 Optional
<size_t> DefLineNumber
= None
)
286 : Name(Name
), ImplicitFormat(ImplicitFormat
),
287 DefLineNumber(DefLineNumber
) {}
289 /// \returns name of this numeric variable.
290 StringRef
getName() const { return Name
; }
292 /// \returns implicit format of this numeric variable.
293 ExpressionFormat
getImplicitFormat() const { return ImplicitFormat
; }
295 /// \returns this variable's value.
296 Optional
<ExpressionValue
> getValue() const { return Value
; }
298 /// \returns the input buffer's string from which this variable's value was
299 /// parsed, or None if the value is not yet defined or was not parsed from the
300 /// input buffer. For example, the value of @LINE is not parsed from the
301 /// input buffer, and some numeric variables are parsed from the command
303 Optional
<StringRef
> getStringValue() const { return StrValue
; }
305 /// Sets value of this numeric variable to \p NewValue, and sets the input
306 /// buffer string from which it was parsed to \p NewStrValue. See comments on
307 /// getStringValue for a discussion of when the latter can be None.
308 void setValue(ExpressionValue NewValue
,
309 Optional
<StringRef
> NewStrValue
= None
) {
311 StrValue
= NewStrValue
;
314 /// Clears value of this numeric variable, regardless of whether it is
315 /// currently defined or not.
321 /// \returns the line number where this variable is defined, if any, or None
322 /// if defined before input is parsed.
323 Optional
<size_t> getDefLineNumber() const { return DefLineNumber
; }
326 /// Class representing the use of a numeric variable in the AST of an
328 class NumericVariableUse
: public ExpressionAST
{
330 /// Pointer to the class instance for the variable this use is about.
331 NumericVariable
*Variable
;
334 NumericVariableUse(StringRef Name
, NumericVariable
*Variable
)
335 : ExpressionAST(Name
), Variable(Variable
) {}
336 /// \returns the value of the variable referenced by this instance.
337 Expected
<ExpressionValue
> eval() const override
;
339 /// \returns implicit format of this numeric variable.
340 Expected
<ExpressionFormat
>
341 getImplicitFormat(const SourceMgr
&SM
) const override
{
342 return Variable
->getImplicitFormat();
346 /// Type of functions evaluating a given binary operation.
347 using binop_eval_t
= Expected
<ExpressionValue
> (*)(const ExpressionValue
&,
348 const ExpressionValue
&);
350 /// Class representing a single binary operation in the AST of an expression.
351 class BinaryOperation
: public ExpressionAST
{
354 std::unique_ptr
<ExpressionAST
> LeftOperand
;
357 std::unique_ptr
<ExpressionAST
> RightOperand
;
359 /// Pointer to function that can evaluate this binary operation.
360 binop_eval_t EvalBinop
;
363 BinaryOperation(StringRef ExpressionStr
, binop_eval_t EvalBinop
,
364 std::unique_ptr
<ExpressionAST
> LeftOp
,
365 std::unique_ptr
<ExpressionAST
> RightOp
)
366 : ExpressionAST(ExpressionStr
), EvalBinop(EvalBinop
) {
367 LeftOperand
= std::move(LeftOp
);
368 RightOperand
= std::move(RightOp
);
371 /// Evaluates the value of the binary operation represented by this AST,
372 /// using EvalBinop on the result of recursively evaluating the operands.
373 /// \returns the expression value or an error if an undefined numeric
374 /// variable is used in one of the operands.
375 Expected
<ExpressionValue
> eval() const override
;
377 /// \returns the implicit format of this AST, if any, a diagnostic against
378 /// \p SM if the implicit formats of the AST's components conflict, or no
379 /// format if the AST has no implicit format (e.g. AST is made of a single
381 Expected
<ExpressionFormat
>
382 getImplicitFormat(const SourceMgr
&SM
) const override
;
385 class FileCheckPatternContext
;
387 /// Class representing a substitution to perform in the RegExStr string.
390 /// Pointer to a class instance holding, among other things, the table with
391 /// the values of live string variables at the start of any given CHECK line.
392 /// Used for substituting string variables with the text they were defined
393 /// as. Expressions are linked to the numeric variables they use at
394 /// parse time and directly access the value of the numeric variable to
395 /// evaluate their value.
396 FileCheckPatternContext
*Context
;
398 /// The string that needs to be substituted for something else. For a
399 /// string variable this is its name, otherwise this is the whole expression.
402 // Index in RegExStr of where to do the substitution.
406 Substitution(FileCheckPatternContext
*Context
, StringRef VarName
,
408 : Context(Context
), FromStr(VarName
), InsertIdx(InsertIdx
) {}
410 virtual ~Substitution() = default;
412 /// \returns the string to be substituted for something else.
413 StringRef
getFromString() const { return FromStr
; }
415 /// \returns the index where the substitution is to be performed in RegExStr.
416 size_t getIndex() const { return InsertIdx
; }
418 /// \returns a string containing the result of the substitution represented
419 /// by this class instance or an error if substitution failed.
420 virtual Expected
<std::string
> getResult() const = 0;
423 class StringSubstitution
: public Substitution
{
425 StringSubstitution(FileCheckPatternContext
*Context
, StringRef VarName
,
427 : Substitution(Context
, VarName
, InsertIdx
) {}
429 /// \returns the text that the string variable in this substitution matched
430 /// when defined, or an error if the variable is undefined.
431 Expected
<std::string
> getResult() const override
;
434 class NumericSubstitution
: public Substitution
{
436 /// Pointer to the class representing the expression whose value is to be
438 std::unique_ptr
<Expression
> ExpressionPointer
;
441 NumericSubstitution(FileCheckPatternContext
*Context
, StringRef ExpressionStr
,
442 std::unique_ptr
<Expression
> ExpressionPointer
,
444 : Substitution(Context
, ExpressionStr
, InsertIdx
),
445 ExpressionPointer(std::move(ExpressionPointer
)) {}
447 /// \returns a string containing the result of evaluating the expression in
448 /// this substitution, or an error if evaluation failed.
449 Expected
<std::string
> getResult() const override
;
452 //===----------------------------------------------------------------------===//
453 // Pattern handling code.
454 //===----------------------------------------------------------------------===//
456 /// Class holding the Pattern global state, shared by all patterns: tables
457 /// holding values of variables and whether they are defined or not at any
458 /// given time in the matching process.
459 class FileCheckPatternContext
{
460 friend class Pattern
;
463 /// When matching a given pattern, this holds the value of all the string
464 /// variables defined in previous patterns. In a pattern, only the last
465 /// definition for a given variable is recorded in this table.
466 /// Back-references are used for uses after any the other definition.
467 StringMap
<StringRef
> GlobalVariableTable
;
469 /// Map of all string variables defined so far. Used at parse time to detect
470 /// a name conflict between a numeric variable and a string variable when
471 /// the former is defined on a later line than the latter.
472 StringMap
<bool> DefinedVariableTable
;
474 /// When matching a given pattern, this holds the pointers to the classes
475 /// representing the numeric variables defined in previous patterns. When
476 /// matching a pattern all definitions for that pattern are recorded in the
477 /// NumericVariableDefs table in the Pattern instance of that pattern.
478 StringMap
<NumericVariable
*> GlobalNumericVariableTable
;
480 /// Pointer to the class instance representing the @LINE pseudo variable for
481 /// easily updating its value.
482 NumericVariable
*LineVariable
= nullptr;
484 /// Vector holding pointers to all parsed numeric variables. Used to
485 /// automatically free them once they are guaranteed to no longer be used.
486 std::vector
<std::unique_ptr
<NumericVariable
>> NumericVariables
;
488 /// Vector holding pointers to all parsed expressions. Used to automatically
489 /// free the expressions once they are guaranteed to no longer be used.
490 std::vector
<std::unique_ptr
<Expression
>> Expressions
;
492 /// Vector holding pointers to all substitutions. Used to automatically free
493 /// them once they are guaranteed to no longer be used.
494 std::vector
<std::unique_ptr
<Substitution
>> Substitutions
;
497 /// \returns the value of string variable \p VarName or an error if no such
498 /// variable has been defined.
499 Expected
<StringRef
> getPatternVarValue(StringRef VarName
);
501 /// Defines string and numeric variables from definitions given on the
502 /// command line, passed as a vector of [#]VAR=VAL strings in
503 /// \p CmdlineDefines. \returns an error list containing diagnostics against
504 /// \p SM for all definition parsing failures, if any, or Success otherwise.
505 Error
defineCmdlineVariables(ArrayRef
<StringRef
> CmdlineDefines
,
508 /// Create @LINE pseudo variable. Value is set when pattern are being
510 void createLineVariable();
512 /// Undefines local variables (variables whose name does not start with a '$'
513 /// sign), i.e. removes them from GlobalVariableTable and from
514 /// GlobalNumericVariableTable and also clears the value of numeric
516 void clearLocalVars();
519 /// Makes a new numeric variable and registers it for destruction when the
520 /// context is destroyed.
521 template <class... Types
> NumericVariable
*makeNumericVariable(Types
... args
);
523 /// Makes a new string substitution and registers it for destruction when the
524 /// context is destroyed.
525 Substitution
*makeStringSubstitution(StringRef VarName
, size_t InsertIdx
);
527 /// Makes a new numeric substitution and registers it for destruction when
528 /// the context is destroyed.
529 Substitution
*makeNumericSubstitution(StringRef ExpressionStr
,
530 std::unique_ptr
<Expression
> Expression
,
534 /// Class to represent an error holding a diagnostic with location information
535 /// used when printing it.
536 class ErrorDiagnostic
: public ErrorInfo
<ErrorDiagnostic
> {
538 SMDiagnostic Diagnostic
;
544 ErrorDiagnostic(SMDiagnostic
&&Diag
, SMRange Range
)
545 : Diagnostic(Diag
), Range(Range
) {}
547 std::error_code
convertToErrorCode() const override
{
548 return inconvertibleErrorCode();
551 /// Print diagnostic associated with this error when printing the error.
552 void log(raw_ostream
&OS
) const override
{ Diagnostic
.print(nullptr, OS
); }
554 StringRef
getMessage() const { return Diagnostic
.getMessage(); }
555 SMRange
getRange() const { return Range
; }
557 static Error
get(const SourceMgr
&SM
, SMLoc Loc
, const Twine
&ErrMsg
,
558 SMRange Range
= None
) {
559 return make_error
<ErrorDiagnostic
>(
560 SM
.GetMessage(Loc
, SourceMgr::DK_Error
, ErrMsg
), Range
);
563 static Error
get(const SourceMgr
&SM
, StringRef Buffer
, const Twine
&ErrMsg
) {
564 SMLoc Start
= SMLoc::getFromPointer(Buffer
.data());
565 SMLoc End
= SMLoc::getFromPointer(Buffer
.data() + Buffer
.size());
566 return get(SM
, Start
, ErrMsg
, SMRange(Start
, End
));
570 class NotFoundError
: public ErrorInfo
<NotFoundError
> {
574 std::error_code
convertToErrorCode() const override
{
575 return inconvertibleErrorCode();
578 /// Print diagnostic associated with this error when printing the error.
579 void log(raw_ostream
&OS
) const override
{
580 OS
<< "String not found in input";
584 /// An error that has already been reported.
586 /// This class is designed to support a function whose callers may need to know
587 /// whether the function encountered and reported an error but never need to
588 /// know the nature of that error. For example, the function has a return type
589 /// of \c Error and always returns either \c ErrorReported or \c ErrorSuccess.
590 /// That interface is similar to that of a function returning bool to indicate
591 /// an error except, in the former case, (1) there is no confusion over polarity
592 /// and (2) the caller must either check the result or explicitly ignore it with
593 /// a call like \c consumeError.
594 class ErrorReported final
: public ErrorInfo
<ErrorReported
> {
598 std::error_code
convertToErrorCode() const override
{
599 return inconvertibleErrorCode();
602 /// Print diagnostic associated with this error when printing the error.
603 void log(raw_ostream
&OS
) const override
{
604 OS
<< "error previously reported";
607 static inline Error
reportedOrSuccess(bool HasErrorReported
) {
608 if (HasErrorReported
)
609 return make_error
<ErrorReported
>();
610 return Error::success();
617 /// A fixed string to match as the pattern or empty if this pattern requires
621 /// A regex string to match as the pattern or empty if this pattern requires
622 /// a fixed string to match.
623 std::string RegExStr
;
625 /// Entries in this vector represent a substitution of a string variable or
626 /// an expression in the RegExStr regex at match time. For example, in the
627 /// case of a CHECK directive with the pattern "foo[[bar]]baz[[#N+1]]",
628 /// RegExStr will contain "foobaz" and we'll get two entries in this vector
629 /// that tells us to insert the value of string variable "bar" at offset 3
630 /// and the value of expression "N+1" at offset 6.
631 std::vector
<Substitution
*> Substitutions
;
633 /// Maps names of string variables defined in a pattern to the number of
634 /// their parenthesis group in RegExStr capturing their last definition.
636 /// E.g. for the pattern "foo[[bar:.*]]baz([[bar]][[QUUX]][[bar:.*]])",
637 /// RegExStr will be "foo(.*)baz(\1<quux value>(.*))" where <quux value> is
638 /// the value captured for QUUX on the earlier line where it was defined, and
639 /// VariableDefs will map "bar" to the third parenthesis group which captures
640 /// the second definition of "bar".
642 /// Note: uses std::map rather than StringMap to be able to get the key when
643 /// iterating over values.
644 std::map
<StringRef
, unsigned> VariableDefs
;
646 /// Structure representing the definition of a numeric variable in a pattern.
647 /// It holds the pointer to the class instance holding the value and matching
648 /// format of the numeric variable whose value is being defined and the
649 /// number of the parenthesis group in RegExStr to capture that value.
650 struct NumericVariableMatch
{
651 /// Pointer to class instance holding the value and matching format of the
652 /// numeric variable being defined.
653 NumericVariable
*DefinedNumericVariable
;
655 /// Number of the parenthesis group in RegExStr that captures the value of
656 /// this numeric variable definition.
657 unsigned CaptureParenGroup
;
660 /// Holds the number of the parenthesis group in RegExStr and pointer to the
661 /// corresponding NumericVariable class instance of all numeric variable
662 /// definitions. Used to set the matched value of all those variables.
663 StringMap
<NumericVariableMatch
> NumericVariableDefs
;
665 /// Pointer to a class instance holding the global state shared by all
667 /// - separate tables with the values of live string and numeric variables
668 /// respectively at the start of any given CHECK line;
669 /// - table holding whether a string variable has been defined at any given
670 /// point during the parsing phase.
671 FileCheckPatternContext
*Context
;
673 Check::FileCheckType CheckTy
;
675 /// Line number for this CHECK pattern or None if it is an implicit pattern.
676 /// Used to determine whether a variable definition is made on an earlier
677 /// line to the one with this CHECK.
678 Optional
<size_t> LineNumber
;
680 /// Ignore case while matching if set to true.
681 bool IgnoreCase
= false;
684 Pattern(Check::FileCheckType Ty
, FileCheckPatternContext
*Context
,
685 Optional
<size_t> Line
= None
)
686 : Context(Context
), CheckTy(Ty
), LineNumber(Line
) {}
688 /// \returns the location in source code.
689 SMLoc
getLoc() const { return PatternLoc
; }
691 /// \returns the pointer to the global state for all patterns in this
692 /// FileCheck instance.
693 FileCheckPatternContext
*getContext() const { return Context
; }
695 /// \returns whether \p C is a valid first character for a variable name.
696 static bool isValidVarNameStart(char C
);
698 /// Parsing information about a variable.
699 struct VariableProperties
{
704 /// Parses the string at the start of \p Str for a variable name. \returns
705 /// a VariableProperties structure holding the variable name and whether it
706 /// is the name of a pseudo variable, or an error holding a diagnostic
707 /// against \p SM if parsing fail. If parsing was successful, also strips
708 /// \p Str from the variable name.
709 static Expected
<VariableProperties
> parseVariable(StringRef
&Str
,
710 const SourceMgr
&SM
);
711 /// Parses \p Expr for a numeric substitution block at line \p LineNumber,
712 /// or before input is parsed if \p LineNumber is None. Parameter
713 /// \p IsLegacyLineExpr indicates whether \p Expr should be a legacy @LINE
714 /// expression and \p Context points to the class instance holding the live
715 /// string and numeric variables. \returns a pointer to the class instance
716 /// representing the expression whose value must be substitued, or an error
717 /// holding a diagnostic against \p SM if parsing fails. If substitution was
718 /// successful, sets \p DefinedNumericVariable to point to the class
719 /// representing the numeric variable defined in this numeric substitution
720 /// block, or None if this block does not define any variable.
721 static Expected
<std::unique_ptr
<Expression
>> parseNumericSubstitutionBlock(
722 StringRef Expr
, Optional
<NumericVariable
*> &DefinedNumericVariable
,
723 bool IsLegacyLineExpr
, Optional
<size_t> LineNumber
,
724 FileCheckPatternContext
*Context
, const SourceMgr
&SM
);
725 /// Parses the pattern in \p PatternStr and initializes this Pattern instance
728 /// \p Prefix provides which prefix is being matched, \p Req describes the
729 /// global options that influence the parsing such as whitespace
730 /// canonicalization, \p SM provides the SourceMgr used for error reports.
731 /// \returns true in case of an error, false otherwise.
732 bool parsePattern(StringRef PatternStr
, StringRef Prefix
, SourceMgr
&SM
,
733 const FileCheckRequest
&Req
);
739 Optional
<Match
> TheMatch
;
741 MatchResult(size_t MatchPos
, size_t MatchLen
, Error E
)
742 : TheMatch(Match
{MatchPos
, MatchLen
}), TheError(std::move(E
)) {}
743 MatchResult(Match M
, Error E
) : TheMatch(M
), TheError(std::move(E
)) {}
744 MatchResult(Error E
) : TheError(std::move(E
)) {}
746 /// Matches the pattern string against the input buffer \p Buffer.
748 /// \returns either (1) an error resulting in no match or (2) a match possibly
749 /// with an error encountered while processing the match.
751 /// The GlobalVariableTable StringMap in the FileCheckPatternContext class
752 /// instance provides the current values of FileCheck string variables and is
753 /// updated if this match defines new values. Likewise, the
754 /// GlobalNumericVariableTable StringMap in the same class provides the
755 /// current values of FileCheck numeric variables and is updated if this
756 /// match defines new numeric values.
757 MatchResult
match(StringRef Buffer
, const SourceMgr
&SM
) const;
758 /// Prints the value of successful substitutions.
759 void printSubstitutions(const SourceMgr
&SM
, StringRef Buffer
,
760 SMRange MatchRange
, FileCheckDiag::MatchType MatchTy
,
761 std::vector
<FileCheckDiag
> *Diags
) const;
762 void printFuzzyMatch(const SourceMgr
&SM
, StringRef Buffer
,
763 std::vector
<FileCheckDiag
> *Diags
) const;
765 bool hasVariable() const {
766 return !(Substitutions
.empty() && VariableDefs
.empty());
768 void printVariableDefs(const SourceMgr
&SM
, FileCheckDiag::MatchType MatchTy
,
769 std::vector
<FileCheckDiag
> *Diags
) const;
771 Check::FileCheckType
getCheckTy() const { return CheckTy
; }
773 int getCount() const { return CheckTy
.getCount(); }
776 bool AddRegExToRegEx(StringRef RS
, unsigned &CurParen
, SourceMgr
&SM
);
777 void AddBackrefToRegEx(unsigned BackrefNum
);
778 /// Computes an arbitrary estimate for the quality of matching this pattern
779 /// at the start of \p Buffer; a distance of zero should correspond to a
781 unsigned computeMatchDistance(StringRef Buffer
) const;
782 /// Finds the closing sequence of a regex variable usage or definition.
784 /// \p Str has to point in the beginning of the definition (right after the
785 /// opening sequence). \p SM holds the SourceMgr used for error reporting.
786 /// \returns the offset of the closing sequence within Str, or npos if it
788 static size_t FindRegexVarEnd(StringRef Str
, SourceMgr
&SM
);
790 /// Parses \p Expr for the name of a numeric variable to be defined at line
791 /// \p LineNumber, or before input is parsed if \p LineNumber is None.
792 /// \returns a pointer to the class instance representing that variable,
793 /// creating it if needed, or an error holding a diagnostic against \p SM
794 /// should defining such a variable be invalid.
795 static Expected
<NumericVariable
*> parseNumericVariableDefinition(
796 StringRef
&Expr
, FileCheckPatternContext
*Context
,
797 Optional
<size_t> LineNumber
, ExpressionFormat ImplicitFormat
,
798 const SourceMgr
&SM
);
799 /// Parses \p Name as a (pseudo if \p IsPseudo is true) numeric variable use
800 /// at line \p LineNumber, or before input is parsed if \p LineNumber is
801 /// None. Parameter \p Context points to the class instance holding the live
802 /// string and numeric variables. \returns the pointer to the class instance
803 /// representing that variable if successful, or an error holding a
804 /// diagnostic against \p SM otherwise.
805 static Expected
<std::unique_ptr
<NumericVariableUse
>> parseNumericVariableUse(
806 StringRef Name
, bool IsPseudo
, Optional
<size_t> LineNumber
,
807 FileCheckPatternContext
*Context
, const SourceMgr
&SM
);
808 enum class AllowedOperand
{ LineVar
, LegacyLiteral
, Any
};
809 /// Parses \p Expr for use of a numeric operand at line \p LineNumber, or
810 /// before input is parsed if \p LineNumber is None. Accepts literal values,
811 /// numeric variables and function calls, depending on the value of \p AO.
812 /// \p MaybeInvalidConstraint indicates whether the text being parsed could
813 /// be an invalid constraint. \p Context points to the class instance holding
814 /// the live string and numeric variables. \returns the class representing
815 /// that operand in the AST of the expression or an error holding a
816 /// diagnostic against \p SM otherwise. If \p Expr starts with a "(" this
817 /// function will attempt to parse a parenthesized expression.
818 static Expected
<std::unique_ptr
<ExpressionAST
>>
819 parseNumericOperand(StringRef
&Expr
, AllowedOperand AO
, bool ConstraintParsed
,
820 Optional
<size_t> LineNumber
,
821 FileCheckPatternContext
*Context
, const SourceMgr
&SM
);
822 /// Parses and updates \p RemainingExpr for a binary operation at line
823 /// \p LineNumber, or before input is parsed if \p LineNumber is None. The
824 /// left operand of this binary operation is given in \p LeftOp and \p Expr
825 /// holds the string for the full expression, including the left operand.
826 /// Parameter \p IsLegacyLineExpr indicates whether we are parsing a legacy
827 /// @LINE expression. Parameter \p Context points to the class instance
828 /// holding the live string and numeric variables. \returns the class
829 /// representing the binary operation in the AST of the expression, or an
830 /// error holding a diagnostic against \p SM otherwise.
831 static Expected
<std::unique_ptr
<ExpressionAST
>>
832 parseBinop(StringRef Expr
, StringRef
&RemainingExpr
,
833 std::unique_ptr
<ExpressionAST
> LeftOp
, bool IsLegacyLineExpr
,
834 Optional
<size_t> LineNumber
, FileCheckPatternContext
*Context
,
835 const SourceMgr
&SM
);
837 /// Parses a parenthesized expression inside \p Expr at line \p LineNumber, or
838 /// before input is parsed if \p LineNumber is None. \p Expr must start with
839 /// a '('. Accepts both literal values and numeric variables. Parameter \p
840 /// Context points to the class instance holding the live string and numeric
841 /// variables. \returns the class representing that operand in the AST of the
842 /// expression or an error holding a diagnostic against \p SM otherwise.
843 static Expected
<std::unique_ptr
<ExpressionAST
>>
844 parseParenExpr(StringRef
&Expr
, Optional
<size_t> LineNumber
,
845 FileCheckPatternContext
*Context
, const SourceMgr
&SM
);
847 /// Parses \p Expr for an argument list belonging to a call to function \p
848 /// FuncName at line \p LineNumber, or before input is parsed if \p LineNumber
849 /// is None. Parameter \p FuncLoc is the source location used for diagnostics.
850 /// Parameter \p Context points to the class instance holding the live string
851 /// and numeric variables. \returns the class representing that call in the
852 /// AST of the expression or an error holding a diagnostic against \p SM
854 static Expected
<std::unique_ptr
<ExpressionAST
>>
855 parseCallExpr(StringRef
&Expr
, StringRef FuncName
,
856 Optional
<size_t> LineNumber
, FileCheckPatternContext
*Context
,
857 const SourceMgr
&SM
);
860 //===----------------------------------------------------------------------===//
862 //===----------------------------------------------------------------------===//
864 /// A check that we found in the input file.
865 struct FileCheckString
{
866 /// The pattern to match.
869 /// Which prefix name this check matched.
872 /// The location in the match file that the check string was specified.
875 /// All of the strings that are disallowed from occurring between this match
876 /// string and the previous one (or start of file).
877 std::vector
<Pattern
> DagNotStrings
;
879 FileCheckString(const Pattern
&P
, StringRef S
, SMLoc L
)
880 : Pat(P
), Prefix(S
), Loc(L
) {}
882 /// Matches check string and its "not strings" and/or "dag strings".
883 size_t Check(const SourceMgr
&SM
, StringRef Buffer
, bool IsLabelScanMode
,
884 size_t &MatchLen
, FileCheckRequest
&Req
,
885 std::vector
<FileCheckDiag
> *Diags
) const;
887 /// Verifies that there is a single line in the given \p Buffer. Errors are
888 /// reported against \p SM.
889 bool CheckNext(const SourceMgr
&SM
, StringRef Buffer
) const;
890 /// Verifies that there is no newline in the given \p Buffer. Errors are
891 /// reported against \p SM.
892 bool CheckSame(const SourceMgr
&SM
, StringRef Buffer
) const;
893 /// Verifies that none of the strings in \p NotStrings are found in the given
894 /// \p Buffer. Errors are reported against \p SM and diagnostics recorded in
895 /// \p Diags according to the verbosity level set in \p Req.
896 bool CheckNot(const SourceMgr
&SM
, StringRef Buffer
,
897 const std::vector
<const Pattern
*> &NotStrings
,
898 const FileCheckRequest
&Req
,
899 std::vector
<FileCheckDiag
> *Diags
) const;
900 /// Matches "dag strings" and their mixed "not strings".
901 size_t CheckDag(const SourceMgr
&SM
, StringRef Buffer
,
902 std::vector
<const Pattern
*> &NotStrings
,
903 const FileCheckRequest
&Req
,
904 std::vector
<FileCheckDiag
> *Diags
) const;