clang 22.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
33/// VerifyDiagnosticConsumer - Create a diagnostic client which will use
34/// markers in the input source to check that all the emitted diagnostics match
35/// those expected. See clang/docs/InternalsManual.rst for details about how to
36/// write tests to verify diagnostics.
37///
39 public CommentHandler {
40public:
41 /// Directive - Abstract class representing a parsed verify directive.
42 ///
43 class Directive {
44 public:
45 static std::unique_ptr<Directive>
46 create(bool RegexKind, SourceLocation DirectiveLoc,
48 bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text,
49 unsigned Min, unsigned Max);
50
51 public:
52 /// Constant representing n or more matches.
53 static const unsigned MaxCount = std::numeric_limits<unsigned>::max();
54
57 const std::string Spelling;
58 const std::string Text;
59 unsigned Min, Max;
61 bool MatchAnyFileAndLine; // `MatchAnyFileAndLine` implies `MatchAnyLine`.
62
63 Directive(const Directive &) = delete;
64 Directive &operator=(const Directive &) = delete;
65 virtual ~Directive() = default;
66
67 // Returns true if directive text is valid.
68 // Otherwise returns false and populates E.
69 virtual bool isValid(std::string &Error) = 0;
70
71 // Returns true on match.
72 virtual bool match(StringRef S) = 0;
73
74 protected:
76 StringRef Spelling, bool MatchAnyFileAndLine, bool MatchAnyLine,
77 StringRef Text, unsigned Min, unsigned Max)
82 assert(!DirectiveLoc.isInvalid() && "DirectiveLoc is invalid!");
83 assert((!DiagnosticLoc.isInvalid() || MatchAnyLine) &&
84 "DiagnosticLoc is invalid!");
85 }
86 };
87
88 using DirectiveList = std::vector<std::unique_ptr<Directive>>;
89
90 /// ExpectedData - owns directive objects and deletes on destructor.
91 struct ExpectedData {
96
97 void Reset() {
98 Errors.clear();
99 Warnings.clear();
100 Remarks.clear();
101 Notes.clear();
102 }
103 };
104
111
116
117 class MarkerTracker;
118
119private:
120 DiagnosticsEngine &Diags;
121 DiagnosticConsumer *PrimaryClient;
122 std::unique_ptr<DiagnosticConsumer> PrimaryClientOwner;
123 std::unique_ptr<TextDiagnosticBuffer> Buffer;
124 std::unique_ptr<MarkerTracker> Markers;
125 const Preprocessor *CurrentPreprocessor = nullptr;
126 const LangOptions *LangOpts = nullptr;
127 SourceManager *SrcManager = nullptr;
128 unsigned ActiveSourceFiles = 0;
129 ParsingState State;
130 ExpectedData ED;
131
132 void CheckDiagnostics();
133
134 void setSourceManager(SourceManager &SM) {
135 assert((!SrcManager || SrcManager == &SM) && "SourceManager changed!");
136 SrcManager = &SM;
137 }
138
139 // These facilities are used for validation in debug builds.
140 class UnparsedFileStatus {
142 bool FoundDirectives;
143
144 public:
145 UnparsedFileStatus(OptionalFileEntryRef File, bool FoundDirectives)
146 : File(File), FoundDirectives(FoundDirectives) {}
147
148 OptionalFileEntryRef getFile() const { return File; }
149 bool foundDirectives() const { return FoundDirectives; }
150 };
151
152 using ParsedFilesMap = llvm::DenseMap<FileID, const FileEntry *>;
153 using UnparsedFilesMap = llvm::DenseMap<FileID, UnparsedFileStatus>;
154
155 ParsedFilesMap ParsedFiles;
156 UnparsedFilesMap UnparsedFiles;
157
158public:
159 /// Create a new verifying diagnostic client, which will issue errors to
160 /// the currently-attached diagnostic client when a diagnostic does not match
161 /// what is expected (as indicated in the source file).
162 VerifyDiagnosticConsumer(DiagnosticsEngine &Diags);
163 ~VerifyDiagnosticConsumer() override;
164
165 void BeginSourceFile(const LangOptions &LangOpts,
166 const Preprocessor *PP) override;
167
168 void EndSourceFile() override;
169
171 /// File has been processed via HandleComment.
173
174 /// File has diagnostics and may have directives.
176
177 /// File has diagnostics but guaranteed no directives.
179 };
180
181 /// Update lists of parsed and unparsed files.
183
184 bool HandleComment(Preprocessor &PP, SourceRange Comment) override;
185
187 const Diagnostic &Info) override;
188};
189
190} // namespace clang
191
192#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:231
Level
The level of the diagnostic, after it has been through mapping.
Definition Diagnostic.h:236
Cached information about one file (either on disk or in the virtual file system).
Definition FileEntry.h:306
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.
Directive(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, StringRef Spelling, bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text, unsigned Min, unsigned Max)
virtual bool isValid(std::string &Error)=0
static std::unique_ptr< Directive > create(bool RegexKind, SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, StringRef Spelling, bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text, unsigned Min, unsigned Max)
static const unsigned MaxCount
Constant representing n or more matches.
virtual bool match(StringRef S)=0
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
ExpectedData - owns directive objects and deletes on destructor.