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;
75 operator FileCheckKind() const { return Kind
; }
77 int getCount() const { return Count
; }
78 FileCheckType
&setCount(int C
);
80 // \returns a description of \p Prefix.
81 std::string
getDescription(StringRef Prefix
) const;
85 struct FileCheckDiag
{
86 /// What is the FileCheck directive for this diagnostic?
87 Check::FileCheckType CheckTy
;
88 /// Where is the FileCheck directive for this diagnostic?
89 unsigned CheckLine
, CheckCol
;
90 /// What type of match result does this diagnostic describe?
92 /// A directive's supplied pattern is said to be either expected or excluded
93 /// depending on whether the pattern must have or must not have a match in
94 /// order for the directive to succeed. For example, a CHECK directive's
95 /// pattern is expected, and a CHECK-NOT directive's pattern is excluded.
96 /// All match result types whose names end with "Excluded" are for excluded
97 /// patterns, and all others are for expected patterns.
99 /// There might be more than one match result for a single pattern. For
100 /// example, there might be several discarded matches
101 /// (MatchFoundButDiscarded) before either a good match
102 /// (MatchFoundAndExpected) or a failure to match (MatchNoneButExpected),
103 /// and there might be a fuzzy match (MatchFuzzy) after the latter.
105 /// Indicates a good match for an expected pattern.
106 MatchFoundAndExpected
,
107 /// Indicates a match for an excluded pattern.
108 MatchFoundButExcluded
,
109 /// Indicates a match for an expected pattern, but the match is on the
111 MatchFoundButWrongLine
,
112 /// Indicates a discarded match for an expected pattern.
113 MatchFoundButDiscarded
,
114 /// Indicates no match for an excluded pattern.
115 MatchNoneAndExcluded
,
116 /// Indicates no match for an expected pattern, but this might follow good
117 /// matches when multiple matches are expected for the pattern, or it might
118 /// follow discarded matches for the pattern.
119 MatchNoneButExpected
,
120 /// Indicates a fuzzy match that serves as a suggestion for the next
121 /// intended match for an expected pattern with too few or no good matches.
124 /// The search range if MatchTy is MatchNoneAndExcluded or
125 /// MatchNoneButExpected, or the match range otherwise.
126 unsigned InputStartLine
;
127 unsigned InputStartCol
;
128 unsigned InputEndLine
;
129 unsigned InputEndCol
;
130 FileCheckDiag(const SourceMgr
&SM
, const Check::FileCheckType
&CheckTy
,
131 SMLoc CheckLoc
, MatchType MatchTy
, SMRange InputRange
);
134 class FileCheckPatternContext
;
135 struct FileCheckString
;
137 /// FileCheck class takes the request and exposes various methods that
138 /// use information from the request.
140 FileCheckRequest Req
;
141 std::unique_ptr
<FileCheckPatternContext
> PatternContext
;
142 // C++17 TODO: make this a plain std::vector.
143 std::unique_ptr
<std::vector
<FileCheckString
>> CheckStrings
;
146 explicit FileCheck(FileCheckRequest Req
);
149 // Combines the check prefixes into a single regex so that we can efficiently
150 // scan for any of the set.
152 // The semantics are that the longest-match wins which matches our regex
154 Regex
buildCheckPrefixRegex();
156 /// Reads the check file from \p Buffer and records the expected strings it
157 /// contains. Errors are reported against \p SM.
159 /// Only expected strings whose prefix is one of those listed in \p PrefixRE
160 /// are recorded. \returns true in case of an error, false otherwise.
161 bool readCheckFile(SourceMgr
&SM
, StringRef Buffer
, Regex
&PrefixRE
);
163 bool ValidateCheckPrefixes();
165 /// Canonicalizes whitespaces in the file. Line endings are replaced with
167 StringRef
CanonicalizeFile(MemoryBuffer
&MB
,
168 SmallVectorImpl
<char> &OutputBuffer
);
170 /// Checks the input to FileCheck provided in the \p Buffer against the
171 /// expected strings read from the check file and record diagnostics emitted
172 /// in \p Diags. Errors are recorded against \p SM.
174 /// \returns false if the input fails to satisfy the checks.
175 bool checkInput(SourceMgr
&SM
, StringRef Buffer
,
176 std::vector
<FileCheckDiag
> *Diags
= nullptr);