]> granicus.if.org Git - clang/commitdiff
Improve clang-format's handling of unary operators.
authorDaniel Jasper <djasper@google.com>
Thu, 6 Dec 2012 13:16:39 +0000 (13:16 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 6 Dec 2012 13:16:39 +0000 (13:16 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169500 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 08096866bf39779680c9a050538935f4cda2aec9..51db69c5ae65dc1371ea2b70770b5dfa7e563d9c 100644 (file)
@@ -618,12 +618,24 @@ private:
 
   bool isUnaryOperator(unsigned Index) {
     const Token &Tok = Line.Tokens[Index].Tok;
+
+    // '++', '--' and '!' are always unary operators.
+    if (Tok.is(tok::minusminus) || Tok.is(tok::plusplus) ||
+        Tok.is(tok::exclaim))
+      return true;
+
+    // The other possible unary operators are '+' and '-' as we
+    // determine the usage of '*' and '&' in determineStarAmpUsage().
     if (Tok.isNot(tok::minus) && Tok.isNot(tok::plus))
       return false;
+
+    // Use heuristics to recognize unary operators.
     const Token &PreviousTok = Line.Tokens[Index - 1].Tok;
     if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) ||
         PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square))
       return true;
+
+    // Fall back to marking the token as binary operator.
     return Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator;
   }
 
index 7628b34d4b9c01be98688a630dc7928277475169..a2a37cf1a7014fceb70a7001ec6bb05df6e928cd 100644 (file)
@@ -364,6 +364,9 @@ TEST_F(FormatTest, UndestandsUnaryOperators) {
   verifyFormat("if (i != -1) {\n}");
   verifyFormat("if (i > -1) {\n}");
   verifyFormat("if (i < -1) {\n}");
+  verifyFormat("++(a->f());");
+  verifyFormat("--(a->f());");
+  verifyFormat("if (!(a->f())) {\n}");
 }
 
 TEST_F(FormatTest, UndestandsOverloadedOperators) {