blob: b3a8433b54e658909e8fc51950716c1ae3a65ad5 [file] [log] [blame]
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001//==-- llvm/Support/FileCheck.h ---------------------------*- C++ -*-==//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +01006//
7//===----------------------------------------------------------------------===//
8//
9/// \file This file has some utilities to use FileCheck as an API
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_FILECHECK_H
14#define LLVM_SUPPORT_FILECHECK_H
15
16#include "llvm/ADT/StringMap.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/Support/Regex.h"
19#include "llvm/Support/SourceMgr.h"
20#include <vector>
21#include <map>
22
23namespace llvm {
24
25/// Contains info about various FileCheck options.
26struct FileCheckRequest {
27 std::vector<std::string> CheckPrefixes;
28 bool NoCanonicalizeWhiteSpace = false;
29 std::vector<std::string> ImplicitCheckNot;
30 std::vector<std::string> GlobalDefines;
31 bool AllowEmptyInput = false;
32 bool MatchFullLines = false;
33 bool EnableVarScope = false;
34 bool AllowDeprecatedDagOverlap = false;
35 bool Verbose = false;
36 bool VerboseVerbose = false;
37};
38
Andrew Walbran3d2c1972020-04-07 12:24:26 +010039//===----------------------------------------------------------------------===//
40// Numeric substitution handling code.
41//===----------------------------------------------------------------------===//
42
43/// Class representing a numeric variable and its associated current value.
44class FileCheckNumericVariable {
45private:
46 /// Name of the numeric variable.
47 StringRef Name;
48
49 /// Value of numeric variable, if defined, or None otherwise.
50 Optional<uint64_t> Value;
51
52 /// Line number where this variable is defined. Used to determine whether a
53 /// variable is defined on the same line as a given use.
54 size_t DefLineNumber;
55
56public:
57 /// Constructor for a variable \p Name defined at line \p DefLineNumber.
58 FileCheckNumericVariable(size_t DefLineNumber, StringRef Name)
59 : Name(Name), DefLineNumber(DefLineNumber) {}
60
61 /// Constructor for numeric variable \p Name with a known \p Value at parse
62 /// time (e.g. the @LINE numeric variable).
63 FileCheckNumericVariable(StringRef Name, uint64_t Value)
64 : Name(Name), Value(Value), DefLineNumber(0) {}
65
66 /// \returns name of this numeric variable.
67 StringRef getName() const { return Name; }
68
69 /// \returns this variable's value.
70 Optional<uint64_t> getValue() const { return Value; }
71
72 /// Sets value of this numeric variable, if undefined. Triggers an assertion
73 /// failure if the variable is actually defined.
74 void setValue(uint64_t Value);
75
76 /// Clears value of this numeric variable, regardless of whether it is
77 /// currently defined or not.
78 void clearValue();
79
80 /// \returns the line number where this variable is defined.
81 size_t getDefLineNumber() { return DefLineNumber; }
82};
83
84/// Type of functions evaluating a given binary operation.
85using binop_eval_t = uint64_t (*)(uint64_t, uint64_t);
86
87/// Class to represent an undefined variable error which prints that variable's
88/// name between quotes when printed.
89class FileCheckUndefVarError : public ErrorInfo<FileCheckUndefVarError> {
90private:
91 StringRef VarName;
92
93public:
94 static char ID;
95
96 FileCheckUndefVarError(StringRef VarName) : VarName(VarName) {}
97
98 StringRef getVarName() const { return VarName; }
99
100 std::error_code convertToErrorCode() const override {
101 return inconvertibleErrorCode();
102 }
103
104 /// Print name of variable associated with this error.
105 void log(raw_ostream &OS) const override {
106 OS << "\"";
107 OS.write_escaped(VarName) << "\"";
108 }
109};
110
111/// Class representing an expression consisting of either a single numeric
112/// variable or a binary operation between a numeric variable and an
113/// immediate.
114class FileCheckExpression {
115private:
116 /// Left operand.
117 FileCheckNumericVariable *LeftOp;
118
119 /// Right operand.
120 uint64_t RightOp;
121
122 /// Pointer to function that can evaluate this binary operation.
123 binop_eval_t EvalBinop;
124
125public:
126 FileCheckExpression(binop_eval_t EvalBinop,
127 FileCheckNumericVariable *OperandLeft,
128 uint64_t OperandRight)
129 : LeftOp(OperandLeft), RightOp(OperandRight), EvalBinop(EvalBinop) {}
130
131 /// Evaluates the value of this expression, using EvalBinop to perform the
132 /// binary operation it consists of. \returns an error if the numeric
133 /// variable used is undefined, or the expression value otherwise.
134 Expected<uint64_t> eval() const;
135};
136
137class FileCheckPatternContext;
138
139/// Class representing a substitution to perform in the RegExStr string.
140class FileCheckSubstitution {
141protected:
142 /// Pointer to a class instance holding, among other things, the table with
143 /// the values of live string variables at the start of any given CHECK line.
144 /// Used for substituting string variables with the text they were defined
145 /// as. Expressions are linked to the numeric variables they use at
146 /// parse time and directly access the value of the numeric variable to
147 /// evaluate their value.
148 FileCheckPatternContext *Context;
149
150 /// The string that needs to be substituted for something else. For a
151 /// string variable this is its name, otherwise this is the whole expression.
152 StringRef FromStr;
153
154 // Index in RegExStr of where to do the substitution.
155 size_t InsertIdx;
156
157public:
158 FileCheckSubstitution(FileCheckPatternContext *Context, StringRef VarName,
159 size_t InsertIdx)
160 : Context(Context), FromStr(VarName), InsertIdx(InsertIdx) {}
161
162 virtual ~FileCheckSubstitution() = default;
163
164 /// \returns the string to be substituted for something else.
165 StringRef getFromString() const { return FromStr; }
166
167 /// \returns the index where the substitution is to be performed in RegExStr.
168 size_t getIndex() const { return InsertIdx; }
169
170 /// \returns a string containing the result of the substitution represented
171 /// by this class instance or an error if substitution failed.
172 virtual Expected<std::string> getResult() const = 0;
173};
174
175class FileCheckStringSubstitution : public FileCheckSubstitution {
176public:
177 FileCheckStringSubstitution(FileCheckPatternContext *Context,
178 StringRef VarName, size_t InsertIdx)
179 : FileCheckSubstitution(Context, VarName, InsertIdx) {}
180
181 /// \returns the text that the string variable in this substitution matched
182 /// when defined, or an error if the variable is undefined.
183 Expected<std::string> getResult() const override;
184};
185
186class FileCheckNumericSubstitution : public FileCheckSubstitution {
187private:
188 /// Pointer to the class representing the expression whose value is to be
189 /// substituted.
190 FileCheckExpression *Expression;
191
192public:
193 FileCheckNumericSubstitution(FileCheckPatternContext *Context,
194 StringRef ExpressionStr,
195 FileCheckExpression *Expression,
196 size_t InsertIdx)
197 : FileCheckSubstitution(Context, ExpressionStr, InsertIdx),
198 Expression(Expression) {}
199
200 /// \returns a string containing the result of evaluating the expression in
201 /// this substitution, or an error if evaluation failed.
202 Expected<std::string> getResult() const override;
203};
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100204
205//===----------------------------------------------------------------------===//
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100206// Pattern handling code.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100207//===----------------------------------------------------------------------===//
208
209namespace Check {
Andrew Walbran16937d02019-10-22 13:54:20 +0100210
211enum FileCheckKind {
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100212 CheckNone = 0,
213 CheckPlain,
214 CheckNext,
215 CheckSame,
216 CheckNot,
217 CheckDAG,
218 CheckLabel,
219 CheckEmpty,
220
221 /// Indicates the pattern only matches the end of file. This is used for
222 /// trailing CHECK-NOTs.
223 CheckEOF,
224
225 /// Marks when parsing found a -NOT check combined with another CHECK suffix.
Andrew Walbran16937d02019-10-22 13:54:20 +0100226 CheckBadNot,
227
228 /// Marks when parsing found a -COUNT directive with invalid count value.
229 CheckBadCount
230};
231
232class FileCheckType {
233 FileCheckKind Kind;
234 int Count; ///< optional Count for some checks
235
236public:
237 FileCheckType(FileCheckKind Kind = CheckNone) : Kind(Kind), Count(1) {}
238 FileCheckType(const FileCheckType &) = default;
239
240 operator FileCheckKind() const { return Kind; }
241
242 int getCount() const { return Count; }
243 FileCheckType &setCount(int C);
244
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100245 // \returns a description of \p Prefix.
Andrew Walbran16937d02019-10-22 13:54:20 +0100246 std::string getDescription(StringRef Prefix) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100247};
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100248} // namespace Check
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100249
Andrew Walbran16937d02019-10-22 13:54:20 +0100250struct FileCheckDiag;
251
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100252/// Class holding the FileCheckPattern global state, shared by all patterns:
253/// tables holding values of variables and whether they are defined or not at
254/// any given time in the matching process.
255class FileCheckPatternContext {
256 friend class FileCheckPattern;
257
258private:
259 /// When matching a given pattern, this holds the value of all the string
260 /// variables defined in previous patterns. In a pattern, only the last
261 /// definition for a given variable is recorded in this table.
262 /// Back-references are used for uses after any the other definition.
263 StringMap<StringRef> GlobalVariableTable;
264
265 /// Map of all string variables defined so far. Used at parse time to detect
266 /// a name conflict between a numeric variable and a string variable when
267 /// the former is defined on a later line than the latter.
268 StringMap<bool> DefinedVariableTable;
269
270 /// When matching a given pattern, this holds the pointers to the classes
271 /// representing the numeric variables defined in previous patterns. When
272 /// matching a pattern all definitions for that pattern are recorded in the
273 /// NumericVariableDefs table in the FileCheckPattern instance of that
274 /// pattern.
275 StringMap<FileCheckNumericVariable *> GlobalNumericVariableTable;
276
277 /// Pointer to the class instance representing the @LINE pseudo variable for
278 /// easily updating its value.
279 FileCheckNumericVariable *LineVariable = nullptr;
280
281 /// Vector holding pointers to all parsed expressions. Used to automatically
282 /// free the expressions once they are guaranteed to no longer be used.
283 std::vector<std::unique_ptr<FileCheckExpression>> Expressions;
284
285 /// Vector holding pointers to all parsed numeric variables. Used to
286 /// automatically free them once they are guaranteed to no longer be used.
287 std::vector<std::unique_ptr<FileCheckNumericVariable>> NumericVariables;
288
289 /// Vector holding pointers to all substitutions. Used to automatically free
290 /// them once they are guaranteed to no longer be used.
291 std::vector<std::unique_ptr<FileCheckSubstitution>> Substitutions;
292
293public:
294 /// \returns the value of string variable \p VarName or an error if no such
295 /// variable has been defined.
296 Expected<StringRef> getPatternVarValue(StringRef VarName);
297
298 /// Defines string and numeric variables from definitions given on the
299 /// command line, passed as a vector of [#]VAR=VAL strings in
300 /// \p CmdlineDefines. \returns an error list containing diagnostics against
301 /// \p SM for all definition parsing failures, if any, or Success otherwise.
302 Error defineCmdlineVariables(std::vector<std::string> &CmdlineDefines,
303 SourceMgr &SM);
304
305 /// Create @LINE pseudo variable. Value is set when pattern are being
306 /// matched.
307 void createLineVariable();
308
309 /// Undefines local variables (variables whose name does not start with a '$'
310 /// sign), i.e. removes them from GlobalVariableTable and from
311 /// GlobalNumericVariableTable and also clears the value of numeric
312 /// variables.
313 void clearLocalVars();
314
315private:
316 /// Makes a new expression instance and registers it for destruction when
317 /// the context is destroyed.
318 FileCheckExpression *makeExpression(binop_eval_t EvalBinop,
319 FileCheckNumericVariable *OperandLeft,
320 uint64_t OperandRight);
321
322 /// Makes a new numeric variable and registers it for destruction when the
323 /// context is destroyed.
324 template <class... Types>
325 FileCheckNumericVariable *makeNumericVariable(Types... args);
326
327 /// Makes a new string substitution and registers it for destruction when the
328 /// context is destroyed.
329 FileCheckSubstitution *makeStringSubstitution(StringRef VarName,
330 size_t InsertIdx);
331
332 /// Makes a new numeric substitution and registers it for destruction when
333 /// the context is destroyed.
334 FileCheckSubstitution *
335 makeNumericSubstitution(StringRef ExpressionStr,
336 FileCheckExpression *Expression, size_t InsertIdx);
337};
338
339/// Class to represent an error holding a diagnostic with location information
340/// used when printing it.
341class FileCheckErrorDiagnostic : public ErrorInfo<FileCheckErrorDiagnostic> {
342private:
343 SMDiagnostic Diagnostic;
344
345public:
346 static char ID;
347
348 FileCheckErrorDiagnostic(SMDiagnostic &&Diag) : Diagnostic(Diag) {}
349
350 std::error_code convertToErrorCode() const override {
351 return inconvertibleErrorCode();
352 }
353
354 /// Print diagnostic associated with this error when printing the error.
355 void log(raw_ostream &OS) const override { Diagnostic.print(nullptr, OS); }
356
357 static Error get(const SourceMgr &SM, SMLoc Loc, const Twine &ErrMsg) {
358 return make_error<FileCheckErrorDiagnostic>(
359 SM.GetMessage(Loc, SourceMgr::DK_Error, ErrMsg));
360 }
361
362 static Error get(const SourceMgr &SM, StringRef Buffer, const Twine &ErrMsg) {
363 return get(SM, SMLoc::getFromPointer(Buffer.data()), ErrMsg);
364 }
365};
366
367class FileCheckNotFoundError : public ErrorInfo<FileCheckNotFoundError> {
368public:
369 static char ID;
370
371 std::error_code convertToErrorCode() const override {
372 return inconvertibleErrorCode();
373 }
374
375 /// Print diagnostic associated with this error when printing the error.
376 void log(raw_ostream &OS) const override {
377 OS << "String not found in input";
378 }
379};
380
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100381class FileCheckPattern {
382 SMLoc PatternLoc;
383
384 /// A fixed string to match as the pattern or empty if this pattern requires
385 /// a regex match.
386 StringRef FixedStr;
387
388 /// A regex string to match as the pattern or empty if this pattern requires
389 /// a fixed string to match.
390 std::string RegExStr;
391
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100392 /// Entries in this vector represent a substitution of a string variable or
393 /// an expression in the RegExStr regex at match time. For example, in the
394 /// case of a CHECK directive with the pattern "foo[[bar]]baz[[#N+1]]",
395 /// RegExStr will contain "foobaz" and we'll get two entries in this vector
396 /// that tells us to insert the value of string variable "bar" at offset 3
397 /// and the value of expression "N+1" at offset 6.
398 std::vector<FileCheckSubstitution *> Substitutions;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100399
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100400 /// Maps names of string variables defined in a pattern to the number of
401 /// their parenthesis group in RegExStr capturing their last definition.
Andrew Scull0372a572018-11-16 15:47:06 +0000402 ///
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100403 /// E.g. for the pattern "foo[[bar:.*]]baz([[bar]][[QUUX]][[bar:.*]])",
404 /// RegExStr will be "foo(.*)baz(\1<quux value>(.*))" where <quux value> is
405 /// the value captured for QUUX on the earlier line where it was defined, and
406 /// VariableDefs will map "bar" to the third parenthesis group which captures
407 /// the second definition of "bar".
408 ///
409 /// Note: uses std::map rather than StringMap to be able to get the key when
410 /// iterating over values.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100411 std::map<StringRef, unsigned> VariableDefs;
412
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100413 /// Structure representing the definition of a numeric variable in a pattern.
414 /// It holds the pointer to the class representing the numeric variable whose
415 /// value is being defined and the number of the parenthesis group in
416 /// RegExStr to capture that value.
417 struct FileCheckNumericVariableMatch {
418 /// Pointer to class representing the numeric variable whose value is being
419 /// defined.
420 FileCheckNumericVariable *DefinedNumericVariable;
421
422 /// Number of the parenthesis group in RegExStr that captures the value of
423 /// this numeric variable definition.
424 unsigned CaptureParenGroup;
425 };
426
427 /// Holds the number of the parenthesis group in RegExStr and pointer to the
428 /// corresponding FileCheckNumericVariable class instance of all numeric
429 /// variable definitions. Used to set the matched value of all those
430 /// variables.
431 StringMap<FileCheckNumericVariableMatch> NumericVariableDefs;
432
433 /// Pointer to a class instance holding the global state shared by all
434 /// patterns:
435 /// - separate tables with the values of live string and numeric variables
436 /// respectively at the start of any given CHECK line;
437 /// - table holding whether a string variable has been defined at any given
438 /// point during the parsing phase.
439 FileCheckPatternContext *Context;
440
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100441 Check::FileCheckType CheckTy;
442
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100443 /// Line number for this CHECK pattern. Used to determine whether a variable
444 /// definition is made on an earlier line to the one with this CHECK.
445 size_t LineNumber;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100446
447public:
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100448 FileCheckPattern(Check::FileCheckType Ty, FileCheckPatternContext *Context,
449 size_t Line)
450 : Context(Context), CheckTy(Ty), LineNumber(Line) {}
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100451
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100452 /// \returns the location in source code.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100453 SMLoc getLoc() const { return PatternLoc; }
454
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100455 /// \returns the pointer to the global state for all patterns in this
456 /// FileCheck instance.
457 FileCheckPatternContext *getContext() const { return Context; }
458
459 /// \returns whether \p C is a valid first character for a variable name.
460 static bool isValidVarNameStart(char C);
461 /// Parses the string at the start of \p Str for a variable name. \returns
462 /// an error holding a diagnostic against \p SM if parsing fail, or the
463 /// name of the variable otherwise. In the latter case, sets \p IsPseudo to
464 /// indicate if it is a pseudo variable and strips \p Str from the variable
465 /// name.
466 static Expected<StringRef> parseVariable(StringRef &Str, bool &IsPseudo,
467 const SourceMgr &SM);
468 /// Parses \p Expr for the name of a numeric variable to be defined at line
469 /// \p LineNumber. \returns a pointer to the class instance representing that
470 /// variable, creating it if needed, or an error holding a diagnostic against
471 /// \p SM should defining such a variable be invalid.
472 static Expected<FileCheckNumericVariable *>
473 parseNumericVariableDefinition(StringRef &Expr,
474 FileCheckPatternContext *Context,
475 size_t LineNumber, const SourceMgr &SM);
476 /// Parses \p Expr for a numeric substitution block. \returns the class
477 /// representing the AST of the expression whose value must be substituted,
478 /// or an error holding a diagnostic against \p SM if parsing fails. If
479 /// substitution was successful, sets \p DefinedNumericVariable to point to
480 /// the class representing the numeric variable defined in this numeric
481 /// substitution block, or None if this block does not define any variable.
482 Expected<FileCheckExpression *> parseNumericSubstitutionBlock(
483 StringRef Expr,
484 Optional<FileCheckNumericVariable *> &DefinedNumericVariable,
485 const SourceMgr &SM) const;
486 /// Parses the pattern in \p PatternStr and initializes this FileCheckPattern
487 /// instance accordingly.
488 ///
489 /// \p Prefix provides which prefix is being matched, \p Req describes the
490 /// global options that influence the parsing such as whitespace
491 /// canonicalization, \p SM provides the SourceMgr used for error reports.
492 /// \returns true in case of an error, false otherwise.
493 bool parsePattern(StringRef PatternStr, StringRef Prefix, SourceMgr &SM,
494 const FileCheckRequest &Req);
495 /// Matches the pattern string against the input buffer \p Buffer
496 ///
497 /// \returns the position that is matched or an error indicating why matching
498 /// failed. If there is a match, updates \p MatchLen with the size of the
499 /// matched string.
500 ///
501 /// The GlobalVariableTable StringMap in the FileCheckPatternContext class
502 /// instance provides the current values of FileCheck string variables and
503 /// is updated if this match defines new values. Likewise, the
504 /// GlobalNumericVariableTable StringMap in the same class provides the
505 /// current values of FileCheck numeric variables and is updated if this
506 /// match defines new numeric values.
507 Expected<size_t> match(StringRef Buffer, size_t &MatchLen,
508 const SourceMgr &SM) const;
509 /// Prints the value of successful substitutions or the name of the undefined
510 /// string or numeric variable preventing a successful substitution.
511 void printSubstitutions(const SourceMgr &SM, StringRef Buffer,
512 SMRange MatchRange = None) const;
513 void printFuzzyMatch(const SourceMgr &SM, StringRef Buffer,
Andrew Walbran16937d02019-10-22 13:54:20 +0100514 std::vector<FileCheckDiag> *Diags) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100515
516 bool hasVariable() const {
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100517 return !(Substitutions.empty() && VariableDefs.empty());
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100518 }
519
520 Check::FileCheckType getCheckTy() const { return CheckTy; }
521
Andrew Walbran16937d02019-10-22 13:54:20 +0100522 int getCount() const { return CheckTy.getCount(); }
523
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100524private:
525 bool AddRegExToRegEx(StringRef RS, unsigned &CurParen, SourceMgr &SM);
526 void AddBackrefToRegEx(unsigned BackrefNum);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100527 /// Computes an arbitrary estimate for the quality of matching this pattern
528 /// at the start of \p Buffer; a distance of zero should correspond to a
529 /// perfect match.
530 unsigned computeMatchDistance(StringRef Buffer) const;
531 /// Finds the closing sequence of a regex variable usage or definition.
532 ///
533 /// \p Str has to point in the beginning of the definition (right after the
534 /// opening sequence). \p SM holds the SourceMgr used for error repporting.
535 /// \returns the offset of the closing sequence within Str, or npos if it
536 /// was not found.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100537 size_t FindRegexVarEnd(StringRef Str, SourceMgr &SM);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100538
539 /// Parses \p Expr for the use of a numeric variable. \returns the pointer to
540 /// the class instance representing that variable if successful, or an error
541 /// holding a diagnostic against \p SM otherwise.
542 Expected<FileCheckNumericVariable *>
543 parseNumericVariableUse(StringRef &Expr, const SourceMgr &SM) const;
544 /// Parses \p Expr for a binary operation.
545 /// \returns the class representing the binary operation of the expression,
546 /// or an error holding a diagnostic against \p SM otherwise.
547 Expected<FileCheckExpression *> parseBinop(StringRef &Expr,
548 const SourceMgr &SM) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100549};
550
551//===----------------------------------------------------------------------===//
Andrew Walbran16937d02019-10-22 13:54:20 +0100552/// Summary of a FileCheck diagnostic.
553//===----------------------------------------------------------------------===//
554
555struct FileCheckDiag {
556 /// What is the FileCheck directive for this diagnostic?
557 Check::FileCheckType CheckTy;
558 /// Where is the FileCheck directive for this diagnostic?
559 unsigned CheckLine, CheckCol;
560 /// What type of match result does this diagnostic describe?
561 ///
562 /// A directive's supplied pattern is said to be either expected or excluded
563 /// depending on whether the pattern must have or must not have a match in
564 /// order for the directive to succeed. For example, a CHECK directive's
565 /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
566 /// All match result types whose names end with "Excluded" are for excluded
567 /// patterns, and all others are for expected patterns.
568 ///
569 /// There might be more than one match result for a single pattern. For
570 /// example, there might be several discarded matches
571 /// (MatchFoundButDiscarded) before either a good match
572 /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
573 /// and there might be a fuzzy match (MatchFuzzy) after the latter.
574 enum MatchType {
575 /// Indicates a good match for an expected pattern.
576 MatchFoundAndExpected,
577 /// Indicates a match for an excluded pattern.
578 MatchFoundButExcluded,
579 /// Indicates a match for an expected pattern, but the match is on the
580 /// wrong line.
581 MatchFoundButWrongLine,
582 /// Indicates a discarded match for an expected pattern.
583 MatchFoundButDiscarded,
584 /// Indicates no match for an excluded pattern.
585 MatchNoneAndExcluded,
586 /// Indicates no match for an expected pattern, but this might follow good
587 /// matches when multiple matches are expected for the pattern, or it might
588 /// follow discarded matches for the pattern.
589 MatchNoneButExpected,
590 /// Indicates a fuzzy match that serves as a suggestion for the next
591 /// intended match for an expected pattern with too few or no good matches.
592 MatchFuzzy,
593 } MatchTy;
594 /// The search range if MatchTy is MatchNoneAndExcluded or
595 /// MatchNoneButExpected, or the match range otherwise.
596 unsigned InputStartLine;
597 unsigned InputStartCol;
598 unsigned InputEndLine;
599 unsigned InputEndCol;
600 FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy,
601 SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange);
602};
603
604//===----------------------------------------------------------------------===//
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100605// Check Strings.
606//===----------------------------------------------------------------------===//
607
608/// A check that we found in the input file.
609struct FileCheckString {
610 /// The pattern to match.
611 FileCheckPattern Pat;
612
613 /// Which prefix name this check matched.
614 StringRef Prefix;
615
616 /// The location in the match file that the check string was specified.
617 SMLoc Loc;
618
619 /// All of the strings that are disallowed from occurring between this match
620 /// string and the previous one (or start of file).
621 std::vector<FileCheckPattern> DagNotStrings;
622
623 FileCheckString(const FileCheckPattern &P, StringRef S, SMLoc L)
624 : Pat(P), Prefix(S), Loc(L) {}
625
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100626 /// Matches check string and its "not strings" and/or "dag strings".
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100627 size_t Check(const SourceMgr &SM, StringRef Buffer, bool IsLabelScanMode,
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100628 size_t &MatchLen, FileCheckRequest &Req,
629 std::vector<FileCheckDiag> *Diags) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100630
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100631 /// Verifies that there is a single line in the given \p Buffer. Errors are
632 /// reported against \p SM.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100633 bool CheckNext(const SourceMgr &SM, StringRef Buffer) const;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100634 /// Verifies that there is no newline in the given \p Buffer. Errors are
635 /// reported against \p SM.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100636 bool CheckSame(const SourceMgr &SM, StringRef Buffer) const;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100637 /// Verifies that none of the strings in \p NotStrings are found in the given
638 /// \p Buffer. Errors are reported against \p SM and diagnostics recorded in
639 /// \p Diags according to the verbosity level set in \p Req.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100640 bool CheckNot(const SourceMgr &SM, StringRef Buffer,
641 const std::vector<const FileCheckPattern *> &NotStrings,
Andrew Walbran16937d02019-10-22 13:54:20 +0100642 const FileCheckRequest &Req,
643 std::vector<FileCheckDiag> *Diags) const;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100644 /// Matches "dag strings" and their mixed "not strings".
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100645 size_t CheckDag(const SourceMgr &SM, StringRef Buffer,
646 std::vector<const FileCheckPattern *> &NotStrings,
Andrew Walbran16937d02019-10-22 13:54:20 +0100647 const FileCheckRequest &Req,
648 std::vector<FileCheckDiag> *Diags) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100649};
650
651/// FileCheck class takes the request and exposes various methods that
652/// use information from the request.
653class FileCheck {
654 FileCheckRequest Req;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100655 FileCheckPatternContext PatternContext;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100656
657public:
658 FileCheck(FileCheckRequest Req) : Req(Req) {}
659
660 // Combines the check prefixes into a single regex so that we can efficiently
661 // scan for any of the set.
662 //
663 // The semantics are that the longest-match wins which matches our regex
664 // library.
665 Regex buildCheckPrefixRegex();
666
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100667 /// Reads the check file from \p Buffer and records the expected strings it
668 /// contains in the \p CheckStrings vector. Errors are reported against
669 /// \p SM.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100670 ///
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100671 /// Only expected strings whose prefix is one of those listed in \p PrefixRE
672 /// are recorded. \returns true in case of an error, false otherwise.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100673 bool ReadCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE,
674 std::vector<FileCheckString> &CheckStrings);
675
676 bool ValidateCheckPrefixes();
677
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100678 /// Canonicalizes whitespaces in the file. Line endings are replaced with
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100679 /// UNIX-style '\n'.
680 StringRef CanonicalizeFile(MemoryBuffer &MB,
681 SmallVectorImpl<char> &OutputBuffer);
682
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100683 /// Checks the input to FileCheck provided in the \p Buffer against the
684 /// \p CheckStrings read from the check file and record diagnostics emitted
685 /// in \p Diags. Errors are recorded against \p SM.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100686 ///
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100687 /// \returns false if the input fails to satisfy the checks.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100688 bool CheckInput(SourceMgr &SM, StringRef Buffer,
Andrew Walbran16937d02019-10-22 13:54:20 +0100689 ArrayRef<FileCheckString> CheckStrings,
690 std::vector<FileCheckDiag> *Diags = nullptr);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100691};
692} // namespace llvm
693#endif