clang-tools 20.0.0git
Public Member Functions | Friends | List of all members
clang::tidy::utils::IncludeInserter Class Reference

Produces fixes to insert specified includes to source files, if not yet present. More...

#include <IncludeInserter.h>

Public Member Functions

 IncludeInserter (IncludeSorter::IncludeStyle Style, bool SelfContainedDiags)
 Initializes the IncludeInserter using the IncludeStyle Style.
 
void registerPreprocessor (Preprocessor *PP)
 Registers this with the Preprocessor PP, must be called before this class is used.
 
std::optional< FixItHint > createIncludeInsertion (FileID FileID, llvm::StringRef Header)
 Creates a Header inclusion directive fixit in the File FileID.
 
std::optional< FixItHint > createMainFileIncludeInsertion (llvm::StringRef Header)
 Creates a Header inclusion directive fixit in the main file.
 
IncludeSorter::IncludeStyle getStyle () const
 

Friends

class IncludeInserterCallback
 

Detailed Description

Produces fixes to insert specified includes to source files, if not yet present.

IncludeInserter can be used in clang-tidy checks in the following way:

#include "../ClangTidyCheck.h"
#include "../utils/IncludeInserter.h"
namespace clang {
namespace tidy {
class MyCheck : public ClangTidyCheck {
public:
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
Preprocessor *ModuleExpanderPP) override {
Inserter.registerPreprocessor(PP);
}
void registerMatchers(ast_matchers::MatchFinder* Finder) override { ... }
void check(
const ast_matchers::MatchFinder::MatchResult& Result) override {
...
Inserter.createMainFileIncludeInsertion("path/to/Header.h");
...
}
private:
utils::IncludeInserter Inserter{utils::IncludeSorter::IS_Google};
};
} // namespace tidy
} // namespace clang
Base class for all clang-tidy checks.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//

Definition at line 55 of file IncludeInserter.h.

Constructor & Destructor Documentation

◆ IncludeInserter()

clang::tidy::utils::IncludeInserter::IncludeInserter ( IncludeSorter::IncludeStyle  Style,
bool  SelfContainedDiags 
)
explicit

Initializes the IncludeInserter using the IncludeStyle Style.

In most cases the Style will be retrieved from the ClangTidyOptions using

Options.getLocalOrGlobal("IncludeStyle", <DefaultStyle>)

Definition at line 39 of file IncludeInserter.cpp.

Member Function Documentation

◆ createIncludeInsertion()

std::optional< FixItHint > clang::tidy::utils::IncludeInserter::createIncludeInsertion ( FileID  FileID,
llvm::StringRef  Header 
)

◆ createMainFileIncludeInsertion()

std::optional< FixItHint > clang::tidy::utils::IncludeInserter::createMainFileIncludeInsertion ( llvm::StringRef  Header)

Creates a Header inclusion directive fixit in the main file.

When Header is enclosed in angle brackets, uses angle brackets in the inclusion directive, otherwise uses quotes. Returns std::nullopt on error or if the inclusion directive already exists.

Definition at line 86 of file IncludeInserter.cpp.

References createIncludeInsertion().

Referenced by clang::tidy::cppcoreguidelines::ProBoundsConstantArrayIndexCheck::check(), and clang::tidy::modernize::ReplaceAutoPtrCheck::check().

◆ getStyle()

IncludeSorter::IncludeStyle clang::tidy::utils::IncludeInserter::getStyle ( ) const
inline

◆ registerPreprocessor()

void clang::tidy::utils::IncludeInserter::registerPreprocessor ( Preprocessor *  PP)

Registers this with the Preprocessor PP, must be called before this class is used.

Definition at line 43 of file IncludeInserter.cpp.

Referenced by clang::tidy::utils::UseRangesCheck::registerPPCallbacks(), clang::tidy::abseil::StringFindStartswithCheck::registerPPCallbacks(), clang::tidy::bugprone::ImplicitWideningOfMultiplicationResultCheck::registerPPCallbacks(), clang::tidy::cppcoreguidelines::InitVariablesCheck::registerPPCallbacks(), clang::tidy::cppcoreguidelines::ProBoundsConstantArrayIndexCheck::registerPPCallbacks(), clang::tidy::misc::UniqueptrResetReleaseCheck::registerPPCallbacks(), clang::tidy::modernize::LoopConvertCheck::registerPPCallbacks(), clang::tidy::modernize::MakeSmartPtrCheck::registerPPCallbacks(), clang::tidy::modernize::MinMaxUseInitializerListCheck::registerPPCallbacks(), clang::tidy::modernize::PassByValueCheck::registerPPCallbacks(), clang::tidy::modernize::ReplaceAutoPtrCheck::registerPPCallbacks(), clang::tidy::modernize::ReplaceRandomShuffleCheck::registerPPCallbacks(), clang::tidy::modernize::UseIntegerSignComparisonCheck::registerPPCallbacks(), clang::tidy::modernize::UseStdFormatCheck::registerPPCallbacks(), clang::tidy::modernize::UseStdNumbersCheck::registerPPCallbacks(), clang::tidy::modernize::UseStdPrintCheck::registerPPCallbacks(), clang::tidy::performance::TypePromotionInMathFnCheck::registerPPCallbacks(), clang::tidy::performance::UnnecessaryValueParamCheck::registerPPCallbacks(), clang::tidy::readability::UseStdMinMaxCheck::registerPPCallbacks(), and clang::tidy::utils::TransformerClangTidyCheck::registerPPCallbacks().

Friends And Related Function Documentation

◆ IncludeInserterCallback

friend class IncludeInserterCallback
friend

Definition at line 98 of file IncludeInserter.h.


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