10#include "clang/Basic/SourceManager.h"
11#include "clang/Lex/Lexer.h"
20StringRef removeFirstSuffix(StringRef Str, ArrayRef<const char *>
Suffixes) {
22 if (Str.ends_with(
Suffix)) {
23 return Str.substr(0, Str.size() -
Suffix.size());
37 return removeFirstSuffix(
38 removeFirstSuffix(Str, {
".cc",
".cpp",
".c",
".h",
".hpp"}), {
"Test"});
42 removeFirstSuffix(removeFirstSuffix(Str, {
".cc",
".cpp",
".c",
".h",
43 ".hpp",
".mm",
".m"}),
44 {
"_unittest",
"_regtest",
"_test",
"Test"});
48 size_t StartIndex =
Canonical.find_last_of(
'/');
49 if (StartIndex == StringRef::npos) {
53 0,
Canonical.find_first_of(
'+', StartIndex));
55 return removeFirstSuffix(
56 removeFirstSuffix(Str, {
".cc",
".cpp",
".c",
".h",
".hpp"}),
57 {
"_unittest",
"_regtest",
"_test"});
61size_t findNextLine(
const char *
Text) {
62 size_t EOLIndex = std::strcspn(
Text,
"\n");
63 return Text[EOLIndex] ==
'\0' ? EOLIndex : EOLIndex + 1;
67determineIncludeKind(StringRef CanonicalFile, StringRef IncludeFile,
79 StringRef CanonicalInclude = makeCanonicalName(IncludeFile, Style);
80 if (CanonicalFile.ends_with(CanonicalInclude) ||
81 CanonicalInclude.ends_with(CanonicalFile)) {
86 std::pair<StringRef, StringRef> Parts = CanonicalInclude.split(
"/public/");
87 StringRef FileCopy = CanonicalFile;
88 if (FileCopy.consume_front(Parts.first) &&
89 FileCopy.consume_back(Parts.second)) {
91 if (FileCopy ==
"/internal/" || FileCopy ==
"/proto/") {
97 if (IncludeFile.ends_with(
".generated.h") ||
98 IncludeFile.ends_with(
".proto.h") ||
99 IncludeFile.ends_with(
".pbobjc.h")) {
106int compareHeaders(StringRef LHS, StringRef RHS,
109 const std::pair<const char *, const char *> &Mismatch =
110 std::mismatch(LHS.begin(), LHS.end(), RHS.begin(), RHS.end());
111 if ((Mismatch.first != LHS.end()) && (Mismatch.second != RHS.end())) {
112 if ((*Mismatch.first ==
'.') && (*Mismatch.second ==
'+')) {
115 if ((*Mismatch.first ==
'+') && (*Mismatch.second ==
'.')) {
120 return LHS.compare(RHS);
126 const FileID FileID, StringRef
FileName,
128 : SourceMgr(SourceMgr), Style(Style), CurrentFileID(FileID),
129 CanonicalFile(makeCanonicalName(
FileName, Style)) {}
132 SourceLocation HashLocation,
133 SourceLocation EndLocation) {
134 int Offset = findNextLine(SourceMgr->getCharacterData(EndLocation));
137 auto &IncludeLocation = IncludeLocations[
FileName];
138 IncludeLocation.push_back(
139 SourceRange(HashLocation, EndLocation.getLocWithOffset(
Offset)));
140 SourceLocations.push_back(IncludeLocation.back());
143 if (IncludeLocation.size() > 1)
153std::optional<FixItHint>
155 std::string IncludeStmt;
158 ? llvm::Twine(
"#import <" +
FileName +
">\n").str()
159 : llvm::Twine(
"#import \"" +
FileName +
"\"\n").str();
162 ? llvm::Twine(
"#include <" +
FileName +
">\n").str()
163 : llvm::Twine(
"#include \"" +
FileName +
"\"\n").str();
165 if (SourceLocations.empty()) {
168 IncludeStmt.append(
"\n");
169 return FixItHint::CreateInsertion(
170 SourceMgr->getLocForStartOfFile(CurrentFileID), IncludeStmt);
176 if (!IncludeBucket[IncludeKind].empty()) {
177 for (
const std::string &IncludeEntry : IncludeBucket[IncludeKind]) {
178 if (compareHeaders(
FileName, IncludeEntry, Style) < 0) {
179 const auto &
Location = IncludeLocations[IncludeEntry][0];
180 return FixItHint::CreateInsertion(
Location.getBegin(), IncludeStmt);
188 const std::string &LastInclude = IncludeBucket[IncludeKind].back();
189 SourceRange LastIncludeLocation = IncludeLocations[LastInclude].back();
190 return FixItHint::CreateInsertion(LastIncludeLocation.getEnd(),
200 if (!IncludeBucket[I].empty()) {
202 if (NonEmptyKind < IncludeKind)
210 if (NonEmptyKind < IncludeKind) {
212 const std::string &LastInclude = IncludeBucket[NonEmptyKind].back();
213 SourceRange LastIncludeLocation = IncludeLocations[LastInclude].back();
214 IncludeStmt =
'\n' + IncludeStmt;
215 return FixItHint::CreateInsertion(LastIncludeLocation.getEnd(),
219 const std::string &FirstInclude = IncludeBucket[NonEmptyKind][0];
220 SourceRange FirstIncludeLocation = IncludeLocations[FirstInclude].back();
221 IncludeStmt.append(
"\n");
222 return FixItHint::CreateInsertion(FirstIncludeLocation.getBegin(),
228llvm::ArrayRef<std::pair<utils::IncludeSorter::IncludeStyle, StringRef>>
230 static constexpr std::pair<utils::IncludeSorter::IncludeStyle, StringRef>
bool IsAngled
true if this was an include with angle brackets
static constexpr llvm::StringLiteral Suffixes
IncludeKinds
The classifications of inclusions, in the order they should be sorted.
@ IK_CSystemInclude
e.g. #include <stdio.h>
@ IK_NonSystemInclude
e.g. #include "bar.h"
@ IK_InvalidInclude
total number of valid IncludeKinds
@ IK_MainTUInclude
e.g. #include "foo.h" when editing foo.cc
@ IK_GeneratedInclude
e.g. #include "bar.proto.h"
@ IK_CXXSystemInclude
e.g. #include <vector>
void addInclude(StringRef FileName, bool IsAngled, SourceLocation HashLocation, SourceLocation EndLocation)
Adds the given include directive to the sorter.
IncludeSorter(const SourceManager *SourceMgr, const FileID FileID, StringRef FileName, IncludeStyle Style)
IncludeSorter constructor; takes the FileID and name of the file to be processed by the sorter.
IncludeStyle
Supported include styles.
std::optional< FixItHint > createIncludeInsertion(StringRef FileName, bool IsAngled)
Creates a quoted inclusion directive in the right sort order.
@ Canonical
The two mix because the types refer to the same CanonicalType, but we do not elaborate as to how.
static ArrayRef< std::pair< T, StringRef > > getEnumMapping()=delete