aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2012-12-24 10:56:04 +0000
committerDaniel Jasper <djasper@google.com>2012-12-24 10:56:04 +0000
commitf6aef6a25bd47f2a7bfc24938e29b746fdccdc72 (patch)
tree5e74351af264f724fd44014bde9faefb713f2020
parent34ef11be9d09ebf916ab02a521e37be6e9e102d3 (diff)
Fix formatting over overloaded operators.
This fixes llvm.org/pr14686. We used to add too many spaces for different versions of overloaded operator function declarations/definitions. This patch changes, e.g. operator *() {} operator >() {} operator () () {} to operator*() {} operator>() {} operator()() {} git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171028 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Format/Format.cpp21
-rw-r--r--unittests/Format/FormatTest.cpp17
2 files changed, 35 insertions, 3 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index ad85a8ecf2..8fb6932db9 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -553,9 +553,19 @@ public:
Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator;
break;
case tok::kw_operator:
- if (!Tokens[Index].Tok.is(tok::l_paren))
+ if (Tokens[Index].Tok.is(tok::l_paren)) {
Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator;
- next();
+ next();
+ if (Index < Tokens.size() && Tokens[Index].Tok.is(tok::r_paren)) {
+ Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator;
+ next();
+ }
+ } else {
+ while (Index < Tokens.size() && !Tokens[Index].Tok.is(tok::l_paren)) {
+ Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator;
+ next();
+ }
+ }
break;
case tok::question:
parseConditional();
@@ -633,6 +643,13 @@ public:
if (Annotation.Type == TokenAnnotation::TT_CtorInitializerColon) {
Annotation.MustBreakBefore = true;
Annotation.SpaceRequiredBefore = true;
+ } else if (Annotation.Type == TokenAnnotation::TT_OverloadedOperator) {
+ Annotation.SpaceRequiredBefore =
+ Line.Tokens[i].Tok.is(tok::identifier) || Line.Tokens[i].Tok.is(
+ tok::kw_new) || Line.Tokens[i].Tok.is(tok::kw_delete);
+ } else if (
+ Annotations[i - 1].Type == TokenAnnotation::TT_OverloadedOperator) {
+ Annotation.SpaceRequiredBefore = false;
} else if (IsObjCMethodDecl && Line.Tokens[i].Tok.is(tok::identifier) &&
(i != e - 1) && Line.Tokens[i + 1].Tok.is(tok::colon) &&
Line.Tokens[i - 1].Tok.is(tok::identifier)) {
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 0d7901525b..dda1f82e82 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -589,7 +589,22 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) {
}
TEST_F(FormatTest, UndestandsOverloadedOperators) {
- verifyFormat("bool operator<() {\n}");
+ verifyFormat("bool operator<();");
+ verifyFormat("bool operator>();");
+ verifyFormat("bool operator=();");
+ verifyFormat("bool operator==();");
+ verifyFormat("bool operator!=();");
+ verifyFormat("int operator+();");
+ verifyFormat("int operator++();");
+ verifyFormat("bool operator();");
+ verifyFormat("bool operator()();");
+ verifyFormat("bool operator[]();");
+ verifyFormat("operator bool();");
+ verifyFormat("operator SomeType<int>();");
+ verifyFormat("void *operator new(std::size_t size);");
+ verifyFormat("void *operator new[](std::size_t size);");
+ verifyFormat("void operator delete(void *ptr);");
+ verifyFormat("void operator delete[](void *ptr);");
}
TEST_F(FormatTest, UnderstandsUsesOfStar) {