1 //===- FileCheck.cpp - Check that File's Contents match what is expected --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // FileCheck does a line-by line check of a file that validates whether it
11 // contains the expected content.  This is useful for regression tests etc.
12 //
13 // This program exits with an exit status of 2 on error, exit status of 0 if
14 // the file matched the expected contents, and exit status of 1 if it did not
15 // contain the expected contents.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/InitLLVM.h"
21 #include "llvm/Support/Process.h"
22 #include "llvm/Support/WithColor.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Support/FileCheck.h"
25 using namespace llvm;
26 
27 static cl::opt<std::string>
28     CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Optional);
29 
30 static cl::opt<std::string>
31     InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
32                   cl::init("-"), cl::value_desc("filename"));
33 
34 static cl::list<std::string> CheckPrefixes(
35     "check-prefix",
36     cl::desc("Prefix to use from check file (defaults to 'CHECK')"));
37 static cl::alias CheckPrefixesAlias(
38     "check-prefixes", cl::aliasopt(CheckPrefixes), cl::CommaSeparated,
39     cl::NotHidden,
40     cl::desc(
41         "Alias for -check-prefix permitting multiple comma separated values"));
42 
43 static cl::opt<bool> NoCanonicalizeWhiteSpace(
44     "strict-whitespace",
45     cl::desc("Do not treat all horizontal whitespace as equivalent"));
46 
47 static cl::list<std::string> ImplicitCheckNot(
48     "implicit-check-not",
49     cl::desc("Add an implicit negative check with this pattern to every\n"
50              "positive check. This can be used to ensure that no instances of\n"
51              "this pattern occur which are not matched by a positive pattern"),
52     cl::value_desc("pattern"));
53 
54 static cl::list<std::string> GlobalDefines("D", cl::Prefix,
55     cl::desc("Define a variable to be used in capture patterns."),
56     cl::value_desc("VAR=VALUE"));
57 
58 static cl::opt<bool> AllowEmptyInput(
59     "allow-empty", cl::init(false),
60     cl::desc("Allow the input file to be empty. This is useful when making\n"
61              "checks that some error message does not occur, for example."));
62 
63 static cl::opt<bool> MatchFullLines(
64     "match-full-lines", cl::init(false),
65     cl::desc("Require all positive matches to cover an entire input line.\n"
66              "Allows leading and trailing whitespace if --strict-whitespace\n"
67              "is not also passed."));
68 
69 static cl::opt<bool> EnableVarScope(
70     "enable-var-scope", cl::init(false),
71     cl::desc("Enables scope for regex variables. Variables with names that\n"
72              "do not start with '$' will be reset at the beginning of\n"
73              "each CHECK-LABEL block."));
74 
75 static cl::opt<bool> AllowDeprecatedDagOverlap(
76     "allow-deprecated-dag-overlap", cl::init(false),
77     cl::desc("Enable overlapping among matches in a group of consecutive\n"
78              "CHECK-DAG directives.  This option is deprecated and is only\n"
79              "provided for convenience as old tests are migrated to the new\n"
80              "non-overlapping CHECK-DAG implementation.\n"));
81 
82 static cl::opt<bool> Verbose("v", cl::init(false),
83                              cl::desc("Print directive pattern matches.\n"));
84 
85 static cl::opt<bool> VerboseVerbose(
86     "vv", cl::init(false),
87     cl::desc("Print information helpful in diagnosing internal FileCheck\n"
88              "issues.  Implies -v.\n"));
89 static const char * DumpInputEnv = "FILECHECK_DUMP_INPUT_ON_FAILURE";
90 
91 static cl::opt<bool> DumpInputOnFailure(
92     "dump-input-on-failure", cl::init(std::getenv(DumpInputEnv)),
93     cl::desc("Dump original input to stderr before failing.\n"
94              "The value can be also controlled using\n"
95              "FILECHECK_DUMP_INPUT_ON_FAILURE environment variable.\n"
96              "This option is deprecated in favor of -dump-input=fail.\n"));
97 
98 enum DumpInputValue {
99   DumpInputDefault,
100   DumpInputHelp,
101   DumpInputNever,
102   DumpInputFail,
103   DumpInputAlways
104 };
105 
106 static cl::opt<DumpInputValue> DumpInput(
107     "dump-input", cl::init(DumpInputDefault),
108     cl::desc("Dump input to stderr, adding annotations representing\n"
109              " currently enabled diagnostics\n"),
110     cl::value_desc("mode"),
111     cl::values(clEnumValN(DumpInputHelp, "help",
112                           "Explain dump format and quit"),
113                clEnumValN(DumpInputNever, "never", "Never dump input"),
114                clEnumValN(DumpInputFail, "fail", "Dump input on failure"),
115                clEnumValN(DumpInputAlways, "always", "Always dump input")));
116 
117 typedef cl::list<std::string>::const_iterator prefix_iterator;
118 
119 
120 
121 
122 
123 
124 
125 static void DumpCommandLine(int argc, char **argv) {
126   errs() << "FileCheck command line: ";
127   for (int I = 0; I < argc; I++)
128     errs() << " " << argv[I];
129   errs() << "\n";
130 }
131 
132 struct MarkerStyle {
133   /// The starting char (before tildes) for marking the line.
134   char Lead;
135   /// What color to use for this annotation.
136   raw_ostream::Colors Color;
137   /// A note to follow the marker, or empty string if none.
138   std::string Note;
139   MarkerStyle() {}
140   MarkerStyle(char Lead, raw_ostream::Colors Color, const std::string &Note)
141       : Lead(Lead), Color(Color), Note(Note) {}
142 };
143 
144 static MarkerStyle GetMarker(FileCheckDiag::MatchType MatchTy) {
145   switch (MatchTy) {
146   case FileCheckDiag::MatchFinalButWrongLine:
147     return MarkerStyle('!', raw_ostream::RED, "error: match on wrong line");
148   case FileCheckDiag::MatchNoneButExpected:
149     return MarkerStyle('X', raw_ostream::RED, "error: no match found");
150   case FileCheckDiag::MatchFuzzy:
151     return MarkerStyle('?', raw_ostream::MAGENTA, "possible intended match");
152   case FileCheckDiag::MatchTypeCount:
153     llvm_unreachable_internal("unexpected match type");
154   }
155   llvm_unreachable_internal("unexpected match type");
156 }
157 
158 static void DumpInputAnnotationHelp(raw_ostream &OS) {
159   OS << "The following description was requested by -dump-input=help to\n"
160      << "explain the input annotations printed by -dump-input=always and\n"
161      << "-dump-input=fail:\n\n";
162 
163   // Labels for input lines.
164   OS << "  - ";
165   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "L:";
166   OS << "     labels line number L of the input file\n";
167 
168   // Labels for annotation lines.
169   OS << "  - ";
170   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L";
171   OS << "    labels the only match result for a pattern of type T from "
172      << "line L of\n"
173      << "           the check file\n";
174   OS << "  - ";
175   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L'N";
176   OS << "  labels the Nth match result for a pattern of type T from line "
177      << "L of\n"
178      << "           the check file\n";
179 
180   // Markers on annotation lines.
181   OS << "  - ";
182   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "!~~";
183   OS << "    marks bad match, such as:\n"
184      << "           - CHECK-NEXT on same line as previous match (error)\n"
185      << "  - ";
186   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "X~~";
187   OS << "    marks search range when no match is found, such as:\n"
188      << "           - CHECK-NEXT not found (error)\n"
189      << "  - ";
190   WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "?";
191   OS << "      marks fuzzy match when no match is found\n";
192 
193   // Colors.
194   OS << "  - colors ";
195   WithColor(OS, raw_ostream::RED, true) << "error";
196   OS << ", ";
197   WithColor(OS, raw_ostream::MAGENTA, true) << "fuzzy match";
198   OS << "\n\n"
199      << "If you are not seeing color above or in input dumps, try: -color\n";
200 }
201 
202 /// An annotation for a single input line.
203 struct InputAnnotation {
204   /// The check file line (one-origin indexing) where the directive that
205   /// produced this annotation is located.
206   unsigned CheckLine;
207   /// The index of the match result for this check.
208   unsigned CheckDiagIndex;
209   /// The label for this annotation.
210   std::string Label;
211   /// What input line (one-origin indexing) this annotation marks.  This might
212   /// be different from the starting line of the original diagnostic if this is
213   /// a non-initial fragment of a diagnostic that has been broken across
214   /// multiple lines.
215   unsigned InputLine;
216   /// The column range (one-origin indexing, open end) in which to to mark the
217   /// input line.  If InputEndCol is UINT_MAX, treat it as the last column
218   /// before the newline.
219   unsigned InputStartCol, InputEndCol;
220   /// The marker to use.
221   MarkerStyle Marker;
222 };
223 
224 /// Get an abbreviation for the check type.
225 std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) {
226   switch (Ty) {
227   case Check::CheckPlain:
228     if (Ty.getCount() > 1)
229       return "count";
230     return "check";
231   case Check::CheckNext:
232     return "next";
233   case Check::CheckSame:
234     return "same";
235   case Check::CheckNot:
236     return "not";
237   case Check::CheckDAG:
238     return "dag";
239   case Check::CheckLabel:
240     return "label";
241   case Check::CheckEmpty:
242     return "empty";
243   case Check::CheckEOF:
244     return "eof";
245   case Check::CheckBadNot:
246     return "bad-not";
247   case Check::CheckBadCount:
248     return "bad-count";
249   case Check::CheckNone:
250     llvm_unreachable("invalid FileCheckType");
251   }
252   llvm_unreachable("unknown FileCheckType");
253 }
254 
255 static void BuildInputAnnotations(const std::vector<FileCheckDiag> &Diags,
256                                   std::vector<InputAnnotation> &Annotations,
257                                   unsigned &LabelWidth) {
258   // How many diagnostics has the current check seen so far?
259   unsigned CheckDiagCount = 0;
260   // What's the widest label?
261   LabelWidth = 0;
262   for (auto DiagItr = Diags.begin(), DiagEnd = Diags.end(); DiagItr != DiagEnd;
263        ++DiagItr) {
264     InputAnnotation A;
265 
266     // Build label, which uniquely identifies this check result.
267     A.CheckLine = DiagItr->CheckLine;
268     llvm::raw_string_ostream Label(A.Label);
269     Label << GetCheckTypeAbbreviation(DiagItr->CheckTy) << ":"
270           << DiagItr->CheckLine;
271     A.CheckDiagIndex = UINT_MAX;
272     auto DiagNext = std::next(DiagItr);
273     if (DiagNext != DiagEnd && DiagItr->CheckTy == DiagNext->CheckTy &&
274         DiagItr->CheckLine == DiagNext->CheckLine)
275       A.CheckDiagIndex = CheckDiagCount++;
276     else if (CheckDiagCount) {
277       A.CheckDiagIndex = CheckDiagCount;
278       CheckDiagCount = 0;
279     }
280     if (A.CheckDiagIndex != UINT_MAX)
281       Label << "'" << A.CheckDiagIndex;
282     else
283       A.CheckDiagIndex = 0;
284     Label.flush();
285     LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size());
286 
287     MarkerStyle Marker = GetMarker(DiagItr->MatchTy);
288     A.Marker = Marker;
289 
290     // Compute the mark location, and break annotation into multiple
291     // annotations if it spans multiple lines.
292     A.InputLine = DiagItr->InputStartLine;
293     A.InputStartCol = DiagItr->InputStartCol;
294     if (DiagItr->InputStartLine == DiagItr->InputEndLine) {
295       // Sometimes ranges are empty in order to indicate a specific point, but
296       // that would mean nothing would be marked, so adjust the range to
297       // include the following character.
298       A.InputEndCol =
299           std::max(DiagItr->InputStartCol + 1, DiagItr->InputEndCol);
300       Annotations.push_back(A);
301     } else {
302       assert(DiagItr->InputStartLine < DiagItr->InputEndLine &&
303              "expected input range not to be inverted");
304       A.InputEndCol = UINT_MAX;
305       A.Marker.Note = "";
306       Annotations.push_back(A);
307       for (unsigned L = DiagItr->InputStartLine + 1, E = DiagItr->InputEndLine;
308            L <= E; ++L) {
309         // If a range ends before the first column on a line, then it has no
310         // characters on that line, so there's nothing to render.
311         if (DiagItr->InputEndCol == 1 && L == E) {
312           Annotations.back().Marker.Note = Marker.Note;
313           break;
314         }
315         InputAnnotation B;
316         B.CheckLine = A.CheckLine;
317         B.CheckDiagIndex = A.CheckDiagIndex;
318         B.Label = A.Label;
319         B.InputLine = L;
320         B.Marker = Marker;
321         B.Marker.Lead = '~';
322         B.InputStartCol = 1;
323         if (L != E) {
324           B.InputEndCol = UINT_MAX;
325           B.Marker.Note = "";
326         } else
327           B.InputEndCol = DiagItr->InputEndCol;
328         Annotations.push_back(B);
329       }
330     }
331   }
332 }
333 
334 static void DumpAnnotatedInput(
335     raw_ostream &OS, StringRef InputFileText,
336     std::vector<InputAnnotation> &Annotations, unsigned LabelWidth) {
337   OS << "Full input was:\n<<<<<<\n";
338 
339   // Sort annotations.
340   //
341   // First, sort in the order of input lines to make it easier to find relevant
342   // annotations while iterating input lines in the implementation below.
343   // FileCheck diagnostics are not always reported and recorded in the order of
344   // input lines due to, for example, CHECK-DAG and CHECK-NOT.
345   //
346   // Second, for annotations for the same input line, sort in the order of the
347   // FileCheck directive's line in the check file (where there's at most one
348   // directive per line) and then by the index of the match result for that
349   // directive.  The rationale of this choice is that, for any input line, this
350   // sort establishes a total order of annotations that, with respect to match
351   // results, is consistent across multiple lines, thus making match results
352   // easier to track from one line to the next when they span multiple lines.
353   std::sort(Annotations.begin(), Annotations.end(),
354             [](const InputAnnotation &A, const InputAnnotation &B) {
355               if (A.InputLine != B.InputLine)
356                 return A.InputLine < B.InputLine;
357               if (A.CheckLine != B.CheckLine)
358                 return A.CheckLine < B.CheckLine;
359               assert(A.CheckDiagIndex != B.CheckDiagIndex &&
360                      "expected diagnostic indices to be unique within a "
361                      " check line");
362               return A.CheckDiagIndex < B.CheckDiagIndex;
363             });
364 
365   // Compute the width of the label column.
366   const unsigned char *InputFilePtr = InputFileText.bytes_begin(),
367                       *InputFileEnd = InputFileText.bytes_end();
368   unsigned LineCount = InputFileText.count('\n');
369   if (InputFileEnd[-1] != '\n')
370     ++LineCount;
371   unsigned LineNoWidth = log10(LineCount) + 1;
372   // +3 below adds spaces (1) to the left of the (right-aligned) line numbers
373   // on input lines and (2) to the right of the (left-aligned) labels on
374   // annotation lines so that input lines and annotation lines are more
375   // visually distinct.  For example, the spaces on the annotation lines ensure
376   // that input line numbers and check directive line numbers never align
377   // horizontally.  Those line numbers might not even be for the same file.
378   // One space would be enough to achieve that, but more makes it even easier
379   // to see.
380   LabelWidth = std::max(LabelWidth, LineNoWidth) + 3;
381 
382   // Print annotated input lines.
383   auto AnnotationItr = Annotations.begin(), AnnotationEnd = Annotations.end();
384   for (unsigned Line = 1;
385        InputFilePtr != InputFileEnd || AnnotationItr != AnnotationEnd;
386        ++Line) {
387     const unsigned char *InputFileLine = InputFilePtr;
388 
389     // Print right-aligned line number.
390     WithColor(OS, raw_ostream::BLACK, true)
391         << format_decimal(Line, LabelWidth) << ": ";
392 
393     // Print numbered line.
394     bool Newline = false;
395     while (InputFilePtr != InputFileEnd && !Newline) {
396       if (*InputFilePtr == '\n')
397         Newline = true;
398       else
399         OS << *InputFilePtr;
400       ++InputFilePtr;
401     }
402     OS << '\n';
403     unsigned InputLineWidth = InputFilePtr - InputFileLine - Newline;
404 
405     // Print any annotations.
406     while (AnnotationItr != AnnotationEnd &&
407            AnnotationItr->InputLine == Line) {
408       WithColor COS(OS, AnnotationItr->Marker.Color, true);
409       // The two spaces below are where the ": " appears on input lines.
410       COS << left_justify(AnnotationItr->Label, LabelWidth) << "  ";
411       unsigned Col;
412       for (Col = 1; Col < AnnotationItr->InputStartCol; ++Col)
413         COS << ' ';
414       COS << AnnotationItr->Marker.Lead;
415       // If InputEndCol=UINT_MAX, stop at InputLineWidth.
416       for (++Col; Col < AnnotationItr->InputEndCol && Col <= InputLineWidth;
417            ++Col)
418         COS << '~';
419       const std::string &Note = AnnotationItr->Marker.Note;
420       if (!Note.empty()) {
421         // Put the note at the end of the input line.  If we were to instead
422         // put the note right after the marker, subsequent annotations for the
423         // same input line might appear to mark this note instead of the input
424         // line.
425         for (; Col <= InputLineWidth; ++Col)
426           COS << ' ';
427         COS << ' ' << Note;
428       }
429       COS << '\n';
430       ++AnnotationItr;
431     }
432   }
433 
434   OS << ">>>>>>\n";
435 }
436 
437 int main(int argc, char **argv) {
438   // Enable use of ANSI color codes because FileCheck is using them to
439   // highlight text.
440   llvm::sys::Process::UseANSIEscapeCodes(true);
441 
442   InitLLVM X(argc, argv);
443   cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr,
444                               "FILECHECK_OPTS");
445   if (DumpInput == DumpInputHelp) {
446     DumpInputAnnotationHelp(outs());
447     return 0;
448   }
449   if (CheckFilename.empty()) {
450     errs() << "<check-file> not specified\n";
451     return 2;
452   }
453 
454   FileCheckRequest Req;
455   for (auto Prefix : CheckPrefixes)
456     Req.CheckPrefixes.push_back(Prefix);
457 
458   for (auto CheckNot : ImplicitCheckNot)
459     Req.ImplicitCheckNot.push_back(CheckNot);
460 
461   for (auto G : GlobalDefines)
462     Req.GlobalDefines.push_back(G);
463 
464   Req.AllowEmptyInput = AllowEmptyInput;
465   Req.EnableVarScope = EnableVarScope;
466   Req.AllowDeprecatedDagOverlap = AllowDeprecatedDagOverlap;
467   Req.Verbose = Verbose;
468   Req.VerboseVerbose = VerboseVerbose;
469   Req.NoCanonicalizeWhiteSpace = NoCanonicalizeWhiteSpace;
470   Req.MatchFullLines = MatchFullLines;
471 
472   if (VerboseVerbose)
473     Req.Verbose = true;
474 
475   FileCheck FC(Req);
476   if (!FC.ValidateCheckPrefixes()) {
477     errs() << "Supplied check-prefix is invalid! Prefixes must be unique and "
478               "start with a letter and contain only alphanumeric characters, "
479               "hyphens and underscores\n";
480     return 2;
481   }
482 
483   Regex PrefixRE = FC.buildCheckPrefixRegex();
484   std::string REError;
485   if (!PrefixRE.isValid(REError)) {
486     errs() << "Unable to combine check-prefix strings into a prefix regular "
487               "expression! This is likely a bug in FileCheck's verification of "
488               "the check-prefix strings. Regular expression parsing failed "
489               "with the following error: "
490            << REError << "\n";
491     return 2;
492   }
493 
494   SourceMgr SM;
495 
496   // Read the expected strings from the check file.
497   ErrorOr<std::unique_ptr<MemoryBuffer>> CheckFileOrErr =
498       MemoryBuffer::getFileOrSTDIN(CheckFilename);
499   if (std::error_code EC = CheckFileOrErr.getError()) {
500     errs() << "Could not open check file '" << CheckFilename
501            << "': " << EC.message() << '\n';
502     return 2;
503   }
504   MemoryBuffer &CheckFile = *CheckFileOrErr.get();
505 
506   SmallString<4096> CheckFileBuffer;
507   StringRef CheckFileText = FC.CanonicalizeFile(CheckFile, CheckFileBuffer);
508 
509   SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer(
510                             CheckFileText, CheckFile.getBufferIdentifier()),
511                         SMLoc());
512 
513   std::vector<FileCheckString> CheckStrings;
514   if (FC.ReadCheckFile(SM, CheckFileText, PrefixRE, CheckStrings))
515     return 2;
516 
517   // Open the file to check and add it to SourceMgr.
518   ErrorOr<std::unique_ptr<MemoryBuffer>> InputFileOrErr =
519       MemoryBuffer::getFileOrSTDIN(InputFilename);
520   if (std::error_code EC = InputFileOrErr.getError()) {
521     errs() << "Could not open input file '" << InputFilename
522            << "': " << EC.message() << '\n';
523     return 2;
524   }
525   MemoryBuffer &InputFile = *InputFileOrErr.get();
526 
527   if (InputFile.getBufferSize() == 0 && !AllowEmptyInput) {
528     errs() << "FileCheck error: '" << InputFilename << "' is empty.\n";
529     DumpCommandLine(argc, argv);
530     return 2;
531   }
532 
533   SmallString<4096> InputFileBuffer;
534   StringRef InputFileText = FC.CanonicalizeFile(InputFile, InputFileBuffer);
535 
536   SM.AddNewSourceBuffer(MemoryBuffer::getMemBuffer(
537                             InputFileText, InputFile.getBufferIdentifier()),
538                         SMLoc());
539 
540   if (DumpInput == DumpInputDefault)
541     DumpInput = DumpInputOnFailure ? DumpInputFail : DumpInputNever;
542 
543   std::vector<FileCheckDiag> Diags;
544   int ExitCode = FC.CheckInput(SM, InputFileText, CheckStrings,
545                                DumpInput == DumpInputNever ? nullptr : &Diags)
546                      ? EXIT_SUCCESS
547                      : 1;
548   if (DumpInput == DumpInputAlways ||
549       (ExitCode == 1 && DumpInput == DumpInputFail)) {
550     errs() << "\n"
551            << "Input file: "
552            << (InputFilename == "-" ? "<stdin>" : InputFilename.getValue())
553            << "\n"
554            << "Check file: " << CheckFilename << "\n"
555            << "\n"
556            << "-dump-input=help describes the format of the following dump.\n"
557            << "\n";
558     std::vector<InputAnnotation> Annotations;
559     unsigned LabelWidth;
560     BuildInputAnnotations(Diags, Annotations, LabelWidth);
561     DumpAnnotatedInput(errs(), InputFileText, Annotations, LabelWidth);
562   }
563 
564   return ExitCode;
565 }
566