-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[clang-format] Add IgnoreExtension to SortIncludes #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
Conversation
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-format Author: Daan De Meyer (DaanDeMeyer) ChangesSorting 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
We'll now get
Full diff: https://github.com/llvm/llvm-project/pull/137840.diff 1 Files Affected:
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 5a1c3f556b331..bc1e681c9be78 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3219,17 +3219,19 @@ static void sortCppIncludes(const FormatStyle &Style,
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();
- return std::tie(Includes[LHSI].Priority, LHSFilenameLower,
- Includes[LHSI].Filename) <
- std::tie(Includes[RHSI].Priority, RHSFilenameLower,
- Includes[RHSI].Filename);
+ const auto LHSStem = llvm::sys::path::stem(Includes[LHSI].Filename);
+ const auto RHSStem = llvm::sys::path::stem(Includes[RHSI].Filename);
+ const auto LHSStemLower = LHSStem.lower();
+ const auto RHSStemLower = RHSStem.lower();
+ return std::tie(Includes[LHSI].Priority, LHSStemLower, LHSStem) <
+ std::tie(Includes[RHSI].Priority, RHSStemLower, RHSStem);
});
} 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);
+ const auto LHSStem = llvm::sys::path::stem(Includes[LHSI].Filename);
+ const auto RHSStem = llvm::sys::path::stem(Includes[RHSI].Filename);
+ return std::tie(Includes[LHSI].Priority, LHSStem) <
+ std::tie(Includes[RHSI].Priority, RHSStem);
});
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You definitely need some tests. And I'm not sure if everyone would like that, of if it should be an option.
I was hoping to get some feedback on whether it should be an option or not before adding tests. |
This was done by running a locally built clang-format with llvm/llvm-project#137617 and llvm/llvm-project#137840 applied on all .c and .h files.
This was done by running a locally built clang-format with llvm/llvm-project#137617 and llvm/llvm-project#137840 applied on all .c and .h files.
This was done by running a locally built clang-format with llvm/llvm-project#137617 and llvm/llvm-project#137840 applied on all .c and .h files.
This was done by running a locally built clang-format with llvm/llvm-project#137617 and llvm/llvm-project#137840 applied on all .c and .h files.
This was done by running a locally built clang-format with llvm/llvm-project#137617 and llvm/llvm-project#137840 applied on all .c and .h files.
This was done by running a locally built clang-format with llvm/llvm-project#137617 and llvm/llvm-project#137840 applied on all .c and .h files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you think should files be handled, that only differ in the caseness of the extension?
I would be in favor. |
IMO, unless it's a bug, we need a new option to ensure it's a non-breaking change. |
@HazardyKnusperkeks @owenca Updated PR to introduce a new option |
✅ With the latest revision this PR passed the C/C++ code formatter. |
I would expect that file extensions be sorted regardless. For example:
|
Addressed |
@owenca @HazardyKnusperkeks Rebased onto latest main to take advantage of @owenca's rework and addressed comments. |
33f84b2
to
beab0ec
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have the correct current default
6a3e7f0
to
3cf3ada
Compare
Ping? Would love to get this over the finish line |
0827e9c
to
607b938
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix all compiler warnings, e.g.
llvm-project/clang/lib/Format/Format.cpp:667:70: warning: missing field 'IgnoreExtension' initializer [-Wmissing-field-initializers]
667 | /*IgnoreCase=*/true}));
| ^
Also, the new option is not recognized:
$ cat .clang-format
SortIncludes:
Enabled: true
IgnoreExtension: true
$ clang-format -dump-config
.clang-format:3:3: error: unknown key 'IgnoreExtension'
IgnoreExtension: true
^~~~~~~~~~~~~~~
f175705
to
22a095d
Compare
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.
@owenca @mydeveloperday @HazardyKnusperkeks Can any of you merge this? I don't have commit rights |
This was done by running a locally built clang-format with llvm/llvm-project#137617 and llvm/llvm-project#137840 applied on all .c and .h files.
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 an 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 is arguably nicer to read.
TLDR; Instead of
You'd get
Let's allow sorting by stem instead of full path by adding IgnoreExtension
to SortIncludes.