Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6a3e7f0

Browse filesBrowse files
committed
clang-format: Add IgnoreExtension option to SortIncludesOptions
Sorting without taking the file extension into account 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" """ You'd get """ /#include "analyze.h" /#include "analyze-blame.h" """ Let's allow sorting without taking the file extension into account unless two headers otherwise compare equal by introducing a new boolean option IgnoreExtension for SortIncludesOptions.
1 parent bca39f4 commit 6a3e7f0
Copy full SHA for 6a3e7f0

File tree

Expand file treeCollapse file tree

4 files changed

+81
-14
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+81
-14
lines changed

‎clang/docs/ClangFormatStyleOptions.rst

Copy file name to clipboardExpand all lines: clang/docs/ClangFormatStyleOptions.rst
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6010,6 +6010,17 @@ the configuration (without a prefix: ``Auto``).
60106010
#include "B/A.h" #include "B/a.h"
60116011
#include "B/a.h" #include "a/b.h"
60126012

6013+
* ``bool IgnoreExtension`` :versionbadge:`clang-format 21`
6014+
When sorting includes in each block, Only take file extensions into
6015+
account if two includes compare equal otherwise.
6016+
6017+
.. code-block:: c++
6018+
6019+
true: false:
6020+
# include "A.h" # include "A-util.h"
6021+
# include "A.inc" # include "A.h"
6022+
# include "A-util.h" # include "A.inc"
6023+
60136024

60146025
.. _SortJavaStaticImport:
60156026

‎clang/include/clang/Format/Format.h

Copy file name to clipboardExpand all lines: clang/include/clang/Format/Format.h
+12-1Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4382,8 +4382,19 @@ struct FormatStyle {
43824382
/// #include "B/a.h" #include "a/b.h"
43834383
/// \endcode
43844384
bool IgnoreCase;
4385+
/// When sorting includes in each block, Only take file extensions into
4386+
/// account if two includes compare equal otherwise.
4387+
/// \code
4388+
/// true: false:
4389+
/// # include "A.h" # include "A-util.h"
4390+
/// # include "A.inc" # include "A.h"
4391+
/// # include "A-util.h" # include "A.inc"
4392+
/// \endcode
4393+
/// \version 21
4394+
bool IgnoreExtension;
43854395
bool operator==(const SortIncludesOptions &R) const {
4386-
return Enabled == R.Enabled && IgnoreCase == R.IgnoreCase;
4396+
return Enabled == R.Enabled && IgnoreCase == R.IgnoreCase &&
4397+
IgnoreExtension == R.IgnoreExtension;
43874398
}
43884399
bool operator!=(const SortIncludesOptions &R) const {
43894400
return !(*this == R);

‎clang/lib/Format/Format.cpp

Copy file name to clipboardExpand all lines: clang/lib/Format/Format.cpp
+22-13Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,8 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
16471647
LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
16481648
LLVMStyle.ShortNamespaceLines = 1;
16491649
LLVMStyle.SkipMacroDefinitionBody = false;
1650-
LLVMStyle.SortIncludes = {/*Enabled=*/true, /*IgnoreCase=*/false};
1650+
LLVMStyle.SortIncludes = {/*Enabled=*/true, /*IgnoreCase=*/false,
1651+
/*IgnoreExtension=*/false};
16511652
LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before;
16521653
LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
16531654
LLVMStyle.SpaceAfterCStyleCast = false;
@@ -3230,19 +3231,27 @@ static void sortCppIncludes(const FormatStyle &Style,
32303231
SmallVector<unsigned, 16> Indices =
32313232
llvm::to_vector<16>(llvm::seq<unsigned>(0, Includes.size()));
32323233

3233-
if (Style.SortIncludes.Enabled && Style.SortIncludes.IgnoreCase) {
3234+
if (Style.SortIncludes.Enabled) {
32343235
stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
3235-
const auto LHSFilenameLower = Includes[LHSI].Filename.lower();
3236-
const auto RHSFilenameLower = Includes[RHSI].Filename.lower();
3237-
return std::tie(Includes[LHSI].Priority, LHSFilenameLower,
3238-
Includes[LHSI].Filename) <
3239-
std::tie(Includes[RHSI].Priority, RHSFilenameLower,
3240-
Includes[RHSI].Filename);
3241-
});
3242-
} else {
3243-
stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
3244-
return std::tie(Includes[LHSI].Priority, Includes[LHSI].Filename) <
3245-
std::tie(Includes[RHSI].Priority, Includes[RHSI].Filename);
3236+
SmallString<128> LHSStem, RHSStem;
3237+
if (Style.SortIncludes.IgnoreExtension) {
3238+
LHSStem = Includes[LHSI].Filename;
3239+
RHSStem = Includes[RHSI].Filename;
3240+
llvm::sys::path::replace_extension(LHSStem, "");
3241+
llvm::sys::path::replace_extension(RHSStem, "");
3242+
}
3243+
std::string LHSStemLower, RHSStemLower;
3244+
std::string LHSFilenameLower, RHSFilenameLower;
3245+
if (Style.SortIncludes.IgnoreCase) {
3246+
LHSStemLower = LHSStem.str().lower();
3247+
RHSStemLower = RHSStem.str().lower();
3248+
LHSFilenameLower = Includes[LHSI].Filename.lower();
3249+
RHSFilenameLower = Includes[RHSI].Filename.lower();
3250+
}
3251+
return std::tie(Includes[LHSI].Priority, LHSStemLower, LHSStem,
3252+
LHSFilenameLower, Includes[LHSI].Filename) <
3253+
std::tie(Includes[RHSI].Priority, RHSStemLower, RHSStem,
3254+
RHSFilenameLower, Includes[RHSI].Filename);
32463255
});
32473256
}
32483257

‎clang/unittests/Format/SortIncludesTest.cpp

Copy file name to clipboardExpand all lines: clang/unittests/Format/SortIncludesTest.cpp
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,42 @@ TEST_F(SortIncludesTest, BlockCommentedOutIncludes) {
14831483
verifyFormat(Code, sort(Code, "input.cpp", 0));
14841484
}
14851485

1486+
TEST_F(SortIncludesTest, IgnoreExtension) {
1487+
verifyFormat("#include <a-util.h>\n"
1488+
"#include <a.h>\n"
1489+
"#include <a.inc>",
1490+
sort("#include <a.inc>\n"
1491+
"#include <a-util.h>\n"
1492+
"#include <a.h>",
1493+
"input.h", 1));
1494+
1495+
verifyFormat("#include <ab-beta.h>\n"
1496+
"#include <ab-data.h>\n"
1497+
"#include <ab.h>",
1498+
sort("#include <ab-data.h>\n"
1499+
"#include <ab.h>\n"
1500+
"#include <ab-beta.h>",
1501+
"input.h", 1));
1502+
1503+
FmtStyle.SortIncludes.IgnoreExtension = true;
1504+
1505+
verifyFormat("#include <a.h>\n"
1506+
"#include <a.inc>\n"
1507+
"#include <a-util.h>",
1508+
sort("#include <a.inc>\n"
1509+
"#include <a-util.h>\n"
1510+
"#include <a.h>",
1511+
"input.h", 1));
1512+
1513+
verifyFormat("#include <ab.h>\n"
1514+
"#include <ab-beta.h>\n"
1515+
"#include <ab-data.h>",
1516+
sort("#include <ab-data.h>\n"
1517+
"#include <ab.h>\n"
1518+
"#include <ab-beta.h>",
1519+
"input.h", 1));
1520+
}
1521+
14861522
} // end namespace
14871523
} // end namespace format
14881524
} // end namespace clang

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.