10#include "clang/Lex/PPCallbacks.h"
11#include "clang/Lex/Preprocessor.h"
12#include "clang/Lex/Token.h"
20 : Inserter(Inserter) {}
24 const Token &IncludeToken, StringRef FileNameRef,
25 bool IsAngled, CharSourceRange FileNameRange,
26 OptionalFileEntryRef ,
27 StringRef , StringRef ,
30 SrcMgr::CharacteristicKind )
override {
31 Inserter->addInclude(FileNameRef,
IsAngled, HashLocation,
32 IncludeToken.getEndLoc());
40 bool SelfContainedDiags)
41 : Style(Style), SelfContainedDiags(SelfContainedDiags) {}
44 assert(PP &&
"PP shouldn't be null");
45 SourceMgr = &PP->getSourceManager();
48 if (!IncludeSorterByFile.empty())
49 IncludeSorterByFile.clear();
50 if (!InsertedHeaders.empty())
51 InsertedHeaders.clear();
52 PP->addPPCallbacks(std::make_unique<IncludeInserterCallback>(
this));
56 assert(SourceMgr &&
"SourceMgr shouldn't be null; did you remember to call "
57 "registerPreprocessor()?");
60 std::unique_ptr<IncludeSorter> &
Entry = IncludeSorterByFile[FileID];
63 Entry = std::make_unique<IncludeSorter>(
65 SourceMgr->getFilename(SourceMgr->getLocForStartOfFile(FileID)), Style);
70std::optional<FixItHint>
72 bool IsAngled = Header.consume_front(
"<");
73 if (
IsAngled != Header.consume_back(
">"))
79 if (!SelfContainedDiags && !InsertedHeaders[FileID].insert(Header).second)
85std::optional<FixItHint>
87 assert(SourceMgr &&
"SourceMgr shouldn't be null; did you remember to call "
88 "registerPreprocessor()?");
93 SourceLocation HashLocation,
94 SourceLocation EndLocation) {
95 assert(SourceMgr &&
"SourceMgr shouldn't be null; did you remember to call "
96 "registerPreprocessor()?");
97 FileID FileID = SourceMgr->getFileID(HashLocation);
bool IsAngled
true if this was an include with angle brackets
IncludeInserterCallback(IncludeInserter *Inserter)
void InclusionDirective(SourceLocation HashLocation, const Token &IncludeToken, StringRef FileNameRef, bool IsAngled, CharSourceRange FileNameRange, OptionalFileEntryRef, StringRef, StringRef, const Module *, bool, SrcMgr::CharacteristicKind) override
Produces fixes to insert specified includes to source files, if not yet present.
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.
Class used by IncludeInserterCallback to record the names of the inclusions in a given source file be...
void addInclude(StringRef FileName, bool IsAngled, SourceLocation HashLocation, SourceLocation EndLocation)
Adds the given include directive to the sorter.
IncludeStyle
Supported include styles.
std::optional< FixItHint > createIncludeInsertion(StringRef FileName, bool IsAngled)
Creates a quoted inclusion directive in the right sort order.