blob: a75530ccb59e62c385a464e04ec2de57c9b10d43 [file] [log] [blame]
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +00001//===-- 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_SUPPORT_FILECHECKIMPL_H
16#define LLVM_LIB_SUPPORT_FILECHECKIMPL_H
17
18#include "llvm/ADT/Optional.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/Error.h"
22#include "llvm/Support/SourceMgr.h"
23#include <map>
24#include <string>
25#include <vector>
26
27namespace llvm {
28
29//===----------------------------------------------------------------------===//
30// Numeric substitution handling code.
31//===----------------------------------------------------------------------===//
32
Thomas Preud'homme8e966972019-03-05 23:20:29 +000033/// Type representing the format an expression value should be textualized into
34/// for matching. Used to represent both explicit format specifiers as well as
35/// implicit format from using numeric variables.
36struct ExpressionFormat {
37 enum class Kind {
38 /// Denote absence of format. Used for implicit format of literals and
39 /// empty expressions.
40 NoFormat,
41 /// Used when there are several conflicting implicit formats in an
42 /// expression.
43 Conflict,
44 /// Value is an unsigned integer and should be printed as a decimal number.
45 Unsigned,
46 /// Value should be printed as an uppercase hex number.
47 HexUpper,
48 /// Value should be printed as a lowercase hex number.
49 HexLower
50 };
51
52private:
53 Kind Value;
54
55public:
56 /// Evaluates a format to true if it can be used in a match.
57 explicit operator bool() const {
58 return Value != Kind::NoFormat && Value != Kind::Conflict;
59 }
60
61 /// Define format equality: formats are equal if neither is NoFormat and
62 /// their kinds are the same.
63 bool operator==(const ExpressionFormat &Other) const {
64 return Value != Kind::NoFormat && Value == Other.Value;
65 }
66
67 bool operator!=(const ExpressionFormat &other) const {
68 return !(*this == other);
69 }
70
71 bool operator==(Kind OtherValue) const { return Value == OtherValue; }
72
73 bool operator!=(Kind OtherValue) const { return !(*this == OtherValue); }
74
75 ExpressionFormat() : Value(Kind::NoFormat){};
76 explicit ExpressionFormat(Kind Value) : Value(Value){};
77
78 /// \returns a wildcard regular expression StringRef that matches any value
79 /// in the format represented by this instance, or an error if the format is
80 /// NoFormat or Conflict.
81 Expected<StringRef> getWildcardRegex() const;
82
83 /// \returns the string representation of \p Value in the format represented
84 /// by this instance, or an error if the format is NoFormat or Conflict.
85 Expected<std::string> getMatchingString(uint64_t Value) const;
86
87 /// \returns the value corresponding to string representation \p StrVal
88 /// according to the matching format represented by this instance or an error
89 /// with diagnostic against \p SM if \p StrVal does not correspond to a valid
90 /// and representable value.
91 Expected<uint64_t> valueFromStringRepr(StringRef StrVal,
92 const SourceMgr &SM) const;
93};
94
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +000095/// Base class representing the AST of a given expression.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +000096class ExpressionAST {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +000097public:
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +000098 virtual ~ExpressionAST() = default;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +000099
100 /// Evaluates and \returns the value of the expression represented by this
101 /// AST or an error if evaluation fails.
102 virtual Expected<uint64_t> eval() const = 0;
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000103
104 /// \returns either the implicit format of this AST, FormatConflict if
105 /// implicit formats of the AST's components conflict, or NoFormat if the AST
106 /// has no implicit format (e.g. AST is made up of a single literal).
107 virtual ExpressionFormat getImplicitFormat() const {
108 return ExpressionFormat();
109 }
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000110};
111
112/// Class representing an unsigned literal in the AST of an expression.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000113class ExpressionLiteral : public ExpressionAST {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000114private:
115 /// Actual value of the literal.
116 uint64_t Value;
117
118public:
119 /// Constructs a literal with the specified value.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000120 ExpressionLiteral(uint64_t Val) : Value(Val) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000121
122 /// \returns the literal's value.
Simon Pilgrim0e0dea82019-11-11 18:51:14 +0000123 Expected<uint64_t> eval() const override { return Value; }
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000124};
125
126/// Class to represent an undefined variable error, which quotes that
127/// variable's name when printed.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000128class UndefVarError : public ErrorInfo<UndefVarError> {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000129private:
130 StringRef VarName;
131
132public:
133 static char ID;
134
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000135 UndefVarError(StringRef VarName) : VarName(VarName) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000136
137 StringRef getVarName() const { return VarName; }
138
139 std::error_code convertToErrorCode() const override {
140 return inconvertibleErrorCode();
141 }
142
143 /// Print name of variable associated with this error.
144 void log(raw_ostream &OS) const override {
145 OS << "\"";
146 OS.write_escaped(VarName) << "\"";
147 }
148};
149
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000150/// Class representing an expression and its matching format.
151class Expression {
152private:
153 /// Pointer to AST of the expression.
154 std::unique_ptr<ExpressionAST> AST;
155
156 /// Format to use (e.g. hex upper case letters) when matching the value.
157 ExpressionFormat Format;
158
159public:
160 /// Generic constructor for an expression represented by the given \p AST and
161 /// whose matching format is \p Format.
162 Expression(std::unique_ptr<ExpressionAST> AST, ExpressionFormat Format)
163 : AST(std::move(AST)), Format(Format) {}
164
165 /// \returns pointer to AST of the expression. Pointer is guaranteed to be
166 /// valid as long as this object is.
167 ExpressionAST *getAST() const { return AST.get(); }
168
169 ExpressionFormat getFormat() const { return Format; }
170};
171
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000172/// Class representing a numeric variable and its associated current value.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000173class NumericVariable {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000174private:
175 /// Name of the numeric variable.
176 StringRef Name;
177
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000178 /// Format to use for expressions using this variable without an explicit
179 /// format.
180 ExpressionFormat ImplicitFormat;
181
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000182 /// Value of numeric variable, if defined, or None otherwise.
183 Optional<uint64_t> Value;
184
185 /// Line number where this variable is defined, or None if defined before
186 /// input is parsed. Used to determine whether a variable is defined on the
187 /// same line as a given use.
188 Optional<size_t> DefLineNumber;
189
190public:
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000191 /// Constructor for a variable \p Name with implicit format \p ImplicitFormat
192 /// defined at line \p DefLineNumber or defined before input is parsed if
193 /// \p DefLineNumber is None.
194 explicit NumericVariable(StringRef Name, ExpressionFormat ImplicitFormat,
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000195 Optional<size_t> DefLineNumber = None)
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000196 : Name(Name), ImplicitFormat(ImplicitFormat),
197 DefLineNumber(DefLineNumber) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000198
199 /// \returns name of this numeric variable.
200 StringRef getName() const { return Name; }
201
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000202 /// \returns implicit format of this numeric variable.
203 ExpressionFormat getImplicitFormat() const { return ImplicitFormat; }
204
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000205 /// \returns this variable's value.
206 Optional<uint64_t> getValue() const { return Value; }
207
208 /// Sets value of this numeric variable to \p NewValue.
209 void setValue(uint64_t NewValue) { Value = NewValue; }
210
211 /// Clears value of this numeric variable, regardless of whether it is
212 /// currently defined or not.
213 void clearValue() { Value = None; }
214
215 /// \returns the line number where this variable is defined, if any, or None
216 /// if defined before input is parsed.
Simon Pilgrim0d908e12019-11-11 18:44:13 +0000217 Optional<size_t> getDefLineNumber() const { return DefLineNumber; }
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000218};
219
220/// Class representing the use of a numeric variable in the AST of an
221/// expression.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000222class NumericVariableUse : public ExpressionAST {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000223private:
224 /// Name of the numeric variable.
225 StringRef Name;
226
227 /// Pointer to the class instance for the variable this use is about.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000228 NumericVariable *Variable;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000229
230public:
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000231 NumericVariableUse(StringRef Name, NumericVariable *Variable)
232 : Name(Name), Variable(Variable) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000233
234 /// \returns the value of the variable referenced by this instance.
Simon Pilgrim0e0dea82019-11-11 18:51:14 +0000235 Expected<uint64_t> eval() const override;
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000236
237 /// \returns implicit format of this numeric variable.
238 ExpressionFormat getImplicitFormat() const override {
239 return Variable->getImplicitFormat();
240 }
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000241};
242
243/// Type of functions evaluating a given binary operation.
244using binop_eval_t = uint64_t (*)(uint64_t, uint64_t);
245
246/// Class representing a single binary operation in the AST of an expression.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000247class BinaryOperation : public ExpressionAST {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000248private:
249 /// Left operand.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000250 std::unique_ptr<ExpressionAST> LeftOperand;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000251
252 /// Right operand.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000253 std::unique_ptr<ExpressionAST> RightOperand;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000254
255 /// Pointer to function that can evaluate this binary operation.
256 binop_eval_t EvalBinop;
257
258public:
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000259 BinaryOperation(binop_eval_t EvalBinop, std::unique_ptr<ExpressionAST> LeftOp,
260 std::unique_ptr<ExpressionAST> RightOp)
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000261 : EvalBinop(EvalBinop) {
262 LeftOperand = std::move(LeftOp);
263 RightOperand = std::move(RightOp);
264 }
265
266 /// Evaluates the value of the binary operation represented by this AST,
267 /// using EvalBinop on the result of recursively evaluating the operands.
268 /// \returns the expression value or an error if an undefined numeric
269 /// variable is used in one of the operands.
Simon Pilgrim0e0dea82019-11-11 18:51:14 +0000270 Expected<uint64_t> eval() const override;
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000271
272 /// \returns the implicit format of this AST, if any, a format conflict if
273 /// the implicit formats of the AST's components conflict, or no format if
274 /// the AST has no implicit format (e.g. AST is made of a single literal).
275 ExpressionFormat getImplicitFormat() const override;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000276};
277
278class FileCheckPatternContext;
279
280/// Class representing a substitution to perform in the RegExStr string.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000281class Substitution {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000282protected:
283 /// Pointer to a class instance holding, among other things, the table with
284 /// the values of live string variables at the start of any given CHECK line.
285 /// Used for substituting string variables with the text they were defined
286 /// as. Expressions are linked to the numeric variables they use at
287 /// parse time and directly access the value of the numeric variable to
288 /// evaluate their value.
289 FileCheckPatternContext *Context;
290
291 /// The string that needs to be substituted for something else. For a
292 /// string variable this is its name, otherwise this is the whole expression.
293 StringRef FromStr;
294
295 // Index in RegExStr of where to do the substitution.
296 size_t InsertIdx;
297
298public:
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000299 Substitution(FileCheckPatternContext *Context, StringRef VarName,
300 size_t InsertIdx)
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000301 : Context(Context), FromStr(VarName), InsertIdx(InsertIdx) {}
302
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000303 virtual ~Substitution() = default;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000304
305 /// \returns the string to be substituted for something else.
306 StringRef getFromString() const { return FromStr; }
307
308 /// \returns the index where the substitution is to be performed in RegExStr.
309 size_t getIndex() const { return InsertIdx; }
310
311 /// \returns a string containing the result of the substitution represented
312 /// by this class instance or an error if substitution failed.
313 virtual Expected<std::string> getResult() const = 0;
314};
315
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000316class StringSubstitution : public Substitution {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000317public:
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000318 StringSubstitution(FileCheckPatternContext *Context, StringRef VarName,
319 size_t InsertIdx)
320 : Substitution(Context, VarName, InsertIdx) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000321
322 /// \returns the text that the string variable in this substitution matched
323 /// when defined, or an error if the variable is undefined.
324 Expected<std::string> getResult() const override;
325};
326
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000327class NumericSubstitution : public Substitution {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000328private:
329 /// Pointer to the class representing the expression whose value is to be
330 /// substituted.
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000331 std::unique_ptr<Expression> ExpressionPointer;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000332
333public:
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000334 NumericSubstitution(FileCheckPatternContext *Context, StringRef ExpressionStr,
335 std::unique_ptr<Expression> ExpressionPointer,
336 size_t InsertIdx)
337 : Substitution(Context, ExpressionStr, InsertIdx),
338 ExpressionPointer(std::move(ExpressionPointer)) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000339
340 /// \returns a string containing the result of evaluating the expression in
341 /// this substitution, or an error if evaluation failed.
342 Expected<std::string> getResult() const override;
343};
344
345//===----------------------------------------------------------------------===//
346// Pattern handling code.
347//===----------------------------------------------------------------------===//
348
349struct FileCheckDiag;
350
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000351/// Class holding the Pattern global state, shared by all patterns: tables
352/// holding values of variables and whether they are defined or not at any
353/// given time in the matching process.
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000354class FileCheckPatternContext {
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000355 friend class Pattern;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000356
357private:
358 /// When matching a given pattern, this holds the value of all the string
359 /// variables defined in previous patterns. In a pattern, only the last
360 /// definition for a given variable is recorded in this table.
361 /// Back-references are used for uses after any the other definition.
362 StringMap<StringRef> GlobalVariableTable;
363
364 /// Map of all string variables defined so far. Used at parse time to detect
365 /// a name conflict between a numeric variable and a string variable when
366 /// the former is defined on a later line than the latter.
367 StringMap<bool> DefinedVariableTable;
368
369 /// When matching a given pattern, this holds the pointers to the classes
370 /// representing the numeric variables defined in previous patterns. When
371 /// matching a pattern all definitions for that pattern are recorded in the
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000372 /// NumericVariableDefs table in the Pattern instance of that pattern.
373 StringMap<NumericVariable *> GlobalNumericVariableTable;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000374
375 /// Pointer to the class instance representing the @LINE pseudo variable for
376 /// easily updating its value.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000377 NumericVariable *LineVariable = nullptr;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000378
379 /// Vector holding pointers to all parsed numeric variables. Used to
380 /// automatically free them once they are guaranteed to no longer be used.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000381 std::vector<std::unique_ptr<NumericVariable>> NumericVariables;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000382
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000383 /// Vector holding pointers to all parsed expressions. Used to automatically
384 /// free the expressions once they are guaranteed to no longer be used.
385 std::vector<std::unique_ptr<Expression>> Expressions;
386
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000387 /// Vector holding pointers to all substitutions. Used to automatically free
388 /// them once they are guaranteed to no longer be used.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000389 std::vector<std::unique_ptr<Substitution>> Substitutions;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000390
391public:
392 /// \returns the value of string variable \p VarName or an error if no such
393 /// variable has been defined.
394 Expected<StringRef> getPatternVarValue(StringRef VarName);
395
396 /// Defines string and numeric variables from definitions given on the
397 /// command line, passed as a vector of [#]VAR=VAL strings in
398 /// \p CmdlineDefines. \returns an error list containing diagnostics against
399 /// \p SM for all definition parsing failures, if any, or Success otherwise.
400 Error defineCmdlineVariables(std::vector<std::string> &CmdlineDefines,
401 SourceMgr &SM);
402
403 /// Create @LINE pseudo variable. Value is set when pattern are being
404 /// matched.
405 void createLineVariable();
406
407 /// Undefines local variables (variables whose name does not start with a '$'
408 /// sign), i.e. removes them from GlobalVariableTable and from
409 /// GlobalNumericVariableTable and also clears the value of numeric
410 /// variables.
411 void clearLocalVars();
412
413private:
414 /// Makes a new numeric variable and registers it for destruction when the
415 /// context is destroyed.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000416 template <class... Types> NumericVariable *makeNumericVariable(Types... args);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000417
418 /// Makes a new string substitution and registers it for destruction when the
419 /// context is destroyed.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000420 Substitution *makeStringSubstitution(StringRef VarName, size_t InsertIdx);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000421
422 /// Makes a new numeric substitution and registers it for destruction when
423 /// the context is destroyed.
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000424 Substitution *makeNumericSubstitution(StringRef ExpressionStr,
425 std::unique_ptr<Expression> Expression,
426 size_t InsertIdx);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000427};
428
429/// Class to represent an error holding a diagnostic with location information
430/// used when printing it.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000431class ErrorDiagnostic : public ErrorInfo<ErrorDiagnostic> {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000432private:
433 SMDiagnostic Diagnostic;
434
435public:
436 static char ID;
437
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000438 ErrorDiagnostic(SMDiagnostic &&Diag) : Diagnostic(Diag) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000439
440 std::error_code convertToErrorCode() const override {
441 return inconvertibleErrorCode();
442 }
443
444 /// Print diagnostic associated with this error when printing the error.
445 void log(raw_ostream &OS) const override { Diagnostic.print(nullptr, OS); }
446
447 static Error get(const SourceMgr &SM, SMLoc Loc, const Twine &ErrMsg) {
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000448 return make_error<ErrorDiagnostic>(
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000449 SM.GetMessage(Loc, SourceMgr::DK_Error, ErrMsg));
450 }
451
452 static Error get(const SourceMgr &SM, StringRef Buffer, const Twine &ErrMsg) {
453 return get(SM, SMLoc::getFromPointer(Buffer.data()), ErrMsg);
454 }
455};
456
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000457class NotFoundError : public ErrorInfo<NotFoundError> {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000458public:
459 static char ID;
460
461 std::error_code convertToErrorCode() const override {
462 return inconvertibleErrorCode();
463 }
464
465 /// Print diagnostic associated with this error when printing the error.
466 void log(raw_ostream &OS) const override {
467 OS << "String not found in input";
468 }
469};
470
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000471class Pattern {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000472 SMLoc PatternLoc;
473
474 /// A fixed string to match as the pattern or empty if this pattern requires
475 /// a regex match.
476 StringRef FixedStr;
477
478 /// A regex string to match as the pattern or empty if this pattern requires
479 /// a fixed string to match.
480 std::string RegExStr;
481
482 /// Entries in this vector represent a substitution of a string variable or
483 /// an expression in the RegExStr regex at match time. For example, in the
484 /// case of a CHECK directive with the pattern "foo[[bar]]baz[[#N+1]]",
485 /// RegExStr will contain "foobaz" and we'll get two entries in this vector
486 /// that tells us to insert the value of string variable "bar" at offset 3
487 /// and the value of expression "N+1" at offset 6.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000488 std::vector<Substitution *> Substitutions;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000489
490 /// Maps names of string variables defined in a pattern to the number of
491 /// their parenthesis group in RegExStr capturing their last definition.
492 ///
493 /// E.g. for the pattern "foo[[bar:.*]]baz([[bar]][[QUUX]][[bar:.*]])",
494 /// RegExStr will be "foo(.*)baz(\1<quux value>(.*))" where <quux value> is
495 /// the value captured for QUUX on the earlier line where it was defined, and
496 /// VariableDefs will map "bar" to the third parenthesis group which captures
497 /// the second definition of "bar".
498 ///
499 /// Note: uses std::map rather than StringMap to be able to get the key when
500 /// iterating over values.
501 std::map<StringRef, unsigned> VariableDefs;
502
503 /// Structure representing the definition of a numeric variable in a pattern.
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000504 /// It holds the pointer to the class instance holding the value and matching
505 /// format of the numeric variable whose value is being defined and the
506 /// number of the parenthesis group in RegExStr to capture that value.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000507 struct NumericVariableMatch {
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000508 /// Pointer to class instance holding the value and matching format of the
509 /// numeric variable being defined.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000510 NumericVariable *DefinedNumericVariable;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000511
512 /// Number of the parenthesis group in RegExStr that captures the value of
513 /// this numeric variable definition.
514 unsigned CaptureParenGroup;
515 };
516
517 /// Holds the number of the parenthesis group in RegExStr and pointer to the
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000518 /// corresponding NumericVariable class instance of all numeric variable
519 /// definitions. Used to set the matched value of all those variables.
520 StringMap<NumericVariableMatch> NumericVariableDefs;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000521
522 /// Pointer to a class instance holding the global state shared by all
523 /// patterns:
524 /// - separate tables with the values of live string and numeric variables
525 /// respectively at the start of any given CHECK line;
526 /// - table holding whether a string variable has been defined at any given
527 /// point during the parsing phase.
528 FileCheckPatternContext *Context;
529
530 Check::FileCheckType CheckTy;
531
532 /// Line number for this CHECK pattern or None if it is an implicit pattern.
533 /// Used to determine whether a variable definition is made on an earlier
534 /// line to the one with this CHECK.
535 Optional<size_t> LineNumber;
536
Kai Nacke5b5b2fd2019-10-11 11:59:14 +0000537 /// Ignore case while matching if set to true.
538 bool IgnoreCase = false;
539
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000540public:
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000541 Pattern(Check::FileCheckType Ty, FileCheckPatternContext *Context,
542 Optional<size_t> Line = None)
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000543 : Context(Context), CheckTy(Ty), LineNumber(Line) {}
544
545 /// \returns the location in source code.
546 SMLoc getLoc() const { return PatternLoc; }
547
548 /// \returns the pointer to the global state for all patterns in this
549 /// FileCheck instance.
550 FileCheckPatternContext *getContext() const { return Context; }
551
552 /// \returns whether \p C is a valid first character for a variable name.
553 static bool isValidVarNameStart(char C);
554
555 /// Parsing information about a variable.
556 struct VariableProperties {
557 StringRef Name;
558 bool IsPseudo;
559 };
560
561 /// Parses the string at the start of \p Str for a variable name. \returns
562 /// a VariableProperties structure holding the variable name and whether it
563 /// is the name of a pseudo variable, or an error holding a diagnostic
564 /// against \p SM if parsing fail. If parsing was successful, also strips
565 /// \p Str from the variable name.
566 static Expected<VariableProperties> parseVariable(StringRef &Str,
567 const SourceMgr &SM);
568 /// Parses \p Expr for a numeric substitution block at line \p LineNumber,
569 /// or before input is parsed if \p LineNumber is None. Parameter
570 /// \p IsLegacyLineExpr indicates whether \p Expr should be a legacy @LINE
571 /// expression and \p Context points to the class instance holding the live
572 /// string and numeric variables. \returns a pointer to the class instance
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000573 /// representing the expression whose value must be substitued, or an error
574 /// holding a diagnostic against \p SM if parsing fails. If substitution was
575 /// successful, sets \p DefinedNumericVariable to point to the class
576 /// representing the numeric variable defined in this numeric substitution
577 /// block, or None if this block does not define any variable.
578 static Expected<std::unique_ptr<Expression>> parseNumericSubstitutionBlock(
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000579 StringRef Expr, Optional<NumericVariable *> &DefinedNumericVariable,
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000580 bool IsLegacyLineExpr, Optional<size_t> LineNumber,
581 FileCheckPatternContext *Context, const SourceMgr &SM);
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000582 /// Parses the pattern in \p PatternStr and initializes this Pattern instance
583 /// accordingly.
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000584 ///
585 /// \p Prefix provides which prefix is being matched, \p Req describes the
586 /// global options that influence the parsing such as whitespace
587 /// canonicalization, \p SM provides the SourceMgr used for error reports.
588 /// \returns true in case of an error, false otherwise.
589 bool parsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
590 const FileCheckRequest &Req);
591 /// Matches the pattern string against the input buffer \p Buffer
592 ///
593 /// \returns the position that is matched or an error indicating why matching
594 /// failed. If there is a match, updates \p MatchLen with the size of the
595 /// matched string.
596 ///
597 /// The GlobalVariableTable StringMap in the FileCheckPatternContext class
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000598 /// instance provides the current values of FileCheck string variables and is
599 /// updated if this match defines new values. Likewise, the
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000600 /// GlobalNumericVariableTable StringMap in the same class provides the
601 /// current values of FileCheck numeric variables and is updated if this
602 /// match defines new numeric values.
603 Expected<size_t> match(StringRef Buffer, size_t &MatchLen,
604 const SourceMgr &SM) const;
605 /// Prints the value of successful substitutions or the name of the undefined
606 /// string or numeric variables preventing a successful substitution.
607 void printSubstitutions(const SourceMgr &SM, StringRef Buffer,
608 SMRange MatchRange = None) const;
609 void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
610 std::vector<FileCheckDiag> *Diags) const;
611
612 bool hasVariable() const {
613 return !(Substitutions.empty() && VariableDefs.empty());
614 }
615
616 Check::FileCheckType getCheckTy() const { return CheckTy; }
617
618 int getCount() const { return CheckTy.getCount(); }
619
620private:
621 bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
622 void AddBackrefToRegEx(unsigned BackrefNum);
623 /// Computes an arbitrary estimate for the quality of matching this pattern
624 /// at the start of \p Buffer; a distance of zero should correspond to a
625 /// perfect match.
626 unsigned computeMatchDistance(StringRef Buffer) const;
627 /// Finds the closing sequence of a regex variable usage or definition.
628 ///
629 /// \p Str has to point in the beginning of the definition (right after the
Simon Pilgrim6da34a82019-11-12 11:14:03 +0000630 /// opening sequence). \p SM holds the SourceMgr used for error reporting.
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000631 /// \returns the offset of the closing sequence within Str, or npos if it
632 /// was not found.
Simon Pilgrim6da34a82019-11-12 11:14:03 +0000633 static size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000634
635 /// Parses \p Expr for the name of a numeric variable to be defined at line
636 /// \p LineNumber, or before input is parsed if \p LineNumber is None.
637 /// \returns a pointer to the class instance representing that variable,
638 /// creating it if needed, or an error holding a diagnostic against \p SM
639 /// should defining such a variable be invalid.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000640 static Expected<NumericVariable *> parseNumericVariableDefinition(
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000641 StringRef &Expr, FileCheckPatternContext *Context,
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000642 Optional<size_t> LineNumber, ExpressionFormat ImplicitFormat,
643 const SourceMgr &SM);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000644 /// Parses \p Name as a (pseudo if \p IsPseudo is true) numeric variable use
645 /// at line \p LineNumber, or before input is parsed if \p LineNumber is
646 /// None. Parameter \p Context points to the class instance holding the live
647 /// string and numeric variables. \returns the pointer to the class instance
648 /// representing that variable if successful, or an error holding a
649 /// diagnostic against \p SM otherwise.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000650 static Expected<std::unique_ptr<NumericVariableUse>> parseNumericVariableUse(
651 StringRef Name, bool IsPseudo, Optional<size_t> LineNumber,
652 FileCheckPatternContext *Context, const SourceMgr &SM);
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000653 enum class AllowedOperand { LineVar, LegacyLiteral, Any };
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000654 /// Parses \p Expr for use of a numeric operand at line \p LineNumber, or
655 /// before input is parsed if \p LineNumber is None. Accepts both literal
656 /// values and numeric variables, depending on the value of \p AO. Parameter
657 /// \p Context points to the class instance holding the live string and
658 /// numeric variables. \returns the class representing that operand in the
659 /// AST of the expression or an error holding a diagnostic against \p SM
660 /// otherwise.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000661 static Expected<std::unique_ptr<ExpressionAST>>
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000662 parseNumericOperand(StringRef &Expr, AllowedOperand AO,
663 Optional<size_t> LineNumber,
664 FileCheckPatternContext *Context, const SourceMgr &SM);
665 /// Parses \p Expr for a binary operation at line \p LineNumber, or before
666 /// input is parsed if \p LineNumber is None. The left operand of this binary
667 /// operation is given in \p LeftOp and \p IsLegacyLineExpr indicates whether
668 /// we are parsing a legacy @LINE expression. Parameter \p Context points to
669 /// the class instance holding the live string and numeric variables.
670 /// \returns the class representing the binary operation in the AST of the
671 /// expression, or an error holding a diagnostic against \p SM otherwise.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000672 static Expected<std::unique_ptr<ExpressionAST>>
673 parseBinop(StringRef &Expr, std::unique_ptr<ExpressionAST> LeftOp,
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000674 bool IsLegacyLineExpr, Optional<size_t> LineNumber,
675 FileCheckPatternContext *Context, const SourceMgr &SM);
676};
677
678//===----------------------------------------------------------------------===//
679// Check Strings.
680//===----------------------------------------------------------------------===//
681
682/// A check that we found in the input file.
683struct FileCheckString {
684 /// The pattern to match.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000685 Pattern Pat;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000686
687 /// Which prefix name this check matched.
688 StringRef Prefix;
689
690 /// The location in the match file that the check string was specified.
691 SMLoc Loc;
692
693 /// All of the strings that are disallowed from occurring between this match
694 /// string and the previous one (or start of file).
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000695 std::vector<Pattern> DagNotStrings;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000696
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000697 FileCheckString(const Pattern &P, StringRef S, SMLoc L)
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000698 : Pat(P), Prefix(S), Loc(L) {}
699
700 /// Matches check string and its "not strings" and/or "dag strings".
701 size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
702 size_t &MatchLen, FileCheckRequest &Req,
703 std::vector<FileCheckDiag> *Diags) const;
704
705 /// Verifies that there is a single line in the given \p Buffer. Errors are
706 /// reported against \p SM.
707 bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
708 /// Verifies that there is no newline in the given \p Buffer. Errors are
709 /// reported against \p SM.
710 bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
711 /// Verifies that none of the strings in \p NotStrings are found in the given
712 /// \p Buffer. Errors are reported against \p SM and diagnostics recorded in
713 /// \p Diags according to the verbosity level set in \p Req.
714 bool CheckNot(const SourceMgr &SM, StringRef Buffer,
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000715 const std::vector<const Pattern *> &NotStrings,
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000716 const FileCheckRequest &Req,
717 std::vector<FileCheckDiag> *Diags) const;
718 /// Matches "dag strings" and their mixed "not strings".
719 size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000720 std::vector<const Pattern *> &NotStrings,
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000721 const FileCheckRequest &Req,
722 std::vector<FileCheckDiag> *Diags) const;
723};
724
725} // namespace llvm
726
727#endif