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