9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
16#include "clang/Basic/Diagnostic.h"
17#include "clang/Tooling/Core/Diagnostic.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/StringSet.h"
20#include "llvm/Support/Regex.h"
40 ClangTidyError(StringRef CheckName, Level DiagLevel, StringRef BuildDirectory,
83 DiagnosticsEngine *DiagEngine) {
84 this->DiagOpts = std::move(DiagOpts);
85 this->DiagEngine = DiagEngine;
98 DiagnosticBuilder
diag(StringRef CheckName, SourceLocation Loc,
99 StringRef Description,
100 DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
102 DiagnosticBuilder
diag(StringRef CheckName, StringRef Description,
103 DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
105 DiagnosticBuilder
diag(
const tooling::Diagnostic &Error);
110 DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
128 const Diagnostic &Info,
129 SmallVectorImpl<tooling::Diagnostic> &NoLintErrors,
130 bool AllowIO =
true,
bool EnableNoLintBlocks =
true);
175 return HeaderFileExtensions;
179 return ImplementationFileExtensions;
192 std::optional<ClangTidyProfiling::StorageParams>
197 CurrentBuildDirectory = std::string(BuildDirectory);
202 return CurrentBuildDirectory;
208 return AllowEnablingAnalyzerAlphaCheckers;
214 return EnableModuleHeadersParsing;
227 SourceLocation Loc) {
228 return {
static_cast<DiagnosticIDs::Level
>(
229 DiagEngine->getDiagnosticLevel(DiagnosticID, Loc)),
231 DiagEngine->getDiagnosticIDs()->getDescription(DiagnosticID))};
235 OptionsCollector = Collector;
243 std::unique_ptr<DiagnosticOptions> DiagOpts =
nullptr;
244 DiagnosticsEngine *DiagEngine =
nullptr;
245 std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider;
247 std::string CurrentFile;
250 std::unique_ptr<CachedGlobList> CheckFilter;
251 std::unique_ptr<CachedGlobList> WarningAsErrorFilter;
256 LangOptions LangOpts;
260 std::string CurrentBuildDirectory;
262 llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID;
264 bool Profile =
false;
265 std::string ProfilePrefix;
267 bool AllowEnablingAnalyzerAlphaCheckers;
268 bool EnableModuleHeadersParsing;
269 bool ExperimentalCustomChecks;
271 bool SelfContainedDiags =
false;
274 llvm::StringSet<> *OptionsCollector =
nullptr;
281const llvm::StringMap<tooling::Replacements> *
282getFixIt(
const tooling::Diagnostic &Diagnostic,
bool AnyFix);
293 DiagnosticsEngine *ExternalDiagEngine =
nullptr,
294 bool RemoveIncompatibleErrors =
true,
295 bool GetFixesFromNotes =
false,
296 bool EnableNolintBlocks =
true);
302 const Diagnostic &Info)
override;
305 const Preprocessor *PP =
nullptr)
override;
310 std::vector<ClangTidyError>
take();
313 void finalizeLastError();
314 void removeIncompatibleErrors();
315 void removeDuplicatedDiagnosticsOfAliasCheckers();
319 llvm::Regex *getHeaderFilter();
323 llvm::Regex *getExcludeHeaderFilter();
327 void checkFilters(SourceLocation
Location,
const SourceManager &Sources);
328 bool passesLineFilter(StringRef FileName,
unsigned LineNumber)
const;
330 void forwardDiagnostic(
const Diagnostic &Info);
333 DiagnosticsEngine *ExternalDiagEngine;
334 bool RemoveIncompatibleErrors;
335 bool GetFixesFromNotes;
336 bool EnableNolintBlocks;
337 std::vector<ClangTidyError> Errors;
338 std::unique_ptr<llvm::Regex> HeaderFilter;
339 std::unique_ptr<llvm::Regex> ExcludeHeaderFilter;
340 bool LastErrorRelatesToUserCode =
false;
341 bool LastErrorPassesLineFilter =
false;
342 bool LastErrorWasIgnored =
false;
347 bool InSourceFile =
false;
static cl::opt< bool > ExperimentalCustomChecks("experimental-custom-checks", desc(R"(
Enable experimental clang-query based
custom checks.
see https://clang.llvm.org/extra/clang-tidy/QueryBasedCustomChecks.html.
)"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< bool > AllowEnablingAnalyzerAlphaCheckers("allow-enabling-analyzer-alpha-checkers", cl::init(false), cl::Hidden, cl::cat(ClangTidyCategory))
This option allows enabling the experimental alpha checkers from the static analyzer.
static cl::opt< bool > EnableModuleHeadersParsing("enable-module-headers-parsing", desc(R"(
Enables preprocessor-level module header parsing
for C++20 and above, empowering specific checks
to detect macro definitions within modules. This
feature may cause performance and parsing issues
and is therefore considered experimental.
)"), cl::init(false), cl::cat(ClangTidyCategory))
A GlobList that caches search results, so that search is performed only once for the same query.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
ClangTidyContext(const ClangTidyContext &)=delete
const ClangTidyStats & getStats() const
Returns ClangTidyStats containing issued and ignored diagnostic counters.
bool canEnableAnalyzerAlphaCheckers() const
If the experimental alpha checkers from the static analyzer can be enabled.
ClangTidyContext(std::unique_ptr< ClangTidyOptionsProvider > OptionsProvider)
bool isCheckEnabled(StringRef CheckName) const
Returns true if the check is enabled for the CurrentFile.
std::string getCheckName(unsigned DiagnosticID) const
Returns the name of the clang-tidy check which produced this diagnostic ID.
void setSelfContainedDiags(bool Value)
void setOptionsCollector(llvm::StringSet<> *Collector)
bool areDiagsSelfContained() const
bool canEnableModuleHeadersParsing() const
const ClangTidyOptions & getOptions() const
Returns options for CurrentFile.
llvm::StringSet * getOptionsCollector() const
const std::string & getCurrentBuildDirectory() const
Returns build directory of the current translation unit.
void setCurrentBuildDirectory(StringRef BuildDirectory)
Should be called when starting to process new translation unit.
const LangOptions & getLangOpts() const
Gets the language options from the AST context.
bool getEnableProfiling() const
DiagnosticBuilder configurationDiag(StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors to do with reading the configuration using this method.
void setASTContext(ASTContext *Context)
Sets ASTContext for the current translation unit.
const FileExtensionsSet & getHeaderFileExtensions() const
void setProfileStoragePrefix(StringRef ProfilePrefix)
Control storage of profile date.
bool canExperimentalCustomChecks() const
void setEnableProfiling(bool Profile)
Control profile collection in clang-tidy.
DiagLevelAndFormatString getDiagLevelAndFormatString(unsigned DiagnosticID, SourceLocation Loc)
std::pair< DiagnosticIDs::Level, std::string > DiagLevelAndFormatString
void setCurrentFile(StringRef File)
Should be called when starting to process new translation unit.
bool shouldSuppressDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info, SmallVectorImpl< tooling::Diagnostic > &NoLintErrors, bool AllowIO=true, bool EnableNoLintBlocks=true)
Check whether a given diagnostic should be suppressed due to the presence of a "NOLINT" suppression c...
bool treatAsError(StringRef CheckName) const
Returns true if the check should be upgraded to error for the CurrentFile.
DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors detected using this method.
void setDiagnosticsEngine(std::unique_ptr< DiagnosticOptions > DiagOpts, DiagnosticsEngine *DiagEngine)
Sets the DiagnosticsEngine that diag() will emit diagnostics to.
ClangTidyContext & operator=(const ClangTidyContext &)=delete
ClangTidyOptions getOptionsForFile(StringRef File) const
Returns options for File.
std::optional< ClangTidyProfiling::StorageParams > getProfileStorageParams() const
friend class ClangTidyDiagnosticConsumer
StringRef getCurrentFile() const
Returns the main file name of the current translation unit.
const ClangTidyGlobalOptions & getGlobalOptions() const
Returns global options.
void setSourceManager(SourceManager *SourceMgr)
Sets the SourceManager of the used DiagnosticsEngine.
const FileExtensionsSet & getImplementationFileExtensions() const
ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx, DiagnosticsEngine *ExternalDiagEngine=nullptr, bool RemoveIncompatibleErrors=true, bool GetFixesFromNotes=false, bool EnableNolintBlocks=true)
void EndSourceFile() override
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override
void BeginSourceFile(const LangOptions &LangOpts, const Preprocessor *PP=nullptr) override
std::vector< ClangTidyError > take()
This class is used to locate NOLINT comments in the file being analyzed, to decide whether a diagnost...
const llvm::StringMap< tooling::Replacements > * getFixIt(const tooling::Diagnostic &Diagnostic, bool AnyFix)
Gets the Fix attached to Diagnostic.
llvm::SmallSet< llvm::StringRef, 5 > FileExtensionsSet
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
ClangTidyError(StringRef CheckName, Level DiagLevel, StringRef BuildDirectory, bool IsWarningAsError)
std::vector< std::string > EnabledDiagnosticAliases
Contains options for clang-tidy.
Contains displayed and ignored diagnostic counters for a ClangTidy run.
unsigned ErrorsIgnoredCheckFilter
unsigned ErrorsIgnoredNonUserCode
unsigned ErrorsIgnoredLineFilter
unsigned ErrorsIgnoredNOLINT
unsigned errorsIgnored() const