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