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 131bce8fcedSJoel E. Denny static cl::list<unsigned> DumpInputContexts( 132bce8fcedSJoel E. Denny "dump-input-context", cl::value_desc("N"), 133bce8fcedSJoel E. Denny cl::desc("In the dump requested by -dump-input=fail, print <N> input\n" 134bce8fcedSJoel E. Denny "lines before and <N> input lines after the starting line of\n" 135bce8fcedSJoel E. Denny "any error diagnostic. When there are multiple occurrences of\n" 136bce8fcedSJoel E. Denny "this option, the largest specified <N> has precedence. The\n" 137bce8fcedSJoel E. Denny "default is 5.\n")); 138bce8fcedSJoel E. Denny 13913df4626SMatt Arsenault typedef cl::list<std::string>::const_iterator prefix_iterator; 14013df4626SMatt Arsenault 14113df4626SMatt Arsenault 14213df4626SMatt Arsenault 143726774cbSChandler Carruth 144726774cbSChandler Carruth 145726774cbSChandler Carruth 146c2735158SRui Ueyama 1472bd4f8b6SXinliang David Li static void DumpCommandLine(int argc, char **argv) { 1482bd4f8b6SXinliang David Li errs() << "FileCheck command line: "; 1492bd4f8b6SXinliang David Li for (int I = 0; I < argc; I++) 1502bd4f8b6SXinliang David Li errs() << " " << argv[I]; 1512bd4f8b6SXinliang David Li errs() << "\n"; 1522bd4f8b6SXinliang David Li } 1532bd4f8b6SXinliang David Li 1543c5d267eSJoel E. Denny struct MarkerStyle { 1553c5d267eSJoel E. Denny /// The starting char (before tildes) for marking the line. 1563c5d267eSJoel E. Denny char Lead; 1573c5d267eSJoel E. Denny /// What color to use for this annotation. 1584d41c332SRui Ueyama raw_ostream::Colors Color; 1593c5d267eSJoel E. Denny /// A note to follow the marker, or empty string if none. 1603c5d267eSJoel E. Denny std::string Note; 161bce8fcedSJoel E. Denny /// Does this marker indicate inclusion by the input filter implied by 162bce8fcedSJoel E. Denny /// -dump-input=fail? 163bce8fcedSJoel E. Denny bool FiltersAsError; 1643c5d267eSJoel E. Denny MarkerStyle() {} 1654d41c332SRui Ueyama MarkerStyle(char Lead, raw_ostream::Colors Color, 166bce8fcedSJoel E. Denny const std::string &Note = "", bool FiltersAsError = false) 167bce8fcedSJoel E. Denny : Lead(Lead), Color(Color), Note(Note), FiltersAsError(FiltersAsError) { 168bce8fcedSJoel E. Denny assert((!FiltersAsError || !Note.empty()) && 169bce8fcedSJoel E. Denny "expected error diagnostic to have note"); 170bce8fcedSJoel E. Denny } 1713c5d267eSJoel E. Denny }; 1723c5d267eSJoel E. Denny 1733c5d267eSJoel E. Denny static MarkerStyle GetMarker(FileCheckDiag::MatchType MatchTy) { 1743c5d267eSJoel E. Denny switch (MatchTy) { 175e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundAndExpected: 1767df86967SJoel E. Denny return MarkerStyle('^', raw_ostream::GREEN); 177e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundButExcluded: 178bce8fcedSJoel E. Denny return MarkerStyle('!', raw_ostream::RED, "error: no match expected", 179bce8fcedSJoel E. Denny /*FiltersAsError=*/true); 180e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundButWrongLine: 181bce8fcedSJoel E. Denny return MarkerStyle('!', raw_ostream::RED, "error: match on wrong line", 182bce8fcedSJoel E. Denny /*FiltersAsError=*/true); 183e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundButDiscarded: 184f7c1c4d8SJoel E. Denny return MarkerStyle('!', raw_ostream::CYAN, 185f7c1c4d8SJoel E. Denny "discard: overlaps earlier match"); 18696f0e84cSJoel E. Denny case FileCheckDiag::MatchNoneAndExcluded: 18796f0e84cSJoel E. Denny return MarkerStyle('X', raw_ostream::GREEN); 1883c5d267eSJoel E. Denny case FileCheckDiag::MatchNoneButExpected: 189bce8fcedSJoel E. Denny return MarkerStyle('X', raw_ostream::RED, "error: no match found", 190bce8fcedSJoel E. Denny /*FiltersAsError=*/true); 1912c007c80SJoel E. Denny case FileCheckDiag::MatchFuzzy: 192bce8fcedSJoel E. Denny return MarkerStyle('?', raw_ostream::MAGENTA, "possible intended match", 193bce8fcedSJoel E. Denny /*FiltersAsError=*/true); 1943c5d267eSJoel E. Denny } 1953c5d267eSJoel E. Denny llvm_unreachable_internal("unexpected match type"); 1963c5d267eSJoel E. Denny } 1973c5d267eSJoel E. Denny 1983c5d267eSJoel E. Denny static void DumpInputAnnotationHelp(raw_ostream &OS) { 1993c5d267eSJoel E. Denny OS << "The following description was requested by -dump-input=help to\n" 200839f8e4fSJoel E. Denny << "explain the input dump printed by FileCheck.\n" 201839f8e4fSJoel E. Denny << "\n" 202839f8e4fSJoel E. Denny << "Related command-line options:\n" 203839f8e4fSJoel E. Denny << " - -dump-input=<value> enables or disables the input dump\n" 204bce8fcedSJoel E. Denny << " - -dump-input-context=<N> adjusts the context of errors\n" 205839f8e4fSJoel E. Denny << " - -v and -vv add more annotations\n" 206839f8e4fSJoel E. Denny << " - -color forces colors to be enabled both in the dump and below\n" 207839f8e4fSJoel E. Denny << " - -help documents the above options in more detail\n" 208839f8e4fSJoel E. Denny << "\n" 209839f8e4fSJoel E. Denny << "Input dump annotation format:\n"; 2103c5d267eSJoel E. Denny 2113c5d267eSJoel E. Denny // Labels for input lines. 2123c5d267eSJoel E. Denny OS << " - "; 2133c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "L:"; 2143c5d267eSJoel E. Denny OS << " labels line number L of the input file\n"; 2153c5d267eSJoel E. Denny 2163c5d267eSJoel E. Denny // Labels for annotation lines. 2173c5d267eSJoel E. Denny OS << " - "; 2183c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L"; 219b5a24610SJoel E. Denny OS << " labels the only match result for either (1) a pattern of type T" 220b5a24610SJoel E. Denny << " from\n" 221b5a24610SJoel E. Denny << " line L of the check file if L is an integer or (2) the" 222b5a24610SJoel E. Denny << " I-th implicit\n" 223b5a24610SJoel E. Denny << " pattern if L is \"imp\" followed by an integer " 224b5a24610SJoel E. Denny << "I (index origin one)\n"; 2252c007c80SJoel E. Denny OS << " - "; 2262c007c80SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L'N"; 227b5a24610SJoel E. Denny OS << " labels the Nth match result for such a pattern\n"; 2283c5d267eSJoel E. Denny 2293c5d267eSJoel E. Denny // Markers on annotation lines. 2303c5d267eSJoel E. Denny OS << " - "; 2317df86967SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "^~~"; 2327df86967SJoel E. Denny OS << " marks good match (reported if -v)\n" 2337df86967SJoel E. Denny << " - "; 234cadfcef4SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "!~~"; 235cadfcef4SJoel E. Denny OS << " marks bad match, such as:\n" 236cadfcef4SJoel E. Denny << " - CHECK-NEXT on same line as previous match (error)\n" 2370e7e3fa0SJoel E. Denny << " - CHECK-NOT found (error)\n" 238f7c1c4d8SJoel E. Denny << " - CHECK-DAG overlapping match (discarded, reported if " 239f7c1c4d8SJoel E. Denny << "-vv)\n" 240cadfcef4SJoel E. Denny << " - "; 2413c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "X~~"; 242cadfcef4SJoel E. Denny OS << " marks search range when no match is found, such as:\n" 243cadfcef4SJoel E. Denny << " - CHECK-NEXT not found (error)\n" 24496f0e84cSJoel E. Denny << " - CHECK-NOT not found (success, reported if -vv)\n" 245f7c1c4d8SJoel E. Denny << " - CHECK-DAG not found after discarded matches (error)\n" 2462c007c80SJoel E. Denny << " - "; 2472c007c80SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "?"; 2482c007c80SJoel E. Denny OS << " marks fuzzy match when no match is found\n"; 2493c5d267eSJoel E. Denny 250bce8fcedSJoel E. Denny // Elided lines. 251bce8fcedSJoel E. Denny OS << " - "; 252bce8fcedSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "..."; 253bce8fcedSJoel E. Denny OS << " indicates elided input lines and annotations, as specified by\n" 254bce8fcedSJoel E. Denny << " -dump-input=fail and -dump-input-context\n"; 255bce8fcedSJoel E. Denny 2563c5d267eSJoel E. Denny // Colors. 2573c5d267eSJoel E. Denny OS << " - colors "; 2587df86967SJoel E. Denny WithColor(OS, raw_ostream::GREEN, true) << "success"; 2597df86967SJoel E. Denny OS << ", "; 2603c5d267eSJoel E. Denny WithColor(OS, raw_ostream::RED, true) << "error"; 2612c007c80SJoel E. Denny OS << ", "; 2622c007c80SJoel E. Denny WithColor(OS, raw_ostream::MAGENTA, true) << "fuzzy match"; 2637df86967SJoel E. Denny OS << ", "; 264f7c1c4d8SJoel E. Denny WithColor(OS, raw_ostream::CYAN, true, false) << "discarded match"; 265f7c1c4d8SJoel E. Denny OS << ", "; 2667df86967SJoel E. Denny WithColor(OS, raw_ostream::CYAN, true, true) << "unmatched input"; 267839f8e4fSJoel E. Denny OS << "\n"; 2683c5d267eSJoel E. Denny } 2693c5d267eSJoel E. Denny 2703c5d267eSJoel E. Denny /// An annotation for a single input line. 2713c5d267eSJoel E. Denny struct InputAnnotation { 272ce685455SJoel E. Denny /// The index of the match result across all checks 273ce685455SJoel E. Denny unsigned DiagIndex; 2743c5d267eSJoel E. Denny /// The label for this annotation. 2753c5d267eSJoel E. Denny std::string Label; 276bce8fcedSJoel E. Denny /// Is this the initial fragment of a diagnostic that has been broken across 277bce8fcedSJoel E. Denny /// multiple lines? 278bce8fcedSJoel E. Denny bool IsFirstLine; 2793c5d267eSJoel E. Denny /// What input line (one-origin indexing) this annotation marks. This might 280bce8fcedSJoel E. Denny /// be different from the starting line of the original diagnostic if 281bce8fcedSJoel E. Denny /// !IsFirstLine. 2823c5d267eSJoel E. Denny unsigned InputLine; 283ce685455SJoel E. Denny /// The column range (one-origin indexing, open end) in which to mark the 2843c5d267eSJoel E. Denny /// input line. If InputEndCol is UINT_MAX, treat it as the last column 2853c5d267eSJoel E. Denny /// before the newline. 2863c5d267eSJoel E. Denny unsigned InputStartCol, InputEndCol; 2873c5d267eSJoel E. Denny /// The marker to use. 2883c5d267eSJoel E. Denny MarkerStyle Marker; 289e2afb614SJoel E. Denny /// Whether this annotation represents a good match for an expected pattern. 290e2afb614SJoel E. Denny bool FoundAndExpectedMatch; 2913c5d267eSJoel E. Denny }; 2923c5d267eSJoel E. Denny 2933c5d267eSJoel E. Denny /// Get an abbreviation for the check type. 2943c5d267eSJoel E. Denny std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) { 2953c5d267eSJoel E. Denny switch (Ty) { 2963c5d267eSJoel E. Denny case Check::CheckPlain: 2973c5d267eSJoel E. Denny if (Ty.getCount() > 1) 2983c5d267eSJoel E. Denny return "count"; 2993c5d267eSJoel E. Denny return "check"; 3003c5d267eSJoel E. Denny case Check::CheckNext: 3013c5d267eSJoel E. Denny return "next"; 3023c5d267eSJoel E. Denny case Check::CheckSame: 3033c5d267eSJoel E. Denny return "same"; 3043c5d267eSJoel E. Denny case Check::CheckNot: 3053c5d267eSJoel E. Denny return "not"; 3063c5d267eSJoel E. Denny case Check::CheckDAG: 3073c5d267eSJoel E. Denny return "dag"; 3083c5d267eSJoel E. Denny case Check::CheckLabel: 3093c5d267eSJoel E. Denny return "label"; 3103c5d267eSJoel E. Denny case Check::CheckEmpty: 3113c5d267eSJoel E. Denny return "empty"; 312a1fd1882SJoel E. Denny case Check::CheckComment: 313a1fd1882SJoel E. Denny return "com"; 3143c5d267eSJoel E. Denny case Check::CheckEOF: 3153c5d267eSJoel E. Denny return "eof"; 3163c5d267eSJoel E. Denny case Check::CheckBadNot: 3173c5d267eSJoel E. Denny return "bad-not"; 3183c5d267eSJoel E. Denny case Check::CheckBadCount: 3193c5d267eSJoel E. Denny return "bad-count"; 3203c5d267eSJoel E. Denny case Check::CheckNone: 3213c5d267eSJoel E. Denny llvm_unreachable("invalid FileCheckType"); 3223c5d267eSJoel E. Denny } 3233c5d267eSJoel E. Denny llvm_unreachable("unknown FileCheckType"); 3243c5d267eSJoel E. Denny } 3253c5d267eSJoel E. Denny 326b5a24610SJoel E. Denny static void 327b5a24610SJoel E. Denny BuildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID, 328b5a24610SJoel E. Denny const std::pair<unsigned, unsigned> &ImpPatBufferIDRange, 329b5a24610SJoel E. Denny const std::vector<FileCheckDiag> &Diags, 3303c5d267eSJoel E. Denny std::vector<InputAnnotation> &Annotations, 3313c5d267eSJoel E. Denny unsigned &LabelWidth) { 332ce685455SJoel E. Denny // How many diagnostics have we seen so far? 333ce685455SJoel E. Denny unsigned DiagCount = 0; 3342c007c80SJoel E. Denny // How many diagnostics has the current check seen so far? 3352c007c80SJoel E. Denny unsigned CheckDiagCount = 0; 3363c5d267eSJoel E. Denny // What's the widest label? 3373c5d267eSJoel E. Denny LabelWidth = 0; 3383c5d267eSJoel E. Denny for (auto DiagItr = Diags.begin(), DiagEnd = Diags.end(); DiagItr != DiagEnd; 3393c5d267eSJoel E. Denny ++DiagItr) { 3403c5d267eSJoel E. Denny InputAnnotation A; 341ce685455SJoel E. Denny A.DiagIndex = DiagCount++; 3423c5d267eSJoel E. Denny 3433c5d267eSJoel E. Denny // Build label, which uniquely identifies this check result. 344b5a24610SJoel E. Denny unsigned CheckBufferID = SM.FindBufferContainingLoc(DiagItr->CheckLoc); 345b5a24610SJoel E. Denny auto CheckLineAndCol = 346b5a24610SJoel E. Denny SM.getLineAndColumn(DiagItr->CheckLoc, CheckBufferID); 3473c5d267eSJoel E. Denny llvm::raw_string_ostream Label(A.Label); 348b5a24610SJoel E. Denny Label << GetCheckTypeAbbreviation(DiagItr->CheckTy) << ":"; 349b5a24610SJoel E. Denny if (CheckBufferID == CheckFileBufferID) 350b5a24610SJoel E. Denny Label << CheckLineAndCol.first; 351b5a24610SJoel E. Denny else if (ImpPatBufferIDRange.first <= CheckBufferID && 352b5a24610SJoel E. Denny CheckBufferID < ImpPatBufferIDRange.second) 353b5a24610SJoel E. Denny Label << "imp" << (CheckBufferID - ImpPatBufferIDRange.first + 1); 354b5a24610SJoel E. Denny else 355b5a24610SJoel E. Denny llvm_unreachable("expected diagnostic's check location to be either in " 356b5a24610SJoel E. Denny "the check file or for an implicit pattern"); 357ce685455SJoel E. Denny unsigned CheckDiagIndex = UINT_MAX; 3582c007c80SJoel E. Denny auto DiagNext = std::next(DiagItr); 3592c007c80SJoel E. Denny if (DiagNext != DiagEnd && DiagItr->CheckTy == DiagNext->CheckTy && 360b5a24610SJoel E. Denny DiagItr->CheckLoc == DiagNext->CheckLoc) 361ce685455SJoel E. Denny CheckDiagIndex = CheckDiagCount++; 3622c007c80SJoel E. Denny else if (CheckDiagCount) { 363ce685455SJoel E. Denny CheckDiagIndex = CheckDiagCount; 3642c007c80SJoel E. Denny CheckDiagCount = 0; 3652c007c80SJoel E. Denny } 366ce685455SJoel E. Denny if (CheckDiagIndex != UINT_MAX) 367ce685455SJoel E. Denny Label << "'" << CheckDiagIndex; 3683c5d267eSJoel E. Denny Label.flush(); 3693c5d267eSJoel E. Denny LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size()); 3703c5d267eSJoel E. Denny 371608f2bfdSJoel E. Denny A.Marker = GetMarker(DiagItr->MatchTy); 372e2afb614SJoel E. Denny A.FoundAndExpectedMatch = 373e2afb614SJoel E. Denny DiagItr->MatchTy == FileCheckDiag::MatchFoundAndExpected; 3743c5d267eSJoel E. Denny 3753c5d267eSJoel E. Denny // Compute the mark location, and break annotation into multiple 3763c5d267eSJoel E. Denny // annotations if it spans multiple lines. 377bce8fcedSJoel E. Denny A.IsFirstLine = true; 3783c5d267eSJoel E. Denny A.InputLine = DiagItr->InputStartLine; 3793c5d267eSJoel E. Denny A.InputStartCol = DiagItr->InputStartCol; 3803c5d267eSJoel E. Denny if (DiagItr->InputStartLine == DiagItr->InputEndLine) { 3813c5d267eSJoel E. Denny // Sometimes ranges are empty in order to indicate a specific point, but 3823c5d267eSJoel E. Denny // that would mean nothing would be marked, so adjust the range to 3833c5d267eSJoel E. Denny // include the following character. 3843c5d267eSJoel E. Denny A.InputEndCol = 3853c5d267eSJoel E. Denny std::max(DiagItr->InputStartCol + 1, DiagItr->InputEndCol); 3863c5d267eSJoel E. Denny Annotations.push_back(A); 3873c5d267eSJoel E. Denny } else { 3883c5d267eSJoel E. Denny assert(DiagItr->InputStartLine < DiagItr->InputEndLine && 3893c5d267eSJoel E. Denny "expected input range not to be inverted"); 3903c5d267eSJoel E. Denny A.InputEndCol = UINT_MAX; 3913c5d267eSJoel E. Denny Annotations.push_back(A); 3923c5d267eSJoel E. Denny for (unsigned L = DiagItr->InputStartLine + 1, E = DiagItr->InputEndLine; 3933c5d267eSJoel E. Denny L <= E; ++L) { 3943c5d267eSJoel E. Denny // If a range ends before the first column on a line, then it has no 3953c5d267eSJoel E. Denny // characters on that line, so there's nothing to render. 396608f2bfdSJoel E. Denny if (DiagItr->InputEndCol == 1 && L == E) 3973c5d267eSJoel E. Denny break; 3983c5d267eSJoel E. Denny InputAnnotation B; 399ce685455SJoel E. Denny B.DiagIndex = A.DiagIndex; 4003c5d267eSJoel E. Denny B.Label = A.Label; 401bce8fcedSJoel E. Denny B.IsFirstLine = false; 4023c5d267eSJoel E. Denny B.InputLine = L; 403608f2bfdSJoel E. Denny B.Marker = A.Marker; 4043c5d267eSJoel E. Denny B.Marker.Lead = '~'; 4053c5d267eSJoel E. Denny B.Marker.Note = ""; 406608f2bfdSJoel E. Denny B.InputStartCol = 1; 407608f2bfdSJoel E. Denny if (L != E) 408608f2bfdSJoel E. Denny B.InputEndCol = UINT_MAX; 409608f2bfdSJoel E. Denny else 4103c5d267eSJoel E. Denny B.InputEndCol = DiagItr->InputEndCol; 411e2afb614SJoel E. Denny B.FoundAndExpectedMatch = A.FoundAndExpectedMatch; 4123c5d267eSJoel E. Denny Annotations.push_back(B); 4133c5d267eSJoel E. Denny } 4143c5d267eSJoel E. Denny } 4153c5d267eSJoel E. Denny } 4163c5d267eSJoel E. Denny } 4173c5d267eSJoel E. Denny 418bce8fcedSJoel E. Denny static unsigned FindInputLineInFilter( 419bce8fcedSJoel E. Denny bool FilterOnError, unsigned CurInputLine, 420bce8fcedSJoel E. Denny const std::vector<InputAnnotation>::iterator &AnnotationBeg, 421bce8fcedSJoel E. Denny const std::vector<InputAnnotation>::iterator &AnnotationEnd) { 422bce8fcedSJoel E. Denny if (!FilterOnError) 423bce8fcedSJoel E. Denny return CurInputLine; 424bce8fcedSJoel E. Denny for (auto AnnotationItr = AnnotationBeg; AnnotationItr != AnnotationEnd; 425bce8fcedSJoel E. Denny ++AnnotationItr) { 426bce8fcedSJoel E. Denny if (AnnotationItr->IsFirstLine && AnnotationItr->Marker.FiltersAsError) 427bce8fcedSJoel E. Denny return AnnotationItr->InputLine; 428bce8fcedSJoel E. Denny } 429bce8fcedSJoel E. Denny return UINT_MAX; 430bce8fcedSJoel E. Denny } 431bce8fcedSJoel E. Denny 432*77b6ddf1SJoel E. Denny /// To OS, print a vertical ellipsis (right-justified at LabelWidth) if it would 433*77b6ddf1SJoel E. Denny /// occupy less lines than ElidedLines, but print ElidedLines otherwise. Either 434*77b6ddf1SJoel E. Denny /// way, clear ElidedLines. Thus, if ElidedLines is empty, do nothing. 435*77b6ddf1SJoel E. Denny static void DumpEllipsisOrElidedLines(raw_ostream &OS, std::string &ElidedLines, 436*77b6ddf1SJoel E. Denny unsigned LabelWidth) { 437*77b6ddf1SJoel E. Denny if (ElidedLines.empty()) 438*77b6ddf1SJoel E. Denny return; 439*77b6ddf1SJoel E. Denny unsigned EllipsisLines = 3; 440*77b6ddf1SJoel E. Denny if (EllipsisLines < StringRef(ElidedLines).count('\n')) { 441*77b6ddf1SJoel E. Denny for (unsigned i = 0; i < EllipsisLines; ++i) { 442*77b6ddf1SJoel E. Denny WithColor(OS, raw_ostream::BLACK, /*Bold=*/true) 443*77b6ddf1SJoel E. Denny << right_justify(".", LabelWidth); 444*77b6ddf1SJoel E. Denny OS << '\n'; 445*77b6ddf1SJoel E. Denny } 446*77b6ddf1SJoel E. Denny } else 447*77b6ddf1SJoel E. Denny OS << ElidedLines; 448*77b6ddf1SJoel E. Denny ElidedLines.clear(); 449*77b6ddf1SJoel E. Denny } 450*77b6ddf1SJoel E. Denny 4517df86967SJoel E. Denny static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req, 452bce8fcedSJoel E. Denny bool DumpInputFilterOnError, 453bce8fcedSJoel E. Denny unsigned DumpInputContext, 4547df86967SJoel E. Denny StringRef InputFileText, 4557df86967SJoel E. Denny std::vector<InputAnnotation> &Annotations, 4567df86967SJoel E. Denny unsigned LabelWidth) { 457bce8fcedSJoel E. Denny OS << "Input was:\n<<<<<<\n"; 4583c5d267eSJoel E. Denny 4593c5d267eSJoel E. Denny // Sort annotations. 4603c5d267eSJoel E. Denny std::sort(Annotations.begin(), Annotations.end(), 4613c5d267eSJoel E. Denny [](const InputAnnotation &A, const InputAnnotation &B) { 462ce685455SJoel E. Denny // 1. Sort annotations in the order of the input lines. 463ce685455SJoel E. Denny // 464ce685455SJoel E. Denny // This makes it easier to find relevant annotations while 465ce685455SJoel E. Denny // iterating input lines in the implementation below. FileCheck 466ce685455SJoel E. Denny // does not always produce diagnostics in the order of input 467ce685455SJoel E. Denny // lines due to, for example, CHECK-DAG and CHECK-NOT. 4683c5d267eSJoel E. Denny if (A.InputLine != B.InputLine) 4693c5d267eSJoel E. Denny return A.InputLine < B.InputLine; 470ce685455SJoel E. Denny // 2. Sort annotations in the temporal order FileCheck produced 471ce685455SJoel E. Denny // their associated diagnostics. 472ce685455SJoel E. Denny // 473ce685455SJoel E. Denny // This sort offers several benefits: 474ce685455SJoel E. Denny // 475ce685455SJoel E. Denny // A. On a single input line, the order of annotations reflects 476ce685455SJoel E. Denny // the FileCheck logic for processing directives/patterns. 477ce685455SJoel E. Denny // This can be helpful in understanding cases in which the 478ce685455SJoel E. Denny // order of the associated directives/patterns in the check 479ce685455SJoel E. Denny // file or on the command line either (i) does not match the 480ce685455SJoel E. Denny // temporal order in which FileCheck looks for matches for the 481ce685455SJoel E. Denny // directives/patterns (due to, for example, CHECK-LABEL, 482ce685455SJoel E. Denny // CHECK-NOT, or `--implicit-check-not`) or (ii) does match 483ce685455SJoel E. Denny // that order but does not match the order of those 484ce685455SJoel E. Denny // diagnostics along an input line (due to, for example, 485ce685455SJoel E. Denny // CHECK-DAG). 486ce685455SJoel E. Denny // 487ce685455SJoel E. Denny // On the other hand, because our presentation format presents 488ce685455SJoel E. Denny // input lines in order, there's no clear way to offer the 489ce685455SJoel E. Denny // same benefit across input lines. For consistency, it might 490ce685455SJoel E. Denny // then seem worthwhile to have annotations on a single line 491ce685455SJoel E. Denny // also sorted in input order (that is, by input column). 492ce685455SJoel E. Denny // However, in practice, this appears to be more confusing 493ce685455SJoel E. Denny // than helpful. Perhaps it's intuitive to expect annotations 494ce685455SJoel E. Denny // to be listed in the temporal order in which they were 495ce685455SJoel E. Denny // produced except in cases the presentation format obviously 496ce685455SJoel E. Denny // and inherently cannot support it (that is, across input 497ce685455SJoel E. Denny // lines). 498ce685455SJoel E. Denny // 499ce685455SJoel E. Denny // B. When diagnostics' annotations are split among multiple 500ce685455SJoel E. Denny // input lines, the user must track them from one input line 501ce685455SJoel E. Denny // to the next. One property of the sort chosen here is that 502ce685455SJoel E. Denny // it facilitates the user in this regard by ensuring the 503ce685455SJoel E. Denny // following: when comparing any two input lines, a 504ce685455SJoel E. Denny // diagnostic's annotations are sorted in the same position 505ce685455SJoel E. Denny // relative to all other diagnostics' annotations. 506ce685455SJoel E. Denny return A.DiagIndex < B.DiagIndex; 5073c5d267eSJoel E. Denny }); 5083c5d267eSJoel E. Denny 5093c5d267eSJoel E. Denny // Compute the width of the label column. 5103c5d267eSJoel E. Denny const unsigned char *InputFilePtr = InputFileText.bytes_begin(), 5113c5d267eSJoel E. Denny *InputFileEnd = InputFileText.bytes_end(); 5123c5d267eSJoel E. Denny unsigned LineCount = InputFileText.count('\n'); 5133c5d267eSJoel E. Denny if (InputFileEnd[-1] != '\n') 5143c5d267eSJoel E. Denny ++LineCount; 515010982f7SRainer Orth unsigned LineNoWidth = std::log10(LineCount) + 1; 5163c5d267eSJoel E. Denny // +3 below adds spaces (1) to the left of the (right-aligned) line numbers 5173c5d267eSJoel E. Denny // on input lines and (2) to the right of the (left-aligned) labels on 5183c5d267eSJoel E. Denny // annotation lines so that input lines and annotation lines are more 5193c5d267eSJoel E. Denny // visually distinct. For example, the spaces on the annotation lines ensure 5203c5d267eSJoel E. Denny // that input line numbers and check directive line numbers never align 5213c5d267eSJoel E. Denny // horizontally. Those line numbers might not even be for the same file. 5223c5d267eSJoel E. Denny // One space would be enough to achieve that, but more makes it even easier 5233c5d267eSJoel E. Denny // to see. 5243c5d267eSJoel E. Denny LabelWidth = std::max(LabelWidth, LineNoWidth) + 3; 5253c5d267eSJoel E. Denny 5263c5d267eSJoel E. Denny // Print annotated input lines. 527bce8fcedSJoel E. Denny unsigned PrevLineInFilter = 0; // 0 means none so far 528bce8fcedSJoel E. Denny unsigned NextLineInFilter = 0; // 0 means uncomputed, UINT_MAX means none 529*77b6ddf1SJoel E. Denny std::string ElidedLines; 530*77b6ddf1SJoel E. Denny raw_string_ostream ElidedLinesOS(ElidedLines); 531*77b6ddf1SJoel E. Denny ColorMode TheColorMode = 532*77b6ddf1SJoel E. Denny WithColor(OS).colorsEnabled() ? ColorMode::Enable : ColorMode::Disable; 533*77b6ddf1SJoel E. Denny if (TheColorMode == ColorMode::Enable) 534*77b6ddf1SJoel E. Denny ElidedLinesOS.enable_colors(true); 5353c5d267eSJoel E. Denny auto AnnotationItr = Annotations.begin(), AnnotationEnd = Annotations.end(); 5363c5d267eSJoel E. Denny for (unsigned Line = 1; 5373c5d267eSJoel E. Denny InputFilePtr != InputFileEnd || AnnotationItr != AnnotationEnd; 5383c5d267eSJoel E. Denny ++Line) { 5393c5d267eSJoel E. Denny const unsigned char *InputFileLine = InputFilePtr; 5403c5d267eSJoel E. Denny 541bce8fcedSJoel E. Denny // Compute the previous and next line included by the filter. 542bce8fcedSJoel E. Denny if (NextLineInFilter < Line) 543bce8fcedSJoel E. Denny NextLineInFilter = FindInputLineInFilter(DumpInputFilterOnError, Line, 544bce8fcedSJoel E. Denny AnnotationItr, AnnotationEnd); 545bce8fcedSJoel E. Denny assert(NextLineInFilter && "expected NextLineInFilter to be computed"); 546bce8fcedSJoel E. Denny if (NextLineInFilter == Line) 547bce8fcedSJoel E. Denny PrevLineInFilter = Line; 548bce8fcedSJoel E. Denny 549bce8fcedSJoel E. Denny // Elide this input line and its annotations if it's not within the 550bce8fcedSJoel E. Denny // context specified by -dump-input-context of an input line included by 551*77b6ddf1SJoel E. Denny // the dump filter. However, in case the resulting ellipsis would occupy 552*77b6ddf1SJoel E. Denny // more lines than the input lines and annotations it elides, buffer the 553*77b6ddf1SJoel E. Denny // elided lines and annotations so we can print them instead. 554*77b6ddf1SJoel E. Denny raw_ostream *LineOS = &OS; 555bce8fcedSJoel E. Denny if ((!PrevLineInFilter || PrevLineInFilter + DumpInputContext < Line) && 556bce8fcedSJoel E. Denny (NextLineInFilter == UINT_MAX || 557*77b6ddf1SJoel E. Denny Line + DumpInputContext < NextLineInFilter)) 558*77b6ddf1SJoel E. Denny LineOS = &ElidedLinesOS; 559*77b6ddf1SJoel E. Denny else { 560*77b6ddf1SJoel E. Denny LineOS = &OS; 561*77b6ddf1SJoel E. Denny DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth); 562bce8fcedSJoel E. Denny } 563bce8fcedSJoel E. Denny 5643c5d267eSJoel E. Denny // Print right-aligned line number. 565*77b6ddf1SJoel E. Denny WithColor(*LineOS, raw_ostream::BLACK, /*Bold=*/true, /*BF=*/false, 566*77b6ddf1SJoel E. Denny TheColorMode) 5673c5d267eSJoel E. Denny << format_decimal(Line, LabelWidth) << ": "; 5683c5d267eSJoel E. Denny 569e2afb614SJoel E. Denny // For the case where -v and colors are enabled, find the annotations for 570e2afb614SJoel E. Denny // good matches for expected patterns in order to highlight everything 571e2afb614SJoel E. Denny // else in the line. There are no such annotations if -v is disabled. 572e2afb614SJoel E. Denny std::vector<InputAnnotation> FoundAndExpectedMatches; 573*77b6ddf1SJoel E. Denny if (Req.Verbose && TheColorMode == ColorMode::Enable) { 5747df86967SJoel E. Denny for (auto I = AnnotationItr; I != AnnotationEnd && I->InputLine == Line; 5757df86967SJoel E. Denny ++I) { 576e2afb614SJoel E. Denny if (I->FoundAndExpectedMatch) 577e2afb614SJoel E. Denny FoundAndExpectedMatches.push_back(*I); 5787df86967SJoel E. Denny } 5797df86967SJoel E. Denny } 5807df86967SJoel E. Denny 5817df86967SJoel E. Denny // Print numbered line with highlighting where there are no matches for 5827df86967SJoel E. Denny // expected patterns. 5833c5d267eSJoel E. Denny bool Newline = false; 5847df86967SJoel E. Denny { 585*77b6ddf1SJoel E. Denny WithColor COS(*LineOS, raw_ostream::SAVEDCOLOR, /*Bold=*/false, 586*77b6ddf1SJoel E. Denny /*BG=*/false, TheColorMode); 5877df86967SJoel E. Denny bool InMatch = false; 5887df86967SJoel E. Denny if (Req.Verbose) 5897df86967SJoel E. Denny COS.changeColor(raw_ostream::CYAN, true, true); 5907df86967SJoel E. Denny for (unsigned Col = 1; InputFilePtr != InputFileEnd && !Newline; ++Col) { 5917df86967SJoel E. Denny bool WasInMatch = InMatch; 5927df86967SJoel E. Denny InMatch = false; 593e2afb614SJoel E. Denny for (auto M : FoundAndExpectedMatches) { 5947df86967SJoel E. Denny if (M.InputStartCol <= Col && Col < M.InputEndCol) { 5957df86967SJoel E. Denny InMatch = true; 5967df86967SJoel E. Denny break; 5977df86967SJoel E. Denny } 5987df86967SJoel E. Denny } 5997df86967SJoel E. Denny if (!WasInMatch && InMatch) 6007df86967SJoel E. Denny COS.resetColor(); 6017df86967SJoel E. Denny else if (WasInMatch && !InMatch) 6027df86967SJoel E. Denny COS.changeColor(raw_ostream::CYAN, true, true); 6033c5d267eSJoel E. Denny if (*InputFilePtr == '\n') 6043c5d267eSJoel E. Denny Newline = true; 6053c5d267eSJoel E. Denny else 6067df86967SJoel E. Denny COS << *InputFilePtr; 6073c5d267eSJoel E. Denny ++InputFilePtr; 6083c5d267eSJoel E. Denny } 6097df86967SJoel E. Denny } 610*77b6ddf1SJoel E. Denny *LineOS << '\n'; 6113c5d267eSJoel E. Denny unsigned InputLineWidth = InputFilePtr - InputFileLine - Newline; 6123c5d267eSJoel E. Denny 6133c5d267eSJoel E. Denny // Print any annotations. 6143c5d267eSJoel E. Denny while (AnnotationItr != AnnotationEnd && 6153c5d267eSJoel E. Denny AnnotationItr->InputLine == Line) { 616*77b6ddf1SJoel E. Denny WithColor COS(*LineOS, AnnotationItr->Marker.Color, /*Bold=*/true, 617*77b6ddf1SJoel E. Denny /*BG=*/false, TheColorMode); 6183c5d267eSJoel E. Denny // The two spaces below are where the ": " appears on input lines. 6193c5d267eSJoel E. Denny COS << left_justify(AnnotationItr->Label, LabelWidth) << " "; 6203c5d267eSJoel E. Denny unsigned Col; 6213c5d267eSJoel E. Denny for (Col = 1; Col < AnnotationItr->InputStartCol; ++Col) 6223c5d267eSJoel E. Denny COS << ' '; 6233c5d267eSJoel E. Denny COS << AnnotationItr->Marker.Lead; 6243c5d267eSJoel E. Denny // If InputEndCol=UINT_MAX, stop at InputLineWidth. 6253c5d267eSJoel E. Denny for (++Col; Col < AnnotationItr->InputEndCol && Col <= InputLineWidth; 6263c5d267eSJoel E. Denny ++Col) 6273c5d267eSJoel E. Denny COS << '~'; 6283c5d267eSJoel E. Denny const std::string &Note = AnnotationItr->Marker.Note; 6293c5d267eSJoel E. Denny if (!Note.empty()) { 6303c5d267eSJoel E. Denny // Put the note at the end of the input line. If we were to instead 6313c5d267eSJoel E. Denny // put the note right after the marker, subsequent annotations for the 6323c5d267eSJoel E. Denny // same input line might appear to mark this note instead of the input 6333c5d267eSJoel E. Denny // line. 6343c5d267eSJoel E. Denny for (; Col <= InputLineWidth; ++Col) 6353c5d267eSJoel E. Denny COS << ' '; 6363c5d267eSJoel E. Denny COS << ' ' << Note; 6373c5d267eSJoel E. Denny } 6383c5d267eSJoel E. Denny COS << '\n'; 6393c5d267eSJoel E. Denny ++AnnotationItr; 6403c5d267eSJoel E. Denny } 6413c5d267eSJoel E. Denny } 642*77b6ddf1SJoel E. Denny DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth); 6433c5d267eSJoel E. Denny 6443c5d267eSJoel E. Denny OS << ">>>>>>\n"; 6453c5d267eSJoel E. Denny } 6463c5d267eSJoel E. Denny 647ee3c74fbSChris Lattner int main(int argc, char **argv) { 6483e66509fSJoel E. Denny // Enable use of ANSI color codes because FileCheck is using them to 6493e66509fSJoel E. Denny // highlight text. 6503e66509fSJoel E. Denny llvm::sys::Process::UseANSIEscapeCodes(true); 6513e66509fSJoel E. Denny 652197194b6SRui Ueyama InitLLVM X(argc, argv); 65324994d77SJoel E. Denny cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr, 65424994d77SJoel E. Denny "FILECHECK_OPTS"); 655bce8fcedSJoel E. Denny 656bce8fcedSJoel E. Denny // Select -dump-input* values. The -help documentation specifies the default 657bce8fcedSJoel E. Denny // value and which value to choose if an option is specified multiple times. 658bce8fcedSJoel E. Denny // In the latter case, the general rule of thumb is to choose the value that 659bce8fcedSJoel E. Denny // provides the most information. 660fdde18a7SJoel E. Denny DumpInputValue DumpInput = 661fdde18a7SJoel E. Denny DumpInputs.empty() 6623b83501cSJoel E. Denny ? DumpInputFail 663fdde18a7SJoel E. Denny : *std::max_element(DumpInputs.begin(), DumpInputs.end()); 664bce8fcedSJoel E. Denny bool DumpInputFilterOnError = DumpInput == DumpInputFail; 665bce8fcedSJoel E. Denny unsigned DumpInputContext = DumpInputContexts.empty() 666bce8fcedSJoel E. Denny ? 5 667bce8fcedSJoel E. Denny : *std::max_element(DumpInputContexts.begin(), 668bce8fcedSJoel E. Denny DumpInputContexts.end()); 669bce8fcedSJoel E. Denny 6703c5d267eSJoel E. Denny if (DumpInput == DumpInputHelp) { 6713c5d267eSJoel E. Denny DumpInputAnnotationHelp(outs()); 6723c5d267eSJoel E. Denny return 0; 6733c5d267eSJoel E. Denny } 6743c5d267eSJoel E. Denny if (CheckFilename.empty()) { 6753c5d267eSJoel E. Denny errs() << "<check-file> not specified\n"; 6763c5d267eSJoel E. Denny return 2; 6773c5d267eSJoel E. Denny } 678ee3c74fbSChris Lattner 679ffa9d2e4SAditya Nandakumar FileCheckRequest Req; 68076e0ab23SGeorgii Rymar for (StringRef Prefix : CheckPrefixes) 681ffa9d2e4SAditya Nandakumar Req.CheckPrefixes.push_back(Prefix); 682ffa9d2e4SAditya Nandakumar 683a1fd1882SJoel E. Denny for (StringRef Prefix : CommentPrefixes) 684a1fd1882SJoel E. Denny Req.CommentPrefixes.push_back(Prefix); 685a1fd1882SJoel E. Denny 68676e0ab23SGeorgii Rymar for (StringRef CheckNot : ImplicitCheckNot) 687ffa9d2e4SAditya Nandakumar Req.ImplicitCheckNot.push_back(CheckNot); 688ffa9d2e4SAditya Nandakumar 689a5e233bfSThomas Preud'homme bool GlobalDefineError = false; 69076e0ab23SGeorgii Rymar for (StringRef G : GlobalDefines) { 691a5e233bfSThomas Preud'homme size_t EqIdx = G.find('='); 692a5e233bfSThomas Preud'homme if (EqIdx == std::string::npos) { 693a5e233bfSThomas Preud'homme errs() << "Missing equal sign in command-line definition '-D" << G 694a5e233bfSThomas Preud'homme << "'\n"; 695a5e233bfSThomas Preud'homme GlobalDefineError = true; 696a5e233bfSThomas Preud'homme continue; 697a5e233bfSThomas Preud'homme } 698a5e233bfSThomas Preud'homme if (EqIdx == 0) { 6991a944d27SThomas Preud'homme errs() << "Missing variable name in command-line definition '-D" << G 7001a944d27SThomas Preud'homme << "'\n"; 701a5e233bfSThomas Preud'homme GlobalDefineError = true; 702a5e233bfSThomas Preud'homme continue; 703a5e233bfSThomas Preud'homme } 704ffa9d2e4SAditya Nandakumar Req.GlobalDefines.push_back(G); 705a5e233bfSThomas Preud'homme } 706a5e233bfSThomas Preud'homme if (GlobalDefineError) 707a5e233bfSThomas Preud'homme return 2; 708ffa9d2e4SAditya Nandakumar 709ffa9d2e4SAditya Nandakumar Req.AllowEmptyInput = AllowEmptyInput; 710ffa9d2e4SAditya Nandakumar Req.EnableVarScope = EnableVarScope; 711ffa9d2e4SAditya Nandakumar Req.AllowDeprecatedDagOverlap = AllowDeprecatedDagOverlap; 712ffa9d2e4SAditya Nandakumar Req.Verbose = Verbose; 713ffa9d2e4SAditya Nandakumar Req.VerboseVerbose = VerboseVerbose; 714ffa9d2e4SAditya Nandakumar Req.NoCanonicalizeWhiteSpace = NoCanonicalizeWhiteSpace; 715ffa9d2e4SAditya Nandakumar Req.MatchFullLines = MatchFullLines; 7165b5b2fd2SKai Nacke Req.IgnoreCase = IgnoreCase; 717ffa9d2e4SAditya Nandakumar 718ffa9d2e4SAditya Nandakumar if (VerboseVerbose) 719ffa9d2e4SAditya Nandakumar Req.Verbose = true; 720ffa9d2e4SAditya Nandakumar 721ffa9d2e4SAditya Nandakumar FileCheck FC(Req); 7222aa0217aSJoel E. Denny if (!FC.ValidateCheckPrefixes()) 723c2735158SRui Ueyama return 2; 724c2735158SRui Ueyama 725ffa9d2e4SAditya Nandakumar Regex PrefixRE = FC.buildCheckPrefixRegex(); 726726774cbSChandler Carruth std::string REError; 727726774cbSChandler Carruth if (!PrefixRE.isValid(REError)) { 728726774cbSChandler Carruth errs() << "Unable to combine check-prefix strings into a prefix regular " 729726774cbSChandler Carruth "expression! This is likely a bug in FileCheck's verification of " 730726774cbSChandler Carruth "the check-prefix strings. Regular expression parsing failed " 731726774cbSChandler Carruth "with the following error: " 732726774cbSChandler Carruth << REError << "\n"; 733726774cbSChandler Carruth return 2; 734726774cbSChandler Carruth } 73513df4626SMatt Arsenault 736ee3c74fbSChris Lattner SourceMgr SM; 737ee3c74fbSChris Lattner 738ee3c74fbSChris Lattner // Read the expected strings from the check file. 73920247900SChandler Carruth ErrorOr<std::unique_ptr<MemoryBuffer>> CheckFileOrErr = 74020247900SChandler Carruth MemoryBuffer::getFileOrSTDIN(CheckFilename); 74120247900SChandler Carruth if (std::error_code EC = CheckFileOrErr.getError()) { 74220247900SChandler Carruth errs() << "Could not open check file '" << CheckFilename 74320247900SChandler Carruth << "': " << EC.message() << '\n'; 74420247900SChandler Carruth return 2; 74520247900SChandler Carruth } 74620247900SChandler Carruth MemoryBuffer &CheckFile = *CheckFileOrErr.get(); 74720247900SChandler Carruth 74820247900SChandler Carruth SmallString<4096> CheckFileBuffer; 749ffa9d2e4SAditya Nandakumar StringRef CheckFileText = FC.CanonicalizeFile(CheckFile, CheckFileBuffer); 75020247900SChandler Carruth 751b5a24610SJoel E. Denny unsigned CheckFileBufferID = 75220247900SChandler Carruth SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 75320247900SChandler Carruth CheckFileText, CheckFile.getBufferIdentifier()), 75420247900SChandler Carruth SMLoc()); 75520247900SChandler Carruth 756b5a24610SJoel E. Denny std::pair<unsigned, unsigned> ImpPatBufferIDRange; 757b5a24610SJoel E. Denny if (FC.readCheckFile(SM, CheckFileText, PrefixRE, &ImpPatBufferIDRange)) 758ee3c74fbSChris Lattner return 2; 759ee3c74fbSChris Lattner 760ee3c74fbSChris Lattner // Open the file to check and add it to SourceMgr. 76120247900SChandler Carruth ErrorOr<std::unique_ptr<MemoryBuffer>> InputFileOrErr = 762adf21f2aSRafael Espindola MemoryBuffer::getFileOrSTDIN(InputFilename); 7636e01cd67SDavid Bozier if (InputFilename == "-") 7646e01cd67SDavid Bozier InputFilename = "<stdin>"; // Overwrite for improved diagnostic messages 76520247900SChandler Carruth if (std::error_code EC = InputFileOrErr.getError()) { 766adf21f2aSRafael Espindola errs() << "Could not open input file '" << InputFilename 767adf21f2aSRafael Espindola << "': " << EC.message() << '\n'; 7688e1c6477SEli Bendersky return 2; 769ee3c74fbSChris Lattner } 77020247900SChandler Carruth MemoryBuffer &InputFile = *InputFileOrErr.get(); 7712c3e5cdfSChris Lattner 77220247900SChandler Carruth if (InputFile.getBufferSize() == 0 && !AllowEmptyInput) { 773b692bed7SChris Lattner errs() << "FileCheck error: '" << InputFilename << "' is empty.\n"; 7742bd4f8b6SXinliang David Li DumpCommandLine(argc, argv); 7758e1c6477SEli Bendersky return 2; 776b692bed7SChris Lattner } 777b692bed7SChris Lattner 77820247900SChandler Carruth SmallString<4096> InputFileBuffer; 779ffa9d2e4SAditya Nandakumar StringRef InputFileText = FC.CanonicalizeFile(InputFile, InputFileBuffer); 7802c3e5cdfSChris Lattner 781e8f2fb20SChandler Carruth SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 782e8f2fb20SChandler Carruth InputFileText, InputFile.getBufferIdentifier()), 783e8f2fb20SChandler Carruth SMLoc()); 784ee3c74fbSChris Lattner 7853c5d267eSJoel E. Denny std::vector<FileCheckDiag> Diags; 78602ada9bdSThomas Preud'homme int ExitCode = FC.checkInput(SM, InputFileText, 7873c5d267eSJoel E. Denny DumpInput == DumpInputNever ? nullptr : &Diags) 7883c5d267eSJoel E. Denny ? EXIT_SUCCESS 7893c5d267eSJoel E. Denny : 1; 7903c5d267eSJoel E. Denny if (DumpInput == DumpInputAlways || 7913c5d267eSJoel E. Denny (ExitCode == 1 && DumpInput == DumpInputFail)) { 7923c5d267eSJoel E. Denny errs() << "\n" 793839f8e4fSJoel E. Denny << "Input file: " << InputFilename << "\n" 7943c5d267eSJoel E. Denny << "Check file: " << CheckFilename << "\n" 7953c5d267eSJoel E. Denny << "\n" 796839f8e4fSJoel E. Denny << "-dump-input=help explains the following input dump.\n" 7973c5d267eSJoel E. Denny << "\n"; 7983c5d267eSJoel E. Denny std::vector<InputAnnotation> Annotations; 7993c5d267eSJoel E. Denny unsigned LabelWidth; 800b5a24610SJoel E. Denny BuildInputAnnotations(SM, CheckFileBufferID, ImpPatBufferIDRange, Diags, 801b5a24610SJoel E. Denny Annotations, LabelWidth); 802bce8fcedSJoel E. Denny DumpAnnotatedInput(errs(), Req, DumpInputFilterOnError, DumpInputContext, 803bce8fcedSJoel E. Denny InputFileText, Annotations, LabelWidth); 8043c5d267eSJoel E. Denny } 805346dfbe2SGeorge Karpenkov 806346dfbe2SGeorge Karpenkov return ExitCode; 807ee3c74fbSChris Lattner } 808