summaryrefslogtreecommitdiffstats
path: root/llvm/utils/FileCheck
diff options
context:
space:
mode:
authorJoel E. Denny <jdenny.ornl@gmail.com>2018-12-18 00:01:39 +0000
committerJoel E. Denny <jdenny.ornl@gmail.com>2018-12-18 00:01:39 +0000
commit3c5d267eb728556b8f25a145d3b006f9f5356589 (patch)
treefc464ad8572cec841994fabd0eca79e70b59cad0 /llvm/utils/FileCheck
parentf47c734e49da195cd5c0435119ed2a86cdfdb5b3 (diff)
downloadbcm5719-llvm-3c5d267eb728556b8f25a145d3b006f9f5356589.tar.gz
bcm5719-llvm-3c5d267eb728556b8f25a145d3b006f9f5356589.zip
[FileCheck] Annotate input dump (1/7)
Extend FileCheck to dump its input annotated with FileCheck's diagnostics: errors, good matches if -v, and additional information if -vv. The goal is to make it easier to visualize FileCheck's matching behavior when debugging. Each patch in this series implements input annotations for a particular category of FileCheck diagnostics. While the first few patches alone are somewhat useful, the annotations become much more useful as later patches implement annotations for -v and -vv diagnostics, which show the matching behavior leading up to the error. This first patch implements boilerplate plus input annotations for error diagnostics reporting that no matches were found for a directive. These annotations mark the search ranges of the failed directives. Instead of using the usual `^~~`, which is used by later patches for good matches, these annotations use `X~~` so that this category of errors is visually distinct. For example: ``` $ FileCheck -dump-input=help The following description was requested by -dump-input=help to explain the input annotations printed by -dump-input=always and -dump-input=fail: - L: labels line number L of the input file - T:L labels the match result for a pattern of type T from line L of the check file - X~~ marks search range when no match is found - colors error If you are not seeing color above or in input dumps, try: -color $ FileCheck -v -dump-input=always check1 < input1 |& sed -n '/^Input file/,$p' Input file: <stdin> Check file: check1 -dump-input=help describes the format of the following dump. Full input was: <<<<<< 1: ; abc def 2: ; ghI jkl next:3 X~~~~~~~~ error: no match found >>>>>> $ cat check1 CHECK: abc CHECK-SAME: def CHECK-NEXT: ghi CHECK-SAME: jkl $ cat input1 ; abc def ; ghI jkl ``` Some additional details related to the boilerplate: * Enabling: The annotated input dump is enabled by `-dump-input`, which can also be set via the `FILECHECK_OPTS` environment variable. Accepted values are `help`, `always`, `fail`, or `never`. As shown above, `help` describes the format of the dump. `always` is helpful when you want to investigate a successful FileCheck run, perhaps for an unexpected pass. `-dump-input-on-failure` and `FILECHECK_DUMP_INPUT_ON_FAILURE` remain as a deprecated alias for `-dump-input=fail`. * Diagnostics: The usual diagnostics are not suppressed in this mode and are printed first. For brevity in the example above, I've omitted them using a sed command. Sometimes they're perfectly sufficient, and then they make debugging quicker than if you were forced to hunt through a dump of long input looking for the error. If you think they'll get in the way sometimes, keep in mind that it's pretty easy to grep for the start of the input dump, which is `<<<`. * Colored Annotations: The annotated input is colored if colors are enabled (enabling colors can be forced using -color). For example, errors are red. However, as in the above example, colors are not vital to reading the annotations. I don't know how to test color in the output, so any hints here would be appreciated. Reviewed By: george.karpenkov, zturner, probinson Differential Revision: https://reviews.llvm.org/D52999 llvm-svn: 349418
Diffstat (limited to 'llvm/utils/FileCheck')
-rw-r--r--llvm/utils/FileCheck/FileCheck.cpp324
1 files changed, 317 insertions, 7 deletions
diff --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index 967d22f12b6..33449147fcc 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/utils/FileCheck/FileCheck.cpp
@@ -19,12 +19,13 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/Process.h"
+#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/FileCheck.h"
using namespace llvm;
static cl::opt<std::string>
- CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Required);
+ CheckFilename(cl::Positional, cl::desc("<check-file>"), cl::Optional);
static cl::opt<std::string>
InputFilename("input-file", cl::desc("File to check (defaults to stdin)"),
@@ -91,7 +92,27 @@ static cl::opt<bool> DumpInputOnFailure(
"dump-input-on-failure", cl::init(std::getenv(DumpInputEnv)),
cl::desc("Dump original input to stderr before failing.\n"
"The value can be also controlled using\n"
- "FILECHECK_DUMP_INPUT_ON_FAILURE environment variable.\n"));
+ "FILECHECK_DUMP_INPUT_ON_FAILURE environment variable.\n"
+ "This option is deprecated in favor of -dump-input=fail.\n"));
+
+enum DumpInputValue {
+ DumpInputDefault,
+ DumpInputHelp,
+ DumpInputNever,
+ DumpInputFail,
+ DumpInputAlways
+};
+
+static cl::opt<DumpInputValue> DumpInput(
+ "dump-input", cl::init(DumpInputDefault),
+ cl::desc("Dump input to stderr, adding annotations representing\n"
+ " currently enabled diagnostics\n"),
+ cl::value_desc("mode"),
+ cl::values(clEnumValN(DumpInputHelp, "help",
+ "Explain dump format and quit"),
+ clEnumValN(DumpInputNever, "never", "Never dump input"),
+ clEnumValN(DumpInputFail, "fail", "Dump input on failure"),
+ clEnumValN(DumpInputAlways, "always", "Always dump input")));
typedef cl::list<std::string>::const_iterator prefix_iterator;
@@ -108,6 +129,269 @@ static void DumpCommandLine(int argc, char **argv) {
errs() << "\n";
}
+struct MarkerStyle {
+ /// The starting char (before tildes) for marking the line.
+ char Lead;
+ /// What color to use for this annotation.
+ raw_ostream::Colors Color;
+ /// A note to follow the marker, or empty string if none.
+ std::string Note;
+ MarkerStyle() {}
+ MarkerStyle(char Lead, raw_ostream::Colors Color, const std::string &Note)
+ : Lead(Lead), Color(Color), Note(Note) {}
+};
+
+static MarkerStyle GetMarker(FileCheckDiag::MatchType MatchTy) {
+ switch (MatchTy) {
+ case FileCheckDiag::MatchNoneButExpected:
+ return MarkerStyle('X', raw_ostream::RED, "error: no match found");
+ case FileCheckDiag::MatchTypeCount:
+ llvm_unreachable_internal("unexpected match type");
+ }
+ llvm_unreachable_internal("unexpected match type");
+}
+
+static void DumpInputAnnotationHelp(raw_ostream &OS) {
+ OS << "The following description was requested by -dump-input=help to\n"
+ << "explain the input annotations printed by -dump-input=always and\n"
+ << "-dump-input=fail:\n\n";
+
+ // Labels for input lines.
+ OS << " - ";
+ WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "L:";
+ OS << " labels line number L of the input file\n";
+
+ // Labels for annotation lines.
+ OS << " - ";
+ WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "T:L";
+ OS << " labels the match result for a pattern of type T from "
+ << "line L of\n"
+ << " the check file\n";
+
+ // Markers on annotation lines.
+ OS << " - ";
+ WithColor(OS, raw_ostream::SAVEDCOLOR, true) << "X~~";
+ OS << " marks search range when no match is found\n";
+
+ // Colors.
+ OS << " - colors ";
+ WithColor(OS, raw_ostream::RED, true) << "error";
+ OS << "\n\n"
+ << "If you are not seeing color above or in input dumps, try: -color\n";
+}
+
+/// An annotation for a single input line.
+struct InputAnnotation {
+ /// The check file line (one-origin indexing) where the directive that
+ /// produced this annotation is located.
+ unsigned CheckLine;
+ /// The label for this annotation.
+ std::string Label;
+ /// What input line (one-origin indexing) this annotation marks. This might
+ /// be different from the starting line of the original diagnostic if this is
+ /// a non-initial fragment of a diagnostic that has been broken across
+ /// multiple lines.
+ unsigned InputLine;
+ /// The column range (one-origin indexing, open end) in which to to mark the
+ /// input line. If InputEndCol is UINT_MAX, treat it as the last column
+ /// before the newline.
+ unsigned InputStartCol, InputEndCol;
+ /// The marker to use.
+ MarkerStyle Marker;
+};
+
+/// Get an abbreviation for the check type.
+std::string GetCheckTypeAbbreviation(Check::FileCheckType Ty) {
+ switch (Ty) {
+ case Check::CheckPlain:
+ if (Ty.getCount() > 1)
+ return "count";
+ return "check";
+ case Check::CheckNext:
+ return "next";
+ case Check::CheckSame:
+ return "same";
+ case Check::CheckNot:
+ return "not";
+ case Check::CheckDAG:
+ return "dag";
+ case Check::CheckLabel:
+ return "label";
+ case Check::CheckEmpty:
+ return "empty";
+ case Check::CheckEOF:
+ return "eof";
+ case Check::CheckBadNot:
+ return "bad-not";
+ case Check::CheckBadCount:
+ return "bad-count";
+ case Check::CheckNone:
+ llvm_unreachable("invalid FileCheckType");
+ }
+ llvm_unreachable("unknown FileCheckType");
+}
+
+static void BuildInputAnnotations(const std::vector<FileCheckDiag> &Diags,
+ std::vector<InputAnnotation> &Annotations,
+ unsigned &LabelWidth) {
+ // What's the widest label?
+ LabelWidth = 0;
+ for (auto DiagItr = Diags.begin(), DiagEnd = Diags.end(); DiagItr != DiagEnd;
+ ++DiagItr) {
+ InputAnnotation A;
+
+ // Build label, which uniquely identifies this check result.
+ A.CheckLine = DiagItr->CheckLine;
+ llvm::raw_string_ostream Label(A.Label);
+ Label << GetCheckTypeAbbreviation(DiagItr->CheckTy) << ":"
+ << DiagItr->CheckLine;
+ Label.flush();
+ LabelWidth = std::max((std::string::size_type)LabelWidth, A.Label.size());
+
+ MarkerStyle Marker = GetMarker(DiagItr->MatchTy);
+ A.Marker = Marker;
+
+ // Compute the mark location, and break annotation into multiple
+ // annotations if it spans multiple lines.
+ A.InputLine = DiagItr->InputStartLine;
+ A.InputStartCol = DiagItr->InputStartCol;
+ if (DiagItr->InputStartLine == DiagItr->InputEndLine) {
+ // Sometimes ranges are empty in order to indicate a specific point, but
+ // that would mean nothing would be marked, so adjust the range to
+ // include the following character.
+ A.InputEndCol =
+ std::max(DiagItr->InputStartCol + 1, DiagItr->InputEndCol);
+ Annotations.push_back(A);
+ } else {
+ assert(DiagItr->InputStartLine < DiagItr->InputEndLine &&
+ "expected input range not to be inverted");
+ A.InputEndCol = UINT_MAX;
+ A.Marker.Note = "";
+ Annotations.push_back(A);
+ for (unsigned L = DiagItr->InputStartLine + 1, E = DiagItr->InputEndLine;
+ L <= E; ++L) {
+ // If a range ends before the first column on a line, then it has no
+ // characters on that line, so there's nothing to render.
+ if (DiagItr->InputEndCol == 1 && L == E) {
+ Annotations.back().Marker.Note = Marker.Note;
+ break;
+ }
+ InputAnnotation B;
+ B.CheckLine = A.CheckLine;
+ B.Label = A.Label;
+ B.InputLine = L;
+ B.Marker = Marker;
+ B.Marker.Lead = '~';
+ B.InputStartCol = 1;
+ if (L != E) {
+ B.InputEndCol = UINT_MAX;
+ B.Marker.Note = "";
+ } else
+ B.InputEndCol = DiagItr->InputEndCol;
+ Annotations.push_back(B);
+ }
+ }
+ }
+}
+
+static void DumpAnnotatedInput(
+ raw_ostream &OS, StringRef InputFileText,
+ std::vector<InputAnnotation> &Annotations, unsigned LabelWidth) {
+ OS << "Full input was:\n<<<<<<\n";
+
+ // Sort annotations.
+ //
+ // First, sort in the order of input lines to make it easier to find relevant
+ // annotations while iterating input lines in the implementation below.
+ // FileCheck diagnostics are not always reported and recorded in the order of
+ // input lines due to, for example, CHECK-DAG and CHECK-NOT.
+ //
+ // Second, for annotations for the same input line, sort in the order of the
+ // FileCheck directive's line in the check file (where there's at most one
+ // directive per line). The rationale of this choice is that, for any input
+ // line, this sort establishes a total order of annotations that, with
+ // respect to match results, is consistent across multiple lines, thus
+ // making match results easier to track from one line to the next when they
+ // span multiple lines.
+ std::sort(Annotations.begin(), Annotations.end(),
+ [](const InputAnnotation &A, const InputAnnotation &B) {
+ if (A.InputLine != B.InputLine)
+ return A.InputLine < B.InputLine;
+ return A.CheckLine < B.CheckLine;
+ });
+
+ // Compute the width of the label column.
+ const unsigned char *InputFilePtr = InputFileText.bytes_begin(),
+ *InputFileEnd = InputFileText.bytes_end();
+ unsigned LineCount = InputFileText.count('\n');
+ if (InputFileEnd[-1] != '\n')
+ ++LineCount;
+ unsigned LineNoWidth = log10(LineCount) + 1;
+ // +3 below adds spaces (1) to the left of the (right-aligned) line numbers
+ // on input lines and (2) to the right of the (left-aligned) labels on
+ // annotation lines so that input lines and annotation lines are more
+ // visually distinct. For example, the spaces on the annotation lines ensure
+ // that input line numbers and check directive line numbers never align
+ // horizontally. Those line numbers might not even be for the same file.
+ // One space would be enough to achieve that, but more makes it even easier
+ // to see.
+ LabelWidth = std::max(LabelWidth, LineNoWidth) + 3;
+
+ // Print annotated input lines.
+ auto AnnotationItr = Annotations.begin(), AnnotationEnd = Annotations.end();
+ for (unsigned Line = 1;
+ InputFilePtr != InputFileEnd || AnnotationItr != AnnotationEnd;
+ ++Line) {
+ const unsigned char *InputFileLine = InputFilePtr;
+
+ // Print right-aligned line number.
+ WithColor(OS, raw_ostream::BLACK, true)
+ << format_decimal(Line, LabelWidth) << ": ";
+
+ // Print numbered line.
+ bool Newline = false;
+ while (InputFilePtr != InputFileEnd && !Newline) {
+ if (*InputFilePtr == '\n')
+ Newline = true;
+ else
+ OS << *InputFilePtr;
+ ++InputFilePtr;
+ }
+ OS << '\n';
+ unsigned InputLineWidth = InputFilePtr - InputFileLine - Newline;
+
+ // Print any annotations.
+ while (AnnotationItr != AnnotationEnd &&
+ AnnotationItr->InputLine == Line) {
+ WithColor COS(OS, AnnotationItr->Marker.Color, true);
+ // The two spaces below are where the ": " appears on input lines.
+ COS << left_justify(AnnotationItr->Label, LabelWidth) << " ";
+ unsigned Col;
+ for (Col = 1; Col < AnnotationItr->InputStartCol; ++Col)
+ COS << ' ';
+ COS << AnnotationItr->Marker.Lead;
+ // If InputEndCol=UINT_MAX, stop at InputLineWidth.
+ for (++Col; Col < AnnotationItr->InputEndCol && Col <= InputLineWidth;
+ ++Col)
+ COS << '~';
+ const std::string &Note = AnnotationItr->Marker.Note;
+ if (!Note.empty()) {
+ // Put the note at the end of the input line. If we were to instead
+ // put the note right after the marker, subsequent annotations for the
+ // same input line might appear to mark this note instead of the input
+ // line.
+ for (; Col <= InputLineWidth; ++Col)
+ COS << ' ';
+ COS << ' ' << Note;
+ }
+ COS << '\n';
+ ++AnnotationItr;
+ }
+ }
+
+ OS << ">>>>>>\n";
+}
+
int main(int argc, char **argv) {
// Enable use of ANSI color codes because FileCheck is using them to
// highlight text.
@@ -116,6 +400,14 @@ int main(int argc, char **argv) {
InitLLVM X(argc, argv);
cl::ParseCommandLineOptions(argc, argv, /*Overview*/ "", /*Errs*/ nullptr,
"FILECHECK_OPTS");
+ if (DumpInput == DumpInputHelp) {
+ DumpInputAnnotationHelp(outs());
+ return 0;
+ }
+ if (CheckFilename.empty()) {
+ errs() << "<check-file> not specified\n";
+ return 2;
+ }
FileCheckRequest Req;
for (auto Prefix : CheckPrefixes)
@@ -157,7 +449,6 @@ int main(int argc, char **argv) {
return 2;
}
-
SourceMgr SM;
// Read the expected strings from the check file.
@@ -204,10 +495,29 @@ int main(int argc, char **argv) {
InputFileText, InputFile.getBufferIdentifier()),
SMLoc());
- int ExitCode =
- FC.CheckInput(SM, InputFileText, CheckStrings) ? EXIT_SUCCESS : 1;
- if (ExitCode == 1 && DumpInputOnFailure)
- errs() << "Full input was:\n<<<<<<\n" << InputFileText << "\n>>>>>>\n";
+ if (DumpInput == DumpInputDefault)
+ DumpInput = DumpInputOnFailure ? DumpInputFail : DumpInputNever;
+
+ std::vector<FileCheckDiag> Diags;
+ int ExitCode = FC.CheckInput(SM, InputFileText, CheckStrings,
+ DumpInput == DumpInputNever ? nullptr : &Diags)
+ ? EXIT_SUCCESS
+ : 1;
+ if (DumpInput == DumpInputAlways ||
+ (ExitCode == 1 && DumpInput == DumpInputFail)) {
+ errs() << "\n"
+ << "Input file: "
+ << (InputFilename == "-" ? "<stdin>" : InputFilename.getValue())
+ << "\n"
+ << "Check file: " << CheckFilename << "\n"
+ << "\n"
+ << "-dump-input=help describes the format of the following dump.\n"
+ << "\n";
+ std::vector<InputAnnotation> Annotations;
+ unsigned LabelWidth;
+ BuildInputAnnotations(Diags, Annotations, LabelWidth);
+ DumpAnnotatedInput(errs(), InputFileText, Annotations, LabelWidth);
+ }
return ExitCode;
}
OpenPOWER on IntegriCloud