11 #include "clang/Frontend/CompilerInstance.h"
12 #include "clang/Lex/PPCallbacks.h"
13 #include "clang/Lex/Preprocessor.h"
20 class IncludeOrderPPCallbacks :
public PPCallbacks {
22 explicit IncludeOrderPPCallbacks(ClangTidyCheck &
Check, SourceManager &
SM)
25 void InclusionDirective(SourceLocation HashLoc,
const Token &IncludeTok,
27 CharSourceRange FilenameRange,
const FileEntry *
File,
28 StringRef SearchPath, StringRef RelativePath,
29 const Module *Imported)
override;
30 void EndOfMainFile()
override;
33 struct IncludeDirective {
49 Compiler.getPreprocessor().addPPCallbacks(
50 ::llvm::make_unique<IncludeOrderPPCallbacks>(
51 *
this, Compiler.getSourceManager()));
60 if (Filename.startswith(
"llvm/") || Filename.startswith(
"llvm-c/") ||
61 Filename.startswith(
"clang/") || Filename.startswith(
"clang-c/"))
65 if (IsAngled || Filename.startswith(
"gtest/"))
72 void IncludeOrderPPCallbacks::InclusionDirective(
73 SourceLocation HashLoc,
const Token &IncludeTok, StringRef FileName,
74 bool IsAngled, CharSourceRange FilenameRange,
const FileEntry *
File,
75 StringRef SearchPath, StringRef RelativePath,
const Module *Imported) {
78 IncludeDirective ID = {HashLoc, FilenameRange, FileName,
IsAngled,
false};
80 ID.IsMainModule =
true;
86 void IncludeOrderPPCallbacks::EndOfMainFile() {
98 std::vector<unsigned> Blocks(1, 0);
106 std::vector<unsigned> IncludeIndices;
108 IncludeIndices.push_back(I);
111 for (
unsigned BI = 0, BE = Blocks.size() - 1; BI != BE; ++BI)
112 std::sort(IncludeIndices.begin() + Blocks[BI],
113 IncludeIndices.begin() + Blocks[BI + 1],
114 [
this](
unsigned LHSI,
unsigned RHSI) {
119 getPriority(LHS.Filename, LHS.IsAngled, LHS.IsMainModule);
121 getPriority(RHS.Filename, RHS.IsAngled, RHS.IsMainModule);
123 return std::tie(PriorityLHS, LHS.Filename) <
124 std::tie(PriorityRHS, RHS.Filename);
128 for (
unsigned BI = 0, BE = Blocks.size() - 1; BI != BE; ++BI) {
131 for (I = Blocks[BI], E = Blocks[BI + 1]; I != E; ++I)
132 if (IncludeIndices[I] != I)
140 "#includes are not sorted properly");
143 for (; I != E; ++I) {
144 if (IncludeIndices[I] == I)
148 SourceLocation FromLoc = CopyFrom.Range.getBegin();
149 const char *FromData =
SM.getCharacterData(FromLoc);
150 unsigned FromLen = std::strcspn(FromData,
"\n");
152 StringRef FixedName(FromData, FromLen);
155 const char *ToData =
SM.getCharacterData(ToLoc);
156 unsigned ToLen = std::strcspn(ToData,
"\n");
158 CharSourceRange::getCharRange(ToLoc, ToLoc.getLocWithOffset(ToLen));
160 D << FixItHint::CreateReplacement(ToRange, FixedName);
SourceLocation Loc
'#' location in the include directive
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
std::string Filename
Filename as a string.
bool IsAngled
true if this was an include with angle brackets
std::vector< IncludeDirective > IncludeDirectives
bool IsMainModule
true if this was the first include in a file
CharSourceRange Range
SourceRange for the file name.
static int getPriority(StringRef Filename, bool IsAngled, bool IsMainModule)