[cfe-commits] r171028 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Daniel Jasper djasper at google.com
Mon Dec 24 02:56:04 PST 2012


Author: djasper
Date: Mon Dec 24 04:56:04 2012
New Revision: 171028

URL: http://llvm.org/viewvc/llvm-project?rev=171028&view=rev
Log:
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()() {}

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=171028&r1=171027&r2=171028&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Dec 24 04:56:04 2012
@@ -553,9 +553,19 @@
         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 @@
       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)) {

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=171028&r1=171027&r2=171028&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Dec 24 04:56:04 2012
@@ -589,7 +589,22 @@
 }
 
 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) {





More information about the cfe-commits mailing list