[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
This commit is contained in:
Marek Kurdej 2020-12-03 10:38:37 +01:00 committed by Marek Kurdej
parent 6627a3c287
commit fe21c86ee7
2 changed files with 15 additions and 1 deletions

View File

@ -2179,7 +2179,8 @@ static void sortCppIncludes(const FormatStyle &Style,
// Deduplicate #includes.
Indices.erase(std::unique(Indices.begin(), Indices.end(),
[&](unsigned LHSI, unsigned RHSI) {
return Includes[LHSI].Text == Includes[RHSI].Text;
return Includes[LHSI].Text.trim() ==
Includes[RHSI].Text.trim();
}),
Indices.end());

View File

@ -289,6 +289,19 @@ TEST_F(SortIncludesTest, LeadingWhitespace) {
sort("# include \"a.h\"\n"
"# include \"c.h\"\n"
"# include \"b.h\"\n"));
EXPECT_EQ("#include \"a.h\"\n", sort("#include \"a.h\"\n"
" #include \"a.h\"\n"));
}
TEST_F(SortIncludesTest, TrailingWhitespace) {
EXPECT_EQ("#include \"a.h\"\n"
"#include \"b.h\"\n"
"#include \"c.h\"\n",
sort("#include \"a.h\" \n"
"#include \"c.h\" \n"
"#include \"b.h\" \n"));
EXPECT_EQ("#include \"a.h\"\n", sort("#include \"a.h\"\n"
"#include \"a.h\" \n"));
}
TEST_F(SortIncludesTest, GreaterInComment) {