clang-tools 19.0.0git
Classes | Public Types | Public Member Functions | Protected Member Functions | List of all members
clang::tidy::RenamerClangTidyCheck Class Referenceabstract

Base class for clang-tidy checks that want to flag declarations and/or macros for renaming based on customizable criteria. More...

#include <RenamerClangTidyCheck.h>

Inheritance diagram for clang::tidy::RenamerClangTidyCheck:
Inheritance graph
[legend]

Classes

struct  DiagInfo
 Represents customized diagnostic text and how arguments should be applied. More...
 
struct  FailureInfo
 Information describing a failed check. More...
 
struct  NamingCheckFailure
 Holds an identifier name check failure, tracking the kind of the identifier, its possible fixup and the starting locations of all the identifier usages. More...
 

Public Types

enum class  ShouldFixStatus {
  ShouldFix , ConflictsWithKeyword , ConflictsWithMacroDefinition , FixInvalidIdentifier ,
  IgnoreFailureThreshold , InsideMacro
}
 This enum will be used in select of the diagnostic message. More...
 
using NamingCheckId = std::pair< SourceLocation, StringRef >
 
using NamingCheckFailureMap = llvm::DenseMap< NamingCheckId, NamingCheckFailure >
 

Public Member Functions

 RenamerClangTidyCheck (StringRef CheckName, ClangTidyContext *Context)
 
 ~RenamerClangTidyCheck ()
 
void registerMatchers (ast_matchers::MatchFinder *Finder) final
 Derived classes should not implement any matching logic themselves; this class will do the matching and call the derived class' getDeclFailureInfo() and getMacroFailureInfo() for determining whether a given identifier passes or fails the check.
 
void check (const ast_matchers::MatchFinder::MatchResult &Result) final
 ClangTidyChecks that register ASTMatchers should do the actual work in here.
 
void registerPPCallbacks (const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) final
 Override this to register PPCallbacks in the preprocessor.
 
void onEndOfTranslationUnit () final
 
void storeOptions (ClangTidyOptions::OptionMap &Opts) override
 Derived classes that override this function should call this method from the overridden method.
 
void checkMacro (const SourceManager &SourceMgr, const Token &MacroNameTok, const MacroInfo *MI)
 Check Macros for style violations.
 
void expandMacro (const Token &MacroNameTok, const MacroInfo *MI)
 Add a usage of a macro if it already has a violation.
 
void addUsage (const RenamerClangTidyCheck::NamingCheckId &Decl, SourceRange Range, const SourceManager *SourceMgr=nullptr)
 
void addUsage (const NamedDecl *Decl, SourceRange Range, const SourceManager *SourceMgr=nullptr)
 Convenience method when the usage to be added is a NamedDecl.
 
void checkNamedDecl (const NamedDecl *Decl, const SourceManager &SourceMgr)
 
- Public Member Functions inherited from clang::tidy::ClangTidyCheck
 ClangTidyCheck (StringRef CheckName, ClangTidyContext *Context)
 Initializes the check with CheckName and Context.
 
virtual bool isLanguageVersionSupported (const LangOptions &LangOpts) const
 Override this to disable registering matchers and PP callbacks if an invalid language version is being used.
 
virtual void registerPPCallbacks (const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP)
 Override this to register PPCallbacks in the preprocessor.
 
virtual void registerMatchers (ast_matchers::MatchFinder *Finder)
 Override this to register AST matchers with Finder.
 
virtual void check (const ast_matchers::MatchFinder::MatchResult &Result)
 ClangTidyChecks that register ASTMatchers should do the actual work in here.
 
