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