blob: 6ca67ec2964c29c32fc292a8ad50b4c02dc9af21 [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
Thomas Preud'hommeb34c9042020-04-14 18:24:32 +010018#include "llvm/Support/FileCheck.h"
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +000019#include "llvm/ADT/Optional.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/SourceMgr.h"
24#include <map>
25#include <string>
26#include <vector>
27
28namespace llvm {
29
30//===----------------------------------------------------------------------===//
31// Numeric substitution handling code.
32//===----------------------------------------------------------------------===//
33
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +000034class ExpressionValue;
35
Thomas Preud'homme8e966972019-03-05 23:20:29 +000036/// 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.
39struct ExpressionFormat {
40 enum class Kind {
41 /// Denote absence of format. Used for implicit format of literals and
42 /// empty expressions.
43 NoFormat,
Thomas Preud'homme8e966972019-03-05 23:20:29 +000044 /// Value is an unsigned integer and should be printed as a decimal number.
45 Unsigned,
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +000046 /// Value is a signed integer and should be printed as a decimal number.
47 Signed,
Thomas Preud'homme8e966972019-03-05 23:20:29 +000048 /// Value should be printed as an uppercase hex number.
49 HexUpper,
50 /// Value should be printed as a lowercase hex number.
51 HexLower
52 };
53
54private:
55 Kind Value;
56
57public:
58 /// Evaluates a format to true if it can be used in a match.
Thomas Preud'homme97431232020-04-04 01:02:45 +010059 explicit operator bool() const { return Value != Kind::NoFormat; }
Thomas Preud'homme8e966972019-03-05 23:20:29 +000060
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
Paul Walkered9df862020-06-01 10:09:58 +000067 bool operator!=(const ExpressionFormat &Other) const {
68 return !(*this == Other);
Thomas Preud'homme8e966972019-03-05 23:20:29 +000069 }
70
71 bool operator==(Kind OtherValue) const { return Value == OtherValue; }
72
73 bool operator!=(Kind OtherValue) const { return !(*this == OtherValue); }
74
Thomas Preud'homme97431232020-04-04 01:02:45 +010075 /// \returns the format specifier corresponding to this format as a string.
76 StringRef toString() const;
77
Thomas Preud'homme8e966972019-03-05 23:20:29 +000078 ExpressionFormat() : Value(Kind::NoFormat){};
79 explicit ExpressionFormat(Kind Value) : Value(Value){};
80
81 /// \returns a wildcard regular expression StringRef that matches any value
82 /// in the format represented by this instance, or an error if the format is
Thomas Preud'homme97431232020-04-04 01:02:45 +010083 /// NoFormat.
Thomas Preud'homme8e966972019-03-05 23:20:29 +000084 Expected<StringRef> getWildcardRegex() const;
85
86 /// \returns the string representation of \p Value in the format represented
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +000087 /// by this instance, or an error if conversion to this format failed or the
88 /// format is NoFormat.
89 Expected<std::string> getMatchingString(ExpressionValue Value) const;
Thomas Preud'homme8e966972019-03-05 23:20:29 +000090
91 /// \returns the value corresponding to string representation \p StrVal
92 /// according to the matching format represented by this instance or an error
93 /// with diagnostic against \p SM if \p StrVal does not correspond to a valid
94 /// and representable value.
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +000095 Expected<ExpressionValue> valueFromStringRepr(StringRef StrVal,
96 const SourceMgr &SM) const;
Thomas Preud'homme8e966972019-03-05 23:20:29 +000097};
98
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +000099/// Class to represent an overflow error that might result when manipulating a
100/// value.
101class OverflowError : public ErrorInfo<OverflowError> {
102public:
103 static char ID;
104
105 std::error_code convertToErrorCode() const override {
106 return std::make_error_code(std::errc::value_too_large);
107 }
108
109 void log(raw_ostream &OS) const override { OS << "overflow error"; }
110};
111
112/// Class representing a numeric value.
113class ExpressionValue {
114private:
115 uint64_t Value;
116 bool Negative;
117
118public:
119 template <class T>
120 explicit ExpressionValue(T Val) : Value(Val), Negative(Val < 0) {}
121
Paul Walkered9df862020-06-01 10:09:58 +0000122 bool operator==(const ExpressionValue &Other) const {
123 return Value == Other.Value && isNegative() == Other.isNegative();
124 }
125
126 bool operator!=(const ExpressionValue &Other) const {
127 return !(*this == Other);
128 }
129
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000130 /// Returns true if value is signed and negative, false otherwise.
Paul Walkered9df862020-06-01 10:09:58 +0000131 bool isNegative() const {
132 assert((Value != 0 || !Negative) && "Unexpected negative zero!");
133 return Negative;
134 }
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000135
136 /// \returns the value as a signed integer or an error if the value is out of
137 /// range.
138 Expected<int64_t> getSignedValue() const;
139
140 /// \returns the value as an unsigned integer or an error if the value is out
141 /// of range.
142 Expected<uint64_t> getUnsignedValue() const;
143
144 /// \returns an unsigned ExpressionValue instance whose value is the absolute
145 /// value to this object's value.
146 ExpressionValue getAbsolute() const;
147};
148
149/// Performs operation and \returns its result or an error in case of failure,
150/// such as if an overflow occurs.
151Expected<ExpressionValue> operator+(const ExpressionValue &Lhs,
152 const ExpressionValue &Rhs);
153Expected<ExpressionValue> operator-(const ExpressionValue &Lhs,
154 const ExpressionValue &Rhs);
Paul Walker95db1e72020-06-01 10:09:58 +0000155Expected<ExpressionValue> operator*(const ExpressionValue &Lhs,
156 const ExpressionValue &Rhs);
157Expected<ExpressionValue> operator/(const ExpressionValue &Lhs,
158 const ExpressionValue &Rhs);
Paul Walker8fd22702020-05-14 10:32:58 +0000159Expected<ExpressionValue> max(const ExpressionValue &Lhs,
160 const ExpressionValue &Rhs);
161Expected<ExpressionValue> min(const ExpressionValue &Lhs,
162 const ExpressionValue &Rhs);
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000163
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000164/// Base class representing the AST of a given expression.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000165class ExpressionAST {
Thomas Preud'homme97431232020-04-04 01:02:45 +0100166private:
167 StringRef ExpressionStr;
168
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000169public:
Thomas Preud'homme97431232020-04-04 01:02:45 +0100170 ExpressionAST(StringRef ExpressionStr) : ExpressionStr(ExpressionStr) {}
171
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000172 virtual ~ExpressionAST() = default;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000173
Thomas Preud'homme97431232020-04-04 01:02:45 +0100174 StringRef getExpressionStr() const { return ExpressionStr; }
175
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000176 /// Evaluates and \returns the value of the expression represented by this
177 /// AST or an error if evaluation fails.
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000178 virtual Expected<ExpressionValue> eval() const = 0;
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000179
Thomas Preud'homme97431232020-04-04 01:02:45 +0100180 /// \returns either the implicit format of this AST, a diagnostic against
181 /// \p SM if implicit formats of the AST's components conflict, or NoFormat
182 /// if the AST has no implicit format (e.g. AST is made up of a single
183 /// literal).
184 virtual Expected<ExpressionFormat>
185 getImplicitFormat(const SourceMgr &SM) const {
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000186 return ExpressionFormat();
187 }
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000188};
189
190/// Class representing an unsigned literal in the AST of an expression.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000191class ExpressionLiteral : public ExpressionAST {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000192private:
193 /// Actual value of the literal.
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000194 ExpressionValue Value;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000195
196public:
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000197 template <class T>
198 explicit ExpressionLiteral(StringRef ExpressionStr, T Val)
Thomas Preud'homme97431232020-04-04 01:02:45 +0100199 : ExpressionAST(ExpressionStr), Value(Val) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000200
201 /// \returns the literal's value.
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000202 Expected<ExpressionValue> eval() const override { return Value; }
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000203};
204
205/// Class to represent an undefined variable error, which quotes that
206/// variable's name when printed.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000207class UndefVarError : public ErrorInfo<UndefVarError> {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000208private:
209 StringRef VarName;
210
211public:
212 static char ID;
213
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000214 UndefVarError(StringRef VarName) : VarName(VarName) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000215
216 StringRef getVarName() const { return VarName; }
217
218 std::error_code convertToErrorCode() const override {
219 return inconvertibleErrorCode();
220 }
221
222 /// Print name of variable associated with this error.
223 void log(raw_ostream &OS) const override {
224 OS << "\"";
225 OS.write_escaped(VarName) << "\"";
226 }
227};
228
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000229/// Class representing an expression and its matching format.
230class Expression {
231private:
232 /// Pointer to AST of the expression.
233 std::unique_ptr<ExpressionAST> AST;
234
235 /// Format to use (e.g. hex upper case letters) when matching the value.
236 ExpressionFormat Format;
237
238public:
239 /// Generic constructor for an expression represented by the given \p AST and
240 /// whose matching format is \p Format.
241 Expression(std::unique_ptr<ExpressionAST> AST, ExpressionFormat Format)
242 : AST(std::move(AST)), Format(Format) {}
243
244 /// \returns pointer to AST of the expression. Pointer is guaranteed to be
245 /// valid as long as this object is.
246 ExpressionAST *getAST() const { return AST.get(); }
247
248 ExpressionFormat getFormat() const { return Format; }
249};
250
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000251/// Class representing a numeric variable and its associated current value.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000252class NumericVariable {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000253private:
254 /// Name of the numeric variable.
255 StringRef Name;
256
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000257 /// Format to use for expressions using this variable without an explicit
258 /// format.
259 ExpressionFormat ImplicitFormat;
260
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000261 /// Value of numeric variable, if defined, or None otherwise.
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000262 Optional<ExpressionValue> Value;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000263
264 /// Line number where this variable is defined, or None if defined before
265 /// input is parsed. Used to determine whether a variable is defined on the
266 /// same line as a given use.
267 Optional<size_t> DefLineNumber;
268
269public:
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000270 /// Constructor for a variable \p Name with implicit format \p ImplicitFormat
271 /// defined at line \p DefLineNumber or defined before input is parsed if
272 /// \p DefLineNumber is None.
273 explicit NumericVariable(StringRef Name, ExpressionFormat ImplicitFormat,
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000274 Optional<size_t> DefLineNumber = None)
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000275 : Name(Name), ImplicitFormat(ImplicitFormat),
276 DefLineNumber(DefLineNumber) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000277
278 /// \returns name of this numeric variable.
279 StringRef getName() const { return Name; }
280
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000281 /// \returns implicit format of this numeric variable.
282 ExpressionFormat getImplicitFormat() const { return ImplicitFormat; }
283
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000284 /// \returns this variable's value.
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000285 Optional<ExpressionValue> getValue() const { return Value; }
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000286
287 /// Sets value of this numeric variable to \p NewValue.
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000288 void setValue(ExpressionValue NewValue) { Value = NewValue; }
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000289
290 /// Clears value of this numeric variable, regardless of whether it is
291 /// currently defined or not.
292 void clearValue() { Value = None; }
293
294 /// \returns the line number where this variable is defined, if any, or None
295 /// if defined before input is parsed.
Simon Pilgrim0d908e12019-11-11 18:44:13 +0000296 Optional<size_t> getDefLineNumber() const { return DefLineNumber; }
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000297};
298
299/// Class representing the use of a numeric variable in the AST of an
300/// expression.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000301class NumericVariableUse : public ExpressionAST {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000302private:
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000303 /// Pointer to the class instance for the variable this use is about.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000304 NumericVariable *Variable;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000305
306public:
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000307 NumericVariableUse(StringRef Name, NumericVariable *Variable)
Thomas Preud'homme97431232020-04-04 01:02:45 +0100308 : ExpressionAST(Name), Variable(Variable) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000309 /// \returns the value of the variable referenced by this instance.
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000310 Expected<ExpressionValue> eval() const override;
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000311
312 /// \returns implicit format of this numeric variable.
Thomas Preud'homme97431232020-04-04 01:02:45 +0100313 Expected<ExpressionFormat>
314 getImplicitFormat(const SourceMgr &SM) const override {
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000315 return Variable->getImplicitFormat();
316 }
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000317};
318
319/// Type of functions evaluating a given binary operation.
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000320using binop_eval_t = Expected<ExpressionValue> (*)(const ExpressionValue &,
321 const ExpressionValue &);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000322
323/// Class representing a single binary operation in the AST of an expression.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000324class BinaryOperation : public ExpressionAST {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000325private:
326 /// Left operand.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000327 std::unique_ptr<ExpressionAST> LeftOperand;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000328
329 /// Right operand.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000330 std::unique_ptr<ExpressionAST> RightOperand;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000331
332 /// Pointer to function that can evaluate this binary operation.
333 binop_eval_t EvalBinop;
334
335public:
Thomas Preud'homme97431232020-04-04 01:02:45 +0100336 BinaryOperation(StringRef ExpressionStr, binop_eval_t EvalBinop,
337 std::unique_ptr<ExpressionAST> LeftOp,
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000338 std::unique_ptr<ExpressionAST> RightOp)
Thomas Preud'homme97431232020-04-04 01:02:45 +0100339 : ExpressionAST(ExpressionStr), EvalBinop(EvalBinop) {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000340 LeftOperand = std::move(LeftOp);
341 RightOperand = std::move(RightOp);
342 }
343
344 /// Evaluates the value of the binary operation represented by this AST,
345 /// using EvalBinop on the result of recursively evaluating the operands.
346 /// \returns the expression value or an error if an undefined numeric
347 /// variable is used in one of the operands.
Thomas Preud'homme23ac16c2019-03-05 23:20:29 +0000348 Expected<ExpressionValue> eval() const override;
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000349
Thomas Preud'homme97431232020-04-04 01:02:45 +0100350 /// \returns the implicit format of this AST, if any, a diagnostic against
351 /// \p SM if the implicit formats of the AST's components conflict, or no
352 /// format if the AST has no implicit format (e.g. AST is made of a single
353 /// literal).
354 Expected<ExpressionFormat>
355 getImplicitFormat(const SourceMgr &SM) const override;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000356};
357
358class FileCheckPatternContext;
359
360/// Class representing a substitution to perform in the RegExStr string.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000361class Substitution {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000362protected:
363 /// Pointer to a class instance holding, among other things, the table with
364 /// the values of live string variables at the start of any given CHECK line.
365 /// Used for substituting string variables with the text they were defined
366 /// as. Expressions are linked to the numeric variables they use at
367 /// parse time and directly access the value of the numeric variable to
368 /// evaluate their value.
369 FileCheckPatternContext *Context;
370
371 /// The string that needs to be substituted for something else. For a
372 /// string variable this is its name, otherwise this is the whole expression.
373 StringRef FromStr;
374
375 // Index in RegExStr of where to do the substitution.
376 size_t InsertIdx;
377
378public:
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000379 Substitution(FileCheckPatternContext *Context, StringRef VarName,
380 size_t InsertIdx)
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000381 : Context(Context), FromStr(VarName), InsertIdx(InsertIdx) {}
382
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000383 virtual ~Substitution() = default;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000384
385 /// \returns the string to be substituted for something else.
386 StringRef getFromString() const { return FromStr; }
387
388 /// \returns the index where the substitution is to be performed in RegExStr.
389 size_t getIndex() const { return InsertIdx; }
390
391 /// \returns a string containing the result of the substitution represented
392 /// by this class instance or an error if substitution failed.
393 virtual Expected<std::string> getResult() const = 0;
394};
395
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000396class StringSubstitution : public Substitution {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000397public:
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000398 StringSubstitution(FileCheckPatternContext *Context, StringRef VarName,
399 size_t InsertIdx)
400 : Substitution(Context, VarName, InsertIdx) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000401
402 /// \returns the text that the string variable in this substitution matched
403 /// when defined, or an error if the variable is undefined.
404 Expected<std::string> getResult() const override;
405};
406
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000407class NumericSubstitution : public Substitution {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000408private:
409 /// Pointer to the class representing the expression whose value is to be
410 /// substituted.
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000411 std::unique_ptr<Expression> ExpressionPointer;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000412
413public:
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000414 NumericSubstitution(FileCheckPatternContext *Context, StringRef ExpressionStr,
415 std::unique_ptr<Expression> ExpressionPointer,
416 size_t InsertIdx)
417 : Substitution(Context, ExpressionStr, InsertIdx),
418 ExpressionPointer(std::move(ExpressionPointer)) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000419
420 /// \returns a string containing the result of evaluating the expression in
421 /// this substitution, or an error if evaluation failed.
422 Expected<std::string> getResult() const override;
423};
424
425//===----------------------------------------------------------------------===//
426// Pattern handling code.
427//===----------------------------------------------------------------------===//
428
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000429/// Class holding the Pattern global state, shared by all patterns: tables
430/// holding values of variables and whether they are defined or not at any
431/// given time in the matching process.
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000432class FileCheckPatternContext {
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000433 friend class Pattern;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000434
435private:
436 /// When matching a given pattern, this holds the value of all the string
437 /// variables defined in previous patterns. In a pattern, only the last
438 /// definition for a given variable is recorded in this table.
439 /// Back-references are used for uses after any the other definition.
440 StringMap<StringRef> GlobalVariableTable;
441
442 /// Map of all string variables defined so far. Used at parse time to detect
443 /// a name conflict between a numeric variable and a string variable when
444 /// the former is defined on a later line than the latter.
445 StringMap<bool> DefinedVariableTable;
446
447 /// When matching a given pattern, this holds the pointers to the classes
448 /// representing the numeric variables defined in previous patterns. When
449 /// matching a pattern all definitions for that pattern are recorded in the
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000450 /// NumericVariableDefs table in the Pattern instance of that pattern.
451 StringMap<NumericVariable *> GlobalNumericVariableTable;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000452
453 /// Pointer to the class instance representing the @LINE pseudo variable for
454 /// easily updating its value.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000455 NumericVariable *LineVariable = nullptr;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000456
457 /// Vector holding pointers to all parsed numeric variables. Used to
458 /// automatically free them once they are guaranteed to no longer be used.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000459 std::vector<std::unique_ptr<NumericVariable>> NumericVariables;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000460
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000461 /// Vector holding pointers to all parsed expressions. Used to automatically
462 /// free the expressions once they are guaranteed to no longer be used.
463 std::vector<std::unique_ptr<Expression>> Expressions;
464
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000465 /// Vector holding pointers to all substitutions. Used to automatically free
466 /// them once they are guaranteed to no longer be used.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000467 std::vector<std::unique_ptr<Substitution>> Substitutions;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000468
469public:
470 /// \returns the value of string variable \p VarName or an error if no such
471 /// variable has been defined.
472 Expected<StringRef> getPatternVarValue(StringRef VarName);
473
474 /// Defines string and numeric variables from definitions given on the
475 /// command line, passed as a vector of [#]VAR=VAL strings in
476 /// \p CmdlineDefines. \returns an error list containing diagnostics against
477 /// \p SM for all definition parsing failures, if any, or Success otherwise.
Georgii Rymar76e0ab22020-04-15 15:30:21 +0300478 Error defineCmdlineVariables(ArrayRef<StringRef> CmdlineDefines,
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000479 SourceMgr &SM);
480
481 /// Create @LINE pseudo variable. Value is set when pattern are being
482 /// matched.
483 void createLineVariable();
484
485 /// Undefines local variables (variables whose name does not start with a '$'
486 /// sign), i.e. removes them from GlobalVariableTable and from
487 /// GlobalNumericVariableTable and also clears the value of numeric
488 /// variables.
489 void clearLocalVars();
490
491private:
492 /// Makes a new numeric variable and registers it for destruction when the
493 /// context is destroyed.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000494 template <class... Types> NumericVariable *makeNumericVariable(Types... args);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000495
496 /// Makes a new string substitution and registers it for destruction when the
497 /// context is destroyed.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000498 Substitution *makeStringSubstitution(StringRef VarName, size_t InsertIdx);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000499
500 /// Makes a new numeric substitution and registers it for destruction when
501 /// the context is destroyed.
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000502 Substitution *makeNumericSubstitution(StringRef ExpressionStr,
503 std::unique_ptr<Expression> Expression,
504 size_t InsertIdx);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000505};
506
507/// Class to represent an error holding a diagnostic with location information
508/// used when printing it.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000509class ErrorDiagnostic : public ErrorInfo<ErrorDiagnostic> {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000510private:
511 SMDiagnostic Diagnostic;
512
513public:
514 static char ID;
515
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000516 ErrorDiagnostic(SMDiagnostic &&Diag) : Diagnostic(Diag) {}
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000517
518 std::error_code convertToErrorCode() const override {
519 return inconvertibleErrorCode();
520 }
521
522 /// Print diagnostic associated with this error when printing the error.
523 void log(raw_ostream &OS) const override { Diagnostic.print(nullptr, OS); }
524
525 static Error get(const SourceMgr &SM, SMLoc Loc, const Twine &ErrMsg) {
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000526 return make_error<ErrorDiagnostic>(
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000527 SM.GetMessage(Loc, SourceMgr::DK_Error, ErrMsg));
528 }
529
530 static Error get(const SourceMgr &SM, StringRef Buffer, const Twine &ErrMsg) {
531 return get(SM, SMLoc::getFromPointer(Buffer.data()), ErrMsg);
532 }
533};
534
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000535class NotFoundError : public ErrorInfo<NotFoundError> {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000536public:
537 static char ID;
538
539 std::error_code convertToErrorCode() const override {
540 return inconvertibleErrorCode();
541 }
542
543 /// Print diagnostic associated with this error when printing the error.
544 void log(raw_ostream &OS) const override {
545 OS << "String not found in input";
546 }
547};
548
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000549class Pattern {
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000550 SMLoc PatternLoc;
551
552 /// A fixed string to match as the pattern or empty if this pattern requires
553 /// a regex match.
554 StringRef FixedStr;
555
556 /// A regex string to match as the pattern or empty if this pattern requires
557 /// a fixed string to match.
558 std::string RegExStr;
559
560 /// Entries in this vector represent a substitution of a string variable or
561 /// an expression in the RegExStr regex at match time. For example, in the
562 /// case of a CHECK directive with the pattern "foo[[bar]]baz[[#N+1]]",
563 /// RegExStr will contain "foobaz" and we'll get two entries in this vector
564 /// that tells us to insert the value of string variable "bar" at offset 3
565 /// and the value of expression "N+1" at offset 6.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000566 std::vector<Substitution *> Substitutions;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000567
568 /// Maps names of string variables defined in a pattern to the number of
569 /// their parenthesis group in RegExStr capturing their last definition.
570 ///
571 /// E.g. for the pattern "foo[[bar:.*]]baz([[bar]][[QUUX]][[bar:.*]])",
572 /// RegExStr will be "foo(.*)baz(\1<quux value>(.*))" where <quux value> is
573 /// the value captured for QUUX on the earlier line where it was defined, and
574 /// VariableDefs will map "bar" to the third parenthesis group which captures
575 /// the second definition of "bar".
576 ///
577 /// Note: uses std::map rather than StringMap to be able to get the key when
578 /// iterating over values.
579 std::map<StringRef, unsigned> VariableDefs;
580
581 /// Structure representing the definition of a numeric variable in a pattern.
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000582 /// It holds the pointer to the class instance holding the value and matching
583 /// format of the numeric variable whose value is being defined and the
584 /// number of the parenthesis group in RegExStr to capture that value.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000585 struct NumericVariableMatch {
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000586 /// Pointer to class instance holding the value and matching format of the
587 /// numeric variable being defined.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000588 NumericVariable *DefinedNumericVariable;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000589
590 /// Number of the parenthesis group in RegExStr that captures the value of
591 /// this numeric variable definition.
592 unsigned CaptureParenGroup;
593 };
594
595 /// Holds the number of the parenthesis group in RegExStr and pointer to the
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000596 /// corresponding NumericVariable class instance of all numeric variable
597 /// definitions. Used to set the matched value of all those variables.
598 StringMap<NumericVariableMatch> NumericVariableDefs;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000599
600 /// Pointer to a class instance holding the global state shared by all
601 /// patterns:
602 /// - separate tables with the values of live string and numeric variables
603 /// respectively at the start of any given CHECK line;
604 /// - table holding whether a string variable has been defined at any given
605 /// point during the parsing phase.
606 FileCheckPatternContext *Context;
607
608 Check::FileCheckType CheckTy;
609
610 /// Line number for this CHECK pattern or None if it is an implicit pattern.
611 /// Used to determine whether a variable definition is made on an earlier
612 /// line to the one with this CHECK.
613 Optional<size_t> LineNumber;
614
Kai Nacke5b5b2fd2019-10-11 11:59:14 +0000615 /// Ignore case while matching if set to true.
616 bool IgnoreCase = false;
617
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000618public:
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000619 Pattern(Check::FileCheckType Ty, FileCheckPatternContext *Context,
620 Optional<size_t> Line = None)
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000621 : Context(Context), CheckTy(Ty), LineNumber(Line) {}
622
623 /// \returns the location in source code.
624 SMLoc getLoc() const { return PatternLoc; }
625
626 /// \returns the pointer to the global state for all patterns in this
627 /// FileCheck instance.
628 FileCheckPatternContext *getContext() const { return Context; }
629
630 /// \returns whether \p C is a valid first character for a variable name.
631 static bool isValidVarNameStart(char C);
632
633 /// Parsing information about a variable.
634 struct VariableProperties {
635 StringRef Name;
636 bool IsPseudo;
637 };
638
639 /// Parses the string at the start of \p Str for a variable name. \returns
640 /// a VariableProperties structure holding the variable name and whether it
641 /// is the name of a pseudo variable, or an error holding a diagnostic
642 /// against \p SM if parsing fail. If parsing was successful, also strips
643 /// \p Str from the variable name.
644 static Expected<VariableProperties> parseVariable(StringRef &Str,
645 const SourceMgr &SM);
646 /// Parses \p Expr for a numeric substitution block at line \p LineNumber,
647 /// or before input is parsed if \p LineNumber is None. Parameter
648 /// \p IsLegacyLineExpr indicates whether \p Expr should be a legacy @LINE
649 /// expression and \p Context points to the class instance holding the live
650 /// string and numeric variables. \returns a pointer to the class instance
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000651 /// representing the expression whose value must be substitued, or an error
652 /// holding a diagnostic against \p SM if parsing fails. If substitution was
653 /// successful, sets \p DefinedNumericVariable to point to the class
654 /// representing the numeric variable defined in this numeric substitution
655 /// block, or None if this block does not define any variable.
656 static Expected<std::unique_ptr<Expression>> parseNumericSubstitutionBlock(
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000657 StringRef Expr, Optional<NumericVariable *> &DefinedNumericVariable,
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000658 bool IsLegacyLineExpr, Optional<size_t> LineNumber,
659 FileCheckPatternContext *Context, const SourceMgr &SM);
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000660 /// Parses the pattern in \p PatternStr and initializes this Pattern instance
661 /// accordingly.
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000662 ///
663 /// \p Prefix provides which prefix is being matched, \p Req describes the
664 /// global options that influence the parsing such as whitespace
665 /// canonicalization, \p SM provides the SourceMgr used for error reports.
666 /// \returns true in case of an error, false otherwise.
667 bool parsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
668 const FileCheckRequest &Req);
669 /// Matches the pattern string against the input buffer \p Buffer
670 ///
671 /// \returns the position that is matched or an error indicating why matching
672 /// failed. If there is a match, updates \p MatchLen with the size of the
673 /// matched string.
674 ///
675 /// The GlobalVariableTable StringMap in the FileCheckPatternContext class
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000676 /// instance provides the current values of FileCheck string variables and is
677 /// updated if this match defines new values. Likewise, the
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000678 /// GlobalNumericVariableTable StringMap in the same class provides the
679 /// current values of FileCheck numeric variables and is updated if this
680 /// match defines new numeric values.
681 Expected<size_t> match(StringRef Buffer, size_t &MatchLen,
682 const SourceMgr &SM) const;
683 /// Prints the value of successful substitutions or the name of the undefined
684 /// string or numeric variables preventing a successful substitution.
685 void printSubstitutions(const SourceMgr &SM, StringRef Buffer,
686 SMRange MatchRange = None) const;
687 void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
688 std::vector<FileCheckDiag> *Diags) const;
689
690 bool hasVariable() const {
691 return !(Substitutions.empty() && VariableDefs.empty());
692 }
693
694 Check::FileCheckType getCheckTy() const { return CheckTy; }
695
696 int getCount() const { return CheckTy.getCount(); }
697
698private:
699 bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
700 void AddBackrefToRegEx(unsigned BackrefNum);
701 /// Computes an arbitrary estimate for the quality of matching this pattern
702 /// at the start of \p Buffer; a distance of zero should correspond to a
703 /// perfect match.
704 unsigned computeMatchDistance(StringRef Buffer) const;
705 /// Finds the closing sequence of a regex variable usage or definition.
706 ///
707 /// \p Str has to point in the beginning of the definition (right after the
Simon Pilgrim6da34a82019-11-12 11:14:03 +0000708 /// opening sequence). \p SM holds the SourceMgr used for error reporting.
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000709 /// \returns the offset of the closing sequence within Str, or npos if it
710 /// was not found.
Simon Pilgrim6da34a82019-11-12 11:14:03 +0000711 static size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000712
713 /// Parses \p Expr for the name of a numeric variable to be defined at line
714 /// \p LineNumber, or before input is parsed if \p LineNumber is None.
715 /// \returns a pointer to the class instance representing that variable,
716 /// creating it if needed, or an error holding a diagnostic against \p SM
717 /// should defining such a variable be invalid.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000718 static Expected<NumericVariable *> parseNumericVariableDefinition(
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000719 StringRef &Expr, FileCheckPatternContext *Context,
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000720 Optional<size_t> LineNumber, ExpressionFormat ImplicitFormat,
721 const SourceMgr &SM);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000722 /// Parses \p Name as a (pseudo if \p IsPseudo is true) numeric variable use
723 /// at line \p LineNumber, or before input is parsed if \p LineNumber is
724 /// None. Parameter \p Context points to the class instance holding the live
725 /// string and numeric variables. \returns the pointer to the class instance
726 /// representing that variable if successful, or an error holding a
727 /// diagnostic against \p SM otherwise.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000728 static Expected<std::unique_ptr<NumericVariableUse>> parseNumericVariableUse(
729 StringRef Name, bool IsPseudo, Optional<size_t> LineNumber,
730 FileCheckPatternContext *Context, const SourceMgr &SM);
Thomas Preud'homme8e966972019-03-05 23:20:29 +0000731 enum class AllowedOperand { LineVar, LegacyLiteral, Any };
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000732 /// Parses \p Expr for use of a numeric operand at line \p LineNumber, or
Paul Walker8fd22702020-05-14 10:32:58 +0000733 /// before input is parsed if \p LineNumber is None. Accepts literal values,
734 /// numeric variables and function calls, depending on the value of \p AO.
Thomas Preud'homme47934c72019-07-17 16:15:52 +0100735 /// \p MaybeInvalidConstraint indicates whether the text being parsed could
736 /// be an invalid constraint. \p Context points to the class instance holding
737 /// the live string and numeric variables. \returns the class representing
738 /// that operand in the AST of the expression or an error holding a
739 /// diagnostic against \p SM otherwise. If \p Expr starts with a "(" this
740 /// function will attempt to parse a parenthesized expression.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000741 static Expected<std::unique_ptr<ExpressionAST>>
Thomas Preud'homme47934c72019-07-17 16:15:52 +0100742 parseNumericOperand(StringRef &Expr, AllowedOperand AO, bool ConstraintParsed,
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000743 Optional<size_t> LineNumber,
744 FileCheckPatternContext *Context, const SourceMgr &SM);
Thomas Preud'homme97431232020-04-04 01:02:45 +0100745 /// Parses and updates \p RemainingExpr for a binary operation at line
746 /// \p LineNumber, or before input is parsed if \p LineNumber is None. The
747 /// left operand of this binary operation is given in \p LeftOp and \p Expr
748 /// holds the string for the full expression, including the left operand.
749 /// Parameter \p IsLegacyLineExpr indicates whether we are parsing a legacy
750 /// @LINE expression. Parameter \p Context points to the class instance
751 /// holding the live string and numeric variables. \returns the class
752 /// representing the binary operation in the AST of the expression, or an
753 /// error holding a diagnostic against \p SM otherwise.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000754 static Expected<std::unique_ptr<ExpressionAST>>
Thomas Preud'homme97431232020-04-04 01:02:45 +0100755 parseBinop(StringRef Expr, StringRef &RemainingExpr,
756 std::unique_ptr<ExpressionAST> LeftOp, bool IsLegacyLineExpr,
757 Optional<size_t> LineNumber, FileCheckPatternContext *Context,
758 const SourceMgr &SM);
Alex Richardson3be5e532020-05-27 15:13:42 +0100759
760 /// Parses a parenthesized expression inside \p Expr at line \p LineNumber, or
761 /// before input is parsed if \p LineNumber is None. \p Expr must start with
762 /// a '('. Accepts both literal values and numeric variables. Parameter \p
763 /// Context points to the class instance holding the live string and numeric
764 /// variables. \returns the class representing that operand in the AST of the
765 /// expression or an error holding a diagnostic against \p SM otherwise.
766 static Expected<std::unique_ptr<ExpressionAST>>
767 parseParenExpr(StringRef &Expr, Optional<size_t> LineNumber,
768 FileCheckPatternContext *Context, const SourceMgr &SM);
Paul Walker8fd22702020-05-14 10:32:58 +0000769
770 /// Parses \p Expr for an argument list belonging to a call to function \p
771 /// FuncName at line \p LineNumber, or before input is parsed if \p LineNumber
772 /// is None. Parameter \p FuncLoc is the source location used for diagnostics.
773 /// Parameter \p Context points to the class instance holding the live string
774 /// and numeric variables. \returns the class representing that call in the
775 /// AST of the expression or an error holding a diagnostic against \p SM
776 /// otherwise.
777 static Expected<std::unique_ptr<ExpressionAST>>
778 parseCallExpr(StringRef &Expr, StringRef FuncName,
779 Optional<size_t> LineNumber, FileCheckPatternContext *Context,
780 const SourceMgr &SM);
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000781};
782
783//===----------------------------------------------------------------------===//
784// Check Strings.
785//===----------------------------------------------------------------------===//
786
787/// A check that we found in the input file.
788struct FileCheckString {
789 /// The pattern to match.
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000790 Pattern Pat;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000791
792 /// Which prefix name this check matched.
793 StringRef Prefix;
794
795 /// The location in the match file that the check string was specified.
796 SMLoc Loc;
797
798 /// All of the strings that are disallowed from occurring between this match
799 /// string and the previous one (or start of file).
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000800 std::vector<Pattern> DagNotStrings;
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000801
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000802 FileCheckString(const Pattern &P, StringRef S, SMLoc L)
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000803 : Pat(P), Prefix(S), Loc(L) {}
804
805 /// Matches check string and its "not strings" and/or "dag strings".
806 size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
807 size_t &MatchLen, FileCheckRequest &Req,
808 std::vector<FileCheckDiag> *Diags) const;
809
810 /// Verifies that there is a single line in the given \p Buffer. Errors are
811 /// reported against \p SM.
812 bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
813 /// Verifies that there is no newline in the given \p Buffer. Errors are
814 /// reported against \p SM.
815 bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
816 /// Verifies that none of the strings in \p NotStrings are found in the given
817 /// \p Buffer. Errors are reported against \p SM and diagnostics recorded in
818 /// \p Diags according to the verbosity level set in \p Req.
819 bool CheckNot(const SourceMgr &SM, StringRef Buffer,
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000820 const std::vector<const Pattern *> &NotStrings,
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000821 const FileCheckRequest &Req,
822 std::vector<FileCheckDiag> *Diags) const;
823 /// Matches "dag strings" and their mixed "not strings".
824 size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
Thomas Preud'hommed8fd92e2019-12-11 23:48:01 +0000825 std::vector<const Pattern *> &NotStrings,
Dmitri Gribenkod3aed7f2019-10-10 14:27:14 +0000826 const FileCheckRequest &Req,
827 std::vector<FileCheckDiag> *Diags) const;
828};
829
830} // namespace llvm
831
832#endif