Skip to content

Commit 4747e31

Browse files
committed
clang-format: Sort includes by stem rather than full filename
Sorting by stem gives nicer results when various header file names are substrings of other header file names, for example, a CLI application with a main header named analyze.h and a analyze-xxx.h header for each subcommand currently will always put analyze.h last after all the analyze-xxx.h headers, but putting analyze.h first instead of last is arguable nicer to read. TLDR; Instead of """ /#include "analyze-blame.h" /#include "analyze.h" """ We'll now get """ /#include "analyze.h" /#include "analyze-blame.h" """
1 parent bb21a68 commit 4747e31

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

clang/lib/Format/Format.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3219,17 +3219,23 @@ static void sortCppIncludes(const FormatStyle &Style,
32193219

32203220
if (Style.SortIncludes == FormatStyle::SI_CaseInsensitive) {
32213221
stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
3222-
const auto LHSFilenameLower = Includes[LHSI].Filename.lower();
3223-
const auto RHSFilenameLower = Includes[RHSI].Filename.lower();
3224-
return std::tie(Includes[LHSI].Priority, LHSFilenameLower,
3225-
Includes[LHSI].Filename) <
3226-
std::tie(Includes[RHSI].Priority, RHSFilenameLower,
3227-
Includes[RHSI].Filename);
3222+
SmallString<128> LHSStem = Includes[LHSI].Filename;
3223+
SmallString<128> RHSStem = Includes[RHSI].Filename;
3224+
llvm::sys::path::replace_extension(LHSStem, "");
3225+
llvm::sys::path::replace_extension(RHSStem, "");
3226+
const auto LHSStemLower = LHSStem.str().lower();
3227+
const auto RHSStemLower = RHSStem.str().lower();
3228+
return std::tie(Includes[LHSI].Priority, LHSStemLower, LHSStem) <
3229+
std::tie(Includes[RHSI].Priority, RHSStemLower, RHSStem);
32283230
});
32293231
} else {
32303232
stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
3231-
return std::tie(Includes[LHSI].Priority, Includes[LHSI].Filename) <
3232-
std::tie(Includes[RHSI].Priority, Includes[RHSI].Filename);
3233+
SmallString<128> LHSStem = Includes[LHSI].Filename;
3234+
SmallString<128> RHSStem = Includes[RHSI].Filename;
3235+
llvm::sys::path::replace_extension(LHSStem, "");
3236+
llvm::sys::path::replace_extension(RHSStem, "");
3237+
return std::tie(Includes[LHSI].Priority, LHSStem) <
3238+
std::tie(Includes[RHSI].Priority, RHSStem);
32333239
});
32343240
}
32353241

0 commit comments

Comments
 (0)