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*bce8fcedSJoel E. Denny static cl::list<unsigned> DumpInputContexts( 132*bce8fcedSJoel E. Denny "dump-input-context", cl::value_desc("N"), 133*bce8fcedSJoel E. Denny cl::desc("In the dump requested by -dump-input=fail, print <N> input\n" 134*bce8fcedSJoel E. Denny "lines before and <N> input lines after the starting line of\n" 135*bce8fcedSJoel E. Denny "any error diagnostic. When there are multiple occurrences of\n" 136*bce8fcedSJoel E. Denny "this option, the largest specified <N> has precedence. The\n" 137*bce8fcedSJoel E. Denny "default is 5.\n")); 138*bce8fcedSJoel 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; 161*bce8fcedSJoel E. Denny /// Does this marker indicate inclusion by the input filter implied by 162*bce8fcedSJoel E. Denny /// -dump-input=fail? 163*bce8fcedSJoel E. Denny bool FiltersAsError; 1643c5d267eSJoel E. Denny MarkerStyle() {} 1654d41c332SRui Ueyama MarkerStyle(char Lead, raw_ostream::Colors Color, 166*bce8fcedSJoel E. Denny const std::string &Note = "", bool FiltersAsError = false) 167*bce8fcedSJoel E. Denny : Lead(Lead), Color(Color), Note(Note), FiltersAsError(FiltersAsError) { 168*bce8fcedSJoel E. Denny assert((!FiltersAsError || !Note.empty()) && 169*bce8fcedSJoel E. Denny "expected error diagnostic to have note"); 170*bce8fcedSJoel 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: 178*bce8fcedSJoel E. Denny return MarkerStyle('!', raw_ostream::RED, "error: no match expected", 179*bce8fcedSJoel E. Denny /*FiltersAsError=*/true); 180e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundButWrongLine: 181*bce8fcedSJoel E. Denny return MarkerStyle('!', raw_ostream::RED, "error: match on wrong line", 182*bce8fcedSJoel 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: 189*bce8fcedSJoel E. Denny return MarkerStyle('X', raw_ostream::RED, "error: no match found", 190*bce8fcedSJoel E. Denny /*FiltersAsError=*/true); 1912c007c80SJoel E. Denny case FileCheckDiag::MatchFuzzy: 192*bce8fcedSJoel E. Denny return MarkerStyle('?', raw_ostream::MAGENTA, "possible intended match", 193*bce8fcedSJoel 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" 204*bce8fcedSJoel 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 250*bce8fcedSJoel E. Denny // Elided lines. 251*bce8fcedSJoel E. Denny OS << " - "; 252*bce8fcedSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "..."; 253*bce8fcedSJoel E. Denny OS << " indicates elided input lines and annotations, as specified by\n" 254*bce8fcedSJoel E. Denny << " -dump-input=fail and -dump-input-context\n"; 255*bce8fcedSJoel 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; 276*bce8fcedSJoel E. Denny /// Is this the initial fragment of a diagnostic that has been broken across 277*bce8fcedSJoel E. Denny /// multiple lines? 278*bce8fcedSJoel E. Denny bool IsFirstLine; 2793c5d267eSJoel E. Denny /// What input line (one-origin indexing) this annotation marks. This might 280*bce8fcedSJoel E. Denny /// be different from the starting line of the original diagnostic if 281*bce8fcedSJoel 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. 377*bce8fcedSJoel 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; 401*bce8fcedSJoel 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 418*bce8fcedSJoel E. Denny static unsigned FindInputLineInFilter( 419*bce8fcedSJoel E. Denny bool FilterOnError, unsigned CurInputLine, 420*bce8fcedSJoel E. Denny const std::vector<InputAnnotation>::iterator &AnnotationBeg, 421*bce8fcedSJoel E. Denny const std::vector<InputAnnotation>::iterator &AnnotationEnd) { 422*bce8fcedSJoel E. Denny if (!FilterOnError) 423*bce8fcedSJoel E. Denny return CurInputLine; 424*bce8fcedSJoel E. Denny for (auto AnnotationItr = AnnotationBeg; AnnotationItr != AnnotationEnd; 425*bce8fcedSJoel E. Denny ++AnnotationItr) { 426*bce8fcedSJoel E. Denny if (AnnotationItr->IsFirstLine && AnnotationItr->Marker.FiltersAsError) 427*bce8fcedSJoel E. Denny return AnnotationItr->InputLine; 428*bce8fcedSJoel E. Denny } 429*bce8fcedSJoel E. Denny return UINT_MAX; 430*bce8fcedSJoel E. Denny } 431*bce8fcedSJoel E. Denny 4327df86967SJoel E. Denny static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req, 433*bce8fcedSJoel E. Denny bool DumpInputFilterOnError, 434*bce8fcedSJoel E. Denny unsigned DumpInputContext, 4357df86967SJoel E. Denny StringRef InputFileText, 4367df86967SJoel E. Denny std::vector<InputAnnotation> &Annotations, 4377df86967SJoel E. Denny unsigned LabelWidth) { 438*bce8fcedSJoel E. Denny OS << "Input was:\n<<<<<<\n"; 4393c5d267eSJoel E. Denny 4403c5d267eSJoel E. Denny // Sort annotations. 4413c5d267eSJoel E. Denny std::sort(Annotations.begin(), Annotations.end(), 4423c5d267eSJoel E. Denny [](const InputAnnotation &A, const InputAnnotation &B) { 443ce685455SJoel E. Denny // 1. Sort annotations in the order of the input lines. 444ce685455SJoel E. Denny // 445ce685455SJoel E. Denny // This makes it easier to find relevant annotations while 446ce685455SJoel E. Denny // iterating input lines in the implementation below. FileCheck 447ce685455SJoel E. Denny // does not always produce diagnostics in the order of input 448ce685455SJoel E. Denny // lines due to, for example, CHECK-DAG and CHECK-NOT. 4493c5d267eSJoel E. Denny if (A.InputLine != B.InputLine) 4503c5d267eSJoel E. Denny return A.InputLine < B.InputLine; 451ce685455SJoel E. Denny // 2. Sort annotations in the temporal order FileCheck produced 452ce685455SJoel E. Denny // their associated diagnostics. 453ce685455SJoel E. Denny // 454ce685455SJoel E. Denny // This sort offers several benefits: 455ce685455SJoel E. Denny // 456ce685455SJoel E. Denny // A. On a single input line, the order of annotations reflects 457ce685455SJoel E. Denny // the FileCheck logic for processing directives/patterns. 458ce685455SJoel E. Denny // This can be helpful in understanding cases in which the 459ce685455SJoel E. Denny // order of the associated directives/patterns in the check 460ce685455SJoel E. Denny // file or on the command line either (i) does not match the 461ce685455SJoel E. Denny // temporal order in which FileCheck looks for matches for the 462ce685455SJoel E. Denny // directives/patterns (due to, for example, CHECK-LABEL, 463ce685455SJoel E. Denny // CHECK-NOT, or `--implicit-check-not`) or (ii) does match 464ce685455SJoel E. Denny // that order but does not match the order of those 465ce685455SJoel E. Denny // diagnostics along an input line (due to, for example, 466ce685455SJoel E. Denny // CHECK-DAG). 467ce685455SJoel E. Denny // 468ce685455SJoel E. Denny // On the other hand, because our presentation format presents 469ce685455SJoel E. Denny // input lines in order, there's no clear way to offer the 470ce685455SJoel E. Denny // same benefit across input lines. For consistency, it might 471ce685455SJoel E. Denny // then seem worthwhile to have annotations on a single line 472ce685455SJoel E. Denny // also sorted in input order (that is, by input column). 473ce685455SJoel E. Denny // However, in practice, this appears to be more confusing 474ce685455SJoel E. Denny // than helpful. Perhaps it's intuitive to expect annotations 475ce685455SJoel E. Denny // to be listed in the temporal order in which they were 476ce685455SJoel E. Denny // produced except in cases the presentation format obviously 477ce685455SJoel E. Denny // and inherently cannot support it (that is, across input 478ce685455SJoel E. Denny // lines). 479ce685455SJoel E. Denny // 480ce685455SJoel E. Denny // B. When diagnostics' annotations are split among multiple 481ce685455SJoel E. Denny // input lines, the user must track them from one input line 482ce685455SJoel E. Denny // to the next. One property of the sort chosen here is that 483ce685455SJoel E. Denny // it facilitates the user in this regard by ensuring the 484ce685455SJoel E. Denny // following: when comparing any two input lines, a 485ce685455SJoel E. Denny // diagnostic's annotations are sorted in the same position 486ce685455SJoel E. Denny // relative to all other diagnostics' annotations. 487ce685455SJoel E. Denny return A.DiagIndex < B.DiagIndex; 4883c5d267eSJoel E. Denny }); 4893c5d267eSJoel E. Denny 4903c5d267eSJoel E. Denny // Compute the width of the label column. 4913c5d267eSJoel E. Denny const unsigned char *InputFilePtr = InputFileText.bytes_begin(), 4923c5d267eSJoel E. Denny *InputFileEnd = InputFileText.bytes_end(); 4933c5d267eSJoel E. Denny unsigned LineCount = InputFileText.count('\n'); 4943c5d267eSJoel E. Denny if (InputFileEnd[-1] != '\n') 4953c5d267eSJoel E. Denny ++LineCount; 496010982f7SRainer Orth unsigned LineNoWidth = std::log10(LineCount) + 1; 4973c5d267eSJoel E. Denny // +3 below adds spaces (1) to the left of the (right-aligned) line numbers 4983c5d267eSJoel E. Denny // on input lines and (2) to the right of the (left-aligned) labels on 4993c5d267eSJoel E. Denny // annotation lines so that input lines and annotation lines are more 5003c5d267eSJoel E. Denny // visually distinct. For example, the spaces on the annotation lines ensure 5013c5d267eSJoel E. Denny // that input line numbers and check directive line numbers never align 5023c5d267eSJoel E. Denny // horizontally. Those line numbers might not even be for the same file. 5033c5d267eSJoel E. Denny // One space would be enough to achieve that, but more makes it even easier 5043c5d267eSJoel E. Denny // to see. 5053c5d267eSJoel E. Denny LabelWidth = std::max(LabelWidth, LineNoWidth) + 3; 5063c5d267eSJoel E. Denny 5073c5d267eSJoel E. Denny // Print annotated input lines. 508*bce8fcedSJoel E. Denny unsigned PrevLineInFilter = 0; // 0 means none so far 509*bce8fcedSJoel E. Denny unsigned NextLineInFilter = 0; // 0 means uncomputed, UINT_MAX means none 510*bce8fcedSJoel E. Denny bool PrevLineElided = false; 5113c5d267eSJoel E. Denny auto AnnotationItr = Annotations.begin(), AnnotationEnd = Annotations.end(); 5123c5d267eSJoel E. Denny for (unsigned Line = 1; 5133c5d267eSJoel E. Denny InputFilePtr != InputFileEnd || AnnotationItr != AnnotationEnd; 5143c5d267eSJoel E. Denny ++Line) { 5153c5d267eSJoel E. Denny const unsigned char *InputFileLine = InputFilePtr; 5163c5d267eSJoel E. Denny 517*bce8fcedSJoel E. Denny // Compute the previous and next line included by the filter. 518*bce8fcedSJoel E. Denny if (NextLineInFilter < Line) 519*bce8fcedSJoel E. Denny NextLineInFilter = FindInputLineInFilter(DumpInputFilterOnError, Line, 520*bce8fcedSJoel E. Denny AnnotationItr, AnnotationEnd); 521*bce8fcedSJoel E. Denny assert(NextLineInFilter && "expected NextLineInFilter to be computed"); 522*bce8fcedSJoel E. Denny if (NextLineInFilter == Line) 523*bce8fcedSJoel E. Denny PrevLineInFilter = Line; 524*bce8fcedSJoel E. Denny 525*bce8fcedSJoel E. Denny // Elide this input line and its annotations if it's not within the 526*bce8fcedSJoel E. Denny // context specified by -dump-input-context of an input line included by 527*bce8fcedSJoel E. Denny // the dump filter. 528*bce8fcedSJoel E. Denny if ((!PrevLineInFilter || PrevLineInFilter + DumpInputContext < Line) && 529*bce8fcedSJoel E. Denny (NextLineInFilter == UINT_MAX || 530*bce8fcedSJoel E. Denny Line + DumpInputContext < NextLineInFilter)) { 531*bce8fcedSJoel E. Denny while (InputFilePtr != InputFileEnd && *InputFilePtr != '\n') 532*bce8fcedSJoel E. Denny ++InputFilePtr; 533*bce8fcedSJoel E. Denny if (InputFilePtr != InputFileEnd) 534*bce8fcedSJoel E. Denny ++InputFilePtr; 535*bce8fcedSJoel E. Denny while (AnnotationItr != AnnotationEnd && AnnotationItr->InputLine == Line) 536*bce8fcedSJoel E. Denny ++AnnotationItr; 537*bce8fcedSJoel E. Denny if (!PrevLineElided) { 538*bce8fcedSJoel E. Denny for (unsigned i = 0; i < 3; ++i) { 539*bce8fcedSJoel E. Denny WithColor(OS, raw_ostream::BLACK, /*Bold=*/true) 540*bce8fcedSJoel E. Denny << right_justify(".", LabelWidth); 541*bce8fcedSJoel E. Denny OS << '\n'; 542*bce8fcedSJoel E. Denny } 543*bce8fcedSJoel E. Denny PrevLineElided = true; 544*bce8fcedSJoel E. Denny } 545*bce8fcedSJoel E. Denny continue; 546*bce8fcedSJoel E. Denny } 547*bce8fcedSJoel E. Denny PrevLineElided = false; 548*bce8fcedSJoel E. Denny 5493c5d267eSJoel E. Denny // Print right-aligned line number. 5503c5d267eSJoel E. Denny WithColor(OS, raw_ostream::BLACK, true) 5513c5d267eSJoel E. Denny << format_decimal(Line, LabelWidth) << ": "; 5523c5d267eSJoel E. Denny 553e2afb614SJoel E. Denny // For the case where -v and colors are enabled, find the annotations for 554e2afb614SJoel E. Denny // good matches for expected patterns in order to highlight everything 555e2afb614SJoel E. Denny // else in the line. There are no such annotations if -v is disabled. 556e2afb614SJoel E. Denny std::vector<InputAnnotation> FoundAndExpectedMatches; 5577df86967SJoel E. Denny if (Req.Verbose && WithColor(OS).colorsEnabled()) { 5587df86967SJoel E. Denny for (auto I = AnnotationItr; I != AnnotationEnd && I->InputLine == Line; 5597df86967SJoel E. Denny ++I) { 560e2afb614SJoel E. Denny if (I->FoundAndExpectedMatch) 561e2afb614SJoel E. Denny FoundAndExpectedMatches.push_back(*I); 5627df86967SJoel E. Denny } 5637df86967SJoel E. Denny } 5647df86967SJoel E. Denny 5657df86967SJoel E. Denny // Print numbered line with highlighting where there are no matches for 5667df86967SJoel E. Denny // expected patterns. 5673c5d267eSJoel E. Denny bool Newline = false; 5687df86967SJoel E. Denny { 5697df86967SJoel E. Denny WithColor COS(OS); 5707df86967SJoel E. Denny bool InMatch = false; 5717df86967SJoel E. Denny if (Req.Verbose) 5727df86967SJoel E. Denny COS.changeColor(raw_ostream::CYAN, true, true); 5737df86967SJoel E. Denny for (unsigned Col = 1; InputFilePtr != InputFileEnd && !Newline; ++Col) { 5747df86967SJoel E. Denny bool WasInMatch = InMatch; 5757df86967SJoel E. Denny InMatch = false; 576e2afb614SJoel E. Denny for (auto M : FoundAndExpectedMatches) { 5777df86967SJoel E. Denny if (M.InputStartCol <= Col && Col < M.InputEndCol) { 5787df86967SJoel E. Denny InMatch = true; 5797df86967SJoel E. Denny break; 5807df86967SJoel E. Denny } 5817df86967SJoel E. Denny } 5827df86967SJoel E. Denny if (!WasInMatch && InMatch) 5837df86967SJoel E. Denny COS.resetColor(); 5847df86967SJoel E. Denny else if (WasInMatch && !InMatch) 5857df86967SJoel E. Denny COS.changeColor(raw_ostream::CYAN, true, true); 5863c5d267eSJoel E. Denny if (*InputFilePtr == '\n') 5873c5d267eSJoel E. Denny Newline = true; 5883c5d267eSJoel E. Denny else 5897df86967SJoel E. Denny COS << *InputFilePtr; 5903c5d267eSJoel E. Denny ++InputFilePtr; 5913c5d267eSJoel E. Denny } 5927df86967SJoel E. Denny } 5933c5d267eSJoel E. Denny OS << '\n'; 5943c5d267eSJoel E. Denny unsigned InputLineWidth = InputFilePtr - InputFileLine - Newline; 5953c5d267eSJoel E. Denny 5963c5d267eSJoel E. Denny // Print any annotations. 5973c5d267eSJoel E. Denny while (AnnotationItr != AnnotationEnd && 5983c5d267eSJoel E. Denny AnnotationItr->InputLine == Line) { 5993c5d267eSJoel E. Denny WithColor COS(OS, AnnotationItr->Marker.Color, true); 6003c5d267eSJoel E. Denny // The two spaces below are where the ": " appears on input lines. 6013c5d267eSJoel E. Denny COS << left_justify(AnnotationItr->Label, LabelWidth) << " "; 6023c5d267eSJoel E. Denny unsigned Col; 6033c5d267eSJoel E. Denny for (Col = 1; Col < AnnotationItr->InputStartCol; ++Col) 6043c5d267eSJoel E. Denny COS << ' '; 6053c5d267eSJoel E. Denny COS << AnnotationItr->Marker.Lead; 6063c5d267eSJoel E. Denny // If InputEndCol=UINT_MAX, stop at InputLineWidth. 6073c5d267eSJoel E. Denny for (++Col; Col < AnnotationItr->InputEndCol && Col <= InputLineWidth; 6083c5d267eSJoel E. Denny ++Col) 6093c5d267eSJoel E. Denny COS << '~'; 6103c5d267eSJoel E. Denny const std::string &Note = AnnotationItr->Marker.Note; 6113c5d267eSJoel E. Denny if (!Note.empty()) { 6123c5d267eSJoel E. Denny // Put the note at the end of the input line. If we were to instead 6133c5d267eSJoel E. Denny // put the note right after the marker, subsequent annotations for the 6143c5d267eSJoel E. Denny // same input line might appear to mark this note instead of the input 6153c5d267eSJoel E. Denny // line. 6163c5d267eSJoel E. Denny for (; Col <= InputLineWidth; ++Col) 6173c5d267eSJoel E. Denny COS << ' '; 6183c5d267eSJoel E. Denny COS << ' ' << Note; 6193c5d267eSJoel E. Denny } 6203c5d267eSJoel E. Denny COS << '\n'; 6213c5d267eSJoel E. Denny ++AnnotationItr; 6223c5d267eSJoel E. Denny } 6233c5d267eSJoel E. Denny } 6243c5d267eSJoel E. Denny 6253c5d267eSJoel E. Denny OS << ">>>>>>\n"; 6263c5d267eSJoel E. Denny } 6273c5d267eSJoel E. Denny 628ee3c74fbSChris Lattner int main(int argc, char **argv) { 6293e66509fSJoel E. Denny // Enable use of ANSI color codes because FileCheck is using them to 6303e66509fSJoel E. Denny // highlight text. 6313e66509fSJoel E. Denny llvm::sys::Process::UseANSIEscapeCodes(true); 6323e66509fSJoel E. Denny 633197194b6SRui Ueyama InitLLVM X(argc, argv); 63424994d77SJoel E. Denny cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr, 63524994d77SJoel E. Denny "FILECHECK_OPTS"); 636*bce8fcedSJoel E. Denny 637*bce8fcedSJoel E. Denny // Select -dump-input* values. The -help documentation specifies the default 638*bce8fcedSJoel E. Denny // value and which value to choose if an option is specified multiple times. 639*bce8fcedSJoel E. Denny // In the latter case, the general rule of thumb is to choose the value that 640*bce8fcedSJoel E. Denny // provides the most information. 641fdde18a7SJoel E. Denny DumpInputValue DumpInput = 642fdde18a7SJoel E. Denny DumpInputs.empty() 6433b83501cSJoel E. Denny ? DumpInputFail 644fdde18a7SJoel E. Denny : *std::max_element(DumpInputs.begin(), DumpInputs.end()); 645*bce8fcedSJoel E. Denny bool DumpInputFilterOnError = DumpInput == DumpInputFail; 646*bce8fcedSJoel E. Denny unsigned DumpInputContext = DumpInputContexts.empty() 647*bce8fcedSJoel E. Denny ? 5 648*bce8fcedSJoel E. Denny : *std::max_element(DumpInputContexts.begin(), 649*bce8fcedSJoel E. Denny DumpInputContexts.end()); 650*bce8fcedSJoel E. Denny 6513c5d267eSJoel E. Denny if (DumpInput == DumpInputHelp) { 6523c5d267eSJoel E. Denny DumpInputAnnotationHelp(outs()); 6533c5d267eSJoel E. Denny return 0; 6543c5d267eSJoel E. Denny } 6553c5d267eSJoel E. Denny if (CheckFilename.empty()) { 6563c5d267eSJoel E. Denny errs() << "<check-file> not specified\n"; 6573c5d267eSJoel E. Denny return 2; 6583c5d267eSJoel E. Denny } 659ee3c74fbSChris Lattner 660ffa9d2e4SAditya Nandakumar FileCheckRequest Req; 66176e0ab23SGeorgii Rymar for (StringRef Prefix : CheckPrefixes) 662ffa9d2e4SAditya Nandakumar Req.CheckPrefixes.push_back(Prefix); 663ffa9d2e4SAditya Nandakumar 664a1fd1882SJoel E. Denny for (StringRef Prefix : CommentPrefixes) 665a1fd1882SJoel E. Denny Req.CommentPrefixes.push_back(Prefix); 666a1fd1882SJoel E. Denny 66776e0ab23SGeorgii Rymar for (StringRef CheckNot : ImplicitCheckNot) 668ffa9d2e4SAditya Nandakumar Req.ImplicitCheckNot.push_back(CheckNot); 669ffa9d2e4SAditya Nandakumar 670a5e233bfSThomas Preud'homme bool GlobalDefineError = false; 67176e0ab23SGeorgii Rymar for (StringRef G : GlobalDefines) { 672a5e233bfSThomas Preud'homme size_t EqIdx = G.find('='); 673a5e233bfSThomas Preud'homme if (EqIdx == std::string::npos) { 674a5e233bfSThomas Preud'homme errs() << "Missing equal sign in command-line definition '-D" << G 675a5e233bfSThomas Preud'homme << "'\n"; 676a5e233bfSThomas Preud'homme GlobalDefineError = true; 677a5e233bfSThomas Preud'homme continue; 678a5e233bfSThomas Preud'homme } 679a5e233bfSThomas Preud'homme if (EqIdx == 0) { 6801a944d27SThomas Preud'homme errs() << "Missing variable name in command-line definition '-D" << G 6811a944d27SThomas Preud'homme << "'\n"; 682a5e233bfSThomas Preud'homme GlobalDefineError = true; 683a5e233bfSThomas Preud'homme continue; 684a5e233bfSThomas Preud'homme } 685ffa9d2e4SAditya Nandakumar Req.GlobalDefines.push_back(G); 686a5e233bfSThomas Preud'homme } 687a5e233bfSThomas Preud'homme if (GlobalDefineError) 688a5e233bfSThomas Preud'homme return 2; 689ffa9d2e4SAditya Nandakumar 690ffa9d2e4SAditya Nandakumar Req.AllowEmptyInput = AllowEmptyInput; 691ffa9d2e4SAditya Nandakumar Req.EnableVarScope = EnableVarScope; 692ffa9d2e4SAditya Nandakumar Req.AllowDeprecatedDagOverlap = AllowDeprecatedDagOverlap; 693ffa9d2e4SAditya Nandakumar Req.Verbose = Verbose; 694ffa9d2e4SAditya Nandakumar Req.VerboseVerbose = VerboseVerbose; 695ffa9d2e4SAditya Nandakumar Req.NoCanonicalizeWhiteSpace = NoCanonicalizeWhiteSpace; 696ffa9d2e4SAditya Nandakumar Req.MatchFullLines = MatchFullLines; 6975b5b2fd2SKai Nacke Req.IgnoreCase = IgnoreCase; 698ffa9d2e4SAditya Nandakumar 699ffa9d2e4SAditya Nandakumar if (VerboseVerbose) 700ffa9d2e4SAditya Nandakumar Req.Verbose = true; 701ffa9d2e4SAditya Nandakumar 702ffa9d2e4SAditya Nandakumar FileCheck FC(Req); 7032aa0217aSJoel E. Denny if (!FC.ValidateCheckPrefixes()) 704c2735158SRui Ueyama return 2; 705c2735158SRui Ueyama 706ffa9d2e4SAditya Nandakumar Regex PrefixRE = FC.buildCheckPrefixRegex(); 707726774cbSChandler Carruth std::string REError; 708726774cbSChandler Carruth if (!PrefixRE.isValid(REError)) { 709726774cbSChandler Carruth errs() << "Unable to combine check-prefix strings into a prefix regular " 710726774cbSChandler Carruth "expression! This is likely a bug in FileCheck's verification of " 711726774cbSChandler Carruth "the check-prefix strings. Regular expression parsing failed " 712726774cbSChandler Carruth "with the following error: " 713726774cbSChandler Carruth << REError << "\n"; 714726774cbSChandler Carruth return 2; 715726774cbSChandler Carruth } 71613df4626SMatt Arsenault 717ee3c74fbSChris Lattner SourceMgr SM; 718ee3c74fbSChris Lattner 719ee3c74fbSChris Lattner // Read the expected strings from the check file. 72020247900SChandler Carruth ErrorOr<std::unique_ptr<MemoryBuffer>> CheckFileOrErr = 72120247900SChandler Carruth MemoryBuffer::getFileOrSTDIN(CheckFilename); 72220247900SChandler Carruth if (std::error_code EC = CheckFileOrErr.getError()) { 72320247900SChandler Carruth errs() << "Could not open check file '" << CheckFilename 72420247900SChandler Carruth << "': " << EC.message() << '\n'; 72520247900SChandler Carruth return 2; 72620247900SChandler Carruth } 72720247900SChandler Carruth MemoryBuffer &CheckFile = *CheckFileOrErr.get(); 72820247900SChandler Carruth 72920247900SChandler Carruth SmallString<4096> CheckFileBuffer; 730ffa9d2e4SAditya Nandakumar StringRef CheckFileText = FC.CanonicalizeFile(CheckFile, CheckFileBuffer); 73120247900SChandler Carruth 732b5a24610SJoel E. Denny unsigned CheckFileBufferID = 73320247900SChandler Carruth SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 73420247900SChandler Carruth CheckFileText, CheckFile.getBufferIdentifier()), 73520247900SChandler Carruth SMLoc()); 73620247900SChandler Carruth 737b5a24610SJoel E. Denny std::pair<unsigned, unsigned> ImpPatBufferIDRange; 738b5a24610SJoel E. Denny if (FC.readCheckFile(SM, CheckFileText, PrefixRE, &ImpPatBufferIDRange)) 739ee3c74fbSChris Lattner return 2; 740ee3c74fbSChris Lattner 741ee3c74fbSChris Lattner // Open the file to check and add it to SourceMgr. 74220247900SChandler Carruth ErrorOr<std::unique_ptr<MemoryBuffer>> InputFileOrErr = 743adf21f2aSRafael Espindola MemoryBuffer::getFileOrSTDIN(InputFilename); 7446e01cd67SDavid Bozier if (InputFilename == "-") 7456e01cd67SDavid Bozier InputFilename = "<stdin>"; // Overwrite for improved diagnostic messages 74620247900SChandler Carruth if (std::error_code EC = InputFileOrErr.getError()) { 747adf21f2aSRafael Espindola errs() << "Could not open input file '" << InputFilename 748adf21f2aSRafael Espindola << "': " << EC.message() << '\n'; 7498e1c6477SEli Bendersky return 2; 750ee3c74fbSChris Lattner } 75120247900SChandler Carruth MemoryBuffer &InputFile = *InputFileOrErr.get(); 7522c3e5cdfSChris Lattner 75320247900SChandler Carruth if (InputFile.getBufferSize() == 0 && !AllowEmptyInput) { 754b692bed7SChris Lattner errs() << "FileCheck error: '" << InputFilename << "' is empty.\n"; 7552bd4f8b6SXinliang David Li DumpCommandLine(argc, argv); 7568e1c6477SEli Bendersky return 2; 757b692bed7SChris Lattner } 758b692bed7SChris Lattner 75920247900SChandler Carruth SmallString<4096> InputFileBuffer; 760ffa9d2e4SAditya Nandakumar StringRef InputFileText = FC.CanonicalizeFile(InputFile, InputFileBuffer); 7612c3e5cdfSChris Lattner 762e8f2fb20SChandler Carruth SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 763e8f2fb20SChandler Carruth InputFileText, InputFile.getBufferIdentifier()), 764e8f2fb20SChandler Carruth SMLoc()); 765ee3c74fbSChris Lattner 7663c5d267eSJoel E. Denny std::vector<FileCheckDiag> Diags; 76702ada9bdSThomas Preud'homme int ExitCode = FC.checkInput(SM, InputFileText, 7683c5d267eSJoel E. Denny DumpInput == DumpInputNever ? nullptr : &Diags) 7693c5d267eSJoel E. Denny ? EXIT_SUCCESS 7703c5d267eSJoel E. Denny : 1; 7713c5d267eSJoel E. Denny if (DumpInput == DumpInputAlways || 7723c5d267eSJoel E. Denny (ExitCode == 1 && DumpInput == DumpInputFail)) { 7733c5d267eSJoel E. Denny errs() << "\n" 774839f8e4fSJoel E. Denny << "Input file: " << InputFilename << "\n" 7753c5d267eSJoel E. Denny << "Check file: " << CheckFilename << "\n" 7763c5d267eSJoel E. Denny << "\n" 777839f8e4fSJoel E. Denny << "-dump-input=help explains the following input dump.\n" 7783c5d267eSJoel E. Denny << "\n"; 7793c5d267eSJoel E. Denny std::vector<InputAnnotation> Annotations; 7803c5d267eSJoel E. Denny unsigned LabelWidth; 781b5a24610SJoel E. Denny BuildInputAnnotations(SM, CheckFileBufferID, ImpPatBufferIDRange, Diags, 782b5a24610SJoel E. Denny Annotations, LabelWidth); 783*bce8fcedSJoel E. Denny DumpAnnotatedInput(errs(), Req, DumpInputFilterOnError, DumpInputContext, 784*bce8fcedSJoel E. Denny InputFileText, Annotations, LabelWidth); 7853c5d267eSJoel E. Denny } 786346dfbe2SGeorge Karpenkov 787346dfbe2SGeorge Karpenkov return ExitCode; 788ee3c74fbSChris Lattner } 789