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 if (FileCopy ==
"/internal/" || FileCopy ==
"/proto/")
88 return IncludeSorter::IK_MainTUInclude;
91 if (Style == IncludeSorter::IS_Google_ObjC) {
92 if (IncludeFile.ends_with(
".generated.h") ||
93 IncludeFile.ends_with(
".proto.h") ||
94 IncludeFile.ends_with(
".pbobjc.h")) {
95 return IncludeSorter::IK_GeneratedInclude;
98 return IncludeSorter::IK_NonSystemInclude;
102 IncludeSorter::IncludeStyle Style) {
103 if (Style == IncludeSorter::IncludeStyle::IS_Google_ObjC) {
104 const std::pair<const char *, const char *> &Mismatch =
105 llvm::mismatch(LHS, RHS);
106 if ((Mismatch.first != LHS.end()) && (Mismatch.second != RHS.end())) {
107 if ((*Mismatch.first ==
'.') && (*Mismatch.second ==
'+'))
109 if ((*Mismatch.first ==
'+') && (*Mismatch.second ==
'.'))
113 return LHS.compare(RHS);
116IncludeSorter::IncludeSorter(
const SourceManager *SourceMgr, FileID FileID,
117 StringRef FileName, IncludeStyle Style)
118 : SourceMgr(SourceMgr), Style(Style), CurrentFileID(FileID),
138 determineIncludeKind(CanonicalFile, FileName, IsAngled, Style);
144IncludeSorter::createIncludeInsertion(StringRef FileName,
bool IsAngled) {
145 const StringRef LineEnding =
146 SourceMgr->getBufferData(CurrentFileID).detectEOL();
147 std::string IncludeStmt;
148 if (Style == IncludeStyle::IS_Google_ObjC) {
151 ? llvm::Twine(
"#import <" + FileName +
">" + LineEnding).str()
152 : llvm::Twine(
"#import \"" + FileName +
"\"" + LineEnding).str();
156 ? llvm::Twine(
"#include <" + FileName +
">" + LineEnding).str()
157 : llvm::Twine(
"#include \"" + FileName +
"\"" + LineEnding).str();
159 if (SourceLocations.empty()) {
162 IncludeStmt.append(LineEnding);
163 return FixItHint::CreateInsertion(
164 SourceMgr->getLocForStartOfFile(CurrentFileID), IncludeStmt);
170 if (!IncludeBucket[IncludeKind].empty()) {
171 for (
const std::string &IncludeEntry : IncludeBucket[IncludeKind]) {
173 const auto &
Location = IncludeLocations[IncludeEntry][0];
174 return FixItHint::CreateInsertion(
Location.getBegin(), IncludeStmt);
176 if (FileName == IncludeEntry)
181 const std::string &LastInclude = IncludeBucket[IncludeKind].back();
182 const SourceRange LastIncludeLocation =
183 IncludeLocations[LastInclude].back();
184 return FixItHint::CreateInsertion(LastIncludeLocation.getEnd(),
192 IncludeKinds NonEmptyKind = IK_InvalidInclude;
193 for (
int I = IK_InvalidInclude - 1; I >= 0; --I) {
194 if (!IncludeBucket[I].empty()) {
195 NonEmptyKind =
static_cast<IncludeKinds
>(I);
196 if (NonEmptyKind < IncludeKind)
200 if (NonEmptyKind == IK_InvalidInclude)
203 if (NonEmptyKind < IncludeKind) {
205 const std::string &LastInclude = IncludeBucket[NonEmptyKind].back();
206 const SourceRange LastIncludeLocation =
207 IncludeLocations[LastInclude].back();
208 IncludeStmt.insert(0, LineEnding);
209 return FixItHint::CreateInsertion(LastIncludeLocation.getEnd(),
213 const std::string &FirstInclude = IncludeBucket[NonEmptyKind][0];
214 const SourceRange FirstIncludeLocation =
215 IncludeLocations[FirstInclude].back();
216 IncludeStmt.append(LineEnding);
217 return FixItHint::CreateInsertion(FirstIncludeLocation.getBegin(),