clang-tools  14.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)
 Initializes the IncludeInserter using the IncludeStyle Style. More...
 
void registerPreprocessor (Preprocessor *PP)
 Registers this with the Preprocessor PP, must be called before this class is used. More...
 
llvm::Optional< FixItHint > createIncludeInsertion (FileID FileID, llvm::StringRef Header)
 Creates a Header inclusion directive fixit in the File FileID. More...
 
llvm::Optional< FixItHint > createMainFileIncludeInsertion (llvm::StringRef Header)
 Creates a Header inclusion directive fixit in the main file. More...
 
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

Definition at line 55 of file IncludeInserter.h.

Constructor & Destructor Documentation

◆ IncludeInserter()

clang::tidy::utils::IncludeInserter::IncludeInserter ( IncludeSorter::IncludeStyle  Style)
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()

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

Creates a Header inclusion directive fixit in the File FileID.

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

Definition at line 70 of file IncludeInserter.cpp.

References clang::tidy::utils::IncludeSorter::CreateIncludeInsertion(), and IsAngled.

Referenced by createMainFileIncludeInsertion().

◆ createMainFileIncludeInsertion()

llvm::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 llvm::None on error or if the inclusion directive already exists.

Definition at line 83 of file IncludeInserter.cpp.

References createIncludeInsertion().

◆ getStyle()

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

Definition at line 84 of file IncludeInserter.h.

◆ registerPreprocessor()

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

Friends And Related Function Documentation

◆ IncludeInserterCallback

friend class IncludeInserterCallback
friend

Definition at line 96 of file IncludeInserter.h.


The documentation for this class was generated from the following files:
clang::tidy::utils::IncludeSorter::IS_Google
@ IS_Google
Definition: IncludeSorter.h:26
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
clang::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:253
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
SM
const SourceManager & SM
Definition: IncludeCleaner.cpp:127