clang-tools 19.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)

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: