Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions clang/lib/Tooling/Core/Replacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ llvm::Error Replacements::add(const Replacement &R) {
assert(R.getLength() == 0);
// `I` is also an insertion, `R` and `I` conflict.
if (I->getLength() == 0) {
// If the two insertions are identical, `R` is redundant; keep the
// existing one rather than concatenating. This happens e.g. when
// clang-format analyzes the same code under several preprocessor
// branches and emits the same insertion in each run.
if (R.getReplacementText() == I->getReplacementText())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the symptom, while the root cause is kept. I'm currently not that fit in the preprocessor branches stuff, but it would be nice if we can fix it there. It it outside of the branches and an unwrapped line on itself.

return llvm::Error::success();
// Check if two insertions are order-independent: if inserting them in
// either order produces the same text, they are order-independent.
if ((R.getReplacementText() + I->getReplacementText()).str() !=
Expand Down
13 changes: 13 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25210,6 +25210,19 @@ TEST_F(FormatTest, EnumTrailingComma) {
" MY_ENUM = 0U\n"
"};",
Style);

// Issue https://github.com/llvm/llvm-project/issues/205571
verifyFormat("#ifdef FOO\n"
"#else\n"
"#endif\n"
"enum {\n"
" E = 1,\n"
"};",
"#ifdef FOO\n"
"#else\n"
"#endif\n"
"enum { E = 1 };",
Style);
}

TEST_F(FormatTest, BreakAfterAttributes) {
Expand Down
12 changes: 12 additions & 0 deletions clang/unittests/Tooling/RefactoringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@ TEST_F(ReplacementTest, AddInsertAtOtherInsertWhenOderIndependent) {
EXPECT_EQ(Replacement("x.cc", 10, 3, ""), *std::next(Replaces.begin()));
}

TEST_F(ReplacementTest, AddIdenticalInsertionsAtSameOffsetDeduplicates) {
Replacements Replaces;
auto Err = Replaces.add(Replacement("x.cc", 10, 0, ","));
EXPECT_TRUE(!Err);
llvm::consumeError(std::move(Err));
Err = Replaces.add(Replacement("x.cc", 10, 0, ","));
EXPECT_TRUE(!Err);
llvm::consumeError(std::move(Err));
EXPECT_EQ(1u, Replaces.size());
EXPECT_EQ(Replacement("x.cc", 10, 0, ","), *Replaces.begin());
}

TEST_F(ReplacementTest, InsertBetweenAdjacentReplacements) {
Replacements Replaces;
auto Err = Replaces.add(Replacement("x.cc", 10, 5, "a"));
Expand Down
Loading