26 IncludeSorter::IncludeStyle Style) {
33 if (Style == IncludeSorter::IS_LLVM) {
37 if (Style == IncludeSorter::IS_Google_ObjC) {
38 const StringRef Canonical =
40 ".hpp",
".mm",
".m"}),
41 {
"_unittest",
"_regtest",
"_test",
"Test"});
45 size_t StartIndex = Canonical.find_last_of(
'/');
46 if (StartIndex == StringRef::npos)
48 return Canonical.substr(0, Canonical.find_first_of(
'+', StartIndex));
52 {
"_unittest",
"_regtest",
"_test"});
63 bool IsAngled, IncludeSorter::IncludeStyle Style) {
71 return IncludeFile.ends_with(
".h") ? IncludeSorter::IK_CSystemInclude
72 : IncludeSorter::IK_CXXSystemInclude;
75 if (CanonicalFile.ends_with(CanonicalInclude) ||
76 CanonicalInclude.ends_with(CanonicalFile)) {
77 return IncludeSorter::IK_MainTUInclude;
79 if ((Style == IncludeSorter::IS_Google) ||
80 (Style == IncludeSorter::IS_Google_ObjC)) {
81 const std::pair<StringRef, StringRef> Parts =
82 CanonicalInclude.split(
"/public/");
83 StringRef FileCopy = CanonicalFile;
84 if (FileCopy.consume_front(Parts.first) &&
85 FileCopy.consume_back(Parts.second) &&
87 (FileCopy ==
"/internal/" || FileCopy ==
"/proto/"))
88 return IncludeSorter::IK_MainTUInclude;
90 if (Style == IncludeSorter::IS_Google_ObjC &&
91 (IncludeFile.ends_with(
".generated.h") ||
92 IncludeFile.ends_with(
".proto.h") ||
93 IncludeFile.ends_with(
".pbobjc.h"))) {
94 return IncludeSorter::IK_GeneratedInclude;
96 return IncludeSorter::IK_NonSystemInclude;
100 IncludeSorter::IncludeStyle Style) {
101 if (Style == IncludeSorter::IncludeStyle::IS_Google_ObjC) {
102 const std::pair<const char *, const char *> &Mismatch =
103 llvm::mismatch(LHS, RHS);
104 if ((Mismatch.first != LHS.end()) && (Mismatch.second != RHS.end())) {
105 if ((*Mismatch.first ==
'.') && (*Mismatch.second ==
'+'))
107 if ((*Mismatch.first ==
'+') && (*Mismatch.second ==
'.'))
111 return LHS.compare(RHS);
114IncludeSorter::IncludeSorter(
const SourceManager *SourceMgr, FileID FileID,
115 StringRef FileName, IncludeStyle Style)
116 : SourceMgr(SourceMgr), Style(Style), CurrentFileID(FileID),
136 determineIncludeKind(CanonicalFile, FileName, IsAngled, Style);
142IncludeSorter::createIncludeInsertion(StringRef FileName,
bool IsAngled) {
143 const StringRef LineEnding =
144 SourceMgr->getBufferData(CurrentFileID).detectEOL();
145 std::string IncludeStmt;
146 if (Style == IncludeStyle::IS_Google_ObjC) {
149 ? llvm::Twine(
"#import <" + FileName +
">" + LineEnding).str()
150 : llvm::Twine(
"#import \"" + FileName +
"\"" + LineEnding).str();
154 ? llvm::Twine(
"#include <" + FileName +
">" + LineEnding).str()
155 : llvm::Twine(
"#include \"" + FileName +
"\"" + LineEnding).str();
157 if (SourceLocations.empty()) {
160 IncludeStmt.append(LineEnding);
161 return FixItHint::CreateInsertion(
162 SourceMgr->getLocForStartOfFile(CurrentFileID), IncludeStmt);
165 const auto IncludeKind =
168 if (!IncludeBucket[IncludeKind].empty()) {
169 for (
const std::string &IncludeEntry : IncludeBucket[IncludeKind]) {
171 const auto &
Location = IncludeLocations[IncludeEntry][0];
172 return FixItHint::CreateInsertion(
Location.getBegin(), IncludeStmt);
174 if (FileName == IncludeEntry)
179 const std::string &LastInclude = IncludeBucket[IncludeKind].back();
180 const SourceRange LastIncludeLocation =
181 IncludeLocations[LastInclude].back();
182 return FixItHint::CreateInsertion(LastIncludeLocation.getEnd(),
190 IncludeKinds NonEmptyKind = IK_InvalidInclude;
191 for (
int I = IK_InvalidInclude - 1; I >= 0; --I) {
192 if (!IncludeBucket[I].empty()) {
193 NonEmptyKind =
static_cast<IncludeKinds
>(I);
194 if (NonEmptyKind < IncludeKind)
198 if (NonEmptyKind == IK_InvalidInclude)
201 if (NonEmptyKind < IncludeKind) {
203 const std::string &LastInclude = IncludeBucket[NonEmptyKind].back();
204 const SourceRange LastIncludeLocation =
205 IncludeLocations[LastInclude].back();
206 IncludeStmt.insert(0, LineEnding);
207 return FixItHint::CreateInsertion(LastIncludeLocation.getEnd(),
211 const std::string &FirstInclude = IncludeBucket[NonEmptyKind][0];
212 const SourceRange FirstIncludeLocation =
213 IncludeLocations[FirstInclude].back();
214 IncludeStmt.append(LineEnding);
215 return FixItHint::CreateInsertion(FirstIncludeLocation.getBegin(),