Skip to content

Commit fe21c86

Browse files
committed
[clang-format] De-duplicate includes with leading or trailing whitespace.
This fixes PR46555 (https://bugs.llvm.org/show_bug.cgi?id=46555). Reviewed By: MyDeveloperDay Differential Revision: https://reviews.llvm.org/D88296
1 parent 6627a3c commit fe21c86

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/Format/Format.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,8 @@ static void sortCppIncludes(const FormatStyle &Style,
21792179
// Deduplicate #includes.
21802180
Indices.erase(std::unique(Indices.begin(), Indices.end(),
21812181
[&](unsigned LHSI, unsigned RHSI) {
2182-
return Includes[LHSI].Text == Includes[RHSI].Text;
2182+
return Includes[LHSI].Text.trim() ==
2183+
Includes[RHSI].Text.trim();
21832184
}),
21842185
Indices.end());
21852186

clang/unittests/Format/SortIncludesTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,19 @@ TEST_F(SortIncludesTest, LeadingWhitespace) {
289289
sort("# include \"a.h\"\n"
290290
"# include \"c.h\"\n"
291291
"# include \"b.h\"\n"));
292+
EXPECT_EQ("#include \"a.h\"\n", sort("#include \"a.h\"\n"
293+
" #include \"a.h\"\n"));
294+
}
295+
296+
TEST_F(SortIncludesTest, TrailingWhitespace) {
297+
EXPECT_EQ("#include \"a.h\"\n"
298+
"#include \"b.h\"\n"
299+
"#include \"c.h\"\n",
300+
sort("#include \"a.h\" \n"
301+
"#include \"c.h\" \n"
302+
"#include \"b.h\" \n"));
303+
EXPECT_EQ("#include \"a.h\"\n", sort("#include \"a.h\"\n"
304+
"#include \"a.h\" \n"));
292305
}
293306

294307
TEST_F(SortIncludesTest, GreaterInComment) {

0 commit comments

Comments
 (0)