10#include "clang/Basic/SourceManager.h"
11#include "clang/Lex/Lexer.h"
20StringRef removeFirstSuffix(StringRef Str, ArrayRef<const char *>
Suffixes) {
22 if (Str.endswith(
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.endswith(CanonicalInclude)
81 || CanonicalInclude.endswith(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.equals(
"/internal/") ||
92 FileCopy.equals(
"/proto/")) {
98 if (IncludeFile.endswith(
".generated.h") ||
99 IncludeFile.endswith(
".proto.h") || IncludeFile.endswith(
".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());
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 IncludeLocations[
FileName].push_back(
138 SourceRange(HashLocation, EndLocation.getLocWithOffset(
Offset)));
139 SourceLocations.push_back(IncludeLocations[
FileName].back());
142 if (IncludeLocations[
FileName].size() > 1)
152std::optional<FixItHint>
154 std::string IncludeStmt;
157 ? llvm::Twine(
"#import <" +
FileName +
">\n").str()
158 : llvm::Twine(
"#import \"" +
FileName +
"\"\n").str();
161 ? llvm::Twine(
"#include <" +
FileName +
">\n").str()
162 : llvm::Twine(
"#include \"" +
FileName +
"\"\n").str();
164 if (SourceLocations.empty()) {
167 IncludeStmt.append(
"\n");
168 return FixItHint::CreateInsertion(
169 SourceMgr->getLocForStartOfFile(CurrentFileID), IncludeStmt);
175 if (!IncludeBucket[IncludeKind].empty()) {
176 for (
const std::string &IncludeEntry : IncludeBucket[IncludeKind]) {
177 if (compareHeaders(
FileName, IncludeEntry, Style) < 0) {
178 const auto &
Location = IncludeLocations[IncludeEntry][0];
179 return FixItHint::CreateInsertion(
Location.getBegin(), IncludeStmt);
187 const std::string &LastInclude = IncludeBucket[IncludeKind].back();
188 SourceRange LastIncludeLocation = IncludeLocations[LastInclude].back();
189 return FixItHint::CreateInsertion(LastIncludeLocation.getEnd(),
199 if (!IncludeBucket[I].empty()) {
201 if (NonEmptyKind < IncludeKind)
209 if (NonEmptyKind < IncludeKind) {
211 const std::string &LastInclude = IncludeBucket[NonEmptyKind].back();
212 SourceRange LastIncludeLocation = IncludeLocations[LastInclude].back();
213 IncludeStmt =
'\n' + IncludeStmt;
214 return FixItHint::CreateInsertion(LastIncludeLocation.getEnd(),
218 const std::string &FirstInclude = IncludeBucket[NonEmptyKind][0];
219 SourceRange FirstIncludeLocation = IncludeLocations[FirstInclude].back();
220 IncludeStmt.append(
"\n");
221 return FixItHint::CreateInsertion(FirstIncludeLocation.getBegin(),
227llvm::ArrayRef<std::pair<utils::IncludeSorter::IncludeStyle, StringRef>>
229 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