clang 19.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;
31class TextDiagnosticBuffer;
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 MatchAnyLine, StringRef Text, unsigned Min, unsigned Max);
49
50 public:
51 /// Constant representing n or more matches.
52 static const unsigned MaxCount = std::numeric_limits<unsigned>::max();
53
56 const std::string Text;
57 unsigned Min, Max;
59 bool MatchAnyFileAndLine; // `MatchAnyFileAndLine` implies `MatchAnyLine`.
60
61 Directive(const Directive &) = delete;
62 Directive &operator=(const Directive &) = delete;
63 virtual ~Directive() = default;
64
65 // Returns true if directive text is valid.
66 // Otherwise returns false and populates E.
67 virtual bool isValid(std::string &Error) = 0;
68
69 // Returns true on match.
70 virtual bool match(StringRef S) = 0;
71
72 protected:
74 bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text,
75 unsigned Min, unsigned Max)
79 assert(!DirectiveLoc.isInvalid() && "DirectiveLoc is invalid!");
80 assert((!DiagnosticLoc.isInvalid() || MatchAnyLine) &&
81 "DiagnosticLoc is invalid!");
82 }
83 };
84
85 using DirectiveList = std::vector<std::unique_ptr<Directive>>;
86
87 /// ExpectedData - owns directive objects and deletes on destructor.
88 struct ExpectedData {
93
94 void Reset() {
95 Errors.clear();
96 Warnings.clear();
97 Remarks.clear();
98 Notes.clear();
99 }
100 };
101
107 };
108
109 class MarkerTracker;
110
111private:
112 DiagnosticsEngine &Diags;
113 DiagnosticConsumer *PrimaryClient;
114 std::unique_ptr<DiagnosticConsumer> PrimaryClientOwner;
115 std::unique_ptr<TextDiagnosticBuffer> Buffer;
116 std::unique_ptr<MarkerTracker> Markers;
117 const Preprocessor *CurrentPreprocessor = nullptr;
118 const LangOptions *LangOpts = nullptr;
119 SourceManager *SrcManager = nullptr;
120 unsigned ActiveSourceFiles = 0;
121 DirectiveStatus Status;
122 ExpectedData ED;
123
124 void CheckDiagnostics();
125
126 void setSourceManager(SourceManager &SM) {
127 assert((!SrcManager || SrcManager == &SM) && "SourceManager changed!");
128 SrcManager = &SM;
129 }
130
131 // These facilities are used for validation in debug builds.
132 class UnparsedFileStatus {
134 bool FoundDirectives;
135
136 public:
137 UnparsedFileStatus(OptionalFileEntryRef File, bool FoundDirectives)
138 : File(File), FoundDirectives(FoundDirectives) {}
139
140 OptionalFileEntryRef getFile() const { return File; }
141 bool foundDirectives() const { return FoundDirectives; }
142 };
143
144 using ParsedFilesMap = llvm::DenseMap<FileID, const FileEntry *>;
145 using UnparsedFilesMap = llvm::DenseMap<FileID, UnparsedFileStatus>;
146
147 ParsedFilesMap ParsedFiles;
148 UnparsedFilesMap UnparsedFiles;
149
150public:
151 /// Create a new verifying diagnostic client, which will issue errors to
152 /// the currently-attached diagnostic client when a diagnostic does not match
153 /// what is expected (as indicated in the source file).
154 VerifyDiagnosticConsumer(DiagnosticsEngine &Diags);
155 ~VerifyDiagnosticConsumer() override;
156
157 void BeginSourceFile(const LangOptions &LangOpts,
158 const Preprocessor *PP) override;
159
160 void EndSourceFile() override;
161
163 /// File has been processed via HandleComment.
165
166 /// File has diagnostics and may have directives.
168
169 /// File has diagnostics but guaranteed no directives.
171 };
172
173 /// Update lists of parsed and unparsed files.
175
176 bool HandleComment(Preprocessor &PP, SourceRange Comment) override;
177
179 const Diagnostic &Info) override;
180};
181
182} // namespace clang
183
184#endif // LLVM_CLANG_FRONTEND_VERIFYDIAGNOSTICCONSUMER_H
#define SM(sm)
Definition: Cuda.cpp:82
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.
Defines the clang::Preprocessor interface.
Defines the clang::SourceLocation class and associated facilities.
VerifyDiagnosticConsumer::ExpectedData ExpectedData
Abstract base class that describes a handler that will receive source ranges for each of the comments...
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1745
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1571
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:195
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...
Definition: LangOptions.h:454
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128
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 - Abstract class representing a parsed verify directive.
virtual bool isValid(std::string &Error)=0
static std::unique_ptr< Directive > create(bool RegexKind, SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, 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(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, bool MatchAnyFileAndLine, bool MatchAnyLine, StringRef Text, unsigned Min, unsigned Max)
Directive & operator=(const Directive &)=delete
VerifyDiagnosticConsumer - Create a diagnostic client which will use markers in the input source to c...
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:202
ExpectedData - owns directive objects and deletes on destructor.