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