Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1 | //===- FileCheck.cpp - Check that File's Contents match what is expected --===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // FileCheck does a line-by line check of a file that validates whether it |
| 10 | // contains the expected content. This is useful for regression tests etc. |
| 11 | // |
| 12 | // This file implements most of the API that will be used by the FileCheck utility |
| 13 | // as well as various unittests. |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Support/FileCheck.h" |
| 17 | #include "llvm/ADT/StringSet.h" |
Thomas Preud'homme | b1add2b | 2019-08-08 13:56:59 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Twine.h" |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FormatVariadic.h" |
| 20 | #include <cstdint> |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 21 | #include <list> |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 22 | #include <tuple> |
| 23 | #include <utility> |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 27 | Expected<uint64_t> FileCheckNumericVariableUse::eval() const { |
| 28 | Optional<uint64_t> Value = NumericVariable->getValue(); |
| 29 | if (Value) |
| 30 | return *Value; |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 31 | |
Thomas Preud'homme | a291b95 | 2019-09-02 14:04:00 +0000 | [diff] [blame] | 32 | return make_error<FileCheckUndefVarError>(Name); |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Expected<uint64_t> FileCheckASTBinop::eval() const { |
| 36 | Expected<uint64_t> LeftOp = LeftOperand->eval(); |
| 37 | Expected<uint64_t> RightOp = RightOperand->eval(); |
| 38 | |
| 39 | // Bubble up any error (e.g. undefined variables) in the recursive |
| 40 | // evaluation. |
| 41 | if (!LeftOp || !RightOp) { |
| 42 | Error Err = Error::success(); |
| 43 | if (!LeftOp) |
| 44 | Err = joinErrors(std::move(Err), LeftOp.takeError()); |
| 45 | if (!RightOp) |
| 46 | Err = joinErrors(std::move(Err), RightOp.takeError()); |
| 47 | return std::move(Err); |
| 48 | } |
| 49 | |
| 50 | return EvalBinop(*LeftOp, *RightOp); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 53 | Expected<std::string> FileCheckNumericSubstitution::getResult() const { |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 54 | Expected<uint64_t> EvaluatedValue = ExpressionAST->eval(); |
Thomas Preud'homme | f3b9bb3 | 2019-05-23 00:10:29 +0000 | [diff] [blame] | 55 | if (!EvaluatedValue) |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 56 | return EvaluatedValue.takeError(); |
Thomas Preud'homme | f3b9bb3 | 2019-05-23 00:10:29 +0000 | [diff] [blame] | 57 | return utostr(*EvaluatedValue); |
| 58 | } |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 59 | |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 60 | Expected<std::string> FileCheckStringSubstitution::getResult() const { |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 61 | // Look up the value and escape it so that we can put it into the regex. |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 62 | Expected<StringRef> VarVal = Context->getPatternVarValue(FromStr); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 63 | if (!VarVal) |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 64 | return VarVal.takeError(); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 65 | return Regex::escape(*VarVal); |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 68 | bool FileCheckPattern::isValidVarNameStart(char C) { |
| 69 | return C == '_' || isalpha(C); |
| 70 | } |
| 71 | |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 72 | Expected<FileCheckPattern::VariableProperties> |
| 73 | FileCheckPattern::parseVariable(StringRef &Str, const SourceMgr &SM) { |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 74 | if (Str.empty()) |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 75 | return FileCheckErrorDiagnostic::get(SM, Str, "empty variable name"); |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 76 | |
| 77 | bool ParsedOneChar = false; |
| 78 | unsigned I = 0; |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 79 | bool IsPseudo = Str[0] == '@'; |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 80 | |
| 81 | // Global vars start with '$'. |
| 82 | if (Str[0] == '$' || IsPseudo) |
| 83 | ++I; |
| 84 | |
| 85 | for (unsigned E = Str.size(); I != E; ++I) { |
| 86 | if (!ParsedOneChar && !isValidVarNameStart(Str[I])) |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 87 | return FileCheckErrorDiagnostic::get(SM, Str, "invalid variable name"); |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 88 | |
| 89 | // Variable names are composed of alphanumeric characters and underscores. |
| 90 | if (Str[I] != '_' && !isalnum(Str[I])) |
| 91 | break; |
| 92 | ParsedOneChar = true; |
| 93 | } |
| 94 | |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 95 | StringRef Name = Str.take_front(I); |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 96 | Str = Str.substr(I); |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 97 | return VariableProperties {Name, IsPseudo}; |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 100 | // StringRef holding all characters considered as horizontal whitespaces by |
| 101 | // FileCheck input canonicalization. |
Benjamin Kramer | dc5f805 | 2019-08-23 19:59:23 +0000 | [diff] [blame] | 102 | constexpr StringLiteral SpaceChars = " \t"; |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 103 | |
Thomas Preud'homme | 15cb1f1 | 2019-04-29 17:46:26 +0000 | [diff] [blame] | 104 | // Parsing helper function that strips the first character in S and returns it. |
| 105 | static char popFront(StringRef &S) { |
| 106 | char C = S.front(); |
| 107 | S = S.drop_front(); |
| 108 | return C; |
| 109 | } |
| 110 | |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 111 | char FileCheckUndefVarError::ID = 0; |
| 112 | char FileCheckErrorDiagnostic::ID = 0; |
| 113 | char FileCheckNotFoundError::ID = 0; |
| 114 | |
Thomas Preud'homme | 56f6308 | 2019-07-05 16:25:46 +0000 | [diff] [blame] | 115 | Expected<FileCheckNumericVariable *> |
| 116 | FileCheckPattern::parseNumericVariableDefinition( |
Thomas Preud'homme | 99f2a10 | 2019-07-15 19:04:56 +0000 | [diff] [blame] | 117 | StringRef &Expr, FileCheckPatternContext *Context, |
Thomas Preud'homme | a291b95 | 2019-09-02 14:04:00 +0000 | [diff] [blame] | 118 | Optional<size_t> LineNumber, const SourceMgr &SM) { |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 119 | Expected<VariableProperties> ParseVarResult = parseVariable(Expr, SM); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 120 | if (!ParseVarResult) |
| 121 | return ParseVarResult.takeError(); |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 122 | StringRef Name = ParseVarResult->Name; |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 123 | |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 124 | if (ParseVarResult->IsPseudo) |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 125 | return FileCheckErrorDiagnostic::get( |
| 126 | SM, Name, "definition of pseudo numeric variable unsupported"); |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 127 | |
| 128 | // Detect collisions between string and numeric variables when the latter |
| 129 | // is created later than the former. |
| 130 | if (Context->DefinedVariableTable.find(Name) != |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 131 | Context->DefinedVariableTable.end()) |
| 132 | return FileCheckErrorDiagnostic::get( |
| 133 | SM, Name, "string variable with name '" + Name + "' already exists"); |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 134 | |
Thomas Preud'homme | 28196a5 | 2019-07-05 12:01:06 +0000 | [diff] [blame] | 135 | Expr = Expr.ltrim(SpaceChars); |
| 136 | if (!Expr.empty()) |
| 137 | return FileCheckErrorDiagnostic::get( |
| 138 | SM, Expr, "unexpected characters after numeric variable name"); |
| 139 | |
Thomas Preud'homme | 56f6308 | 2019-07-05 16:25:46 +0000 | [diff] [blame] | 140 | FileCheckNumericVariable *DefinedNumericVariable; |
| 141 | auto VarTableIter = Context->GlobalNumericVariableTable.find(Name); |
| 142 | if (VarTableIter != Context->GlobalNumericVariableTable.end()) |
| 143 | DefinedNumericVariable = VarTableIter->second; |
| 144 | else |
Thomas Preud'homme | a291b95 | 2019-09-02 14:04:00 +0000 | [diff] [blame] | 145 | DefinedNumericVariable = Context->makeNumericVariable(Name, LineNumber); |
Thomas Preud'homme | 56f6308 | 2019-07-05 16:25:46 +0000 | [diff] [blame] | 146 | |
| 147 | return DefinedNumericVariable; |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 150 | Expected<std::unique_ptr<FileCheckNumericVariableUse>> |
| 151 | FileCheckPattern::parseNumericVariableUse(StringRef Name, bool IsPseudo, |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 152 | Optional<size_t> LineNumber, |
| 153 | FileCheckPatternContext *Context, |
| 154 | const SourceMgr &SM) { |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 155 | if (IsPseudo && !Name.equals("@LINE")) |
| 156 | return FileCheckErrorDiagnostic::get( |
| 157 | SM, Name, "invalid pseudo numeric variable '" + Name + "'"); |
Thomas Preud'homme | 15cb1f1 | 2019-04-29 17:46:26 +0000 | [diff] [blame] | 158 | |
Thomas Preud'homme | 41f2bea | 2019-07-05 12:01:12 +0000 | [diff] [blame] | 159 | // Numeric variable definitions and uses are parsed in the order in which |
| 160 | // they appear in the CHECK patterns. For each definition, the pointer to the |
| 161 | // class instance of the corresponding numeric variable definition is stored |
Thomas Preud'homme | 56f6308 | 2019-07-05 16:25:46 +0000 | [diff] [blame] | 162 | // in GlobalNumericVariableTable in parsePattern. Therefore, if the pointer |
| 163 | // we get below is null, it means no such variable was defined before. When |
| 164 | // that happens, we create a dummy variable so that parsing can continue. All |
| 165 | // uses of undefined variables, whether string or numeric, are then diagnosed |
| 166 | // in printSubstitutions() after failing to match. |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 167 | auto VarTableIter = Context->GlobalNumericVariableTable.find(Name); |
Thomas Preud'homme | fe7ac17 | 2019-07-05 16:25:33 +0000 | [diff] [blame] | 168 | FileCheckNumericVariable *NumericVariable; |
| 169 | if (VarTableIter != Context->GlobalNumericVariableTable.end()) |
| 170 | NumericVariable = VarTableIter->second; |
| 171 | else { |
Thomas Preud'homme | 99f2a10 | 2019-07-15 19:04:56 +0000 | [diff] [blame] | 172 | NumericVariable = Context->makeNumericVariable(Name); |
Thomas Preud'homme | fe7ac17 | 2019-07-05 16:25:33 +0000 | [diff] [blame] | 173 | Context->GlobalNumericVariableTable[Name] = NumericVariable; |
| 174 | } |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 175 | |
Thomas Preud'homme | 99f2a10 | 2019-07-15 19:04:56 +0000 | [diff] [blame] | 176 | Optional<size_t> DefLineNumber = NumericVariable->getDefLineNumber(); |
Thomas Preud'homme | a291b95 | 2019-09-02 14:04:00 +0000 | [diff] [blame] | 177 | if (DefLineNumber && LineNumber && *DefLineNumber == *LineNumber) |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 178 | return FileCheckErrorDiagnostic::get( |
| 179 | SM, Name, |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 180 | "numeric variable '" + Name + |
Thomas Preud'homme | a291b95 | 2019-09-02 14:04:00 +0000 | [diff] [blame] | 181 | "' defined earlier in the same CHECK directive"); |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 182 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 183 | return std::make_unique<FileCheckNumericVariableUse>(Name, NumericVariable); |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | Expected<std::unique_ptr<FileCheckExpressionAST>> |
| 187 | FileCheckPattern::parseNumericOperand(StringRef &Expr, AllowedOperand AO, |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 188 | Optional<size_t> LineNumber, |
| 189 | FileCheckPatternContext *Context, |
| 190 | const SourceMgr &SM) { |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 191 | if (AO == AllowedOperand::LineVar || AO == AllowedOperand::Any) { |
| 192 | // Try to parse as a numeric variable use. |
| 193 | Expected<FileCheckPattern::VariableProperties> ParseVarResult = |
| 194 | parseVariable(Expr, SM); |
| 195 | if (ParseVarResult) |
| 196 | return parseNumericVariableUse(ParseVarResult->Name, |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 197 | ParseVarResult->IsPseudo, LineNumber, |
| 198 | Context, SM); |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 199 | if (AO == AllowedOperand::LineVar) |
| 200 | return ParseVarResult.takeError(); |
| 201 | // Ignore the error and retry parsing as a literal. |
| 202 | consumeError(ParseVarResult.takeError()); |
| 203 | } |
| 204 | |
| 205 | // Otherwise, parse it as a literal. |
| 206 | uint64_t LiteralValue; |
| 207 | if (!Expr.consumeInteger(/*Radix=*/10, LiteralValue)) |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 208 | return std::make_unique<FileCheckExpressionLiteral>(LiteralValue); |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 209 | |
| 210 | return FileCheckErrorDiagnostic::get(SM, Expr, |
| 211 | "invalid operand format '" + Expr + "'"); |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static uint64_t add(uint64_t LeftOp, uint64_t RightOp) { |
| 215 | return LeftOp + RightOp; |
| 216 | } |
| 217 | |
| 218 | static uint64_t sub(uint64_t LeftOp, uint64_t RightOp) { |
| 219 | return LeftOp - RightOp; |
| 220 | } |
| 221 | |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 222 | Expected<std::unique_ptr<FileCheckExpressionAST>> FileCheckPattern::parseBinop( |
| 223 | StringRef &Expr, std::unique_ptr<FileCheckExpressionAST> LeftOp, |
| 224 | bool IsLegacyLineExpr, Optional<size_t> LineNumber, |
| 225 | FileCheckPatternContext *Context, const SourceMgr &SM) { |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 226 | Expr = Expr.ltrim(SpaceChars); |
| 227 | if (Expr.empty()) |
| 228 | return std::move(LeftOp); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 229 | |
| 230 | // Check if this is a supported operation and select a function to perform |
| 231 | // it. |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 232 | SMLoc OpLoc = SMLoc::getFromPointer(Expr.data()); |
| 233 | char Operator = popFront(Expr); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 234 | binop_eval_t EvalBinop; |
| 235 | switch (Operator) { |
| 236 | case '+': |
| 237 | EvalBinop = add; |
| 238 | break; |
| 239 | case '-': |
| 240 | EvalBinop = sub; |
| 241 | break; |
| 242 | default: |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 243 | return FileCheckErrorDiagnostic::get( |
Thomas Preud'homme | a2ef1ba | 2019-06-19 23:47:24 +0000 | [diff] [blame] | 244 | SM, OpLoc, Twine("unsupported operation '") + Twine(Operator) + "'"); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 245 | } |
Thomas Preud'homme | 15cb1f1 | 2019-04-29 17:46:26 +0000 | [diff] [blame] | 246 | |
| 247 | // Parse right operand. |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 248 | Expr = Expr.ltrim(SpaceChars); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 249 | if (Expr.empty()) |
Thomas Preud'homme | a2ef1ba | 2019-06-19 23:47:24 +0000 | [diff] [blame] | 250 | return FileCheckErrorDiagnostic::get(SM, Expr, |
| 251 | "missing operand in expression"); |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 252 | // The second operand in a legacy @LINE expression is always a literal. |
| 253 | AllowedOperand AO = |
| 254 | IsLegacyLineExpr ? AllowedOperand::Literal : AllowedOperand::Any; |
| 255 | Expected<std::unique_ptr<FileCheckExpressionAST>> RightOpResult = |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 256 | parseNumericOperand(Expr, AO, LineNumber, Context, SM); |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 257 | if (!RightOpResult) |
| 258 | return RightOpResult; |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 259 | |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 260 | Expr = Expr.ltrim(SpaceChars); |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 261 | return std::make_unique<FileCheckASTBinop>(EvalBinop, std::move(LeftOp), |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 262 | std::move(*RightOpResult)); |
Thomas Preud'homme | 15cb1f1 | 2019-04-29 17:46:26 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 265 | Expected<std::unique_ptr<FileCheckExpressionAST>> |
| 266 | FileCheckPattern::parseNumericSubstitutionBlock( |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 267 | StringRef Expr, |
| 268 | Optional<FileCheckNumericVariable *> &DefinedNumericVariable, |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 269 | bool IsLegacyLineExpr, Optional<size_t> LineNumber, |
| 270 | FileCheckPatternContext *Context, const SourceMgr &SM) { |
| 271 | std::unique_ptr<FileCheckExpressionAST> ExpressionAST = nullptr; |
| 272 | StringRef DefExpr = StringRef(); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 273 | DefinedNumericVariable = None; |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 274 | // Save variable definition expression if any. |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 275 | size_t DefEnd = Expr.find(':'); |
| 276 | if (DefEnd != StringRef::npos) { |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 277 | DefExpr = Expr.substr(0, DefEnd); |
| 278 | Expr = Expr.substr(DefEnd + 1); |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Thomas Preud'homme | a2ef1ba | 2019-06-19 23:47:24 +0000 | [diff] [blame] | 281 | // Parse the expression itself. |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 282 | Expr = Expr.ltrim(SpaceChars); |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 283 | if (!Expr.empty()) { |
| 284 | // The first operand in a legacy @LINE expression is always the @LINE |
| 285 | // pseudo variable. |
| 286 | AllowedOperand AO = |
| 287 | IsLegacyLineExpr ? AllowedOperand::LineVar : AllowedOperand::Any; |
| 288 | Expected<std::unique_ptr<FileCheckExpressionAST>> ParseResult = |
| 289 | parseNumericOperand(Expr, AO, LineNumber, Context, SM); |
| 290 | while (ParseResult && !Expr.empty()) { |
| 291 | ParseResult = parseBinop(Expr, std::move(*ParseResult), IsLegacyLineExpr, |
| 292 | LineNumber, Context, SM); |
| 293 | // Legacy @LINE expressions only allow 2 operands. |
| 294 | if (ParseResult && IsLegacyLineExpr && !Expr.empty()) |
| 295 | return FileCheckErrorDiagnostic::get( |
| 296 | SM, Expr, |
| 297 | "unexpected characters at end of expression '" + Expr + "'"); |
| 298 | } |
| 299 | if (!ParseResult) |
| 300 | return ParseResult; |
| 301 | ExpressionAST = std::move(*ParseResult); |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 302 | } |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 303 | |
| 304 | // Parse the numeric variable definition. |
| 305 | if (DefEnd != StringRef::npos) { |
| 306 | DefExpr = DefExpr.ltrim(SpaceChars); |
| 307 | Expected<FileCheckNumericVariable *> ParseResult = |
Thomas Preud'homme | a291b95 | 2019-09-02 14:04:00 +0000 | [diff] [blame] | 308 | parseNumericVariableDefinition(DefExpr, Context, LineNumber, SM); |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 309 | |
| 310 | if (!ParseResult) |
| 311 | return ParseResult.takeError(); |
| 312 | DefinedNumericVariable = *ParseResult; |
| 313 | } |
| 314 | |
| 315 | return std::move(ExpressionAST); |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | bool FileCheckPattern::parsePattern(StringRef PatternStr, StringRef Prefix, |
| 319 | SourceMgr &SM, |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 320 | const FileCheckRequest &Req) { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 321 | bool MatchFullLinesHere = Req.MatchFullLines && CheckTy != Check::CheckNot; |
| 322 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 323 | PatternLoc = SMLoc::getFromPointer(PatternStr.data()); |
| 324 | |
| 325 | if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines)) |
| 326 | // Ignore trailing whitespace. |
| 327 | while (!PatternStr.empty() && |
| 328 | (PatternStr.back() == ' ' || PatternStr.back() == '\t')) |
| 329 | PatternStr = PatternStr.substr(0, PatternStr.size() - 1); |
| 330 | |
| 331 | // Check that there is something on the line. |
| 332 | if (PatternStr.empty() && CheckTy != Check::CheckEmpty) { |
| 333 | SM.PrintMessage(PatternLoc, SourceMgr::DK_Error, |
| 334 | "found empty check string with prefix '" + Prefix + ":'"); |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | if (!PatternStr.empty() && CheckTy == Check::CheckEmpty) { |
| 339 | SM.PrintMessage( |
| 340 | PatternLoc, SourceMgr::DK_Error, |
| 341 | "found non-empty check string for empty check with prefix '" + Prefix + |
| 342 | ":'"); |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | if (CheckTy == Check::CheckEmpty) { |
| 347 | RegExStr = "(\n$)"; |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | // Check to see if this is a fixed string, or if it has regex pieces. |
| 352 | if (!MatchFullLinesHere && |
| 353 | (PatternStr.size() < 2 || (PatternStr.find("{{") == StringRef::npos && |
| 354 | PatternStr.find("[[") == StringRef::npos))) { |
| 355 | FixedStr = PatternStr; |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | if (MatchFullLinesHere) { |
| 360 | RegExStr += '^'; |
| 361 | if (!Req.NoCanonicalizeWhiteSpace) |
| 362 | RegExStr += " *"; |
| 363 | } |
| 364 | |
| 365 | // Paren value #0 is for the fully matched string. Any new parenthesized |
| 366 | // values add from there. |
| 367 | unsigned CurParen = 1; |
| 368 | |
| 369 | // Otherwise, there is at least one regex piece. Build up the regex pattern |
| 370 | // by escaping scary characters in fixed strings, building up one big regex. |
| 371 | while (!PatternStr.empty()) { |
| 372 | // RegEx matches. |
| 373 | if (PatternStr.startswith("{{")) { |
| 374 | // This is the start of a regex match. Scan for the }}. |
| 375 | size_t End = PatternStr.find("}}"); |
| 376 | if (End == StringRef::npos) { |
| 377 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()), |
| 378 | SourceMgr::DK_Error, |
| 379 | "found start of regex string with no end '}}'"); |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | // Enclose {{}} patterns in parens just like [[]] even though we're not |
| 384 | // capturing the result for any purpose. This is required in case the |
| 385 | // expression contains an alternation like: CHECK: abc{{x|z}}def. We |
| 386 | // want this to turn into: "abc(x|z)def" not "abcx|zdef". |
| 387 | RegExStr += '('; |
| 388 | ++CurParen; |
| 389 | |
| 390 | if (AddRegExToRegEx(PatternStr.substr(2, End - 2), CurParen, SM)) |
| 391 | return true; |
| 392 | RegExStr += ')'; |
| 393 | |
| 394 | PatternStr = PatternStr.substr(End + 2); |
| 395 | continue; |
| 396 | } |
| 397 | |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 398 | // String and numeric substitution blocks. Pattern substitution blocks come |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 399 | // in two forms: [[foo:.*]] and [[foo]]. The former matches .* (or some |
| 400 | // other regex) and assigns it to the string variable 'foo'. The latter |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 401 | // substitutes foo's value. Numeric substitution blocks recognize the same |
| 402 | // form as string ones, but start with a '#' sign after the double |
| 403 | // brackets. They also accept a combined form which sets a numeric variable |
| 404 | // to the evaluation of an expression. Both string and numeric variable |
| 405 | // names must satisfy the regular expression "[a-zA-Z_][0-9a-zA-Z_]*" to be |
| 406 | // valid, as this helps catch some common errors. |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 407 | if (PatternStr.startswith("[[")) { |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 408 | StringRef UnparsedPatternStr = PatternStr.substr(2); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 409 | // Find the closing bracket pair ending the match. End is going to be an |
| 410 | // offset relative to the beginning of the match string. |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 411 | size_t End = FindRegexVarEnd(UnparsedPatternStr, SM); |
| 412 | StringRef MatchStr = UnparsedPatternStr.substr(0, End); |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 413 | bool IsNumBlock = MatchStr.consume_front("#"); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 414 | |
| 415 | if (End == StringRef::npos) { |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 416 | SM.PrintMessage(SMLoc::getFromPointer(PatternStr.data()), |
| 417 | SourceMgr::DK_Error, |
| 418 | "Invalid substitution block, no ]] found"); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 419 | return true; |
| 420 | } |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 421 | // Strip the substitution block we are parsing. End points to the start |
| 422 | // of the "]]" closing the expression so account for it in computing the |
| 423 | // index of the first unparsed character. |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 424 | PatternStr = UnparsedPatternStr.substr(End + 2); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 425 | |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 426 | bool IsDefinition = false; |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 427 | bool SubstNeeded = false; |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 428 | // Whether the substitution block is a legacy use of @LINE with string |
| 429 | // substitution block syntax. |
| 430 | bool IsLegacyLineExpr = false; |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 431 | StringRef DefName; |
| 432 | StringRef SubstStr; |
| 433 | StringRef MatchRegexp; |
| 434 | size_t SubstInsertIdx = RegExStr.size(); |
| 435 | |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 436 | // Parse string variable or legacy @LINE expression. |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 437 | if (!IsNumBlock) { |
| 438 | size_t VarEndIdx = MatchStr.find(":"); |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 439 | size_t SpacePos = MatchStr.substr(0, VarEndIdx).find_first_of(" \t"); |
| 440 | if (SpacePos != StringRef::npos) { |
| 441 | SM.PrintMessage(SMLoc::getFromPointer(MatchStr.data() + SpacePos), |
| 442 | SourceMgr::DK_Error, "unexpected whitespace"); |
| 443 | return true; |
| 444 | } |
Thomas Preud'homme | 15cb1f1 | 2019-04-29 17:46:26 +0000 | [diff] [blame] | 445 | |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 446 | // Get the name (e.g. "foo") and verify it is well formed. |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 447 | StringRef OrigMatchStr = MatchStr; |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 448 | Expected<FileCheckPattern::VariableProperties> ParseVarResult = |
| 449 | parseVariable(MatchStr, SM); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 450 | if (!ParseVarResult) { |
| 451 | logAllUnhandledErrors(ParseVarResult.takeError(), errs()); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 452 | return true; |
| 453 | } |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 454 | StringRef Name = ParseVarResult->Name; |
| 455 | bool IsPseudo = ParseVarResult->IsPseudo; |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 456 | |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 457 | IsDefinition = (VarEndIdx != StringRef::npos); |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 458 | SubstNeeded = !IsDefinition; |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 459 | if (IsDefinition) { |
| 460 | if ((IsPseudo || !MatchStr.consume_front(":"))) { |
| 461 | SM.PrintMessage(SMLoc::getFromPointer(Name.data()), |
| 462 | SourceMgr::DK_Error, |
| 463 | "invalid name in string variable definition"); |
| 464 | return true; |
| 465 | } |
| 466 | |
| 467 | // Detect collisions between string and numeric variables when the |
| 468 | // former is created later than the latter. |
| 469 | if (Context->GlobalNumericVariableTable.find(Name) != |
| 470 | Context->GlobalNumericVariableTable.end()) { |
| 471 | SM.PrintMessage( |
| 472 | SMLoc::getFromPointer(Name.data()), SourceMgr::DK_Error, |
| 473 | "numeric variable with name '" + Name + "' already exists"); |
| 474 | return true; |
| 475 | } |
| 476 | DefName = Name; |
| 477 | MatchRegexp = MatchStr; |
| 478 | } else { |
| 479 | if (IsPseudo) { |
| 480 | MatchStr = OrigMatchStr; |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 481 | IsLegacyLineExpr = IsNumBlock = true; |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 482 | } else |
| 483 | SubstStr = Name; |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 484 | } |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 487 | // Parse numeric substitution block. |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 488 | std::unique_ptr<FileCheckExpressionAST> ExpressionAST; |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 489 | Optional<FileCheckNumericVariable *> DefinedNumericVariable; |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 490 | if (IsNumBlock) { |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 491 | Expected<std::unique_ptr<FileCheckExpressionAST>> ParseResult = |
| 492 | parseNumericSubstitutionBlock(MatchStr, DefinedNumericVariable, |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 493 | IsLegacyLineExpr, LineNumber, Context, |
| 494 | SM); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 495 | if (!ParseResult) { |
| 496 | logAllUnhandledErrors(ParseResult.takeError(), errs()); |
Thomas Preud'homme | 15cb1f1 | 2019-04-29 17:46:26 +0000 | [diff] [blame] | 497 | return true; |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 498 | } |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 499 | ExpressionAST = std::move(*ParseResult); |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 500 | SubstNeeded = ExpressionAST != nullptr; |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 501 | if (DefinedNumericVariable) { |
| 502 | IsDefinition = true; |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 503 | DefName = (*DefinedNumericVariable)->getName(); |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 504 | } |
| 505 | if (SubstNeeded) |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 506 | SubstStr = MatchStr; |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 507 | else |
| 508 | MatchRegexp = "[0-9]+"; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 511 | // Handle variable definition: [[<def>:(...)]] and [[#(...)<def>:(...)]]. |
| 512 | if (IsDefinition) { |
| 513 | RegExStr += '('; |
| 514 | ++SubstInsertIdx; |
| 515 | |
| 516 | if (IsNumBlock) { |
| 517 | FileCheckNumericVariableMatch NumericVariableDefinition = { |
| 518 | *DefinedNumericVariable, CurParen}; |
| 519 | NumericVariableDefs[DefName] = NumericVariableDefinition; |
| 520 | // This store is done here rather than in match() to allow |
| 521 | // parseNumericVariableUse() to get the pointer to the class instance |
| 522 | // of the right variable definition corresponding to a given numeric |
| 523 | // variable use. |
| 524 | Context->GlobalNumericVariableTable[DefName] = |
| 525 | *DefinedNumericVariable; |
| 526 | } else { |
| 527 | VariableDefs[DefName] = CurParen; |
| 528 | // Mark string variable as defined to detect collisions between |
| 529 | // string and numeric variables in parseNumericVariableUse() and |
| 530 | // defineCmdlineVariables() when the latter is created later than the |
| 531 | // former. We cannot reuse GlobalVariableTable for this by populating |
| 532 | // it with an empty string since we would then lose the ability to |
| 533 | // detect the use of an undefined variable in match(). |
| 534 | Context->DefinedVariableTable[DefName] = true; |
| 535 | } |
| 536 | |
| 537 | ++CurParen; |
| 538 | } |
| 539 | |
| 540 | if (!MatchRegexp.empty() && AddRegExToRegEx(MatchRegexp, CurParen, SM)) |
| 541 | return true; |
| 542 | |
| 543 | if (IsDefinition) |
| 544 | RegExStr += ')'; |
| 545 | |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 546 | // Handle substitutions: [[foo]] and [[#<foo expr>]]. |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 547 | if (SubstNeeded) { |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 548 | // Handle substitution of string variables that were defined earlier on |
Thomas Preud'homme | a2ef1ba | 2019-06-19 23:47:24 +0000 | [diff] [blame] | 549 | // the same line by emitting a backreference. Expressions do not |
| 550 | // support substituting a numeric variable defined on the same line. |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 551 | if (!IsNumBlock && VariableDefs.find(SubstStr) != VariableDefs.end()) { |
| 552 | unsigned CaptureParenGroup = VariableDefs[SubstStr]; |
| 553 | if (CaptureParenGroup < 1 || CaptureParenGroup > 9) { |
| 554 | SM.PrintMessage(SMLoc::getFromPointer(SubstStr.data()), |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 555 | SourceMgr::DK_Error, |
| 556 | "Can't back-reference more than 9 variables"); |
| 557 | return true; |
| 558 | } |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 559 | AddBackrefToRegEx(CaptureParenGroup); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 560 | } else { |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 561 | // Handle substitution of string variables ([[<var>]]) defined in |
Thomas Preud'homme | a2ef1ba | 2019-06-19 23:47:24 +0000 | [diff] [blame] | 562 | // previous CHECK patterns, and substitution of expressions. |
Thomas Preud'homme | f3b9bb3 | 2019-05-23 00:10:29 +0000 | [diff] [blame] | 563 | FileCheckSubstitution *Substitution = |
| 564 | IsNumBlock |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 565 | ? Context->makeNumericSubstitution( |
| 566 | SubstStr, std::move(ExpressionAST), SubstInsertIdx) |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 567 | : Context->makeStringSubstitution(SubstStr, SubstInsertIdx); |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 568 | Substitutions.push_back(Substitution); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 569 | } |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 570 | } |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | // Handle fixed string matches. |
| 574 | // Find the end, which is the start of the next regex. |
| 575 | size_t FixedMatchEnd = PatternStr.find("{{"); |
| 576 | FixedMatchEnd = std::min(FixedMatchEnd, PatternStr.find("[[")); |
| 577 | RegExStr += Regex::escape(PatternStr.substr(0, FixedMatchEnd)); |
| 578 | PatternStr = PatternStr.substr(FixedMatchEnd); |
| 579 | } |
| 580 | |
| 581 | if (MatchFullLinesHere) { |
| 582 | if (!Req.NoCanonicalizeWhiteSpace) |
| 583 | RegExStr += " *"; |
| 584 | RegExStr += '$'; |
| 585 | } |
| 586 | |
| 587 | return false; |
| 588 | } |
| 589 | |
| 590 | bool FileCheckPattern::AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM) { |
| 591 | Regex R(RS); |
| 592 | std::string Error; |
| 593 | if (!R.isValid(Error)) { |
| 594 | SM.PrintMessage(SMLoc::getFromPointer(RS.data()), SourceMgr::DK_Error, |
| 595 | "invalid regex: " + Error); |
| 596 | return true; |
| 597 | } |
| 598 | |
| 599 | RegExStr += RS.str(); |
| 600 | CurParen += R.getNumMatches(); |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | void FileCheckPattern::AddBackrefToRegEx(unsigned BackrefNum) { |
| 605 | assert(BackrefNum >= 1 && BackrefNum <= 9 && "Invalid backref number"); |
| 606 | std::string Backref = std::string("\\") + std::string(1, '0' + BackrefNum); |
| 607 | RegExStr += Backref; |
| 608 | } |
| 609 | |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 610 | Expected<size_t> FileCheckPattern::match(StringRef Buffer, size_t &MatchLen, |
| 611 | const SourceMgr &SM) const { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 612 | // If this is the EOF pattern, match it immediately. |
| 613 | if (CheckTy == Check::CheckEOF) { |
| 614 | MatchLen = 0; |
| 615 | return Buffer.size(); |
| 616 | } |
| 617 | |
| 618 | // If this is a fixed string pattern, just match it now. |
| 619 | if (!FixedStr.empty()) { |
| 620 | MatchLen = FixedStr.size(); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 621 | size_t Pos = Buffer.find(FixedStr); |
| 622 | if (Pos == StringRef::npos) |
| 623 | return make_error<FileCheckNotFoundError>(); |
| 624 | return Pos; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | // Regex match. |
| 628 | |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 629 | // If there are substitutions, we need to create a temporary string with the |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 630 | // actual value. |
| 631 | StringRef RegExToMatch = RegExStr; |
| 632 | std::string TmpStr; |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 633 | if (!Substitutions.empty()) { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 634 | TmpStr = RegExStr; |
Thomas Preud'homme | 99f2a10 | 2019-07-15 19:04:56 +0000 | [diff] [blame] | 635 | if (LineNumber) |
| 636 | Context->LineVariable->setValue(*LineNumber); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 637 | |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 638 | size_t InsertOffset = 0; |
Thomas Preud'homme | a2ef1ba | 2019-06-19 23:47:24 +0000 | [diff] [blame] | 639 | // Substitute all string variables and expressions whose values are only |
| 640 | // now known. Use of string variables defined on the same line are handled |
| 641 | // by back-references. |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 642 | for (const auto &Substitution : Substitutions) { |
| 643 | // Substitute and check for failure (e.g. use of undefined variable). |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 644 | Expected<std::string> Value = Substitution->getResult(); |
Thomas Preud'homme | 7049449 | 2019-07-18 13:39:04 +0000 | [diff] [blame] | 645 | if (!Value) |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 646 | return Value.takeError(); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 647 | |
| 648 | // Plop it into the regex at the adjusted offset. |
Thomas Preud'homme | f3b9bb3 | 2019-05-23 00:10:29 +0000 | [diff] [blame] | 649 | TmpStr.insert(TmpStr.begin() + Substitution->getIndex() + InsertOffset, |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 650 | Value->begin(), Value->end()); |
| 651 | InsertOffset += Value->size(); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | // Match the newly constructed regex. |
| 655 | RegExToMatch = TmpStr; |
| 656 | } |
| 657 | |
| 658 | SmallVector<StringRef, 4> MatchInfo; |
| 659 | if (!Regex(RegExToMatch, Regex::Newline).match(Buffer, &MatchInfo)) |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 660 | return make_error<FileCheckNotFoundError>(); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 661 | |
| 662 | // Successful regex match. |
| 663 | assert(!MatchInfo.empty() && "Didn't get any match"); |
| 664 | StringRef FullMatch = MatchInfo[0]; |
| 665 | |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 666 | // If this defines any string variables, remember their values. |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 667 | for (const auto &VariableDef : VariableDefs) { |
| 668 | assert(VariableDef.second < MatchInfo.size() && "Internal paren error"); |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 669 | Context->GlobalVariableTable[VariableDef.first] = |
| 670 | MatchInfo[VariableDef.second]; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 673 | // If this defines any numeric variables, remember their values. |
| 674 | for (const auto &NumericVariableDef : NumericVariableDefs) { |
Thomas Preud'homme | a2ef1ba | 2019-06-19 23:47:24 +0000 | [diff] [blame] | 675 | const FileCheckNumericVariableMatch &NumericVariableMatch = |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 676 | NumericVariableDef.getValue(); |
| 677 | unsigned CaptureParenGroup = NumericVariableMatch.CaptureParenGroup; |
| 678 | assert(CaptureParenGroup < MatchInfo.size() && "Internal paren error"); |
| 679 | FileCheckNumericVariable *DefinedNumericVariable = |
| 680 | NumericVariableMatch.DefinedNumericVariable; |
| 681 | |
| 682 | StringRef MatchedValue = MatchInfo[CaptureParenGroup]; |
| 683 | uint64_t Val; |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 684 | if (MatchedValue.getAsInteger(10, Val)) |
| 685 | return FileCheckErrorDiagnostic::get(SM, MatchedValue, |
| 686 | "Unable to represent numeric value"); |
Thomas Preud'homme | 2bf04f2 | 2019-07-10 12:49:28 +0000 | [diff] [blame] | 687 | DefinedNumericVariable->setValue(Val); |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 690 | // Like CHECK-NEXT, CHECK-EMPTY's match range is considered to start after |
| 691 | // the required preceding newline, which is consumed by the pattern in the |
| 692 | // case of CHECK-EMPTY but not CHECK-NEXT. |
| 693 | size_t MatchStartSkip = CheckTy == Check::CheckEmpty; |
| 694 | MatchLen = FullMatch.size() - MatchStartSkip; |
| 695 | return FullMatch.data() - Buffer.data() + MatchStartSkip; |
| 696 | } |
| 697 | |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 698 | unsigned FileCheckPattern::computeMatchDistance(StringRef Buffer) const { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 699 | // Just compute the number of matching characters. For regular expressions, we |
| 700 | // just compare against the regex itself and hope for the best. |
| 701 | // |
| 702 | // FIXME: One easy improvement here is have the regex lib generate a single |
| 703 | // example regular expression which matches, and use that as the example |
| 704 | // string. |
| 705 | StringRef ExampleString(FixedStr); |
| 706 | if (ExampleString.empty()) |
| 707 | ExampleString = RegExStr; |
| 708 | |
| 709 | // Only compare up to the first line in the buffer, or the string size. |
| 710 | StringRef BufferPrefix = Buffer.substr(0, ExampleString.size()); |
| 711 | BufferPrefix = BufferPrefix.split('\n').first; |
| 712 | return BufferPrefix.edit_distance(ExampleString); |
| 713 | } |
| 714 | |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 715 | void FileCheckPattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer, |
| 716 | SMRange MatchRange) const { |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 717 | // Print what we know about substitutions. |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 718 | if (!Substitutions.empty()) { |
| 719 | for (const auto &Substitution : Substitutions) { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 720 | SmallString<256> Msg; |
| 721 | raw_svector_ostream OS(Msg); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 722 | Expected<std::string> MatchedValue = Substitution->getResult(); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 723 | |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 724 | // Substitution failed or is not known at match time, print the undefined |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 725 | // variables it uses. |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 726 | if (!MatchedValue) { |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 727 | bool UndefSeen = false; |
| 728 | handleAllErrors(MatchedValue.takeError(), |
| 729 | [](const FileCheckNotFoundError &E) {}, |
Thomas Preud'homme | a188ad2 | 2019-07-05 12:00:56 +0000 | [diff] [blame] | 730 | // Handled in PrintNoMatch(). |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 731 | [](const FileCheckErrorDiagnostic &E) {}, |
| 732 | [&](const FileCheckUndefVarError &E) { |
| 733 | if (!UndefSeen) { |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 734 | OS << "uses undefined variable(s):"; |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 735 | UndefSeen = true; |
| 736 | } |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 737 | OS << " "; |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 738 | E.log(OS); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 739 | }); |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 740 | } else { |
| 741 | // Substitution succeeded. Print substituted value. |
Thomas Preud'homme | f3b9bb3 | 2019-05-23 00:10:29 +0000 | [diff] [blame] | 742 | OS << "with \""; |
| 743 | OS.write_escaped(Substitution->getFromString()) << "\" equal to \""; |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 744 | OS.write_escaped(*MatchedValue) << "\""; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | if (MatchRange.isValid()) |
| 748 | SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, OS.str(), |
| 749 | {MatchRange}); |
| 750 | else |
| 751 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), |
| 752 | SourceMgr::DK_Note, OS.str()); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
Joel E. Denny | 3c5d267 | 2018-12-18 00:01:39 +0000 | [diff] [blame] | 757 | static SMRange ProcessMatchResult(FileCheckDiag::MatchType MatchTy, |
| 758 | const SourceMgr &SM, SMLoc Loc, |
| 759 | Check::FileCheckType CheckTy, |
| 760 | StringRef Buffer, size_t Pos, size_t Len, |
Joel E. Denny | 7df8696 | 2018-12-18 00:03:03 +0000 | [diff] [blame] | 761 | std::vector<FileCheckDiag> *Diags, |
| 762 | bool AdjustPrevDiag = false) { |
Joel E. Denny | 3c5d267 | 2018-12-18 00:01:39 +0000 | [diff] [blame] | 763 | SMLoc Start = SMLoc::getFromPointer(Buffer.data() + Pos); |
| 764 | SMLoc End = SMLoc::getFromPointer(Buffer.data() + Pos + Len); |
| 765 | SMRange Range(Start, End); |
Joel E. Denny | 96f0e84 | 2018-12-18 00:03:36 +0000 | [diff] [blame] | 766 | if (Diags) { |
Joel E. Denny | 7df8696 | 2018-12-18 00:03:03 +0000 | [diff] [blame] | 767 | if (AdjustPrevDiag) |
| 768 | Diags->rbegin()->MatchTy = MatchTy; |
| 769 | else |
| 770 | Diags->emplace_back(SM, CheckTy, Loc, MatchTy, Range); |
| 771 | } |
Joel E. Denny | 3c5d267 | 2018-12-18 00:01:39 +0000 | [diff] [blame] | 772 | return Range; |
| 773 | } |
| 774 | |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 775 | void FileCheckPattern::printFuzzyMatch( |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 776 | const SourceMgr &SM, StringRef Buffer, |
Joel E. Denny | 2c007c8 | 2018-12-18 00:02:04 +0000 | [diff] [blame] | 777 | std::vector<FileCheckDiag> *Diags) const { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 778 | // Attempt to find the closest/best fuzzy match. Usually an error happens |
| 779 | // because some string in the output didn't exactly match. In these cases, we |
| 780 | // would like to show the user a best guess at what "should have" matched, to |
| 781 | // save them having to actually check the input manually. |
| 782 | size_t NumLinesForward = 0; |
| 783 | size_t Best = StringRef::npos; |
| 784 | double BestQuality = 0; |
| 785 | |
| 786 | // Use an arbitrary 4k limit on how far we will search. |
| 787 | for (size_t i = 0, e = std::min(size_t(4096), Buffer.size()); i != e; ++i) { |
| 788 | if (Buffer[i] == '\n') |
| 789 | ++NumLinesForward; |
| 790 | |
| 791 | // Patterns have leading whitespace stripped, so skip whitespace when |
| 792 | // looking for something which looks like a pattern. |
| 793 | if (Buffer[i] == ' ' || Buffer[i] == '\t') |
| 794 | continue; |
| 795 | |
| 796 | // Compute the "quality" of this match as an arbitrary combination of the |
| 797 | // match distance and the number of lines skipped to get to this match. |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 798 | unsigned Distance = computeMatchDistance(Buffer.substr(i)); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 799 | double Quality = Distance + (NumLinesForward / 100.); |
| 800 | |
| 801 | if (Quality < BestQuality || Best == StringRef::npos) { |
| 802 | Best = i; |
| 803 | BestQuality = Quality; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | // Print the "possible intended match here" line if we found something |
| 808 | // reasonable and not equal to what we showed in the "scanning from here" |
| 809 | // line. |
| 810 | if (Best && Best != StringRef::npos && BestQuality < 50) { |
Joel E. Denny | 2c007c8 | 2018-12-18 00:02:04 +0000 | [diff] [blame] | 811 | SMRange MatchRange = |
| 812 | ProcessMatchResult(FileCheckDiag::MatchFuzzy, SM, getLoc(), |
| 813 | getCheckTy(), Buffer, Best, 0, Diags); |
| 814 | SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, |
| 815 | "possible intended match here"); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 816 | |
| 817 | // FIXME: If we wanted to be really friendly we would show why the match |
| 818 | // failed, as it can be hard to spot simple one character differences. |
| 819 | } |
| 820 | } |
| 821 | |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 822 | Expected<StringRef> |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 823 | FileCheckPatternContext::getPatternVarValue(StringRef VarName) { |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 824 | auto VarIter = GlobalVariableTable.find(VarName); |
| 825 | if (VarIter == GlobalVariableTable.end()) |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 826 | return make_error<FileCheckUndefVarError>(VarName); |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 827 | |
| 828 | return VarIter->second; |
| 829 | } |
| 830 | |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 831 | template <class... Types> |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 832 | FileCheckNumericVariable * |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 833 | FileCheckPatternContext::makeNumericVariable(Types... args) { |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 834 | NumericVariables.push_back( |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 835 | std::make_unique<FileCheckNumericVariable>(args...)); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 836 | return NumericVariables.back().get(); |
| 837 | } |
| 838 | |
Thomas Preud'homme | f3b9bb3 | 2019-05-23 00:10:29 +0000 | [diff] [blame] | 839 | FileCheckSubstitution * |
| 840 | FileCheckPatternContext::makeStringSubstitution(StringRef VarName, |
| 841 | size_t InsertIdx) { |
| 842 | Substitutions.push_back( |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 843 | std::make_unique<FileCheckStringSubstitution>(this, VarName, InsertIdx)); |
Thomas Preud'homme | f3b9bb3 | 2019-05-23 00:10:29 +0000 | [diff] [blame] | 844 | return Substitutions.back().get(); |
| 845 | } |
| 846 | |
| 847 | FileCheckSubstitution *FileCheckPatternContext::makeNumericSubstitution( |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 848 | StringRef ExpressionStr, |
| 849 | std::unique_ptr<FileCheckExpressionAST> ExpressionAST, size_t InsertIdx) { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 850 | Substitutions.push_back(std::make_unique<FileCheckNumericSubstitution>( |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 851 | this, ExpressionStr, std::move(ExpressionAST), InsertIdx)); |
Thomas Preud'homme | f3b9bb3 | 2019-05-23 00:10:29 +0000 | [diff] [blame] | 852 | return Substitutions.back().get(); |
| 853 | } |
| 854 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 855 | size_t FileCheckPattern::FindRegexVarEnd(StringRef Str, SourceMgr &SM) { |
| 856 | // Offset keeps track of the current offset within the input Str |
| 857 | size_t Offset = 0; |
| 858 | // [...] Nesting depth |
| 859 | size_t BracketDepth = 0; |
| 860 | |
| 861 | while (!Str.empty()) { |
| 862 | if (Str.startswith("]]") && BracketDepth == 0) |
| 863 | return Offset; |
| 864 | if (Str[0] == '\\') { |
| 865 | // Backslash escapes the next char within regexes, so skip them both. |
| 866 | Str = Str.substr(2); |
| 867 | Offset += 2; |
| 868 | } else { |
| 869 | switch (Str[0]) { |
| 870 | default: |
| 871 | break; |
| 872 | case '[': |
| 873 | BracketDepth++; |
| 874 | break; |
| 875 | case ']': |
| 876 | if (BracketDepth == 0) { |
| 877 | SM.PrintMessage(SMLoc::getFromPointer(Str.data()), |
| 878 | SourceMgr::DK_Error, |
| 879 | "missing closing \"]\" for regex variable"); |
| 880 | exit(1); |
| 881 | } |
| 882 | BracketDepth--; |
| 883 | break; |
| 884 | } |
| 885 | Str = Str.substr(1); |
| 886 | Offset++; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | return StringRef::npos; |
| 891 | } |
| 892 | |
Thomas Preud'homme | 7b7683d | 2019-05-23 17:19:36 +0000 | [diff] [blame] | 893 | StringRef FileCheck::CanonicalizeFile(MemoryBuffer &MB, |
| 894 | SmallVectorImpl<char> &OutputBuffer) { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 895 | OutputBuffer.reserve(MB.getBufferSize()); |
| 896 | |
| 897 | for (const char *Ptr = MB.getBufferStart(), *End = MB.getBufferEnd(); |
| 898 | Ptr != End; ++Ptr) { |
| 899 | // Eliminate trailing dosish \r. |
| 900 | if (Ptr <= End - 2 && Ptr[0] == '\r' && Ptr[1] == '\n') { |
| 901 | continue; |
| 902 | } |
| 903 | |
| 904 | // If current char is not a horizontal whitespace or if horizontal |
| 905 | // whitespace canonicalization is disabled, dump it to output as is. |
| 906 | if (Req.NoCanonicalizeWhiteSpace || (*Ptr != ' ' && *Ptr != '\t')) { |
| 907 | OutputBuffer.push_back(*Ptr); |
| 908 | continue; |
| 909 | } |
| 910 | |
| 911 | // Otherwise, add one space and advance over neighboring space. |
| 912 | OutputBuffer.push_back(' '); |
| 913 | while (Ptr + 1 != End && (Ptr[1] == ' ' || Ptr[1] == '\t')) |
| 914 | ++Ptr; |
| 915 | } |
| 916 | |
| 917 | // Add a null byte and then return all but that byte. |
| 918 | OutputBuffer.push_back('\0'); |
| 919 | return StringRef(OutputBuffer.data(), OutputBuffer.size() - 1); |
| 920 | } |
| 921 | |
Joel E. Denny | 3c5d267 | 2018-12-18 00:01:39 +0000 | [diff] [blame] | 922 | FileCheckDiag::FileCheckDiag(const SourceMgr &SM, |
| 923 | const Check::FileCheckType &CheckTy, |
| 924 | SMLoc CheckLoc, MatchType MatchTy, |
| 925 | SMRange InputRange) |
| 926 | : CheckTy(CheckTy), MatchTy(MatchTy) { |
| 927 | auto Start = SM.getLineAndColumn(InputRange.Start); |
| 928 | auto End = SM.getLineAndColumn(InputRange.End); |
| 929 | InputStartLine = Start.first; |
| 930 | InputStartCol = Start.second; |
| 931 | InputEndLine = End.first; |
| 932 | InputEndCol = End.second; |
| 933 | Start = SM.getLineAndColumn(CheckLoc); |
| 934 | CheckLine = Start.first; |
| 935 | CheckCol = Start.second; |
| 936 | } |
| 937 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 938 | static bool IsPartOfWord(char c) { |
| 939 | return (isalnum(c) || c == '-' || c == '_'); |
| 940 | } |
| 941 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 942 | Check::FileCheckType &Check::FileCheckType::setCount(int C) { |
Fedor Sergeev | 8477a3e | 2018-11-13 01:09:53 +0000 | [diff] [blame] | 943 | assert(Count > 0 && "zero and negative counts are not supported"); |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 944 | assert((C == 1 || Kind == CheckPlain) && |
| 945 | "count supported only for plain CHECK directives"); |
| 946 | Count = C; |
| 947 | return *this; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 950 | std::string Check::FileCheckType::getDescription(StringRef Prefix) const { |
| 951 | switch (Kind) { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 952 | case Check::CheckNone: |
| 953 | return "invalid"; |
| 954 | case Check::CheckPlain: |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 955 | if (Count > 1) |
| 956 | return Prefix.str() + "-COUNT"; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 957 | return Prefix; |
| 958 | case Check::CheckNext: |
| 959 | return Prefix.str() + "-NEXT"; |
| 960 | case Check::CheckSame: |
| 961 | return Prefix.str() + "-SAME"; |
| 962 | case Check::CheckNot: |
| 963 | return Prefix.str() + "-NOT"; |
| 964 | case Check::CheckDAG: |
| 965 | return Prefix.str() + "-DAG"; |
| 966 | case Check::CheckLabel: |
| 967 | return Prefix.str() + "-LABEL"; |
| 968 | case Check::CheckEmpty: |
| 969 | return Prefix.str() + "-EMPTY"; |
| 970 | case Check::CheckEOF: |
| 971 | return "implicit EOF"; |
| 972 | case Check::CheckBadNot: |
| 973 | return "bad NOT"; |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 974 | case Check::CheckBadCount: |
| 975 | return "bad COUNT"; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 976 | } |
| 977 | llvm_unreachable("unknown FileCheckType"); |
| 978 | } |
| 979 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 980 | static std::pair<Check::FileCheckType, StringRef> |
| 981 | FindCheckType(StringRef Buffer, StringRef Prefix) { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 982 | if (Buffer.size() <= Prefix.size()) |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 983 | return {Check::CheckNone, StringRef()}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 984 | |
| 985 | char NextChar = Buffer[Prefix.size()]; |
| 986 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 987 | StringRef Rest = Buffer.drop_front(Prefix.size() + 1); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 988 | // Verify that the : is present after the prefix. |
| 989 | if (NextChar == ':') |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 990 | return {Check::CheckPlain, Rest}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 991 | |
| 992 | if (NextChar != '-') |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 993 | return {Check::CheckNone, StringRef()}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 994 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 995 | if (Rest.consume_front("COUNT-")) { |
| 996 | int64_t Count; |
| 997 | if (Rest.consumeInteger(10, Count)) |
| 998 | // Error happened in parsing integer. |
| 999 | return {Check::CheckBadCount, Rest}; |
| 1000 | if (Count <= 0 || Count > INT32_MAX) |
| 1001 | return {Check::CheckBadCount, Rest}; |
| 1002 | if (!Rest.consume_front(":")) |
| 1003 | return {Check::CheckBadCount, Rest}; |
| 1004 | return {Check::FileCheckType(Check::CheckPlain).setCount(Count), Rest}; |
| 1005 | } |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1006 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1007 | if (Rest.consume_front("NEXT:")) |
| 1008 | return {Check::CheckNext, Rest}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1009 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1010 | if (Rest.consume_front("SAME:")) |
| 1011 | return {Check::CheckSame, Rest}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1012 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1013 | if (Rest.consume_front("NOT:")) |
| 1014 | return {Check::CheckNot, Rest}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1015 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1016 | if (Rest.consume_front("DAG:")) |
| 1017 | return {Check::CheckDAG, Rest}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1018 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1019 | if (Rest.consume_front("LABEL:")) |
| 1020 | return {Check::CheckLabel, Rest}; |
| 1021 | |
| 1022 | if (Rest.consume_front("EMPTY:")) |
| 1023 | return {Check::CheckEmpty, Rest}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1024 | |
| 1025 | // You can't combine -NOT with another suffix. |
| 1026 | if (Rest.startswith("DAG-NOT:") || Rest.startswith("NOT-DAG:") || |
| 1027 | Rest.startswith("NEXT-NOT:") || Rest.startswith("NOT-NEXT:") || |
| 1028 | Rest.startswith("SAME-NOT:") || Rest.startswith("NOT-SAME:") || |
| 1029 | Rest.startswith("EMPTY-NOT:") || Rest.startswith("NOT-EMPTY:")) |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1030 | return {Check::CheckBadNot, Rest}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1031 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1032 | return {Check::CheckNone, Rest}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | // From the given position, find the next character after the word. |
| 1036 | static size_t SkipWord(StringRef Str, size_t Loc) { |
| 1037 | while (Loc < Str.size() && IsPartOfWord(Str[Loc])) |
| 1038 | ++Loc; |
| 1039 | return Loc; |
| 1040 | } |
| 1041 | |
Thomas Preud'homme | 4a8ef11 | 2019-05-08 21:47:31 +0000 | [diff] [blame] | 1042 | /// Searches the buffer for the first prefix in the prefix regular expression. |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1043 | /// |
| 1044 | /// This searches the buffer using the provided regular expression, however it |
| 1045 | /// enforces constraints beyond that: |
| 1046 | /// 1) The found prefix must not be a suffix of something that looks like |
| 1047 | /// a valid prefix. |
| 1048 | /// 2) The found prefix must be followed by a valid check type suffix using \c |
| 1049 | /// FindCheckType above. |
| 1050 | /// |
Thomas Preud'homme | 4a8ef11 | 2019-05-08 21:47:31 +0000 | [diff] [blame] | 1051 | /// \returns a pair of StringRefs into the Buffer, which combines: |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1052 | /// - the first match of the regular expression to satisfy these two is |
| 1053 | /// returned, |
| 1054 | /// otherwise an empty StringRef is returned to indicate failure. |
| 1055 | /// - buffer rewound to the location right after parsed suffix, for parsing |
| 1056 | /// to continue from |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1057 | /// |
| 1058 | /// If this routine returns a valid prefix, it will also shrink \p Buffer to |
| 1059 | /// start at the beginning of the returned prefix, increment \p LineNumber for |
| 1060 | /// each new line consumed from \p Buffer, and set \p CheckTy to the type of |
| 1061 | /// check found by examining the suffix. |
| 1062 | /// |
| 1063 | /// If no valid prefix is found, the state of Buffer, LineNumber, and CheckTy |
| 1064 | /// is unspecified. |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1065 | static std::pair<StringRef, StringRef> |
| 1066 | FindFirstMatchingPrefix(Regex &PrefixRE, StringRef &Buffer, |
| 1067 | unsigned &LineNumber, Check::FileCheckType &CheckTy) { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1068 | SmallVector<StringRef, 2> Matches; |
| 1069 | |
| 1070 | while (!Buffer.empty()) { |
| 1071 | // Find the first (longest) match using the RE. |
| 1072 | if (!PrefixRE.match(Buffer, &Matches)) |
| 1073 | // No match at all, bail. |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1074 | return {StringRef(), StringRef()}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1075 | |
| 1076 | StringRef Prefix = Matches[0]; |
| 1077 | Matches.clear(); |
| 1078 | |
| 1079 | assert(Prefix.data() >= Buffer.data() && |
| 1080 | Prefix.data() < Buffer.data() + Buffer.size() && |
| 1081 | "Prefix doesn't start inside of buffer!"); |
| 1082 | size_t Loc = Prefix.data() - Buffer.data(); |
| 1083 | StringRef Skipped = Buffer.substr(0, Loc); |
| 1084 | Buffer = Buffer.drop_front(Loc); |
| 1085 | LineNumber += Skipped.count('\n'); |
| 1086 | |
| 1087 | // Check that the matched prefix isn't a suffix of some other check-like |
| 1088 | // word. |
| 1089 | // FIXME: This is a very ad-hoc check. it would be better handled in some |
| 1090 | // other way. Among other things it seems hard to distinguish between |
| 1091 | // intentional and unintentional uses of this feature. |
| 1092 | if (Skipped.empty() || !IsPartOfWord(Skipped.back())) { |
| 1093 | // Now extract the type. |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1094 | StringRef AfterSuffix; |
| 1095 | std::tie(CheckTy, AfterSuffix) = FindCheckType(Buffer, Prefix); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1096 | |
| 1097 | // If we've found a valid check type for this prefix, we're done. |
| 1098 | if (CheckTy != Check::CheckNone) |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1099 | return {Prefix, AfterSuffix}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | // If we didn't successfully find a prefix, we need to skip this invalid |
| 1103 | // prefix and continue scanning. We directly skip the prefix that was |
| 1104 | // matched and any additional parts of that check-like word. |
| 1105 | Buffer = Buffer.drop_front(SkipWord(Buffer, Prefix.size())); |
| 1106 | } |
| 1107 | |
| 1108 | // We ran out of buffer while skipping partial matches so give up. |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1109 | return {StringRef(), StringRef()}; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Thomas Preud'homme | 56f6308 | 2019-07-05 16:25:46 +0000 | [diff] [blame] | 1112 | void FileCheckPatternContext::createLineVariable() { |
| 1113 | assert(!LineVariable && "@LINE pseudo numeric variable already created"); |
| 1114 | StringRef LineName = "@LINE"; |
Thomas Preud'homme | 99f2a10 | 2019-07-15 19:04:56 +0000 | [diff] [blame] | 1115 | LineVariable = makeNumericVariable(LineName); |
Thomas Preud'homme | 56f6308 | 2019-07-05 16:25:46 +0000 | [diff] [blame] | 1116 | GlobalNumericVariableTable[LineName] = LineVariable; |
| 1117 | } |
| 1118 | |
Thomas Preud'homme | 7b7683d | 2019-05-23 17:19:36 +0000 | [diff] [blame] | 1119 | bool FileCheck::ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE, |
| 1120 | std::vector<FileCheckString> &CheckStrings) { |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1121 | Error DefineError = |
| 1122 | PatternContext.defineCmdlineVariables(Req.GlobalDefines, SM); |
| 1123 | if (DefineError) { |
| 1124 | logAllUnhandledErrors(std::move(DefineError), errs()); |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 1125 | return true; |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1126 | } |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1127 | |
Thomas Preud'homme | 56f6308 | 2019-07-05 16:25:46 +0000 | [diff] [blame] | 1128 | PatternContext.createLineVariable(); |
| 1129 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1130 | std::vector<FileCheckPattern> ImplicitNegativeChecks; |
| 1131 | for (const auto &PatternString : Req.ImplicitCheckNot) { |
| 1132 | // Create a buffer with fake command line content in order to display the |
| 1133 | // command line option responsible for the specific implicit CHECK-NOT. |
| 1134 | std::string Prefix = "-implicit-check-not='"; |
| 1135 | std::string Suffix = "'"; |
| 1136 | std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy( |
| 1137 | Prefix + PatternString + Suffix, "command line"); |
| 1138 | |
| 1139 | StringRef PatternInBuffer = |
| 1140 | CmdLine->getBuffer().substr(Prefix.size(), PatternString.size()); |
| 1141 | SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc()); |
| 1142 | |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1143 | ImplicitNegativeChecks.push_back( |
Thomas Preud'homme | 99f2a10 | 2019-07-15 19:04:56 +0000 | [diff] [blame] | 1144 | FileCheckPattern(Check::CheckNot, &PatternContext)); |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 1145 | ImplicitNegativeChecks.back().parsePattern(PatternInBuffer, |
| 1146 | "IMPLICIT-CHECK", SM, Req); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | std::vector<FileCheckPattern> DagNotMatches = ImplicitNegativeChecks; |
| 1150 | |
| 1151 | // LineNumber keeps track of the line on which CheckPrefix instances are |
| 1152 | // found. |
| 1153 | unsigned LineNumber = 1; |
| 1154 | |
| 1155 | while (1) { |
| 1156 | Check::FileCheckType CheckTy; |
| 1157 | |
| 1158 | // See if a prefix occurs in the memory buffer. |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1159 | StringRef UsedPrefix; |
| 1160 | StringRef AfterSuffix; |
| 1161 | std::tie(UsedPrefix, AfterSuffix) = |
| 1162 | FindFirstMatchingPrefix(PrefixRE, Buffer, LineNumber, CheckTy); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1163 | if (UsedPrefix.empty()) |
| 1164 | break; |
| 1165 | assert(UsedPrefix.data() == Buffer.data() && |
| 1166 | "Failed to move Buffer's start forward, or pointed prefix outside " |
| 1167 | "of the buffer!"); |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1168 | assert(AfterSuffix.data() >= Buffer.data() && |
| 1169 | AfterSuffix.data() < Buffer.data() + Buffer.size() && |
| 1170 | "Parsing after suffix doesn't start inside of buffer!"); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1171 | |
| 1172 | // Location to use for error messages. |
| 1173 | const char *UsedPrefixStart = UsedPrefix.data(); |
| 1174 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1175 | // Skip the buffer to the end of parsed suffix (or just prefix, if no good |
| 1176 | // suffix was processed). |
| 1177 | Buffer = AfterSuffix.empty() ? Buffer.drop_front(UsedPrefix.size()) |
| 1178 | : AfterSuffix; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1179 | |
| 1180 | // Complain about useful-looking but unsupported suffixes. |
| 1181 | if (CheckTy == Check::CheckBadNot) { |
| 1182 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error, |
| 1183 | "unsupported -NOT combo on prefix '" + UsedPrefix + "'"); |
| 1184 | return true; |
| 1185 | } |
| 1186 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1187 | // Complain about invalid count specification. |
| 1188 | if (CheckTy == Check::CheckBadCount) { |
| 1189 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Error, |
| 1190 | "invalid count in -COUNT specification on prefix '" + |
| 1191 | UsedPrefix + "'"); |
| 1192 | return true; |
| 1193 | } |
| 1194 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1195 | // Okay, we found the prefix, yay. Remember the rest of the line, but ignore |
| 1196 | // leading whitespace. |
| 1197 | if (!(Req.NoCanonicalizeWhiteSpace && Req.MatchFullLines)) |
| 1198 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t")); |
| 1199 | |
| 1200 | // Scan ahead to the end of line. |
| 1201 | size_t EOL = Buffer.find_first_of("\n\r"); |
| 1202 | |
| 1203 | // Remember the location of the start of the pattern, for diagnostics. |
| 1204 | SMLoc PatternLoc = SMLoc::getFromPointer(Buffer.data()); |
| 1205 | |
| 1206 | // Parse the pattern. |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 1207 | FileCheckPattern P(CheckTy, &PatternContext, LineNumber); |
| 1208 | if (P.parsePattern(Buffer.substr(0, EOL), UsedPrefix, SM, Req)) |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1209 | return true; |
| 1210 | |
| 1211 | // Verify that CHECK-LABEL lines do not define or use variables |
| 1212 | if ((CheckTy == Check::CheckLabel) && P.hasVariable()) { |
| 1213 | SM.PrintMessage( |
| 1214 | SMLoc::getFromPointer(UsedPrefixStart), SourceMgr::DK_Error, |
| 1215 | "found '" + UsedPrefix + "-LABEL:'" |
| 1216 | " with variable definition or use"); |
| 1217 | return true; |
| 1218 | } |
| 1219 | |
| 1220 | Buffer = Buffer.substr(EOL); |
| 1221 | |
| 1222 | // Verify that CHECK-NEXT/SAME/EMPTY lines have at least one CHECK line before them. |
| 1223 | if ((CheckTy == Check::CheckNext || CheckTy == Check::CheckSame || |
| 1224 | CheckTy == Check::CheckEmpty) && |
| 1225 | CheckStrings.empty()) { |
| 1226 | StringRef Type = CheckTy == Check::CheckNext |
| 1227 | ? "NEXT" |
| 1228 | : CheckTy == Check::CheckEmpty ? "EMPTY" : "SAME"; |
| 1229 | SM.PrintMessage(SMLoc::getFromPointer(UsedPrefixStart), |
| 1230 | SourceMgr::DK_Error, |
| 1231 | "found '" + UsedPrefix + "-" + Type + |
| 1232 | "' without previous '" + UsedPrefix + ": line"); |
| 1233 | return true; |
| 1234 | } |
| 1235 | |
| 1236 | // Handle CHECK-DAG/-NOT. |
| 1237 | if (CheckTy == Check::CheckDAG || CheckTy == Check::CheckNot) { |
| 1238 | DagNotMatches.push_back(P); |
| 1239 | continue; |
| 1240 | } |
| 1241 | |
| 1242 | // Okay, add the string we captured to the output vector and move on. |
| 1243 | CheckStrings.emplace_back(P, UsedPrefix, PatternLoc); |
| 1244 | std::swap(DagNotMatches, CheckStrings.back().DagNotStrings); |
| 1245 | DagNotMatches = ImplicitNegativeChecks; |
| 1246 | } |
| 1247 | |
| 1248 | // Add an EOF pattern for any trailing CHECK-DAG/-NOTs, and use the first |
| 1249 | // prefix as a filler for the error message. |
| 1250 | if (!DagNotMatches.empty()) { |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1251 | CheckStrings.emplace_back( |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 1252 | FileCheckPattern(Check::CheckEOF, &PatternContext, LineNumber + 1), |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1253 | *Req.CheckPrefixes.begin(), SMLoc::getFromPointer(Buffer.data())); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1254 | std::swap(DagNotMatches, CheckStrings.back().DagNotStrings); |
| 1255 | } |
| 1256 | |
| 1257 | if (CheckStrings.empty()) { |
| 1258 | errs() << "error: no check strings found with prefix" |
| 1259 | << (Req.CheckPrefixes.size() > 1 ? "es " : " "); |
| 1260 | auto I = Req.CheckPrefixes.begin(); |
| 1261 | auto E = Req.CheckPrefixes.end(); |
| 1262 | if (I != E) { |
| 1263 | errs() << "\'" << *I << ":'"; |
| 1264 | ++I; |
| 1265 | } |
| 1266 | for (; I != E; ++I) |
| 1267 | errs() << ", \'" << *I << ":'"; |
| 1268 | |
| 1269 | errs() << '\n'; |
| 1270 | return true; |
| 1271 | } |
| 1272 | |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
| 1276 | static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM, |
| 1277 | StringRef Prefix, SMLoc Loc, const FileCheckPattern &Pat, |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1278 | int MatchedCount, StringRef Buffer, size_t MatchPos, |
Joel E. Denny | 0e7e3fa | 2018-12-18 00:02:47 +0000 | [diff] [blame] | 1279 | size_t MatchLen, const FileCheckRequest &Req, |
| 1280 | std::vector<FileCheckDiag> *Diags) { |
Joel E. Denny | 352695c | 2019-01-22 21:41:42 +0000 | [diff] [blame] | 1281 | bool PrintDiag = true; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1282 | if (ExpectedMatch) { |
| 1283 | if (!Req.Verbose) |
| 1284 | return; |
| 1285 | if (!Req.VerboseVerbose && Pat.getCheckTy() == Check::CheckEOF) |
| 1286 | return; |
Joel E. Denny | 352695c | 2019-01-22 21:41:42 +0000 | [diff] [blame] | 1287 | // Due to their verbosity, we don't print verbose diagnostics here if we're |
| 1288 | // gathering them for a different rendering, but we always print other |
| 1289 | // diagnostics. |
| 1290 | PrintDiag = !Diags; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1291 | } |
Joel E. Denny | 0e7e3fa | 2018-12-18 00:02:47 +0000 | [diff] [blame] | 1292 | SMRange MatchRange = ProcessMatchResult( |
Joel E. Denny | e2afb61 | 2018-12-18 00:03:51 +0000 | [diff] [blame] | 1293 | ExpectedMatch ? FileCheckDiag::MatchFoundAndExpected |
| 1294 | : FileCheckDiag::MatchFoundButExcluded, |
Joel E. Denny | 0e7e3fa | 2018-12-18 00:02:47 +0000 | [diff] [blame] | 1295 | SM, Loc, Pat.getCheckTy(), Buffer, MatchPos, MatchLen, Diags); |
Joel E. Denny | 352695c | 2019-01-22 21:41:42 +0000 | [diff] [blame] | 1296 | if (!PrintDiag) |
| 1297 | return; |
| 1298 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1299 | std::string Message = formatv("{0}: {1} string found in input", |
| 1300 | Pat.getCheckTy().getDescription(Prefix), |
| 1301 | (ExpectedMatch ? "expected" : "excluded")) |
| 1302 | .str(); |
| 1303 | if (Pat.getCount() > 1) |
| 1304 | Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str(); |
| 1305 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1306 | SM.PrintMessage( |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1307 | Loc, ExpectedMatch ? SourceMgr::DK_Remark : SourceMgr::DK_Error, Message); |
Joel E. Denny | 0e7e3fa | 2018-12-18 00:02:47 +0000 | [diff] [blame] | 1308 | SM.PrintMessage(MatchRange.Start, SourceMgr::DK_Note, "found here", |
| 1309 | {MatchRange}); |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 1310 | Pat.printSubstitutions(SM, Buffer, MatchRange); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | static void PrintMatch(bool ExpectedMatch, const SourceMgr &SM, |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1314 | const FileCheckString &CheckStr, int MatchedCount, |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1315 | StringRef Buffer, size_t MatchPos, size_t MatchLen, |
| 1316 | FileCheckRequest &Req, |
Joel E. Denny | 0e7e3fa | 2018-12-18 00:02:47 +0000 | [diff] [blame] | 1317 | std::vector<FileCheckDiag> *Diags) { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1318 | PrintMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat, |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1319 | MatchedCount, Buffer, MatchPos, MatchLen, Req, Diags); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM, |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1323 | StringRef Prefix, SMLoc Loc, |
| 1324 | const FileCheckPattern &Pat, int MatchedCount, |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1325 | StringRef Buffer, bool VerboseVerbose, |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1326 | std::vector<FileCheckDiag> *Diags, Error MatchErrors) { |
| 1327 | assert(MatchErrors && "Called on successful match"); |
Joel E. Denny | 352695c | 2019-01-22 21:41:42 +0000 | [diff] [blame] | 1328 | bool PrintDiag = true; |
| 1329 | if (!ExpectedMatch) { |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1330 | if (!VerboseVerbose) { |
| 1331 | consumeError(std::move(MatchErrors)); |
Joel E. Denny | 352695c | 2019-01-22 21:41:42 +0000 | [diff] [blame] | 1332 | return; |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1333 | } |
Joel E. Denny | 352695c | 2019-01-22 21:41:42 +0000 | [diff] [blame] | 1334 | // Due to their verbosity, we don't print verbose diagnostics here if we're |
| 1335 | // gathering them for a different rendering, but we always print other |
| 1336 | // diagnostics. |
| 1337 | PrintDiag = !Diags; |
| 1338 | } |
| 1339 | |
| 1340 | // If the current position is at the end of a line, advance to the start of |
| 1341 | // the next line. |
| 1342 | Buffer = Buffer.substr(Buffer.find_first_not_of(" \t\n\r")); |
| 1343 | SMRange SearchRange = ProcessMatchResult( |
| 1344 | ExpectedMatch ? FileCheckDiag::MatchNoneButExpected |
| 1345 | : FileCheckDiag::MatchNoneAndExcluded, |
| 1346 | SM, Loc, Pat.getCheckTy(), Buffer, 0, Buffer.size(), Diags); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1347 | if (!PrintDiag) { |
| 1348 | consumeError(std::move(MatchErrors)); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1349 | return; |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | MatchErrors = |
| 1353 | handleErrors(std::move(MatchErrors), |
| 1354 | [](const FileCheckErrorDiagnostic &E) { E.log(errs()); }); |
| 1355 | |
| 1356 | // No problem matching the string per se. |
| 1357 | if (!MatchErrors) |
| 1358 | return; |
| 1359 | consumeError(std::move(MatchErrors)); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1360 | |
Joel E. Denny | 352695c | 2019-01-22 21:41:42 +0000 | [diff] [blame] | 1361 | // Print "not found" diagnostic. |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1362 | std::string Message = formatv("{0}: {1} string not found in input", |
| 1363 | Pat.getCheckTy().getDescription(Prefix), |
| 1364 | (ExpectedMatch ? "expected" : "excluded")) |
| 1365 | .str(); |
| 1366 | if (Pat.getCount() > 1) |
| 1367 | Message += formatv(" ({0} out of {1})", MatchedCount, Pat.getCount()).str(); |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1368 | SM.PrintMessage( |
| 1369 | Loc, ExpectedMatch ? SourceMgr::DK_Error : SourceMgr::DK_Remark, Message); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1370 | |
Joel E. Denny | 352695c | 2019-01-22 21:41:42 +0000 | [diff] [blame] | 1371 | // Print the "scanning from here" line. |
Joel E. Denny | 3c5d267 | 2018-12-18 00:01:39 +0000 | [diff] [blame] | 1372 | SM.PrintMessage(SearchRange.Start, SourceMgr::DK_Note, "scanning from here"); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1373 | |
| 1374 | // Allow the pattern to print additional information if desired. |
Thomas Preud'homme | 288ed91 | 2019-05-02 00:04:38 +0000 | [diff] [blame] | 1375 | Pat.printSubstitutions(SM, Buffer); |
Joel E. Denny | 96f0e84 | 2018-12-18 00:03:36 +0000 | [diff] [blame] | 1376 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1377 | if (ExpectedMatch) |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1378 | Pat.printFuzzyMatch(SM, Buffer, Diags); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | static void PrintNoMatch(bool ExpectedMatch, const SourceMgr &SM, |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1382 | const FileCheckString &CheckStr, int MatchedCount, |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1383 | StringRef Buffer, bool VerboseVerbose, |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1384 | std::vector<FileCheckDiag> *Diags, Error MatchErrors) { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1385 | PrintNoMatch(ExpectedMatch, SM, CheckStr.Prefix, CheckStr.Loc, CheckStr.Pat, |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1386 | MatchedCount, Buffer, VerboseVerbose, Diags, |
| 1387 | std::move(MatchErrors)); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Thomas Preud'homme | 4a8ef11 | 2019-05-08 21:47:31 +0000 | [diff] [blame] | 1390 | /// Counts the number of newlines in the specified range. |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1391 | static unsigned CountNumNewlinesBetween(StringRef Range, |
| 1392 | const char *&FirstNewLine) { |
| 1393 | unsigned NumNewLines = 0; |
| 1394 | while (1) { |
| 1395 | // Scan for newline. |
| 1396 | Range = Range.substr(Range.find_first_of("\n\r")); |
| 1397 | if (Range.empty()) |
| 1398 | return NumNewLines; |
| 1399 | |
| 1400 | ++NumNewLines; |
| 1401 | |
| 1402 | // Handle \n\r and \r\n as a single newline. |
| 1403 | if (Range.size() > 1 && (Range[1] == '\n' || Range[1] == '\r') && |
| 1404 | (Range[0] != Range[1])) |
| 1405 | Range = Range.substr(1); |
| 1406 | Range = Range.substr(1); |
| 1407 | |
| 1408 | if (NumNewLines == 1) |
| 1409 | FirstNewLine = Range.begin(); |
| 1410 | } |
| 1411 | } |
| 1412 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1413 | size_t FileCheckString::Check(const SourceMgr &SM, StringRef Buffer, |
Joel E. Denny | 3c5d267 | 2018-12-18 00:01:39 +0000 | [diff] [blame] | 1414 | bool IsLabelScanMode, size_t &MatchLen, |
Joel E. Denny | 3c5d267 | 2018-12-18 00:01:39 +0000 | [diff] [blame] | 1415 | FileCheckRequest &Req, |
| 1416 | std::vector<FileCheckDiag> *Diags) const { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1417 | size_t LastPos = 0; |
| 1418 | std::vector<const FileCheckPattern *> NotStrings; |
| 1419 | |
| 1420 | // IsLabelScanMode is true when we are scanning forward to find CHECK-LABEL |
| 1421 | // bounds; we have not processed variable definitions within the bounded block |
| 1422 | // yet so cannot handle any final CHECK-DAG yet; this is handled when going |
| 1423 | // over the block again (including the last CHECK-LABEL) in normal mode. |
| 1424 | if (!IsLabelScanMode) { |
| 1425 | // Match "dag strings" (with mixed "not strings" if any). |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1426 | LastPos = CheckDag(SM, Buffer, NotStrings, Req, Diags); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1427 | if (LastPos == StringRef::npos) |
| 1428 | return StringRef::npos; |
| 1429 | } |
| 1430 | |
| 1431 | // Match itself from the last position after matching CHECK-DAG. |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1432 | size_t LastMatchEnd = LastPos; |
| 1433 | size_t FirstMatchPos = 0; |
| 1434 | // Go match the pattern Count times. Majority of patterns only match with |
| 1435 | // count 1 though. |
| 1436 | assert(Pat.getCount() != 0 && "pattern count can not be zero"); |
| 1437 | for (int i = 1; i <= Pat.getCount(); i++) { |
| 1438 | StringRef MatchBuffer = Buffer.substr(LastMatchEnd); |
| 1439 | size_t CurrentMatchLen; |
| 1440 | // get a match at current start point |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1441 | Expected<size_t> MatchResult = Pat.match(MatchBuffer, CurrentMatchLen, SM); |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1442 | |
| 1443 | // report |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1444 | if (!MatchResult) { |
| 1445 | PrintNoMatch(true, SM, *this, i, MatchBuffer, Req.VerboseVerbose, Diags, |
| 1446 | MatchResult.takeError()); |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1447 | return StringRef::npos; |
| 1448 | } |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1449 | size_t MatchPos = *MatchResult; |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1450 | PrintMatch(true, SM, *this, i, MatchBuffer, MatchPos, CurrentMatchLen, Req, |
| 1451 | Diags); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1452 | if (i == 1) |
| 1453 | FirstMatchPos = LastPos + MatchPos; |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1454 | |
| 1455 | // move start point after the match |
| 1456 | LastMatchEnd += MatchPos + CurrentMatchLen; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1457 | } |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1458 | // Full match len counts from first match pos. |
| 1459 | MatchLen = LastMatchEnd - FirstMatchPos; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1460 | |
| 1461 | // Similar to the above, in "label-scan mode" we can't yet handle CHECK-NEXT |
| 1462 | // or CHECK-NOT |
| 1463 | if (!IsLabelScanMode) { |
Joel E. Denny | cadfcef | 2018-12-18 00:02:22 +0000 | [diff] [blame] | 1464 | size_t MatchPos = FirstMatchPos - LastPos; |
| 1465 | StringRef MatchBuffer = Buffer.substr(LastPos); |
| 1466 | StringRef SkippedRegion = Buffer.substr(LastPos, MatchPos); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1467 | |
| 1468 | // If this check is a "CHECK-NEXT", verify that the previous match was on |
| 1469 | // the previous line (i.e. that there is one newline between them). |
Joel E. Denny | cadfcef | 2018-12-18 00:02:22 +0000 | [diff] [blame] | 1470 | if (CheckNext(SM, SkippedRegion)) { |
Joel E. Denny | e2afb61 | 2018-12-18 00:03:51 +0000 | [diff] [blame] | 1471 | ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc, |
Joel E. Denny | cadfcef | 2018-12-18 00:02:22 +0000 | [diff] [blame] | 1472 | Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen, |
Joel E. Denny | 7df8696 | 2018-12-18 00:03:03 +0000 | [diff] [blame] | 1473 | Diags, Req.Verbose); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1474 | return StringRef::npos; |
Joel E. Denny | cadfcef | 2018-12-18 00:02:22 +0000 | [diff] [blame] | 1475 | } |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1476 | |
| 1477 | // If this check is a "CHECK-SAME", verify that the previous match was on |
| 1478 | // the same line (i.e. that there is no newline between them). |
Joel E. Denny | cadfcef | 2018-12-18 00:02:22 +0000 | [diff] [blame] | 1479 | if (CheckSame(SM, SkippedRegion)) { |
Joel E. Denny | e2afb61 | 2018-12-18 00:03:51 +0000 | [diff] [blame] | 1480 | ProcessMatchResult(FileCheckDiag::MatchFoundButWrongLine, SM, Loc, |
Joel E. Denny | cadfcef | 2018-12-18 00:02:22 +0000 | [diff] [blame] | 1481 | Pat.getCheckTy(), MatchBuffer, MatchPos, MatchLen, |
Joel E. Denny | 7df8696 | 2018-12-18 00:03:03 +0000 | [diff] [blame] | 1482 | Diags, Req.Verbose); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1483 | return StringRef::npos; |
Joel E. Denny | cadfcef | 2018-12-18 00:02:22 +0000 | [diff] [blame] | 1484 | } |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1485 | |
| 1486 | // If this match had "not strings", verify that they don't exist in the |
| 1487 | // skipped region. |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1488 | if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags)) |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1489 | return StringRef::npos; |
| 1490 | } |
| 1491 | |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1492 | return FirstMatchPos; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1495 | bool FileCheckString::CheckNext(const SourceMgr &SM, StringRef Buffer) const { |
| 1496 | if (Pat.getCheckTy() != Check::CheckNext && |
| 1497 | Pat.getCheckTy() != Check::CheckEmpty) |
| 1498 | return false; |
| 1499 | |
| 1500 | Twine CheckName = |
| 1501 | Prefix + |
| 1502 | Twine(Pat.getCheckTy() == Check::CheckEmpty ? "-EMPTY" : "-NEXT"); |
| 1503 | |
| 1504 | // Count the number of newlines between the previous match and this one. |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1505 | const char *FirstNewLine = nullptr; |
| 1506 | unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine); |
| 1507 | |
| 1508 | if (NumNewLines == 0) { |
| 1509 | SM.PrintMessage(Loc, SourceMgr::DK_Error, |
| 1510 | CheckName + ": is on the same line as previous match"); |
| 1511 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note, |
| 1512 | "'next' match was here"); |
| 1513 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note, |
| 1514 | "previous match ended here"); |
| 1515 | return true; |
| 1516 | } |
| 1517 | |
| 1518 | if (NumNewLines != 1) { |
| 1519 | SM.PrintMessage(Loc, SourceMgr::DK_Error, |
| 1520 | CheckName + |
| 1521 | ": is not on the line after the previous match"); |
| 1522 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note, |
| 1523 | "'next' match was here"); |
| 1524 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note, |
| 1525 | "previous match ended here"); |
| 1526 | SM.PrintMessage(SMLoc::getFromPointer(FirstNewLine), SourceMgr::DK_Note, |
| 1527 | "non-matching line after previous match is here"); |
| 1528 | return true; |
| 1529 | } |
| 1530 | |
| 1531 | return false; |
| 1532 | } |
| 1533 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1534 | bool FileCheckString::CheckSame(const SourceMgr &SM, StringRef Buffer) const { |
| 1535 | if (Pat.getCheckTy() != Check::CheckSame) |
| 1536 | return false; |
| 1537 | |
| 1538 | // Count the number of newlines between the previous match and this one. |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1539 | const char *FirstNewLine = nullptr; |
| 1540 | unsigned NumNewLines = CountNumNewlinesBetween(Buffer, FirstNewLine); |
| 1541 | |
| 1542 | if (NumNewLines != 0) { |
| 1543 | SM.PrintMessage(Loc, SourceMgr::DK_Error, |
| 1544 | Prefix + |
| 1545 | "-SAME: is not on the same line as the previous match"); |
| 1546 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.end()), SourceMgr::DK_Note, |
| 1547 | "'next' match was here"); |
| 1548 | SM.PrintMessage(SMLoc::getFromPointer(Buffer.data()), SourceMgr::DK_Note, |
| 1549 | "previous match ended here"); |
| 1550 | return true; |
| 1551 | } |
| 1552 | |
| 1553 | return false; |
| 1554 | } |
| 1555 | |
Joel E. Denny | 0e7e3fa | 2018-12-18 00:02:47 +0000 | [diff] [blame] | 1556 | bool FileCheckString::CheckNot( |
| 1557 | const SourceMgr &SM, StringRef Buffer, |
| 1558 | const std::vector<const FileCheckPattern *> &NotStrings, |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1559 | const FileCheckRequest &Req, std::vector<FileCheckDiag> *Diags) const { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1560 | for (const FileCheckPattern *Pat : NotStrings) { |
| 1561 | assert((Pat->getCheckTy() == Check::CheckNot) && "Expect CHECK-NOT!"); |
| 1562 | |
| 1563 | size_t MatchLen = 0; |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1564 | Expected<size_t> MatchResult = Pat->match(Buffer, MatchLen, SM); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1565 | |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1566 | if (!MatchResult) { |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1567 | PrintNoMatch(false, SM, Prefix, Pat->getLoc(), *Pat, 1, Buffer, |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1568 | Req.VerboseVerbose, Diags, MatchResult.takeError()); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1569 | continue; |
| 1570 | } |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1571 | size_t Pos = *MatchResult; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1572 | |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1573 | PrintMatch(false, SM, Prefix, Pat->getLoc(), *Pat, 1, Buffer, Pos, MatchLen, |
| 1574 | Req, Diags); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1575 | |
| 1576 | return true; |
| 1577 | } |
| 1578 | |
| 1579 | return false; |
| 1580 | } |
| 1581 | |
Joel E. Denny | 3c5d267 | 2018-12-18 00:01:39 +0000 | [diff] [blame] | 1582 | size_t |
| 1583 | FileCheckString::CheckDag(const SourceMgr &SM, StringRef Buffer, |
| 1584 | std::vector<const FileCheckPattern *> &NotStrings, |
Joel E. Denny | 3c5d267 | 2018-12-18 00:01:39 +0000 | [diff] [blame] | 1585 | const FileCheckRequest &Req, |
| 1586 | std::vector<FileCheckDiag> *Diags) const { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1587 | if (DagNotStrings.empty()) |
| 1588 | return 0; |
| 1589 | |
| 1590 | // The start of the search range. |
| 1591 | size_t StartPos = 0; |
| 1592 | |
| 1593 | struct MatchRange { |
| 1594 | size_t Pos; |
| 1595 | size_t End; |
| 1596 | }; |
| 1597 | // A sorted list of ranges for non-overlapping CHECK-DAG matches. Match |
| 1598 | // ranges are erased from this list once they are no longer in the search |
| 1599 | // range. |
| 1600 | std::list<MatchRange> MatchRanges; |
| 1601 | |
| 1602 | // We need PatItr and PatEnd later for detecting the end of a CHECK-DAG |
| 1603 | // group, so we don't use a range-based for loop here. |
| 1604 | for (auto PatItr = DagNotStrings.begin(), PatEnd = DagNotStrings.end(); |
| 1605 | PatItr != PatEnd; ++PatItr) { |
| 1606 | const FileCheckPattern &Pat = *PatItr; |
| 1607 | assert((Pat.getCheckTy() == Check::CheckDAG || |
| 1608 | Pat.getCheckTy() == Check::CheckNot) && |
| 1609 | "Invalid CHECK-DAG or CHECK-NOT!"); |
| 1610 | |
| 1611 | if (Pat.getCheckTy() == Check::CheckNot) { |
| 1612 | NotStrings.push_back(&Pat); |
| 1613 | continue; |
| 1614 | } |
| 1615 | |
| 1616 | assert((Pat.getCheckTy() == Check::CheckDAG) && "Expect CHECK-DAG!"); |
| 1617 | |
| 1618 | // CHECK-DAG always matches from the start. |
| 1619 | size_t MatchLen = 0, MatchPos = StartPos; |
| 1620 | |
| 1621 | // Search for a match that doesn't overlap a previous match in this |
| 1622 | // CHECK-DAG group. |
| 1623 | for (auto MI = MatchRanges.begin(), ME = MatchRanges.end(); true; ++MI) { |
| 1624 | StringRef MatchBuffer = Buffer.substr(MatchPos); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1625 | Expected<size_t> MatchResult = Pat.match(MatchBuffer, MatchLen, SM); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1626 | // With a group of CHECK-DAGs, a single mismatching means the match on |
| 1627 | // that group of CHECK-DAGs fails immediately. |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1628 | if (!MatchResult) { |
Fedor Sergeev | 6c9e19b | 2018-11-13 00:46:13 +0000 | [diff] [blame] | 1629 | PrintNoMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, MatchBuffer, |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1630 | Req.VerboseVerbose, Diags, MatchResult.takeError()); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1631 | return StringRef::npos; |
| 1632 | } |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1633 | size_t MatchPosBuf = *MatchResult; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1634 | // Re-calc it as the offset relative to the start of the original string. |
| 1635 | MatchPos += MatchPosBuf; |
| 1636 | if (Req.VerboseVerbose) |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1637 | PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer, MatchPos, |
| 1638 | MatchLen, Req, Diags); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1639 | MatchRange M{MatchPos, MatchPos + MatchLen}; |
| 1640 | if (Req.AllowDeprecatedDagOverlap) { |
| 1641 | // We don't need to track all matches in this mode, so we just maintain |
| 1642 | // one match range that encompasses the current CHECK-DAG group's |
| 1643 | // matches. |
| 1644 | if (MatchRanges.empty()) |
| 1645 | MatchRanges.insert(MatchRanges.end(), M); |
| 1646 | else { |
| 1647 | auto Block = MatchRanges.begin(); |
| 1648 | Block->Pos = std::min(Block->Pos, M.Pos); |
| 1649 | Block->End = std::max(Block->End, M.End); |
| 1650 | } |
| 1651 | break; |
| 1652 | } |
| 1653 | // Iterate previous matches until overlapping match or insertion point. |
| 1654 | bool Overlap = false; |
| 1655 | for (; MI != ME; ++MI) { |
| 1656 | if (M.Pos < MI->End) { |
| 1657 | // !Overlap => New match has no overlap and is before this old match. |
| 1658 | // Overlap => New match overlaps this old match. |
| 1659 | Overlap = MI->Pos < M.End; |
| 1660 | break; |
| 1661 | } |
| 1662 | } |
| 1663 | if (!Overlap) { |
| 1664 | // Insert non-overlapping match into list. |
| 1665 | MatchRanges.insert(MI, M); |
| 1666 | break; |
| 1667 | } |
| 1668 | if (Req.VerboseVerbose) { |
Joel E. Denny | 352695c | 2019-01-22 21:41:42 +0000 | [diff] [blame] | 1669 | // Due to their verbosity, we don't print verbose diagnostics here if |
| 1670 | // we're gathering them for a different rendering, but we always print |
| 1671 | // other diagnostics. |
| 1672 | if (!Diags) { |
| 1673 | SMLoc OldStart = SMLoc::getFromPointer(Buffer.data() + MI->Pos); |
| 1674 | SMLoc OldEnd = SMLoc::getFromPointer(Buffer.data() + MI->End); |
| 1675 | SMRange OldRange(OldStart, OldEnd); |
| 1676 | SM.PrintMessage(OldStart, SourceMgr::DK_Note, |
| 1677 | "match discarded, overlaps earlier DAG match here", |
| 1678 | {OldRange}); |
| 1679 | } else |
Joel E. Denny | e2afb61 | 2018-12-18 00:03:51 +0000 | [diff] [blame] | 1680 | Diags->rbegin()->MatchTy = FileCheckDiag::MatchFoundButDiscarded; |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1681 | } |
| 1682 | MatchPos = MI->End; |
| 1683 | } |
| 1684 | if (!Req.VerboseVerbose) |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1685 | PrintMatch(true, SM, Prefix, Pat.getLoc(), Pat, 1, Buffer, MatchPos, |
| 1686 | MatchLen, Req, Diags); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1687 | |
| 1688 | // Handle the end of a CHECK-DAG group. |
| 1689 | if (std::next(PatItr) == PatEnd || |
| 1690 | std::next(PatItr)->getCheckTy() == Check::CheckNot) { |
| 1691 | if (!NotStrings.empty()) { |
| 1692 | // If there are CHECK-NOTs between two CHECK-DAGs or from CHECK to |
| 1693 | // CHECK-DAG, verify that there are no 'not' strings occurred in that |
| 1694 | // region. |
| 1695 | StringRef SkippedRegion = |
| 1696 | Buffer.slice(StartPos, MatchRanges.begin()->Pos); |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1697 | if (CheckNot(SM, SkippedRegion, NotStrings, Req, Diags)) |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1698 | return StringRef::npos; |
| 1699 | // Clear "not strings". |
| 1700 | NotStrings.clear(); |
| 1701 | } |
| 1702 | // All subsequent CHECK-DAGs and CHECK-NOTs should be matched from the |
| 1703 | // end of this CHECK-DAG group's match range. |
| 1704 | StartPos = MatchRanges.rbegin()->End; |
| 1705 | // Don't waste time checking for (impossible) overlaps before that. |
| 1706 | MatchRanges.clear(); |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | return StartPos; |
| 1711 | } |
| 1712 | |
| 1713 | // A check prefix must contain only alphanumeric, hyphens and underscores. |
| 1714 | static bool ValidateCheckPrefix(StringRef CheckPrefix) { |
| 1715 | Regex Validator("^[a-zA-Z0-9_-]*$"); |
| 1716 | return Validator.match(CheckPrefix); |
| 1717 | } |
| 1718 | |
Thomas Preud'homme | 7b7683d | 2019-05-23 17:19:36 +0000 | [diff] [blame] | 1719 | bool FileCheck::ValidateCheckPrefixes() { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1720 | StringSet<> PrefixSet; |
| 1721 | |
| 1722 | for (StringRef Prefix : Req.CheckPrefixes) { |
| 1723 | // Reject empty prefixes. |
| 1724 | if (Prefix == "") |
| 1725 | return false; |
| 1726 | |
| 1727 | if (!PrefixSet.insert(Prefix).second) |
| 1728 | return false; |
| 1729 | |
| 1730 | if (!ValidateCheckPrefix(Prefix)) |
| 1731 | return false; |
| 1732 | } |
| 1733 | |
| 1734 | return true; |
| 1735 | } |
| 1736 | |
Thomas Preud'homme | 7b7683d | 2019-05-23 17:19:36 +0000 | [diff] [blame] | 1737 | Regex FileCheck::buildCheckPrefixRegex() { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1738 | // I don't think there's a way to specify an initial value for cl::list, |
| 1739 | // so if nothing was specified, add the default |
| 1740 | if (Req.CheckPrefixes.empty()) |
| 1741 | Req.CheckPrefixes.push_back("CHECK"); |
| 1742 | |
| 1743 | // We already validated the contents of CheckPrefixes so just concatenate |
| 1744 | // them as alternatives. |
| 1745 | SmallString<32> PrefixRegexStr; |
| 1746 | for (StringRef Prefix : Req.CheckPrefixes) { |
| 1747 | if (Prefix != Req.CheckPrefixes.front()) |
| 1748 | PrefixRegexStr.push_back('|'); |
| 1749 | |
| 1750 | PrefixRegexStr.append(Prefix); |
| 1751 | } |
| 1752 | |
| 1753 | return Regex(PrefixRegexStr); |
| 1754 | } |
| 1755 | |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1756 | Error FileCheckPatternContext::defineCmdlineVariables( |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 1757 | std::vector<std::string> &CmdlineDefines, SourceMgr &SM) { |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1758 | assert(GlobalVariableTable.empty() && GlobalNumericVariableTable.empty() && |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1759 | "Overriding defined variable with command-line variable definitions"); |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 1760 | |
| 1761 | if (CmdlineDefines.empty()) |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1762 | return Error::success(); |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 1763 | |
| 1764 | // Create a string representing the vector of command-line definitions. Each |
| 1765 | // definition is on its own line and prefixed with a definition number to |
| 1766 | // clarify which definition a given diagnostic corresponds to. |
| 1767 | unsigned I = 0; |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1768 | Error Errs = Error::success(); |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 1769 | std::string CmdlineDefsDiag; |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 1770 | SmallVector<std::pair<size_t, size_t>, 4> CmdlineDefsIndices; |
| 1771 | for (StringRef CmdlineDef : CmdlineDefines) { |
| 1772 | std::string DefPrefix = ("Global define #" + Twine(++I) + ": ").str(); |
| 1773 | size_t EqIdx = CmdlineDef.find('='); |
| 1774 | if (EqIdx == StringRef::npos) { |
| 1775 | CmdlineDefsIndices.push_back(std::make_pair(CmdlineDefsDiag.size(), 0)); |
| 1776 | continue; |
| 1777 | } |
| 1778 | // Numeric variable definition. |
| 1779 | if (CmdlineDef[0] == '#') { |
| 1780 | // Append a copy of the command-line definition adapted to use the same |
| 1781 | // format as in the input file to be able to reuse |
| 1782 | // parseNumericSubstitutionBlock. |
| 1783 | CmdlineDefsDiag += (DefPrefix + CmdlineDef + " (parsed as: [[").str(); |
| 1784 | std::string SubstitutionStr = CmdlineDef; |
| 1785 | SubstitutionStr[EqIdx] = ':'; |
| 1786 | CmdlineDefsIndices.push_back( |
| 1787 | std::make_pair(CmdlineDefsDiag.size(), SubstitutionStr.size())); |
| 1788 | CmdlineDefsDiag += (SubstitutionStr + Twine("]])\n")).str(); |
| 1789 | } else { |
| 1790 | CmdlineDefsDiag += DefPrefix; |
| 1791 | CmdlineDefsIndices.push_back( |
| 1792 | std::make_pair(CmdlineDefsDiag.size(), CmdlineDef.size())); |
| 1793 | CmdlineDefsDiag += (CmdlineDef + "\n").str(); |
| 1794 | } |
| 1795 | } |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 1796 | |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1797 | // Create a buffer with fake command line content in order to display |
| 1798 | // parsing diagnostic with location information and point to the |
| 1799 | // global definition with invalid syntax. |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 1800 | std::unique_ptr<MemoryBuffer> CmdLineDefsDiagBuffer = |
| 1801 | MemoryBuffer::getMemBufferCopy(CmdlineDefsDiag, "Global defines"); |
| 1802 | StringRef CmdlineDefsDiagRef = CmdLineDefsDiagBuffer->getBuffer(); |
| 1803 | SM.AddNewSourceBuffer(std::move(CmdLineDefsDiagBuffer), SMLoc()); |
| 1804 | |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 1805 | for (std::pair<size_t, size_t> CmdlineDefIndices : CmdlineDefsIndices) { |
| 1806 | StringRef CmdlineDef = CmdlineDefsDiagRef.substr(CmdlineDefIndices.first, |
| 1807 | CmdlineDefIndices.second); |
| 1808 | if (CmdlineDef.empty()) { |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1809 | Errs = joinErrors( |
| 1810 | std::move(Errs), |
| 1811 | FileCheckErrorDiagnostic::get( |
| 1812 | SM, CmdlineDef, "missing equal sign in global definition")); |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 1813 | continue; |
| 1814 | } |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1815 | |
| 1816 | // Numeric variable definition. |
| 1817 | if (CmdlineDef[0] == '#') { |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 1818 | // Now parse the definition both to check that the syntax is correct and |
| 1819 | // to create the necessary class instance. |
| 1820 | StringRef CmdlineDefExpr = CmdlineDef.substr(1); |
| 1821 | Optional<FileCheckNumericVariable *> DefinedNumericVariable; |
| 1822 | Expected<std::unique_ptr<FileCheckExpressionAST>> ExpressionASTResult = |
| 1823 | FileCheckPattern::parseNumericSubstitutionBlock( |
| 1824 | CmdlineDefExpr, DefinedNumericVariable, false, None, this, SM); |
| 1825 | if (!ExpressionASTResult) { |
| 1826 | Errs = joinErrors(std::move(Errs), ExpressionASTResult.takeError()); |
| 1827 | continue; |
| 1828 | } |
| 1829 | std::unique_ptr<FileCheckExpressionAST> ExpressionAST = |
| 1830 | std::move(*ExpressionASTResult); |
| 1831 | // Now evaluate the expression whose value this variable should be set |
| 1832 | // to, since the expression of a command-line variable definition should |
| 1833 | // only use variables defined earlier on the command-line. If not, this |
| 1834 | // is an error and we report it. |
| 1835 | Expected<uint64_t> Value = ExpressionAST->eval(); |
| 1836 | if (!Value) { |
| 1837 | Errs = joinErrors(std::move(Errs), Value.takeError()); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1838 | continue; |
| 1839 | } |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1840 | |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 1841 | assert(DefinedNumericVariable && "No variable defined"); |
| 1842 | (*DefinedNumericVariable)->setValue(*Value); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1843 | |
| 1844 | // Record this variable definition. |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 1845 | GlobalNumericVariableTable[(*DefinedNumericVariable)->getName()] = |
| 1846 | *DefinedNumericVariable; |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1847 | } else { |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 1848 | // String variable definition. |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1849 | std::pair<StringRef, StringRef> CmdlineNameVal = CmdlineDef.split('='); |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 1850 | StringRef CmdlineName = CmdlineNameVal.first; |
| 1851 | StringRef OrigCmdlineName = CmdlineName; |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 1852 | Expected<FileCheckPattern::VariableProperties> ParseVarResult = |
| 1853 | FileCheckPattern::parseVariable(CmdlineName, SM); |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1854 | if (!ParseVarResult) { |
| 1855 | Errs = joinErrors(std::move(Errs), ParseVarResult.takeError()); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1856 | continue; |
| 1857 | } |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1858 | // Check that CmdlineName does not denote a pseudo variable is only |
| 1859 | // composed of the parsed numeric variable. This catches cases like |
| 1860 | // "FOO+2" in a "FOO+2=10" definition. |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 1861 | if (ParseVarResult->IsPseudo || !CmdlineName.empty()) { |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1862 | Errs = joinErrors(std::move(Errs), |
| 1863 | FileCheckErrorDiagnostic::get( |
| 1864 | SM, OrigCmdlineName, |
| 1865 | "invalid name in string variable definition '" + |
| 1866 | OrigCmdlineName + "'")); |
| 1867 | continue; |
| 1868 | } |
Thomas Preud'homme | 2a7f520 | 2019-07-13 13:24:30 +0000 | [diff] [blame] | 1869 | StringRef Name = ParseVarResult->Name; |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1870 | |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 1871 | // Detect collisions between string and numeric variables when the former |
| 1872 | // is created later than the latter. |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1873 | if (GlobalNumericVariableTable.find(Name) != |
| 1874 | GlobalNumericVariableTable.end()) { |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1875 | Errs = joinErrors(std::move(Errs), FileCheckErrorDiagnostic::get( |
| 1876 | SM, Name, |
| 1877 | "numeric variable with name '" + |
| 1878 | Name + "' already exists")); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1879 | continue; |
| 1880 | } |
| 1881 | GlobalVariableTable.insert(CmdlineNameVal); |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 1882 | // Mark the string variable as defined to detect collisions between |
Thomas Preud'homme | 4cd9b85 | 2019-07-24 12:38:22 +0000 | [diff] [blame] | 1883 | // string and numeric variables in defineCmdlineVariables when the latter |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 1884 | // is created later than the former. We cannot reuse GlobalVariableTable |
Thomas Preud'homme | 71d3f22 | 2019-06-06 13:21:06 +0000 | [diff] [blame] | 1885 | // for this by populating it with an empty string since we would then |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 1886 | // lose the ability to detect the use of an undefined variable in |
| 1887 | // match(). |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1888 | DefinedVariableTable[Name] = true; |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 1889 | } |
Thomas Preud'homme | 5a33047 | 2019-04-29 13:32:36 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Thomas Preud'homme | baae41f | 2019-06-19 23:47:10 +0000 | [diff] [blame] | 1892 | return Errs; |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1893 | } |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1894 | |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1895 | void FileCheckPatternContext::clearLocalVars() { |
| 1896 | SmallVector<StringRef, 16> LocalPatternVars, LocalNumericVars; |
| 1897 | for (const StringMapEntry<StringRef> &Var : GlobalVariableTable) |
| 1898 | if (Var.first()[0] != '$') |
| 1899 | LocalPatternVars.push_back(Var.first()); |
| 1900 | |
Thomas Preud'homme | 1a944d2 | 2019-05-23 00:10:14 +0000 | [diff] [blame] | 1901 | // Numeric substitution reads the value of a variable directly, not via |
| 1902 | // GlobalNumericVariableTable. Therefore, we clear local variables by |
| 1903 | // clearing their value which will lead to a numeric substitution failure. We |
| 1904 | // also mark the variable for removal from GlobalNumericVariableTable since |
| 1905 | // this is what defineCmdlineVariables checks to decide that no global |
| 1906 | // variable has been defined. |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1907 | for (const auto &Var : GlobalNumericVariableTable) |
| 1908 | if (Var.first()[0] != '$') { |
| 1909 | Var.getValue()->clearValue(); |
| 1910 | LocalNumericVars.push_back(Var.first()); |
| 1911 | } |
| 1912 | |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1913 | for (const auto &Var : LocalPatternVars) |
| 1914 | GlobalVariableTable.erase(Var); |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1915 | for (const auto &Var : LocalNumericVars) |
| 1916 | GlobalNumericVariableTable.erase(Var); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Thomas Preud'homme | 7b7683d | 2019-05-23 17:19:36 +0000 | [diff] [blame] | 1919 | bool FileCheck::CheckInput(SourceMgr &SM, StringRef Buffer, |
| 1920 | ArrayRef<FileCheckString> CheckStrings, |
| 1921 | std::vector<FileCheckDiag> *Diags) { |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1922 | bool ChecksFailed = false; |
| 1923 | |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1924 | unsigned i = 0, j = 0, e = CheckStrings.size(); |
| 1925 | while (true) { |
| 1926 | StringRef CheckRegion; |
| 1927 | if (j == e) { |
| 1928 | CheckRegion = Buffer; |
| 1929 | } else { |
| 1930 | const FileCheckString &CheckLabelStr = CheckStrings[j]; |
| 1931 | if (CheckLabelStr.Pat.getCheckTy() != Check::CheckLabel) { |
| 1932 | ++j; |
| 1933 | continue; |
| 1934 | } |
| 1935 | |
| 1936 | // Scan to next CHECK-LABEL match, ignoring CHECK-NOT and CHECK-DAG |
| 1937 | size_t MatchLabelLen = 0; |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1938 | size_t MatchLabelPos = |
| 1939 | CheckLabelStr.Check(SM, Buffer, true, MatchLabelLen, Req, Diags); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1940 | if (MatchLabelPos == StringRef::npos) |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1941 | // Immediately bail if CHECK-LABEL fails, nothing else we can do. |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1942 | return false; |
| 1943 | |
| 1944 | CheckRegion = Buffer.substr(0, MatchLabelPos + MatchLabelLen); |
| 1945 | Buffer = Buffer.substr(MatchLabelPos + MatchLabelLen); |
| 1946 | ++j; |
| 1947 | } |
| 1948 | |
Thomas Preud'homme | 7b4ecdd | 2019-05-14 11:58:30 +0000 | [diff] [blame] | 1949 | // Do not clear the first region as it's the one before the first |
| 1950 | // CHECK-LABEL and it would clear variables defined on the command-line |
| 1951 | // before they get used. |
| 1952 | if (i != 0 && Req.EnableVarScope) |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1953 | PatternContext.clearLocalVars(); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1954 | |
| 1955 | for (; i != j; ++i) { |
| 1956 | const FileCheckString &CheckStr = CheckStrings[i]; |
| 1957 | |
| 1958 | // Check each string within the scanned region, including a second check |
| 1959 | // of any final CHECK-LABEL (to verify CHECK-NOT and CHECK-DAG) |
| 1960 | size_t MatchLen = 0; |
Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1961 | size_t MatchPos = |
| 1962 | CheckStr.Check(SM, CheckRegion, false, MatchLen, Req, Diags); |
Aditya Nandakumar | ffa9d2e | 2018-08-07 21:58:49 +0000 | [diff] [blame] | 1963 | |
| 1964 | if (MatchPos == StringRef::npos) { |
| 1965 | ChecksFailed = true; |
| 1966 | i = j; |
| 1967 | break; |
| 1968 | } |
| 1969 | |
| 1970 | CheckRegion = CheckRegion.substr(MatchPos + MatchLen); |
| 1971 | } |
| 1972 | |
| 1973 | if (j == e) |
| 1974 | break; |
| 1975 | } |
| 1976 | |
| 1977 | // Success if no checks failed. |
| 1978 | return !ChecksFailed; |
| 1979 | } |