1ee3c74fbSChris Lattner //===- FileCheck.cpp - Check that File's Contents match what is expected --===// 2ee3c74fbSChris Lattner // 3ee3c74fbSChris Lattner // The LLVM Compiler Infrastructure 4ee3c74fbSChris Lattner // 5ee3c74fbSChris Lattner // This file is distributed under the University of Illinois Open Source 6ee3c74fbSChris Lattner // License. See LICENSE.TXT for details. 7ee3c74fbSChris Lattner // 8ee3c74fbSChris Lattner //===----------------------------------------------------------------------===// 9ee3c74fbSChris Lattner // 10ee3c74fbSChris Lattner // FileCheck does a line-by line check of a file that validates whether it 11ee3c74fbSChris Lattner // contains the expected content. This is useful for regression tests etc. 12ee3c74fbSChris Lattner // 13b5ecceffSJames Henderson // This program exits with an exit status of 2 on error, exit status of 0 if 14ee3c74fbSChris Lattner // the file matched the expected contents, and exit status of 1 if it did not 15ee3c74fbSChris Lattner // contain the expected contents. 16ee3c74fbSChris Lattner // 17ee3c74fbSChris Lattner //===----------------------------------------------------------------------===// 18ee3c74fbSChris Lattner 19ee3c74fbSChris Lattner #include "llvm/Support/CommandLine.h" 20197194b6SRui Ueyama #include "llvm/Support/InitLLVM.h" 213e66509fSJoel E. Denny #include "llvm/Support/Process.h" 22*3c5d267eSJoel E. Denny #include "llvm/Support/WithColor.h" 23ee3c74fbSChris Lattner #include "llvm/Support/raw_ostream.h" 24ffa9d2e4SAditya Nandakumar #include "llvm/Support/FileCheck.h" 25ee3c74fbSChris Lattner using namespace llvm; 26ee3c74fbSChris Lattner 27ee3c74fbSChris Lattner static cl::opt<std::string> 28*3c5d267eSJoel E. Denny CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Optional); 29ee3c74fbSChris Lattner 30ee3c74fbSChris Lattner static cl::opt<std::string> 31ee3c74fbSChris Lattner InputFilename("input-file", cl::desc("File to check (defaults to stdin)"), 32ee3c74fbSChris Lattner cl::init("-"), cl::value_desc("filename")); 33ee3c74fbSChris Lattner 34e8f2fb20SChandler Carruth static cl::list<std::string> CheckPrefixes( 35e8f2fb20SChandler Carruth "check-prefix", 36ee3c74fbSChris Lattner cl::desc("Prefix to use from check file (defaults to 'CHECK')")); 37fd557cb0SDaniel Sanders static cl::alias CheckPrefixesAlias( 38fd557cb0SDaniel Sanders "check-prefixes", cl::aliasopt(CheckPrefixes), cl::CommaSeparated, 39fd557cb0SDaniel Sanders cl::NotHidden, 40fd557cb0SDaniel Sanders cl::desc( 41fd557cb0SDaniel Sanders "Alias for -check-prefix permitting multiple comma separated values")); 42ee3c74fbSChris Lattner 43e8f2fb20SChandler Carruth static cl::opt<bool> NoCanonicalizeWhiteSpace( 44e8f2fb20SChandler Carruth "strict-whitespace", 452c3e5cdfSChris Lattner cl::desc("Do not treat all horizontal whitespace as equivalent")); 462c3e5cdfSChris Lattner 4756ccdbbdSAlexander Kornienko static cl::list<std::string> ImplicitCheckNot( 4856ccdbbdSAlexander Kornienko "implicit-check-not", 4956ccdbbdSAlexander Kornienko cl::desc("Add an implicit negative check with this pattern to every\n" 5056ccdbbdSAlexander Kornienko "positive check. This can be used to ensure that no instances of\n" 5156ccdbbdSAlexander Kornienko "this pattern occur which are not matched by a positive pattern"), 5256ccdbbdSAlexander Kornienko cl::value_desc("pattern")); 5356ccdbbdSAlexander Kornienko 5446e1fd61SAlexander Richardson static cl::list<std::string> GlobalDefines("D", cl::Prefix, 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 82dc5ba317SJoel E. Denny static cl::opt<bool> Verbose("v", cl::init(false), 83dc5ba317SJoel E. Denny cl::desc("Print directive pattern matches.\n")); 84dc5ba317SJoel E. Denny 85dc5ba317SJoel E. Denny static cl::opt<bool> VerboseVerbose( 86dc5ba317SJoel E. Denny "vv", cl::init(false), 87dc5ba317SJoel E. Denny cl::desc("Print information helpful in diagnosing internal FileCheck\n" 88dc5ba317SJoel E. Denny "issues. Implies -v.\n")); 89346dfbe2SGeorge Karpenkov static const char * DumpInputEnv = "FILECHECK_DUMP_INPUT_ON_FAILURE"; 90346dfbe2SGeorge Karpenkov 91346dfbe2SGeorge Karpenkov static cl::opt<bool> DumpInputOnFailure( 92346dfbe2SGeorge Karpenkov "dump-input-on-failure", cl::init(std::getenv(DumpInputEnv)), 93346dfbe2SGeorge Karpenkov cl::desc("Dump original input to stderr before failing.\n" 94346dfbe2SGeorge Karpenkov "The value can be also controlled using\n" 95*3c5d267eSJoel E. Denny "FILECHECK_DUMP_INPUT_ON_FAILURE environment variable.\n" 96*3c5d267eSJoel E. Denny "This option is deprecated in favor of -dump-input=fail.\n")); 97*3c5d267eSJoel E. Denny 98*3c5d267eSJoel E. Denny enum DumpInputValue { 99*3c5d267eSJoel E. Denny DumpInputDefault, 100*3c5d267eSJoel E. Denny DumpInputHelp, 101*3c5d267eSJoel E. Denny DumpInputNever, 102*3c5d267eSJoel E. Denny DumpInputFail, 103*3c5d267eSJoel E. Denny DumpInputAlways 104*3c5d267eSJoel E. Denny }; 105*3c5d267eSJoel E. Denny 106*3c5d267eSJoel E. Denny static cl::opt<DumpInputValue> DumpInput( 107*3c5d267eSJoel E. Denny "dump-input", cl::init(DumpInputDefault), 108*3c5d267eSJoel E. Denny cl::desc("Dump input to stderr, adding annotations representing\n" 109*3c5d267eSJoel E. Denny " currently enabled diagnostics\n"), 110*3c5d267eSJoel E. Denny cl::value_desc("mode"), 111*3c5d267eSJoel E. Denny cl::values(clEnumValN(DumpInputHelp, "help", 112*3c5d267eSJoel E. Denny "Explain dump format and quit"), 113*3c5d267eSJoel E. Denny clEnumValN(DumpInputNever, "never", "Never dump input"), 114*3c5d267eSJoel E. Denny clEnumValN(DumpInputFail, "fail", "Dump input on failure"), 115*3c5d267eSJoel E. Denny clEnumValN(DumpInputAlways, "always", "Always dump input"))); 116dc5ba317SJoel E. Denny 11713df4626SMatt Arsenault typedef cl::list<std::string>::const_iterator prefix_iterator; 11813df4626SMatt Arsenault 11913df4626SMatt Arsenault 12013df4626SMatt Arsenault 121726774cbSChandler Carruth 122726774cbSChandler Carruth 123726774cbSChandler Carruth 124c2735158SRui Ueyama 1252bd4f8b6SXinliang David Li static void DumpCommandLine(int argc, char **argv) { 1262bd4f8b6SXinliang David Li errs() << "FileCheck command line: "; 1272bd4f8b6SXinliang David Li for (int I = 0; I < argc; I++) 1282bd4f8b6SXinliang David Li errs() << " " << argv[I]; 1292bd4f8b6SXinliang David Li errs() << "\n"; 1302bd4f8b6SXinliang David Li } 1312bd4f8b6SXinliang David Li 132*3c5d267eSJoel E. Denny struct MarkerStyle { 133*3c5d267eSJoel E. Denny /// The starting char (before tildes) for marking the line. 134*3c5d267eSJoel E. Denny char Lead; 135*3c5d267eSJoel E. Denny /// What color to use for this annotation. 136*3c5d267eSJoel E. Denny raw_ostream::Colors Color; 137*3c5d267eSJoel E. Denny /// A note to follow the marker, or empty string if none. 138*3c5d267eSJoel E. Denny std::string Note; 139*3c5d267eSJoel E. Denny MarkerStyle() {} 140*3c5d267eSJoel E. Denny MarkerStyle(char Lead, raw_ostream::Colors Color, const std::string &Note) 141*3c5d267eSJoel E. Denny : Lead(Lead), Color(Color), Note(Note) {} 142*3c5d267eSJoel E. Denny }; 143*3c5d267eSJoel E. Denny 144*3c5d267eSJoel E. Denny static MarkerStyle GetMarker(FileCheckDiag::MatchType MatchTy) { 145*3c5d267eSJoel E. Denny switch (MatchTy) { 146*3c5d267eSJoel E. Denny case FileCheckDiag::MatchNoneButExpected: 147*3c5d267eSJoel E. Denny return MarkerStyle('X', raw_ostream::RED, "error: no match found"); 148*3c5d267eSJoel E. Denny case FileCheckDiag::MatchTypeCount: 149*3c5d267eSJoel E. Denny llvm_unreachable_internal("unexpected match type"); 150*3c5d267eSJoel E. Denny } 151*3c5d267eSJoel E. Denny llvm_unreachable_internal("unexpected match type"); 152*3c5d267eSJoel E. Denny } 153*3c5d267eSJoel E. Denny 154*3c5d267eSJoel E. Denny static void DumpInputAnnotationHelp(raw_ostream &OS) { 155*3c5d267eSJoel E. Denny OS << "The following description was requested by -dump-input=help to\n" 156*3c5d267eSJoel E. Denny << "explain the input annotations printed by -dump-input=always and\n" 157*3c5d267eSJoel E. Denny << "-dump-input=fail:\n\n"; 158*3c5d267eSJoel E. Denny 159*3c5d267eSJoel E. Denny // Labels for input lines. 160*3c5d267eSJoel E. Denny OS << " - "; 161*3c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "L:"; 162*3c5d267eSJoel E. Denny OS << " labels line number L of the input file\n"; 163*3c5d267eSJoel E. Denny 164*3c5d267eSJoel E. Denny // Labels for annotation lines. 165*3c5d267eSJoel E. Denny OS << " - "; 166*3c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L"; 167*3c5d267eSJoel E. Denny OS << " labels the match result for a pattern of type T from " 168*3c5d267eSJoel E. Denny << "line L of\n" 169*3c5d267eSJoel E. Denny << " the check file\n"; 170*3c5d267eSJoel E. Denny 171*3c5d267eSJoel E. Denny // Markers on annotation lines. 172*3c5d267eSJoel E. Denny OS << " - "; 173*3c5d267eSJoel E. Denny WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "X~~"; 174*3c5d267eSJoel E. Denny OS << " marks search range when no match is found\n"; 175*3c5d267eSJoel E. Denny 176*3c5d267eSJoel E. Denny // Colors. 177*3c5d267eSJoel E. Denny OS << " - colors "; 178*3c5d267eSJoel E. Denny WithColor(OS, raw_ostream::RED, true) << "error"; 179*3c5d267eSJoel E. Denny OS << "\n\n" 180*3c5d267eSJoel E. Denny << "If you are not seeing color above or in input dumps, try: -color\n"; 181*3c5d267eSJoel E. Denny } 182*3c5d267eSJoel E. Denny 183*3c5d267eSJoel E. Denny /// An annotation for a single input line. 184*3c5d267eSJoel E. Denny struct InputAnnotation { 185*3c5d267eSJoel E. Denny /// The check file line (one-origin indexing) where the directive that 186*3c5d267eSJoel E. Denny /// produced this annotation is located. 187*3c5d267eSJoel E. Denny unsigned CheckLine; 188*3c5d267eSJoel E. Denny /// The label for this annotation. 189*3c5d267eSJoel E. Denny std::string Label; 190*3c5d267eSJoel E. Denny /// What input line (one-origin indexing) this annotation marks. This might 191*3c5d267eSJoel E. Denny /// be different from the starting line of the original diagnostic if this is 192*3c5d267eSJoel E. Denny /// a non-initial fragment of a diagnostic that has been broken across 193*3c5d267eSJoel E. Denny /// multiple lines. 194*3c5d267eSJoel E. Denny unsigned InputLine; 195*3c5d267eSJoel E. Denny /// The column range (one-origin indexing, open end) in which to to mark the 196*3c5d267eSJoel E. Denny /// input line. If InputEndCol is UINT_MAX, treat it as the last column 197*3c5d267eSJoel E. Denny /// before the newline. 198*3c5d267eSJoel E. Denny unsigned InputStartCol, InputEndCol; 199*3c5d267eSJoel E. Denny /// The marker to use. 200*3c5d267eSJoel E. Denny MarkerStyle Marker; 201*3c5d267eSJoel E. Denny }; 202*3c5d267eSJoel E. Denny 203*3c5d267eSJoel E. Denny /// Get an abbreviation for the check type. 204*3c5d267eSJoel E. Denny std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) { 205*3c5d267eSJoel E. Denny switch (Ty) { 206*3c5d267eSJoel E. Denny case Check::CheckPlain: 207*3c5d267eSJoel E. Denny if (Ty.getCount() > 1) 208*3c5d267eSJoel E. Denny return "count"; 209*3c5d267eSJoel E. Denny return "check"; 210*3c5d267eSJoel E. Denny case Check::CheckNext: 211*3c5d267eSJoel E. Denny return "next"; 212*3c5d267eSJoel E. Denny case Check::CheckSame: 213*3c5d267eSJoel E. Denny return "same"; 214*3c5d267eSJoel E. Denny case Check::CheckNot: 215*3c5d267eSJoel E. Denny return "not"; 216*3c5d267eSJoel E. Denny case Check::CheckDAG: 217*3c5d267eSJoel E. Denny return "dag"; 218*3c5d267eSJoel E. Denny case Check::CheckLabel: 219*3c5d267eSJoel E. Denny return "label"; 220*3c5d267eSJoel E. Denny case Check::CheckEmpty: 221*3c5d267eSJoel E. Denny return "empty"; 222*3c5d267eSJoel E. Denny case Check::CheckEOF: 223*3c5d267eSJoel E. Denny return "eof"; 224*3c5d267eSJoel E. Denny case Check::CheckBadNot: 225*3c5d267eSJoel E. Denny return "bad-not"; 226*3c5d267eSJoel E. Denny case Check::CheckBadCount: 227*3c5d267eSJoel E. Denny return "bad-count"; 228*3c5d267eSJoel E. Denny case Check::CheckNone: 229*3c5d267eSJoel E. Denny llvm_unreachable("invalid FileCheckType"); 230*3c5d267eSJoel E. Denny } 231*3c5d267eSJoel E. Denny llvm_unreachable("unknown FileCheckType"); 232*3c5d267eSJoel E. Denny } 233*3c5d267eSJoel E. Denny 234*3c5d267eSJoel E. Denny static void BuildInputAnnotations(const std::vector<FileCheckDiag> &Diags, 235*3c5d267eSJoel E. Denny std::vector<InputAnnotation> &Annotations, 236*3c5d267eSJoel E. Denny unsigned &LabelWidth) { 237*3c5d267eSJoel E. Denny // What's the widest label? 238*3c5d267eSJoel E. Denny LabelWidth = 0; 239*3c5d267eSJoel E. Denny for (auto DiagItr = Diags.begin(), DiagEnd = Diags.end(); DiagItr != DiagEnd; 240*3c5d267eSJoel E. Denny ++DiagItr) { 241*3c5d267eSJoel E. Denny InputAnnotation A; 242*3c5d267eSJoel E. Denny 243*3c5d267eSJoel E. Denny // Build label, which uniquely identifies this check result. 244*3c5d267eSJoel E. Denny A.CheckLine = DiagItr->CheckLine; 245*3c5d267eSJoel E. Denny llvm::raw_string_ostream Label(A.Label); 246*3c5d267eSJoel E. Denny Label << GetCheckTypeAbbreviation(DiagItr->CheckTy) << ":" 247*3c5d267eSJoel E. Denny << DiagItr->CheckLine; 248*3c5d267eSJoel E. Denny Label.flush(); 249*3c5d267eSJoel E. Denny LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size()); 250*3c5d267eSJoel E. Denny 251*3c5d267eSJoel E. Denny MarkerStyle Marker = GetMarker(DiagItr->MatchTy); 252*3c5d267eSJoel E. Denny A.Marker = Marker; 253*3c5d267eSJoel E. Denny 254*3c5d267eSJoel E. Denny // Compute the mark location, and break annotation into multiple 255*3c5d267eSJoel E. Denny // annotations if it spans multiple lines. 256*3c5d267eSJoel E. Denny A.InputLine = DiagItr->InputStartLine; 257*3c5d267eSJoel E. Denny A.InputStartCol = DiagItr->InputStartCol; 258*3c5d267eSJoel E. Denny if (DiagItr->InputStartLine == DiagItr->InputEndLine) { 259*3c5d267eSJoel E. Denny // Sometimes ranges are empty in order to indicate a specific point, but 260*3c5d267eSJoel E. Denny // that would mean nothing would be marked, so adjust the range to 261*3c5d267eSJoel E. Denny // include the following character. 262*3c5d267eSJoel E. Denny A.InputEndCol = 263*3c5d267eSJoel E. Denny std::max(DiagItr->InputStartCol + 1, DiagItr->InputEndCol); 264*3c5d267eSJoel E. Denny Annotations.push_back(A); 265*3c5d267eSJoel E. Denny } else { 266*3c5d267eSJoel E. Denny assert(DiagItr->InputStartLine < DiagItr->InputEndLine && 267*3c5d267eSJoel E. Denny "expected input range not to be inverted"); 268*3c5d267eSJoel E. Denny A.InputEndCol = UINT_MAX; 269*3c5d267eSJoel E. Denny A.Marker.Note = ""; 270*3c5d267eSJoel E. Denny Annotations.push_back(A); 271*3c5d267eSJoel E. Denny for (unsigned L = DiagItr->InputStartLine + 1, E = DiagItr->InputEndLine; 272*3c5d267eSJoel E. Denny L <= E; ++L) { 273*3c5d267eSJoel E. Denny // If a range ends before the first column on a line, then it has no 274*3c5d267eSJoel E. Denny // characters on that line, so there's nothing to render. 275*3c5d267eSJoel E. Denny if (DiagItr->InputEndCol == 1 && L == E) { 276*3c5d267eSJoel E. Denny Annotations.back().Marker.Note = Marker.Note; 277*3c5d267eSJoel E. Denny break; 278*3c5d267eSJoel E. Denny } 279*3c5d267eSJoel E. Denny InputAnnotation B; 280*3c5d267eSJoel E. Denny B.CheckLine = A.CheckLine; 281*3c5d267eSJoel E. Denny B.Label = A.Label; 282*3c5d267eSJoel E. Denny B.InputLine = L; 283*3c5d267eSJoel E. Denny B.Marker = Marker; 284*3c5d267eSJoel E. Denny B.Marker.Lead = '~'; 285*3c5d267eSJoel E. Denny B.InputStartCol = 1; 286*3c5d267eSJoel E. Denny if (L != E) { 287*3c5d267eSJoel E. Denny B.InputEndCol = UINT_MAX; 288*3c5d267eSJoel E. Denny B.Marker.Note = ""; 289*3c5d267eSJoel E. Denny } else 290*3c5d267eSJoel E. Denny B.InputEndCol = DiagItr->InputEndCol; 291*3c5d267eSJoel E. Denny Annotations.push_back(B); 292*3c5d267eSJoel E. Denny } 293*3c5d267eSJoel E. Denny } 294*3c5d267eSJoel E. Denny } 295*3c5d267eSJoel E. Denny } 296*3c5d267eSJoel E. Denny 297*3c5d267eSJoel E. Denny static void DumpAnnotatedInput( 298*3c5d267eSJoel E. Denny raw_ostream &OS, StringRef InputFileText, 299*3c5d267eSJoel E. Denny std::vector<InputAnnotation> &Annotations, unsigned LabelWidth) { 300*3c5d267eSJoel E. Denny OS << "Full input was:\n<<<<<<\n"; 301*3c5d267eSJoel E. Denny 302*3c5d267eSJoel E. Denny // Sort annotations. 303*3c5d267eSJoel E. Denny // 304*3c5d267eSJoel E. Denny // First, sort in the order of input lines to make it easier to find relevant 305*3c5d267eSJoel E. Denny // annotations while iterating input lines in the implementation below. 306*3c5d267eSJoel E. Denny // FileCheck diagnostics are not always reported and recorded in the order of 307*3c5d267eSJoel E. Denny // input lines due to, for example, CHECK-DAG and CHECK-NOT. 308*3c5d267eSJoel E. Denny // 309*3c5d267eSJoel E. Denny // Second, for annotations for the same input line, sort in the order of the 310*3c5d267eSJoel E. Denny // FileCheck directive's line in the check file (where there's at most one 311*3c5d267eSJoel E. Denny // directive per line). The rationale of this choice is that, for any input 312*3c5d267eSJoel E. Denny // line, this sort establishes a total order of annotations that, with 313*3c5d267eSJoel E. Denny // respect to match results, is consistent across multiple lines, thus 314*3c5d267eSJoel E. Denny // making match results easier to track from one line to the next when they 315*3c5d267eSJoel E. Denny // span multiple lines. 316*3c5d267eSJoel E. Denny std::sort(Annotations.begin(), Annotations.end(), 317*3c5d267eSJoel E. Denny [](const InputAnnotation &A, const InputAnnotation &B) { 318*3c5d267eSJoel E. Denny if (A.InputLine != B.InputLine) 319*3c5d267eSJoel E. Denny return A.InputLine < B.InputLine; 320*3c5d267eSJoel E. Denny return A.CheckLine < B.CheckLine; 321*3c5d267eSJoel E. Denny }); 322*3c5d267eSJoel E. Denny 323*3c5d267eSJoel E. Denny // Compute the width of the label column. 324*3c5d267eSJoel E. Denny const unsigned char *InputFilePtr = InputFileText.bytes_begin(), 325*3c5d267eSJoel E. Denny *InputFileEnd = InputFileText.bytes_end(); 326*3c5d267eSJoel E. Denny unsigned LineCount = InputFileText.count('\n'); 327*3c5d267eSJoel E. Denny if (InputFileEnd[-1] != '\n') 328*3c5d267eSJoel E. Denny ++LineCount; 329*3c5d267eSJoel E. Denny unsigned LineNoWidth = log10(LineCount) + 1; 330*3c5d267eSJoel E. Denny // +3 below adds spaces (1) to the left of the (right-aligned) line numbers 331*3c5d267eSJoel E. Denny // on input lines and (2) to the right of the (left-aligned) labels on 332*3c5d267eSJoel E. Denny // annotation lines so that input lines and annotation lines are more 333*3c5d267eSJoel E. Denny // visually distinct. For example, the spaces on the annotation lines ensure 334*3c5d267eSJoel E. Denny // that input line numbers and check directive line numbers never align 335*3c5d267eSJoel E. Denny // horizontally. Those line numbers might not even be for the same file. 336*3c5d267eSJoel E. Denny // One space would be enough to achieve that, but more makes it even easier 337*3c5d267eSJoel E. Denny // to see. 338*3c5d267eSJoel E. Denny LabelWidth = std::max(LabelWidth, LineNoWidth) + 3; 339*3c5d267eSJoel E. Denny 340*3c5d267eSJoel E. Denny // Print annotated input lines. 341*3c5d267eSJoel E. Denny auto AnnotationItr = Annotations.begin(), AnnotationEnd = Annotations.end(); 342*3c5d267eSJoel E. Denny for (unsigned Line = 1; 343*3c5d267eSJoel E. Denny InputFilePtr != InputFileEnd || AnnotationItr != AnnotationEnd; 344*3c5d267eSJoel E. Denny ++Line) { 345*3c5d267eSJoel E. Denny const unsigned char *InputFileLine = InputFilePtr; 346*3c5d267eSJoel E. Denny 347*3c5d267eSJoel E. Denny // Print right-aligned line number. 348*3c5d267eSJoel E. Denny WithColor(OS, raw_ostream::BLACK, true) 349*3c5d267eSJoel E. Denny << format_decimal(Line, LabelWidth) << ": "; 350*3c5d267eSJoel E. Denny 351*3c5d267eSJoel E. Denny // Print numbered line. 352*3c5d267eSJoel E. Denny bool Newline = false; 353*3c5d267eSJoel E. Denny while (InputFilePtr != InputFileEnd && !Newline) { 354*3c5d267eSJoel E. Denny if (*InputFilePtr == '\n') 355*3c5d267eSJoel E. Denny Newline = true; 356*3c5d267eSJoel E. Denny else 357*3c5d267eSJoel E. Denny OS << *InputFilePtr; 358*3c5d267eSJoel E. Denny ++InputFilePtr; 359*3c5d267eSJoel E. Denny } 360*3c5d267eSJoel E. Denny OS << '\n'; 361*3c5d267eSJoel E. Denny unsigned InputLineWidth = InputFilePtr - InputFileLine - Newline; 362*3c5d267eSJoel E. Denny 363*3c5d267eSJoel E. Denny // Print any annotations. 364*3c5d267eSJoel E. Denny while (AnnotationItr != AnnotationEnd && 365*3c5d267eSJoel E. Denny AnnotationItr->InputLine == Line) { 366*3c5d267eSJoel E. Denny WithColor COS(OS, AnnotationItr->Marker.Color, true); 367*3c5d267eSJoel E. Denny // The two spaces below are where the ": " appears on input lines. 368*3c5d267eSJoel E. Denny COS << left_justify(AnnotationItr->Label, LabelWidth) << " "; 369*3c5d267eSJoel E. Denny unsigned Col; 370*3c5d267eSJoel E. Denny for (Col = 1; Col < AnnotationItr->InputStartCol; ++Col) 371*3c5d267eSJoel E. Denny COS << ' '; 372*3c5d267eSJoel E. Denny COS << AnnotationItr->Marker.Lead; 373*3c5d267eSJoel E. Denny // If InputEndCol=UINT_MAX, stop at InputLineWidth. 374*3c5d267eSJoel E. Denny for (++Col; Col < AnnotationItr->InputEndCol && Col <= InputLineWidth; 375*3c5d267eSJoel E. Denny ++Col) 376*3c5d267eSJoel E. Denny COS << '~'; 377*3c5d267eSJoel E. Denny const std::string &Note = AnnotationItr->Marker.Note; 378*3c5d267eSJoel E. Denny if (!Note.empty()) { 379*3c5d267eSJoel E. Denny // Put the note at the end of the input line. If we were to instead 380*3c5d267eSJoel E. Denny // put the note right after the marker, subsequent annotations for the 381*3c5d267eSJoel E. Denny // same input line might appear to mark this note instead of the input 382*3c5d267eSJoel E. Denny // line. 383*3c5d267eSJoel E. Denny for (; Col <= InputLineWidth; ++Col) 384*3c5d267eSJoel E. Denny COS << ' '; 385*3c5d267eSJoel E. Denny COS << ' ' << Note; 386*3c5d267eSJoel E. Denny } 387*3c5d267eSJoel E. Denny COS << '\n'; 388*3c5d267eSJoel E. Denny ++AnnotationItr; 389*3c5d267eSJoel E. Denny } 390*3c5d267eSJoel E. Denny } 391*3c5d267eSJoel E. Denny 392*3c5d267eSJoel E. Denny OS << ">>>>>>\n"; 393*3c5d267eSJoel E. Denny } 394*3c5d267eSJoel E. Denny 395ee3c74fbSChris Lattner int main(int argc, char **argv) { 3963e66509fSJoel E. Denny // Enable use of ANSI color codes because FileCheck is using them to 3973e66509fSJoel E. Denny // highlight text. 3983e66509fSJoel E. Denny llvm::sys::Process::UseANSIEscapeCodes(true); 3993e66509fSJoel E. Denny 400197194b6SRui Ueyama InitLLVM X(argc, argv); 40124994d77SJoel E. Denny cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr, 40224994d77SJoel E. Denny "FILECHECK_OPTS"); 403*3c5d267eSJoel E. Denny if (DumpInput == DumpInputHelp) { 404*3c5d267eSJoel E. Denny DumpInputAnnotationHelp(outs()); 405*3c5d267eSJoel E. Denny return 0; 406*3c5d267eSJoel E. Denny } 407*3c5d267eSJoel E. Denny if (CheckFilename.empty()) { 408*3c5d267eSJoel E. Denny errs() << "<check-file> not specified\n"; 409*3c5d267eSJoel E. Denny return 2; 410*3c5d267eSJoel E. Denny } 411ee3c74fbSChris Lattner 412ffa9d2e4SAditya Nandakumar FileCheckRequest Req; 413ffa9d2e4SAditya Nandakumar for (auto Prefix : CheckPrefixes) 414ffa9d2e4SAditya Nandakumar Req.CheckPrefixes.push_back(Prefix); 415ffa9d2e4SAditya Nandakumar 416ffa9d2e4SAditya Nandakumar for (auto CheckNot : ImplicitCheckNot) 417ffa9d2e4SAditya Nandakumar Req.ImplicitCheckNot.push_back(CheckNot); 418ffa9d2e4SAditya Nandakumar 419ffa9d2e4SAditya Nandakumar for (auto G : GlobalDefines) 420ffa9d2e4SAditya Nandakumar Req.GlobalDefines.push_back(G); 421ffa9d2e4SAditya Nandakumar 422ffa9d2e4SAditya Nandakumar Req.AllowEmptyInput = AllowEmptyInput; 423ffa9d2e4SAditya Nandakumar Req.EnableVarScope = EnableVarScope; 424ffa9d2e4SAditya Nandakumar Req.AllowDeprecatedDagOverlap = AllowDeprecatedDagOverlap; 425ffa9d2e4SAditya Nandakumar Req.Verbose = Verbose; 426ffa9d2e4SAditya Nandakumar Req.VerboseVerbose = VerboseVerbose; 427ffa9d2e4SAditya Nandakumar Req.NoCanonicalizeWhiteSpace = NoCanonicalizeWhiteSpace; 428ffa9d2e4SAditya Nandakumar Req.MatchFullLines = MatchFullLines; 429ffa9d2e4SAditya Nandakumar 430ffa9d2e4SAditya Nandakumar if (VerboseVerbose) 431ffa9d2e4SAditya Nandakumar Req.Verbose = true; 432ffa9d2e4SAditya Nandakumar 433ffa9d2e4SAditya Nandakumar FileCheck FC(Req); 434ffa9d2e4SAditya Nandakumar if (!FC.ValidateCheckPrefixes()) { 43513df4626SMatt Arsenault errs() << "Supplied check-prefix is invalid! Prefixes must be unique and " 43613df4626SMatt Arsenault "start with a letter and contain only alphanumeric characters, " 43713df4626SMatt Arsenault "hyphens and underscores\n"; 438c2735158SRui Ueyama return 2; 439c2735158SRui Ueyama } 440c2735158SRui Ueyama 441ffa9d2e4SAditya Nandakumar Regex PrefixRE = FC.buildCheckPrefixRegex(); 442726774cbSChandler Carruth std::string REError; 443726774cbSChandler Carruth if (!PrefixRE.isValid(REError)) { 444726774cbSChandler Carruth errs() << "Unable to combine check-prefix strings into a prefix regular " 445726774cbSChandler Carruth "expression! This is likely a bug in FileCheck's verification of " 446726774cbSChandler Carruth "the check-prefix strings. Regular expression parsing failed " 447726774cbSChandler Carruth "with the following error: " 448726774cbSChandler Carruth << REError << "\n"; 449726774cbSChandler Carruth return 2; 450726774cbSChandler Carruth } 45113df4626SMatt Arsenault 452ee3c74fbSChris Lattner SourceMgr SM; 453ee3c74fbSChris Lattner 454ee3c74fbSChris Lattner // Read the expected strings from the check file. 45520247900SChandler Carruth ErrorOr<std::unique_ptr<MemoryBuffer>> CheckFileOrErr = 45620247900SChandler Carruth MemoryBuffer::getFileOrSTDIN(CheckFilename); 45720247900SChandler Carruth if (std::error_code EC = CheckFileOrErr.getError()) { 45820247900SChandler Carruth errs() << "Could not open check file '" << CheckFilename 45920247900SChandler Carruth << "': " << EC.message() << '\n'; 46020247900SChandler Carruth return 2; 46120247900SChandler Carruth } 46220247900SChandler Carruth MemoryBuffer &CheckFile = *CheckFileOrErr.get(); 46320247900SChandler Carruth 46420247900SChandler Carruth SmallString<4096> CheckFileBuffer; 465ffa9d2e4SAditya Nandakumar StringRef CheckFileText = FC.CanonicalizeFile(CheckFile, CheckFileBuffer); 46620247900SChandler Carruth 46720247900SChandler Carruth SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 46820247900SChandler Carruth CheckFileText, CheckFile.getBufferIdentifier()), 46920247900SChandler Carruth SMLoc()); 47020247900SChandler Carruth 471ffa9d2e4SAditya Nandakumar std::vector<FileCheckString> CheckStrings; 472ffa9d2e4SAditya Nandakumar if (FC.ReadCheckFile(SM, CheckFileText, PrefixRE, CheckStrings)) 473ee3c74fbSChris Lattner return 2; 474ee3c74fbSChris Lattner 475ee3c74fbSChris Lattner // Open the file to check and add it to SourceMgr. 47620247900SChandler Carruth ErrorOr<std::unique_ptr<MemoryBuffer>> InputFileOrErr = 477adf21f2aSRafael Espindola MemoryBuffer::getFileOrSTDIN(InputFilename); 47820247900SChandler Carruth if (std::error_code EC = InputFileOrErr.getError()) { 479adf21f2aSRafael Espindola errs() << "Could not open input file '" << InputFilename 480adf21f2aSRafael Espindola << "': " << EC.message() << '\n'; 4818e1c6477SEli Bendersky return 2; 482ee3c74fbSChris Lattner } 48320247900SChandler Carruth MemoryBuffer &InputFile = *InputFileOrErr.get(); 4842c3e5cdfSChris Lattner 48520247900SChandler Carruth if (InputFile.getBufferSize() == 0 && !AllowEmptyInput) { 486b692bed7SChris Lattner errs() << "FileCheck error: '" << InputFilename << "' is empty.\n"; 4872bd4f8b6SXinliang David Li DumpCommandLine(argc, argv); 4888e1c6477SEli Bendersky return 2; 489b692bed7SChris Lattner } 490b692bed7SChris Lattner 49120247900SChandler Carruth SmallString<4096> InputFileBuffer; 492ffa9d2e4SAditya Nandakumar StringRef InputFileText = FC.CanonicalizeFile(InputFile, InputFileBuffer); 4932c3e5cdfSChris Lattner 494e8f2fb20SChandler Carruth SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer( 495e8f2fb20SChandler Carruth InputFileText, InputFile.getBufferIdentifier()), 496e8f2fb20SChandler Carruth SMLoc()); 497ee3c74fbSChris Lattner 498*3c5d267eSJoel E. Denny if (DumpInput == DumpInputDefault) 499*3c5d267eSJoel E. Denny DumpInput = DumpInputOnFailure ? DumpInputFail : DumpInputNever; 500*3c5d267eSJoel E. Denny 501*3c5d267eSJoel E. Denny std::vector<FileCheckDiag> Diags; 502*3c5d267eSJoel E. Denny int ExitCode = FC.CheckInput(SM, InputFileText, CheckStrings, 503*3c5d267eSJoel E. Denny DumpInput == DumpInputNever ? nullptr : &Diags) 504*3c5d267eSJoel E. Denny ? EXIT_SUCCESS 505*3c5d267eSJoel E. Denny : 1; 506*3c5d267eSJoel E. Denny if (DumpInput == DumpInputAlways || 507*3c5d267eSJoel E. Denny (ExitCode == 1 && DumpInput == DumpInputFail)) { 508*3c5d267eSJoel E. Denny errs() << "\n" 509*3c5d267eSJoel E. Denny << "Input file: " 510*3c5d267eSJoel E. Denny << (InputFilename == "-" ? "<stdin>" : InputFilename.getValue()) 511*3c5d267eSJoel E. Denny << "\n" 512*3c5d267eSJoel E. Denny << "Check file: " << CheckFilename << "\n" 513*3c5d267eSJoel E. Denny << "\n" 514*3c5d267eSJoel E. Denny << "-dump-input=help describes the format of the following dump.\n" 515*3c5d267eSJoel E. Denny << "\n"; 516*3c5d267eSJoel E. Denny std::vector<InputAnnotation> Annotations; 517*3c5d267eSJoel E. Denny unsigned LabelWidth; 518*3c5d267eSJoel E. Denny BuildInputAnnotations(Diags, Annotations, LabelWidth); 519*3c5d267eSJoel E. Denny DumpAnnotatedInput(errs(), InputFileText, Annotations, LabelWidth); 520*3c5d267eSJoel E. Denny } 521346dfbe2SGeorge Karpenkov 522346dfbe2SGeorge Karpenkov return ExitCode; 523ee3c74fbSChris Lattner } 524