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 ,
29 SrcMgr::CharacteristicKind )
override {
30 Inserter->addInclude(FileNameRef,
IsAngled, HashLocation,
31 IncludeToken.getEndLoc());
39 bool SelfContainedDiags)
40 : Style(Style), SelfContainedDiags(SelfContainedDiags) {}
43 assert(PP &&
"PP shouldn't be null");
44 SourceMgr = &PP->getSourceManager();
47 if (!IncludeSorterByFile.empty())
48 IncludeSorterByFile.clear();
49 if (!InsertedHeaders.empty())
50 InsertedHeaders.clear();
51 PP->addPPCallbacks(std::make_unique<IncludeInserterCallback>(
this));
55 assert(SourceMgr &&
"SourceMgr shouldn't be null; did you remember to call "
56 "registerPreprocessor()?");
59 std::unique_ptr<IncludeSorter> &
Entry = IncludeSorterByFile[FileID];
62 Entry = std::make_unique<IncludeSorter>(
64 SourceMgr->getFilename(SourceMgr->getLocForStartOfFile(FileID)), Style);
69std::optional<FixItHint>
71 bool IsAngled = Header.consume_front(
"<");
72 if (
IsAngled != Header.consume_back(
">"))
78 if (!SelfContainedDiags && !InsertedHeaders[FileID].insert(Header).second)
84std::optional<FixItHint>
86 assert(SourceMgr &&
"SourceMgr shouldn't be null; did you remember to call "
87 "registerPreprocessor()?");
92 SourceLocation HashLocation,
93 SourceLocation EndLocation) {
94 assert(SourceMgr &&
"SourceMgr shouldn't be null; did you remember to call "
95 "registerPreprocessor()?");
96 FileID FileID = SourceMgr->getFileID(HashLocation);
bool IsAngled
true if this was an include with angle brackets
void InclusionDirective(SourceLocation HashLocation, const Token &IncludeToken, StringRef FileNameRef, bool IsAngled, CharSourceRange FileNameRange, OptionalFileEntryRef, StringRef, StringRef, const Module *, SrcMgr::CharacteristicKind) override
IncludeInserterCallback(IncludeInserter *Inserter)
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.