1ee3c74fbSChris Lattner //===- FileCheck.cpp - Check that File's Contents match what is expected --===// 2ee3c74fbSChris Lattner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6ee3c74fbSChris Lattner // 7ee3c74fbSChris Lattner //===----------------------------------------------------------------------===// 8ee3c74fbSChris Lattner // 9ee3c74fbSChris Lattner // FileCheck does a line-by line check of a file that validates whether it 10ee3c74fbSChris Lattner // contains the expected content. This is useful for regression tests etc. 11ee3c74fbSChris Lattner // 12b5ecceffSJames Henderson // This program exits with an exit status of 2 on error, exit status of 0 if 13ee3c74fbSChris Lattner // the file matched the expected contents, and exit status of 1 if it did not 14ee3c74fbSChris Lattner // contain the expected contents. 15ee3c74fbSChris Lattner // 16ee3c74fbSChris Lattner //===----------------------------------------------------------------------===// 17ee3c74fbSChris Lattner 18ee3c74fbSChris Lattner #include "llvm/Support/CommandLine.h" 19197194b6SRui Ueyama #include "llvm/Support/InitLLVM.h" 203e66509fSJoel E. Denny #include "llvm/Support/Process.h" 213c5d267eSJoel E. Denny #include "llvm/Support/WithColor.h" 22ee3c74fbSChris Lattner #include "llvm/Support/raw_ostream.h" 23ffa9d2e4SAditya Nandakumar #include "llvm/Support/FileCheck.h" 24010982f7SRainer Orth #include <cmath> 25ee3c74fbSChris Lattner using namespace llvm; 26ee3c74fbSChris Lattner 27dbb757f4SJoel E. Denny static cl::extrahelp FileCheckOptsEnv( 28dbb757f4SJoel E. Denny "\nOptions are parsed from the environment variable FILECHECK_OPTS and\n" 29dbb757f4SJoel E. Denny "from the command line.\n"); 30dbb757f4SJoel E. Denny 31ee3c74fbSChris Lattner static cl::opt<std::string> 323c5d267eSJoel E. Denny CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Optional); 33ee3c74fbSChris Lattner 34ee3c74fbSChris Lattner static cl::opt<std::string> 35ee3c74fbSChris Lattner InputFilename("input-file", cl::desc("File to check (defaults to stdin)"), 36ee3c74fbSChris Lattner cl::init("-"), cl::value_desc("filename")); 37ee3c74fbSChris Lattner 38e8f2fb20SChandler Carruth static cl::list<std::string> CheckPrefixes( 39e8f2fb20SChandler Carruth "check-prefix", 40ee3c74fbSChris Lattner cl::desc("Prefix to use from check file (defaults to 'CHECK')")); 41fd557cb0SDaniel Sanders static cl::alias CheckPrefixesAlias( 42fd557cb0SDaniel Sanders "check-prefixes", cl::aliasopt(CheckPrefixes), cl::CommaSeparated, 43fd557cb0SDaniel Sanders cl::NotHidden, 44fd557cb0SDaniel Sanders cl::desc( 45fd557cb0SDaniel Sanders "Alias for -check-prefix permitting multiple comma separated values")); 46ee3c74fbSChris Lattner 47a1fd1882SJoel E. Denny static cl::list<std::string> CommentPrefixes( 48a1fd1882SJoel E. Denny "comment-prefixes", cl::CommaSeparated, cl::Hidden, 49a1fd1882SJoel E. Denny cl::desc("Comma-separated list of comment prefixes to use from check file\n" 50a1fd1882SJoel E. Denny "(defaults to 'COM,RUN'). Please avoid using this feature in\n" 51a1fd1882SJoel E. Denny "LLVM's LIT-based test suites, which should be easier to\n" 52a1fd1882SJoel E. Denny "maintain if they all follow a consistent comment style. This\n" 53a1fd1882SJoel E. Denny "feature is meant for non-LIT test suites using FileCheck.")); 54a1fd1882SJoel E. Denny 55e8f2fb20SChandler Carruth static cl::opt<bool> NoCanonicalizeWhiteSpace( 56e8f2fb20SChandler Carruth "strict-whitespace", 572c3e5cdfSChris Lattner cl::desc("Do not treat all horizontal whitespace as equivalent")); 582c3e5cdfSChris Lattner 595b5b2fd2SKai Nacke static cl::opt<bool> IgnoreCase( 605b5b2fd2SKai Nacke "ignore-case", 615b5b2fd2SKai Nacke cl::desc("Use case-insensitive matching")); 625b5b2fd2SKai Nacke 6356ccdbbdSAlexander Kornienko static cl::list<std::string> ImplicitCheckNot( 6456ccdbbdSAlexander Kornienko "implicit-check-not", 6556ccdbbdSAlexander Kornienko cl::desc("Add an implicit negative check with this pattern to every\n" 6656ccdbbdSAlexander Kornienko "positive check. This can be used to ensure that no instances of\n" 6756ccdbbdSAlexander Kornienko "this pattern occur which are not matched by a positive pattern"), 6856ccdbbdSAlexander Kornienko cl::value_desc("pattern")); 6956ccdbbdSAlexander Kornienko 70a5e233bfSThomas Preud'homme static cl::list<std::string> 71a5e233bfSThomas Preud'homme GlobalDefines("D", cl::AlwaysPrefix, 7246e1fd61SAlexander Richardson cl::desc("Define a variable to be used in capture patterns."), 7346e1fd61SAlexander Richardson cl::value_desc("VAR=VALUE")); 7446e1fd61SAlexander Richardson 751b9f936fSJustin Bogner static cl::opt<bool> AllowEmptyInput( 761b9f936fSJustin Bogner "allow-empty", cl::init(false), 771b9f936fSJustin Bogner cl::desc("Allow the input file to be empty. This is useful when making\n" 781b9f936fSJustin Bogner "checks that some error message does not occur, for example.")); 791b9f936fSJustin Bogner 8085913ccaSJames Y Knight static cl::opt<bool> MatchFullLines( 8185913ccaSJames Y Knight "match-full-lines", cl::init(false), 8285913ccaSJames Y Knight cl::desc("Require all positive matches to cover an entire input line.\n" 8385913ccaSJames Y Knight "Allows leading and trailing whitespace if --strict-whitespace\n" 8485913ccaSJames Y Knight "is not also passed.")); 8585913ccaSJames Y Knight 86f55e72a5SArtem Belevich static cl::opt<bool> EnableVarScope( 87f55e72a5SArtem Belevich "enable-var-scope", cl::init(false), 88f55e72a5SArtem Belevich cl::desc("Enables scope for regex variables. Variables with names that\n" 89f55e72a5SArtem Belevich "do not start with '$' will be reset at the beginning of\n" 90f55e72a5SArtem Belevich "each CHECK-LABEL block.")); 91f55e72a5SArtem Belevich 92bcf5b441SJoel E. Denny static cl::opt<bool> AllowDeprecatedDagOverlap( 93bcf5b441SJoel E. Denny "allow-deprecated-dag-overlap", cl::init(false), 94bcf5b441SJoel E. Denny cl::desc("Enable overlapping among matches in a group of consecutive\n" 95bcf5b441SJoel E. Denny "CHECK-DAG directives. This option is deprecated and is only\n" 96bcf5b441SJoel E. Denny "provided for convenience as old tests are migrated to the new\n" 97bcf5b441SJoel E. Denny "non-overlapping CHECK-DAG implementation.\n")); 98bcf5b441SJoel E. Denny 99352695c3SJoel E. Denny static cl::opt<bool> Verbose( 100782585a2SJoel E. Denny "v", cl::init(false), cl::ZeroOrMore, 101352695c3SJoel E. Denny cl::desc("Print directive pattern matches, or add them to the input dump\n" 102352695c3SJoel E. Denny "if enabled.\n")); 103dc5ba317SJoel E. Denny 104dc5ba317SJoel E. Denny static cl::opt<bool> VerboseVerbose( 105782585a2SJoel E. Denny "vv", cl::init(false), cl::ZeroOrMore, 106dc5ba317SJoel E. Denny cl::desc("Print information helpful in diagnosing internal FileCheck\n" 107352695c3SJoel E. Denny "issues, or add it to the input dump if enabled. Implies\n" 108352695c3SJoel E. Denny "-v.\n")); 1093c5d267eSJoel E. Denny 110fdde18a7SJoel E. Denny // The order of DumpInputValue members affects their precedence, as documented 111fdde18a7SJoel E. Denny // for -dump-input below. 1123c5d267eSJoel E. Denny enum DumpInputValue { 1133c5d267eSJoel E. Denny DumpInputNever, 1143c5d267eSJoel E. Denny DumpInputFail, 115fdde18a7SJoel E. Denny DumpInputAlways, 116fdde18a7SJoel E. Denny DumpInputHelp 1173c5d267eSJoel E. Denny }; 1183c5d267eSJoel E. Denny 119fdde18a7SJoel E. Denny static cl::list<DumpInputValue> DumpInputs( 120fdde18a7SJoel E. Denny "dump-input", 1213c5d267eSJoel E. Denny cl::desc("Dump input to stderr, adding annotations representing\n" 122fdde18a7SJoel E. Denny "currently enabled diagnostics. When there are multiple\n" 123fdde18a7SJoel E. Denny "occurrences of this option, the <value> that appears earliest\n" 124839f8e4fSJoel E. Denny "in the list below has precedence. The default is 'fail'.\n"), 1253c5d267eSJoel E. Denny cl::value_desc("mode"), 126839f8e4fSJoel E. Denny cl::values(clEnumValN(DumpInputHelp, "help", "Explain input dump and quit"), 127fdde18a7SJoel E. Denny clEnumValN(DumpInputAlways, "always", "Always dump input"), 1283c5d267eSJoel E. Denny clEnumValN(DumpInputFail, "fail", "Dump input on failure"), 129fdde18a7SJoel E. Denny clEnumValN(DumpInputNever, "never", "Never dump input"))); 130dc5ba317SJoel E. Denny 131*9fd4b5faSJoel E. Denny // The order of DumpInputFilterValue members affects their precedence, as 132*9fd4b5faSJoel E. Denny // documented for -dump-input-filter below. 133*9fd4b5faSJoel E. Denny enum DumpInputFilterValue { 134*9fd4b5faSJoel E. Denny DumpInputFilterError, 135*9fd4b5faSJoel E. Denny DumpInputFilterAnnotation, 136*9fd4b5faSJoel E. Denny DumpInputFilterAnnotationFull, 137*9fd4b5faSJoel E. Denny DumpInputFilterAll 138*9fd4b5faSJoel E. Denny }; 139*9fd4b5faSJoel E. Denny 140*9fd4b5faSJoel E. Denny static cl::list<DumpInputFilterValue> DumpInputFilters( 141*9fd4b5faSJoel E. Denny "dump-input-filter", 142*9fd4b5faSJoel E. Denny cl::desc("In the dump requested by -dump-input, print only input lines of\n" 143*9fd4b5faSJoel E. Denny "kind <kind> plus any context specified by -dump-input-context.\n" 144*9fd4b5faSJoel E. Denny "When there are multiple occurrences of this option, the <kind>\n" 145*9fd4b5faSJoel E. Denny "that appears earliest in the list below has precedence. The\n" 146*9fd4b5faSJoel E. Denny "default is 'error' when -dump-input=fail, and it's 'all' when\n" 147*9fd4b5faSJoel E. Denny "-dump-input=always.\n"), 148*9fd4b5faSJoel E. Denny cl::value_desc("kind"), 149*9fd4b5faSJoel E. Denny cl::values(clEnumValN(DumpInputFilterAll, "all", "All input lines"), 150*9fd4b5faSJoel E. Denny clEnumValN(DumpInputFilterAnnotationFull, "annotation-full", 151*9fd4b5faSJoel E. Denny "Input lines with annotations"), 152*9fd4b5faSJoel E. Denny clEnumValN(DumpInputFilterAnnotation, "annotation", 153*9fd4b5faSJoel E. Denny "Input lines with starting points of annotations"), 154*9fd4b5faSJoel E. Denny clEnumValN(DumpInputFilterError, "error", 155*9fd4b5faSJoel E. Denny "Input lines with starting points of error " 156*9fd4b5faSJoel E. Denny "annotations"))); 157*9fd4b5faSJoel E. Denny 158bce8fcedSJoel E. Denny static cl::list<unsigned> DumpInputContexts( 159bce8fcedSJoel E. Denny "dump-input-context", cl::value_desc("N"), 160*9fd4b5faSJoel E. Denny cl::desc("In the dump requested by -dump-input, print <N> input lines\n" 161*9fd4b5faSJoel E. Denny "before and <N> input lines after any lines specified by\n" 162*9fd4b5faSJoel E. Denny "-dump-input-filter. When there are multiple occurrences of\n" 163bce8fcedSJoel E. Denny "this option, the largest specified <N> has precedence. The\n" 164bce8fcedSJoel E. Denny "default is 5.\n")); 165bce8fcedSJoel E. Denny 16613df4626SMatt Arsenault typedef cl::list<std::string>::const_iterator prefix_iterator; 16713df4626SMatt Arsenault 16813df4626SMatt Arsenault 16913df4626SMatt Arsenault 170726774cbSChandler Carruth 171726774cbSChandler Carruth 172726774cbSChandler Carruth 173c2735158SRui Ueyama 1742bd4f8b6SXinliang David Li static void DumpCommandLine(int argc, char **argv) { 1752bd4f8b6SXinliang David Li errs() << "FileCheck command line: "; 1762bd4f8b6SXinliang David Li for (int I = 0; I < argc; I++) 1772bd4f8b6SXinliang David Li errs() << " " << argv[I]; 1782bd4f8b6SXinliang David Li errs() << "\n"; 1792bd4f8b6SXinliang David Li } 1802bd4f8b6SXinliang David Li 1813c5d267eSJoel E. Denny struct MarkerStyle { 1823c5d267eSJoel E. Denny /// The starting char (before tildes) for marking the line. 1833c5d267eSJoel E. Denny char Lead; 1843c5d267eSJoel E. Denny /// What color to use for this annotation. 1854d41c332SRui Ueyama raw_ostream::Colors Color; 1863c5d267eSJoel E. Denny /// A note to follow the marker, or empty string if none. 1873c5d267eSJoel E. Denny std::string Note; 188*9fd4b5faSJoel E. Denny /// Does this marker indicate inclusion by -dump-input-filter=error? 189bce8fcedSJoel E. Denny bool FiltersAsError; 1903c5d267eSJoel E. Denny MarkerStyle() {} 1914d41c332SRui Ueyama MarkerStyle(char Lead, raw_ostream::Colors Color, 192bce8fcedSJoel E. Denny const std::string &Note = "", bool FiltersAsError = false) 193bce8fcedSJoel E. Denny : Lead(Lead), Color(Color), Note(Note), FiltersAsError(FiltersAsError) { 194bce8fcedSJoel E. Denny assert((!FiltersAsError || !Note.empty()) && 195bce8fcedSJoel E. Denny "expected error diagnostic to have note"); 196bce8fcedSJoel E. Denny } 1973c5d267eSJoel E. Denny }; 1983c5d267eSJoel E. Denny 1993c5d267eSJoel E. Denny static MarkerStyle GetMarker(FileCheckDiag::MatchType MatchTy) { 2003c5d267eSJoel E. Denny switch (MatchTy) { 201e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundAndExpected: 2027df86967SJoel E. Denny return MarkerStyle('^', raw_ostream::GREEN); 203e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundButExcluded: 204bce8fcedSJoel E. Denny return MarkerStyle('!', raw_ostream::RED, "error: no match expected", 205bce8fcedSJoel E. Denny /*FiltersAsError=*/true); 206e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundButWrongLine: 207bce8fcedSJoel E. Denny return MarkerStyle('!', raw_ostream::RED, "error: match on wrong line", 208bce8fcedSJoel E. Denny /*FiltersAsError=*/true); 209e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundButDiscarded: 210f7c1c4d8SJoel E. Denny return MarkerStyle('!', raw_ostream::CYAN, 211f7c1c4d8SJoel E. Denny "discard: overlaps earlier match"); 21296f0e84cSJoel E. Denny case FileCheckDiag::MatchNoneAndExcluded: 21396f0e84cSJoel E. Denny return MarkerStyle('X', raw_ostream::GREEN); 2143c5d267eSJoel E. Denny case FileCheckDiag::MatchNoneButExpected: 215bce8fcedSJoel E. Denny return MarkerStyle('X', raw_ostream::RED, "error: no match found", 216bce8fcedSJoel E. Denny /*FiltersAsError=*/true); 2172c007c80SJoel E. Denny case FileCheckDiag::MatchFuzzy: 218bce8fcedSJoel E. Denny return MarkerStyle('?', raw_ostream::MAGENTA, "possible intended match", 219bce8fcedSJoel E. Denny /*FiltersAsError=*/true); 2203c5d267eSJoel E. Denny } 2213c5d267eSJoel E. Denny llvm_unreachable_internal("unexpected match type"); 2223c5d267eSJoel E. Denny } 2233c5d267eSJoel E. Denny 2243c5d267eSJoel E. Denny static void DumpInputAnnotationHelp(raw_ostream &OS) { 2253c5d267eSJoel E. Denny OS << "The following description was requested by -dump-input=help to\n" 226839f8e4fSJoel E. Denny << "explain the input dump printed by FileCheck.\n" 227839f8e4fSJoel E. Denny << "\n" 228839f8e4fSJoel E. Denny << "Related command-line options:\n" 229839f8e4fSJoel E. Denny << " - -dump-input=<value> enables or disables the input dump\n" 230*9fd4b5faSJoel E. Denny << " - -dump-input-filter=<kind> filters the input lines\n" 231*9fd4b5faSJoel E. Denny << " - -dump-input-context=<N> adjusts the context of filtered lines\n" 232839f8e4fSJoel E. Denny << " - -v and -vv add more annotations\n" 233839f8e4fSJoel E. Denny << " - -color forces colors to be enabled both in the dump and below\n" 234839f8e4fSJoel E. Denny << " - -help documents the above options in more detail\n" 235839f8e4fSJoel E. Denny << "\n" 236839f8e4fSJoel E. Denny << "Input dump annotation format:\n"; 2373c5d267eSJoel E. Denny 2383c5d267eSJoel E. Denny // Labels for input lines. 2393c5d267eSJoel E. Denny OS << " - "; 2403c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "L:"; 2413c5d267eSJoel E. Denny OS << " labels line number L of the input file\n"; 2423c5d267eSJoel E. Denny 2433c5d267eSJoel E. Denny // Labels for annotation lines. 2443c5d267eSJoel E. Denny OS << " - "; 2453c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L"; 246b5a24610SJoel E. Denny OS << " labels the only match result for either (1) a pattern of type T" 247b5a24610SJoel E. Denny << " from\n" 248b5a24610SJoel E. Denny << " line L of the check file if L is an integer or (2) the" 249b5a24610SJoel E. Denny << " I-th implicit\n" 250b5a24610SJoel E. Denny << " pattern if L is \"imp\" followed by an integer " 251b5a24610SJoel E. Denny << "I (index origin one)\n"; 2522c007c80SJoel E. Denny OS << " - "; 2532c007c80SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L'N"; 254b5a24610SJoel E. Denny OS << " labels the Nth match result for such a pattern\n"; 2553c5d267eSJoel E. Denny 2563c5d267eSJoel E. Denny // Markers on annotation lines. 2573c5d267eSJoel E. Denny OS << " - "; 2587df86967SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "^~~"; 2597df86967SJoel E. Denny OS << " marks good match (reported if -v)\n" 2607df86967SJoel E. Denny << " - "; 261cadfcef4SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "!~~"; 262cadfcef4SJoel E. Denny OS << " marks bad match, such as:\n" 263cadfcef4SJoel E. Denny << " - CHECK-NEXT on same line as previous match (error)\n" 2640e7e3fa0SJoel E. Denny << " - CHECK-NOT found (error)\n" 265f7c1c4d8SJoel E. Denny << " - CHECK-DAG overlapping match (discarded, reported if " 266f7c1c4d8SJoel E. Denny << "-vv)\n" 267cadfcef4SJoel E. Denny << " - "; 2683c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "X~~"; 269cadfcef4SJoel E. Denny OS << " marks search range when no match is found, such as:\n" 270cadfcef4SJoel E. Denny << " - CHECK-NEXT not found (error)\n" 27196f0e84cSJoel E. Denny << " - CHECK-NOT not found (success, reported if -vv)\n" 272f7c1c4d8SJoel E. Denny << " - CHECK-DAG not found after discarded matches (error)\n" 2732c007c80SJoel E. Denny << " - "; 2742c007c80SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "?"; 2752c007c80SJoel E. Denny OS << " marks fuzzy match when no match is found\n"; 2763c5d267eSJoel E. Denny 277bce8fcedSJoel E. Denny // Elided lines. 278bce8fcedSJoel E. Denny OS << " - "; 279bce8fcedSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "..."; 280bce8fcedSJoel E. Denny OS << " indicates elided input lines and annotations, as specified by\n" 281*9fd4b5faSJoel E. Denny << " -dump-input-filter and -dump-input-context\n"; 282bce8fcedSJoel E. Denny 2833c5d267eSJoel E. Denny // Colors. 2843c5d267eSJoel E. Denny OS << " - colors "; 2857df86967SJoel E. Denny WithColor(OS, raw_ostream::GREEN, true) << "success"; 2867df86967SJoel E. Denny OS << ", "; 2873c5d267eSJoel E. Denny WithColor(OS, raw_ostream::RED, true) << "error"; 2882c007c80SJoel E. Denny OS << ", "; 2892c007c80SJoel E. Denny WithColor(OS, raw_ostream::MAGENTA, true) << "fuzzy match"; 2907df86967SJoel E. Denny OS << ", "; 291f7c1c4d8SJoel E. Denny WithColor(OS, raw_ostream::CYAN, true, false) << "discarded match"; 292f7c1c4d8SJoel E. Denny OS << ", "; 2937df86967SJoel E. Denny WithColor(OS, raw_ostream::CYAN, true, true) << "unmatched input"; 294839f8e4fSJoel E. Denny OS << "\n"; 2953c5d267eSJoel E. Denny } 2963c5d267eSJoel E. Denny 2973c5d267eSJoel E. Denny /// An annotation for a single input line. 2983c5d267eSJoel E. Denny struct InputAnnotation { 299ce685455SJoel E. Denny /// The index of the match result across all checks 300ce685455SJoel E. Denny unsigned DiagIndex; 3013c5d267eSJoel E. Denny /// The label for this annotation. 3023c5d267eSJoel E. Denny std::string Label; 303bce8fcedSJoel E. Denny /// Is this the initial fragment of a diagnostic that has been broken across 304bce8fcedSJoel E. Denny /// multiple lines? 305bce8fcedSJoel E. Denny bool IsFirstLine; 3063c5d267eSJoel E. Denny /// What input line (one-origin indexing) this annotation marks. This might 307bce8fcedSJoel E. Denny /// be different from the starting line of the original diagnostic if 308bce8fcedSJoel E. Denny /// !IsFirstLine. 3093c5d267eSJoel E. Denny unsigned InputLine; 310ce685455SJoel E. Denny /// The column range (one-origin indexing, open end) in which to mark the 3113c5d267eSJoel E. Denny /// input line. If InputEndCol is UINT_MAX, treat it as the last column 3123c5d267eSJoel E. Denny /// before the newline. 3133c5d267eSJoel E. Denny unsigned InputStartCol, InputEndCol; 3143c5d267eSJoel E. Denny /// The marker to use. 3153c5d267eSJoel E. Denny MarkerStyle Marker; 316e2afb614SJoel E. Denny /// Whether this annotation represents a good match for an expected pattern. 317e2afb614SJoel E. Denny bool FoundAndExpectedMatch; 3183c5d267eSJoel E. Denny }; 3193c5d267eSJoel E. Denny 3203c5d267eSJoel E. Denny /// Get an abbreviation for the check type. 3213c5d267eSJoel E. Denny std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) { 3223c5d267eSJoel E. Denny switch (Ty) { 3233c5d267eSJoel E. Denny case Check::CheckPlain: 3243c5d267eSJoel E. Denny if (Ty.getCount() > 1) 3253c5d267eSJoel E. Denny return "count"; 3263c5d267eSJoel E. Denny return "check"; 3273c5d267eSJoel E. Denny case Check::CheckNext: 3283c5d267eSJoel E. Denny return "next"; 3293c5d267eSJoel E. Denny case Check::CheckSame: 3303c5d267eSJoel E. Denny return "same"; 3313c5d267eSJoel E. Denny case Check::CheckNot: 3323c5d267eSJoel E. Denny return "not"; 3333c5d267eSJoel E. Denny case Check::CheckDAG: 3343c5d267eSJoel E. Denny return "dag"; 3353c5d267eSJoel E. Denny case Check::CheckLabel: 3363c5d267eSJoel E. Denny return "label"; 3373c5d267eSJoel E. Denny case Check::CheckEmpty: 3383c5d267eSJoel E. Denny return "empty"; 339a1fd1882SJoel E. Denny case Check::CheckComment: 340a1fd1882SJoel E. Denny return "com"; 3413c5d267eSJoel E. Denny case Check::CheckEOF: 3423c5d267eSJoel E. Denny return "eof"; 3433c5d267eSJoel E. Denny case Check::CheckBadNot: 3443c5d267eSJoel E. Denny return "bad-not"; 3453c5d267eSJoel E. Denny case Check::CheckBadCount: 3463c5d267eSJoel E. Denny return "bad-count"; 3473c5d267eSJoel E. Denny case Check::CheckNone: 3483c5d267eSJoel E. Denny llvm_unreachable("invalid FileCheckType"); 3493c5d267eSJoel E. Denny } 3503c5d267eSJoel E. Denny llvm_unreachable("unknown FileCheckType"); 3513c5d267eSJoel E. Denny } 3523c5d267eSJoel E. Denny 353b5a24610SJoel E. Denny static void 354b5a24610SJoel E. Denny BuildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID, 355b5a24610SJoel E. Denny const std::pair<unsigned, unsigned> &ImpPatBufferIDRange, 356b5a24610SJoel E. Denny const std::vector<FileCheckDiag> &Diags, 3573c5d267eSJoel E. Denny std::vector<InputAnnotation> &Annotations, 3583c5d267eSJoel E. Denny unsigned &LabelWidth) { 359ce685455SJoel E. Denny // How many diagnostics have we seen so far? 360ce685455SJoel E. Denny unsigned DiagCount = 0; 3612c007c80SJoel E. Denny // How many diagnostics has the current check seen so far? 3622c007c80SJoel E. Denny unsigned CheckDiagCount = 0; 3633c5d267eSJoel E. Denny // What's the widest label? 3643c5d267eSJoel E. Denny LabelWidth = 0; 3653c5d267eSJoel E. Denny for (auto DiagItr = Diags.begin(), DiagEnd = Diags.end(); DiagItr != DiagEnd; 3663c5d267eSJoel E. Denny ++DiagItr) { 3673c5d267eSJoel E. Denny InputAnnotation A; 368ce685455SJoel E. Denny A.DiagIndex = DiagCount++; 3693c5d267eSJoel E. Denny 3703c5d267eSJoel E. Denny // Build label, which uniquely identifies this check result. 371b5a24610SJoel E. Denny unsigned CheckBufferID = SM.FindBufferContainingLoc(DiagItr->CheckLoc); 372b5a24610SJoel E. Denny auto CheckLineAndCol = 373b5a24610SJoel E. Denny SM.getLineAndColumn(DiagItr->CheckLoc, CheckBufferID); 3743c5d267eSJoel E. Denny llvm::raw_string_ostream Label(A.Label); 375b5a24610SJoel E. Denny Label << GetCheckTypeAbbreviation(DiagItr->CheckTy) << ":"; 376b5a24610SJoel E. Denny if (CheckBufferID == CheckFileBufferID) 377b5a24610SJoel E. Denny Label << CheckLineAndCol.first; 378b5a24610SJoel E. Denny else if (ImpPatBufferIDRange.first <= CheckBufferID && 379b5a24610SJoel E. Denny CheckBufferID < ImpPatBufferIDRange.second) 380b5a24610SJoel E. Denny Label << "imp" << (CheckBufferID - ImpPatBufferIDRange.first + 1); 381b5a24610SJoel E. Denny else 382b5a24610SJoel E. Denny llvm_unreachable("expected diagnostic's check location to be either in " 383b5a24610SJoel E. Denny "the check file or for an implicit pattern"); 384ce685455SJoel E. Denny unsigned CheckDiagIndex = UINT_MAX; 3852c007c80SJoel E. Denny auto DiagNext = std::next(DiagItr); 3862c007c80SJoel E. Denny if (DiagNext != DiagEnd && DiagItr->CheckTy == DiagNext->CheckTy && 387b5a24610SJoel E. Denny DiagItr->CheckLoc == DiagNext->CheckLoc) 388ce685455SJoel E. Denny CheckDiagIndex = CheckDiagCount++; 3892c007c80SJoel E. Denny else if (CheckDiagCount) { 390ce685455SJoel E. Denny CheckDiagIndex = CheckDiagCount; 3912c007c80SJoel E. Denny CheckDiagCount = 0; 3922c007c80SJoel E. Denny } 393ce685455SJoel E. Denny if (CheckDiagIndex != UINT_MAX) 394ce685455SJoel E. Denny Label << "'" << CheckDiagIndex; 3953c5d267eSJoel E. Denny Label.flush(); 3963c5d267eSJoel E. Denny LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size()); 3973c5d267eSJoel E. Denny 398608f2bfdSJoel E. Denny A.Marker = GetMarker(DiagItr->MatchTy); 399e2afb614SJoel E. Denny A.FoundAndExpectedMatch = 400e2afb614SJoel E. Denny DiagItr->MatchTy == FileCheckDiag::MatchFoundAndExpected; 4013c5d267eSJoel E. Denny 4023c5d267eSJoel E. Denny // Compute the mark location, and break annotation into multiple 4033c5d267eSJoel E. Denny // annotations if it spans multiple lines. 404bce8fcedSJoel E. Denny A.IsFirstLine = true; 4053c5d267eSJoel E. Denny A.InputLine = DiagItr->InputStartLine; 4063c5d267eSJoel E. Denny A.InputStartCol = DiagItr->InputStartCol; 4073c5d267eSJoel E. Denny if (DiagItr->InputStartLine == DiagItr->InputEndLine) { 4083c5d267eSJoel E. Denny // Sometimes ranges are empty in order to indicate a specific point, but 4093c5d267eSJoel E. Denny // that would mean nothing would be marked, so adjust the range to 4103c5d267eSJoel E. Denny // include the following character. 4113c5d267eSJoel E. Denny A.InputEndCol = 4123c5d267eSJoel E. Denny std::max(DiagItr->InputStartCol + 1, DiagItr->InputEndCol); 4133c5d267eSJoel E. Denny Annotations.push_back(A); 4143c5d267eSJoel E. Denny } else { 4153c5d267eSJoel E. Denny assert(DiagItr->InputStartLine < DiagItr->InputEndLine && 4163c5d267eSJoel E. Denny "expected input range not to be inverted"); 4173c5d267eSJoel E. Denny A.InputEndCol = UINT_MAX; 4183c5d267eSJoel E. Denny Annotations.push_back(A); 4193c5d267eSJoel E. Denny for (unsigned L = DiagItr->InputStartLine + 1, E = DiagItr->InputEndLine; 4203c5d267eSJoel E. Denny L <= E; ++L) { 4213c5d267eSJoel E. Denny // If a range ends before the first column on a line, then it has no 4223c5d267eSJoel E. Denny // characters on that line, so there's nothing to render. 423608f2bfdSJoel E. Denny if (DiagItr->InputEndCol == 1 && L == E) 4243c5d267eSJoel E. Denny break; 4253c5d267eSJoel E. Denny InputAnnotation B; 426ce685455SJoel E. Denny B.DiagIndex = A.DiagIndex; 4273c5d267eSJoel E. Denny B.Label = A.Label; 428bce8fcedSJoel E. Denny B.IsFirstLine = false; 4293c5d267eSJoel E. Denny B.InputLine = L; 430608f2bfdSJoel E. Denny B.Marker = A.Marker; 4313c5d267eSJoel E. Denny B.Marker.Lead = '~'; 4323c5d267eSJoel E. Denny B.Marker.Note = ""; 433608f2bfdSJoel E. Denny B.InputStartCol = 1; 434608f2bfdSJoel E. Denny if (L != E) 435608f2bfdSJoel E. Denny B.InputEndCol = UINT_MAX; 436608f2bfdSJoel E. Denny else 4373c5d267eSJoel E. Denny B.InputEndCol = DiagItr->InputEndCol; 438e2afb614SJoel E. Denny B.FoundAndExpectedMatch = A.FoundAndExpectedMatch; 4393c5d267eSJoel E. Denny Annotations.push_back(B); 4403c5d267eSJoel E. Denny } 4413c5d267eSJoel E. Denny } 4423c5d267eSJoel E. Denny } 4433c5d267eSJoel E. Denny } 4443c5d267eSJoel E. Denny 445bce8fcedSJoel E. Denny static unsigned FindInputLineInFilter( 446*9fd4b5faSJoel E. Denny DumpInputFilterValue DumpInputFilter, unsigned CurInputLine, 447bce8fcedSJoel E. Denny const std::vector<InputAnnotation>::iterator &AnnotationBeg, 448bce8fcedSJoel E. Denny const std::vector<InputAnnotation>::iterator &AnnotationEnd) { 449*9fd4b5faSJoel E. Denny if (DumpInputFilter == DumpInputFilterAll) 450bce8fcedSJoel E. Denny return CurInputLine; 451bce8fcedSJoel E. Denny for (auto AnnotationItr = AnnotationBeg; AnnotationItr != AnnotationEnd; 452bce8fcedSJoel E. Denny ++AnnotationItr) { 453*9fd4b5faSJoel E. Denny switch (DumpInputFilter) { 454*9fd4b5faSJoel E. Denny case DumpInputFilterAll: 455*9fd4b5faSJoel E. Denny llvm_unreachable("unexpected DumpInputFilterAll"); 456*9fd4b5faSJoel E. Denny break; 457*9fd4b5faSJoel E. Denny case DumpInputFilterAnnotationFull: 458*9fd4b5faSJoel E. Denny return AnnotationItr->InputLine; 459*9fd4b5faSJoel E. Denny case DumpInputFilterAnnotation: 460*9fd4b5faSJoel E. Denny if (AnnotationItr->IsFirstLine) 461*9fd4b5faSJoel E. Denny return AnnotationItr->InputLine; 462*9fd4b5faSJoel E. Denny break; 463*9fd4b5faSJoel E. Denny case DumpInputFilterError: 464bce8fcedSJoel E. Denny if (AnnotationItr->IsFirstLine && AnnotationItr->Marker.FiltersAsError) 465bce8fcedSJoel E. Denny return AnnotationItr->InputLine; 466*9fd4b5faSJoel E. Denny break; 467*9fd4b5faSJoel E. Denny } 468bce8fcedSJoel E. Denny } 469bce8fcedSJoel E. Denny return UINT_MAX; 470bce8fcedSJoel E. Denny } 471bce8fcedSJoel E. Denny 47277b6ddf1SJoel E. Denny /// To OS, print a vertical ellipsis (right-justified at LabelWidth) if it would 47377b6ddf1SJoel E. Denny /// occupy less lines than ElidedLines, but print ElidedLines otherwise. Either 47477b6ddf1SJoel E. Denny /// way, clear ElidedLines. Thus, if ElidedLines is empty, do nothing. 47577b6ddf1SJoel E. Denny static void DumpEllipsisOrElidedLines(raw_ostream &OS, std::string &ElidedLines, 47677b6ddf1SJoel E. Denny unsigned LabelWidth) { 47777b6ddf1SJoel E. Denny if (ElidedLines.empty()) 47877b6ddf1SJoel E. Denny return; 47977b6ddf1SJoel E. Denny unsigned EllipsisLines = 3; 48077b6ddf1SJoel E. Denny if (EllipsisLines < StringRef(ElidedLines).count('\n')) { 48177b6ddf1SJoel E. Denny for (unsigned i = 0; i < EllipsisLines; ++i) { 48277b6ddf1SJoel E. Denny WithColor(OS, raw_ostream::BLACK, /*Bold=*/true) 48377b6ddf1SJoel E. Denny << right_justify(".", LabelWidth); 48477b6ddf1SJoel E. Denny OS << '\n'; 48577b6ddf1SJoel E. Denny } 48677b6ddf1SJoel E. Denny } else 48777b6ddf1SJoel E. Denny OS << ElidedLines; 48877b6ddf1SJoel E. Denny ElidedLines.clear(); 48977b6ddf1SJoel E. Denny } 49077b6ddf1SJoel E. Denny 4917df86967SJoel E. Denny static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req, 492*9fd4b5faSJoel E. Denny DumpInputFilterValue DumpInputFilter, 493bce8fcedSJoel E. Denny unsigned DumpInputContext, 4947df86967SJoel E. Denny StringRef InputFileText, 4957df86967SJoel E. Denny std::vector<InputAnnotation> &Annotations, 4967df86967SJoel E. Denny unsigned LabelWidth) { 497bce8fcedSJoel E. Denny OS << "Input was:\n<<<<<<\n"; 4983c5d267eSJoel E. Denny 4993c5d267eSJoel E. Denny // Sort annotations. 5003c5d267eSJoel E. Denny std::sort(Annotations.begin(), Annotations.end(), 5013c5d267eSJoel E. Denny [](const InputAnnotation &A, const InputAnnotation &B) { 502ce685455SJoel E. Denny // 1. Sort annotations in the order of the input lines. 503ce685455SJoel E. Denny // 504ce685455SJoel E. Denny // This makes it easier to find relevant annotations while 505ce685455SJoel E. Denny // iterating input lines in the implementation below. FileCheck 506ce685455SJoel E. Denny // does not always produce diagnostics in the order of input 507ce685455SJoel E. Denny // lines due to, for example, CHECK-DAG and CHECK-NOT. 5083c5d267eSJoel E. Denny if (A.InputLine != B.InputLine) 5093c5d267eSJoel E. Denny return A.InputLine < B.InputLine; 510ce685455SJoel E. Denny // 2. Sort annotations in the temporal order FileCheck produced 511ce685455SJoel E. Denny // their associated diagnostics. 512ce685455SJoel E. Denny // 513ce685455SJoel E. Denny // This sort offers several benefits: 514ce685455SJoel E. Denny // 515ce685455SJoel E. Denny // A. On a single input line, the order of annotations reflects 516ce685455SJoel E. Denny // the FileCheck logic for processing directives/patterns. 517ce685455SJoel E. Denny // This can be helpful in understanding cases in which the 518ce685455SJoel E. Denny // order of the associated directives/patterns in the check 519ce685455SJoel E. Denny // file or on the command line either (i) does not match the 520ce685455SJoel E. Denny // temporal order in which FileCheck looks for matches for the 521ce685455SJoel E. Denny // directives/patterns (due to, for example, CHECK-LABEL, 522ce685455SJoel E. Denny // CHECK-NOT, or `--implicit-check-not`) or (ii) does match 523ce685455SJoel E. Denny // that order but does not match the order of those 524ce685455SJoel E. Denny // diagnostics along an input line (due to, for example, 525ce685455SJoel E. Denny // CHECK-DAG). 526ce685455SJoel E. Denny // 527ce685455SJoel E. Denny // On the other hand, because our presentation format presents 528ce685455SJoel E. Denny // input lines in order, there's no clear way to offer the 529ce685455SJoel E. Denny // same benefit across input lines. For consistency, it might 530ce685455SJoel E. Denny // then seem worthwhile to have annotations on a single line 531ce685455SJoel E. Denny // also sorted in input order (that is, by input column). 532ce685455SJoel E. Denny // However, in practice, this appears to be more confusing 533ce685455SJoel E. Denny // than helpful. Perhaps it's intuitive to expect annotations 534ce685455SJoel E. Denny // to be listed in the temporal order in which they were 535ce685455SJoel E. Denny // produced except in cases the presentation format obviously 536ce685455SJoel E. Denny // and inherently cannot support it (that is, across input 537ce685455SJoel E. Denny // lines). 538ce685455SJoel E. Denny // 539ce685455SJoel E. Denny // B. When diagnostics' annotations are split among multiple 540ce685455SJoel E. Denny // input lines, the user must track them from one input line 541ce685455SJoel E. Denny // to the next. One property of the sort chosen here is that 542ce685455SJoel E. Denny // it facilitates the user in this regard by ensuring the 543ce685455SJoel E. Denny // following: when comparing any two input lines, a 544ce685455SJoel E. Denny // diagnostic's annotations are sorted in the same position 545ce685455SJoel E. Denny // relative to all other diagnostics' annotations. 546ce685455SJoel E. Denny return A.DiagIndex < B.DiagIndex; 5473c5d267eSJoel E. Denny }); 5483c5d267eSJoel E. Denny 5493c5d267eSJoel E. Denny // Compute the width of the label column. 5503c5d267eSJoel E. Denny const unsigned char *InputFilePtr = InputFileText.bytes_begin(), 5513c5d267eSJoel E. Denny *InputFileEnd = InputFileText.bytes_end(); 5523c5d267eSJoel E. Denny unsigned LineCount = InputFileText.count('\n'); 5533c5d267eSJoel E. Denny if (InputFileEnd[-1] != '\n') 5543c5d267eSJoel E. Denny ++LineCount; 555010982f7SRainer Orth unsigned LineNoWidth = std::log10(LineCount) + 1; 5563c5d267eSJoel E. Denny // +3 below adds spaces (1) to the left of the (right-aligned) line numbers 5573c5d267eSJoel E. Denny // on input lines and (2) to the right of the (left-aligned) labels on 5583c5d267eSJoel E. Denny // annotation lines so that input lines and annotation lines are more 5593c5d267eSJoel E. Denny // visually distinct. For example, the spaces on the annotation lines ensure 5603c5d267eSJoel E. Denny // that input line numbers and check directive line numbers never align 5613c5d267eSJoel E. Denny // horizontally. Those line numbers might not even be for the same file. 5623c5d267eSJoel E. Denny // One space would be enough to achieve that, but more makes it even easier 5633c5d267eSJoel E. Denny // to see. 5643c5d267eSJoel E. Denny LabelWidth = std::max(LabelWidth, LineNoWidth) + 3; 5653c5d267eSJoel E. Denny 5663c5d267eSJoel E. Denny // Print annotated input lines. 567bce8fcedSJoel E. Denny unsigned PrevLineInFilter = 0; // 0 means none so far 568bce8fcedSJoel E. Denny unsigned NextLineInFilter = 0; // 0 means uncomputed, UINT_MAX means none 56977b6ddf1SJoel E. Denny std::string ElidedLines; 57077b6ddf1SJoel E. Denny raw_string_ostream ElidedLinesOS(ElidedLines); 57177b6ddf1SJoel E. Denny ColorMode TheColorMode = 57277b6ddf1SJoel E. Denny WithColor(OS).colorsEnabled() ? ColorMode::Enable : ColorMode::Disable; 57377b6ddf1SJoel E. Denny if (TheColorMode == ColorMode::Enable) 57477b6ddf1SJoel E. Denny ElidedLinesOS.enable_colors(true); 5753c5d267eSJoel E. Denny auto AnnotationItr = Annotations.begin(), AnnotationEnd = Annotations.end(); 5763c5d267eSJoel E. Denny for (unsigned Line = 1; 5773c5d267eSJoel E. Denny InputFilePtr != InputFileEnd || AnnotationItr != AnnotationEnd; 5783c5d267eSJoel E. Denny ++Line) { 5793c5d267eSJoel E. Denny const unsigned char *InputFileLine = InputFilePtr; 5803c5d267eSJoel E. Denny 581bce8fcedSJoel E. Denny // Compute the previous and next line included by the filter. 582bce8fcedSJoel E. Denny if (NextLineInFilter < Line) 583*9fd4b5faSJoel E. Denny NextLineInFilter = FindInputLineInFilter(DumpInputFilter, Line, 584bce8fcedSJoel E. Denny AnnotationItr, AnnotationEnd); 585bce8fcedSJoel E. Denny assert(NextLineInFilter && "expected NextLineInFilter to be computed"); 586bce8fcedSJoel E. Denny if (NextLineInFilter == Line) 587bce8fcedSJoel E. Denny PrevLineInFilter = Line; 588bce8fcedSJoel E. Denny 589bce8fcedSJoel E. Denny // Elide this input line and its annotations if it's not within the 590bce8fcedSJoel E. Denny // context specified by -dump-input-context of an input line included by 591*9fd4b5faSJoel E. Denny // -dump-input-filter. However, in case the resulting ellipsis would occupy 59277b6ddf1SJoel E. Denny // more lines than the input lines and annotations it elides, buffer the 59377b6ddf1SJoel E. Denny // elided lines and annotations so we can print them instead. 59477b6ddf1SJoel E. Denny raw_ostream *LineOS = &OS; 595bce8fcedSJoel E. Denny if ((!PrevLineInFilter || PrevLineInFilter + DumpInputContext < Line) && 596bce8fcedSJoel E. Denny (NextLineInFilter == UINT_MAX || 59777b6ddf1SJoel E. Denny Line + DumpInputContext < NextLineInFilter)) 59877b6ddf1SJoel E. Denny LineOS = &ElidedLinesOS; 59977b6ddf1SJoel E. Denny else { 60077b6ddf1SJoel E. Denny LineOS = &OS; 60177b6ddf1SJoel E. Denny DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth); 602bce8fcedSJoel E. Denny } 603bce8fcedSJoel E. Denny 6043c5d267eSJoel E. Denny // Print right-aligned line number. 60577b6ddf1SJoel E. Denny WithColor(*LineOS, raw_ostream::BLACK, /*Bold=*/true, /*BF=*/false, 60677b6ddf1SJoel E. Denny TheColorMode) 6073c5d267eSJoel E. Denny << format_decimal(Line, LabelWidth) << ": "; 6083c5d267eSJoel E. Denny 609e2afb614SJoel E. Denny // For the case where -v and colors are enabled, find the annotations for 610e2afb614SJoel E. Denny // good matches for expected patterns in order to highlight everything 611e2afb614SJoel E. Denny // else in the line. There are no such annotations if -v is disabled. 612e2afb614SJoel E. Denny std::vector<InputAnnotation> FoundAndExpectedMatches; 61377b6ddf1SJoel E. Denny if (Req.Verbose && TheColorMode == ColorMode::Enable) { 6147df86967SJoel E. Denny for (auto I = AnnotationItr; I != AnnotationEnd && I->InputLine == Line; 6157df86967SJoel E. Denny ++I) { 616e2afb614SJoel E. Denny if (I->FoundAndExpectedMatch) 617e2afb614SJoel E. Denny FoundAndExpectedMatches.push_back(*I); 6187df86967SJoel E. Denny } 6197df86967SJoel E. Denny } 6207df86967SJoel E. Denny 6217df86967SJoel E. Denny // Print numbered line with highlighting where there are no matches for 6227df86967SJoel E. Denny // expected patterns. 6233c5d267eSJoel E. Denny bool Newline = false; 6247df86967SJoel E. Denny { 62577b6ddf1SJoel E. Denny WithColor COS(*LineOS, raw_ostream::SAVEDCOLOR, /*Bold=*/false, 62677b6ddf1SJoel E. Denny /*BG=*/false, TheColorMode); 6277df86967SJoel E. Denny bool InMatch = false; 6287df86967SJoel E. Denny if (Req.Verbose) 6297df86967SJoel E. Denny COS.changeColor(raw_ostream::CYAN, true, true); 6307df86967SJoel E. Denny for (unsigned Col = 1; InputFilePtr != InputFileEnd && !Newline; ++Col) { 6317df86967SJoel E. Denny bool WasInMatch = InMatch; 6327df86967SJoel E. Denny InMatch = false; 633e2afb614SJoel E. Denny for (auto M : FoundAndExpectedMatches) { 6347df86967SJoel E. Denny if (M.InputStartCol <= Col && Col < M.InputEndCol) { 6357df86967SJoel E. Denny InMatch = true; 6367df86967SJoel E. Denny break; 6377df86967SJoel E. Denny } 6387df86967SJoel E. Denny } 6397df86967SJoel E. Denny if (!WasInMatch && InMatch) 6407df86967SJoel E. Denny COS.resetColor(); 6417df86967SJoel E. Denny else if (WasInMatch && !InMatch) 6427df86967SJoel E. Denny COS.changeColor(raw_ostream::CYAN, true, true); 6433c5d267eSJoel E. Denny if (*InputFilePtr == '\n') 6443c5d267eSJoel E. Denny Newline = true; 6453c5d267eSJoel E. Denny else 6467df86967SJoel E. Denny COS << *InputFilePtr; 6473c5d267eSJoel E. Denny ++InputFilePtr; 6483c5d267eSJoel E. Denny } 6497df86967SJoel E. Denny } 65077b6ddf1SJoel E. Denny *LineOS << '\n'; 6513c5d267eSJoel E. Denny unsigned InputLineWidth = InputFilePtr - InputFileLine - Newline; 6523c5d267eSJoel E. Denny 6533c5d267eSJoel E. Denny // Print any annotations. 6543c5d267eSJoel E. Denny while (AnnotationItr != AnnotationEnd && 6553c5d267eSJoel E. Denny AnnotationItr->InputLine == Line) { 65677b6ddf1SJoel E. Denny WithColor COS(*LineOS, AnnotationItr->Marker.Color, /*Bold=*/true, 65777b6ddf1SJoel E. Denny /*BG=*/false, TheColorMode); 6583c5d267eSJoel E. Denny // The two spaces below are where the ": " appears on input lines. 6593c5d267eSJoel E. Denny COS << left_justify(AnnotationItr->Label, LabelWidth) << " "; 6603c5d267eSJoel E. Denny unsigned Col; 6613c5d267eSJoel E. Denny for (Col = 1; Col < AnnotationItr->InputStartCol; ++Col) 6623c5d267eSJoel E. Denny COS << ' '; 6633c5d267eSJoel E. Denny COS << AnnotationItr->Marker.Lead; 6643c5d267eSJoel E. Denny // If InputEndCol=UINT_MAX, stop at InputLineWidth. 6653c5d267eSJoel E. Denny for (++Col; Col < AnnotationItr->InputEndCol && Col <= InputLineWidth; 6663c5d267eSJoel E. Denny ++Col) 6673c5d267eSJoel E. Denny COS << '~'; 6683c5d267eSJoel E. Denny const std::string &Note = AnnotationItr->Marker.Note; 6693c5d267eSJoel E. Denny if (!Note.empty()) { 6703c5d267eSJoel E. Denny // Put the note at the end of the input line. If we were to instead 6713c5d267eSJoel E. Denny // put the note right after the marker, subsequent annotations for the 6723c5d267eSJoel E. Denny // same input line might appear to mark this note instead of the input 6733c5d267eSJoel E. Denny // line. 6743c5d267eSJoel E. Denny for (; Col <= InputLineWidth; ++Col) 6753c5d267eSJoel E. Denny COS << ' '; 6763c5d267eSJoel E. Denny COS << ' ' << Note; 6773c5d267eSJoel E. Denny } 6783c5d267eSJoel E. Denny COS << '\n'; 6793c5d267eSJoel E. Denny ++AnnotationItr; 6803c5d267eSJoel E. Denny } 6813c5d267eSJoel E. Denny } 68277b6ddf1SJoel E. Denny DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth); 6833c5d267eSJoel E. Denny 6843c5d267eSJoel E. Denny OS << ">>>>>>\n"; 6853c5d267eSJoel E. Denny } 6863c5d267eSJoel E. Denny 687ee3c74fbSChris Lattner int main(int argc, char **argv) { 6883e66509fSJoel E. Denny // Enable use of ANSI color codes because FileCheck is using them to 6893e66509fSJoel E. Denny // highlight text. 6903e66509fSJoel E. Denny llvm::sys::Process::UseANSIEscapeCodes(true); 6913e66509fSJoel E. Denny 692197194b6SRui Ueyama InitLLVM X(argc, argv); 69324994d77SJoel E. Denny cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr, 69424994d77SJoel E. Denny "FILECHECK_OPTS"); 695bce8fcedSJoel E. Denny 696bce8fcedSJoel E. Denny // Select -dump-input* values. The -help documentation specifies the default 697bce8fcedSJoel E. Denny // value and which value to choose if an option is specified multiple times. 698bce8fcedSJoel E. Denny // In the latter case, the general rule of thumb is to choose the value that 699bce8fcedSJoel E. Denny // provides the most information. 700fdde18a7SJoel E. Denny DumpInputValue DumpInput = 701fdde18a7SJoel E. Denny DumpInputs.empty() 7023b83501cSJoel E. Denny ? DumpInputFail 703fdde18a7SJoel E. Denny : *std::max_element(DumpInputs.begin(), DumpInputs.end()); 704*9fd4b5faSJoel E. Denny DumpInputFilterValue DumpInputFilter; 705*9fd4b5faSJoel E. Denny if (DumpInputFilters.empty()) 706*9fd4b5faSJoel E. Denny DumpInputFilter = DumpInput == DumpInputAlways ? DumpInputFilterAll 707*9fd4b5faSJoel E. Denny : DumpInputFilterError; 708*9fd4b5faSJoel E. Denny else 709*9fd4b5faSJoel E. Denny DumpInputFilter = 710*9fd4b5faSJoel E. Denny *std::max_element(DumpInputFilters.begin(), DumpInputFilters.end()); 711bce8fcedSJoel E. Denny unsigned DumpInputContext = DumpInputContexts.empty() 712bce8fcedSJoel E. Denny ? 5 713bce8fcedSJoel E. Denny : *std::max_element(DumpInputContexts.begin(), 714bce8fcedSJoel E. Denny DumpInputContexts.end()); 715bce8fcedSJoel E. Denny 7163c5d267eSJoel E. Denny if (DumpInput == DumpInputHelp) { 7173c5d267eSJoel E. Denny DumpInputAnnotationHelp(outs()); 7183c5d267eSJoel E. Denny return 0; 7193c5d267eSJoel E. Denny } 7203c5d267eSJoel E. Denny if (CheckFilename.empty()) { 7213c5d267eSJoel E. Denny errs() << "<check-file> not specified\n"; 7223c5d267eSJoel E. Denny return 2; 7233c5d267eSJoel E. Denny } 724ee3c74fbSChris Lattner 725ffa9d2e4SAditya Nandakumar FileCheckRequest Req; 72676e0ab23SGeorgii Rymar for (StringRef Prefix : CheckPrefixes) 727ffa9d2e4SAditya Nandakumar Req.CheckPrefixes.push_back(Prefix); 728ffa9d2e4SAditya Nandakumar 729a1fd1882SJoel E. Denny for (StringRef Prefix : CommentPrefixes) 730a1fd1882SJoel E. Denny Req.CommentPrefixes.push_back(Prefix); 731a1fd1882SJoel E. Denny 73276e0ab23SGeorgii Rymar for (StringRef CheckNot : ImplicitCheckNot) 733ffa9d2e4SAditya Nandakumar Req.ImplicitCheckNot.push_back(CheckNot); 734ffa9d2e4SAditya Nandakumar 735a5e233bfSThomas Preud'homme bool GlobalDefineError = false; 73676e0ab23SGeorgii Rymar for (StringRef G : GlobalDefines) { 737a5e233bfSThomas Preud'homme size_t EqIdx = G.find('='); 738a5e233bfSThomas Preud'homme if (EqIdx == std::string::npos) { 739a5e233bfSThomas Preud'homme errs() << "Missing equal sign in command-line definition '-D" << G 740a5e233bfSThomas Preud'homme << "'\n"; 741a5e233bfSThomas Preud'homme GlobalDefineError = true; 742a5e233bfSThomas Preud'homme continue; 743a5e233bfSThomas Preud'homme } 744a5e233bfSThomas Preud'homme if (EqIdx == 0) { 7451a944d27SThomas Preud'homme errs() << "Missing variable name in command-line definition '-D" << G 7461a944d27SThomas Preud'homme << "'\n"; 747a5e233bfSThomas Preud'homme GlobalDefineError = true; 748a5e233bfSThomas Preud'homme continue; 749a5e233bfSThomas Preud'homme } 750ffa9d2e4SAditya Nandakumar Req.GlobalDefines.push_back(G); 751a5e233bfSThomas Preud'homme } 752a5e233bfSThomas Preud'homme if (GlobalDefineError) 753a5e233bfSThomas Preud'homme return 2; 754ffa9d2e4SAditya Nandakumar 755ffa9d2e4SAditya Nandakumar Req.AllowEmptyInput = AllowEmptyInput; 756ffa9d2e4SAditya Nandakumar Req.EnableVarScope = EnableVarScope; 757ffa9d2e4SAditya Nandakumar Req.AllowDeprecatedDagOverlap = AllowDeprecatedDagOverlap; 758ffa9d2e4SAditya Nandakumar Req.Verbose = Verbose; 759ffa9d2e4SAditya Nandakumar Req.VerboseVerbose = VerboseVerbose; 760ffa9d2e4SAditya Nandakumar Req.NoCanonicalizeWhiteSpace = NoCanonicalizeWhiteSpace; 761ffa9d2e4SAditya Nandakumar Req.MatchFullLines = MatchFullLines; 7625b5b2fd2SKai Nacke Req.IgnoreCase = IgnoreCase; 763ffa9d2e4SAditya Nandakumar 764ffa9d2e4SAditya Nandakumar if (VerboseVerbose) 765ffa9d2e4SAditya Nandakumar Req.Verbose = true; 766ffa9d2e4SAditya Nandakumar 767ffa9d2e4SAditya Nandakumar FileCheck FC(Req); 7682aa0217aSJoel E. Denny if (!FC.ValidateCheckPrefixes()) 769c2735158SRui Ueyama return 2; 770c2735158SRui Ueyama 771ffa9d2e4SAditya Nandakumar Regex PrefixRE = FC.buildCheckPrefixRegex(); 772726774cbSChandler Carruth std::string REError; 773726774cbSChandler Carruth if (!PrefixRE.isValid(REError)) { 774726774cbSChandler Carruth errs() << "Unable to combine check-prefix strings into a prefix regular " 775726774cbSChandler Carruth "expression! This is likely a bug in FileCheck's verification of " 776726774cbSChandler Carruth "the check-prefix strings. Regular expression parsing failed " 777726774cbSChandler Carruth "with the following error: " 778726774cbSChandler Carruth << REError << "\n"; 779726774cbSChandler Carruth return 2; 780726774cbSChandler Carruth } 78113df4626SMatt Arsenault 782ee3c74fbSChris Lattner SourceMgr SM; 783ee3c74fbSChris Lattner 784ee3c74fbSChris Lattner // Read the expected strings from the check file. 78520247900SChandler Carruth ErrorOr<std::unique_ptr<MemoryBuffer>> CheckFileOrErr = 78620247900SChandler Carruth MemoryBuffer::getFileOrSTDIN(CheckFilename); 78720247900SChandler Carruth if (std::error_code EC = CheckFileOrErr.getError()) { 78820247900SChandler Carruth errs() << "Could not open check file '" << CheckFilename 78920247900SChandler Carruth << "': " << EC.message() << '\n'; 79020247900SChandler Carruth return 2; 79120247900SChandler Carruth } 79220247900SChandler Carruth MemoryBuffer &CheckFile = *CheckFileOrErr.get(); 79320247900SChandler Carruth 79420247900SChandler Carruth SmallString<4096> CheckFileBuffer; 795ffa9d2e4SAditya Nandakumar StringRef CheckFileText = FC.CanonicalizeFile(CheckFile, CheckFileBuffer); 79620247900SChandler Carruth 797b5a24610SJoel E. Denny unsigned CheckFileBufferID = 79820247900SChandler Carruth SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 79920247900SChandler Carruth CheckFileText, CheckFile.getBufferIdentifier()), 80020247900SChandler Carruth SMLoc()); 80120247900SChandler Carruth 802b5a24610SJoel E. Denny std::pair<unsigned, unsigned> ImpPatBufferIDRange; 803b5a24610SJoel E. Denny if (FC.readCheckFile(SM, CheckFileText, PrefixRE, &ImpPatBufferIDRange)) 804ee3c74fbSChris Lattner return 2; 805ee3c74fbSChris Lattner 806ee3c74fbSChris Lattner // Open the file to check and add it to SourceMgr. 80720247900SChandler Carruth ErrorOr<std::unique_ptr<MemoryBuffer>> InputFileOrErr = 808adf21f2aSRafael Espindola MemoryBuffer::getFileOrSTDIN(InputFilename); 8096e01cd67SDavid Bozier if (InputFilename == "-") 8106e01cd67SDavid Bozier InputFilename = "<stdin>"; // Overwrite for improved diagnostic messages 81120247900SChandler Carruth if (std::error_code EC = InputFileOrErr.getError()) { 812adf21f2aSRafael Espindola errs() << "Could not open input file '" << InputFilename 813adf21f2aSRafael Espindola << "': " << EC.message() << '\n'; 8148e1c6477SEli Bendersky return 2; 815ee3c74fbSChris Lattner } 81620247900SChandler Carruth MemoryBuffer &InputFile = *InputFileOrErr.get(); 8172c3e5cdfSChris Lattner 81820247900SChandler Carruth if (InputFile.getBufferSize() == 0 && !AllowEmptyInput) { 819b692bed7SChris Lattner errs() << "FileCheck error: '" << InputFilename << "' is empty.\n"; 8202bd4f8b6SXinliang David Li DumpCommandLine(argc, argv); 8218e1c6477SEli Bendersky return 2; 822b692bed7SChris Lattner } 823b692bed7SChris Lattner 82420247900SChandler Carruth SmallString<4096> InputFileBuffer; 825ffa9d2e4SAditya Nandakumar StringRef InputFileText = FC.CanonicalizeFile(InputFile, InputFileBuffer); 8262c3e5cdfSChris Lattner 827e8f2fb20SChandler Carruth SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 828e8f2fb20SChandler Carruth InputFileText, InputFile.getBufferIdentifier()), 829e8f2fb20SChandler Carruth SMLoc()); 830ee3c74fbSChris Lattner 8313c5d267eSJoel E. Denny std::vector<FileCheckDiag> Diags; 83202ada9bdSThomas Preud'homme int ExitCode = FC.checkInput(SM, InputFileText, 8333c5d267eSJoel E. Denny DumpInput == DumpInputNever ? nullptr : &Diags) 8343c5d267eSJoel E. Denny ? EXIT_SUCCESS 8353c5d267eSJoel E. Denny : 1; 8363c5d267eSJoel E. Denny if (DumpInput == DumpInputAlways || 8373c5d267eSJoel E. Denny (ExitCode == 1 && DumpInput == DumpInputFail)) { 8383c5d267eSJoel E. Denny errs() << "\n" 839839f8e4fSJoel E. Denny << "Input file: " << InputFilename << "\n" 8403c5d267eSJoel E. Denny << "Check file: " << CheckFilename << "\n" 8413c5d267eSJoel E. Denny << "\n" 842839f8e4fSJoel E. Denny << "-dump-input=help explains the following input dump.\n" 8433c5d267eSJoel E. Denny << "\n"; 8443c5d267eSJoel E. Denny std::vector<InputAnnotation> Annotations; 8453c5d267eSJoel E. Denny unsigned LabelWidth; 846b5a24610SJoel E. Denny BuildInputAnnotations(SM, CheckFileBufferID, ImpPatBufferIDRange, Diags, 847b5a24610SJoel E. Denny Annotations, LabelWidth); 848*9fd4b5faSJoel E. Denny DumpAnnotatedInput(errs(), Req, DumpInputFilter, DumpInputContext, 849bce8fcedSJoel E. Denny InputFileText, Annotations, LabelWidth); 8503c5d267eSJoel E. Denny } 851346dfbe2SGeorge Karpenkov 852346dfbe2SGeorge Karpenkov return ExitCode; 853ee3c74fbSChris Lattner } 854