Skip to content

clang-format: Add IncludeSortKey option #137840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6010,6 +6010,16 @@ the configuration (without a prefix: ``Auto``).
#include "B/A.h" #include "B/a.h"
#include "B/a.h" #include "a/b.h"

* ``bool IgnoreExtension`` When sorting includes in each block, Only take file extensions into
account if two includes compare equal otherwise.

.. code-block:: c++

true: false:
# include "A.h" vs. # include "A-util.h"
# include "A.inc" # include "A.h"
# include "A-util.h" # include "A.inc"


.. _SortJavaStaticImport:

Expand Down
12 changes: 11 additions & 1 deletion clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4382,8 +4382,18 @@ struct FormatStyle {
/// #include "B/a.h" #include "a/b.h"
/// \endcode
bool IgnoreCase;
/// When sorting includes in each block, Only take file extensions into
/// account if two includes compare equal otherwise.
/// \code
/// true: false:
/// # include "A.h" vs. # include "A-util.h"
/// # include "A.inc" # include "A.h"
/// # include "A-util.h" # include "A.inc"
/// \endcode
bool IgnoreExtension;
bool operator==(const SortIncludesOptions &R) const {
return Enabled == R.Enabled && IgnoreCase == R.IgnoreCase;
return Enabled == R.Enabled && IgnoreCase == R.IgnoreCase &&
IgnoreExtension == R.IgnoreExtension;
}
bool operator!=(const SortIncludesOptions &R) const {
return !(*this == R);
Expand Down
35 changes: 22 additions & 13 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,8 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
LLVMStyle.ShortNamespaceLines = 1;
LLVMStyle.SkipMacroDefinitionBody = false;
LLVMStyle.SortIncludes = {/*Enabled=*/true, /*IgnoreCase=*/false};
LLVMStyle.SortIncludes = {/*Enabled=*/true, /*IgnoreCase=*/false,
/*IgnoreExtension=*/false};
LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before;
LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
LLVMStyle.SpaceAfterCStyleCast = false;
Expand Down Expand Up @@ -3230,19 +3231,27 @@ static void sortCppIncludes(const FormatStyle &Style,
SmallVector<unsigned, 16> Indices =
llvm::to_vector<16>(llvm::seq<unsigned>(0, Includes.size()));

if (Style.SortIncludes.Enabled && Style.SortIncludes.IgnoreCase) {
if (Style.SortIncludes.Enabled) {
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);
});
} 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, RHSStem;
if (Style.SortIncludes.IgnoreExtension) {
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.IgnoreCase) {
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);
});
}

Expand Down
36 changes: 36 additions & 0 deletions clang/unittests/Format/SortIncludesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,42 @@ TEST_F(SortIncludesTest, BlockCommentedOutIncludes) {
verifyFormat(Code, sort(Code, "input.cpp", 0));
}

TEST_F(SortIncludesTest, IgnoreExtension) {
verifyFormat("#include <a-util.h>\n"
"#include <a.h>\n"
"#include <a.inc>",
sort("#include <a.inc>\n"
"#include <a-util.h>\n"
"#include <a.h>",
"input.h", 1));

verifyFormat("#include <ab-beta.h>\n"
"#include <ab-data.h>\n"
"#include <ab.h>",
sort("#include <ab-data.h>\n"
"#include <ab.h>\n"
"#include <ab-beta.h>",
"input.h", 1));

FmtStyle.SortIncludes.IgnoreExtension = true;

verifyFormat("#include <a.h>\n"
"#include <a.inc>\n"
"#include <a-util.h>",
sort("#include <a.inc>\n"
"#include <a-util.h>\n"
"#include <a.h>",
"input.h", 1));

verifyFormat("#include <ab.h>\n"
"#include <ab-beta.h>\n"
"#include <ab-data.h>",
sort("#include <ab-data.h>\n"
"#include <ab.h>\n"
"#include <ab-beta.h>",
"input.h", 1));
}

} // end namespace
} // end namespace format
} // end namespace clang