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 A.FoundAndExpectedMatch = 406 DiagItr->MatchTy == FileCheckDiag::MatchFoundAndExpected; 407 408 // Compute the mark location, and break annotation into multiple 409 // annotations if it spans multiple lines. 410 A.IsFirstLine = true; 411 A.InputLine = DiagItr->InputStartLine; 412 A.InputStartCol = DiagItr->InputStartCol; 413 if (DiagItr->InputStartLine == DiagItr->InputEndLine) { 414 // Sometimes ranges are empty in order to indicate a specific point, but 415 // that would mean nothing would be marked, so adjust the range to 416 // include the following character. 417 A.InputEndCol = 418 std::max(DiagItr->InputStartCol + 1, DiagItr->InputEndCol); 419 Annotations.push_back(A); 420 } else { 421 assert(DiagItr->InputStartLine < DiagItr->InputEndLine && 422 "expected input range not to be inverted"); 423 A.InputEndCol = UINT_MAX; 424 Annotations.push_back(A); 425 for (unsigned L = DiagItr->InputStartLine + 1, E = DiagItr->InputEndLine; 426 L <= E; ++L) { 427 // If a range ends before the first column on a line, then it has no 428 // characters on that line, so there's nothing to render. 429 if (DiagItr->InputEndCol == 1 && L == E) 430 break; 431 InputAnnotation B; 432 B.DiagIndex = A.DiagIndex; 433 B.Label = A.Label; 434 B.IsFirstLine = false; 435 B.InputLine = L; 436 B.Marker = A.Marker; 437 B.Marker.Lead = '~'; 438 B.Marker.Note = ""; 439 B.InputStartCol = 1; 440 if (L != E) 441 B.InputEndCol = UINT_MAX; 442 else 443 B.InputEndCol = DiagItr->InputEndCol; 444 B.FoundAndExpectedMatch = A.FoundAndExpectedMatch; 445 Annotations.push_back(B); 446 } 447 } 448 } 449 } 450 451 static unsigned FindInputLineInFilter( 452 DumpInputFilterValue DumpInputFilter, unsigned CurInputLine, 453 const std::vector<InputAnnotation>::iterator &AnnotationBeg, 454 const std::vector<InputAnnotation>::iterator &AnnotationEnd) { 455 if (DumpInputFilter == DumpInputFilterAll) 456 return CurInputLine; 457 for (auto AnnotationItr = AnnotationBeg; AnnotationItr != AnnotationEnd; 458 ++AnnotationItr) { 459 switch (DumpInputFilter) { 460 case DumpInputFilterAll: 461 llvm_unreachable("unexpected DumpInputFilterAll"); 462 break; 463 case DumpInputFilterAnnotationFull: 464 return AnnotationItr->InputLine; 465 case DumpInputFilterAnnotation: 466 if (AnnotationItr->IsFirstLine) 467 return AnnotationItr->InputLine; 468 break; 469 case DumpInputFilterError: 470 if (AnnotationItr->IsFirstLine && AnnotationItr->Marker.FiltersAsError) 471 return AnnotationItr->InputLine; 472 break; 473 } 474 } 475 return UINT_MAX; 476 } 477 478 /// To OS, print a vertical ellipsis (right-justified at LabelWidth) if it would 479 /// occupy less lines than ElidedLines, but print ElidedLines otherwise. Either 480 /// way, clear ElidedLines. Thus, if ElidedLines is empty, do nothing. 481 static void DumpEllipsisOrElidedLines(raw_ostream &OS, std::string &ElidedLines, 482 unsigned LabelWidth) { 483 if (ElidedLines.empty()) 484 return; 485 unsigned EllipsisLines = 3; 486 if (EllipsisLines < StringRef(ElidedLines).count('\n')) { 487 for (unsigned i = 0; i < EllipsisLines; ++i) { 488 WithColor(OS, raw_ostream::BLACK, /*Bold=*/true) 489 << right_justify(".", LabelWidth); 490 OS << '\n'; 491 } 492 } else 493 OS << ElidedLines; 494 ElidedLines.clear(); 495 } 496 497 static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req, 498 DumpInputFilterValue DumpInputFilter, 499 unsigned DumpInputContext, 500 StringRef InputFileText, 501 std::vector<InputAnnotation> &Annotations, 502 unsigned LabelWidth) { 503 OS << "Input was:\n<<<<<<\n"; 504 505 // Sort annotations. 506 std::sort(Annotations.begin(), Annotations.end(), 507 [](const InputAnnotation &A, const InputAnnotation &B) { 508 // 1. Sort annotations in the order of the input lines. 509 // 510 // This makes it easier to find relevant annotations while 511 // iterating input lines in the implementation below. FileCheck 512 // does not always produce diagnostics in the order of input 513 // lines due to, for example, CHECK-DAG and CHECK-NOT. 514 if (A.InputLine != B.InputLine) 515 return A.InputLine < B.InputLine; 516 // 2. Sort annotations in the temporal order FileCheck produced 517 // their associated diagnostics. 518 // 519 // This sort offers several benefits: 520 // 521 // A. On a single input line, the order of annotations reflects 522 // the FileCheck logic for processing directives/patterns. 523 // This can be helpful in understanding cases in which the 524 // order of the associated directives/patterns in the check 525 // file or on the command line either (i) does not match the 526 // temporal order in which FileCheck looks for matches for the 527 // directives/patterns (due to, for example, CHECK-LABEL, 528 // CHECK-NOT, or `--implicit-check-not`) or (ii) does match 529 // that order but does not match the order of those 530 // diagnostics along an input line (due to, for example, 531 // CHECK-DAG). 532 // 533 // On the other hand, because our presentation format presents 534 // input lines in order, there's no clear way to offer the 535 // same benefit across input lines. For consistency, it might 536 // then seem worthwhile to have annotations on a single line 537 // also sorted in input order (that is, by input column). 538 // However, in practice, this appears to be more confusing 539 // than helpful. Perhaps it's intuitive to expect annotations 540 // to be listed in the temporal order in which they were 541 // produced except in cases the presentation format obviously 542 // and inherently cannot support it (that is, across input 543 // lines). 544 // 545 // B. When diagnostics' annotations are split among multiple 546 // input lines, the user must track them from one input line 547 // to the next. One property of the sort chosen here is that 548 // it facilitates the user in this regard by ensuring the 549 // following: when comparing any two input lines, a 550 // diagnostic's annotations are sorted in the same position 551 // relative to all other diagnostics' annotations. 552 return A.DiagIndex < B.DiagIndex; 553 }); 554 555 // Compute the width of the label column. 556 const unsigned char *InputFilePtr = InputFileText.bytes_begin(), 557 *InputFileEnd = InputFileText.bytes_end(); 558 unsigned LineCount = InputFileText.count('\n'); 559 if (InputFileEnd[-1] != '\n') 560 ++LineCount; 561 unsigned LineNoWidth = std::log10(LineCount) + 1; 562 // +3 below adds spaces (1) to the left of the (right-aligned) line numbers 563 // on input lines and (2) to the right of the (left-aligned) labels on 564 // annotation lines so that input lines and annotation lines are more 565 // visually distinct. For example, the spaces on the annotation lines ensure 566 // that input line numbers and check directive line numbers never align 567 // horizontally. Those line numbers might not even be for the same file. 568 // One space would be enough to achieve that, but more makes it even easier 569 // to see. 570 LabelWidth = std::max(LabelWidth, LineNoWidth) + 3; 571 572 // Print annotated input lines. 573 unsigned PrevLineInFilter = 0; // 0 means none so far 574 unsigned NextLineInFilter = 0; // 0 means uncomputed, UINT_MAX means none 575 std::string ElidedLines; 576 raw_string_ostream ElidedLinesOS(ElidedLines); 577 ColorMode TheColorMode = 578 WithColor(OS).colorsEnabled() ? ColorMode::Enable : ColorMode::Disable; 579 if (TheColorMode == ColorMode::Enable) 580 ElidedLinesOS.enable_colors(true); 581 auto AnnotationItr = Annotations.begin(), AnnotationEnd = Annotations.end(); 582 for (unsigned Line = 1; 583 InputFilePtr != InputFileEnd || AnnotationItr != AnnotationEnd; 584 ++Line) { 585 const unsigned char *InputFileLine = InputFilePtr; 586 587 // Compute the previous and next line included by the filter. 588 if (NextLineInFilter < Line) 589 NextLineInFilter = FindInputLineInFilter(DumpInputFilter, Line, 590 AnnotationItr, AnnotationEnd); 591 assert(NextLineInFilter && "expected NextLineInFilter to be computed"); 592 if (NextLineInFilter == Line) 593 PrevLineInFilter = Line; 594 595 // Elide this input line and its annotations if it's not within the 596 // context specified by -dump-input-context of an input line included by 597 // -dump-input-filter. However, in case the resulting ellipsis would occupy 598 // more lines than the input lines and annotations it elides, buffer the 599 // elided lines and annotations so we can print them instead. 600 raw_ostream *LineOS = &OS; 601 if ((!PrevLineInFilter || PrevLineInFilter + DumpInputContext < Line) && 602 (NextLineInFilter == UINT_MAX || 603 Line + DumpInputContext < NextLineInFilter)) 604 LineOS = &ElidedLinesOS; 605 else { 606 LineOS = &OS; 607 DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth); 608 } 609 610 // Print right-aligned line number. 611 WithColor(*LineOS, raw_ostream::BLACK, /*Bold=*/true, /*BF=*/false, 612 TheColorMode) 613 << format_decimal(Line, LabelWidth) << ": "; 614 615 // For the case where -v and colors are enabled, find the annotations for 616 // good matches for expected patterns in order to highlight everything 617 // else in the line. There are no such annotations if -v is disabled. 618 std::vector<InputAnnotation> FoundAndExpectedMatches; 619 if (Req.Verbose && TheColorMode == ColorMode::Enable) { 620 for (auto I = AnnotationItr; I != AnnotationEnd && I->InputLine == Line; 621 ++I) { 622 if (I->FoundAndExpectedMatch) 623 FoundAndExpectedMatches.push_back(*I); 624 } 625 } 626 627 // Print numbered line with highlighting where there are no matches for 628 // expected patterns. 629 bool Newline = false; 630 { 631 WithColor COS(*LineOS, raw_ostream::SAVEDCOLOR, /*Bold=*/false, 632 /*BG=*/false, TheColorMode); 633 bool InMatch = false; 634 if (Req.Verbose) 635 COS.changeColor(raw_ostream::CYAN, true, true); 636 for (unsigned Col = 1; InputFilePtr != InputFileEnd && !Newline; ++Col) { 637 bool WasInMatch = InMatch; 638 InMatch = false; 639 for (auto M : FoundAndExpectedMatches) { 640 if (M.InputStartCol <= Col && Col < M.InputEndCol) { 641 InMatch = true; 642 break; 643 } 644 } 645 if (!WasInMatch && InMatch) 646 COS.resetColor(); 647 else if (WasInMatch && !InMatch) 648 COS.changeColor(raw_ostream::CYAN, true, true); 649 if (*InputFilePtr == '\n') 650 Newline = true; 651 else 652 COS << *InputFilePtr; 653 ++InputFilePtr; 654 } 655 } 656 *LineOS << '\n'; 657 unsigned InputLineWidth = InputFilePtr - InputFileLine - Newline; 658 659 // Print any annotations. 660 while (AnnotationItr != AnnotationEnd && 661 AnnotationItr->InputLine == Line) { 662 WithColor COS(*LineOS, AnnotationItr->Marker.Color, /*Bold=*/true, 663 /*BG=*/false, TheColorMode); 664 // The two spaces below are where the ": " appears on input lines. 665 COS << left_justify(AnnotationItr->Label, LabelWidth) << " "; 666 unsigned Col; 667 for (Col = 1; Col < AnnotationItr->InputStartCol; ++Col) 668 COS << ' '; 669 COS << AnnotationItr->Marker.Lead; 670 // If InputEndCol=UINT_MAX, stop at InputLineWidth. 671 for (++Col; Col < AnnotationItr->InputEndCol && Col <= InputLineWidth; 672 ++Col) 673 COS << '~'; 674 const std::string &Note = AnnotationItr->Marker.Note; 675 if (!Note.empty()) { 676 // Put the note at the end of the input line. If we were to instead 677 // put the note right after the marker, subsequent annotations for the 678 // same input line might appear to mark this note instead of the input 679 // line. 680 for (; Col <= InputLineWidth; ++Col) 681 COS << ' '; 682 COS << ' ' << Note; 683 } 684 COS << '\n'; 685 ++AnnotationItr; 686 } 687 } 688 DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth); 689 690 OS << ">>>>>>\n"; 691 } 692 693 int main(int argc, char **argv) { 694 // Enable use of ANSI color codes because FileCheck is using them to 695 // highlight text. 696 llvm::sys::Process::UseANSIEscapeCodes(true); 697 698 InitLLVM X(argc, argv); 699 cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr, 700 "FILECHECK_OPTS"); 701 702 // Select -dump-input* values. The -help documentation specifies the default 703 // value and which value to choose if an option is specified multiple times. 704 // In the latter case, the general rule of thumb is to choose the value that 705 // provides the most information. 706 DumpInputValue DumpInput = 707 DumpInputs.empty() 708 ? DumpInputFail 709 : *std::max_element(DumpInputs.begin(), DumpInputs.end()); 710 DumpInputFilterValue DumpInputFilter; 711 if (DumpInputFilters.empty()) 712 DumpInputFilter = DumpInput == DumpInputAlways ? DumpInputFilterAll 713 : DumpInputFilterError; 714 else 715 DumpInputFilter = 716 *std::max_element(DumpInputFilters.begin(), DumpInputFilters.end()); 717 unsigned DumpInputContext = DumpInputContexts.empty() 718 ? 5 719 : *std::max_element(DumpInputContexts.begin(), 720 DumpInputContexts.end()); 721 722 if (DumpInput == DumpInputHelp) { 723 DumpInputAnnotationHelp(outs()); 724 return 0; 725 } 726 if (CheckFilename.empty()) { 727 errs() << "<check-file> not specified\n"; 728 return 2; 729 } 730 731 FileCheckRequest Req; 732 for (StringRef Prefix : CheckPrefixes) 733 Req.CheckPrefixes.push_back(Prefix); 734 735 for (StringRef Prefix : CommentPrefixes) 736 Req.CommentPrefixes.push_back(Prefix); 737 738 for (StringRef CheckNot : ImplicitCheckNot) 739 Req.ImplicitCheckNot.push_back(CheckNot); 740 741 bool GlobalDefineError = false; 742 for (StringRef G : GlobalDefines) { 743 size_t EqIdx = G.find('='); 744 if (EqIdx == std::string::npos) { 745 errs() << "Missing equal sign in command-line definition '-D" << G 746 << "'\n"; 747 GlobalDefineError = true; 748 continue; 749 } 750 if (EqIdx == 0) { 751 errs() << "Missing variable name in command-line definition '-D" << G 752 << "'\n"; 753 GlobalDefineError = true; 754 continue; 755 } 756 Req.GlobalDefines.push_back(G); 757 } 758 if (GlobalDefineError) 759 return 2; 760 761 Req.AllowEmptyInput = AllowEmptyInput; 762 Req.EnableVarScope = EnableVarScope; 763 Req.AllowDeprecatedDagOverlap = AllowDeprecatedDagOverlap; 764 Req.Verbose = Verbose; 765 Req.VerboseVerbose = VerboseVerbose; 766 Req.NoCanonicalizeWhiteSpace = NoCanonicalizeWhiteSpace; 767 Req.MatchFullLines = MatchFullLines; 768 Req.IgnoreCase = IgnoreCase; 769 770 if (VerboseVerbose) 771 Req.Verbose = true; 772 773 FileCheck FC(Req); 774 if (!FC.ValidateCheckPrefixes()) 775 return 2; 776 777 Regex PrefixRE = FC.buildCheckPrefixRegex(); 778 std::string REError; 779 if (!PrefixRE.isValid(REError)) { 780 errs() << "Unable to combine check-prefix strings into a prefix regular " 781 "expression! This is likely a bug in FileCheck's verification of " 782 "the check-prefix strings. Regular expression parsing failed " 783 "with the following error: " 784 << REError << "\n"; 785 return 2; 786 } 787 788 SourceMgr SM; 789 790 // Read the expected strings from the check file. 791 ErrorOr<std::unique_ptr<MemoryBuffer>> CheckFileOrErr = 792 MemoryBuffer::getFileOrSTDIN(CheckFilename); 793 if (std::error_code EC = CheckFileOrErr.getError()) { 794 errs() << "Could not open check file '" << CheckFilename 795 << "': " << EC.message() << '\n'; 796 return 2; 797 } 798 MemoryBuffer &CheckFile = *CheckFileOrErr.get(); 799 800 SmallString<4096> CheckFileBuffer; 801 StringRef CheckFileText = FC.CanonicalizeFile(CheckFile, CheckFileBuffer); 802 803 unsigned CheckFileBufferID = 804 SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 805 CheckFileText, CheckFile.getBufferIdentifier()), 806 SMLoc()); 807 808 std::pair<unsigned, unsigned> ImpPatBufferIDRange; 809 if (FC.readCheckFile(SM, CheckFileText, PrefixRE, &ImpPatBufferIDRange)) 810 return 2; 811 812 // Open the file to check and add it to SourceMgr. 813 ErrorOr<std::unique_ptr<MemoryBuffer>> InputFileOrErr = 814 MemoryBuffer::getFileOrSTDIN(InputFilename); 815 if (InputFilename == "-") 816 InputFilename = "<stdin>"; // Overwrite for improved diagnostic messages 817 if (std::error_code EC = InputFileOrErr.getError()) { 818 errs() << "Could not open input file '" << InputFilename 819 << "': " << EC.message() << '\n'; 820 return 2; 821 } 822 MemoryBuffer &InputFile = *InputFileOrErr.get(); 823 824 if (InputFile.getBufferSize() == 0 && !AllowEmptyInput) { 825 errs() << "FileCheck error: '" << InputFilename << "' is empty.\n"; 826 DumpCommandLine(argc, argv); 827 return 2; 828 } 829 830 SmallString<4096> InputFileBuffer; 831 StringRef InputFileText = FC.CanonicalizeFile(InputFile, InputFileBuffer); 832 833 SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 834 InputFileText, InputFile.getBufferIdentifier()), 835 SMLoc()); 836 837 std::vector<FileCheckDiag> Diags; 838 int ExitCode = FC.checkInput(SM, InputFileText, 839 DumpInput == DumpInputNever ? nullptr : &Diags) 840 ? EXIT_SUCCESS 841 : 1; 842 if (DumpInput == DumpInputAlways || 843 (ExitCode == 1 && DumpInput == DumpInputFail)) { 844 errs() << "\n" 845 << "Input file: " << InputFilename << "\n" 846 << "Check file: " << CheckFilename << "\n" 847 << "\n" 848 << "-dump-input=help explains the following input dump.\n" 849 << "\n"; 850 std::vector<InputAnnotation> Annotations; 851 unsigned LabelWidth; 852 BuildInputAnnotations(SM, CheckFileBufferID, ImpPatBufferIDRange, Diags, 853 Annotations, LabelWidth); 854 DumpAnnotatedInput(errs(), Req, DumpInputFilter, DumpInputContext, 855 InputFileText, Annotations, LabelWidth); 856 } 857 858 return ExitCode; 859 } 860