clang 23.0.0git
VerifyDiagnosticConsumer.h
Go to the documentation of this file.
1//===- VerifyDiagnosticConsumer.h - Verifying Diagnostic Client -*- C++ -*-===//
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#ifndef LLVM_CLANG_FRONTEND_VERIFYDIAGNOSTICCONSUMER_H
10#define LLVM_CLANG_FRONTEND_VERIFYDIAGNOSTICCONSUMER_H
11
14#include "clang/Basic/LLVM.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/PointerIntPair.h"
19#include "llvm/ADT/StringRef.h"
20#include <cassert>
21#include <limits>
22#include <memory>
23#include <string>
24#include <vector>
25
26namespace clang {
27
28class FileEntry;
29class LangOptions;
30class SourceManager;
32
35 OnlyPartial, /// Match, but not a full match.
36 AtLeastPartial, /// Match, but we didn't check for full match.
38};
39
40/// VerifyDiagnosticConsumer - Create a diagnostic client which will use
41/// markers in the input source to check that all the emitted diagnostics match
42/// those expected. See clang/docs/InternalsManual.rst for details about how to
43/// write tests to verify diagnostics.
44///
46 public CommentHandler {
47public:
48 /// Directive - Abstract class representing a parsed verify directive.
49 ///
50 class Directive {
51 public:
52 static std::unique_ptr<Directive>
53 create(bool RegexKind, SourceLocation DirectiveLoc,
55 bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text,
56 unsigned Min, unsigned Max, bool FullMatchRequired);
57
58 public:
59 /// Constant representing n or more matches.
60 static const unsigned MaxCount = std::numeric_limits<unsigned>::max();
61
64 const std::string Spelling;
65 const std::string Text;
66 unsigned Min, Max;
68 bool MatchAnyFileAndLine; // `MatchAnyFileAndLine` implies `MatchAnyLine`.
70
71 Directive(const Directive &) = delete;
72 Directive &operator=(const Directive &) = delete;
73 virtual ~Directive() = default;
74
75 // Returns true if directive text is valid.
76 // Otherwise returns false and populates E.
77 virtual bool isValid(std::string &Error) = 0;
78
79 // Returns true on match.
80 virtual DiagnosticMatchResult match(StringRef S) const = 0;
81
82 protected:
84 StringRef Spelling, bool MatchAnyFileAndLine, bool MatchAnyLine,
85 StringRef Text, unsigned Min, unsigned Max,
92 assert(!DirectiveLoc.isInvalid() && "DirectiveLoc is invalid!");
93 assert((!DiagnosticLoc.isInvalid() || MatchAnyLine) &&
94 "DiagnosticLoc is invalid!");
95 }
96 };
97
98 using DirectiveList = std::vector<std::unique_ptr<Directive>>;
99
100 /// ExpectedData - owns directive objects and deletes on destructor.
106
107 void Reset() {
108 Errors.clear();
109 Warnings.clear();
110 Remarks.clear();
111 Notes.clear();
112 }
113 };
114
121
128
129 class MarkerTracker;
130
131private:
132 DiagnosticsEngine &Diags;
133 DiagnosticConsumer *PrimaryClient;
134 std::unique_ptr<DiagnosticConsumer> PrimaryClientOwner;
135 std::unique_ptr<TextDiagnosticBuffer> Buffer;
136 std::unique_ptr<MarkerTracker> Markers;
137 const Preprocessor *CurrentPreprocessor = nullptr;
138 const LangOptions *LangOpts = nullptr;
139 SourceManager *SrcManager = nullptr;
140 unsigned ActiveSourceFiles = 0;
141 ParsingState State;
142 ExpectedData ED;
143 bool CheckOrderOfDirectives;
144 bool OneDiagPerDirective;
145 bool DisableWildcardInDiagLoc;
146
147 void CheckDiagnostics();
148
149 void setSourceManager(SourceManager &SM) {
150 assert((!SrcManager || SrcManager == &SM) && "SourceManager changed!");
151 SrcManager = &SM;
152 }
153
154 // These facilities are used for validation in debug builds.
155 class UnparsedFileStatus {
157 bool FoundDirectives;
158
159 public:
160 UnparsedFileStatus(OptionalFileEntryRef File, bool FoundDirectives)
161 : File(File), FoundDirectives(FoundDirectives) {}
162
163 OptionalFileEntryRef getFile() const { return File; }
164 bool foundDirectives() const { return FoundDirectives; }
165 };
166
167 using ParsedFilesMap = llvm::DenseMap<FileID, const FileEntry *>;
168 using UnparsedFilesMap = llvm::DenseMap<FileID, UnparsedFileStatus>;
169
170 ParsedFilesMap ParsedFiles;
171 UnparsedFilesMap UnparsedFiles;
172
173public:
174 /// Create a new verifying diagnostic client, which will issue errors to
175 /// the currently-attached diagnostic client when a diagnostic does not match
176 /// what is expected (as indicated in the source file).
177 VerifyDiagnosticConsumer(DiagnosticsEngine &Diags);
178 ~VerifyDiagnosticConsumer() override;
179
180 void BeginSourceFile(const LangOptions &LangOpts,
181 const Preprocessor *PP) override;
182
183 void EndSourceFile() override;
184
186 /// File has been processed via HandleComment.
188
189 /// File has diagnostics and may have directives.
191
192 /// File has diagnostics but guaranteed no directives.
194 };
195
196 /// Update lists of parsed and unparsed files.
198
199 bool HandleComment(Preprocessor &PP, SourceRange Comment) override;
200
202 const Diagnostic &Info) override;
203};
204
205} // namespace clang
206
207#endif // LLVM_CLANG_FRONTEND_VERIFYDIAGNOSTICCONSUMER_H
Defines the Diagnostic-related interfaces.
Defines the clang::FileManager interface and associated types.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
#define SM(sm)
Defines the clang::Preprocessor interface.
Defines the clang::SourceLocation class and associated facilities.
Abstract base class that describes a handler that will receive source ranges for each of the comments...
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine a...
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:232
Level
The level of the diagnostic, after it has been through mapping.
Definition Diagnostic.h:237
Cached information about one file (either on disk or in the virtual file system).
Definition FileEntry.h:302
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
virtual bool isValid(std::string &Error)=0
Directive(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, StringRef Spelling, bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text, unsigned Min, unsigned Max, bool FullMatchRequired)
virtual DiagnosticMatchResult match(StringRef S) const =0
static const unsigned MaxCount
Constant representing n or more matches.
static std::unique_ptr< Directive > create(bool RegexKind, SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, StringRef Spelling, bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text, unsigned Min, unsigned Max, bool FullMatchRequired)
Directive & operator=(const Directive &)=delete
void UpdateParsedFileStatus(SourceManager &SM, FileID FID, ParsedStatus PS)
Update lists of parsed and unparsed files.
VerifyDiagnosticConsumer(DiagnosticsEngine &Diags)
Create a new verifying diagnostic client, which will issue errors to the currently-attached diagnosti...
@ IsUnparsed
File has diagnostics and may have directives.
@ IsUnparsedNoDirectives
File has diagnostics but guaranteed no directives.
@ IsParsed
File has been processed via HandleComment.
void EndSourceFile() override
Callback to inform the diagnostic client that processing of a source file has ended.
void BeginSourceFile(const LangOptions &LangOpts, const Preprocessor *PP) override
Callback to inform the diagnostic client that processing of a source file is beginning.
std::vector< std::unique_ptr< Directive > > DirectiveList
bool HandleComment(Preprocessor &PP, SourceRange Comment) override
HandleComment - Hook into the preprocessor and extract comments containing expected errors and warnin...
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override
Handle this diagnostic, reporting it to the user or capturing it to a log as needed.
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
@ AtLeastPartial
Match, but not a full match.
@ Full
Match, but we didn't check for full match.
@ None
The alignment was not explicit in code.
Definition ASTContext.h:179
ExpectedData - owns directive objects and deletes on destructor.