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