[clang] clang-format: Add IncludeSortKey option (PR #137840)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Wed May 14 19:02:43 PDT 2025
================
@@ -3221,15 +3223,33 @@ static void sortCppIncludes(const FormatStyle &Style,
stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
const auto LHSFilenameLower = Includes[LHSI].Filename.lower();
const auto RHSFilenameLower = Includes[RHSI].Filename.lower();
- return std::tie(Includes[LHSI].Priority, LHSFilenameLower,
- Includes[LHSI].Filename) <
- std::tie(Includes[RHSI].Priority, RHSFilenameLower,
- Includes[RHSI].Filename);
+ SmallString<128> LHSStem = Includes[LHSI].Filename;
+ SmallString<128> RHSStem = Includes[RHSI].Filename;
+ if (Style.IncludeStyle.IncludeSortKey ==
+ tooling::IncludeStyle::ISK_Stem) {
+ llvm::sys::path::replace_extension(LHSStem, "");
+ llvm::sys::path::replace_extension(RHSStem, "");
+ }
+ const auto LHSStemLower = LHSStem.str().lower();
+ const auto RHSStemLower = RHSStem.str().lower();
+ return std::tie(Includes[LHSI].Priority, LHSStemLower, LHSStem,
+ LHSFilenameLower, Includes[LHSI].Filename) <
+ std::tie(Includes[RHSI].Priority, RHSStemLower, RHSStem,
+ RHSFilenameLower, Includes[RHSI].Filename);
});
} else {
stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
- return std::tie(Includes[LHSI].Priority, Includes[LHSI].Filename) <
- std::tie(Includes[RHSI].Priority, Includes[RHSI].Filename);
+ SmallString<128> LHSStem = Includes[LHSI].Filename;
+ SmallString<128> RHSStem = Includes[RHSI].Filename;
+ if (Style.IncludeStyle.IncludeSortKey ==
+ tooling::IncludeStyle::ISK_Stem) {
+ llvm::sys::path::replace_extension(LHSStem, "");
+ llvm::sys::path::replace_extension(RHSStem, "");
+ }
+ return std::tie(Includes[LHSI].Priority, LHSStem,
+ Includes[LHSI].Filename) <
+ std::tie(Includes[RHSI].Priority, RHSStem,
+ Includes[RHSI].Filename);
----------------
owenca wrote:
Something like the following:
```c++
@@ -3219,39 +3219,27 @@ static void sortCppIncludes(const FormatStyle &Style,
SmallVector<unsigned, 16> Indices =
llvm::to_vector<16>(llvm::seq<unsigned>(0, Includes.size()));
- if (Style.SortIncludes == FormatStyle::SI_CaseInsensitive) {
- stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
- const auto LHSFilenameLower = Includes[LHSI].Filename.lower();
- const auto RHSFilenameLower = Includes[RHSI].Filename.lower();
- SmallString<128> LHSStem = Includes[LHSI].Filename;
- SmallString<128> RHSStem = Includes[RHSI].Filename;
- if (Style.IncludeStyle.IncludeSortKey ==
- tooling::IncludeStyle::ISK_Stem) {
- llvm::sys::path::replace_extension(LHSStem, "");
- llvm::sys::path::replace_extension(RHSStem, "");
- }
- const auto LHSStemLower = LHSStem.str().lower();
- const auto RHSStemLower = RHSStem.str().lower();
- return std::tie(Includes[LHSI].Priority, LHSStemLower, LHSStem,
- LHSFilenameLower, Includes[LHSI].Filename) <
- std::tie(Includes[RHSI].Priority, RHSStemLower, RHSStem,
- RHSFilenameLower, Includes[RHSI].Filename);
- });
- } else {
- stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
- SmallString<128> LHSStem = Includes[LHSI].Filename;
- SmallString<128> RHSStem = Includes[RHSI].Filename;
- if (Style.IncludeStyle.IncludeSortKey ==
- tooling::IncludeStyle::ISK_Stem) {
- llvm::sys::path::replace_extension(LHSStem, "");
- llvm::sys::path::replace_extension(RHSStem, "");
- }
- return std::tie(Includes[LHSI].Priority, LHSStem,
- Includes[LHSI].Filename) <
- std::tie(Includes[RHSI].Priority, RHSStem,
- Includes[RHSI].Filename);
- });
- }
+ stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
+ SmallString<128> LHSStem, RHSStem;
+ if (Style.IncludeStyle.IncludeSortKey == tooling::IncludeStyle::ISK_Stem) {
+ LHSStem = Includes[LHSI].Filename;
+ RHSStem = Includes[RHSI].Filename;
+ llvm::sys::path::replace_extension(LHSStem, "");
+ llvm::sys::path::replace_extension(RHSStem, "");
+ }
+ std::string LHSStemLower, RHSStemLower;
+ std::string LHSFilenameLower, RHSFilenameLower;
+ if (Style.SortIncludes == FormatStyle::SI_CaseInsensitive) {
+ LHSStemLower = LHSStem.str().lower();
+ RHSStemLower = RHSStem.str().lower();
+ LHSFilenameLower = Includes[LHSI].Filename.lower();
+ RHSFilenameLower = Includes[RHSI].Filename.lower();
+ }
+ return std::tie(Includes[LHSI].Priority, LHSStemLower, LHSStem,
+ LHSFilenameLower, Includes[LHSI].Filename) <
+ std::tie(Includes[RHSI].Priority, RHSStemLower, RHSStem,
+ RHSFilenameLower, Includes[RHSI].Filename);
+ });
// The index of the include on which the cursor will be put after
// sorting/deduplicating.
```
https://github.com/llvm/llvm-project/pull/137840
More information about the cfe-commits
mailing list