clang-tools 19.0.0git
Public Types | Public Member Functions | Friends | List of all members
clang::tidy::ClangTidyContext Class Reference

Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context. More...

#include <ClangTidyDiagnosticConsumer.h>

Public Types

using DiagLevelAndFormatString = std::pair< DiagnosticIDs::Level, std::string >
 

Public Member Functions

 ClangTidyContext (std::unique_ptr< ClangTidyOptionsProvider > OptionsProvider, bool AllowEnablingAnalyzerAlphaCheckers=false, bool EnableModuleHeadersParsing=false)
 Initializes ClangTidyContext instance.
 
void setDiagnosticsEngine (DiagnosticsEngine *DiagEngine)
 Sets the DiagnosticsEngine that diag() will emit diagnostics to.
 
 ~ClangTidyContext ()
 
DiagnosticBuilder diag (StringRef CheckName, SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
 Report any errors detected using this method.
 
DiagnosticBuilder diag (StringRef CheckName, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
 
DiagnosticBuilder diag (const tooling::Diagnostic &Error)
 
DiagnosticBuilder configurationDiag (StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
 Report any errors to do with reading the configuration using this method.
 
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 comment.
 
void setSourceManager (SourceManager *SourceMgr)
 Sets the SourceManager of the used DiagnosticsEngine.
 
void setCurrentFile (StringRef File)
 Should be called when starting to process new translation unit.
 
StringRef getCurrentFile () const
 Returns the main file name of the current translation unit.
 
void setASTContext (ASTContext *Context)
 Sets ASTContext for the current translation unit.
 
const LangOptions & getLangOpts () const
 Gets the language options from the AST context.
 
std::string getCheckName (unsigned DiagnosticID) const
 Returns the name of the clang-tidy check which produced this diagnostic ID.
 
bool isCheckEnabled (StringRef CheckName) const
 Returns true if the check is enabled for the CurrentFile.
 
bool treatAsError (StringRef CheckName) const
 Returns true if the check should be upgraded to error for the CurrentFile.
 
const ClangTidyGlobalOptionsgetGlobalOptions () const
 Returns global options.
 
const ClangTidyOptionsgetOptions () const
 Returns options for CurrentFile.
 
ClangTidyOptions getOptionsForFile (StringRef File) const
 Returns options for File.
 
const FileExtensionsSetgetHeaderFileExtensions () const
 
const FileExtensionsSetgetImplementationFileExtensions () const
 
const ClangTidyStatsgetStats () const
 Returns ClangTidyStats containing issued and ignored diagnostic counters.
 
void setEnableProfiling (bool Profile)
 Control profile collection in clang-tidy.
 
bool getEnableProfiling () const
 
void setProfileStoragePrefix (StringRef ProfilePrefix)
 Control storage of profile date.
 
std::optional< ClangTidyProfiling::StorageParamsgetProfileStorageParams () const
 
void setCurrentBuildDirectory (StringRef BuildDirectory)
 Should be called when starting to process new translation unit.
 
const std::string & getCurrentBuildDirectory () const
 Returns build directory of the current translation unit.
 
bool canEnableAnalyzerAlphaCheckers () const
 If the experimental alpha checkers from the static analyzer can be enabled.
 
bool canEnableModuleHeadersParsing () const
 
void setSelfContainedDiags (bool Value)
 
bool areDiagsSelfContained () const
 
DiagLevelAndFormatString getDiagLevelAndFormatString (unsigned DiagnosticID, SourceLocation Loc)
 
void setOptionsCollector (llvm::StringSet<> *Collector)
 
llvm::StringSet * getOptionsCollector () const
 

Friends

class ClangTidyDiagnosticConsumer
 

Detailed Description

Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.

A ClangTidyCheck always has access to the active context to report warnings like:

Context->Diag(Loc, "Single-argument constructors must be explicit")
<< FixItHint::CreateInsertion(Loc, "explicit ");
SourceLocation Loc

Definition at line 69 of file ClangTidyDiagnosticConsumer.h.

Member Typedef Documentation

◆ DiagLevelAndFormatString

using clang::tidy::ClangTidyContext::DiagLevelAndFormatString = std::pair<DiagnosticIDs::Level, std::string>

Definition at line 212 of file ClangTidyDiagnosticConsumer.h.

Constructor & Destructor Documentation

◆ ClangTidyContext()

ClangTidyContext::ClangTidyContext ( std::unique_ptr< ClangTidyOptionsProvider OptionsProvider,
bool  AllowEnablingAnalyzerAlphaCheckers = false,
bool  EnableModuleHeadersParsing = false 
)

Initializes ClangTidyContext instance.

Definition at line 161 of file ClangTidyDiagnosticConsumer.cpp.

References setCurrentFile().

◆ ~ClangTidyContext()

ClangTidyContext::~ClangTidyContext ( )
default

Member Function Documentation

◆ areDiagsSelfContained()

bool clang::tidy::ClangTidyContext::areDiagsSelfContained ( ) const
inline

Definition at line 210 of file ClangTidyDiagnosticConsumer.h.

◆ canEnableAnalyzerAlphaCheckers()

bool clang::tidy::ClangTidyContext::canEnableAnalyzerAlphaCheckers ( ) const
inline

If the experimental alpha checkers from the static analyzer can be enabled.

Definition at line 198 of file ClangTidyDiagnosticConsumer.h.

◆ canEnableModuleHeadersParsing()

bool clang::tidy::ClangTidyContext::canEnableModuleHeadersParsing ( ) const
inline

Definition at line 204 of file ClangTidyDiagnosticConsumer.h.

◆ configurationDiag()

DiagnosticBuilder ClangTidyContext::configurationDiag ( StringRef  Message,
DiagnosticIDs::Level  Level = DiagnosticIDs::Warning 
)

Report any errors to do with reading the configuration using this method.

Definition at line 206 of file ClangTidyDiagnosticConsumer.cpp.

References diag().

Referenced by clang::tidy::ClangTidyCheck::configurationDiag(), and setCurrentFile().

◆ diag() [1/3]

DiagnosticBuilder ClangTidyContext::diag ( const tooling::Diagnostic &  Error)

Definition at line 194 of file ClangTidyDiagnosticConsumer.cpp.

References diag(), Error, ID, and Loc.

◆ diag() [2/3]

DiagnosticBuilder ClangTidyContext::diag ( StringRef  CheckName,
SourceLocation  Loc,
StringRef  Description,
DiagnosticIDs::Level  Level = DiagnosticIDs::Warning 
)

Report any errors detected using this method.

This is still under heavy development and will likely change towards using tablegen'd diagnostic IDs. FIXME: Figure out a way to manage ID spaces.

Definition at line 175 of file ClangTidyDiagnosticConsumer.cpp.

References ID, and Loc.

Referenced by configurationDiag(), diag(), clang::tidy::ClangTidyCheck::diag(), and clang::tidy::ClangTidyDiagnosticConsumer::HandleDiagnostic().

◆ diag() [3/3]

DiagnosticBuilder ClangTidyContext::diag ( StringRef  CheckName,
StringRef  Description,
DiagnosticIDs::Level  Level = DiagnosticIDs::Warning 
)

Definition at line 185 of file ClangTidyDiagnosticConsumer.cpp.

References ID.

◆ getCheckName()

std::string ClangTidyContext::getCheckName ( unsigned  DiagnosticID) const

Returns the name of the clang-tidy check which produced this diagnostic ID.

Definition at line 295 of file ClangTidyDiagnosticConsumer.cpp.

Referenced by clang::tidy::ClangTidyDiagnosticConsumer::HandleDiagnostic(), shouldSuppressDiagnostic(), and clang::clangd::StoreDiags::take().

◆ getCurrentBuildDirectory()

const std::string & clang::tidy::ClangTidyContext::getCurrentBuildDirectory ( ) const
inline

Returns build directory of the current translation unit.

Definition at line 192 of file ClangTidyDiagnosticConsumer.h.

Referenced by clang::tidy::ClangTidyDiagnosticConsumer::HandleDiagnostic().

◆ getCurrentFile()

StringRef clang::tidy::ClangTidyContext::getCurrentFile ( ) const
inline

Returns the main file name of the current translation unit.

Definition at line 132 of file ClangTidyDiagnosticConsumer.h.

◆ getDiagLevelAndFormatString()

DiagLevelAndFormatString clang::tidy::ClangTidyContext::getDiagLevelAndFormatString ( unsigned  DiagnosticID,
SourceLocation  Loc 
)
inline

Definition at line 213 of file ClangTidyDiagnosticConsumer.h.

References Loc.

◆ getEnableProfiling()

bool clang::tidy::ClangTidyContext::getEnableProfiling ( ) const
inline

Definition at line 179 of file ClangTidyDiagnosticConsumer.h.

◆ getGlobalOptions()

const ClangTidyGlobalOptions & ClangTidyContext::getGlobalOptions ( ) const

Returns global options.

Definition at line 256 of file ClangTidyDiagnosticConsumer.cpp.

◆ getHeaderFileExtensions()

const FileExtensionsSet & clang::tidy::ClangTidyContext::getHeaderFileExtensions ( ) const
inline

Definition at line 165 of file ClangTidyDiagnosticConsumer.h.

◆ getImplementationFileExtensions()

const FileExtensionsSet & clang::tidy::ClangTidyContext::getImplementationFileExtensions ( ) const
inline

Definition at line 169 of file ClangTidyDiagnosticConsumer.h.

◆ getLangOpts()

const LangOptions & clang::tidy::ClangTidyContext::getLangOpts ( ) const
inline

Gets the language options from the AST context.

Definition at line 138 of file ClangTidyDiagnosticConsumer.h.

Referenced by clang::tidy::ClangTidyDiagnosticConsumer::HandleDiagnostic().

◆ getOptions()

const ClangTidyOptions & ClangTidyContext::getOptions ( ) const

Returns options for CurrentFile.

The CurrentFile can be changed using setCurrentFile.

Definition at line 260 of file ClangTidyDiagnosticConsumer.cpp.

Referenced by clang::tidy::ClangTidyDiagnosticConsumer::ClangTidyDiagnosticConsumer(), and setCurrentFile().

◆ getOptionsCollector()

llvm::StringSet * clang::tidy::ClangTidyContext::getOptionsCollector ( ) const
inline

◆ getOptionsForFile()

ClangTidyOptions ClangTidyContext::getOptionsForFile ( StringRef  File) const

Returns options for File.

Does not change or depend on CurrentFile.

Definition at line 264 of file ClangTidyDiagnosticConsumer.cpp.

References clang::tidy::ClangTidyOptions::getDefaults(), and clang::tidy::ClangTidyOptions::merge().

Referenced by setCurrentFile().

◆ getProfileStorageParams()

std::optional< ClangTidyProfiling::StorageParams > ClangTidyContext::getProfileStorageParams ( ) const

Definition at line 278 of file ClangTidyDiagnosticConsumer.cpp.

◆ getStats()

const ClangTidyStats & clang::tidy::ClangTidyContext::getStats ( ) const
inline

Returns ClangTidyStats containing issued and ignored diagnostic counters.

Definition at line 175 of file ClangTidyDiagnosticConsumer.h.

◆ isCheckEnabled()

bool ClangTidyContext::isCheckEnabled ( StringRef  CheckName) const

Returns true if the check is enabled for the CurrentFile.

The CurrentFile can be changed using setCurrentFile.

Definition at line 285 of file ClangTidyDiagnosticConsumer.cpp.

◆ setASTContext()

void ClangTidyContext::setASTContext ( ASTContext *  Context)

Sets ASTContext for the current translation unit.

Definition at line 251 of file ClangTidyDiagnosticConsumer.cpp.

◆ setCurrentBuildDirectory()

void clang::tidy::ClangTidyContext::setCurrentBuildDirectory ( StringRef  BuildDirectory)
inline

Should be called when starting to process new translation unit.

Definition at line 187 of file ClangTidyDiagnosticConsumer.h.

◆ setCurrentFile()

void ClangTidyContext::setCurrentFile ( StringRef  File)

Should be called when starting to process new translation unit.

Definition at line 237 of file ClangTidyDiagnosticConsumer.cpp.

References configurationDiag(), getOptions(), getOptionsForFile(), and parseFileExtensions().

Referenced by ClangTidyContext().

◆ setDiagnosticsEngine()

void clang::tidy::ClangTidyContext::setDiagnosticsEngine ( DiagnosticsEngine *  DiagEngine)
inline

Sets the DiagnosticsEngine that diag() will emit diagnostics to.

Definition at line 78 of file ClangTidyDiagnosticConsumer.h.

◆ setEnableProfiling()

void ClangTidyContext::setEnableProfiling ( bool  Profile)

Control profile collection in clang-tidy.

Definition at line 271 of file ClangTidyDiagnosticConsumer.cpp.

◆ setOptionsCollector()

void clang::tidy::ClangTidyContext::setOptionsCollector ( llvm::StringSet<> *  Collector)
inline

Definition at line 222 of file ClangTidyDiagnosticConsumer.h.

◆ setProfileStoragePrefix()

void ClangTidyContext::setProfileStoragePrefix ( StringRef  ProfilePrefix)

Control storage of profile date.

Definition at line 273 of file ClangTidyDiagnosticConsumer.cpp.

◆ setSelfContainedDiags()

void clang::tidy::ClangTidyContext::setSelfContainedDiags ( bool  Value)
inline

Definition at line 208 of file ClangTidyDiagnosticConsumer.h.

◆ setSourceManager()

void ClangTidyContext::setSourceManager ( SourceManager *  SourceMgr)

Sets the SourceManager of the used DiagnosticsEngine.

This is called from the ClangTidyCheck base class.

Definition at line 221 of file ClangTidyDiagnosticConsumer.cpp.

◆ shouldSuppressDiagnostic()

bool ClangTidyContext::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 comment.

This is exposed so that other tools that present clang-tidy diagnostics (such as clangd) can respect the same suppression rules as clang-tidy. This does not handle suppression of notes following a suppressed diagnostic; that is left to the caller as it requires maintaining state in between calls to this function. If any NOLINT is malformed, e.g. a BEGIN without a subsequent END, output

Parameters
NoLintErrorswill return an error about it. If
AllowIOis false, the function does not attempt to read source files from disk which are not already mapped into memory; such files are treated as not containing a suppression comment.
EnableNoLintBlockscontrols whether to honor NOLINTBEGIN/NOLINTEND blocks; if false, only considers line-level disabling.

Definition at line 212 of file ClangTidyDiagnosticConsumer.cpp.

References getCheckName(), Info, and clang::tidy::NoLintDirectiveHandler::shouldSuppress().

Referenced by clang::tidy::ClangTidyDiagnosticConsumer::HandleDiagnostic().

◆ treatAsError()

bool ClangTidyContext::treatAsError ( StringRef  CheckName) const

Returns true if the check should be upgraded to error for the CurrentFile.

Definition at line 290 of file ClangTidyDiagnosticConsumer.cpp.

Referenced by clang::tidy::ClangTidyDiagnosticConsumer::HandleDiagnostic().

Friends And Related Function Documentation

◆ ClangTidyDiagnosticConsumer

friend class ClangTidyDiagnosticConsumer
friend

Definition at line 229 of file ClangTidyDiagnosticConsumer.h.


The documentation for this class was generated from the following files: