[clang-format] Fixing erroneous trailing comma#205631
Conversation
| // 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()) |
There was a problem hiding this comment.
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.
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
|
@llvm/pr-subscribers-clang-format Author: Barry Revzin (brevzin) ChangesFixes #205571 Full diff: https://github.com/llvm/llvm-project/pull/205631.diff 3 Files Affected:
diff --git a/clang/lib/Tooling/Core/Replacement.cpp b/clang/lib/Tooling/Core/Replacement.cpp
index 10bdc223e33f2..ac5a2aed8111f 100644
--- a/clang/lib/Tooling/Core/Replacement.cpp
+++ b/clang/lib/Tooling/Core/Replacement.cpp
@@ -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())
+ 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() !=
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index c42cc147cf21e..78ddb27e61e95 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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) {
diff --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp
index 171dc6de2cae7..7ba989ddb492e 100644
--- a/clang/unittests/Tooling/RefactoringTest.cpp
+++ b/clang/unittests/Tooling/RefactoringTest.cpp
@@ -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"));
|
HazardyKnusperkeks
left a comment
There was a problem hiding this comment.
You would've still only handled the symptom, now with a dynamic allocation hit for everyone. Please take a look at my fix.
Makes a lot of sense to me, thanks! |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/65/builds/36414 Here is the relevant piece of the build log for the reference |
Fixes llvm#205571 --------- Co-authored-by: Barry Revzin <brevzin@jumptrading.com> Co-authored-by: Björn Schäpers <bjoern@hazardy.de>
Fixes llvm#205571 --------- Co-authored-by: Barry Revzin <brevzin@jumptrading.com> Co-authored-by: Björn Schäpers <bjoern@hazardy.de>
Fixes #205571