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 
185ffd940aSRaphael Isemann #include "llvm/FileCheck/FileCheck.h"
19ee3c74fbSChris Lattner #include "llvm/Support/CommandLine.h"
20197194b6SRui Ueyama #include "llvm/Support/InitLLVM.h"
21ed98c1b3Sserge-sans-paille #include "llvm/Support/MemoryBuffer.h"
223e66509fSJoel E. Denny #include "llvm/Support/Process.h"
23ed98c1b3Sserge-sans-paille #include "llvm/Support/SourceMgr.h"
243c5d267eSJoel E. Denny #include "llvm/Support/WithColor.h"
25ee3c74fbSChris Lattner #include "llvm/Support/raw_ostream.h"
26010982f7SRainer Orth #include <cmath>
27c7c542e8SJoel E. Denny #include <map>
28ee3c74fbSChris Lattner using namespace llvm;
29ee3c74fbSChris Lattner 
30dbb757f4SJoel E. Denny static cl::extrahelp FileCheckOptsEnv(
31dbb757f4SJoel E. Denny     "\nOptions are parsed from the environment variable FILECHECK_OPTS and\n"
32dbb757f4SJoel E. Denny     "from the command line.\n");
33dbb757f4SJoel E. Denny 
34ee3c74fbSChris Lattner static cl::opt<std::string>
353c5d267eSJoel E. Denny     CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Optional);
36ee3c74fbSChris Lattner 
37ee3c74fbSChris Lattner static cl::opt<std::string>
38ee3c74fbSChris Lattner     InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
39ee3c74fbSChris Lattner                   cl::init("-"), cl::value_desc("filename"));
40ee3c74fbSChris Lattner 
41e8f2fb20SChandler Carruth static cl::list<std::string> CheckPrefixes(
42e8f2fb20SChandler Carruth     "check-prefix",
43ee3c74fbSChris Lattner     cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
44fd557cb0SDaniel Sanders static cl::alias CheckPrefixesAlias(
45fd557cb0SDaniel Sanders     "check-prefixes", cl::aliasopt(CheckPrefixes), cl::CommaSeparated,
46fd557cb0SDaniel Sanders     cl::NotHidden,
47fd557cb0SDaniel Sanders     cl::desc(
48fd557cb0SDaniel Sanders         "Alias for -check-prefix permitting multiple comma separated values"));
49ee3c74fbSChris Lattner 
50a1fd1882SJoel E. Denny static cl::list<std::string> CommentPrefixes(
51a1fd1882SJoel E. Denny     "comment-prefixes", cl::CommaSeparated, cl::Hidden,
52a1fd1882SJoel E. Denny     cl::desc("Comma-separated list of comment prefixes to use from check file\n"
53a1fd1882SJoel E. Denny              "(defaults to 'COM,RUN'). Please avoid using this feature in\n"
54a1fd1882SJoel E. Denny              "LLVM's LIT-based test suites, which should be easier to\n"
55a1fd1882SJoel E. Denny              "maintain if they all follow a consistent comment style. This\n"
56a1fd1882SJoel E. Denny              "feature is meant for non-LIT test suites using FileCheck."));
57a1fd1882SJoel E. Denny 
58e8f2fb20SChandler Carruth static cl::opt<bool> NoCanonicalizeWhiteSpace(
59e8f2fb20SChandler Carruth     "strict-whitespace",
602c3e5cdfSChris Lattner     cl::desc("Do not treat all horizontal whitespace as equivalent"));
612c3e5cdfSChris Lattner 
625b5b2fd2SKai Nacke static cl::opt<bool> IgnoreCase(
635b5b2fd2SKai Nacke     "ignore-case",
645b5b2fd2SKai Nacke     cl::desc("Use case-insensitive matching"));
655b5b2fd2SKai Nacke 
6656ccdbbdSAlexander Kornienko static cl::list<std::string> ImplicitCheckNot(
6756ccdbbdSAlexander Kornienko     "implicit-check-not",
6856ccdbbdSAlexander Kornienko     cl::desc("Add an implicit negative check with this pattern to every\n"
6956ccdbbdSAlexander Kornienko              "positive check. This can be used to ensure that no instances of\n"
7056ccdbbdSAlexander Kornienko              "this pattern occur which are not matched by a positive pattern"),
7156ccdbbdSAlexander Kornienko     cl::value_desc("pattern"));
7256ccdbbdSAlexander Kornienko 
73a5e233bfSThomas Preud'homme static cl::list<std::string>
74a5e233bfSThomas Preud'homme     GlobalDefines("D", cl::AlwaysPrefix,
7546e1fd61SAlexander Richardson                   cl::desc("Define a variable to be used in capture patterns."),
7646e1fd61SAlexander Richardson                   cl::value_desc("VAR=VALUE"));
7746e1fd61SAlexander Richardson 
781b9f936fSJustin Bogner static cl::opt<bool> AllowEmptyInput(
791b9f936fSJustin Bogner     "allow-empty", cl::init(false),
801b9f936fSJustin Bogner     cl::desc("Allow the input file to be empty. This is useful when making\n"
811b9f936fSJustin Bogner              "checks that some error message does not occur, for example."));
821b9f936fSJustin Bogner 
83871d658cSMircea Trofin static cl::opt<bool> AllowUnusedPrefixes(
84*557efc9aSFangrui Song     "allow-unused-prefixes",
85871d658cSMircea Trofin     cl::desc("Allow prefixes to be specified but not appear in the test."));
86871d658cSMircea Trofin 
8785913ccaSJames Y Knight static cl::opt<bool> MatchFullLines(
8885913ccaSJames Y Knight     "match-full-lines", cl::init(false),
8985913ccaSJames Y Knight     cl::desc("Require all positive matches to cover an entire input line.\n"
9085913ccaSJames Y Knight              "Allows leading and trailing whitespace if --strict-whitespace\n"
9185913ccaSJames Y Knight              "is not also passed."));
9285913ccaSJames Y Knight 
93f55e72a5SArtem Belevich static cl::opt<bool> EnableVarScope(
94f55e72a5SArtem Belevich     "enable-var-scope", cl::init(false),
95f55e72a5SArtem Belevich     cl::desc("Enables scope for regex variables. Variables with names that\n"
96f55e72a5SArtem Belevich              "do not start with '$' will be reset at the beginning of\n"
97f55e72a5SArtem Belevich              "each CHECK-LABEL block."));
98f55e72a5SArtem Belevich 
99bcf5b441SJoel E. Denny static cl::opt<bool> AllowDeprecatedDagOverlap(
100bcf5b441SJoel E. Denny     "allow-deprecated-dag-overlap", cl::init(false),
101bcf5b441SJoel E. Denny     cl::desc("Enable overlapping among matches in a group of consecutive\n"
102bcf5b441SJoel E. Denny              "CHECK-DAG directives.  This option is deprecated and is only\n"
103bcf5b441SJoel E. Denny              "provided for convenience as old tests are migrated to the new\n"
104bcf5b441SJoel E. Denny              "non-overlapping CHECK-DAG implementation.\n"));
105bcf5b441SJoel E. Denny 
106352695c3SJoel E. Denny static cl::opt<bool> Verbose(
107*557efc9aSFangrui Song     "v",
108352695c3SJoel E. Denny     cl::desc("Print directive pattern matches, or add them to the input dump\n"
109352695c3SJoel E. Denny              "if enabled.\n"));
110dc5ba317SJoel E. Denny 
111dc5ba317SJoel E. Denny static cl::opt<bool> VerboseVerbose(
112*557efc9aSFangrui Song     "vv",
113dc5ba317SJoel E. Denny     cl::desc("Print information helpful in diagnosing internal FileCheck\n"
114352695c3SJoel E. Denny              "issues, or add it to the input dump if enabled.  Implies\n"
115352695c3SJoel E. Denny              "-v.\n"));
1163c5d267eSJoel E. Denny 
117fdde18a7SJoel E. Denny // The order of DumpInputValue members affects their precedence, as documented
118fdde18a7SJoel E. Denny // for -dump-input below.
1193c5d267eSJoel E. Denny enum DumpInputValue {
1203c5d267eSJoel E. Denny   DumpInputNever,
1213c5d267eSJoel E. Denny   DumpInputFail,
122fdde18a7SJoel E. Denny   DumpInputAlways,
123fdde18a7SJoel E. Denny   DumpInputHelp
1243c5d267eSJoel E. Denny };
1253c5d267eSJoel E. Denny 
126fdde18a7SJoel E. Denny static cl::list<DumpInputValue> DumpInputs(
127fdde18a7SJoel E. Denny     "dump-input",
1283c5d267eSJoel E. Denny     cl::desc("Dump input to stderr, adding annotations representing\n"
129fdde18a7SJoel E. Denny              "currently enabled diagnostics.  When there are multiple\n"
130fdde18a7SJoel E. Denny              "occurrences of this option, the <value> that appears earliest\n"
131839f8e4fSJoel E. Denny              "in the list below has precedence.  The default is 'fail'.\n"),
1323c5d267eSJoel E. Denny     cl::value_desc("mode"),
133839f8e4fSJoel E. Denny     cl::values(clEnumValN(DumpInputHelp, "help", "Explain input dump and quit"),
134fdde18a7SJoel E. Denny                clEnumValN(DumpInputAlways, "always", "Always dump input"),
1353c5d267eSJoel E. Denny                clEnumValN(DumpInputFail, "fail", "Dump input on failure"),
136fdde18a7SJoel E. Denny                clEnumValN(DumpInputNever, "never", "Never dump input")));
137dc5ba317SJoel E. Denny 
1389fd4b5faSJoel E. Denny // The order of DumpInputFilterValue members affects their precedence, as
1399fd4b5faSJoel E. Denny // documented for -dump-input-filter below.
1409fd4b5faSJoel E. Denny enum DumpInputFilterValue {
1419fd4b5faSJoel E. Denny   DumpInputFilterError,
1429fd4b5faSJoel E. Denny   DumpInputFilterAnnotation,
1439fd4b5faSJoel E. Denny   DumpInputFilterAnnotationFull,
1449fd4b5faSJoel E. Denny   DumpInputFilterAll
1459fd4b5faSJoel E. Denny };
1469fd4b5faSJoel E. Denny 
1479fd4b5faSJoel E. Denny static cl::list<DumpInputFilterValue> DumpInputFilters(
1489fd4b5faSJoel E. Denny     "dump-input-filter",
1499fd4b5faSJoel E. Denny     cl::desc("In the dump requested by -dump-input, print only input lines of\n"
1506dda6ff0SJoel E. Denny              "kind <value> plus any context specified by -dump-input-context.\n"
1516dda6ff0SJoel E. Denny              "When there are multiple occurrences of this option, the <value>\n"
1529fd4b5faSJoel E. Denny              "that appears earliest in the list below has precedence.  The\n"
1539fd4b5faSJoel E. Denny              "default is 'error' when -dump-input=fail, and it's 'all' when\n"
1549fd4b5faSJoel E. Denny              "-dump-input=always.\n"),
1559fd4b5faSJoel E. Denny     cl::values(clEnumValN(DumpInputFilterAll, "all", "All input lines"),
1569fd4b5faSJoel E. Denny                clEnumValN(DumpInputFilterAnnotationFull, "annotation-full",
1579fd4b5faSJoel E. Denny                           "Input lines with annotations"),
1589fd4b5faSJoel E. Denny                clEnumValN(DumpInputFilterAnnotation, "annotation",
1599fd4b5faSJoel E. Denny                           "Input lines with starting points of annotations"),
1609fd4b5faSJoel E. Denny                clEnumValN(DumpInputFilterError, "error",
1619fd4b5faSJoel E. Denny                           "Input lines with starting points of error "
1629fd4b5faSJoel E. Denny                           "annotations")));
1639fd4b5faSJoel E. Denny 
164bce8fcedSJoel E. Denny static cl::list<unsigned> DumpInputContexts(
165bce8fcedSJoel E. Denny     "dump-input-context", cl::value_desc("N"),
1669fd4b5faSJoel E. Denny     cl::desc("In the dump requested by -dump-input, print <N> input lines\n"
1679fd4b5faSJoel E. Denny              "before and <N> input lines after any lines specified by\n"
1689fd4b5faSJoel E. Denny              "-dump-input-filter.  When there are multiple occurrences of\n"
169bce8fcedSJoel E. Denny              "this option, the largest specified <N> has precedence.  The\n"
170bce8fcedSJoel E. Denny              "default is 5.\n"));
171bce8fcedSJoel E. Denny 
17213df4626SMatt Arsenault typedef cl::list<std::string>::const_iterator prefix_iterator;
17313df4626SMatt Arsenault 
17413df4626SMatt Arsenault 
17513df4626SMatt Arsenault 
176726774cbSChandler Carruth 
177726774cbSChandler Carruth 
178726774cbSChandler Carruth 
179c2735158SRui Ueyama 
DumpCommandLine(int argc,char ** argv)1802bd4f8b6SXinliang David Li static void DumpCommandLine(int argc, char **argv) {
1812bd4f8b6SXinliang David Li   errs() << "FileCheck command line: ";
1822bd4f8b6SXinliang David Li   for (int I = 0; I < argc; I++)
1832bd4f8b6SXinliang David Li     errs() << " " << argv[I];
1842bd4f8b6SXinliang David Li   errs() << "\n";
1852bd4f8b6SXinliang David Li }
1862bd4f8b6SXinliang David Li 
1873c5d267eSJoel E. Denny struct MarkerStyle {
1883c5d267eSJoel E. Denny   /// The starting char (before tildes) for marking the line.
1893c5d267eSJoel E. Denny   char Lead;
1903c5d267eSJoel E. Denny   /// What color to use for this annotation.
1914d41c332SRui Ueyama   raw_ostream::Colors Color;
1923c5d267eSJoel E. Denny   /// A note to follow the marker, or empty string if none.
1933c5d267eSJoel E. Denny   std::string Note;
1949fd4b5faSJoel E. Denny   /// Does this marker indicate inclusion by -dump-input-filter=error?
195bce8fcedSJoel E. Denny   bool FiltersAsError;
MarkerStyleMarkerStyle1963c5d267eSJoel E. Denny   MarkerStyle() {}
MarkerStyleMarkerStyle1974d41c332SRui Ueyama   MarkerStyle(char Lead, raw_ostream::Colors Color,
198bce8fcedSJoel E. Denny               const std::string &Note = "", bool FiltersAsError = false)
199bce8fcedSJoel E. Denny       : Lead(Lead), Color(Color), Note(Note), FiltersAsError(FiltersAsError) {
200bce8fcedSJoel E. Denny     assert((!FiltersAsError || !Note.empty()) &&
201bce8fcedSJoel E. Denny            "expected error diagnostic to have note");
202bce8fcedSJoel E. Denny   }
2033c5d267eSJoel E. Denny };
2043c5d267eSJoel E. Denny 
GetMarker(FileCheckDiag::MatchType MatchTy)2053c5d267eSJoel E. Denny static MarkerStyle GetMarker(FileCheckDiag::MatchType MatchTy) {
2063c5d267eSJoel E. Denny   switch (MatchTy) {
207e2afb614SJoel E. Denny   case FileCheckDiag::MatchFoundAndExpected:
2087df86967SJoel E. Denny     return MarkerStyle('^', raw_ostream::GREEN);
209e2afb614SJoel E. Denny   case FileCheckDiag::MatchFoundButExcluded:
210bce8fcedSJoel E. Denny     return MarkerStyle('!', raw_ostream::RED, "error: no match expected",
211bce8fcedSJoel E. Denny                        /*FiltersAsError=*/true);
212e2afb614SJoel E. Denny   case FileCheckDiag::MatchFoundButWrongLine:
213bce8fcedSJoel E. Denny     return MarkerStyle('!', raw_ostream::RED, "error: match on wrong line",
214bce8fcedSJoel E. Denny                        /*FiltersAsError=*/true);
215e2afb614SJoel E. Denny   case FileCheckDiag::MatchFoundButDiscarded:
216f7c1c4d8SJoel E. Denny     return MarkerStyle('!', raw_ostream::CYAN,
217f7c1c4d8SJoel E. Denny                        "discard: overlaps earlier match");
218dd59c132SJoel E. Denny   case FileCheckDiag::MatchFoundErrorNote:
219dd59c132SJoel E. Denny     // Note should always be overridden within the FileCheckDiag.
220dd59c132SJoel E. Denny     return MarkerStyle('!', raw_ostream::RED,
221dd59c132SJoel E. Denny                        "error: unknown error after match",
222dd59c132SJoel E. Denny                        /*FiltersAsError=*/true);
22396f0e84cSJoel E. Denny   case FileCheckDiag::MatchNoneAndExcluded:
22496f0e84cSJoel E. Denny     return MarkerStyle('X', raw_ostream::GREEN);
2253c5d267eSJoel E. Denny   case FileCheckDiag::MatchNoneButExpected:
226bce8fcedSJoel E. Denny     return MarkerStyle('X', raw_ostream::RED, "error: no match found",
227bce8fcedSJoel E. Denny                        /*FiltersAsError=*/true);
228dd59c132SJoel E. Denny   case FileCheckDiag::MatchNoneForInvalidPattern:
229dd59c132SJoel E. Denny     return MarkerStyle('X', raw_ostream::RED,
230dd59c132SJoel E. Denny                        "error: match failed for invalid pattern",
231dd59c132SJoel E. Denny                        /*FiltersAsError=*/true);
2322c007c80SJoel E. Denny   case FileCheckDiag::MatchFuzzy:
233bce8fcedSJoel E. Denny     return MarkerStyle('?', raw_ostream::MAGENTA, "possible intended match",
234bce8fcedSJoel E. Denny                        /*FiltersAsError=*/true);
2353c5d267eSJoel E. Denny   }
2363c5d267eSJoel E. Denny   llvm_unreachable_internal("unexpected match type");
2373c5d267eSJoel E. Denny }
2383c5d267eSJoel E. Denny 
DumpInputAnnotationHelp(raw_ostream & OS)2393c5d267eSJoel E. Denny static void DumpInputAnnotationHelp(raw_ostream &OS) {
2403c5d267eSJoel E. Denny   OS << "The following description was requested by -dump-input=help to\n"
241839f8e4fSJoel E. Denny      << "explain the input dump printed by FileCheck.\n"
242839f8e4fSJoel E. Denny      << "\n"
243839f8e4fSJoel E. Denny      << "Related command-line options:\n"
2446dda6ff0SJoel E. Denny      << "\n"
245839f8e4fSJoel E. Denny      << "  - -dump-input=<value> enables or disables the input dump\n"
2466dda6ff0SJoel E. Denny      << "  - -dump-input-filter=<value> filters the input lines\n"
2479fd4b5faSJoel E. Denny      << "  - -dump-input-context=<N> adjusts the context of filtered lines\n"
248839f8e4fSJoel E. Denny      << "  - -v and -vv add more annotations\n"
249839f8e4fSJoel E. Denny      << "  - -color forces colors to be enabled both in the dump and below\n"
250839f8e4fSJoel E. Denny      << "  - -help documents the above options in more detail\n"
251839f8e4fSJoel E. Denny      << "\n"
2526dda6ff0SJoel E. Denny      << "These options can also be set via FILECHECK_OPTS.  For example, for\n"
2536dda6ff0SJoel E. Denny      << "maximum debugging output on failures:\n"
2546dda6ff0SJoel E. Denny      << "\n"
2556dda6ff0SJoel E. Denny      << "  $ FILECHECK_OPTS='-dump-input-filter=all -vv -color' ninja check\n"
2566dda6ff0SJoel E. Denny      << "\n"
2576dda6ff0SJoel E. Denny      << "Input dump annotation format:\n"
2586dda6ff0SJoel E. Denny      << "\n";
2593c5d267eSJoel E. Denny 
2603c5d267eSJoel E. Denny   // Labels for input lines.
2613c5d267eSJoel E. Denny   OS << "  - ";
2623c5d267eSJoel E. Denny   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "L:";
26309c35739SThomas Preud'homme   OS << "     labels line number L of the input file\n"
26409c35739SThomas Preud'homme      << "           An extra space is added after each input line to represent"
26509c35739SThomas Preud'homme      << " the\n"
26609c35739SThomas Preud'homme      << "           newline character\n";
2673c5d267eSJoel E. Denny 
2683c5d267eSJoel E. Denny   // Labels for annotation lines.
2693c5d267eSJoel E. Denny   OS << "  - ";
2703c5d267eSJoel E. Denny   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L";
271b5a24610SJoel E. Denny   OS << "    labels the only match result for either (1) a pattern of type T"
272b5a24610SJoel E. Denny      << " from\n"
273b5a24610SJoel E. Denny      << "           line L of the check file if L is an integer or (2) the"
274b5a24610SJoel E. Denny      << " I-th implicit\n"
275b5a24610SJoel E. Denny      << "           pattern if L is \"imp\" followed by an integer "
276b5a24610SJoel E. Denny      << "I (index origin one)\n";
2772c007c80SJoel E. Denny   OS << "  - ";
2782c007c80SJoel E. Denny   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L'N";
279b5a24610SJoel E. Denny   OS << "  labels the Nth match result for such a pattern\n";
2803c5d267eSJoel E. Denny 
2813c5d267eSJoel E. Denny   // Markers on annotation lines.
2823c5d267eSJoel E. Denny   OS << "  - ";
2837df86967SJoel E. Denny   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "^~~";
2847df86967SJoel E. Denny   OS << "    marks good match (reported if -v)\n"
2857df86967SJoel E. Denny      << "  - ";
286cadfcef4SJoel E. Denny   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "!~~";
287cadfcef4SJoel E. Denny   OS << "    marks bad match, such as:\n"
288cadfcef4SJoel E. Denny      << "           - CHECK-NEXT on same line as previous match (error)\n"
2890e7e3fa0SJoel E. Denny      << "           - CHECK-NOT found (error)\n"
290f7c1c4d8SJoel E. Denny      << "           - CHECK-DAG overlapping match (discarded, reported if "
291f7c1c4d8SJoel E. Denny      << "-vv)\n"
292cadfcef4SJoel E. Denny      << "  - ";
2933c5d267eSJoel E. Denny   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "X~~";
294cadfcef4SJoel E. Denny   OS << "    marks search range when no match is found, such as:\n"
295cadfcef4SJoel E. Denny      << "           - CHECK-NEXT not found (error)\n"
29696f0e84cSJoel E. Denny      << "           - CHECK-NOT not found (success, reported if -vv)\n"
297f7c1c4d8SJoel E. Denny      << "           - CHECK-DAG not found after discarded matches (error)\n"
2982c007c80SJoel E. Denny      << "  - ";
2992c007c80SJoel E. Denny   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "?";
3002c007c80SJoel E. Denny   OS << "      marks fuzzy match when no match is found\n";
3013c5d267eSJoel E. Denny 
302bce8fcedSJoel E. Denny   // Elided lines.
303bce8fcedSJoel E. Denny   OS << "  - ";
304bce8fcedSJoel E. Denny   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "...";
305bce8fcedSJoel E. Denny   OS << "    indicates elided input lines and annotations, as specified by\n"
3069fd4b5faSJoel E. Denny      << "           -dump-input-filter and -dump-input-context\n";
307bce8fcedSJoel E. Denny 
3083c5d267eSJoel E. Denny   // Colors.
3093c5d267eSJoel E. Denny   OS << "  - colors ";
3107df86967SJoel E. Denny   WithColor(OS, raw_ostream::GREEN, true) << "success";
3117df86967SJoel E. Denny   OS << ", ";
3123c5d267eSJoel E. Denny   WithColor(OS, raw_ostream::RED, true) << "error";
3132c007c80SJoel E. Denny   OS << ", ";
3142c007c80SJoel E. Denny   WithColor(OS, raw_ostream::MAGENTA, true) << "fuzzy match";
3157df86967SJoel E. Denny   OS << ", ";
316f7c1c4d8SJoel E. Denny   WithColor(OS, raw_ostream::CYAN, true, false) << "discarded match";
317f7c1c4d8SJoel E. Denny   OS << ", ";
3187df86967SJoel E. Denny   WithColor(OS, raw_ostream::CYAN, true, true) << "unmatched input";
319839f8e4fSJoel E. Denny   OS << "\n";
3203c5d267eSJoel E. Denny }
3213c5d267eSJoel E. Denny 
3223c5d267eSJoel E. Denny /// An annotation for a single input line.
3233c5d267eSJoel E. Denny struct InputAnnotation {
324ce685455SJoel E. Denny   /// The index of the match result across all checks
325ce685455SJoel E. Denny   unsigned DiagIndex;
3263c5d267eSJoel E. Denny   /// The label for this annotation.
3273c5d267eSJoel E. Denny   std::string Label;
328bce8fcedSJoel E. Denny   /// Is this the initial fragment of a diagnostic that has been broken across
329bce8fcedSJoel E. Denny   /// multiple lines?
330bce8fcedSJoel E. Denny   bool IsFirstLine;
3313c5d267eSJoel E. Denny   /// What input line (one-origin indexing) this annotation marks.  This might
332bce8fcedSJoel E. Denny   /// be different from the starting line of the original diagnostic if
333bce8fcedSJoel E. Denny   /// !IsFirstLine.
3343c5d267eSJoel E. Denny   unsigned InputLine;
335ce685455SJoel E. Denny   /// The column range (one-origin indexing, open end) in which to mark the
3363c5d267eSJoel E. Denny   /// input line.  If InputEndCol is UINT_MAX, treat it as the last column
3373c5d267eSJoel E. Denny   /// before the newline.
3383c5d267eSJoel E. Denny   unsigned InputStartCol, InputEndCol;
3393c5d267eSJoel E. Denny   /// The marker to use.
3403c5d267eSJoel E. Denny   MarkerStyle Marker;
341e2afb614SJoel E. Denny   /// Whether this annotation represents a good match for an expected pattern.
342e2afb614SJoel E. Denny   bool FoundAndExpectedMatch;
3433c5d267eSJoel E. Denny };
3443c5d267eSJoel E. Denny 
3453c5d267eSJoel E. Denny /// Get an abbreviation for the check type.
GetCheckTypeAbbreviation(Check::FileCheckType Ty)34620e9c36cSFangrui Song static std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) {
3473c5d267eSJoel E. Denny   switch (Ty) {
3483c5d267eSJoel E. Denny   case Check::CheckPlain:
3493c5d267eSJoel E. Denny     if (Ty.getCount() > 1)
3503c5d267eSJoel E. Denny       return "count";
3513c5d267eSJoel E. Denny     return "check";
3523c5d267eSJoel E. Denny   case Check::CheckNext:
3533c5d267eSJoel E. Denny     return "next";
3543c5d267eSJoel E. Denny   case Check::CheckSame:
3553c5d267eSJoel E. Denny     return "same";
3563c5d267eSJoel E. Denny   case Check::CheckNot:
3573c5d267eSJoel E. Denny     return "not";
3583c5d267eSJoel E. Denny   case Check::CheckDAG:
3593c5d267eSJoel E. Denny     return "dag";
3603c5d267eSJoel E. Denny   case Check::CheckLabel:
3613c5d267eSJoel E. Denny     return "label";
3623c5d267eSJoel E. Denny   case Check::CheckEmpty:
3633c5d267eSJoel E. Denny     return "empty";
364a1fd1882SJoel E. Denny   case Check::CheckComment:
365a1fd1882SJoel E. Denny     return "com";
3663c5d267eSJoel E. Denny   case Check::CheckEOF:
3673c5d267eSJoel E. Denny     return "eof";
3683c5d267eSJoel E. Denny   case Check::CheckBadNot:
3693c5d267eSJoel E. Denny     return "bad-not";
3703c5d267eSJoel E. Denny   case Check::CheckBadCount:
3713c5d267eSJoel E. Denny     return "bad-count";
3728894c05bSIvan Kosarev   case Check::CheckMisspelled:
3738894c05bSIvan Kosarev     return "misspelled";
3743c5d267eSJoel E. Denny   case Check::CheckNone:
3753c5d267eSJoel E. Denny     llvm_unreachable("invalid FileCheckType");
3763c5d267eSJoel E. Denny   }
3773c5d267eSJoel E. Denny   llvm_unreachable("unknown FileCheckType");
3783c5d267eSJoel E. Denny }
3793c5d267eSJoel E. Denny 
380b5a24610SJoel E. Denny static void
BuildInputAnnotations(const SourceMgr & SM,unsigned CheckFileBufferID,const std::pair<unsigned,unsigned> & ImpPatBufferIDRange,const std::vector<FileCheckDiag> & Diags,std::vector<InputAnnotation> & Annotations,unsigned & LabelWidth)381b5a24610SJoel E. Denny BuildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
382b5a24610SJoel E. Denny                       const std::pair<unsigned, unsigned> &ImpPatBufferIDRange,
383b5a24610SJoel E. Denny                       const std::vector<FileCheckDiag> &Diags,
3843c5d267eSJoel E. Denny                       std::vector<InputAnnotation> &Annotations,
3853c5d267eSJoel E. Denny                       unsigned &LabelWidth) {
386c7c542e8SJoel E. Denny   struct CompareSMLoc {
38743279d1dSJoel E. Denny     bool operator()(const SMLoc &LHS, const SMLoc &RHS) const {
388c7c542e8SJoel E. Denny       return LHS.getPointer() < RHS.getPointer();
389c7c542e8SJoel E. Denny     }
390c7c542e8SJoel E. Denny   };
391c7c542e8SJoel E. Denny   // How many diagnostics does each pattern have?
392c7c542e8SJoel E. Denny   std::map<SMLoc, unsigned, CompareSMLoc> DiagCountPerPattern;
393c7c542e8SJoel E. Denny   for (auto Diag : Diags)
394c7c542e8SJoel E. Denny     ++DiagCountPerPattern[Diag.CheckLoc];
395c7c542e8SJoel E. Denny   // How many diagnostics have we seen so far per pattern?
396c7c542e8SJoel E. Denny   std::map<SMLoc, unsigned, CompareSMLoc> DiagIndexPerPattern;
397c7c542e8SJoel E. Denny   // How many total diagnostics have we seen so far?
398c7c542e8SJoel E. Denny   unsigned DiagIndex = 0;
3993c5d267eSJoel E. Denny   // What's the widest label?
4003c5d267eSJoel E. Denny   LabelWidth = 0;
4013c5d267eSJoel E. Denny   for (auto DiagItr = Diags.begin(), DiagEnd = Diags.end(); DiagItr != DiagEnd;
4023c5d267eSJoel E. Denny        ++DiagItr) {
4033c5d267eSJoel E. Denny     InputAnnotation A;
404c7c542e8SJoel E. Denny     A.DiagIndex = DiagIndex++;
4053c5d267eSJoel E. Denny 
4063c5d267eSJoel E. Denny     // Build label, which uniquely identifies this check result.
407b5a24610SJoel E. Denny     unsigned CheckBufferID = SM.FindBufferContainingLoc(DiagItr->CheckLoc);
408b5a24610SJoel E. Denny     auto CheckLineAndCol =
409b5a24610SJoel E. Denny         SM.getLineAndColumn(DiagItr->CheckLoc, CheckBufferID);
4103c5d267eSJoel E. Denny     llvm::raw_string_ostream Label(A.Label);
411b5a24610SJoel E. Denny     Label << GetCheckTypeAbbreviation(DiagItr->CheckTy) << ":";
412b5a24610SJoel E. Denny     if (CheckBufferID == CheckFileBufferID)
413b5a24610SJoel E. Denny       Label << CheckLineAndCol.first;
414b5a24610SJoel E. Denny     else if (ImpPatBufferIDRange.first <= CheckBufferID &&
415b5a24610SJoel E. Denny              CheckBufferID < ImpPatBufferIDRange.second)
416b5a24610SJoel E. Denny       Label << "imp" << (CheckBufferID - ImpPatBufferIDRange.first + 1);
417b5a24610SJoel E. Denny     else
418b5a24610SJoel E. Denny       llvm_unreachable("expected diagnostic's check location to be either in "
419b5a24610SJoel E. Denny                        "the check file or for an implicit pattern");
420c7c542e8SJoel E. Denny     if (DiagCountPerPattern[DiagItr->CheckLoc] > 1)
421c7c542e8SJoel E. Denny       Label << "'" << DiagIndexPerPattern[DiagItr->CheckLoc]++;
4223c5d267eSJoel E. Denny     LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size());
4233c5d267eSJoel E. Denny 
424608f2bfdSJoel E. Denny     A.Marker = GetMarker(DiagItr->MatchTy);
425d680711bSJoel E. Denny     if (!DiagItr->Note.empty()) {
426d680711bSJoel E. Denny       A.Marker.Note = DiagItr->Note;
427d680711bSJoel E. Denny       // It's less confusing if notes that don't actually have ranges don't have
428d680711bSJoel E. Denny       // markers.  For example, a marker for 'with "VAR" equal to "5"' would
429d680711bSJoel E. Denny       // seem to indicate where "VAR" matches, but the location we actually have
430d680711bSJoel E. Denny       // for the marker simply points to the start of the match/search range for
431d680711bSJoel E. Denny       // the full pattern of which the substitution is potentially just one
432d680711bSJoel E. Denny       // component.
433d680711bSJoel E. Denny       if (DiagItr->InputStartLine == DiagItr->InputEndLine &&
434d680711bSJoel E. Denny           DiagItr->InputStartCol == DiagItr->InputEndCol)
435d680711bSJoel E. Denny         A.Marker.Lead = ' ';
436d680711bSJoel E. Denny     }
437dd59c132SJoel E. Denny     if (DiagItr->MatchTy == FileCheckDiag::MatchFoundErrorNote) {
438dd59c132SJoel E. Denny       assert(!DiagItr->Note.empty() &&
439dd59c132SJoel E. Denny              "expected custom note for MatchFoundErrorNote");
440dd59c132SJoel E. Denny       A.Marker.Note = "error: " + A.Marker.Note;
441dd59c132SJoel E. Denny     }
442e2afb614SJoel E. Denny     A.FoundAndExpectedMatch =
443e2afb614SJoel E. Denny         DiagItr->MatchTy == FileCheckDiag::MatchFoundAndExpected;
4443c5d267eSJoel E. Denny 
4453c5d267eSJoel E. Denny     // Compute the mark location, and break annotation into multiple
4463c5d267eSJoel E. Denny     // annotations if it spans multiple lines.
447bce8fcedSJoel E. Denny     A.IsFirstLine = true;
4483c5d267eSJoel E. Denny     A.InputLine = DiagItr->InputStartLine;
4493c5d267eSJoel E. Denny     A.InputStartCol = DiagItr->InputStartCol;
4503c5d267eSJoel E. Denny     if (DiagItr->InputStartLine == DiagItr->InputEndLine) {
4513c5d267eSJoel E. Denny       // Sometimes ranges are empty in order to indicate a specific point, but
4523c5d267eSJoel E. Denny       // that would mean nothing would be marked, so adjust the range to
4533c5d267eSJoel E. Denny       // include the following character.
4543c5d267eSJoel E. Denny       A.InputEndCol =
4553c5d267eSJoel E. Denny           std::max(DiagItr->InputStartCol + 1, DiagItr->InputEndCol);
4563c5d267eSJoel E. Denny       Annotations.push_back(A);
4573c5d267eSJoel E. Denny     } else {
4583c5d267eSJoel E. Denny       assert(DiagItr->InputStartLine < DiagItr->InputEndLine &&
4593c5d267eSJoel E. Denny              "expected input range not to be inverted");
4603c5d267eSJoel E. Denny       A.InputEndCol = UINT_MAX;
4613c5d267eSJoel E. Denny       Annotations.push_back(A);
4623c5d267eSJoel E. Denny       for (unsigned L = DiagItr->InputStartLine + 1, E = DiagItr->InputEndLine;
4633c5d267eSJoel E. Denny            L <= E; ++L) {
4643c5d267eSJoel E. Denny         // If a range ends before the first column on a line, then it has no
4653c5d267eSJoel E. Denny         // characters on that line, so there's nothing to render.
466608f2bfdSJoel E. Denny         if (DiagItr->InputEndCol == 1 && L == E)
4673c5d267eSJoel E. Denny           break;
4683c5d267eSJoel E. Denny         InputAnnotation B;
469ce685455SJoel E. Denny         B.DiagIndex = A.DiagIndex;
4703c5d267eSJoel E. Denny         B.Label = A.Label;
471bce8fcedSJoel E. Denny         B.IsFirstLine = false;
4723c5d267eSJoel E. Denny         B.InputLine = L;
473608f2bfdSJoel E. Denny         B.Marker = A.Marker;
4743c5d267eSJoel E. Denny         B.Marker.Lead = '~';
4753c5d267eSJoel E. Denny         B.Marker.Note = "";
476608f2bfdSJoel E. Denny         B.InputStartCol = 1;
477608f2bfdSJoel E. Denny         if (L != E)
478608f2bfdSJoel E. Denny           B.InputEndCol = UINT_MAX;
479608f2bfdSJoel E. Denny         else
4803c5d267eSJoel E. Denny           B.InputEndCol = DiagItr->InputEndCol;
481e2afb614SJoel E. Denny         B.FoundAndExpectedMatch = A.FoundAndExpectedMatch;
4823c5d267eSJoel E. Denny         Annotations.push_back(B);
4833c5d267eSJoel E. Denny       }
4843c5d267eSJoel E. Denny     }
4853c5d267eSJoel E. Denny   }
4863c5d267eSJoel E. Denny }
4873c5d267eSJoel E. Denny 
FindInputLineInFilter(DumpInputFilterValue DumpInputFilter,unsigned CurInputLine,const std::vector<InputAnnotation>::iterator & AnnotationBeg,const std::vector<InputAnnotation>::iterator & AnnotationEnd)488bce8fcedSJoel E. Denny static unsigned FindInputLineInFilter(
4899fd4b5faSJoel E. Denny     DumpInputFilterValue DumpInputFilter, unsigned CurInputLine,
490bce8fcedSJoel E. Denny     const std::vector<InputAnnotation>::iterator &AnnotationBeg,
491bce8fcedSJoel E. Denny     const std::vector<InputAnnotation>::iterator &AnnotationEnd) {
4929fd4b5faSJoel E. Denny   if (DumpInputFilter == DumpInputFilterAll)
493bce8fcedSJoel E. Denny     return CurInputLine;
494bce8fcedSJoel E. Denny   for (auto AnnotationItr = AnnotationBeg; AnnotationItr != AnnotationEnd;
495bce8fcedSJoel E. Denny        ++AnnotationItr) {
4969fd4b5faSJoel E. Denny     switch (DumpInputFilter) {
4979fd4b5faSJoel E. Denny     case DumpInputFilterAll:
4989fd4b5faSJoel E. Denny       llvm_unreachable("unexpected DumpInputFilterAll");
4999fd4b5faSJoel E. Denny       break;
5009fd4b5faSJoel E. Denny     case DumpInputFilterAnnotationFull:
5019fd4b5faSJoel E. Denny       return AnnotationItr->InputLine;
5029fd4b5faSJoel E. Denny     case DumpInputFilterAnnotation:
5039fd4b5faSJoel E. Denny       if (AnnotationItr->IsFirstLine)
5049fd4b5faSJoel E. Denny         return AnnotationItr->InputLine;
5059fd4b5faSJoel E. Denny       break;
5069fd4b5faSJoel E. Denny     case DumpInputFilterError:
507bce8fcedSJoel E. Denny       if (AnnotationItr->IsFirstLine && AnnotationItr->Marker.FiltersAsError)
508bce8fcedSJoel E. Denny         return AnnotationItr->InputLine;
5099fd4b5faSJoel E. Denny       break;
5109fd4b5faSJoel E. Denny     }
511bce8fcedSJoel E. Denny   }
512bce8fcedSJoel E. Denny   return UINT_MAX;
513bce8fcedSJoel E. Denny }
514bce8fcedSJoel E. Denny 
51577b6ddf1SJoel E. Denny /// To OS, print a vertical ellipsis (right-justified at LabelWidth) if it would
51677b6ddf1SJoel E. Denny /// occupy less lines than ElidedLines, but print ElidedLines otherwise.  Either
51777b6ddf1SJoel E. Denny /// way, clear ElidedLines.  Thus, if ElidedLines is empty, do nothing.
DumpEllipsisOrElidedLines(raw_ostream & OS,std::string & ElidedLines,unsigned LabelWidth)51877b6ddf1SJoel E. Denny static void DumpEllipsisOrElidedLines(raw_ostream &OS, std::string &ElidedLines,
51977b6ddf1SJoel E. Denny                                       unsigned LabelWidth) {
52077b6ddf1SJoel E. Denny   if (ElidedLines.empty())
52177b6ddf1SJoel E. Denny     return;
52277b6ddf1SJoel E. Denny   unsigned EllipsisLines = 3;
52377b6ddf1SJoel E. Denny   if (EllipsisLines < StringRef(ElidedLines).count('\n')) {
52477b6ddf1SJoel E. Denny     for (unsigned i = 0; i < EllipsisLines; ++i) {
52577b6ddf1SJoel E. Denny       WithColor(OS, raw_ostream::BLACK, /*Bold=*/true)
52677b6ddf1SJoel E. Denny           << right_justify(".", LabelWidth);
52777b6ddf1SJoel E. Denny       OS << '\n';
52877b6ddf1SJoel E. Denny     }
52977b6ddf1SJoel E. Denny   } else
53077b6ddf1SJoel E. Denny     OS << ElidedLines;
53177b6ddf1SJoel E. Denny   ElidedLines.clear();
53277b6ddf1SJoel E. Denny }
53377b6ddf1SJoel E. Denny 
DumpAnnotatedInput(raw_ostream & OS,const FileCheckRequest & Req,DumpInputFilterValue DumpInputFilter,unsigned DumpInputContext,StringRef InputFileText,std::vector<InputAnnotation> & Annotations,unsigned LabelWidth)5347df86967SJoel E. Denny static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req,
5359fd4b5faSJoel E. Denny                                DumpInputFilterValue DumpInputFilter,
536bce8fcedSJoel E. Denny                                unsigned DumpInputContext,
5377df86967SJoel E. Denny                                StringRef InputFileText,
5387df86967SJoel E. Denny                                std::vector<InputAnnotation> &Annotations,
5397df86967SJoel E. Denny                                unsigned LabelWidth) {
540bce8fcedSJoel E. Denny   OS << "Input was:\n<<<<<<\n";
5413c5d267eSJoel E. Denny 
5423c5d267eSJoel E. Denny   // Sort annotations.
543a396e2e0SKazu Hirata   llvm::sort(Annotations,
5443c5d267eSJoel E. Denny              [](const InputAnnotation &A, const InputAnnotation &B) {
545ce685455SJoel E. Denny                // 1. Sort annotations in the order of the input lines.
546ce685455SJoel E. Denny                //
547ce685455SJoel E. Denny                // This makes it easier to find relevant annotations while
548ce685455SJoel E. Denny                // iterating input lines in the implementation below.  FileCheck
549ce685455SJoel E. Denny                // does not always produce diagnostics in the order of input
550ce685455SJoel E. Denny                // lines due to, for example, CHECK-DAG and CHECK-NOT.
5513c5d267eSJoel E. Denny                if (A.InputLine != B.InputLine)
5523c5d267eSJoel E. Denny                  return A.InputLine < B.InputLine;
553ce685455SJoel E. Denny                // 2. Sort annotations in the temporal order FileCheck produced
554ce685455SJoel E. Denny                // their associated diagnostics.
555ce685455SJoel E. Denny                //
556ce685455SJoel E. Denny                // This sort offers several benefits:
557ce685455SJoel E. Denny                //
558ce685455SJoel E. Denny                // A. On a single input line, the order of annotations reflects
559ce685455SJoel E. Denny                //    the FileCheck logic for processing directives/patterns.
560ce685455SJoel E. Denny                //    This can be helpful in understanding cases in which the
561ce685455SJoel E. Denny                //    order of the associated directives/patterns in the check
562ce685455SJoel E. Denny                //    file or on the command line either (i) does not match the
563ce685455SJoel E. Denny                //    temporal order in which FileCheck looks for matches for the
564ce685455SJoel E. Denny                //    directives/patterns (due to, for example, CHECK-LABEL,
565ce685455SJoel E. Denny                //    CHECK-NOT, or `--implicit-check-not`) or (ii) does match
566ce685455SJoel E. Denny                //    that order but does not match the order of those
567ce685455SJoel E. Denny                //    diagnostics along an input line (due to, for example,
568ce685455SJoel E. Denny                //    CHECK-DAG).
569ce685455SJoel E. Denny                //
570ce685455SJoel E. Denny                //    On the other hand, because our presentation format presents
571ce685455SJoel E. Denny                //    input lines in order, there's no clear way to offer the
572ce685455SJoel E. Denny                //    same benefit across input lines.  For consistency, it might
573ce685455SJoel E. Denny                //    then seem worthwhile to have annotations on a single line
574ce685455SJoel E. Denny                //    also sorted in input order (that is, by input column).
575ce685455SJoel E. Denny                //    However, in practice, this appears to be more confusing
576ce685455SJoel E. Denny                //    than helpful.  Perhaps it's intuitive to expect annotations
577ce685455SJoel E. Denny                //    to be listed in the temporal order in which they were
578ce685455SJoel E. Denny                //    produced except in cases the presentation format obviously
579ce685455SJoel E. Denny                //    and inherently cannot support it (that is, across input
580ce685455SJoel E. Denny                //    lines).
581ce685455SJoel E. Denny                //
582ce685455SJoel E. Denny                // B. When diagnostics' annotations are split among multiple
583ce685455SJoel E. Denny                //    input lines, the user must track them from one input line
584ce685455SJoel E. Denny                //    to the next.  One property of the sort chosen here is that
585ce685455SJoel E. Denny                //    it facilitates the user in this regard by ensuring the
586ce685455SJoel E. Denny                //    following: when comparing any two input lines, a
587ce685455SJoel E. Denny                //    diagnostic's annotations are sorted in the same position
588ce685455SJoel E. Denny                //    relative to all other diagnostics' annotations.
589ce685455SJoel E. Denny                return A.DiagIndex < B.DiagIndex;
5903c5d267eSJoel E. Denny              });
5913c5d267eSJoel E. Denny 
5923c5d267eSJoel E. Denny   // Compute the width of the label column.
5933c5d267eSJoel E. Denny   const unsigned char *InputFilePtr = InputFileText.bytes_begin(),
5943c5d267eSJoel E. Denny                       *InputFileEnd = InputFileText.bytes_end();
5953c5d267eSJoel E. Denny   unsigned LineCount = InputFileText.count('\n');
5963c5d267eSJoel E. Denny   if (InputFileEnd[-1] != '\n')
5973c5d267eSJoel E. Denny     ++LineCount;
598010982f7SRainer Orth   unsigned LineNoWidth = std::log10(LineCount) + 1;
5993c5d267eSJoel E. Denny   // +3 below adds spaces (1) to the left of the (right-aligned) line numbers
6003c5d267eSJoel E. Denny   // on input lines and (2) to the right of the (left-aligned) labels on
6013c5d267eSJoel E. Denny   // annotation lines so that input lines and annotation lines are more
6023c5d267eSJoel E. Denny   // visually distinct.  For example, the spaces on the annotation lines ensure
6033c5d267eSJoel E. Denny   // that input line numbers and check directive line numbers never align
6043c5d267eSJoel E. Denny   // horizontally.  Those line numbers might not even be for the same file.
6053c5d267eSJoel E. Denny   // One space would be enough to achieve that, but more makes it even easier
6063c5d267eSJoel E. Denny   // to see.
6073c5d267eSJoel E. Denny   LabelWidth = std::max(LabelWidth, LineNoWidth) + 3;
6083c5d267eSJoel E. Denny 
6093c5d267eSJoel E. Denny   // Print annotated input lines.
610bce8fcedSJoel E. Denny   unsigned PrevLineInFilter = 0; // 0 means none so far
611bce8fcedSJoel E. Denny   unsigned NextLineInFilter = 0; // 0 means uncomputed, UINT_MAX means none
61277b6ddf1SJoel E. Denny   std::string ElidedLines;
61377b6ddf1SJoel E. Denny   raw_string_ostream ElidedLinesOS(ElidedLines);
61477b6ddf1SJoel E. Denny   ColorMode TheColorMode =
61577b6ddf1SJoel E. Denny       WithColor(OS).colorsEnabled() ? ColorMode::Enable : ColorMode::Disable;
61677b6ddf1SJoel E. Denny   if (TheColorMode == ColorMode::Enable)
61777b6ddf1SJoel E. Denny     ElidedLinesOS.enable_colors(true);
6183c5d267eSJoel E. Denny   auto AnnotationItr = Annotations.begin(), AnnotationEnd = Annotations.end();
6193c5d267eSJoel E. Denny   for (unsigned Line = 1;
6203c5d267eSJoel E. Denny        InputFilePtr != InputFileEnd || AnnotationItr != AnnotationEnd;
6213c5d267eSJoel E. Denny        ++Line) {
6223c5d267eSJoel E. Denny     const unsigned char *InputFileLine = InputFilePtr;
6233c5d267eSJoel E. Denny 
624bce8fcedSJoel E. Denny     // Compute the previous and next line included by the filter.
625bce8fcedSJoel E. Denny     if (NextLineInFilter < Line)
6269fd4b5faSJoel E. Denny       NextLineInFilter = FindInputLineInFilter(DumpInputFilter, Line,
627bce8fcedSJoel E. Denny                                                AnnotationItr, AnnotationEnd);
628bce8fcedSJoel E. Denny     assert(NextLineInFilter && "expected NextLineInFilter to be computed");
629bce8fcedSJoel E. Denny     if (NextLineInFilter == Line)
630bce8fcedSJoel E. Denny       PrevLineInFilter = Line;
631bce8fcedSJoel E. Denny 
632bce8fcedSJoel E. Denny     // Elide this input line and its annotations if it's not within the
633bce8fcedSJoel E. Denny     // context specified by -dump-input-context of an input line included by
6349fd4b5faSJoel E. Denny     // -dump-input-filter.  However, in case the resulting ellipsis would occupy
63577b6ddf1SJoel E. Denny     // more lines than the input lines and annotations it elides, buffer the
63677b6ddf1SJoel E. Denny     // elided lines and annotations so we can print them instead.
6371211c418SSimon Pilgrim     raw_ostream *LineOS;
638bce8fcedSJoel E. Denny     if ((!PrevLineInFilter || PrevLineInFilter + DumpInputContext < Line) &&
639bce8fcedSJoel E. Denny         (NextLineInFilter == UINT_MAX ||
64077b6ddf1SJoel E. Denny          Line + DumpInputContext < NextLineInFilter))
64177b6ddf1SJoel E. Denny       LineOS = &ElidedLinesOS;
64277b6ddf1SJoel E. Denny     else {
64377b6ddf1SJoel E. Denny       LineOS = &OS;
64477b6ddf1SJoel E. Denny       DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth);
645bce8fcedSJoel E. Denny     }
646bce8fcedSJoel E. Denny 
6473c5d267eSJoel E. Denny     // Print right-aligned line number.
64877b6ddf1SJoel E. Denny     WithColor(*LineOS, raw_ostream::BLACK, /*Bold=*/true, /*BF=*/false,
64977b6ddf1SJoel E. Denny               TheColorMode)
6503c5d267eSJoel E. Denny         << format_decimal(Line, LabelWidth) << ": ";
6513c5d267eSJoel E. Denny 
652e2afb614SJoel E. Denny     // For the case where -v and colors are enabled, find the annotations for
653e2afb614SJoel E. Denny     // good matches for expected patterns in order to highlight everything
654e2afb614SJoel E. Denny     // else in the line.  There are no such annotations if -v is disabled.
655e2afb614SJoel E. Denny     std::vector<InputAnnotation> FoundAndExpectedMatches;
65677b6ddf1SJoel E. Denny     if (Req.Verbose && TheColorMode == ColorMode::Enable) {
6577df86967SJoel E. Denny       for (auto I = AnnotationItr; I != AnnotationEnd && I->InputLine == Line;
6587df86967SJoel E. Denny            ++I) {
659e2afb614SJoel E. Denny         if (I->FoundAndExpectedMatch)
660e2afb614SJoel E. Denny           FoundAndExpectedMatches.push_back(*I);
6617df86967SJoel E. Denny       }
6627df86967SJoel E. Denny     }
6637df86967SJoel E. Denny 
6647df86967SJoel E. Denny     // Print numbered line with highlighting where there are no matches for
6657df86967SJoel E. Denny     // expected patterns.
6663c5d267eSJoel E. Denny     bool Newline = false;
6677df86967SJoel E. Denny     {
66877b6ddf1SJoel E. Denny       WithColor COS(*LineOS, raw_ostream::SAVEDCOLOR, /*Bold=*/false,
66977b6ddf1SJoel E. Denny                     /*BG=*/false, TheColorMode);
6707df86967SJoel E. Denny       bool InMatch = false;
6717df86967SJoel E. Denny       if (Req.Verbose)
6727df86967SJoel E. Denny         COS.changeColor(raw_ostream::CYAN, true, true);
6737df86967SJoel E. Denny       for (unsigned Col = 1; InputFilePtr != InputFileEnd && !Newline; ++Col) {
6747df86967SJoel E. Denny         bool WasInMatch = InMatch;
6757df86967SJoel E. Denny         InMatch = false;
676e2afb614SJoel E. Denny         for (auto M : FoundAndExpectedMatches) {
6777df86967SJoel E. Denny           if (M.InputStartCol <= Col && Col < M.InputEndCol) {
6787df86967SJoel E. Denny             InMatch = true;
6797df86967SJoel E. Denny             break;
6807df86967SJoel E. Denny           }
6817df86967SJoel E. Denny         }
6827df86967SJoel E. Denny         if (!WasInMatch && InMatch)
6837df86967SJoel E. Denny           COS.resetColor();
6847df86967SJoel E. Denny         else if (WasInMatch && !InMatch)
6857df86967SJoel E. Denny           COS.changeColor(raw_ostream::CYAN, true, true);
68609c35739SThomas Preud'homme         if (*InputFilePtr == '\n') {
6873c5d267eSJoel E. Denny           Newline = true;
68809c35739SThomas Preud'homme           COS << ' ';
68909c35739SThomas Preud'homme         } else
6907df86967SJoel E. Denny           COS << *InputFilePtr;
6913c5d267eSJoel E. Denny         ++InputFilePtr;
6923c5d267eSJoel E. Denny       }
6937df86967SJoel E. Denny     }
69477b6ddf1SJoel E. Denny     *LineOS << '\n';
69509c35739SThomas Preud'homme     unsigned InputLineWidth = InputFilePtr - InputFileLine;
6963c5d267eSJoel E. Denny 
6973c5d267eSJoel E. Denny     // Print any annotations.
6983c5d267eSJoel E. Denny     while (AnnotationItr != AnnotationEnd &&
6993c5d267eSJoel E. Denny            AnnotationItr->InputLine == Line) {
70077b6ddf1SJoel E. Denny       WithColor COS(*LineOS, AnnotationItr->Marker.Color, /*Bold=*/true,
70177b6ddf1SJoel E. Denny                     /*BG=*/false, TheColorMode);
7023c5d267eSJoel E. Denny       // The two spaces below are where the ": " appears on input lines.
7033c5d267eSJoel E. Denny       COS << left_justify(AnnotationItr->Label, LabelWidth) << "  ";
7043c5d267eSJoel E. Denny       unsigned Col;
7053c5d267eSJoel E. Denny       for (Col = 1; Col < AnnotationItr->InputStartCol; ++Col)
7063c5d267eSJoel E. Denny         COS << ' ';
7073c5d267eSJoel E. Denny       COS << AnnotationItr->Marker.Lead;
7083c5d267eSJoel E. Denny       // If InputEndCol=UINT_MAX, stop at InputLineWidth.
7093c5d267eSJoel E. Denny       for (++Col; Col < AnnotationItr->InputEndCol && Col <= InputLineWidth;
7103c5d267eSJoel E. Denny            ++Col)
7113c5d267eSJoel E. Denny         COS << '~';
7123c5d267eSJoel E. Denny       const std::string &Note = AnnotationItr->Marker.Note;
7133c5d267eSJoel E. Denny       if (!Note.empty()) {
7143c5d267eSJoel E. Denny         // Put the note at the end of the input line.  If we were to instead
7153c5d267eSJoel E. Denny         // put the note right after the marker, subsequent annotations for the
7163c5d267eSJoel E. Denny         // same input line might appear to mark this note instead of the input
7173c5d267eSJoel E. Denny         // line.
7183c5d267eSJoel E. Denny         for (; Col <= InputLineWidth; ++Col)
7193c5d267eSJoel E. Denny           COS << ' ';
7203c5d267eSJoel E. Denny         COS << ' ' << Note;
7213c5d267eSJoel E. Denny       }
7223c5d267eSJoel E. Denny       COS << '\n';
7233c5d267eSJoel E. Denny       ++AnnotationItr;
7243c5d267eSJoel E. Denny     }
7253c5d267eSJoel E. Denny   }
72677b6ddf1SJoel E. Denny   DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth);
7273c5d267eSJoel E. Denny 
7283c5d267eSJoel E. Denny   OS << ">>>>>>\n";
7293c5d267eSJoel E. Denny }
7303c5d267eSJoel E. Denny 
main(int argc,char ** argv)731ee3c74fbSChris Lattner int main(int argc, char **argv) {
7323e66509fSJoel E. Denny   // Enable use of ANSI color codes because FileCheck is using them to
7333e66509fSJoel E. Denny   // highlight text.
7343e66509fSJoel E. Denny   llvm::sys::Process::UseANSIEscapeCodes(true);
7353e66509fSJoel E. Denny 
736197194b6SRui Ueyama   InitLLVM X(argc, argv);
73724994d77SJoel E. Denny   cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr,
73824994d77SJoel E. Denny                               "FILECHECK_OPTS");
739bce8fcedSJoel E. Denny 
740bce8fcedSJoel E. Denny   // Select -dump-input* values.  The -help documentation specifies the default
741bce8fcedSJoel E. Denny   // value and which value to choose if an option is specified multiple times.
742bce8fcedSJoel E. Denny   // In the latter case, the general rule of thumb is to choose the value that
743bce8fcedSJoel E. Denny   // provides the most information.
744fdde18a7SJoel E. Denny   DumpInputValue DumpInput =
745fdde18a7SJoel E. Denny       DumpInputs.empty()
7463b83501cSJoel E. Denny           ? DumpInputFail
747fdde18a7SJoel E. Denny           : *std::max_element(DumpInputs.begin(), DumpInputs.end());
7489fd4b5faSJoel E. Denny   DumpInputFilterValue DumpInputFilter;
7499fd4b5faSJoel E. Denny   if (DumpInputFilters.empty())
7509fd4b5faSJoel E. Denny     DumpInputFilter = DumpInput == DumpInputAlways ? DumpInputFilterAll
7519fd4b5faSJoel E. Denny                                                    : DumpInputFilterError;
7529fd4b5faSJoel E. Denny   else
7539fd4b5faSJoel E. Denny     DumpInputFilter =
7549fd4b5faSJoel E. Denny         *std::max_element(DumpInputFilters.begin(), DumpInputFilters.end());
755bce8fcedSJoel E. Denny   unsigned DumpInputContext = DumpInputContexts.empty()
756bce8fcedSJoel E. Denny                                   ? 5
757bce8fcedSJoel E. Denny                                   : *std::max_element(DumpInputContexts.begin(),
758bce8fcedSJoel E. Denny                                                       DumpInputContexts.end());
759bce8fcedSJoel E. Denny 
7603c5d267eSJoel E. Denny   if (DumpInput == DumpInputHelp) {
7613c5d267eSJoel E. Denny     DumpInputAnnotationHelp(outs());
7623c5d267eSJoel E. Denny     return 0;
7633c5d267eSJoel E. Denny   }
7643c5d267eSJoel E. Denny   if (CheckFilename.empty()) {
7653c5d267eSJoel E. Denny     errs() << "<check-file> not specified\n";
7663c5d267eSJoel E. Denny     return 2;
7673c5d267eSJoel E. Denny   }
768ee3c74fbSChris Lattner 
769ffa9d2e4SAditya Nandakumar   FileCheckRequest Req;
7707728cc00SKazu Hirata   append_range(Req.CheckPrefixes, CheckPrefixes);
771ffa9d2e4SAditya Nandakumar 
7727728cc00SKazu Hirata   append_range(Req.CommentPrefixes, CommentPrefixes);
773a1fd1882SJoel E. Denny 
7747728cc00SKazu Hirata   append_range(Req.ImplicitCheckNot, ImplicitCheckNot);
775ffa9d2e4SAditya Nandakumar 
776a5e233bfSThomas Preud'homme   bool GlobalDefineError = false;
77776e0ab23SGeorgii Rymar   for (StringRef G : GlobalDefines) {
778a5e233bfSThomas Preud'homme     size_t EqIdx = G.find('=');
779a5e233bfSThomas Preud'homme     if (EqIdx == std::string::npos) {
780a5e233bfSThomas Preud'homme       errs() << "Missing equal sign in command-line definition '-D" << G
781a5e233bfSThomas Preud'homme              << "'\n";
782a5e233bfSThomas Preud'homme       GlobalDefineError = true;
783a5e233bfSThomas Preud'homme       continue;
784a5e233bfSThomas Preud'homme     }
785a5e233bfSThomas Preud'homme     if (EqIdx == 0) {
7861a944d27SThomas Preud'homme       errs() << "Missing variable name in command-line definition '-D" << G
7871a944d27SThomas Preud'homme              << "'\n";
788a5e233bfSThomas Preud'homme       GlobalDefineError = true;
789a5e233bfSThomas Preud'homme       continue;
790a5e233bfSThomas Preud'homme     }
791ffa9d2e4SAditya Nandakumar     Req.GlobalDefines.push_back(G);
792a5e233bfSThomas Preud'homme   }
793a5e233bfSThomas Preud'homme   if (GlobalDefineError)
794a5e233bfSThomas Preud'homme     return 2;
795ffa9d2e4SAditya Nandakumar 
796ffa9d2e4SAditya Nandakumar   Req.AllowEmptyInput = AllowEmptyInput;
797871d658cSMircea Trofin   Req.AllowUnusedPrefixes = AllowUnusedPrefixes;
798ffa9d2e4SAditya Nandakumar   Req.EnableVarScope = EnableVarScope;
799ffa9d2e4SAditya Nandakumar   Req.AllowDeprecatedDagOverlap = AllowDeprecatedDagOverlap;
800ffa9d2e4SAditya Nandakumar   Req.Verbose = Verbose;
801ffa9d2e4SAditya Nandakumar   Req.VerboseVerbose = VerboseVerbose;
802ffa9d2e4SAditya Nandakumar   Req.NoCanonicalizeWhiteSpace = NoCanonicalizeWhiteSpace;
803ffa9d2e4SAditya Nandakumar   Req.MatchFullLines = MatchFullLines;
8045b5b2fd2SKai Nacke   Req.IgnoreCase = IgnoreCase;
805ffa9d2e4SAditya Nandakumar 
806ffa9d2e4SAditya Nandakumar   if (VerboseVerbose)
807ffa9d2e4SAditya Nandakumar     Req.Verbose = true;
808ffa9d2e4SAditya Nandakumar 
809ffa9d2e4SAditya Nandakumar   FileCheck FC(Req);
8102aa0217aSJoel E. Denny   if (!FC.ValidateCheckPrefixes())
811c2735158SRui Ueyama     return 2;
812c2735158SRui Ueyama 
813ffa9d2e4SAditya Nandakumar   Regex PrefixRE = FC.buildCheckPrefixRegex();
814726774cbSChandler Carruth   std::string REError;
815726774cbSChandler Carruth   if (!PrefixRE.isValid(REError)) {
816726774cbSChandler Carruth     errs() << "Unable to combine check-prefix strings into a prefix regular "
817726774cbSChandler Carruth               "expression! This is likely a bug in FileCheck's verification of "
818726774cbSChandler Carruth               "the check-prefix strings. Regular expression parsing failed "
819726774cbSChandler Carruth               "with the following error: "
820726774cbSChandler Carruth            << REError << "\n";
821726774cbSChandler Carruth     return 2;
822726774cbSChandler Carruth   }
82313df4626SMatt Arsenault 
824ee3c74fbSChris Lattner   SourceMgr SM;
825ee3c74fbSChris Lattner 
826ee3c74fbSChris Lattner   // Read the expected strings from the check file.
82720247900SChandler Carruth   ErrorOr<std::unique_ptr<MemoryBuffer>> CheckFileOrErr =
828c83cd8feSAbhina Sreeskantharajan       MemoryBuffer::getFileOrSTDIN(CheckFilename, /*IsText=*/true);
82920247900SChandler Carruth   if (std::error_code EC = CheckFileOrErr.getError()) {
83020247900SChandler Carruth     errs() << "Could not open check file '" << CheckFilename
83120247900SChandler Carruth            << "': " << EC.message() << '\n';
83220247900SChandler Carruth     return 2;
83320247900SChandler Carruth   }
83420247900SChandler Carruth   MemoryBuffer &CheckFile = *CheckFileOrErr.get();
83520247900SChandler Carruth 
83620247900SChandler Carruth   SmallString<4096> CheckFileBuffer;
837ffa9d2e4SAditya Nandakumar   StringRef CheckFileText = FC.CanonicalizeFile(CheckFile, CheckFileBuffer);
83820247900SChandler Carruth 
839b5a24610SJoel E. Denny   unsigned CheckFileBufferID =
84020247900SChandler Carruth       SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer(
84120247900SChandler Carruth                                 CheckFileText, CheckFile.getBufferIdentifier()),
84220247900SChandler Carruth                             SMLoc());
84320247900SChandler Carruth 
844b5a24610SJoel E. Denny   std::pair<unsigned, unsigned> ImpPatBufferIDRange;
845b5a24610SJoel E. Denny   if (FC.readCheckFile(SM, CheckFileText, PrefixRE, &ImpPatBufferIDRange))
846ee3c74fbSChris Lattner     return 2;
847ee3c74fbSChris Lattner 
848ee3c74fbSChris Lattner   // Open the file to check and add it to SourceMgr.
84920247900SChandler Carruth   ErrorOr<std::unique_ptr<MemoryBuffer>> InputFileOrErr =
850c83cd8feSAbhina Sreeskantharajan       MemoryBuffer::getFileOrSTDIN(InputFilename, /*IsText=*/true);
8516e01cd67SDavid Bozier   if (InputFilename == "-")
8526e01cd67SDavid Bozier     InputFilename = "<stdin>"; // Overwrite for improved diagnostic messages
85320247900SChandler Carruth   if (std::error_code EC = InputFileOrErr.getError()) {
854adf21f2aSRafael Espindola     errs() << "Could not open input file '" << InputFilename
855adf21f2aSRafael Espindola            << "': " << EC.message() << '\n';
8568e1c6477SEli Bendersky     return 2;
857ee3c74fbSChris Lattner   }
85820247900SChandler Carruth   MemoryBuffer &InputFile = *InputFileOrErr.get();
8592c3e5cdfSChris Lattner 
86020247900SChandler Carruth   if (InputFile.getBufferSize() == 0 && !AllowEmptyInput) {
861b692bed7SChris Lattner     errs() << "FileCheck error: '" << InputFilename << "' is empty.\n";
8622bd4f8b6SXinliang David Li     DumpCommandLine(argc, argv);
8638e1c6477SEli Bendersky     return 2;
864b692bed7SChris Lattner   }
865b692bed7SChris Lattner 
86620247900SChandler Carruth   SmallString<4096> InputFileBuffer;
867ffa9d2e4SAditya Nandakumar   StringRef InputFileText = FC.CanonicalizeFile(InputFile, InputFileBuffer);
8682c3e5cdfSChris Lattner 
869e8f2fb20SChandler Carruth   SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer(
870e8f2fb20SChandler Carruth                             InputFileText, InputFile.getBufferIdentifier()),
871e8f2fb20SChandler Carruth                         SMLoc());
872ee3c74fbSChris Lattner 
8733c5d267eSJoel E. Denny   std::vector<FileCheckDiag> Diags;
87402ada9bdSThomas Preud'homme   int ExitCode = FC.checkInput(SM, InputFileText,
8753c5d267eSJoel E. Denny                                DumpInput == DumpInputNever ? nullptr : &Diags)
8763c5d267eSJoel E. Denny                      ? EXIT_SUCCESS
8773c5d267eSJoel E. Denny                      : 1;
8783c5d267eSJoel E. Denny   if (DumpInput == DumpInputAlways ||
8793c5d267eSJoel E. Denny       (ExitCode == 1 && DumpInput == DumpInputFail)) {
8803c5d267eSJoel E. Denny     errs() << "\n"
881839f8e4fSJoel E. Denny            << "Input file: " << InputFilename << "\n"
8823c5d267eSJoel E. Denny            << "Check file: " << CheckFilename << "\n"
8833c5d267eSJoel E. Denny            << "\n"
884839f8e4fSJoel E. Denny            << "-dump-input=help explains the following input dump.\n"
8853c5d267eSJoel E. Denny            << "\n";
8863c5d267eSJoel E. Denny     std::vector<InputAnnotation> Annotations;
8873c5d267eSJoel E. Denny     unsigned LabelWidth;
888b5a24610SJoel E. Denny     BuildInputAnnotations(SM, CheckFileBufferID, ImpPatBufferIDRange, Diags,
889b5a24610SJoel E. Denny                           Annotations, LabelWidth);
8909fd4b5faSJoel E. Denny     DumpAnnotatedInput(errs(), Req, DumpInputFilter, DumpInputContext,
891bce8fcedSJoel E. Denny                        InputFileText, Annotations, LabelWidth);
8923c5d267eSJoel E. Denny   }
893346dfbe2SGeorge Karpenkov 
894346dfbe2SGeorge Karpenkov   return ExitCode;
895ee3c74fbSChris Lattner }
896