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" 24ee3c74fbSChris Lattner using namespace llvm; 25ee3c74fbSChris Lattner 26ee3c74fbSChris Lattner static cl::opt<std::string> 273c5d267eSJoel E. Denny CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Optional); 28ee3c74fbSChris Lattner 29ee3c74fbSChris Lattner static cl::opt<std::string> 30ee3c74fbSChris Lattner InputFilename("input-file", cl::desc("File to check (defaults to stdin)"), 31ee3c74fbSChris Lattner cl::init("-"), cl::value_desc("filename")); 32ee3c74fbSChris Lattner 33e8f2fb20SChandler Carruth static cl::list<std::string> CheckPrefixes( 34e8f2fb20SChandler Carruth "check-prefix", 35ee3c74fbSChris Lattner cl::desc("Prefix to use from check file (defaults to 'CHECK')")); 36fd557cb0SDaniel Sanders static cl::alias CheckPrefixesAlias( 37fd557cb0SDaniel Sanders "check-prefixes", cl::aliasopt(CheckPrefixes), cl::CommaSeparated, 38fd557cb0SDaniel Sanders cl::NotHidden, 39fd557cb0SDaniel Sanders cl::desc( 40fd557cb0SDaniel Sanders "Alias for -check-prefix permitting multiple comma separated values")); 41ee3c74fbSChris Lattner 42e8f2fb20SChandler Carruth static cl::opt<bool> NoCanonicalizeWhiteSpace( 43e8f2fb20SChandler Carruth "strict-whitespace", 442c3e5cdfSChris Lattner cl::desc("Do not treat all horizontal whitespace as equivalent")); 452c3e5cdfSChris Lattner 4656ccdbbdSAlexander Kornienko static cl::list<std::string> ImplicitCheckNot( 4756ccdbbdSAlexander Kornienko "implicit-check-not", 4856ccdbbdSAlexander Kornienko cl::desc("Add an implicit negative check with this pattern to every\n" 4956ccdbbdSAlexander Kornienko "positive check. This can be used to ensure that no instances of\n" 5056ccdbbdSAlexander Kornienko "this pattern occur which are not matched by a positive pattern"), 5156ccdbbdSAlexander Kornienko cl::value_desc("pattern")); 5256ccdbbdSAlexander Kornienko 53*a5e233bfSThomas Preud'homme static cl::list<std::string> 54*a5e233bfSThomas Preud'homme GlobalDefines("D", cl::AlwaysPrefix, 5546e1fd61SAlexander Richardson cl::desc("Define a variable to be used in capture patterns."), 5646e1fd61SAlexander Richardson cl::value_desc("VAR=VALUE")); 5746e1fd61SAlexander Richardson 581b9f936fSJustin Bogner static cl::opt<bool> AllowEmptyInput( 591b9f936fSJustin Bogner "allow-empty", cl::init(false), 601b9f936fSJustin Bogner cl::desc("Allow the input file to be empty. This is useful when making\n" 611b9f936fSJustin Bogner "checks that some error message does not occur, for example.")); 621b9f936fSJustin Bogner 6385913ccaSJames Y Knight static cl::opt<bool> MatchFullLines( 6485913ccaSJames Y Knight "match-full-lines", cl::init(false), 6585913ccaSJames Y Knight cl::desc("Require all positive matches to cover an entire input line.\n" 6685913ccaSJames Y Knight "Allows leading and trailing whitespace if --strict-whitespace\n" 6785913ccaSJames Y Knight "is not also passed.")); 6885913ccaSJames Y Knight 69f55e72a5SArtem Belevich static cl::opt<bool> EnableVarScope( 70f55e72a5SArtem Belevich "enable-var-scope", cl::init(false), 71f55e72a5SArtem Belevich cl::desc("Enables scope for regex variables. Variables with names that\n" 72f55e72a5SArtem Belevich "do not start with '$' will be reset at the beginning of\n" 73f55e72a5SArtem Belevich "each CHECK-LABEL block.")); 74f55e72a5SArtem Belevich 75bcf5b441SJoel E. Denny static cl::opt<bool> AllowDeprecatedDagOverlap( 76bcf5b441SJoel E. Denny "allow-deprecated-dag-overlap", cl::init(false), 77bcf5b441SJoel E. Denny cl::desc("Enable overlapping among matches in a group of consecutive\n" 78bcf5b441SJoel E. Denny "CHECK-DAG directives. This option is deprecated and is only\n" 79bcf5b441SJoel E. Denny "provided for convenience as old tests are migrated to the new\n" 80bcf5b441SJoel E. Denny "non-overlapping CHECK-DAG implementation.\n")); 81bcf5b441SJoel E. Denny 82352695c3SJoel E. Denny static cl::opt<bool> Verbose( 83352695c3SJoel E. Denny "v", cl::init(false), 84352695c3SJoel E. Denny cl::desc("Print directive pattern matches, or add them to the input dump\n" 85352695c3SJoel E. Denny "if enabled.\n")); 86dc5ba317SJoel E. Denny 87dc5ba317SJoel E. Denny static cl::opt<bool> VerboseVerbose( 88dc5ba317SJoel E. Denny "vv", cl::init(false), 89dc5ba317SJoel E. Denny cl::desc("Print information helpful in diagnosing internal FileCheck\n" 90352695c3SJoel E. Denny "issues, or add it to the input dump if enabled. Implies\n" 91352695c3SJoel E. Denny "-v.\n")); 92346dfbe2SGeorge Karpenkov static const char * DumpInputEnv = "FILECHECK_DUMP_INPUT_ON_FAILURE"; 93346dfbe2SGeorge Karpenkov 94346dfbe2SGeorge Karpenkov static cl::opt<bool> DumpInputOnFailure( 95346dfbe2SGeorge Karpenkov "dump-input-on-failure", cl::init(std::getenv(DumpInputEnv)), 96346dfbe2SGeorge Karpenkov cl::desc("Dump original input to stderr before failing.\n" 97346dfbe2SGeorge Karpenkov "The value can be also controlled using\n" 983c5d267eSJoel E. Denny "FILECHECK_DUMP_INPUT_ON_FAILURE environment variable.\n" 993c5d267eSJoel E. Denny "This option is deprecated in favor of -dump-input=fail.\n")); 1003c5d267eSJoel E. Denny 1013c5d267eSJoel E. Denny enum DumpInputValue { 1023c5d267eSJoel E. Denny DumpInputDefault, 1033c5d267eSJoel E. Denny DumpInputHelp, 1043c5d267eSJoel E. Denny DumpInputNever, 1053c5d267eSJoel E. Denny DumpInputFail, 1063c5d267eSJoel E. Denny DumpInputAlways 1073c5d267eSJoel E. Denny }; 1083c5d267eSJoel E. Denny 1093c5d267eSJoel E. Denny static cl::opt<DumpInputValue> DumpInput( 1103c5d267eSJoel E. Denny "dump-input", cl::init(DumpInputDefault), 1113c5d267eSJoel E. Denny cl::desc("Dump input to stderr, adding annotations representing\n" 1123c5d267eSJoel E. Denny " currently enabled diagnostics\n"), 1133c5d267eSJoel E. Denny cl::value_desc("mode"), 1143c5d267eSJoel E. Denny cl::values(clEnumValN(DumpInputHelp, "help", 1153c5d267eSJoel E. Denny "Explain dump format and quit"), 1163c5d267eSJoel E. Denny clEnumValN(DumpInputNever, "never", "Never dump input"), 1173c5d267eSJoel E. Denny clEnumValN(DumpInputFail, "fail", "Dump input on failure"), 1183c5d267eSJoel E. Denny clEnumValN(DumpInputAlways, "always", "Always dump input"))); 119dc5ba317SJoel E. Denny 12013df4626SMatt Arsenault typedef cl::list<std::string>::const_iterator prefix_iterator; 12113df4626SMatt Arsenault 12213df4626SMatt Arsenault 12313df4626SMatt Arsenault 124726774cbSChandler Carruth 125726774cbSChandler Carruth 126726774cbSChandler Carruth 127c2735158SRui Ueyama 1282bd4f8b6SXinliang David Li static void DumpCommandLine(int argc, char **argv) { 1292bd4f8b6SXinliang David Li errs() << "FileCheck command line: "; 1302bd4f8b6SXinliang David Li for (int I = 0; I < argc; I++) 1312bd4f8b6SXinliang David Li errs() << " " << argv[I]; 1322bd4f8b6SXinliang David Li errs() << "\n"; 1332bd4f8b6SXinliang David Li } 1342bd4f8b6SXinliang David Li 1353c5d267eSJoel E. Denny struct MarkerStyle { 1363c5d267eSJoel E. Denny /// The starting char (before tildes) for marking the line. 1373c5d267eSJoel E. Denny char Lead; 1383c5d267eSJoel E. Denny /// What color to use for this annotation. 1393c5d267eSJoel E. Denny raw_ostream::Colors Color; 1403c5d267eSJoel E. Denny /// A note to follow the marker, or empty string if none. 1413c5d267eSJoel E. Denny std::string Note; 1423c5d267eSJoel E. Denny MarkerStyle() {} 1437df86967SJoel E. Denny MarkerStyle(char Lead, raw_ostream::Colors Color, 1447df86967SJoel E. Denny const std::string &Note = "") 1453c5d267eSJoel E. Denny : Lead(Lead), Color(Color), Note(Note) {} 1463c5d267eSJoel E. Denny }; 1473c5d267eSJoel E. Denny 1483c5d267eSJoel E. Denny static MarkerStyle GetMarker(FileCheckDiag::MatchType MatchTy) { 1493c5d267eSJoel E. Denny switch (MatchTy) { 150e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundAndExpected: 1517df86967SJoel E. Denny return MarkerStyle('^', raw_ostream::GREEN); 152e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundButExcluded: 1530e7e3fa0SJoel E. Denny return MarkerStyle('!', raw_ostream::RED, "error: no match expected"); 154e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundButWrongLine: 155cadfcef4SJoel E. Denny return MarkerStyle('!', raw_ostream::RED, "error: match on wrong line"); 156e2afb614SJoel E. Denny case FileCheckDiag::MatchFoundButDiscarded: 157f7c1c4d8SJoel E. Denny return MarkerStyle('!', raw_ostream::CYAN, 158f7c1c4d8SJoel E. Denny "discard: overlaps earlier match"); 15996f0e84cSJoel E. Denny case FileCheckDiag::MatchNoneAndExcluded: 16096f0e84cSJoel E. Denny return MarkerStyle('X', raw_ostream::GREEN); 1613c5d267eSJoel E. Denny case FileCheckDiag::MatchNoneButExpected: 1623c5d267eSJoel E. Denny return MarkerStyle('X', raw_ostream::RED, "error: no match found"); 1632c007c80SJoel E. Denny case FileCheckDiag::MatchFuzzy: 1642c007c80SJoel E. Denny return MarkerStyle('?', raw_ostream::MAGENTA, "possible intended match"); 1653c5d267eSJoel E. Denny } 1663c5d267eSJoel E. Denny llvm_unreachable_internal("unexpected match type"); 1673c5d267eSJoel E. Denny } 1683c5d267eSJoel E. Denny 1693c5d267eSJoel E. Denny static void DumpInputAnnotationHelp(raw_ostream &OS) { 1703c5d267eSJoel E. Denny OS << "The following description was requested by -dump-input=help to\n" 1713c5d267eSJoel E. Denny << "explain the input annotations printed by -dump-input=always and\n" 1723c5d267eSJoel E. Denny << "-dump-input=fail:\n\n"; 1733c5d267eSJoel E. Denny 1743c5d267eSJoel E. Denny // Labels for input lines. 1753c5d267eSJoel E. Denny OS << " - "; 1763c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "L:"; 1773c5d267eSJoel E. Denny OS << " labels line number L of the input file\n"; 1783c5d267eSJoel E. Denny 1793c5d267eSJoel E. Denny // Labels for annotation lines. 1803c5d267eSJoel E. Denny OS << " - "; 1813c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L"; 1822c007c80SJoel E. Denny OS << " labels the only match result for a pattern of type T from " 1833c5d267eSJoel E. Denny << "line L of\n" 1843c5d267eSJoel E. Denny << " the check file\n"; 1852c007c80SJoel E. Denny OS << " - "; 1862c007c80SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L'N"; 1872c007c80SJoel E. Denny OS << " labels the Nth match result for a pattern of type T from line " 1882c007c80SJoel E. Denny << "L of\n" 1892c007c80SJoel E. Denny << " the check file\n"; 1903c5d267eSJoel E. Denny 1913c5d267eSJoel E. Denny // Markers on annotation lines. 1923c5d267eSJoel E. Denny OS << " - "; 1937df86967SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "^~~"; 1947df86967SJoel E. Denny OS << " marks good match (reported if -v)\n" 1957df86967SJoel E. Denny << " - "; 196cadfcef4SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "!~~"; 197cadfcef4SJoel E. Denny OS << " marks bad match, such as:\n" 198cadfcef4SJoel E. Denny << " - CHECK-NEXT on same line as previous match (error)\n" 1990e7e3fa0SJoel E. Denny << " - CHECK-NOT found (error)\n" 200f7c1c4d8SJoel E. Denny << " - CHECK-DAG overlapping match (discarded, reported if " 201f7c1c4d8SJoel E. Denny << "-vv)\n" 202cadfcef4SJoel E. Denny << " - "; 2033c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "X~~"; 204cadfcef4SJoel E. Denny OS << " marks search range when no match is found, such as:\n" 205cadfcef4SJoel E. Denny << " - CHECK-NEXT not found (error)\n" 20696f0e84cSJoel E. Denny << " - CHECK-NOT not found (success, reported if -vv)\n" 207f7c1c4d8SJoel E. Denny << " - CHECK-DAG not found after discarded matches (error)\n" 2082c007c80SJoel E. Denny << " - "; 2092c007c80SJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "?"; 2102c007c80SJoel E. Denny OS << " marks fuzzy match when no match is found\n"; 2113c5d267eSJoel E. Denny 2123c5d267eSJoel E. Denny // Colors. 2133c5d267eSJoel E. Denny OS << " - colors "; 2147df86967SJoel E. Denny WithColor(OS, raw_ostream::GREEN, true) << "success"; 2157df86967SJoel E. Denny OS << ", "; 2163c5d267eSJoel E. Denny WithColor(OS, raw_ostream::RED, true) << "error"; 2172c007c80SJoel E. Denny OS << ", "; 2182c007c80SJoel E. Denny WithColor(OS, raw_ostream::MAGENTA, true) << "fuzzy match"; 2197df86967SJoel E. Denny OS << ", "; 220f7c1c4d8SJoel E. Denny WithColor(OS, raw_ostream::CYAN, true, false) << "discarded match"; 221f7c1c4d8SJoel E. Denny OS << ", "; 2227df86967SJoel E. Denny WithColor(OS, raw_ostream::CYAN, true, true) << "unmatched input"; 2233c5d267eSJoel E. Denny OS << "\n\n" 2243c5d267eSJoel E. Denny << "If you are not seeing color above or in input dumps, try: -color\n"; 2253c5d267eSJoel E. Denny } 2263c5d267eSJoel E. Denny 2273c5d267eSJoel E. Denny /// An annotation for a single input line. 2283c5d267eSJoel E. Denny struct InputAnnotation { 2293c5d267eSJoel E. Denny /// The check file line (one-origin indexing) where the directive that 2303c5d267eSJoel E. Denny /// produced this annotation is located. 2313c5d267eSJoel E. Denny unsigned CheckLine; 2322c007c80SJoel E. Denny /// The index of the match result for this check. 2332c007c80SJoel E. Denny unsigned CheckDiagIndex; 2343c5d267eSJoel E. Denny /// The label for this annotation. 2353c5d267eSJoel E. Denny std::string Label; 2363c5d267eSJoel E. Denny /// What input line (one-origin indexing) this annotation marks. This might 2373c5d267eSJoel E. Denny /// be different from the starting line of the original diagnostic if this is 2383c5d267eSJoel E. Denny /// a non-initial fragment of a diagnostic that has been broken across 2393c5d267eSJoel E. Denny /// multiple lines. 2403c5d267eSJoel E. Denny unsigned InputLine; 2413c5d267eSJoel E. Denny /// The column range (one-origin indexing, open end) in which to to mark the 2423c5d267eSJoel E. Denny /// input line. If InputEndCol is UINT_MAX, treat it as the last column 2433c5d267eSJoel E. Denny /// before the newline. 2443c5d267eSJoel E. Denny unsigned InputStartCol, InputEndCol; 2453c5d267eSJoel E. Denny /// The marker to use. 2463c5d267eSJoel E. Denny MarkerStyle Marker; 247e2afb614SJoel E. Denny /// Whether this annotation represents a good match for an expected pattern. 248e2afb614SJoel E. Denny bool FoundAndExpectedMatch; 2493c5d267eSJoel E. Denny }; 2503c5d267eSJoel E. Denny 2513c5d267eSJoel E. Denny /// Get an abbreviation for the check type. 2523c5d267eSJoel E. Denny std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) { 2533c5d267eSJoel E. Denny switch (Ty) { 2543c5d267eSJoel E. Denny case Check::CheckPlain: 2553c5d267eSJoel E. Denny if (Ty.getCount() > 1) 2563c5d267eSJoel E. Denny return "count"; 2573c5d267eSJoel E. Denny return "check"; 2583c5d267eSJoel E. Denny case Check::CheckNext: 2593c5d267eSJoel E. Denny return "next"; 2603c5d267eSJoel E. Denny case Check::CheckSame: 2613c5d267eSJoel E. Denny return "same"; 2623c5d267eSJoel E. Denny case Check::CheckNot: 2633c5d267eSJoel E. Denny return "not"; 2643c5d267eSJoel E. Denny case Check::CheckDAG: 2653c5d267eSJoel E. Denny return "dag"; 2663c5d267eSJoel E. Denny case Check::CheckLabel: 2673c5d267eSJoel E. Denny return "label"; 2683c5d267eSJoel E. Denny case Check::CheckEmpty: 2693c5d267eSJoel E. Denny return "empty"; 2703c5d267eSJoel E. Denny case Check::CheckEOF: 2713c5d267eSJoel E. Denny return "eof"; 2723c5d267eSJoel E. Denny case Check::CheckBadNot: 2733c5d267eSJoel E. Denny return "bad-not"; 2743c5d267eSJoel E. Denny case Check::CheckBadCount: 2753c5d267eSJoel E. Denny return "bad-count"; 2763c5d267eSJoel E. Denny case Check::CheckNone: 2773c5d267eSJoel E. Denny llvm_unreachable("invalid FileCheckType"); 2783c5d267eSJoel E. Denny } 2793c5d267eSJoel E. Denny llvm_unreachable("unknown FileCheckType"); 2803c5d267eSJoel E. Denny } 2813c5d267eSJoel E. Denny 2823c5d267eSJoel E. Denny static void BuildInputAnnotations(const std::vector<FileCheckDiag> &Diags, 2833c5d267eSJoel E. Denny std::vector<InputAnnotation> &Annotations, 2843c5d267eSJoel E. Denny unsigned &LabelWidth) { 2852c007c80SJoel E. Denny // How many diagnostics has the current check seen so far? 2862c007c80SJoel E. Denny unsigned CheckDiagCount = 0; 2873c5d267eSJoel E. Denny // What's the widest label? 2883c5d267eSJoel E. Denny LabelWidth = 0; 2893c5d267eSJoel E. Denny for (auto DiagItr = Diags.begin(), DiagEnd = Diags.end(); DiagItr != DiagEnd; 2903c5d267eSJoel E. Denny ++DiagItr) { 2913c5d267eSJoel E. Denny InputAnnotation A; 2923c5d267eSJoel E. Denny 2933c5d267eSJoel E. Denny // Build label, which uniquely identifies this check result. 2943c5d267eSJoel E. Denny A.CheckLine = DiagItr->CheckLine; 2953c5d267eSJoel E. Denny llvm::raw_string_ostream Label(A.Label); 2963c5d267eSJoel E. Denny Label << GetCheckTypeAbbreviation(DiagItr->CheckTy) << ":" 2973c5d267eSJoel E. Denny << DiagItr->CheckLine; 2982c007c80SJoel E. Denny A.CheckDiagIndex = UINT_MAX; 2992c007c80SJoel E. Denny auto DiagNext = std::next(DiagItr); 3002c007c80SJoel E. Denny if (DiagNext != DiagEnd && DiagItr->CheckTy == DiagNext->CheckTy && 3012c007c80SJoel E. Denny DiagItr->CheckLine == DiagNext->CheckLine) 3022c007c80SJoel E. Denny A.CheckDiagIndex = CheckDiagCount++; 3032c007c80SJoel E. Denny else if (CheckDiagCount) { 3042c007c80SJoel E. Denny A.CheckDiagIndex = CheckDiagCount; 3052c007c80SJoel E. Denny CheckDiagCount = 0; 3062c007c80SJoel E. Denny } 3072c007c80SJoel E. Denny if (A.CheckDiagIndex != UINT_MAX) 3082c007c80SJoel E. Denny Label << "'" << A.CheckDiagIndex; 3092c007c80SJoel E. Denny else 3102c007c80SJoel E. Denny A.CheckDiagIndex = 0; 3113c5d267eSJoel E. Denny Label.flush(); 3123c5d267eSJoel E. Denny LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size()); 3133c5d267eSJoel E. Denny 3143c5d267eSJoel E. Denny MarkerStyle Marker = GetMarker(DiagItr->MatchTy); 3153c5d267eSJoel E. Denny A.Marker = Marker; 316e2afb614SJoel E. Denny A.FoundAndExpectedMatch = 317e2afb614SJoel E. Denny DiagItr->MatchTy == FileCheckDiag::MatchFoundAndExpected; 3183c5d267eSJoel E. Denny 3193c5d267eSJoel E. Denny // Compute the mark location, and break annotation into multiple 3203c5d267eSJoel E. Denny // annotations if it spans multiple lines. 3213c5d267eSJoel E. Denny A.InputLine = DiagItr->InputStartLine; 3223c5d267eSJoel E. Denny A.InputStartCol = DiagItr->InputStartCol; 3233c5d267eSJoel E. Denny if (DiagItr->InputStartLine == DiagItr->InputEndLine) { 3243c5d267eSJoel E. Denny // Sometimes ranges are empty in order to indicate a specific point, but 3253c5d267eSJoel E. Denny // that would mean nothing would be marked, so adjust the range to 3263c5d267eSJoel E. Denny // include the following character. 3273c5d267eSJoel E. Denny A.InputEndCol = 3283c5d267eSJoel E. Denny std::max(DiagItr->InputStartCol + 1, DiagItr->InputEndCol); 3293c5d267eSJoel E. Denny Annotations.push_back(A); 3303c5d267eSJoel E. Denny } else { 3313c5d267eSJoel E. Denny assert(DiagItr->InputStartLine < DiagItr->InputEndLine && 3323c5d267eSJoel E. Denny "expected input range not to be inverted"); 3333c5d267eSJoel E. Denny A.InputEndCol = UINT_MAX; 3343c5d267eSJoel E. Denny A.Marker.Note = ""; 3353c5d267eSJoel E. Denny Annotations.push_back(A); 3363c5d267eSJoel E. Denny for (unsigned L = DiagItr->InputStartLine + 1, E = DiagItr->InputEndLine; 3373c5d267eSJoel E. Denny L <= E; ++L) { 3383c5d267eSJoel E. Denny // If a range ends before the first column on a line, then it has no 3393c5d267eSJoel E. Denny // characters on that line, so there's nothing to render. 3403c5d267eSJoel E. Denny if (DiagItr->InputEndCol == 1 && L == E) { 3413c5d267eSJoel E. Denny Annotations.back().Marker.Note = Marker.Note; 3423c5d267eSJoel E. Denny break; 3433c5d267eSJoel E. Denny } 3443c5d267eSJoel E. Denny InputAnnotation B; 3453c5d267eSJoel E. Denny B.CheckLine = A.CheckLine; 3462c007c80SJoel E. Denny B.CheckDiagIndex = A.CheckDiagIndex; 3473c5d267eSJoel E. Denny B.Label = A.Label; 3483c5d267eSJoel E. Denny B.InputLine = L; 3493c5d267eSJoel E. Denny B.Marker = Marker; 3503c5d267eSJoel E. Denny B.Marker.Lead = '~'; 3513c5d267eSJoel E. Denny B.InputStartCol = 1; 3523c5d267eSJoel E. Denny if (L != E) { 3533c5d267eSJoel E. Denny B.InputEndCol = UINT_MAX; 3543c5d267eSJoel E. Denny B.Marker.Note = ""; 3553c5d267eSJoel E. Denny } else 3563c5d267eSJoel E. Denny B.InputEndCol = DiagItr->InputEndCol; 357e2afb614SJoel E. Denny B.FoundAndExpectedMatch = A.FoundAndExpectedMatch; 3583c5d267eSJoel E. Denny Annotations.push_back(B); 3593c5d267eSJoel E. Denny } 3603c5d267eSJoel E. Denny } 3613c5d267eSJoel E. Denny } 3623c5d267eSJoel E. Denny } 3633c5d267eSJoel E. Denny 3647df86967SJoel E. Denny static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req, 3657df86967SJoel E. Denny StringRef InputFileText, 3667df86967SJoel E. Denny std::vector<InputAnnotation> &Annotations, 3677df86967SJoel E. Denny unsigned LabelWidth) { 3683c5d267eSJoel E. Denny OS << "Full input was:\n<<<<<<\n"; 3693c5d267eSJoel E. Denny 3703c5d267eSJoel E. Denny // Sort annotations. 3713c5d267eSJoel E. Denny // 3723c5d267eSJoel E. Denny // First, sort in the order of input lines to make it easier to find relevant 3733c5d267eSJoel E. Denny // annotations while iterating input lines in the implementation below. 3743c5d267eSJoel E. Denny // FileCheck diagnostics are not always reported and recorded in the order of 3753c5d267eSJoel E. Denny // input lines due to, for example, CHECK-DAG and CHECK-NOT. 3763c5d267eSJoel E. Denny // 3773c5d267eSJoel E. Denny // Second, for annotations for the same input line, sort in the order of the 3783c5d267eSJoel E. Denny // FileCheck directive's line in the check file (where there's at most one 3792c007c80SJoel E. Denny // directive per line) and then by the index of the match result for that 3802c007c80SJoel E. Denny // directive. The rationale of this choice is that, for any input line, this 3812c007c80SJoel E. Denny // sort establishes a total order of annotations that, with respect to match 3822c007c80SJoel E. Denny // results, is consistent across multiple lines, thus making match results 3832c007c80SJoel E. Denny // easier to track from one line to the next when they span multiple lines. 3843c5d267eSJoel E. Denny std::sort(Annotations.begin(), Annotations.end(), 3853c5d267eSJoel E. Denny [](const InputAnnotation &A, const InputAnnotation &B) { 3863c5d267eSJoel E. Denny if (A.InputLine != B.InputLine) 3873c5d267eSJoel E. Denny return A.InputLine < B.InputLine; 3882c007c80SJoel E. Denny if (A.CheckLine != B.CheckLine) 3893c5d267eSJoel E. Denny return A.CheckLine < B.CheckLine; 3907df86967SJoel E. Denny // FIXME: Sometimes CHECK-LABEL reports its match twice with 3917df86967SJoel E. Denny // other diagnostics in between, and then diag index incrementing 3927df86967SJoel E. Denny // fails to work properly, and then this assert fails. We should 3937df86967SJoel E. Denny // suppress one of those diagnostics or do a better job of 3947df86967SJoel E. Denny // computing this index. For now, we just produce a redundant 3957df86967SJoel E. Denny // CHECK-LABEL annotation. 3967df86967SJoel E. Denny // assert(A.CheckDiagIndex != B.CheckDiagIndex && 3977df86967SJoel E. Denny // "expected diagnostic indices to be unique within a " 3987df86967SJoel E. Denny // " check line"); 3992c007c80SJoel E. Denny return A.CheckDiagIndex < B.CheckDiagIndex; 4003c5d267eSJoel E. Denny }); 4013c5d267eSJoel E. Denny 4023c5d267eSJoel E. Denny // Compute the width of the label column. 4033c5d267eSJoel E. Denny const unsigned char *InputFilePtr = InputFileText.bytes_begin(), 4043c5d267eSJoel E. Denny *InputFileEnd = InputFileText.bytes_end(); 4053c5d267eSJoel E. Denny unsigned LineCount = InputFileText.count('\n'); 4063c5d267eSJoel E. Denny if (InputFileEnd[-1] != '\n') 4073c5d267eSJoel E. Denny ++LineCount; 4083c5d267eSJoel E. Denny unsigned LineNoWidth = log10(LineCount) + 1; 4093c5d267eSJoel E. Denny // +3 below adds spaces (1) to the left of the (right-aligned) line numbers 4103c5d267eSJoel E. Denny // on input lines and (2) to the right of the (left-aligned) labels on 4113c5d267eSJoel E. Denny // annotation lines so that input lines and annotation lines are more 4123c5d267eSJoel E. Denny // visually distinct. For example, the spaces on the annotation lines ensure 4133c5d267eSJoel E. Denny // that input line numbers and check directive line numbers never align 4143c5d267eSJoel E. Denny // horizontally. Those line numbers might not even be for the same file. 4153c5d267eSJoel E. Denny // One space would be enough to achieve that, but more makes it even easier 4163c5d267eSJoel E. Denny // to see. 4173c5d267eSJoel E. Denny LabelWidth = std::max(LabelWidth, LineNoWidth) + 3; 4183c5d267eSJoel E. Denny 4193c5d267eSJoel E. Denny // Print annotated input lines. 4203c5d267eSJoel E. Denny auto AnnotationItr = Annotations.begin(), AnnotationEnd = Annotations.end(); 4213c5d267eSJoel E. Denny for (unsigned Line = 1; 4223c5d267eSJoel E. Denny InputFilePtr != InputFileEnd || AnnotationItr != AnnotationEnd; 4233c5d267eSJoel E. Denny ++Line) { 4243c5d267eSJoel E. Denny const unsigned char *InputFileLine = InputFilePtr; 4253c5d267eSJoel E. Denny 4263c5d267eSJoel E. Denny // Print right-aligned line number. 4273c5d267eSJoel E. Denny WithColor(OS, raw_ostream::BLACK, true) 4283c5d267eSJoel E. Denny << format_decimal(Line, LabelWidth) << ": "; 4293c5d267eSJoel E. Denny 430e2afb614SJoel E. Denny // For the case where -v and colors are enabled, find the annotations for 431e2afb614SJoel E. Denny // good matches for expected patterns in order to highlight everything 432e2afb614SJoel E. Denny // else in the line. There are no such annotations if -v is disabled. 433e2afb614SJoel E. Denny std::vector<InputAnnotation> FoundAndExpectedMatches; 4347df86967SJoel E. Denny if (Req.Verbose && WithColor(OS).colorsEnabled()) { 4357df86967SJoel E. Denny for (auto I = AnnotationItr; I != AnnotationEnd && I->InputLine == Line; 4367df86967SJoel E. Denny ++I) { 437e2afb614SJoel E. Denny if (I->FoundAndExpectedMatch) 438e2afb614SJoel E. Denny FoundAndExpectedMatches.push_back(*I); 4397df86967SJoel E. Denny } 4407df86967SJoel E. Denny } 4417df86967SJoel E. Denny 4427df86967SJoel E. Denny // Print numbered line with highlighting where there are no matches for 4437df86967SJoel E. Denny // expected patterns. 4443c5d267eSJoel E. Denny bool Newline = false; 4457df86967SJoel E. Denny { 4467df86967SJoel E. Denny WithColor COS(OS); 4477df86967SJoel E. Denny bool InMatch = false; 4487df86967SJoel E. Denny if (Req.Verbose) 4497df86967SJoel E. Denny COS.changeColor(raw_ostream::CYAN, true, true); 4507df86967SJoel E. Denny for (unsigned Col = 1; InputFilePtr != InputFileEnd && !Newline; ++Col) { 4517df86967SJoel E. Denny bool WasInMatch = InMatch; 4527df86967SJoel E. Denny InMatch = false; 453e2afb614SJoel E. Denny for (auto M : FoundAndExpectedMatches) { 4547df86967SJoel E. Denny if (M.InputStartCol <= Col && Col < M.InputEndCol) { 4557df86967SJoel E. Denny InMatch = true; 4567df86967SJoel E. Denny break; 4577df86967SJoel E. Denny } 4587df86967SJoel E. Denny } 4597df86967SJoel E. Denny if (!WasInMatch && InMatch) 4607df86967SJoel E. Denny COS.resetColor(); 4617df86967SJoel E. Denny else if (WasInMatch && !InMatch) 4627df86967SJoel E. Denny COS.changeColor(raw_ostream::CYAN, true, true); 4633c5d267eSJoel E. Denny if (*InputFilePtr == '\n') 4643c5d267eSJoel E. Denny Newline = true; 4653c5d267eSJoel E. Denny else 4667df86967SJoel E. Denny COS << *InputFilePtr; 4673c5d267eSJoel E. Denny ++InputFilePtr; 4683c5d267eSJoel E. Denny } 4697df86967SJoel E. Denny } 4703c5d267eSJoel E. Denny OS << '\n'; 4713c5d267eSJoel E. Denny unsigned InputLineWidth = InputFilePtr - InputFileLine - Newline; 4723c5d267eSJoel E. Denny 4733c5d267eSJoel E. Denny // Print any annotations. 4743c5d267eSJoel E. Denny while (AnnotationItr != AnnotationEnd && 4753c5d267eSJoel E. Denny AnnotationItr->InputLine == Line) { 4763c5d267eSJoel E. Denny WithColor COS(OS, AnnotationItr->Marker.Color, true); 4773c5d267eSJoel E. Denny // The two spaces below are where the ": " appears on input lines. 4783c5d267eSJoel E. Denny COS << left_justify(AnnotationItr->Label, LabelWidth) << " "; 4793c5d267eSJoel E. Denny unsigned Col; 4803c5d267eSJoel E. Denny for (Col = 1; Col < AnnotationItr->InputStartCol; ++Col) 4813c5d267eSJoel E. Denny COS << ' '; 4823c5d267eSJoel E. Denny COS << AnnotationItr->Marker.Lead; 4833c5d267eSJoel E. Denny // If InputEndCol=UINT_MAX, stop at InputLineWidth. 4843c5d267eSJoel E. Denny for (++Col; Col < AnnotationItr->InputEndCol && Col <= InputLineWidth; 4853c5d267eSJoel E. Denny ++Col) 4863c5d267eSJoel E. Denny COS << '~'; 4873c5d267eSJoel E. Denny const std::string &Note = AnnotationItr->Marker.Note; 4883c5d267eSJoel E. Denny if (!Note.empty()) { 4893c5d267eSJoel E. Denny // Put the note at the end of the input line. If we were to instead 4903c5d267eSJoel E. Denny // put the note right after the marker, subsequent annotations for the 4913c5d267eSJoel E. Denny // same input line might appear to mark this note instead of the input 4923c5d267eSJoel E. Denny // line. 4933c5d267eSJoel E. Denny for (; Col <= InputLineWidth; ++Col) 4943c5d267eSJoel E. Denny COS << ' '; 4953c5d267eSJoel E. Denny COS << ' ' << Note; 4963c5d267eSJoel E. Denny } 4973c5d267eSJoel E. Denny COS << '\n'; 4983c5d267eSJoel E. Denny ++AnnotationItr; 4993c5d267eSJoel E. Denny } 5003c5d267eSJoel E. Denny } 5013c5d267eSJoel E. Denny 5023c5d267eSJoel E. Denny OS << ">>>>>>\n"; 5033c5d267eSJoel E. Denny } 5043c5d267eSJoel E. Denny 505ee3c74fbSChris Lattner int main(int argc, char **argv) { 5063e66509fSJoel E. Denny // Enable use of ANSI color codes because FileCheck is using them to 5073e66509fSJoel E. Denny // highlight text. 5083e66509fSJoel E. Denny llvm::sys::Process::UseANSIEscapeCodes(true); 5093e66509fSJoel E. Denny 510197194b6SRui Ueyama InitLLVM X(argc, argv); 51124994d77SJoel E. Denny cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr, 51224994d77SJoel E. Denny "FILECHECK_OPTS"); 5133c5d267eSJoel E. Denny if (DumpInput == DumpInputHelp) { 5143c5d267eSJoel E. Denny DumpInputAnnotationHelp(outs()); 5153c5d267eSJoel E. Denny return 0; 5163c5d267eSJoel E. Denny } 5173c5d267eSJoel E. Denny if (CheckFilename.empty()) { 5183c5d267eSJoel E. Denny errs() << "<check-file> not specified\n"; 5193c5d267eSJoel E. Denny return 2; 5203c5d267eSJoel E. Denny } 521ee3c74fbSChris Lattner 522ffa9d2e4SAditya Nandakumar FileCheckRequest Req; 523ffa9d2e4SAditya Nandakumar for (auto Prefix : CheckPrefixes) 524ffa9d2e4SAditya Nandakumar Req.CheckPrefixes.push_back(Prefix); 525ffa9d2e4SAditya Nandakumar 526ffa9d2e4SAditya Nandakumar for (auto CheckNot : ImplicitCheckNot) 527ffa9d2e4SAditya Nandakumar Req.ImplicitCheckNot.push_back(CheckNot); 528ffa9d2e4SAditya Nandakumar 529*a5e233bfSThomas Preud'homme bool GlobalDefineError = false; 530*a5e233bfSThomas Preud'homme for (auto G : GlobalDefines) { 531*a5e233bfSThomas Preud'homme size_t EqIdx = G.find('='); 532*a5e233bfSThomas Preud'homme if (EqIdx == std::string::npos) { 533*a5e233bfSThomas Preud'homme errs() << "Missing equal sign in command-line definition '-D" << G 534*a5e233bfSThomas Preud'homme << "'\n"; 535*a5e233bfSThomas Preud'homme GlobalDefineError = true; 536*a5e233bfSThomas Preud'homme continue; 537*a5e233bfSThomas Preud'homme } 538*a5e233bfSThomas Preud'homme if (EqIdx == 0) { 539*a5e233bfSThomas Preud'homme errs() << "Missing pattern variable name in command-line definition '-D" 540*a5e233bfSThomas Preud'homme << G << "'\n"; 541*a5e233bfSThomas Preud'homme GlobalDefineError = true; 542*a5e233bfSThomas Preud'homme continue; 543*a5e233bfSThomas Preud'homme } 544ffa9d2e4SAditya Nandakumar Req.GlobalDefines.push_back(G); 545*a5e233bfSThomas Preud'homme } 546*a5e233bfSThomas Preud'homme if (GlobalDefineError) 547*a5e233bfSThomas Preud'homme return 2; 548ffa9d2e4SAditya Nandakumar 549ffa9d2e4SAditya Nandakumar Req.AllowEmptyInput = AllowEmptyInput; 550ffa9d2e4SAditya Nandakumar Req.EnableVarScope = EnableVarScope; 551ffa9d2e4SAditya Nandakumar Req.AllowDeprecatedDagOverlap = AllowDeprecatedDagOverlap; 552ffa9d2e4SAditya Nandakumar Req.Verbose = Verbose; 553ffa9d2e4SAditya Nandakumar Req.VerboseVerbose = VerboseVerbose; 554ffa9d2e4SAditya Nandakumar Req.NoCanonicalizeWhiteSpace = NoCanonicalizeWhiteSpace; 555ffa9d2e4SAditya Nandakumar Req.MatchFullLines = MatchFullLines; 556ffa9d2e4SAditya Nandakumar 557ffa9d2e4SAditya Nandakumar if (VerboseVerbose) 558ffa9d2e4SAditya Nandakumar Req.Verbose = true; 559ffa9d2e4SAditya Nandakumar 560ffa9d2e4SAditya Nandakumar FileCheck FC(Req); 561ffa9d2e4SAditya Nandakumar if (!FC.ValidateCheckPrefixes()) { 56213df4626SMatt Arsenault errs() << "Supplied check-prefix is invalid! Prefixes must be unique and " 56313df4626SMatt Arsenault "start with a letter and contain only alphanumeric characters, " 56413df4626SMatt Arsenault "hyphens and underscores\n"; 565c2735158SRui Ueyama return 2; 566c2735158SRui Ueyama } 567c2735158SRui Ueyama 568ffa9d2e4SAditya Nandakumar Regex PrefixRE = FC.buildCheckPrefixRegex(); 569726774cbSChandler Carruth std::string REError; 570726774cbSChandler Carruth if (!PrefixRE.isValid(REError)) { 571726774cbSChandler Carruth errs() << "Unable to combine check-prefix strings into a prefix regular " 572726774cbSChandler Carruth "expression! This is likely a bug in FileCheck's verification of " 573726774cbSChandler Carruth "the check-prefix strings. Regular expression parsing failed " 574726774cbSChandler Carruth "with the following error: " 575726774cbSChandler Carruth << REError << "\n"; 576726774cbSChandler Carruth return 2; 577726774cbSChandler Carruth } 57813df4626SMatt Arsenault 579ee3c74fbSChris Lattner SourceMgr SM; 580ee3c74fbSChris Lattner 581ee3c74fbSChris Lattner // Read the expected strings from the check file. 58220247900SChandler Carruth ErrorOr<std::unique_ptr<MemoryBuffer>> CheckFileOrErr = 58320247900SChandler Carruth MemoryBuffer::getFileOrSTDIN(CheckFilename); 58420247900SChandler Carruth if (std::error_code EC = CheckFileOrErr.getError()) { 58520247900SChandler Carruth errs() << "Could not open check file '" << CheckFilename 58620247900SChandler Carruth << "': " << EC.message() << '\n'; 58720247900SChandler Carruth return 2; 58820247900SChandler Carruth } 58920247900SChandler Carruth MemoryBuffer &CheckFile = *CheckFileOrErr.get(); 59020247900SChandler Carruth 59120247900SChandler Carruth SmallString<4096> CheckFileBuffer; 592ffa9d2e4SAditya Nandakumar StringRef CheckFileText = FC.CanonicalizeFile(CheckFile, CheckFileBuffer); 59320247900SChandler Carruth 59420247900SChandler Carruth SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 59520247900SChandler Carruth CheckFileText, CheckFile.getBufferIdentifier()), 59620247900SChandler Carruth SMLoc()); 59720247900SChandler Carruth 598ffa9d2e4SAditya Nandakumar std::vector<FileCheckString> CheckStrings; 599ffa9d2e4SAditya Nandakumar if (FC.ReadCheckFile(SM, CheckFileText, PrefixRE, CheckStrings)) 600ee3c74fbSChris Lattner return 2; 601ee3c74fbSChris Lattner 602ee3c74fbSChris Lattner // Open the file to check and add it to SourceMgr. 60320247900SChandler Carruth ErrorOr<std::unique_ptr<MemoryBuffer>> InputFileOrErr = 604adf21f2aSRafael Espindola MemoryBuffer::getFileOrSTDIN(InputFilename); 60520247900SChandler Carruth if (std::error_code EC = InputFileOrErr.getError()) { 606adf21f2aSRafael Espindola errs() << "Could not open input file '" << InputFilename 607adf21f2aSRafael Espindola << "': " << EC.message() << '\n'; 6088e1c6477SEli Bendersky return 2; 609ee3c74fbSChris Lattner } 61020247900SChandler Carruth MemoryBuffer &InputFile = *InputFileOrErr.get(); 6112c3e5cdfSChris Lattner 61220247900SChandler Carruth if (InputFile.getBufferSize() == 0 && !AllowEmptyInput) { 613b692bed7SChris Lattner errs() << "FileCheck error: '" << InputFilename << "' is empty.\n"; 6142bd4f8b6SXinliang David Li DumpCommandLine(argc, argv); 6158e1c6477SEli Bendersky return 2; 616b692bed7SChris Lattner } 617b692bed7SChris Lattner 61820247900SChandler Carruth SmallString<4096> InputFileBuffer; 619ffa9d2e4SAditya Nandakumar StringRef InputFileText = FC.CanonicalizeFile(InputFile, InputFileBuffer); 6202c3e5cdfSChris Lattner 621e8f2fb20SChandler Carruth SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 622e8f2fb20SChandler Carruth InputFileText, InputFile.getBufferIdentifier()), 623e8f2fb20SChandler Carruth SMLoc()); 624ee3c74fbSChris Lattner 6253c5d267eSJoel E. Denny if (DumpInput == DumpInputDefault) 6263c5d267eSJoel E. Denny DumpInput = DumpInputOnFailure ? DumpInputFail : DumpInputNever; 6273c5d267eSJoel E. Denny 6283c5d267eSJoel E. Denny std::vector<FileCheckDiag> Diags; 6293c5d267eSJoel E. Denny int ExitCode = FC.CheckInput(SM, InputFileText, CheckStrings, 6303c5d267eSJoel E. Denny DumpInput == DumpInputNever ? nullptr : &Diags) 6313c5d267eSJoel E. Denny ? EXIT_SUCCESS 6323c5d267eSJoel E. Denny : 1; 6333c5d267eSJoel E. Denny if (DumpInput == DumpInputAlways || 6343c5d267eSJoel E. Denny (ExitCode == 1 && DumpInput == DumpInputFail)) { 6353c5d267eSJoel E. Denny errs() << "\n" 6363c5d267eSJoel E. Denny << "Input file: " 6373c5d267eSJoel E. Denny << (InputFilename == "-" ? "<stdin>" : InputFilename.getValue()) 6383c5d267eSJoel E. Denny << "\n" 6393c5d267eSJoel E. Denny << "Check file: " << CheckFilename << "\n" 6403c5d267eSJoel E. Denny << "\n" 6413c5d267eSJoel E. Denny << "-dump-input=help describes the format of the following dump.\n" 6423c5d267eSJoel E. Denny << "\n"; 6433c5d267eSJoel E. Denny std::vector<InputAnnotation> Annotations; 6443c5d267eSJoel E. Denny unsigned LabelWidth; 6453c5d267eSJoel E. Denny BuildInputAnnotations(Diags, Annotations, LabelWidth); 6467df86967SJoel E. Denny DumpAnnotatedInput(errs(), Req, InputFileText, Annotations, LabelWidth); 6473c5d267eSJoel E. Denny } 648346dfbe2SGeorge Karpenkov 649346dfbe2SGeorge Karpenkov return ExitCode; 650ee3c74fbSChris Lattner } 651