[clang] 480a1fa - [clang-format] Fix incorrect alignment of operator= overloads.
Marek Kurdej via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 19 01:18:50 PST 2022
Author: Elliott Maguire
Date: 2022-01-19T10:18:47+01:00
New Revision: 480a1fab72f4e367a5986d572914d252c318431d
URL: https://github.com/llvm/llvm-project/commit/480a1fab72f4e367a5986d572914d252c318431d
DIFF: https://github.com/llvm/llvm-project/commit/480a1fab72f4e367a5986d572914d252c318431d.diff
LOG: [clang-format] Fix incorrect alignment of operator= overloads.
Fixes https://github.com/llvm/llvm-project/issues/31568.
Added a check for operator keyword tokens.
Reviewed By: MyDeveloperDay, curdeius, owenpan, HazardyKnusperkeks
Differential Revision: https://reviews.llvm.org/D117421
Added:
Modified:
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 9f3f18aa3eb0d..0d2e507ac5879 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -731,6 +731,11 @@ void WhitespaceManager::alignConsecutiveAssignments() {
if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
return false;
+ // Do not align operator= overloads.
+ FormatToken *Previous = C.Tok->getPreviousNonComment();
+ if (Previous && Previous->is(tok::kw_operator))
+ return false;
+
return C.Tok->is(tok::equal);
},
Changes, /*StartAt=*/0, Style.AlignConsecutiveAssignments);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index c16b6c20d5966..b8645b8896d2a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16266,6 +16266,34 @@ TEST_F(FormatTest, AlignConsecutiveAssignments) {
verifyFormat("int oneTwoThree = 123; // comment\n"
"int oneTwo = 12; // comment",
Alignment);
+ verifyFormat("int f() = default;\n"
+ "int &operator() = default;\n"
+ "int &operator=() {",
+ Alignment);
+ verifyFormat("int f() = default; // comment\n"
+ "int &operator() = default; // comment\n"
+ "int &operator=() {",
+ Alignment);
+ verifyFormat("int f() = default;\n"
+ "int &operator() = default;\n"
+ "int &operator==() {",
+ Alignment);
+ verifyFormat("int f() = default;\n"
+ "int &operator() = default;\n"
+ "int &operator<=() {",
+ Alignment);
+ verifyFormat("int f() = default;\n"
+ "int &operator() = default;\n"
+ "int &operator!=() {",
+ Alignment);
+ verifyFormat("int f() = default;\n"
+ "int &operator() = default;\n"
+ "int &operator=();",
+ Alignment);
+ verifyFormat("/* long long padding */ int f() = default;\n"
+ "int &operator() = default;\n"
+ "int &operator/**/ =();",
+ Alignment);
// Bug 25167
/* Uncomment when fixed
More information about the cfe-commits
mailing list