1//===-- FileCheckImpl.h - Private FileCheck Interface ------------*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// 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.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_FILECHECK_FILECHECKIMPL_H
16#define LLVM_LIB_FILECHECK_FILECHECKIMPL_H
17
18#include "llvm/ADT/APInt.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"
24#include <map>
25#include <optional>
26#include <string>
27#include <vector>
28
29namespace llvm {
30
31//===----------------------------------------------------------------------===//
32// Numeric substitution handling code.
33//===----------------------------------------------------------------------===//
34
35/// Type representing the format an expression value should be textualized into
36/// for matching. Used to represent both explicit format specifiers as well as
37/// implicit format from using numeric variables.
38struct ExpressionFormat {
39 enum class Kind {
40 /// Denote absence of format. Used for implicit format of literals and
41 /// empty expressions.
42 NoFormat,
43 /// Value is an unsigned integer and should be printed as a decimal number.
44 Unsigned,
45 /// Value is a signed integer and should be printed as a decimal number.
46 Signed,
47 /// Value should be printed as an uppercase hex number.
48 HexUpper,
49 /// Value should be printed as a lowercase hex number.
50 HexLower
51 };
52
53private:
54 Kind Value = Kind::NoFormat;
55 unsigned Precision = 0;
56 /// printf-like "alternate form" selected.
57 bool AlternateForm = false;
58
59public:
60 /// Evaluates a format to true if it can be used in a match.
61 explicit operator bool() const { return Value != Kind::NoFormat; }
62
63 /// Define format equality: formats are equal if neither is NoFormat and
64 /// their kinds and precision are the same.
65 bool operator==(const ExpressionFormat &Other) const {
66 return Value != Kind::NoFormat && Value == Other.Value &&
67 Precision == Other.Precision && AlternateForm == Other.AlternateForm;
68 }
69
70 bool operator!=(const ExpressionFormat &Other) const {
71 return !(*this == Other);
72 }
73
74 bool operator==(Kind OtherValue) const { return Value == OtherValue; }
75
76 bool operator!=(Kind OtherValue) const { return !(*this == OtherValue); }
77
78 /// \returns the format specifier corresponding to this format as a string.
79 StringRef toString() const;
80
81 ExpressionFormat() = default;
82 explicit ExpressionFormat(Kind Value) : Value(Value), Precision(0){};
83 explicit ExpressionFormat(Kind Value, unsigned Precision)
84 : Value(Value), Precision(Precision){};
85 explicit ExpressionFormat(Kind Value, unsigned Precision, bool AlternateForm)
86 : Value(Value), Precision(Precision), AlternateForm(AlternateForm){};
87
88 /// \returns a wildcard regular expression string that matches any value in
89 /// the format represented by this instance and no other value, or an error
90 /// if the format is NoFormat.
91 Expected<std::string> getWildcardRegex() const;
92
93 /// \returns the string representation of \p Value in the format represented
94 /// by this instance, or an error if conversion to this format failed or the
95 /// format is NoFormat.
96 Expected<std::string> getMatchingString(APInt Value) const;
97
98 /// \returns the value corresponding to string representation \p StrVal
99 /// according to the matching format represented by this instance.
100 APInt valueFromStringRepr(StringRef StrVal, const SourceMgr &SM) const;
101};
102
103/// Class to represent an overflow error that might result when manipulating a
104/// value.
105class OverflowError : public ErrorInfo<OverflowError> {
106public:
107 static char ID;
108
109 std::error_code convertToErrorCode() const override {
110 return std::make_error_code(e: std::errc::value_too_large);
111 }
112
113 void log(raw_ostream &OS) const override { OS << "overflow error"; }
114};
115
116/// Performs operation and \returns its result or an error in case of failure,
117/// such as if an overflow occurs.
118Expected<APInt> exprAdd(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
119Expected<APInt> exprSub(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
120Expected<APInt> exprMul(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
121Expected<APInt> exprDiv(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
122Expected<APInt> exprMax(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
123Expected<APInt> exprMin(const APInt &Lhs, const APInt &Rhs, bool &Overflow);
124
125/// Base class representing the AST of a given expression.
126class ExpressionAST {
127private:
128 StringRef ExpressionStr;
129
130public:
131 ExpressionAST(StringRef ExpressionStr) : ExpressionStr(ExpressionStr) {}
132
133 virtual ~ExpressionAST() = default;
134
135 StringRef getExpressionStr() const { return ExpressionStr; }
136
137 /// Evaluates and \returns the value of the expression represented by this
138 /// AST or an error if evaluation fails.
139 virtual Expected<APInt> eval() const = 0;
140
141 /// \returns either the implicit format of this AST, a diagnostic against
142 /// \p SM if implicit formats of the AST's components conflict, or NoFormat
143 /// if the AST has no implicit format (e.g. AST is made up of a single
144 /// literal).
145 virtual Expected<ExpressionFormat>
146 getImplicitFormat(const SourceMgr &SM) const {
147 return ExpressionFormat();
148 }
149};
150
151/// Class representing an unsigned literal in the AST of an expression.
152class ExpressionLiteral : public ExpressionAST {
153private:
154 /// Actual value of the literal.
155 APInt Value;
156
157public:
158 explicit ExpressionLiteral(StringRef ExpressionStr, APInt Val)
159 : ExpressionAST(ExpressionStr), Value(Val) {}
160
161 /// \returns the literal's value.
162 Expected<APInt> eval() const override { return Value; }
163};
164
165/// Class to represent an undefined variable error, which quotes that
166/// variable's name when printed.
167class UndefVarError : public ErrorInfo<UndefVarError> {
168private:
169 StringRef VarName;
170
171public:
172 static char ID;
173
174 UndefVarError(StringRef VarName) : VarName(VarName) {}
175
176 StringRef getVarName() const { return VarName; }
177
178 std::error_code convertToErrorCode() const override {
179 return inconvertibleErrorCode();
180 }
181
182 /// Print name of variable associated with this error.
183 void log(raw_ostream &OS) const override {
184 OS << "undefined variable: " << VarName;
185 }
186};
187
188/// Class representing an expression and its matching format.
189class Expression {
190private:
191 /// Pointer to AST of the expression.
192 std::unique_ptr<ExpressionAST> AST;
193
194 /// Format to use (e.g. hex upper case letters) when matching the value.
195 ExpressionFormat Format;
196
197public:
198 /// Generic constructor for an expression represented by the given \p AST and
199 /// whose matching format is \p Format.
200 Expression(std::unique_ptr<ExpressionAST> AST, ExpressionFormat Format)
201 : AST(std::move(AST)), Format(Format) {}
202
203 /// \returns pointer to AST of the expression. Pointer is guaranteed to be
204 /// valid as long as this object is.
205 ExpressionAST *getAST() const { return AST.get(); }
206
207 ExpressionFormat getFormat() const { return Format; }
208};
209
210/// Class representing a numeric variable and its associated current value.
211class NumericVariable {
212private:
213 /// Name of the numeric variable.
214 StringRef Name;
215
216 /// Format to use for expressions using this variable without an explicit
217 /// format.
218 ExpressionFormat ImplicitFormat;
219
220 /// Value of numeric variable, if defined, or std::nullopt otherwise.
221 std::optional<APInt> Value;
222
223 /// The input buffer's string from which Value was parsed, or std::nullopt.
224 /// See comments on getStringValue for a discussion of the std::nullopt case.
225 std::optional<StringRef> StrValue;
226
227 /// Line number where this variable is defined, or std::nullopt if defined
228 /// before input is parsed. Used to determine whether a variable is defined on
229 /// the same line as a given use.
230 std::optional<size_t> DefLineNumber;
231
232public:
233 /// Constructor for a variable \p Name with implicit format \p ImplicitFormat
234 /// defined at line \p DefLineNumber or defined before input is parsed if
235 /// \p DefLineNumber is std::nullopt.
236 explicit NumericVariable(StringRef Name, ExpressionFormat ImplicitFormat,
237 std::optional<size_t> DefLineNumber = std::nullopt)
238 : Name(Name), ImplicitFormat(ImplicitFormat),
239 DefLineNumber(DefLineNumber) {}
240
241 /// \returns name of this numeric variable.
242 StringRef getName() const { return Name; }
243
244 /// \returns implicit format of this numeric variable.
245 ExpressionFormat getImplicitFormat() const { return ImplicitFormat; }
246
247 /// \returns this variable's value.
248 std::optional<APInt> getValue() const { return Value; }
249
250 /// \returns the input buffer's string from which this variable's value was
251 /// parsed, or std::nullopt if the value is not yet defined or was not parsed
252 /// from the input buffer. For example, the value of @LINE is not parsed from
253 /// the input buffer, and some numeric variables are parsed from the command
254 /// line instead.
255 std::optional<StringRef> getStringValue() const { return StrValue; }
256
257 /// Sets value of this numeric variable to \p NewValue, and sets the input
258 /// buffer string from which it was parsed to \p NewStrValue. See comments on
259 /// getStringValue for a discussion of when the latter can be std::nullopt.
260 void setValue(APInt NewValue,
261 std::optional<StringRef> NewStrValue = std::nullopt) {
262 Value = NewValue;
263 StrValue = NewStrValue;
264 }
265
266 /// Clears value of this numeric variable, regardless of whether it is
267 /// currently defined or not.
268 void clearValue() {
269 Value = std::nullopt;
270 StrValue = std::nullopt;
271 }
272
273 /// \returns the line number where this variable is defined, if any, or
274 /// std::nullopt if defined before input is parsed.
275 std::optional<size_t> getDefLineNumber() const { return DefLineNumber; }
276};
277
278/// Class representing the use of a numeric variable in the AST of an
279/// expression.
280class NumericVariableUse : public ExpressionAST {
281private:
282 /// Pointer to the class instance for the variable this use is about.
283 NumericVariable *Variable;
284
285public:
286 NumericVariableUse(StringRef Name, NumericVariable *Variable)
287 : ExpressionAST(Name), Variable(Variable) {}
288 /// \returns the value of the variable referenced by this instance.
289 Expected<APInt> eval() const override;
290
291 /// \returns implicit format of this numeric variable.
292 Expected<ExpressionFormat>
293 getImplicitFormat(const SourceMgr &SM) const override {
294 return Variable->getImplicitFormat();
295 }
296};
297
298/// Type of functions evaluating a given binary operation.
299using binop_eval_t = Expected<APInt> (*)(const APInt &, const APInt &, bool &);
300
301/// Class representing a single binary operation in the AST of an expression.
302class BinaryOperation : public ExpressionAST {
303private:
304 /// Left operand.
305 std::unique_ptr<ExpressionAST> LeftOperand;
306
307 /// Right operand.
308 std::unique_ptr<ExpressionAST> RightOperand;
309
310 /// Pointer to function that can evaluate this binary operation.
311 binop_eval_t EvalBinop;
312
313public:
314 BinaryOperation(StringRef ExpressionStr, binop_eval_t EvalBinop,
315 std::unique_ptr<ExpressionAST> LeftOp,
316 std::unique_ptr<ExpressionAST> RightOp)
317 : ExpressionAST(ExpressionStr), EvalBinop(EvalBinop) {
318 LeftOperand = std::move(LeftOp);
319 RightOperand = std::move(RightOp);
320 }
321
322 /// Evaluates the value of the binary operation represented by this AST,
323 /// using EvalBinop on the result of recursively evaluating the operands.
324 /// \returns the expression value or an error if an undefined numeric
325 /// variable is used in one of the operands.
326 Expected<APInt> eval() const override;
327
328 /// \returns the implicit format of this AST, if any, a diagnostic against
329 /// \p SM if the implicit formats of the AST's components conflict, or no
330 /// format if the AST has no implicit format (e.g. AST is made of a single
331 /// literal).
332 Expected<ExpressionFormat>
333 getImplicitFormat(const SourceMgr &SM) const override;
334};
335
336class FileCheckPatternContext;
337
338/// Class representing a substitution to perform in the RegExStr string.
339class Substitution {
340protected:
341 /// Pointer to a class instance holding, among other things, the table with
342 /// the values of live string variables at the start of any given CHECK line.
343 /// Used for substituting string variables with the text they were defined
344 /// as. Expressions are linked to the numeric variables they use at
345 /// parse time and directly access the value of the numeric variable to
346 /// evaluate their value.
347 FileCheckPatternContext *Context;
348
349 /// The string that needs to be substituted for something else. For a
350 /// string variable this is its name, otherwise this is the whole expression.
351 StringRef FromStr;
352
353 // Index in RegExStr of where to do the substitution.
354 size_t InsertIdx;
355
356public:
357 Substitution(FileCheckPatternContext *Context, StringRef VarName,
358 size_t InsertIdx)
359 : Context(Context), FromStr(VarName), InsertIdx(InsertIdx) {}
360
361 virtual ~Substitution() = default;
362
363 /// \returns the string to be substituted for something else.
364 StringRef getFromString() const { return FromStr; }
365
366 /// \returns the index where the substitution is to be performed in RegExStr.
367 size_t getIndex() const { return InsertIdx; }
368
369 /// \returns a regular expression string that matches the result of the
370 /// substitution represented by this class instance or an error if
371 /// substitution failed.
372 virtual Expected<std::string> getResultRegex() const = 0;
373
374 /// \returns a string containing the result of the substitution represented
375 /// by this class instance in a form suitable for diagnostics, or an error if
376 /// substitution failed.
377 virtual Expected<std::string> getResultForDiagnostics() const = 0;
378};
379
380class StringSubstitution : public Substitution {
381public:
382 StringSubstitution(FileCheckPatternContext *Context, StringRef VarName,
383 size_t InsertIdx)
384 : Substitution(Context, VarName, InsertIdx) {}
385
386 /// \returns the text that the string variable in this substitution matched
387 /// when defined, or an error if the variable is undefined.
388 Expected<std::string> getResultRegex() const override;
389
390 /// \returns the text that the string variable in this substitution matched
391 /// when defined, in a form suitable for diagnostics, or an error if the
392 /// variable is undefined.
393 Expected<std::string> getResultForDiagnostics() const override;
394};
395
396class NumericSubstitution : public Substitution {
397private:
398 /// Pointer to the class representing the expression whose value is to be
399 /// substituted.
400 std::unique_ptr<Expression> ExpressionPointer;
401
402public:
403 NumericSubstitution(FileCheckPatternContext *Context, StringRef ExpressionStr,
404 std::unique_ptr<Expression> ExpressionPointer,
405 size_t InsertIdx)
406 : Substitution(Context, ExpressionStr, InsertIdx),
407 ExpressionPointer(std::move(ExpressionPointer)) {}
408
409 /// \returns a string containing the result of evaluating the expression in
410 /// this substitution, or an error if evaluation failed.
411 Expected<std::string> getResultRegex() const override;
412
413 /// \returns a string containing the result of evaluating the expression in
414 /// this substitution, in a form suitable for diagnostics, or an error if
415 /// evaluation failed.
416 Expected<std::string> getResultForDiagnostics() const override;
417};
418
419//===----------------------------------------------------------------------===//
420// Pattern handling code.
421//===----------------------------------------------------------------------===//
422
423/// Class holding the Pattern global state, shared by all patterns: tables
424/// holding values of variables and whether they are defined or not at any
425/// given time in the matching process.
426class FileCheckPatternContext {
427 friend class Pattern;
428
429private:
430 /// When matching a given pattern, this holds the value of all the string
431 /// variables defined in previous patterns. In a pattern, only the last
432 /// definition for a given variable is recorded in this table.
433 /// Back-references are used for uses after any the other definition.
434 StringMap<StringRef> GlobalVariableTable;
435
436 /// Map of all string variables defined so far. Used at parse time to detect
437 /// a name conflict between a numeric variable and a string variable when
438 /// the former is defined on a later line than the latter.
439 StringMap<bool> DefinedVariableTable;
440
441 /// When matching a given pattern, this holds the pointers to the classes
442 /// representing the numeric variables defined in previous patterns. When
443 /// matching a pattern all definitions for that pattern are recorded in the
444 /// NumericVariableDefs table in the Pattern instance of that pattern.
445 StringMap<NumericVariable *> GlobalNumericVariableTable;
446
447 /// Pointer to the class instance representing the @LINE pseudo variable for
448 /// easily updating its value.
449 NumericVariable *LineVariable = nullptr;
450
451 /// Vector holding pointers to all parsed numeric variables. Used to
452 /// automatically free them once they are guaranteed to no longer be used.
453 std::vector<std::unique_ptr<NumericVariable>> NumericVariables;
454
455 /// Vector holding pointers to all parsed expressions. Used to automatically
456 /// free the expressions once they are guaranteed to no longer be used.
457 std::vector<std::unique_ptr<Expression>> Expressions;
458
459 /// Vector holding pointers to all substitutions. Used to automatically free
460 /// them once they are guaranteed to no longer be used.
461 std::vector<std::unique_ptr<Substitution>> Substitutions;
462
463public:
464 /// \returns the value of string variable \p VarName or an error if no such
465 /// variable has been defined.
466 Expected<StringRef> getPatternVarValue(StringRef VarName);
467
468 /// Defines string and numeric variables from definitions given on the
469 /// command line, passed as a vector of [#]VAR=VAL strings in
470 /// \p CmdlineDefines. \returns an error list containing diagnostics against
471 /// \p SM for all definition parsing failures, if any, or Success otherwise.
472 Error defineCmdlineVariables(ArrayRef<StringRef> CmdlineDefines,
473 SourceMgr &SM);
474
475 /// Create @LINE pseudo variable. Value is set when pattern are being
476 /// matched.
477 void createLineVariable();
478
479 /// Undefines local variables (variables whose name does not start with a '$'
480 /// sign), i.e. removes them from GlobalVariableTable and from
481 /// GlobalNumericVariableTable and also clears the value of numeric
482 /// variables.
483 void clearLocalVars();
484
485private:
486 /// Makes a new numeric variable and registers it for destruction when the
487 /// context is destroyed.
488 template <class... Types> NumericVariable *makeNumericVariable(Types... args);
489
490 /// Makes a new string substitution and registers it for destruction when the
491 /// context is destroyed.
492 Substitution *makeStringSubstitution(StringRef VarName, size_t InsertIdx);
493
494 /// Makes a new numeric substitution and registers it for destruction when
495 /// the context is destroyed.
496 Substitution *makeNumericSubstitution(StringRef ExpressionStr,
497 std::unique_ptr<Expression> Expression,
498 size_t InsertIdx);
499};
500
501/// Class to represent an error holding a diagnostic with location information
502/// used when printing it.
503class ErrorDiagnostic : public ErrorInfo<ErrorDiagnostic> {
504private:
505 SMDiagnostic Diagnostic;
506 SMRange Range;
507
508public:
509 static char ID;
510
511 ErrorDiagnostic(SMDiagnostic &&Diag, SMRange Range)
512 : Diagnostic(Diag), Range(Range) {}
513
514 std::error_code convertToErrorCode() const override {
515 return inconvertibleErrorCode();
516 }
517
518 /// Print diagnostic associated with this error when printing the error.
519 void log(raw_ostream &OS) const override { Diagnostic.print(ProgName: nullptr, S&: OS); }
520
521 StringRef getMessage() const { return Diagnostic.getMessage(); }
522 SMRange getRange() const { return Range; }
523
524 static Error get(const SourceMgr &SM, SMLoc Loc, const Twine &ErrMsg,
525 SMRange Range = std::nullopt) {
526 return make_error<ErrorDiagnostic>(
527 Args: SM.GetMessage(Loc, Kind: SourceMgr::DK_Error, Msg: ErrMsg), Args&: Range);
528 }
529
530 static Error get(const SourceMgr &SM, StringRef Buffer, const Twine &ErrMsg) {
531 SMLoc Start = SMLoc::getFromPointer(Ptr: Buffer.data());
532 SMLoc End = SMLoc::getFromPointer(Ptr: Buffer.data() + Buffer.size());
533 return get(SM, Loc: Start, ErrMsg, Range: SMRange(Start, End));
534 }
535};
536
537class NotFoundError : public ErrorInfo<NotFoundError> {
538public:
539 static char ID;
540
541 std::error_code convertToErrorCode() const override {
542 return inconvertibleErrorCode();
543 }
544
545 /// Print diagnostic associated with this error when printing the error.
546 void log(raw_ostream &OS) const override {
547 OS << "String not found in input";
548 }
549};
550
551/// An error that has already been reported.
552///
553/// This class is designed to support a function whose callers may need to know
554/// whether the function encountered and reported an error but never need to
555/// know the nature of that error. For example, the function has a return type
556/// of \c Error and always returns either \c ErrorReported or \c ErrorSuccess.
557/// That interface is similar to that of a function returning bool to indicate
558/// an error except, in the former case, (1) there is no confusion over polarity
559/// and (2) the caller must either check the result or explicitly ignore it with
560/// a call like \c consumeError.
561class ErrorReported final : public ErrorInfo<ErrorReported> {
562public:
563 static char ID;
564
565 std::error_code convertToErrorCode() const override {
566 return inconvertibleErrorCode();
567 }
568
569 /// Print diagnostic associated with this error when printing the error.
570 void log(raw_ostream &OS) const override {
571 OS << "error previously reported";
572 }
573
574 static inline Error reportedOrSuccess(bool HasErrorReported) {
575 if (HasErrorReported)
576 return make_error<ErrorReported>();
577 return Error::success();
578 }
579};
580
581class Pattern {
582 SMLoc PatternLoc;
583
584 /// A fixed string to match as the pattern or empty if this pattern requires
585 /// a regex match.
586 StringRef FixedStr;
587
588 /// A regex string to match as the pattern or empty if this pattern requires
589 /// a fixed string to match.
590 std::string RegExStr;
591
592 /// Entries in this vector represent a substitution of a string variable or
593 /// an expression in the RegExStr regex at match time. For example, in the
594 /// case of a CHECK directive with the pattern "foo[[bar]]baz[[#N+1]]",
595 /// RegExStr will contain "foobaz" and we'll get two entries in this vector
596 /// that tells us to insert the value of string variable "bar" at offset 3
597 /// and the value of expression "N+1" at offset 6.
598 std::vector<Substitution *> Substitutions;
599
600 /// Maps names of string variables defined in a pattern to the number of
601 /// their parenthesis group in RegExStr capturing their last definition.
602 ///
603 /// E.g. for the pattern "foo[[bar:.*]]baz([[bar]][[QUUX]][[bar:.*]])",
604 /// RegExStr will be "foo(.*)baz(\1<quux value>(.*))" where <quux value> is
605 /// the value captured for QUUX on the earlier line where it was defined, and
606 /// VariableDefs will map "bar" to the third parenthesis group which captures
607 /// the second definition of "bar".
608 ///
609 /// Note: uses std::map rather than StringMap to be able to get the key when
610 /// iterating over values.
611 std::map<StringRef, unsigned> VariableDefs;
612
613 /// Structure representing the definition of a numeric variable in a pattern.
614 /// It holds the pointer to the class instance holding the value and matching
615 /// format of the numeric variable whose value is being defined and the
616 /// number of the parenthesis group in RegExStr to capture that value.
617 struct NumericVariableMatch {
618 /// Pointer to class instance holding the value and matching format of the
619 /// numeric variable being defined.
620 NumericVariable *DefinedNumericVariable;
621
622 /// Number of the parenthesis group in RegExStr that captures the value of
623 /// this numeric variable definition.
624 unsigned CaptureParenGroup;
625 };
626
627 /// Holds the number of the parenthesis group in RegExStr and pointer to the
628 /// corresponding NumericVariable class instance of all numeric variable
629 /// definitions. Used to set the matched value of all those variables.
630 StringMap<NumericVariableMatch> NumericVariableDefs;
631
632 /// Pointer to a class instance holding the global state shared by all
633 /// patterns:
634 /// - separate tables with the values of live string and numeric variables
635 /// respectively at the start of any given CHECK line;
636 /// - table holding whether a string variable has been defined at any given
637 /// point during the parsing phase.
638 FileCheckPatternContext *Context;
639
640 Check::FileCheckType CheckTy;
641
642 /// Line number for this CHECK pattern or std::nullopt if it is an implicit
643 /// pattern. Used to determine whether a variable definition is made on an
644 /// earlier line to the one with this CHECK.
645 std::optional<size_t> LineNumber;
646
647 /// Ignore case while matching if set to true.
648 bool IgnoreCase = false;
649
650public:
651 Pattern(Check::FileCheckType Ty, FileCheckPatternContext *Context,
652 std::optional<size_t> Line = std::nullopt)
653 : Context(Context), CheckTy(Ty), LineNumber(Line) {}
654
655 /// \returns the location in source code.
656 SMLoc getLoc() const { return PatternLoc; }
657
658 /// \returns the pointer to the global state for all patterns in this
659 /// FileCheck instance.
660 FileCheckPatternContext *getContext() const { return Context; }
661
662 /// \returns whether \p C is a valid first character for a variable name.
663 static bool isValidVarNameStart(char C);
664
665 /// Parsing information about a variable.
666 struct VariableProperties {
667 StringRef Name;
668 bool IsPseudo;
669 };
670
671 /// Parses the string at the start of \p Str for a variable name. \returns
672 /// a VariableProperties structure holding the variable name and whether it
673 /// is the name of a pseudo variable, or an error holding a diagnostic
674 /// against \p SM if parsing fail. If parsing was successful, also strips
675 /// \p Str from the variable name.
676 static Expected<VariableProperties> parseVariable(StringRef &Str,
677 const SourceMgr &SM);
678 /// Parses \p Expr for a numeric substitution block at line \p LineNumber,
679 /// or before input is parsed if \p LineNumber is None. Parameter
680 /// \p IsLegacyLineExpr indicates whether \p Expr should be a legacy @LINE
681 /// expression and \p Context points to the class instance holding the live
682 /// string and numeric variables. \returns a pointer to the class instance
683 /// representing the expression whose value must be substitued, or an error
684 /// holding a diagnostic against \p SM if parsing fails. If substitution was
685 /// successful, sets \p DefinedNumericVariable to point to the class
686 /// representing the numeric variable defined in this numeric substitution
687 /// block, or std::nullopt if this block does not define any variable.
688 static Expected<std::unique_ptr<Expression>> parseNumericSubstitutionBlock(
689 StringRef Expr, std::optional<NumericVariable *> &DefinedNumericVariable,
690 bool IsLegacyLineExpr, std::optional<size_t> LineNumber,
691 FileCheckPatternContext *Context, const SourceMgr &SM);
692 /// Parses the pattern in \p PatternStr and initializes this Pattern instance
693 /// accordingly.
694 ///
695 /// \p Prefix provides which prefix is being matched, \p Req describes the
696 /// global options that influence the parsing such as whitespace
697 /// canonicalization, \p SM provides the SourceMgr used for error reports.
698 /// \returns true in case of an error, false otherwise.
699 bool parsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
700 const FileCheckRequest &Req);
701 struct Match {
702 size_t Pos;
703 size_t Len;
704 };
705 struct MatchResult {
706 std::optional<Match> TheMatch;
707 Error TheError;
708 MatchResult(size_t MatchPos, size_t MatchLen, Error E)
709 : TheMatch(Match{.Pos: MatchPos, .Len: MatchLen}), TheError(std::move(E)) {}
710 MatchResult(Match M, Error E) : TheMatch(M), TheError(std::move(E)) {}
711 MatchResult(Error E) : TheError(std::move(E)) {}
712 };
713 /// Matches the pattern string against the input buffer \p Buffer.
714 ///
715 /// \returns either (1) an error resulting in no match or (2) a match possibly
716 /// with an error encountered while processing the match.
717 ///
718 /// The GlobalVariableTable StringMap in the FileCheckPatternContext class
719 /// instance provides the current values of FileCheck string variables and is
720 /// updated if this match defines new values. Likewise, the
721 /// GlobalNumericVariableTable StringMap in the same class provides the
722 /// current values of FileCheck numeric variables and is updated if this
723 /// match defines new numeric values.
724 MatchResult match(StringRef Buffer, const SourceMgr &SM) const;
725 /// Prints the value of successful substitutions.
726 void printSubstitutions(const SourceMgr &SM, StringRef Buffer,
727 SMRange MatchRange, FileCheckDiag::MatchType MatchTy,
728 std::vector<FileCheckDiag> *Diags) const;
729 void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
730 std::vector<FileCheckDiag> *Diags) const;
731
732 bool hasVariable() const {
733 return !(Substitutions.empty() && VariableDefs.empty());
734 }
735 void printVariableDefs(const SourceMgr &SM, FileCheckDiag::MatchType MatchTy,
736 std::vector<FileCheckDiag> *Diags) const;
737
738 Check::FileCheckType getCheckTy() const { return CheckTy; }
739
740 int getCount() const { return CheckTy.getCount(); }
741
742private:
743 bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
744 void AddBackrefToRegEx(unsigned BackrefNum);
745 /// Computes an arbitrary estimate for the quality of matching this pattern
746 /// at the start of \p Buffer; a distance of zero should correspond to a
747 /// perfect match.
748 unsigned computeMatchDistance(StringRef Buffer) const;
749 /// Finds the closing sequence of a regex variable usage or definition.
750 ///
751 /// \p Str has to point in the beginning of the definition (right after the
752 /// opening sequence). \p SM holds the SourceMgr used for error reporting.
753 /// \returns the offset of the closing sequence within Str, or npos if it
754 /// was not found.
755 static size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
756
757 /// Parses \p Expr for the name of a numeric variable to be defined at line
758 /// \p LineNumber, or before input is parsed if \p LineNumber is None.
759 /// \returns a pointer to the class instance representing that variable,
760 /// creating it if needed, or an error holding a diagnostic against \p SM
761 /// should defining such a variable be invalid.
762 static Expected<NumericVariable *> parseNumericVariableDefinition(
763 StringRef &Expr, FileCheckPatternContext *Context,
764 std::optional<size_t> LineNumber, ExpressionFormat ImplicitFormat,
765 const SourceMgr &SM);
766 /// Parses \p Name as a (pseudo if \p IsPseudo is true) numeric variable use
767 /// at line \p LineNumber, or before input is parsed if \p LineNumber is
768 /// None. Parameter \p Context points to the class instance holding the live
769 /// string and numeric variables. \returns the pointer to the class instance
770 /// representing that variable if successful, or an error holding a
771 /// diagnostic against \p SM otherwise.
772 static Expected<std::unique_ptr<NumericVariableUse>> parseNumericVariableUse(
773 StringRef Name, bool IsPseudo, std::optional<size_t> LineNumber,
774 FileCheckPatternContext *Context, const SourceMgr &SM);
775 enum class AllowedOperand { LineVar, LegacyLiteral, Any };
776 /// Parses \p Expr for use of a numeric operand at line \p LineNumber, or
777 /// before input is parsed if \p LineNumber is None. Accepts literal values,
778 /// numeric variables and function calls, depending on the value of \p AO.
779 /// \p MaybeInvalidConstraint indicates whether the text being parsed could
780 /// be an invalid constraint. \p Context points to the class instance holding
781 /// the live string and numeric variables. \returns the class representing
782 /// that operand in the AST of the expression or an error holding a
783 /// diagnostic against \p SM otherwise. If \p Expr starts with a "(" this
784 /// function will attempt to parse a parenthesized expression.
785 static Expected<std::unique_ptr<ExpressionAST>>
786 parseNumericOperand(StringRef &Expr, AllowedOperand AO, bool ConstraintParsed,
787 std::optional<size_t> LineNumber,
788 FileCheckPatternContext *Context, const SourceMgr &SM);
789 /// Parses and updates \p RemainingExpr for a binary operation at line
790 /// \p LineNumber, or before input is parsed if \p LineNumber is None. The
791 /// left operand of this binary operation is given in \p LeftOp and \p Expr
792 /// holds the string for the full expression, including the left operand.
793 /// Parameter \p IsLegacyLineExpr indicates whether we are parsing a legacy
794 /// @LINE expression. Parameter \p Context points to the class instance
795 /// holding the live string and numeric variables. \returns the class
796 /// representing the binary operation in the AST of the expression, or an
797 /// error holding a diagnostic against \p SM otherwise.
798 static Expected<std::unique_ptr<ExpressionAST>>
799 parseBinop(StringRef Expr, StringRef &RemainingExpr,
800 std::unique_ptr<ExpressionAST> LeftOp, bool IsLegacyLineExpr,
801 std::optional<size_t> LineNumber, FileCheckPatternContext *Context,
802 const SourceMgr &SM);
803
804 /// Parses a parenthesized expression inside \p Expr at line \p LineNumber, or
805 /// before input is parsed if \p LineNumber is None. \p Expr must start with
806 /// a '('. Accepts both literal values and numeric variables. Parameter \p
807 /// Context points to the class instance holding the live string and numeric
808 /// variables. \returns the class representing that operand in the AST of the
809 /// expression or an error holding a diagnostic against \p SM otherwise.
810 static Expected<std::unique_ptr<ExpressionAST>>
811 parseParenExpr(StringRef &Expr, std::optional<size_t> LineNumber,
812 FileCheckPatternContext *Context, const SourceMgr &SM);
813
814 /// Parses \p Expr for an argument list belonging to a call to function \p
815 /// FuncName at line \p LineNumber, or before input is parsed if \p LineNumber
816 /// is None. Parameter \p FuncLoc is the source location used for diagnostics.
817 /// Parameter \p Context points to the class instance holding the live string
818 /// and numeric variables. \returns the class representing that call in the
819 /// AST of the expression or an error holding a diagnostic against \p SM
820 /// otherwise.
821 static Expected<std::unique_ptr<ExpressionAST>>
822 parseCallExpr(StringRef &Expr, StringRef FuncName,
823 std::optional<size_t> LineNumber,
824 FileCheckPatternContext *Context, const SourceMgr &SM);
825};
826
827//===----------------------------------------------------------------------===//
828// Check Strings.
829//===----------------------------------------------------------------------===//
830
831/// A check that we found in the input file.
832struct FileCheckString {
833 /// The pattern to match.
834 Pattern Pat;
835
836 /// Which prefix name this check matched.
837 StringRef Prefix;
838
839 /// The location in the match file that the check string was specified.
840 SMLoc Loc;
841
842 /// Hold the information about the DAG/NOT strings in the program, which are
843 /// not explicitly stored otherwise. This allows for better and more accurate
844 /// diagnostic messages.
845 struct DagNotPrefixInfo {
846 Pattern DagNotPat;
847 StringRef DagNotPrefix;
848
849 DagNotPrefixInfo(const Pattern &P, StringRef S)
850 : DagNotPat(P), DagNotPrefix(S) {}
851 };
852
853 /// Hold the DAG/NOT strings occurring in the input file.
854 std::vector<DagNotPrefixInfo> DagNotStrings;
855
856 FileCheckString(Pattern &&P, StringRef S, SMLoc L,
857 std::vector<DagNotPrefixInfo> &&D)
858 : Pat(std::move(P)), Prefix(S), Loc(L), DagNotStrings(std::move(D)) {}
859
860 /// Matches check string and its "not strings" and/or "dag strings".
861 size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
862 size_t &MatchLen, FileCheckRequest &Req,
863 std::vector<FileCheckDiag> *Diags) const;
864
865 /// Verifies that there is a single line in the given \p Buffer. Errors are
866 /// reported against \p SM.
867 bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
868 /// Verifies that there is no newline in the given \p Buffer. Errors are
869 /// reported against \p SM.
870 bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
871 /// Verifies that none of the strings in \p NotStrings are found in the given
872 /// \p Buffer. Errors are reported against \p SM and diagnostics recorded in
873 /// \p Diags according to the verbosity level set in \p Req.
874 bool CheckNot(const SourceMgr &SM, StringRef Buffer,
875 const std::vector<const DagNotPrefixInfo *> &NotStrings,
876 const FileCheckRequest &Req,
877 std::vector<FileCheckDiag> *Diags) const;
878 /// Matches "dag strings" and their mixed "not strings".
879 size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
880 std::vector<const DagNotPrefixInfo *> &NotStrings,
881 const FileCheckRequest &Req,
882 std::vector<FileCheckDiag> *Diags) const;
883};
884
885} // namespace llvm
886
887#endif
888