1//===- FileCheck.cpp - Check that File's Contents match what is expected --===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// FileCheck does a line-by line check of a file that validates whether it
10// contains the expected content. This is useful for regression tests etc.
11//
12// This program exits with an exit status of 2 on error, exit status of 0 if
13// the file matched the expected contents, and exit status of 1 if it did not
14// contain the expected contents.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/FileCheck/FileCheck.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/InitLLVM.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/Process.h"
23#include "llvm/Support/SourceMgr.h"
24#include "llvm/Support/WithColor.h"
25#include "llvm/Support/raw_ostream.h"
26#include <cmath>
27#include <map>
28using namespace llvm;
29
30static cl::extrahelp FileCheckOptsEnv(
31 "\nOptions are parsed from the environment variable FILECHECK_OPTS and\n"
32 "from the command line.\n");
33
34static cl::opt<std::string>
35 CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Optional);
36
37static cl::opt<std::string>
38 InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
39 cl::init(Val: "-"), cl::value_desc("filename"));
40
41static cl::list<std::string>
42 CheckPrefixes("check-prefixes", cl::CommaSeparated,
43 cl::desc("Comma separated list of prefixes to use from check "
44 "file\n(defaults to 'CHECK')"));
45static cl::alias CheckPrefixesAlias("check-prefix", cl::aliasopt(CheckPrefixes),
46 cl::CommaSeparated, cl::NotHidden,
47 cl::desc("Alias for -check-prefixes"));
48
49static cl::list<std::string> CommentPrefixes(
50 "comment-prefixes", cl::CommaSeparated, cl::Hidden,
51 cl::desc("Comma-separated list of comment prefixes to use from check file\n"
52 "(defaults to 'COM,RUN'). Please avoid using this feature in\n"
53 "LLVM's LIT-based test suites, which should be easier to\n"
54 "maintain if they all follow a consistent comment style. This\n"
55 "feature is meant for non-LIT test suites using FileCheck."));
56
57static cl::opt<bool> NoCanonicalizeWhiteSpace(
58 "strict-whitespace",
59 cl::desc("Do not treat all horizontal whitespace as equivalent"));
60
61static cl::opt<bool> IgnoreCase(
62 "ignore-case",
63 cl::desc("Use case-insensitive matching"));
64
65static cl::list<std::string> ImplicitCheckNot(
66 "implicit-check-not",
67 cl::desc("Add an implicit negative check with this pattern to every\n"
68 "positive check. This can be used to ensure that no instances of\n"
69 "this pattern occur which are not matched by a positive pattern"),
70 cl::value_desc("pattern"));
71
72static cl::list<std::string>
73 GlobalDefines("D", cl::AlwaysPrefix,
74 cl::desc("Define a variable to be used in capture patterns."),
75 cl::value_desc("VAR=VALUE"));
76
77static cl::opt<bool> AllowEmptyInput(
78 "allow-empty", cl::init(Val: false),
79 cl::desc("Allow the input file to be empty. This is useful when making\n"
80 "checks that some error message does not occur, for example."));
81
82static cl::opt<bool> AllowUnusedPrefixes(
83 "allow-unused-prefixes",
84 cl::desc("Allow prefixes to be specified but not appear in the test."));
85
86static cl::opt<bool> MatchFullLines(
87 "match-full-lines", cl::init(Val: false),
88 cl::desc("Require all positive matches to cover an entire input line.\n"
89 "Allows leading and trailing whitespace if --strict-whitespace\n"
90 "is not also passed."));
91
92static cl::opt<bool> EnableVarScope(
93 "enable-var-scope", cl::init(Val: false),
94 cl::desc("Enables scope for regex variables. Variables with names that\n"
95 "do not start with '$' will be reset at the beginning of\n"
96 "each CHECK-LABEL block."));
97
98static cl::opt<bool> AllowDeprecatedDagOverlap(
99 "allow-deprecated-dag-overlap", cl::init(Val: false),
100 cl::desc("Enable overlapping among matches in a group of consecutive\n"
101 "CHECK-DAG directives. This option is deprecated and is only\n"
102 "provided for convenience as old tests are migrated to the new\n"
103 "non-overlapping CHECK-DAG implementation.\n"));
104
105static cl::opt<bool> Verbose(
106 "v",
107 cl::desc("Print directive pattern matches, or add them to the input dump\n"
108 "if enabled.\n"));
109
110static cl::opt<bool> VerboseVerbose(
111 "vv",
112 cl::desc("Print information helpful in diagnosing internal FileCheck\n"
113 "issues, or add it to the input dump if enabled. Implies\n"
114 "-v.\n"));
115
116// The order of DumpInputValue members affects their precedence, as documented
117// for -dump-input below.
118enum DumpInputValue {
119 DumpInputNever,
120 DumpInputFail,
121 DumpInputAlways,
122 DumpInputHelp
123};
124
125static cl::list<DumpInputValue> DumpInputs(
126 "dump-input",
127 cl::desc("Dump input to stderr, adding annotations representing\n"
128 "currently enabled diagnostics. When there are multiple\n"
129 "occurrences of this option, the <value> that appears earliest\n"
130 "in the list below has precedence. The default is 'fail'.\n"),
131 cl::value_desc("mode"),
132 cl::values(clEnumValN(DumpInputHelp, "help", "Explain input dump and quit"),
133 clEnumValN(DumpInputAlways, "always", "Always dump input"),
134 clEnumValN(DumpInputFail, "fail", "Dump input on failure"),
135 clEnumValN(DumpInputNever, "never", "Never dump input")));
136
137// The order of DumpInputFilterValue members affects their precedence, as
138// documented for -dump-input-filter below.
139enum DumpInputFilterValue {
140 DumpInputFilterError,
141 DumpInputFilterAnnotation,
142 DumpInputFilterAnnotationFull,
143 DumpInputFilterAll
144};
145
146static cl::list<DumpInputFilterValue> DumpInputFilters(
147 "dump-input-filter",
148 cl::desc("In the dump requested by -dump-input, print only input lines of\n"
149 "kind <value> plus any context specified by -dump-input-context.\n"
150 "When there are multiple occurrences of this option, the <value>\n"
151 "that appears earliest in the list below has precedence. The\n"
152 "default is 'error' when -dump-input=fail, and it's 'all' when\n"
153 "-dump-input=always.\n"),
154 cl::values(clEnumValN(DumpInputFilterAll, "all", "All input lines"),
155 clEnumValN(DumpInputFilterAnnotationFull, "annotation-full",
156 "Input lines with annotations"),
157 clEnumValN(DumpInputFilterAnnotation, "annotation",
158 "Input lines with starting points of annotations"),
159 clEnumValN(DumpInputFilterError, "error",
160 "Input lines with starting points of error "
161 "annotations")));
162
163static cl::list<unsigned> DumpInputContexts(
164 "dump-input-context", cl::value_desc("N"),
165 cl::desc("In the dump requested by -dump-input, print <N> input lines\n"
166 "before and <N> input lines after any lines specified by\n"
167 "-dump-input-filter. When there are multiple occurrences of\n"
168 "this option, the largest specified <N> has precedence. The\n"
169 "default is 5.\n"));
170
171typedef cl::list<std::string>::const_iterator prefix_iterator;
172
173
174
175
176
177
178
179static void DumpCommandLine(int argc, char **argv) {
180 errs() << "FileCheck command line: ";
181 for (int I = 0; I < argc; I++)
182 errs() << " " << argv[I];
183 errs() << "\n";
184}
185
186struct MarkerStyle {
187 /// The starting char (before tildes) for marking the line.
188 char Lead;
189 /// What color to use for this annotation.
190 raw_ostream::Colors Color;
191 /// A note to follow the marker, or empty string if none.
192 std::string Note;
193 /// Does this marker indicate inclusion by -dump-input-filter=error?
194 bool FiltersAsError;
195 MarkerStyle() = default;
196 MarkerStyle(char Lead, raw_ostream::Colors Color,
197 const std::string &Note = "", bool FiltersAsError = false)
198 : Lead(Lead), Color(Color), Note(Note), FiltersAsError(FiltersAsError) {
199 assert((!FiltersAsError || !Note.empty()) &&
200 "expected error diagnostic to have note");
201 }
202};
203
204static MarkerStyle GetMarker(FileCheckDiag::MatchType MatchTy) {
205 switch (MatchTy) {
206 case FileCheckDiag::MatchFoundAndExpected:
207 return MarkerStyle('^', raw_ostream::GREEN);
208 case FileCheckDiag::MatchFoundButExcluded:
209 return MarkerStyle('!', raw_ostream::RED, "error: no match expected",
210 /*FiltersAsError=*/true);
211 case FileCheckDiag::MatchFoundButWrongLine:
212 return MarkerStyle('!', raw_ostream::RED, "error: match on wrong line",
213 /*FiltersAsError=*/true);
214 case FileCheckDiag::MatchFoundButDiscarded:
215 return MarkerStyle('!', raw_ostream::CYAN,
216 "discard: overlaps earlier match");
217 case FileCheckDiag::MatchFoundErrorNote:
218 // Note should always be overridden within the FileCheckDiag.
219 return MarkerStyle('!', raw_ostream::RED,
220 "error: unknown error after match",
221 /*FiltersAsError=*/true);
222 case FileCheckDiag::MatchNoneAndExcluded:
223 return MarkerStyle('X', raw_ostream::GREEN);
224 case FileCheckDiag::MatchNoneButExpected:
225 return MarkerStyle('X', raw_ostream::RED, "error: no match found",
226 /*FiltersAsError=*/true);
227 case FileCheckDiag::MatchNoneForInvalidPattern:
228 return MarkerStyle('X', raw_ostream::RED,
229 "error: match failed for invalid pattern",
230 /*FiltersAsError=*/true);
231 case FileCheckDiag::MatchFuzzy:
232 return MarkerStyle('?', raw_ostream::MAGENTA, "possible intended match",
233 /*FiltersAsError=*/true);
234 }
235 llvm_unreachable_internal(msg: "unexpected match type");
236}
237
238static void DumpInputAnnotationHelp(raw_ostream &OS) {
239 OS << "The following description was requested by -dump-input=help to\n"
240 << "explain the input dump printed by FileCheck.\n"
241 << "\n"
242 << "Related command-line options:\n"
243 << "\n"
244 << " - -dump-input=<value> enables or disables the input dump\n"
245 << " - -dump-input-filter=<value> filters the input lines\n"
246 << " - -dump-input-context=<N> adjusts the context of filtered lines\n"
247 << " - -v and -vv add more annotations\n"
248 << " - -color forces colors to be enabled both in the dump and below\n"
249 << " - -help documents the above options in more detail\n"
250 << "\n"
251 << "These options can also be set via FILECHECK_OPTS. For example, for\n"
252 << "maximum debugging output on failures:\n"
253 << "\n"
254 << " $ FILECHECK_OPTS='-dump-input-filter=all -vv -color' ninja check\n"
255 << "\n"
256 << "Input dump annotation format:\n"
257 << "\n";
258
259 // Labels for input lines.
260 OS << " - ";
261 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "L:";
262 OS << " labels line number L of the input file\n"
263 << " An extra space is added after each input line to represent"
264 << " the\n"
265 << " newline character\n";
266
267 // Labels for annotation lines.
268 OS << " - ";
269 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L";
270 OS << " labels the only match result for either (1) a pattern of type T"
271 << " from\n"
272 << " line L of the check file if L is an integer or (2) the"
273 << " I-th implicit\n"
274 << " pattern if L is \"imp\" followed by an integer "
275 << "I (index origin one)\n";
276 OS << " - ";
277 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L'N";
278 OS << " labels the Nth match result for such a pattern\n";
279
280 // Markers on annotation lines.
281 OS << " - ";
282 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "^~~";
283 OS << " marks good match (reported if -v)\n"
284 << " - ";
285 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "!~~";
286 OS << " marks bad match, such as:\n"
287 << " - CHECK-NEXT on same line as previous match (error)\n"
288 << " - CHECK-NOT found (error)\n"
289 << " - CHECK-DAG overlapping match (discarded, reported if "
290 << "-vv)\n"
291 << " - ";
292 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "X~~";
293 OS << " marks search range when no match is found, such as:\n"
294 << " - CHECK-NEXT not found (error)\n"
295 << " - CHECK-NOT not found (success, reported if -vv)\n"
296 << " - CHECK-DAG not found after discarded matches (error)\n"
297 << " - ";
298 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "?";
299 OS << " marks fuzzy match when no match is found\n";
300
301 // Elided lines.
302 OS << " - ";
303 WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "...";
304 OS << " indicates elided input lines and annotations, as specified by\n"
305 << " -dump-input-filter and -dump-input-context\n";
306
307 // Colors.
308 OS << " - colors ";
309 WithColor(OS, raw_ostream::GREEN, true) << "success";
310 OS << ", ";
311 WithColor(OS, raw_ostream::RED, true) << "error";
312 OS << ", ";
313 WithColor(OS, raw_ostream::MAGENTA, true) << "fuzzy match";
314 OS << ", ";
315 WithColor(OS, raw_ostream::CYAN, true, false) << "discarded match";
316 OS << ", ";
317 WithColor(OS, raw_ostream::CYAN, true, true) << "unmatched input";
318 OS << "\n";
319}
320
321/// An annotation for a single input line.
322struct InputAnnotation {
323 /// The index of the match result across all checks
324 unsigned DiagIndex;
325 /// The label for this annotation.
326 std::string Label;
327 /// Is this the initial fragment of a diagnostic that has been broken across
328 /// multiple lines?
329 bool IsFirstLine;
330 /// What input line (one-origin indexing) this annotation marks. This might
331 /// be different from the starting line of the original diagnostic if
332 /// !IsFirstLine.
333 unsigned InputLine;
334 /// The column range (one-origin indexing, open end) in which to mark the
335 /// input line. If InputEndCol is UINT_MAX, treat it as the last column
336 /// before the newline.
337 unsigned InputStartCol, InputEndCol;
338 /// The marker to use.
339 MarkerStyle Marker;
340 /// Whether this annotation represents a good match for an expected pattern.
341 bool FoundAndExpectedMatch;
342};
343
344/// Get an abbreviation for the check type.
345static std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) {
346 switch (Ty) {
347 case Check::CheckPlain:
348 if (Ty.getCount() > 1)
349 return "count";
350 return "check";
351 case Check::CheckNext:
352 return "next";
353 case Check::CheckSame:
354 return "same";
355 case Check::CheckNot:
356 return "not";
357 case Check::CheckDAG:
358 return "dag";
359 case Check::CheckLabel:
360 return "label";
361 case Check::CheckEmpty:
362 return "empty";
363 case Check::CheckComment:
364 return "com";
365 case Check::CheckEOF:
366 return "eof";
367 case Check::CheckBadNot:
368 return "bad-not";
369 case Check::CheckBadCount:
370 return "bad-count";
371 case Check::CheckMisspelled:
372 return "misspelled";
373 case Check::CheckNone:
374 llvm_unreachable("invalid FileCheckType");
375 }
376 llvm_unreachable("unknown FileCheckType");
377}
378
379static void
380BuildInputAnnotations(const SourceMgr &SM, unsigned CheckFileBufferID,
381 const std::pair<unsigned, unsigned> &ImpPatBufferIDRange,
382 const std::vector<FileCheckDiag> &Diags,
383 std::vector<InputAnnotation> &Annotations,
384 unsigned &LabelWidth) {
385 struct CompareSMLoc {
386 bool operator()(SMLoc LHS, SMLoc RHS) const {
387 return LHS.getPointer() < RHS.getPointer();
388 }
389 };
390 // How many diagnostics does each pattern have?
391 std::map<SMLoc, unsigned, CompareSMLoc> DiagCountPerPattern;
392 for (const FileCheckDiag &Diag : Diags)
393 ++DiagCountPerPattern[Diag.CheckLoc];
394 // How many diagnostics have we seen so far per pattern?
395 std::map<SMLoc, unsigned, CompareSMLoc> DiagIndexPerPattern;
396 // How many total diagnostics have we seen so far?
397 unsigned DiagIndex = 0;
398 // What's the widest label?
399 LabelWidth = 0;
400 for (auto DiagItr = Diags.begin(), DiagEnd = Diags.end(); DiagItr != DiagEnd;
401 ++DiagItr) {
402 InputAnnotation A;
403 A.DiagIndex = DiagIndex++;
404
405 // Build label, which uniquely identifies this check result.
406 unsigned CheckBufferID = SM.FindBufferContainingLoc(Loc: DiagItr->CheckLoc);
407 auto CheckLineAndCol =
408 SM.getLineAndColumn(Loc: DiagItr->CheckLoc, BufferID: CheckBufferID);
409 llvm::raw_string_ostream Label(A.Label);
410 Label << GetCheckTypeAbbreviation(Ty: DiagItr->CheckTy) << ":";
411 if (CheckBufferID == CheckFileBufferID)
412 Label << CheckLineAndCol.first;
413 else if (ImpPatBufferIDRange.first <= CheckBufferID &&
414 CheckBufferID < ImpPatBufferIDRange.second)
415 Label << "imp" << (CheckBufferID - ImpPatBufferIDRange.first + 1);
416 else
417 llvm_unreachable("expected diagnostic's check location to be either in "
418 "the check file or for an implicit pattern");
419 if (DiagCountPerPattern[DiagItr->CheckLoc] > 1)
420 Label << "'" << DiagIndexPerPattern[DiagItr->CheckLoc]++;
421 LabelWidth = std::max(a: (std::string::size_type)LabelWidth, b: A.Label.size());
422
423 A.Marker = GetMarker(MatchTy: DiagItr->MatchTy);
424 if (!DiagItr->Note.empty()) {
425 A.Marker.Note = DiagItr->Note;
426 // It's less confusing if notes that don't actually have ranges don't have
427 // markers. For example, a marker for 'with "VAR" equal to "5"' would
428 // seem to indicate where "VAR" matches, but the location we actually have
429 // for the marker simply points to the start of the match/search range for
430 // the full pattern of which the substitution is potentially just one
431 // component.
432 if (DiagItr->InputStartLine == DiagItr->InputEndLine &&
433 DiagItr->InputStartCol == DiagItr->InputEndCol)
434 A.Marker.Lead = ' ';
435 }
436 if (DiagItr->MatchTy == FileCheckDiag::MatchFoundErrorNote) {
437 assert(!DiagItr->Note.empty() &&
438 "expected custom note for MatchFoundErrorNote");
439 A.Marker.Note = "error: " + A.Marker.Note;
440 }
441 A.FoundAndExpectedMatch =
442 DiagItr->MatchTy == FileCheckDiag::MatchFoundAndExpected;
443
444 // Compute the mark location, and break annotation into multiple
445 // annotations if it spans multiple lines.
446 A.IsFirstLine = true;
447 A.InputLine = DiagItr->InputStartLine;
448 A.InputStartCol = DiagItr->InputStartCol;
449 if (DiagItr->InputStartLine == DiagItr->InputEndLine) {
450 // Sometimes ranges are empty in order to indicate a specific point, but
451 // that would mean nothing would be marked, so adjust the range to
452 // include the following character.
453 A.InputEndCol =
454 std::max(a: DiagItr->InputStartCol + 1, b: DiagItr->InputEndCol);
455 Annotations.push_back(x: A);
456 } else {
457 assert(DiagItr->InputStartLine < DiagItr->InputEndLine &&
458 "expected input range not to be inverted");
459 A.InputEndCol = UINT_MAX;
460 Annotations.push_back(x: A);
461 for (unsigned L = DiagItr->InputStartLine + 1, E = DiagItr->InputEndLine;
462 L <= E; ++L) {
463 // If a range ends before the first column on a line, then it has no
464 // characters on that line, so there's nothing to render.
465 if (DiagItr->InputEndCol == 1 && L == E)
466 break;
467 InputAnnotation B;
468 B.DiagIndex = A.DiagIndex;
469 B.Label = A.Label;
470 B.IsFirstLine = false;
471 B.InputLine = L;
472 B.Marker = A.Marker;
473 B.Marker.Lead = '~';
474 B.Marker.Note = "";
475 B.InputStartCol = 1;
476 if (L != E)
477 B.InputEndCol = UINT_MAX;
478 else
479 B.InputEndCol = DiagItr->InputEndCol;
480 B.FoundAndExpectedMatch = A.FoundAndExpectedMatch;
481 Annotations.push_back(x: B);
482 }
483 }
484 }
485}
486
487static unsigned FindInputLineInFilter(
488 DumpInputFilterValue DumpInputFilter, unsigned CurInputLine,
489 const std::vector<InputAnnotation>::iterator &AnnotationBeg,
490 const std::vector<InputAnnotation>::iterator &AnnotationEnd) {
491 if (DumpInputFilter == DumpInputFilterAll)
492 return CurInputLine;
493 for (auto AnnotationItr = AnnotationBeg; AnnotationItr != AnnotationEnd;
494 ++AnnotationItr) {
495 switch (DumpInputFilter) {
496 case DumpInputFilterAll:
497 llvm_unreachable("unexpected DumpInputFilterAll");
498 break;
499 case DumpInputFilterAnnotationFull:
500 return AnnotationItr->InputLine;
501 case DumpInputFilterAnnotation:
502 if (AnnotationItr->IsFirstLine)
503 return AnnotationItr->InputLine;
504 break;
505 case DumpInputFilterError:
506 if (AnnotationItr->IsFirstLine && AnnotationItr->Marker.FiltersAsError)
507 return AnnotationItr->InputLine;
508 break;
509 }
510 }
511 return UINT_MAX;
512}
513
514/// To OS, print a vertical ellipsis (right-justified at LabelWidth) if it would
515/// occupy less lines than ElidedLines, but print ElidedLines otherwise. Either
516/// way, clear ElidedLines. Thus, if ElidedLines is empty, do nothing.
517static void DumpEllipsisOrElidedLines(raw_ostream &OS, std::string &ElidedLines,
518 unsigned LabelWidth) {
519 if (ElidedLines.empty())
520 return;
521 unsigned EllipsisLines = 3;
522 if (EllipsisLines < StringRef(ElidedLines).count(C: '\n')) {
523 for (unsigned i = 0; i < EllipsisLines; ++i) {
524 WithColor(OS, raw_ostream::BLACK, /*Bold=*/true)
525 << right_justify(Str: ".", Width: LabelWidth);
526 OS << '\n';
527 }
528 } else
529 OS << ElidedLines;
530 ElidedLines.clear();
531}
532
533static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req,
534 DumpInputFilterValue DumpInputFilter,
535 unsigned DumpInputContext,
536 StringRef InputFileText,
537 std::vector<InputAnnotation> &Annotations,
538 unsigned LabelWidth) {
539 OS << "Input was:\n<<<<<<\n";
540
541 // Sort annotations.
542 llvm::sort(C&: Annotations,
543 Comp: [](const InputAnnotation &A, const InputAnnotation &B) {
544 // 1. Sort annotations in the order of the input lines.
545 //
546 // This makes it easier to find relevant annotations while
547 // iterating input lines in the implementation below. FileCheck
548 // does not always produce diagnostics in the order of input
549 // lines due to, for example, CHECK-DAG and CHECK-NOT.
550 if (A.InputLine != B.InputLine)
551 return A.InputLine < B.InputLine;
552 // 2. Sort annotations in the temporal order FileCheck produced
553 // their associated diagnostics.
554 //
555 // This sort offers several benefits:
556 //
557 // A. On a single input line, the order of annotations reflects
558 // the FileCheck logic for processing directives/patterns.
559 // This can be helpful in understanding cases in which the
560 // order of the associated directives/patterns in the check
561 // file or on the command line either (i) does not match the
562 // temporal order in which FileCheck looks for matches for the
563 // directives/patterns (due to, for example, CHECK-LABEL,
564 // CHECK-NOT, or `--implicit-check-not`) or (ii) does match
565 // that order but does not match the order of those
566 // diagnostics along an input line (due to, for example,
567 // CHECK-DAG).
568 //
569 // On the other hand, because our presentation format presents
570 // input lines in order, there's no clear way to offer the
571 // same benefit across input lines. For consistency, it might
572 // then seem worthwhile to have annotations on a single line
573 // also sorted in input order (that is, by input column).
574 // However, in practice, this appears to be more confusing
575 // than helpful. Perhaps it's intuitive to expect annotations
576 // to be listed in the temporal order in which they were
577 // produced except in cases the presentation format obviously
578 // and inherently cannot support it (that is, across input
579 // lines).
580 //
581 // B. When diagnostics' annotations are split among multiple
582 // input lines, the user must track them from one input line
583 // to the next. One property of the sort chosen here is that
584 // it facilitates the user in this regard by ensuring the
585 // following: when comparing any two input lines, a
586 // diagnostic's annotations are sorted in the same position
587 // relative to all other diagnostics' annotations.
588 return A.DiagIndex < B.DiagIndex;
589 });
590
591 // Compute the width of the label column.
592 const unsigned char *InputFilePtr = InputFileText.bytes_begin(),
593 *InputFileEnd = InputFileText.bytes_end();
594 unsigned LineCount = InputFileText.count(C: '\n');
595 if (InputFileEnd[-1] != '\n')
596 ++LineCount;
597 unsigned LineNoWidth = std::log10(x: LineCount) + 1;
598 // +3 below adds spaces (1) to the left of the (right-aligned) line numbers
599 // on input lines and (2) to the right of the (left-aligned) labels on
600 // annotation lines so that input lines and annotation lines are more
601 // visually distinct. For example, the spaces on the annotation lines ensure
602 // that input line numbers and check directive line numbers never align
603 // horizontally. Those line numbers might not even be for the same file.
604 // One space would be enough to achieve that, but more makes it even easier
605 // to see.
606 LabelWidth = std::max(a: LabelWidth, b: LineNoWidth) + 3;
607
608 // Print annotated input lines.
609 unsigned PrevLineInFilter = 0; // 0 means none so far
610 unsigned NextLineInFilter = 0; // 0 means uncomputed, UINT_MAX means none
611 std::string ElidedLines;
612 raw_string_ostream ElidedLinesOS(ElidedLines);
613 ColorMode TheColorMode =
614 WithColor(OS).colorsEnabled() ? ColorMode::Enable : ColorMode::Disable;
615 if (TheColorMode == ColorMode::Enable)
616 ElidedLinesOS.enable_colors(enable: true);
617 auto AnnotationItr = Annotations.begin(), AnnotationEnd = Annotations.end();
618 for (unsigned Line = 1;
619 InputFilePtr != InputFileEnd || AnnotationItr != AnnotationEnd;
620 ++Line) {
621 const unsigned char *InputFileLine = InputFilePtr;
622
623 // Compute the previous and next line included by the filter.
624 if (NextLineInFilter < Line)
625 NextLineInFilter = FindInputLineInFilter(DumpInputFilter, CurInputLine: Line,
626 AnnotationBeg: AnnotationItr, AnnotationEnd);
627 assert(NextLineInFilter && "expected NextLineInFilter to be computed");
628 if (NextLineInFilter == Line)
629 PrevLineInFilter = Line;
630
631 // Elide this input line and its annotations if it's not within the
632 // context specified by -dump-input-context of an input line included by
633 // -dump-input-filter. However, in case the resulting ellipsis would occupy
634 // more lines than the input lines and annotations it elides, buffer the
635 // elided lines and annotations so we can print them instead.
636 raw_ostream *LineOS;
637 if ((!PrevLineInFilter || PrevLineInFilter + DumpInputContext < Line) &&
638 (NextLineInFilter == UINT_MAX ||
639 Line + DumpInputContext < NextLineInFilter))
640 LineOS = &ElidedLinesOS;
641 else {
642 LineOS = &OS;
643 DumpEllipsisOrElidedLines(OS, ElidedLines, LabelWidth);
644 }
645
646 // Print right-aligned line number.
647 WithColor(*LineOS, raw_ostream::BLACK, /*Bold=*/true, /*BF=*/false,
648 TheColorMode)
649 << format_decimal(N: Line, Width: LabelWidth) << ": ";
650
651 // For the case where -v and colors are enabled, find the annotations for
652 // good matches for expected patterns in order to highlight everything
653 // else in the line. There are no such annotations if -v is disabled.
654 std::vector<InputAnnotation> FoundAndExpectedMatches;
655 if (Req.Verbose && TheColorMode == ColorMode::Enable) {
656 for (auto I = AnnotationItr; I != AnnotationEnd && I->InputLine == Line;
657 ++I) {
658 if (I->FoundAndExpectedMatch)
659 FoundAndExpectedMatches.push_back(x: *I);
660 }
661 }
662
663 // Print numbered line with highlighting where there are no matches for
664 // expected patterns.
665 bool Newline = false;
666 {
667 WithColor COS(*LineOS, raw_ostream::SAVEDCOLOR, /*Bold=*/false,
668 /*BG=*/false, TheColorMode);
669 bool InMatch = false;
670 if (Req.Verbose)
671 COS.changeColor(Color: raw_ostream::CYAN, Bold: true, BG: true);
672 for (unsigned Col = 1; InputFilePtr != InputFileEnd && !Newline; ++Col) {
673 bool WasInMatch = InMatch;
674 InMatch = false;
675 for (const InputAnnotation &M : FoundAndExpectedMatches) {
676 if (M.InputStartCol <= Col && Col < M.InputEndCol) {
677 InMatch = true;
678 break;
679 }
680 }
681 if (!WasInMatch && InMatch)
682 COS.resetColor();
683 else if (WasInMatch && !InMatch)
684 COS.changeColor(Color: raw_ostream::CYAN, Bold: true, BG: true);
685 if (*InputFilePtr == '\n') {
686 Newline = true;
687 COS << ' ';
688 } else
689 COS << *InputFilePtr;
690 ++InputFilePtr;
691 }
692 }
693 *LineOS << '\n';
694 unsigned InputLineWidth = InputFilePtr - InputFileLine;
695
696 // Print any annotations.
697 while (AnnotationItr != AnnotationEnd &&
698 AnnotationItr->InputLine == Line) {
699 WithColor COS(*LineOS, AnnotationItr->Marker.Color, /*Bold=*/true,
700 /*BG=*/false, TheColorMode);
701 // The two spaces below are where the ": " appears on input lines.
702 COS << left_justify(Str: AnnotationItr->Label, Width: LabelWidth) << " ";
703 unsigned Col;
704 for (Col = 1; Col < AnnotationItr->InputStartCol; ++Col)
705 COS << ' ';
706 COS << AnnotationItr->Marker.Lead;
707 // If InputEndCol=UINT_MAX, stop at InputLineWidth.
708 for (++Col; Col < AnnotationItr->InputEndCol && Col <= InputLineWidth;
709 ++Col)
710 COS << '~';
711 const std::string &Note = AnnotationItr->Marker.Note;
712 if (!Note.empty()) {
713 // Put the note at the end of the input line. If we were to instead
714 // put the note right after the marker, subsequent annotations for the
715 // same input line might appear to mark this note instead of the input
716 // line.
717 for (; Col <= InputLineWidth; ++Col)
718 COS << ' ';
719 COS << ' ' << Note;
720 }
721 COS << '\n';
722 ++AnnotationItr;
723 }
724 }
725 DumpEllipsisOrElidedLines(OS, ElidedLines, LabelWidth);
726
727 OS << ">>>>>>\n";
728}
729
730int main(int argc, char **argv) {
731 // Enable use of ANSI color codes because FileCheck is using them to
732 // highlight text.
733 llvm::sys::Process::UseANSIEscapeCodes(enable: true);
734
735 InitLLVM X(argc, argv);
736 cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr,
737 /*VFS*/ nullptr, EnvVar: "FILECHECK_OPTS");
738
739 // Select -dump-input* values. The -help documentation specifies the default
740 // value and which value to choose if an option is specified multiple times.
741 // In the latter case, the general rule of thumb is to choose the value that
742 // provides the most information.
743 DumpInputValue DumpInput =
744 DumpInputs.empty() ? DumpInputFail : *llvm::max_element(Range&: DumpInputs);
745 DumpInputFilterValue DumpInputFilter;
746 if (DumpInputFilters.empty())
747 DumpInputFilter = DumpInput == DumpInputAlways ? DumpInputFilterAll
748 : DumpInputFilterError;
749 else
750 DumpInputFilter = *llvm::max_element(Range&: DumpInputFilters);
751 unsigned DumpInputContext =
752 DumpInputContexts.empty() ? 5 : *llvm::max_element(Range&: DumpInputContexts);
753
754 if (DumpInput == DumpInputHelp) {
755 DumpInputAnnotationHelp(OS&: outs());
756 return 0;
757 }
758 if (CheckFilename.empty()) {
759 errs() << "<check-file> not specified\n";
760 return 2;
761 }
762
763 FileCheckRequest Req;
764 append_range(C&: Req.CheckPrefixes, R&: CheckPrefixes);
765
766 append_range(C&: Req.CommentPrefixes, R&: CommentPrefixes);
767
768 append_range(C&: Req.ImplicitCheckNot, R&: ImplicitCheckNot);
769
770 bool GlobalDefineError = false;
771 for (StringRef G : GlobalDefines) {
772 size_t EqIdx = G.find(C: '=');
773 if (EqIdx == std::string::npos) {
774 errs() << "Missing equal sign in command-line definition '-D" << G
775 << "'\n";
776 GlobalDefineError = true;
777 continue;
778 }
779 if (EqIdx == 0) {
780 errs() << "Missing variable name in command-line definition '-D" << G
781 << "'\n";
782 GlobalDefineError = true;
783 continue;
784 }
785 Req.GlobalDefines.push_back(x: G);
786 }
787 if (GlobalDefineError)
788 return 2;
789
790 Req.AllowEmptyInput = AllowEmptyInput;
791 Req.AllowUnusedPrefixes = AllowUnusedPrefixes;
792 Req.EnableVarScope = EnableVarScope;
793 Req.AllowDeprecatedDagOverlap = AllowDeprecatedDagOverlap;
794 Req.Verbose = Verbose;
795 Req.VerboseVerbose = VerboseVerbose;
796 Req.NoCanonicalizeWhiteSpace = NoCanonicalizeWhiteSpace;
797 Req.MatchFullLines = MatchFullLines;
798 Req.IgnoreCase = IgnoreCase;
799
800 if (VerboseVerbose)
801 Req.Verbose = true;
802
803 FileCheck FC(Req);
804 if (!FC.ValidateCheckPrefixes())
805 return 2;
806
807 SourceMgr SM;
808
809 // Read the expected strings from the check file.
810 ErrorOr<std::unique_ptr<MemoryBuffer>> CheckFileOrErr =
811 MemoryBuffer::getFileOrSTDIN(Filename: CheckFilename, /*IsText=*/true);
812 if (std::error_code EC = CheckFileOrErr.getError()) {
813 errs() << "Could not open check file '" << CheckFilename
814 << "': " << EC.message() << '\n';
815 return 2;
816 }
817 MemoryBuffer &CheckFile = *CheckFileOrErr.get();
818
819 SmallString<4096> CheckFileBuffer;
820 StringRef CheckFileText = FC.CanonicalizeFile(MB&: CheckFile, OutputBuffer&: CheckFileBuffer);
821
822 unsigned CheckFileBufferID =
823 SM.AddNewSourceBuffer(F: MemoryBuffer::getMemBuffer(
824 InputData: CheckFileText, BufferName: CheckFile.getBufferIdentifier()),
825 IncludeLoc: SMLoc());
826
827 std::pair<unsigned, unsigned> ImpPatBufferIDRange;
828 if (FC.readCheckFile(SM, Buffer: CheckFileText, ImpPatBufferIDRange: &ImpPatBufferIDRange))
829 return 2;
830
831 // Open the file to check and add it to SourceMgr.
832 ErrorOr<std::unique_ptr<MemoryBuffer>> InputFileOrErr =
833 MemoryBuffer::getFileOrSTDIN(Filename: InputFilename, /*IsText=*/true);
834 if (InputFilename == "-")
835 InputFilename = "<stdin>"; // Overwrite for improved diagnostic messages
836 if (std::error_code EC = InputFileOrErr.getError()) {
837 errs() << "Could not open input file '" << InputFilename
838 << "': " << EC.message() << '\n';
839 return 2;
840 }
841 MemoryBuffer &InputFile = *InputFileOrErr.get();
842
843 if (InputFile.getBufferSize() == 0 && !AllowEmptyInput) {
844 errs() << "FileCheck error: '" << InputFilename << "' is empty.\n";
845 DumpCommandLine(argc, argv);
846 return 2;
847 }
848
849 SmallString<4096> InputFileBuffer;
850 StringRef InputFileText = FC.CanonicalizeFile(MB&: InputFile, OutputBuffer&: InputFileBuffer);
851
852 SM.AddNewSourceBuffer(F: MemoryBuffer::getMemBuffer(
853 InputData: InputFileText, BufferName: InputFile.getBufferIdentifier()),
854 IncludeLoc: SMLoc());
855
856 std::vector<FileCheckDiag> Diags;
857 int ExitCode = FC.checkInput(SM, Buffer: InputFileText,
858 Diags: DumpInput == DumpInputNever ? nullptr : &Diags)
859 ? EXIT_SUCCESS
860 : 1;
861 if (DumpInput == DumpInputAlways ||
862 (ExitCode == 1 && DumpInput == DumpInputFail)) {
863 errs() << "\n"
864 << "Input file: " << InputFilename << "\n"
865 << "Check file: " << CheckFilename << "\n"
866 << "\n"
867 << "-dump-input=help explains the following input dump.\n"
868 << "\n";
869 std::vector<InputAnnotation> Annotations;
870 unsigned LabelWidth;
871 BuildInputAnnotations(SM, CheckFileBufferID, ImpPatBufferIDRange, Diags,
872 Annotations, LabelWidth);
873 DumpAnnotatedInput(OS&: errs(), Req, DumpInputFilter, DumpInputContext,
874 InputFileText, Annotations, LabelWidth);
875 }
876
877 return ExitCode;
878}
879