1 //===- FileCheck.cpp - Check that File's Contents match what is expected --===// 2 // 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 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 program exits with an exit status of 2 on error, exit status of 0 if 13 // the file matched the expected contents, and exit status of 1 if it did not 14 // contain the expected contents. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm/Support/CommandLine.h" 19 #include "llvm/Support/InitLLVM.h" 20 #include "llvm/Support/Process.h" 21 #include "llvm/Support/WithColor.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include "llvm/Support/FileCheck.h" 24 #include <cmath> 25 using namespace llvm; 26 27 static cl::extrahelp FileCheckOptsEnv( 28 "\nOptions are parsed from the environment variable FILECHECK_OPTS and\n" 29 "from the command line.\n"); 30 31 static cl::opt<std::string> 32 CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Optional); 33 34 static cl::opt<std::string> 35 InputFilename("input-file", cl::desc("File to check (defaults to stdin)"), 36 cl::init("-"), cl::value_desc("filename")); 37 38 static cl::list<std::string> CheckPrefixes( 39 "check-prefix", 40 cl::desc("Prefix to use from check file (defaults to 'CHECK')")); 41 static cl::alias CheckPrefixesAlias( 42 "check-prefixes", cl::aliasopt(CheckPrefixes), cl::CommaSeparated, 43 cl::NotHidden, 44 cl::desc( 45 "Alias for -check-prefix permitting multiple comma separated values")); 46 47 static cl::list<std::string> CommentPrefixes( 48 "comment-prefixes", cl::CommaSeparated, cl::Hidden, 49 cl::desc("Comma-separated list of comment prefixes to use from check file\n" 50 "(defaults to 'COM,RUN'). Please avoid using this feature in\n" 51 "LLVM's LIT-based test suites, which should be easier to\n" 52 "maintain if they all follow a consistent comment style. This\n" 53 "feature is meant for non-LIT test suites using FileCheck.")); 54 55 static cl::opt<bool> NoCanonicalizeWhiteSpace( 56 "strict-whitespace", 57 cl::desc("Do not treat all horizontal whitespace as equivalent")); 58 59 static cl::opt<bool> IgnoreCase( 60 "ignore-case", 61 cl::desc("Use case-insensitive matching")); 62 63 static cl::list<std::string> ImplicitCheckNot( 64 "implicit-check-not", 65 cl::desc("Add an implicit negative check with this pattern to every\n" 66 "positive check. This can be used to ensure that no instances of\n" 67 "this pattern occur which are not matched by a positive pattern"), 68 cl::value_desc("pattern")); 69 70 static cl::list<std::string> 71 GlobalDefines("D", cl::AlwaysPrefix, 72 cl::desc("Define a variable to be used in capture patterns."), 73 cl::value_desc("VAR=VALUE")); 74 75 static cl::opt<bool> AllowEmptyInput( 76 "allow-empty", cl::init(false), 77 cl::desc("Allow the input file to be empty. This is useful when making\n" 78 "checks that some error message does not occur, for example.")); 79 80 static cl::opt<bool> MatchFullLines( 81 "match-full-lines", cl::init(false), 82 cl::desc("Require all positive matches to cover an entire input line.\n" 83 "Allows leading and trailing whitespace if --strict-whitespace\n" 84 "is not also passed.")); 85 86 static cl::opt<bool> EnableVarScope( 87 "enable-var-scope", cl::init(false), 88 cl::desc("Enables scope for regex variables. Variables with names that\n" 89 "do not start with '$' will be reset at the beginning of\n" 90 "each CHECK-LABEL block.")); 91 92 static cl::opt<bool> AllowDeprecatedDagOverlap( 93 "allow-deprecated-dag-overlap", cl::init(false), 94 cl::desc("Enable overlapping among matches in a group of consecutive\n" 95 "CHECK-DAG directives. This option is deprecated and is only\n" 96 "provided for convenience as old tests are migrated to the new\n" 97 "non-overlapping CHECK-DAG implementation.\n")); 98 99 static cl::opt<bool> Verbose( 100 "v", cl::init(false), cl::ZeroOrMore, 101 cl::desc("Print directive pattern matches, or add them to the input dump\n" 102 "if enabled.\n")); 103 104 static cl::opt<bool> VerboseVerbose( 105 "vv", cl::init(false), cl::ZeroOrMore, 106 cl::desc("Print information helpful in diagnosing internal FileCheck\n" 107 "issues, or add it to the input dump if enabled. Implies\n" 108 "-v.\n")); 109 110 // The order of DumpInputValue members affects their precedence, as documented 111 // for -dump-input below. 112 enum DumpInputValue { 113 DumpInputNever, 114 DumpInputFail, 115 DumpInputAlways, 116 DumpInputHelp 117 }; 118 119 static cl::list<DumpInputValue> DumpInputs( 120 "dump-input", 121 cl::desc("Dump input to stderr, adding annotations representing\n" 122 "currently enabled diagnostics. When there are multiple\n" 123 "occurrences of this option, the <value> that appears earliest\n" 124 "in the list below has precedence. The default is 'fail'.\n"), 125 cl::value_desc("mode"), 126 cl::values(clEnumValN(DumpInputHelp, "help", "Explain input dump and quit"), 127 clEnumValN(DumpInputAlways, "always", "Always dump input"), 128 clEnumValN(DumpInputFail, "fail", "Dump input on failure"), 129 clEnumValN(DumpInputNever, "never", "Never dump input"))); 130 131 // The order of DumpInputFilterValue members affects their precedence, as 132 // documented for -dump-input-filter below. 133 enum DumpInputFilterValue { 134 DumpInputFilterError, 135 DumpInputFilterAnnotation, 136 DumpInputFilterAnnotationFull, 137 DumpInputFilterAll 138 }; 139 140 static cl::list<DumpInputFilterValue> DumpInputFilters( 141 "dump-input-filter", 142 cl::desc("In the dump requested by -dump-input, print only input lines of\n" 143 "kind <value> plus any context specified by -dump-input-context.\n" 144 "When there are multiple occurrences of this option, the <value>\n" 145 "that appears earliest in the list below has precedence. The\n" 146 "default is 'error' when -dump-input=fail, and it's 'all' when\n" 147 "-dump-input=always.\n"), 148 cl::values(clEnumValN(DumpInputFilterAll, "all", "All input lines"), 149 clEnumValN(DumpInputFilterAnnotationFull, "annotation-full", 150 "Input lines with annotations"), 151 clEnumValN(DumpInputFilterAnnotation, "annotation", 152 "Input lines with starting points of annotations"), 153 clEnumValN(DumpInputFilterError, "error", 154 "Input lines with starting points of error " 155 "annotations"))); 156 157 static cl::list<unsigned> DumpInputContexts( 158 "dump-input-context", cl::value_desc("N"), 159 cl::desc("In the dump requested by -dump-input, print <N> input lines\n" 160 "before and <N> input lines after any lines specified by\n" 161 "-dump-input-filter. When there are multiple occurrences of\n" 162 "this option, the largest specified <N> has precedence. The\n" 163 "default is 5.\n")); 164 165 typedef cl::list<std::string>::const_iterator prefix_iterator; 166 167 168 169 170 171 172 173 static void DumpCommandLine(int argc, char **argv) { 174 errs() << "FileCheck command line: "; 175 for (int I = 0; I < argc; I++) 176 errs() << " " << argv[I]; 177 errs() << "\n"; 178 } 179 180 struct MarkerStyle { 181 /// The starting char (before tildes) for marking the line. 182 char Lead; 183 /// What color to use for this annotation. 184 raw_ostream::Colors Color; 185 /// A note to follow the marker, or empty string if none. 186 std::string Note; 187 /// Does this marker indicate inclusion by -dump-input-filter=error? 188 bool FiltersAsError; 189 MarkerStyle() {} 190 MarkerStyle(char Lead, raw_ostream::Colors Color, 191 const std::string &Note = "", bool FiltersAsError = false) 192 : Lead(Lead), Color(Color), Note(Note), FiltersAsError(FiltersAsError) { 193 assert((!FiltersAsError || !Note.empty()) && 194 "expected error diagnostic to have note"); 195 } 196 }; 197 198 static MarkerStyle GetMarker(FileCheckDiag::MatchType MatchTy) { 199 switch (MatchTy) { 200 case FileCheckDiag::MatchFoundAndExpected: 201 return MarkerStyle('^', raw_ostream::GREEN); 202 case FileCheckDiag::MatchFoundButExcluded: 203 return MarkerStyle('!', raw_ostream::RED, "error: no match expected", 204 /*FiltersAsError=*/true); 205 case FileCheckDiag::MatchFoundButWrongLine: 206 return MarkerStyle('!', raw_ostream::RED, "error: match on wrong line", 207 /*FiltersAsError=*/true); 208 case FileCheckDiag::MatchFoundButDiscarded: 209 return MarkerStyle('!', raw_ostream::CYAN, 210 "discard: overlaps earlier match"); 211 case FileCheckDiag::MatchNoneAndExcluded: 212 return MarkerStyle('X', raw_ostream::GREEN); 213 case FileCheckDiag::MatchNoneButExpected: 214 return MarkerStyle('X', raw_ostream::RED, "error: no match found", 215 /*FiltersAsError=*/true); 216 case FileCheckDiag::MatchFuzzy: 217 return MarkerStyle('?', raw_ostream::MAGENTA, "possible intended match", 218 /*FiltersAsError=*/true); 219 } 220 llvm_unreachable_internal("unexpected match type"); 221 } 222 223 static void DumpInputAnnotationHelp(raw_ostream &OS) { 224 OS << "The following description was requested by -dump-input=help to\n" 225 << "explain the input dump printed by FileCheck.\n" 226 << "\n" 227 << "Related command-line options:\n" 228 << "\n" 229 << " - -dump-input=<value> enables or disables the input dump\n" 230 << " - -dump-input-filter=<value> filters the input lines\n" 231 << " - -dump-input-context=<N> adjusts the context of filtered lines\n" 232 << " - -v and -vv add more annotations\n" 233 << " - -color forces colors to be enabled both in the dump and below\n" 234 << " - -help documents the above options in more detail\n" 235 << "\n" 236 << "These options can also be set via FILECHECK_OPTS. For example, for\n" 237 << "maximum debugging output on failures:\n" 238 << "\n" 239 << " $ FILECHECK_OPTS='-dump-input-filter=all -vv -color' ninja check\n" 240 << "\n" 241 << "Input dump annotation format:\n" 242 << "\n"; 243 244 // Labels for input lines. 245 OS << " - "; 246 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "L:"; 247 OS << " labels line number L of the input file\n"; 248 249 // Labels for annotation lines. 250 OS << " - "; 251 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L"; 252 OS << " labels the only match result for either (1) a pattern of type T" 253 << " from\n" 254 << " line L of the check file if L is an integer or (2) the" 255 << " I-th implicit\n" 256 << " pattern if L is \"imp\" followed by an integer " 257 << "I (index origin one)\n"; 258 OS << " - "; 259 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L'N"; 260 OS << " labels the Nth match result for such a pattern\n"; 261 262 // Markers on annotation lines. 263 OS << " - "; 264 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "^~~"; 265 OS << " marks good match (reported if -v)\n" 266 << " - "; 267 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "!~~"; 268 OS << " marks bad match, such as:\n" 269 << " - CHECK-NEXT on same line as previous match (error)\n" 270 << " - CHECK-NOT found (error)\n" 271 << " - CHECK-DAG overlapping match (discarded, reported if " 272 << "-vv)\n" 273 << " - "; 274 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "X~~"; 275 OS << " marks search range when no match is found, such as:\n" 276 << " - CHECK-NEXT not found (error)\n" 277 << " - CHECK-NOT not found (success, reported if -vv)\n" 278 << " - CHECK-DAG not found after discarded matches (error)\n" 279 << " - "; 280 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "?"; 281 OS << " marks fuzzy match when no match is found\n"; 282 283 // Elided lines. 284 OS << " - "; 285 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "..."; 286 OS << " indicates elided input lines and annotations, as specified by\n" 287 << " -dump-input-filter and -dump-input-context\n"; 288 289 // Colors. 290 OS << " - colors "; 291 WithColor(OS, raw_ostream::GREEN, true) << "success"; 292 OS << ", "; 293 WithColor(OS, raw_ostream::RED, true) << "error"; 294 OS << ", "; 295 WithColor(OS, raw_ostream::MAGENTA, true) << "fuzzy match"; 296 OS << ", "; 297 WithColor(OS, raw_ostream::CYAN, true, false) << "discarded match"; 298 OS << ", "; 299 WithColor(OS, raw_ostream::CYAN, true, true) << "unmatched input"; 300 OS << "\n"; 301 } 302 303 /// An annotation for a single input line. 304 struct InputAnnotation { 305 /// The index of the match result across all checks 306 unsigned DiagIndex; 307 /// The label for this annotation. 308 std::string Label; 309 /// Is this the initial fragment of a diagnostic that has been broken across 310 /// multiple lines? 311 bool IsFirstLine; 312 /// What input line (one-origin indexing) this annotation marks. This might 313 /// be different from the starting line of the original diagnostic if 314 /// !IsFirstLine. 315 unsigned InputLine; 316 /// The column range (one-origin indexing, open end) in which to mark the 317 /// input line. If InputEndCol is UINT_MAX, treat it as the last column 318 /// before the newline. 319 unsigned InputStartCol, InputEndCol; 320 /// The marker to use. 321 MarkerStyle Marker; 322 /// Whether this annotation represents a good match for an expected pattern. 323 bool FoundAndExpectedMatch; 324 }; 325 326 /// Get an abbreviation for the check type. 327 std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) { 328 switch (Ty) { 329 case Check::CheckPlain: 330 if (Ty.getCount() > 1) 331 return "count"; 332 return "check"; 333 case Check::CheckNext: 334 return "next"; 335 case Check::CheckSame: 336 return "same"; 337 case Check::CheckNot: 338 return "not"; 339 case Check::CheckDAG: 340 return "dag"; 341 case Check::CheckLabel: 342 return "label"; 343 case Check::CheckEmpty: 344 return "empty"; 345 case Check::CheckComment: 346 return "com"; 347 case Check::CheckEOF: 348 return "eof"; 349 case Check::CheckBadNot: 350 return "bad-not"; 351 case Check::CheckBadCount: 352 return "bad-count"; 353 case Check::CheckNone: 354 llvm_unreachable("invalid FileCheckType"); 355 } 356 llvm_unreachable("unknown FileCheckType"); 357 } 358 359 static void 360 BuildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID, 361 const std::pair<unsigned, unsigned> &ImpPatBufferIDRange, 362 const std::vector<FileCheckDiag> &Diags, 363 std::vector<InputAnnotation> &Annotations, 364 unsigned &LabelWidth) { 365 // How many diagnostics have we seen so far? 366 unsigned DiagCount = 0; 367 // How many diagnostics has the current check seen so far? 368 unsigned CheckDiagCount = 0; 369 // What's the widest label? 370 LabelWidth = 0; 371 for (auto DiagItr = Diags.begin(), DiagEnd = Diags.end(); DiagItr != DiagEnd; 372 ++DiagItr) { 373 InputAnnotation A; 374 A.DiagIndex = DiagCount++; 375 376 // Build label, which uniquely identifies this check result. 377 unsigned CheckBufferID = SM.FindBufferContainingLoc(DiagItr->CheckLoc); 378 auto CheckLineAndCol = 379 SM.getLineAndColumn(DiagItr->CheckLoc, CheckBufferID); 380 llvm::raw_string_ostream Label(A.Label); 381 Label << GetCheckTypeAbbreviation(DiagItr->CheckTy) << ":"; 382 if (CheckBufferID == CheckFileBufferID) 383 Label << CheckLineAndCol.first; 384 else if (ImpPatBufferIDRange.first <= CheckBufferID && 385 CheckBufferID < ImpPatBufferIDRange.second) 386 Label << "imp" << (CheckBufferID - ImpPatBufferIDRange.first + 1); 387 else 388 llvm_unreachable("expected diagnostic's check location to be either in " 389 "the check file or for an implicit pattern"); 390 unsigned CheckDiagIndex = UINT_MAX; 391 auto DiagNext = std::next(DiagItr); 392 if (DiagNext != DiagEnd && DiagItr->CheckTy == DiagNext->CheckTy && 393 DiagItr->CheckLoc == DiagNext->CheckLoc) 394 CheckDiagIndex = CheckDiagCount++; 395 else if (CheckDiagCount) { 396 CheckDiagIndex = CheckDiagCount; 397 CheckDiagCount = 0; 398 } 399 if (CheckDiagIndex != UINT_MAX) 400 Label << "'" << CheckDiagIndex; 401 Label.flush(); 402 LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size()); 403 404 A.Marker = GetMarker(DiagItr->MatchTy); 405 if (!DiagItr->Note.empty()) { 406 A.Marker.Note = DiagItr->Note; 407 // It's less confusing if notes that don't actually have ranges don't have 408 // markers. For example, a marker for 'with "VAR" equal to "5"' would 409 // seem to indicate where "VAR" matches, but the location we actually have 410 // for the marker simply points to the start of the match/search range for 411 // the full pattern of which the substitution is potentially just one 412 // component. 413 if (DiagItr->InputStartLine == DiagItr->InputEndLine && 414 DiagItr->InputStartCol == DiagItr->InputEndCol) 415 A.Marker.Lead = ' '; 416 } 417 A.FoundAndExpectedMatch = 418 DiagItr->MatchTy == FileCheckDiag::MatchFoundAndExpected; 419 420 // Compute the mark location, and break annotation into multiple 421 // annotations if it spans multiple lines. 422 A.IsFirstLine = true; 423 A.InputLine = DiagItr->InputStartLine; 424 A.InputStartCol = DiagItr->InputStartCol; 425 if (DiagItr->InputStartLine == DiagItr->InputEndLine) { 426 // Sometimes ranges are empty in order to indicate a specific point, but 427 // that would mean nothing would be marked, so adjust the range to 428 // include the following character. 429 A.InputEndCol = 430 std::max(DiagItr->InputStartCol + 1, DiagItr->InputEndCol); 431 Annotations.push_back(A); 432 } else { 433 assert(DiagItr->InputStartLine < DiagItr->InputEndLine && 434 "expected input range not to be inverted"); 435 A.InputEndCol = UINT_MAX; 436 Annotations.push_back(A); 437 for (unsigned L = DiagItr->InputStartLine + 1, E = DiagItr->InputEndLine; 438 L <= E; ++L) { 439 // If a range ends before the first column on a line, then it has no 440 // characters on that line, so there's nothing to render. 441 if (DiagItr->InputEndCol == 1 && L == E) 442 break; 443 InputAnnotation B; 444 B.DiagIndex = A.DiagIndex; 445 B.Label = A.Label; 446 B.IsFirstLine = false; 447 B.InputLine = L; 448 B.Marker = A.Marker; 449 B.Marker.Lead = '~'; 450 B.Marker.Note = ""; 451 B.InputStartCol = 1; 452 if (L != E) 453 B.InputEndCol = UINT_MAX; 454 else 455 B.InputEndCol = DiagItr->InputEndCol; 456 B.FoundAndExpectedMatch = A.FoundAndExpectedMatch; 457 Annotations.push_back(B); 458 } 459 } 460 } 461 } 462 463 static unsigned FindInputLineInFilter( 464 DumpInputFilterValue DumpInputFilter, unsigned CurInputLine, 465 const std::vector<InputAnnotation>::iterator &AnnotationBeg, 466 const std::vector<InputAnnotation>::iterator &AnnotationEnd) { 467 if (DumpInputFilter == DumpInputFilterAll) 468 return CurInputLine; 469 for (auto AnnotationItr = AnnotationBeg; AnnotationItr != AnnotationEnd; 470 ++AnnotationItr) { 471 switch (DumpInputFilter) { 472 case DumpInputFilterAll: 473 llvm_unreachable("unexpected DumpInputFilterAll"); 474 break; 475 case DumpInputFilterAnnotationFull: 476 return AnnotationItr->InputLine; 477 case DumpInputFilterAnnotation: 478 if (AnnotationItr->IsFirstLine) 479 return AnnotationItr->InputLine; 480 break; 481 case DumpInputFilterError: 482 if (AnnotationItr->IsFirstLine && AnnotationItr->Marker.FiltersAsError) 483 return AnnotationItr->InputLine; 484 break; 485 } 486 } 487 return UINT_MAX; 488 } 489 490 /// To OS, print a vertical ellipsis (right-justified at LabelWidth) if it would 491 /// occupy less lines than ElidedLines, but print ElidedLines otherwise. Either 492 /// way, clear ElidedLines. Thus, if ElidedLines is empty, do nothing. 493 static void DumpEllipsisOrElidedLines(raw_ostream &OS, std::string &ElidedLines, 494 unsigned LabelWidth) { 495 if (ElidedLines.empty()) 496 return; 497 unsigned EllipsisLines = 3; 498 if (EllipsisLines < StringRef(ElidedLines).count('\n')) { 499 for (unsigned i = 0; i < EllipsisLines; ++i) { 500 WithColor(OS, raw_ostream::BLACK, /*Bold=*/true) 501 << right_justify(".", LabelWidth); 502 OS << '\n'; 503 } 504 } else 505 OS << ElidedLines; 506 ElidedLines.clear(); 507 } 508 509 static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req, 510 DumpInputFilterValue DumpInputFilter, 511 unsigned DumpInputContext, 512 StringRef InputFileText, 513 std::vector<InputAnnotation> &Annotations, 514 unsigned LabelWidth) { 515 OS << "Input was:\n<<<<<<\n"; 516 517 // Sort annotations. 518 std::sort(Annotations.begin(), Annotations.end(), 519 [](const InputAnnotation &A, const InputAnnotation &B) { 520 // 1. Sort annotations in the order of the input lines. 521 // 522 // This makes it easier to find relevant annotations while 523 // iterating input lines in the implementation below. FileCheck 524 // does not always produce diagnostics in the order of input 525 // lines due to, for example, CHECK-DAG and CHECK-NOT. 526 if (A.InputLine != B.InputLine) 527 return A.InputLine < B.InputLine; 528 // 2. Sort annotations in the temporal order FileCheck produced 529 // their associated diagnostics. 530 // 531 // This sort offers several benefits: 532 // 533 // A. On a single input line, the order of annotations reflects 534 // the FileCheck logic for processing directives/patterns. 535 // This can be helpful in understanding cases in which the 536 // order of the associated directives/patterns in the check 537 // file or on the command line either (i) does not match the 538 // temporal order in which FileCheck looks for matches for the 539 // directives/patterns (due to, for example, CHECK-LABEL, 540 // CHECK-NOT, or `--implicit-check-not`) or (ii) does match 541 // that order but does not match the order of those 542 // diagnostics along an input line (due to, for example, 543 // CHECK-DAG). 544 // 545 // On the other hand, because our presentation format presents 546 // input lines in order, there's no clear way to offer the 547 // same benefit across input lines. For consistency, it might 548 // then seem worthwhile to have annotations on a single line 549 // also sorted in input order (that is, by input column). 550 // However, in practice, this appears to be more confusing 551 // than helpful. Perhaps it's intuitive to expect annotations 552 // to be listed in the temporal order in which they were 553 // produced except in cases the presentation format obviously 554 // and inherently cannot support it (that is, across input 555 // lines). 556 // 557 // B. When diagnostics' annotations are split among multiple 558 // input lines, the user must track them from one input line 559 // to the next. One property of the sort chosen here is that 560 // it facilitates the user in this regard by ensuring the 561 // following: when comparing any two input lines, a 562 // diagnostic's annotations are sorted in the same position 563 // relative to all other diagnostics' annotations. 564 return A.DiagIndex < B.DiagIndex; 565 }); 566 567 // Compute the width of the label column. 568 const unsigned char *InputFilePtr = InputFileText.bytes_begin(), 569 *InputFileEnd = InputFileText.bytes_end(); 570 unsigned LineCount = InputFileText.count('\n'); 571 if (InputFileEnd[-1] != '\n') 572 ++LineCount; 573 unsigned LineNoWidth = std::log10(LineCount) + 1; 574 // +3 below adds spaces (1) to the left of the (right-aligned) line numbers 575 // on input lines and (2) to the right of the (left-aligned) labels on 576 // annotation lines so that input lines and annotation lines are more 577 // visually distinct. For example, the spaces on the annotation lines ensure 578 // that input line numbers and check directive line numbers never align 579 // horizontally. Those line numbers might not even be for the same file. 580 // One space would be enough to achieve that, but more makes it even easier 581 // to see. 582 LabelWidth = std::max(LabelWidth, LineNoWidth) + 3; 583 584 // Print annotated input lines. 585 unsigned PrevLineInFilter = 0; // 0 means none so far 586 unsigned NextLineInFilter = 0; // 0 means uncomputed, UINT_MAX means none 587 std::string ElidedLines; 588 raw_string_ostream ElidedLinesOS(ElidedLines); 589 ColorMode TheColorMode = 590 WithColor(OS).colorsEnabled() ? ColorMode::Enable : ColorMode::Disable; 591 if (TheColorMode == ColorMode::Enable) 592 ElidedLinesOS.enable_colors(true); 593 auto AnnotationItr = Annotations.begin(), AnnotationEnd = Annotations.end(); 594 for (unsigned Line = 1; 595 InputFilePtr != InputFileEnd || AnnotationItr != AnnotationEnd; 596 ++Line) { 597 const unsigned char *InputFileLine = InputFilePtr; 598 599 // Compute the previous and next line included by the filter. 600 if (NextLineInFilter < Line) 601 NextLineInFilter = FindInputLineInFilter(DumpInputFilter, Line, 602 AnnotationItr, AnnotationEnd); 603 assert(NextLineInFilter && "expected NextLineInFilter to be computed"); 604 if (NextLineInFilter == Line) 605 PrevLineInFilter = Line; 606 607 // Elide this input line and its annotations if it's not within the 608 // context specified by -dump-input-context of an input line included by 609 // -dump-input-filter. However, in case the resulting ellipsis would occupy 610 // more lines than the input lines and annotations it elides, buffer the 611 // elided lines and annotations so we can print them instead. 612 raw_ostream *LineOS = &OS; 613 if ((!PrevLineInFilter || PrevLineInFilter + DumpInputContext < Line) && 614 (NextLineInFilter == UINT_MAX || 615 Line + DumpInputContext < NextLineInFilter)) 616 LineOS = &ElidedLinesOS; 617 else { 618 LineOS = &OS; 619 DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth); 620 } 621 622 // Print right-aligned line number. 623 WithColor(*LineOS, raw_ostream::BLACK, /*Bold=*/true, /*BF=*/false, 624 TheColorMode) 625 << format_decimal(Line, LabelWidth) << ": "; 626 627 // For the case where -v and colors are enabled, find the annotations for 628 // good matches for expected patterns in order to highlight everything 629 // else in the line. There are no such annotations if -v is disabled. 630 std::vector<InputAnnotation> FoundAndExpectedMatches; 631 if (Req.Verbose && TheColorMode == ColorMode::Enable) { 632 for (auto I = AnnotationItr; I != AnnotationEnd && I->InputLine == Line; 633 ++I) { 634 if (I->FoundAndExpectedMatch) 635 FoundAndExpectedMatches.push_back(*I); 636 } 637 } 638 639 // Print numbered line with highlighting where there are no matches for 640 // expected patterns. 641 bool Newline = false; 642 { 643 WithColor COS(*LineOS, raw_ostream::SAVEDCOLOR, /*Bold=*/false, 644 /*BG=*/false, TheColorMode); 645 bool InMatch = false; 646 if (Req.Verbose) 647 COS.changeColor(raw_ostream::CYAN, true, true); 648 for (unsigned Col = 1; InputFilePtr != InputFileEnd && !Newline; ++Col) { 649 bool WasInMatch = InMatch; 650 InMatch = false; 651 for (auto M : FoundAndExpectedMatches) { 652 if (M.InputStartCol <= Col && Col < M.InputEndCol) { 653 InMatch = true; 654 break; 655 } 656 } 657 if (!WasInMatch && InMatch) 658 COS.resetColor(); 659 else if (WasInMatch && !InMatch) 660 COS.changeColor(raw_ostream::CYAN, true, true); 661 if (*InputFilePtr == '\n') 662 Newline = true; 663 else 664 COS << *InputFilePtr; 665 ++InputFilePtr; 666 } 667 } 668 *LineOS << '\n'; 669 unsigned InputLineWidth = InputFilePtr - InputFileLine - Newline; 670 671 // Print any annotations. 672 while (AnnotationItr != AnnotationEnd && 673 AnnotationItr->InputLine == Line) { 674 WithColor COS(*LineOS, AnnotationItr->Marker.Color, /*Bold=*/true, 675 /*BG=*/false, TheColorMode); 676 // The two spaces below are where the ": " appears on input lines. 677 COS << left_justify(AnnotationItr->Label, LabelWidth) << " "; 678 unsigned Col; 679 for (Col = 1; Col < AnnotationItr->InputStartCol; ++Col) 680 COS << ' '; 681 COS << AnnotationItr->Marker.Lead; 682 // If InputEndCol=UINT_MAX, stop at InputLineWidth. 683 for (++Col; Col < AnnotationItr->InputEndCol && Col <= InputLineWidth; 684 ++Col) 685 COS << '~'; 686 const std::string &Note = AnnotationItr->Marker.Note; 687 if (!Note.empty()) { 688 // Put the note at the end of the input line. If we were to instead 689 // put the note right after the marker, subsequent annotations for the 690 // same input line might appear to mark this note instead of the input 691 // line. 692 for (; Col <= InputLineWidth; ++Col) 693 COS << ' '; 694 COS << ' ' << Note; 695 } 696 COS << '\n'; 697 ++AnnotationItr; 698 } 699 } 700 DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth); 701 702 OS << ">>>>>>\n"; 703 } 704 705 int main(int argc, char **argv) { 706 // Enable use of ANSI color codes because FileCheck is using them to 707 // highlight text. 708 llvm::sys::Process::UseANSIEscapeCodes(true); 709 710 InitLLVM X(argc, argv); 711 cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr, 712 "FILECHECK_OPTS"); 713 714 // Select -dump-input* values. The -help documentation specifies the default 715 // value and which value to choose if an option is specified multiple times. 716 // In the latter case, the general rule of thumb is to choose the value that 717 // provides the most information. 718 DumpInputValue DumpInput = 719 DumpInputs.empty() 720 ? DumpInputFail 721 : *std::max_element(DumpInputs.begin(), DumpInputs.end()); 722 DumpInputFilterValue DumpInputFilter; 723 if (DumpInputFilters.empty()) 724 DumpInputFilter = DumpInput == DumpInputAlways ? DumpInputFilterAll 725 : DumpInputFilterError; 726 else 727 DumpInputFilter = 728 *std::max_element(DumpInputFilters.begin(), DumpInputFilters.end()); 729 unsigned DumpInputContext = DumpInputContexts.empty() 730 ? 5 731 : *std::max_element(DumpInputContexts.begin(), 732 DumpInputContexts.end()); 733 734 if (DumpInput == DumpInputHelp) { 735 DumpInputAnnotationHelp(outs()); 736 return 0; 737 } 738 if (CheckFilename.empty()) { 739 errs() << "<check-file> not specified\n"; 740 return 2; 741 } 742 743 FileCheckRequest Req; 744 for (StringRef Prefix : CheckPrefixes) 745 Req.CheckPrefixes.push_back(Prefix); 746 747 for (StringRef Prefix : CommentPrefixes) 748 Req.CommentPrefixes.push_back(Prefix); 749 750 for (StringRef CheckNot : ImplicitCheckNot) 751 Req.ImplicitCheckNot.push_back(CheckNot); 752 753 bool GlobalDefineError = false; 754 for (StringRef G : GlobalDefines) { 755 size_t EqIdx = G.find('='); 756 if (EqIdx == std::string::npos) { 757 errs() << "Missing equal sign in command-line definition '-D" << G 758 << "'\n"; 759 GlobalDefineError = true; 760 continue; 761 } 762 if (EqIdx == 0) { 763 errs() << "Missing variable name in command-line definition '-D" << G 764 << "'\n"; 765 GlobalDefineError = true; 766 continue; 767 } 768 Req.GlobalDefines.push_back(G); 769 } 770 if (GlobalDefineError) 771 return 2; 772 773 Req.AllowEmptyInput = AllowEmptyInput; 774 Req.EnableVarScope = EnableVarScope; 775 Req.AllowDeprecatedDagOverlap = AllowDeprecatedDagOverlap; 776 Req.Verbose = Verbose; 777 Req.VerboseVerbose = VerboseVerbose; 778 Req.NoCanonicalizeWhiteSpace = NoCanonicalizeWhiteSpace; 779 Req.MatchFullLines = MatchFullLines; 780 Req.IgnoreCase = IgnoreCase; 781 782 if (VerboseVerbose) 783 Req.Verbose = true; 784 785 FileCheck FC(Req); 786 if (!FC.ValidateCheckPrefixes()) 787 return 2; 788 789 Regex PrefixRE = FC.buildCheckPrefixRegex(); 790 std::string REError; 791 if (!PrefixRE.isValid(REError)) { 792 errs() << "Unable to combine check-prefix strings into a prefix regular " 793 "expression! This is likely a bug in FileCheck's verification of " 794 "the check-prefix strings. Regular expression parsing failed " 795 "with the following error: " 796 << REError << "\n"; 797 return 2; 798 } 799 800 SourceMgr SM; 801 802 // Read the expected strings from the check file. 803 ErrorOr<std::unique_ptr<MemoryBuffer>> CheckFileOrErr = 804 MemoryBuffer::getFileOrSTDIN(CheckFilename); 805 if (std::error_code EC = CheckFileOrErr.getError()) { 806 errs() << "Could not open check file '" << CheckFilename 807 << "': " << EC.message() << '\n'; 808 return 2; 809 } 810 MemoryBuffer &CheckFile = *CheckFileOrErr.get(); 811 812 SmallString<4096> CheckFileBuffer; 813 StringRef CheckFileText = FC.CanonicalizeFile(CheckFile, CheckFileBuffer); 814 815 unsigned CheckFileBufferID = 816 SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 817 CheckFileText, CheckFile.getBufferIdentifier()), 818 SMLoc()); 819 820 std::pair<unsigned, unsigned> ImpPatBufferIDRange; 821 if (FC.readCheckFile(SM, CheckFileText, PrefixRE, &ImpPatBufferIDRange)) 822 return 2; 823 824 // Open the file to check and add it to SourceMgr. 825 ErrorOr<std::unique_ptr<MemoryBuffer>> InputFileOrErr = 826 MemoryBuffer::getFileOrSTDIN(InputFilename); 827 if (InputFilename == "-") 828 InputFilename = "<stdin>"; // Overwrite for improved diagnostic messages 829 if (std::error_code EC = InputFileOrErr.getError()) { 830 errs() << "Could not open input file '" << InputFilename 831 << "': " << EC.message() << '\n'; 832 return 2; 833 } 834 MemoryBuffer &InputFile = *InputFileOrErr.get(); 835 836 if (InputFile.getBufferSize() == 0 && !AllowEmptyInput) { 837 errs() << "FileCheck error: '" << InputFilename << "' is empty.\n"; 838 DumpCommandLine(argc, argv); 839 return 2; 840 } 841 842 SmallString<4096> InputFileBuffer; 843 StringRef InputFileText = FC.CanonicalizeFile(InputFile, InputFileBuffer); 844 845 SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 846 InputFileText, InputFile.getBufferIdentifier()), 847 SMLoc()); 848 849 std::vector<FileCheckDiag> Diags; 850 int ExitCode = FC.checkInput(SM, InputFileText, 851 DumpInput == DumpInputNever ? nullptr : &Diags) 852 ? EXIT_SUCCESS 853 : 1; 854 if (DumpInput == DumpInputAlways || 855 (ExitCode == 1 && DumpInput == DumpInputFail)) { 856 errs() << "\n" 857 << "Input file: " << InputFilename << "\n" 858 << "Check file: " << CheckFilename << "\n" 859 << "\n" 860 << "-dump-input=help explains the following input dump.\n" 861 << "\n"; 862 std::vector<InputAnnotation> Annotations; 863 unsigned LabelWidth; 864 BuildInputAnnotations(SM, CheckFileBufferID, ImpPatBufferIDRange, Diags, 865 Annotations, LabelWidth); 866 DumpAnnotatedInput(errs(), Req, DumpInputFilter, DumpInputContext, 867 InputFileText, Annotations, LabelWidth); 868 } 869 870 return ExitCode; 871 } 872