1 //==-- llvm/Support/FileCheck.h ---------------------------*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
9 /// \file This file has some utilities to use FileCheck as an API
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_SUPPORT_FILECHECK_H
14 #define LLVM_SUPPORT_FILECHECK_H
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/Regex.h"
19 #include "llvm/Support/SourceMgr.h"
25 /// Contains info about various FileCheck options.
26 struct 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 IgnoreCase = false;
34 bool EnableVarScope = false;
35 bool AllowDeprecatedDagOverlap = false;
37 bool VerboseVerbose = false;
40 //===----------------------------------------------------------------------===//
41 // Summary of a FileCheck diagnostic.
42 //===----------------------------------------------------------------------===//
56 /// Indicates the pattern only matches the end of file. This is used for
57 /// trailing CHECK-NOTs.
60 /// Marks when parsing found a -NOT check combined with another CHECK suffix.
63 /// Marks when parsing found a -COUNT directive with invalid count value.
69 int Count; ///< optional Count for some checks
72 FileCheckType(FileCheckKind Kind = CheckNone) : Kind(Kind), Count(1) {}
73 FileCheckType(const FileCheckType &) = default;
74 FileCheckType &operator=(const FileCheckType &) = default;
76 operator FileCheckKind() const { return Kind; }
78 int getCount() const { return Count; }
79 FileCheckType &setCount(int C);
81 // \returns a description of \p Prefix.
82 std::string getDescription(StringRef Prefix) const;
86 struct FileCheckDiag {
87 /// What is the FileCheck directive for this diagnostic?
88 Check::FileCheckType CheckTy;
89 /// Where is the FileCheck directive for this diagnostic?
90 unsigned CheckLine, CheckCol;
91 /// What type of match result does this diagnostic describe?
93 /// A directive's supplied pattern is said to be either expected or excluded
94 /// depending on whether the pattern must have or must not have a match in
95 /// order for the directive to succeed. For example, a CHECK directive's
96 /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
97 /// All match result types whose names end with "Excluded" are for excluded
98 /// patterns, and all others are for expected patterns.
100 /// There might be more than one match result for a single pattern. For
101 /// example, there might be several discarded matches
102 /// (MatchFoundButDiscarded) before either a good match
103 /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
104 /// and there might be a fuzzy match (MatchFuzzy) after the latter.
106 /// Indicates a good match for an expected pattern.
107 MatchFoundAndExpected,
108 /// Indicates a match for an excluded pattern.
109 MatchFoundButExcluded,
110 /// Indicates a match for an expected pattern, but the match is on the
112 MatchFoundButWrongLine,
113 /// Indicates a discarded match for an expected pattern.
114 MatchFoundButDiscarded,
115 /// Indicates no match for an excluded pattern.
116 MatchNoneAndExcluded,
117 /// Indicates no match for an expected pattern, but this might follow good
118 /// matches when multiple matches are expected for the pattern, or it might
119 /// follow discarded matches for the pattern.
120 MatchNoneButExpected,
121 /// Indicates a fuzzy match that serves as a suggestion for the next
122 /// intended match for an expected pattern with too few or no good matches.
125 /// The search range if MatchTy is MatchNoneAndExcluded or
126 /// MatchNoneButExpected, or the match range otherwise.
127 unsigned InputStartLine;
128 unsigned InputStartCol;
129 unsigned InputEndLine;
130 unsigned InputEndCol;
131 FileCheckDiag(const SourceMgr &SM, const Check::FileCheckType &CheckTy,
132 SMLoc CheckLoc, MatchType MatchTy, SMRange InputRange);
135 class FileCheckPatternContext;
136 struct FileCheckString;
138 /// FileCheck class takes the request and exposes various methods that
139 /// use information from the request.
141 FileCheckRequest Req;
142 std::unique_ptr<FileCheckPatternContext> PatternContext;
143 // C++17 TODO: make this a plain std::vector.
144 std::unique_ptr<std::vector<FileCheckString>> CheckStrings;
147 explicit FileCheck(FileCheckRequest Req);
150 // Combines the check prefixes into a single regex so that we can efficiently
151 // scan for any of the set.
153 // The semantics are that the longest-match wins which matches our regex
155 Regex buildCheckPrefixRegex();
157 /// Reads the check file from \p Buffer and records the expected strings it
158 /// contains. Errors are reported against \p SM.
160 /// Only expected strings whose prefix is one of those listed in \p PrefixRE
161 /// are recorded. \returns true in case of an error, false otherwise.
162 bool readCheckFile(SourceMgr &SM, StringRef Buffer, Regex &PrefixRE);
164 bool ValidateCheckPrefixes();
166 /// Canonicalizes whitespaces in the file. Line endings are replaced with
168 StringRef CanonicalizeFile(MemoryBuffer &MB,
169 SmallVectorImpl<char> &OutputBuffer);
171 /// Checks the input to FileCheck provided in the \p Buffer against the
172 /// expected strings read from the check file and record diagnostics emitted
173 /// in \p Diags. Errors are recorded against \p SM.
175 /// \returns false if the input fails to satisfy the checks.
176 bool checkInput(SourceMgr &SM, StringRef Buffer,
177 std::vector<FileCheckDiag> *Diags = nullptr);