Skip to content

Commit ce0cb85

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 ce0cb85

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-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

clang/unittests/Format/SortIncludesTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,14 @@ TEST_F(SortIncludesTest, BlockCommentedOutIncludes) {
14851485
verifyFormat(Code, sort(Code, "input.cpp", 0));
14861486
}
14871487

1488+
TEST_F(SortIncludesTest, SortViaStem) {
1489+
verifyFormat("#include <a.h>\n"
1490+
"#include <a-util.h>",
1491+
sort("#include <a-util.h>\n"
1492+
"#include <a.h>",
1493+
"input.h", 1));
1494+
}
1495+
14881496
} // end namespace
14891497
} // end namespace format
14901498
} // end namespace clang

0 commit comments

Comments
 (0)