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