DiagnosticBuilder diag (SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
 Add a diagnostic with the check's name.
 
DiagnosticBuilder diag (StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
 Add a diagnostic with the check's name.
 
DiagnosticBuilder configurationDiag (StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
 Adds a diagnostic to report errors in the check's configuration.
 
virtual void storeOptions (ClangTidyOptions::OptionMap &Options)
 Should store all options supported by this check with their current values or default values for options that haven't been overridden.
 

Protected Member Functions

virtual std::optional< FailureInfogetDeclFailureInfo (const NamedDecl *Decl, const SourceManager &SM) const =0
 Overridden by derived classes, returns information about if and how a Decl failed the check.
 
virtual std::optional< FailureInfogetMacroFailureInfo (const Token &MacroNameTok, const SourceManager &SM) const =0
 Overridden by derived classes, returns information about if and how a macro failed the check.
 
virtual DiagInfo getDiagInfo (const NamingCheckId &ID, const NamingCheckFailure &Failure) const =0
 Overridden by derived classes, returns a description of the diagnostic that should be emitted for the given failure.
 
- Protected Member Functions inherited from clang::tidy::ClangTidyCheck
StringRef getCurrentMainFile () const
 Returns the main file name of the current translation unit.
 
const LangOptions & getLangOpts () const
 Returns the language options from the context.
 
bool areDiagsSelfContained () const
 Returns true when the check is run in a use case when only 1 fix will be applied at a time.
 
StringRef getID () const override
 

Additional Inherited Members

- Protected Attributes inherited from clang::tidy::ClangTidyCheck
OptionsView Options
 

Detailed Description

Base class for clang-tidy checks that want to flag declarations and/or macros for renaming based on customizable criteria.

Definition at line 28 of file RenamerClangTidyCheck.h.

Member Typedef Documentation

◆ NamingCheckFailureMap

Definition at line 107 of file RenamerClangTidyCheck.h.

◆ NamingCheckId

using clang::tidy::RenamerClangTidyCheck::NamingCheckId = std::pair<SourceLocation, StringRef>

Definition at line 105 of file RenamerClangTidyCheck.h.

Member Enumeration Documentation

◆ ShouldFixStatus

This enum will be used in select of the diagnostic message.

Each value below IgnoreFailureThreshold should have an error message.

Enumerator
ShouldFix 
ConflictsWithKeyword 

The fixup will conflict with a language keyword, so we can't fix it automatically.

ConflictsWithMacroDefinition 

The fixup will conflict with a macro definition, so we can't fix it automatically.

FixInvalidIdentifier 

The fixup results in an identifier that is not a valid c/c++ identifier.

IgnoreFailureThreshold 

Values pass this threshold will be ignored completely i.e no message, no fixup.

InsideMacro 

If the identifier was used or declared within a macro we won't offer a fixup for safety reasons.

Definition at line 49 of file RenamerClangTidyCheck.h.

Constructor & Destructor Documentation

◆ RenamerClangTidyCheck()

clang::tidy::RenamerClangTidyCheck::RenamerClangTidyCheck ( StringRef  CheckName,
ClangTidyContext Context 
)

Definition at line 378 of file RenamerClangTidyCheck.cpp.

◆ ~RenamerClangTidyCheck()

clang::tidy::RenamerClangTidyCheck::~RenamerClangTidyCheck ( )
default

Member Function Documentation

◆ addUsage() [1/2]

void clang::tidy::RenamerClangTidyCheck::addUsage ( const NamedDecl *  Decl,
SourceRange  Range,
const SourceManager *  SourceMgr = nullptr 
)

Convenience method when the usage to be added is a NamedDecl.

Definition at line 434 of file RenamerClangTidyCheck.cpp.

References addUsage(), Decl, clang::tidy::getOverrideMethod(), and Range.

◆ addUsage() [2/2]

void clang::tidy::RenamerClangTidyCheck::addUsage ( const RenamerClangTidyCheck::NamingCheckId Decl,
SourceRange  Range,
const SourceManager *  SourceMgr = nullptr 
)

◆ check()

void clang::tidy::RenamerClangTidyCheck::check ( const ast_matchers::MatchFinder::MatchResult &  Result)
finalvirtual

ClangTidyChecks that register ASTMatchers should do the actual work in here.

Reimplemented from clang::tidy::ClangTidyCheck.

Definition at line 479 of file RenamerClangTidyCheck.cpp.

◆ checkMacro()

void clang::tidy::RenamerClangTidyCheck::checkMacro ( const SourceManager &  SourceMgr,
const Token &  MacroNameTok,
const MacroInfo *  MI 
)

◆ checkNamedDecl()

void clang::tidy::RenamerClangTidyCheck::checkNamedDecl ( const NamedDecl *  Decl,
const SourceManager &  SourceMgr 
)

◆ expandMacro()

void clang::tidy::RenamerClangTidyCheck::expandMacro ( const Token &  MacroNameTok,
const MacroInfo *  MI 
)

Add a usage of a macro if it already has a violation.

Definition at line 505 of file RenamerClangTidyCheck.cpp.

References addUsage(), ID, Name, and Range.

◆ getDeclFailureInfo()

virtual std::optional< FailureInfo > clang::tidy::RenamerClangTidyCheck::getDeclFailureInfo ( const NamedDecl *  Decl,
const SourceManager &  SM 
) const
protectedpure virtual

Overridden by derived classes, returns information about if and how a Decl failed the check.

A 'std::nullopt' result means the Decl did not fail the check.

Referenced by checkNamedDecl().

◆ getDiagInfo()

virtual DiagInfo clang::tidy::RenamerClangTidyCheck::getDiagInfo ( const NamingCheckId ID,
const NamingCheckFailure Failure 
) const
protectedpure virtual

Overridden by derived classes, returns a description of the diagnostic that should be emitted for the given failure.

The base class will then further customize the diagnostic by adding info about whether the fix-it can be automatically applied or not.

Referenced by onEndOfTranslationUnit().

◆ getMacroFailureInfo()

virtual std::optional< FailureInfo > clang::tidy::RenamerClangTidyCheck::getMacroFailureInfo ( const Token &  MacroNameTok,
const SourceManager &  SM 
) const
protectedpure virtual

Overridden by derived classes, returns information about if and how a macro failed the check.

A 'std::nullopt' result means the macro did not fail the check.

Referenced by checkMacro().

◆ onEndOfTranslationUnit()

void clang::tidy::RenamerClangTidyCheck::onEndOfTranslationUnit ( )
final

◆ registerMatchers()

void clang::tidy::RenamerClangTidyCheck::registerMatchers ( ast_matchers::MatchFinder *  Finder)
finalvirtual

Derived classes should not implement any matching logic themselves; this class will do the matching and call the derived class' getDeclFailureInfo() and getMacroFailureInfo() for determining whether a given identifier passes or fails the check.

Reimplemented from clang::tidy::ClangTidyCheck.

Definition at line 390 of file RenamerClangTidyCheck.cpp.

◆ registerPPCallbacks()

void clang::tidy::RenamerClangTidyCheck::registerPPCallbacks ( const SourceManager &  SM,
Preprocessor *  PP,
Preprocessor *  ModuleExpanderPP 
)
finalvirtual

Override this to register PPCallbacks in the preprocessor.

This should be used for clang-tidy checks that analyze preprocessor- dependent properties, e.g. include directives and macro definitions.

This will only be executed if the function isLanguageVersionSupported returns true.

There are two Preprocessors to choose from that differ in how they handle modular #includes:

  • PP is the real Preprocessor. It doesn't walk into modular #includes and thus doesn't generate PPCallbacks for their contents.
  • ModuleExpanderPP preprocesses the whole translation unit in the non-modular mode, which allows it to generate PPCallbacks not only for the main file and textual headers, but also for all transitively included modular headers when the analysis runs with modules enabled. When modules are not enabled ModuleExpanderPP just points to the real preprocessor.

Reimplemented from clang::tidy::ClangTidyCheck.

Definition at line 394 of file RenamerClangTidyCheck.cpp.

◆ storeOptions()

void clang::tidy::RenamerClangTidyCheck::storeOptions ( ClangTidyOptions::OptionMap Opts)
overridevirtual

Derived classes that override this function should call this method from the overridden method.

Reimplemented from clang::tidy::ClangTidyCheck.

Definition at line 385 of file RenamerClangTidyCheck.cpp.

References clang::tidy::ClangTidyCheck::Options, and clang::tidy::ClangTidyCheck::OptionsView::store().

Referenced by clang::tidy::bugprone::ReservedIdentifierCheck::storeOptions(), and clang::tidy::readability::IdentifierNamingCheck::storeOptions().


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