Skip to content

Commit 33b26cd

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 33b26cd

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

clang/lib/Format/Format.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3219,17 +3219,19 @@ 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+
const auto LHSStem = llvm::sys::path::stem(Includes[LHSI].Filename);
3223+
const auto RHSStem = llvm::sys::path::stem(Includes[RHSI].Filename);
3224+
const auto LHSStemLower = LHSStem.lower();
3225+
const auto RHSStemLower = RHSStem.lower();
3226+
return std::tie(Includes[LHSI].Priority, LHSStemLower, LHSStem) <
3227+
std::tie(Includes[RHSI].Priority, RHSStemLower, RHSStem);
32283228
});
32293229
} else {
32303230
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);
3231+
const auto LHSStem = llvm::sys::path::stem(Includes[LHSI].Filename);
3232+
const auto RHSStem = llvm::sys::path::stem(Includes[RHSI].Filename);
3233+
return std::tie(Includes[LHSI].Priority, LHSStem) <
3234+
std::tie(Includes[RHSI].Priority, RHSStem);
32333235
});
32343236
}
32353237

0 commit comments

Comments
 (0)