Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 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_SUPPORT_FILECHECKIMPL_H |
| 16 | #define LLVM_LIB_SUPPORT_FILECHECKIMPL_H |
| 17 | |
Thomas Preud'homme | b34c904 | 2020-04-14 18:24:32 +0100 | [diff] [blame] | 18 | #include "llvm/Support/FileCheck.h" |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 19 | #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 | |
| 28 | namespace llvm { |
| 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // Numeric substitution handling code. |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 34 | class ExpressionValue; |
| 35 | |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 36 | /// Type representing the format an expression value should be textualized into |
| 37 | /// for matching. Used to represent both explicit format specifiers as well as |
| 38 | /// implicit format from using numeric variables. |
| 39 | struct ExpressionFormat { |
| 40 | enum class Kind { |
| 41 | /// Denote absence of format. Used for implicit format of literals and |
| 42 | /// empty expressions. |
| 43 | NoFormat, |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 44 | /// Value is an unsigned integer and should be printed as a decimal number. |
| 45 | Unsigned, |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 46 | /// Value is a signed integer and should be printed as a decimal number. |
| 47 | Signed, |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 48 | /// 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 | |
| 54 | private: |
| 55 | Kind Value; |
| 56 | |
| 57 | public: |
| 58 | /// Evaluates a format to true if it can be used in a match. |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 59 | explicit operator bool() const { return Value != Kind::NoFormat; } |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 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 | |
Paul Walker | ed9df86 | 2020-06-01 10:09:58 +0000 | [diff] [blame] | 67 | bool operator!=(const ExpressionFormat &Other) const { |
| 68 | return !(*this == Other); |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool operator==(Kind OtherValue) const { return Value == OtherValue; } |
| 72 | |
| 73 | bool operator!=(Kind OtherValue) const { return !(*this == OtherValue); } |
| 74 | |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 75 | /// \returns the format specifier corresponding to this format as a string. |
| 76 | StringRef toString() const; |
| 77 | |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 78 | 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'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 83 | /// NoFormat. |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 84 | Expected<StringRef> getWildcardRegex() const; |
| 85 | |
| 86 | /// \returns the string representation of \p Value in the format represented |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 87 | /// 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'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 90 | |
| 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'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 95 | Expected<ExpressionValue> valueFromStringRepr(StringRef StrVal, |
| 96 | const SourceMgr &SM) const; |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 99 | /// Class to represent an overflow error that might result when manipulating a |
| 100 | /// value. |
| 101 | class OverflowError : public ErrorInfo<OverflowError> { |
| 102 | public: |
| 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. |
| 113 | class ExpressionValue { |
| 114 | private: |
| 115 | uint64_t Value; |
| 116 | bool Negative; |
| 117 | |
| 118 | public: |
| 119 | template <class T> |
| 120 | explicit ExpressionValue(T Val) : Value(Val), Negative(Val < 0) {} |
| 121 | |
Paul Walker | ed9df86 | 2020-06-01 10:09:58 +0000 | [diff] [blame] | 122 | 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'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 130 | /// Returns true if value is signed and negative, false otherwise. |
Paul Walker | ed9df86 | 2020-06-01 10:09:58 +0000 | [diff] [blame] | 131 | bool isNegative() const { |
| 132 | assert((Value != 0 || !Negative) && "Unexpected negative zero!"); |
| 133 | return Negative; |
| 134 | } |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 135 | |
| 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. |
| 151 | Expected<ExpressionValue> operator+(const ExpressionValue &Lhs, |
| 152 | const ExpressionValue &Rhs); |
| 153 | Expected<ExpressionValue> operator-(const ExpressionValue &Lhs, |
| 154 | const ExpressionValue &Rhs); |
Paul Walker | 95db1e7 | 2020-06-01 10:09:58 +0000 | [diff] [blame^] | 155 | Expected<ExpressionValue> operator*(const ExpressionValue &Lhs, |
| 156 | const ExpressionValue &Rhs); |
| 157 | Expected<ExpressionValue> operator/(const ExpressionValue &Lhs, |
| 158 | const ExpressionValue &Rhs); |
Paul Walker | 8fd2270 | 2020-05-14 10:32:58 +0000 | [diff] [blame] | 159 | Expected<ExpressionValue> max(const ExpressionValue &Lhs, |
| 160 | const ExpressionValue &Rhs); |
| 161 | Expected<ExpressionValue> min(const ExpressionValue &Lhs, |
| 162 | const ExpressionValue &Rhs); |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 163 | |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 164 | /// Base class representing the AST of a given expression. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 165 | class ExpressionAST { |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 166 | private: |
| 167 | StringRef ExpressionStr; |
| 168 | |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 169 | public: |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 170 | ExpressionAST(StringRef ExpressionStr) : ExpressionStr(ExpressionStr) {} |
| 171 | |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 172 | virtual ~ExpressionAST() = default; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 173 | |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 174 | StringRef getExpressionStr() const { return ExpressionStr; } |
| 175 | |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 176 | /// Evaluates and \returns the value of the expression represented by this |
| 177 | /// AST or an error if evaluation fails. |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 178 | virtual Expected<ExpressionValue> eval() const = 0; |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 179 | |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 180 | /// \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'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 186 | return ExpressionFormat(); |
| 187 | } |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | /// Class representing an unsigned literal in the AST of an expression. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 191 | class ExpressionLiteral : public ExpressionAST { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 192 | private: |
| 193 | /// Actual value of the literal. |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 194 | ExpressionValue Value; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 195 | |
| 196 | public: |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 197 | template <class T> |
| 198 | explicit ExpressionLiteral(StringRef ExpressionStr, T Val) |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 199 | : ExpressionAST(ExpressionStr), Value(Val) {} |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 200 | |
| 201 | /// \returns the literal's value. |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 202 | Expected<ExpressionValue> eval() const override { return Value; } |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | /// Class to represent an undefined variable error, which quotes that |
| 206 | /// variable's name when printed. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 207 | class UndefVarError : public ErrorInfo<UndefVarError> { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 208 | private: |
| 209 | StringRef VarName; |
| 210 | |
| 211 | public: |
| 212 | static char ID; |
| 213 | |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 214 | UndefVarError(StringRef VarName) : VarName(VarName) {} |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 215 | |
| 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'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 229 | /// Class representing an expression and its matching format. |
| 230 | class Expression { |
| 231 | private: |
| 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 | |
| 238 | public: |
| 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 Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 251 | /// Class representing a numeric variable and its associated current value. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 252 | class NumericVariable { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 253 | private: |
| 254 | /// Name of the numeric variable. |
| 255 | StringRef Name; |
| 256 | |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 257 | /// Format to use for expressions using this variable without an explicit |
| 258 | /// format. |
| 259 | ExpressionFormat ImplicitFormat; |
| 260 | |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 261 | /// Value of numeric variable, if defined, or None otherwise. |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 262 | Optional<ExpressionValue> Value; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 263 | |
| 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 | |
| 269 | public: |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 270 | /// 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 274 | Optional<size_t> DefLineNumber = None) |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 275 | : Name(Name), ImplicitFormat(ImplicitFormat), |
| 276 | DefLineNumber(DefLineNumber) {} |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 277 | |
| 278 | /// \returns name of this numeric variable. |
| 279 | StringRef getName() const { return Name; } |
| 280 | |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 281 | /// \returns implicit format of this numeric variable. |
| 282 | ExpressionFormat getImplicitFormat() const { return ImplicitFormat; } |
| 283 | |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 284 | /// \returns this variable's value. |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 285 | Optional<ExpressionValue> getValue() const { return Value; } |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 286 | |
| 287 | /// Sets value of this numeric variable to \p NewValue. |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 288 | void setValue(ExpressionValue NewValue) { Value = NewValue; } |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 289 | |
| 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 Pilgrim | 0d908e1 | 2019-11-11 18:44:13 +0000 | [diff] [blame] | 296 | Optional<size_t> getDefLineNumber() const { return DefLineNumber; } |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | /// Class representing the use of a numeric variable in the AST of an |
| 300 | /// expression. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 301 | class NumericVariableUse : public ExpressionAST { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 302 | private: |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 303 | /// Pointer to the class instance for the variable this use is about. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 304 | NumericVariable *Variable; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 305 | |
| 306 | public: |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 307 | NumericVariableUse(StringRef Name, NumericVariable *Variable) |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 308 | : ExpressionAST(Name), Variable(Variable) {} |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 309 | /// \returns the value of the variable referenced by this instance. |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 310 | Expected<ExpressionValue> eval() const override; |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 311 | |
| 312 | /// \returns implicit format of this numeric variable. |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 313 | Expected<ExpressionFormat> |
| 314 | getImplicitFormat(const SourceMgr &SM) const override { |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 315 | return Variable->getImplicitFormat(); |
| 316 | } |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | /// Type of functions evaluating a given binary operation. |
Thomas Preud'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 320 | using binop_eval_t = Expected<ExpressionValue> (*)(const ExpressionValue &, |
| 321 | const ExpressionValue &); |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 322 | |
| 323 | /// Class representing a single binary operation in the AST of an expression. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 324 | class BinaryOperation : public ExpressionAST { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 325 | private: |
| 326 | /// Left operand. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 327 | std::unique_ptr<ExpressionAST> LeftOperand; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 328 | |
| 329 | /// Right operand. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 330 | std::unique_ptr<ExpressionAST> RightOperand; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 331 | |
| 332 | /// Pointer to function that can evaluate this binary operation. |
| 333 | binop_eval_t EvalBinop; |
| 334 | |
| 335 | public: |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 336 | BinaryOperation(StringRef ExpressionStr, binop_eval_t EvalBinop, |
| 337 | std::unique_ptr<ExpressionAST> LeftOp, |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 338 | std::unique_ptr<ExpressionAST> RightOp) |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 339 | : ExpressionAST(ExpressionStr), EvalBinop(EvalBinop) { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 340 | 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'homme | 23ac16c | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 348 | Expected<ExpressionValue> eval() const override; |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 349 | |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 350 | /// \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 Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | class FileCheckPatternContext; |
| 359 | |
| 360 | /// Class representing a substitution to perform in the RegExStr string. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 361 | class Substitution { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 362 | protected: |
| 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 | |
| 378 | public: |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 379 | Substitution(FileCheckPatternContext *Context, StringRef VarName, |
| 380 | size_t InsertIdx) |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 381 | : Context(Context), FromStr(VarName), InsertIdx(InsertIdx) {} |
| 382 | |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 383 | virtual ~Substitution() = default; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 384 | |
| 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 396 | class StringSubstitution : public Substitution { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 397 | public: |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 398 | StringSubstitution(FileCheckPatternContext *Context, StringRef VarName, |
| 399 | size_t InsertIdx) |
| 400 | : Substitution(Context, VarName, InsertIdx) {} |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 401 | |
| 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 407 | class NumericSubstitution : public Substitution { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 408 | private: |
| 409 | /// Pointer to the class representing the expression whose value is to be |
| 410 | /// substituted. |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 411 | std::unique_ptr<Expression> ExpressionPointer; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 412 | |
| 413 | public: |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 414 | 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 Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 419 | |
| 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 429 | /// 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 Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 432 | class FileCheckPatternContext { |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 433 | friend class Pattern; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 434 | |
| 435 | private: |
| 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 450 | /// NumericVariableDefs table in the Pattern instance of that pattern. |
| 451 | StringMap<NumericVariable *> GlobalNumericVariableTable; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 452 | |
| 453 | /// Pointer to the class instance representing the @LINE pseudo variable for |
| 454 | /// easily updating its value. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 455 | NumericVariable *LineVariable = nullptr; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 456 | |
| 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 459 | std::vector<std::unique_ptr<NumericVariable>> NumericVariables; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 460 | |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 461 | /// 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 Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 465 | /// Vector holding pointers to all substitutions. Used to automatically free |
| 466 | /// them once they are guaranteed to no longer be used. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 467 | std::vector<std::unique_ptr<Substitution>> Substitutions; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 468 | |
| 469 | public: |
| 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 Rymar | 76e0ab2 | 2020-04-15 15:30:21 +0300 | [diff] [blame] | 478 | Error defineCmdlineVariables(ArrayRef<StringRef> CmdlineDefines, |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 479 | 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 | |
| 491 | private: |
| 492 | /// Makes a new numeric variable and registers it for destruction when the |
| 493 | /// context is destroyed. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 494 | template <class... Types> NumericVariable *makeNumericVariable(Types... args); |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 495 | |
| 496 | /// Makes a new string substitution and registers it for destruction when the |
| 497 | /// context is destroyed. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 498 | Substitution *makeStringSubstitution(StringRef VarName, size_t InsertIdx); |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 499 | |
| 500 | /// Makes a new numeric substitution and registers it for destruction when |
| 501 | /// the context is destroyed. |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 502 | Substitution *makeNumericSubstitution(StringRef ExpressionStr, |
| 503 | std::unique_ptr<Expression> Expression, |
| 504 | size_t InsertIdx); |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 505 | }; |
| 506 | |
| 507 | /// Class to represent an error holding a diagnostic with location information |
| 508 | /// used when printing it. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 509 | class ErrorDiagnostic : public ErrorInfo<ErrorDiagnostic> { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 510 | private: |
| 511 | SMDiagnostic Diagnostic; |
| 512 | |
| 513 | public: |
| 514 | static char ID; |
| 515 | |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 516 | ErrorDiagnostic(SMDiagnostic &&Diag) : Diagnostic(Diag) {} |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 517 | |
| 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 526 | return make_error<ErrorDiagnostic>( |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 527 | 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 535 | class NotFoundError : public ErrorInfo<NotFoundError> { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 536 | public: |
| 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 549 | class Pattern { |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 550 | 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 566 | std::vector<Substitution *> Substitutions; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 567 | |
| 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'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 582 | /// 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 585 | struct NumericVariableMatch { |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 586 | /// Pointer to class instance holding the value and matching format of the |
| 587 | /// numeric variable being defined. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 588 | NumericVariable *DefinedNumericVariable; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 589 | |
| 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 596 | /// 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 Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 599 | |
| 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 Nacke | 5b5b2fd | 2019-10-11 11:59:14 +0000 | [diff] [blame] | 615 | /// Ignore case while matching if set to true. |
| 616 | bool IgnoreCase = false; |
| 617 | |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 618 | public: |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 619 | Pattern(Check::FileCheckType Ty, FileCheckPatternContext *Context, |
| 620 | Optional<size_t> Line = None) |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 621 | : 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'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 651 | /// 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 657 | StringRef Expr, Optional<NumericVariable *> &DefinedNumericVariable, |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 658 | bool IsLegacyLineExpr, Optional<size_t> LineNumber, |
| 659 | FileCheckPatternContext *Context, const SourceMgr &SM); |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 660 | /// Parses the pattern in \p PatternStr and initializes this Pattern instance |
| 661 | /// accordingly. |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 662 | /// |
| 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 676 | /// instance provides the current values of FileCheck string variables and is |
| 677 | /// updated if this match defines new values. Likewise, the |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 678 | /// 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 | |
| 698 | private: |
| 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 Pilgrim | 6da34a8 | 2019-11-12 11:14:03 +0000 | [diff] [blame] | 708 | /// opening sequence). \p SM holds the SourceMgr used for error reporting. |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 709 | /// \returns the offset of the closing sequence within Str, or npos if it |
| 710 | /// was not found. |
Simon Pilgrim | 6da34a8 | 2019-11-12 11:14:03 +0000 | [diff] [blame] | 711 | static size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM); |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 712 | |
| 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 718 | static Expected<NumericVariable *> parseNumericVariableDefinition( |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 719 | StringRef &Expr, FileCheckPatternContext *Context, |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 720 | Optional<size_t> LineNumber, ExpressionFormat ImplicitFormat, |
| 721 | const SourceMgr &SM); |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 722 | /// 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 728 | static Expected<std::unique_ptr<NumericVariableUse>> parseNumericVariableUse( |
| 729 | StringRef Name, bool IsPseudo, Optional<size_t> LineNumber, |
| 730 | FileCheckPatternContext *Context, const SourceMgr &SM); |
Thomas Preud'homme | 8e96697 | 2019-03-05 23:20:29 +0000 | [diff] [blame] | 731 | enum class AllowedOperand { LineVar, LegacyLiteral, Any }; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 732 | /// Parses \p Expr for use of a numeric operand at line \p LineNumber, or |
Paul Walker | 8fd2270 | 2020-05-14 10:32:58 +0000 | [diff] [blame] | 733 | /// 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'homme | 47934c7 | 2019-07-17 16:15:52 +0100 | [diff] [blame] | 735 | /// \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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 741 | static Expected<std::unique_ptr<ExpressionAST>> |
Thomas Preud'homme | 47934c7 | 2019-07-17 16:15:52 +0100 | [diff] [blame] | 742 | parseNumericOperand(StringRef &Expr, AllowedOperand AO, bool ConstraintParsed, |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 743 | Optional<size_t> LineNumber, |
| 744 | FileCheckPatternContext *Context, const SourceMgr &SM); |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 745 | /// 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 754 | static Expected<std::unique_ptr<ExpressionAST>> |
Thomas Preud'homme | 9743123 | 2020-04-04 01:02:45 +0100 | [diff] [blame] | 755 | 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 Richardson | 3be5e53 | 2020-05-27 15:13:42 +0100 | [diff] [blame] | 759 | |
| 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 Walker | 8fd2270 | 2020-05-14 10:32:58 +0000 | [diff] [blame] | 769 | |
| 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 Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 781 | }; |
| 782 | |
| 783 | //===----------------------------------------------------------------------===// |
| 784 | // Check Strings. |
| 785 | //===----------------------------------------------------------------------===// |
| 786 | |
| 787 | /// A check that we found in the input file. |
| 788 | struct FileCheckString { |
| 789 | /// The pattern to match. |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 790 | Pattern Pat; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 791 | |
| 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 800 | std::vector<Pattern> DagNotStrings; |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 801 | |
Thomas Preud'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 802 | FileCheckString(const Pattern &P, StringRef S, SMLoc L) |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 803 | : 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 820 | const std::vector<const Pattern *> &NotStrings, |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 821 | 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'homme | d8fd92e | 2019-12-11 23:48:01 +0000 | [diff] [blame] | 825 | std::vector<const Pattern *> &NotStrings, |
Dmitri Gribenko | d3aed7f | 2019-10-10 14:27:14 +0000 | [diff] [blame] | 826 | const FileCheckRequest &Req, |
| 827 | std::vector<FileCheckDiag> *Diags) const; |
| 828 | }; |
| 829 | |
| 830 | } // namespace llvm |
| 831 | |
| 832 | #endif |