<div style="font-family: arial, helvetica, sans-serif; font-size: 10pt"><div>Cool, thanks!</div><br><div class="gmail_quote">On Fri, Dec 21, 2012 at 1:41 AM, Daniel Jasper <span dir="ltr"><<a href="mailto:djasper@google.com" target="_blank">djasper@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: djasper<br>
Date: Fri Dec 21 03:41:31 2012<br>
New Revision: 170873<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=170873&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=170873&view=rev</a><br>
Log:<br>
Formatting fixes for PR14680<br>
<br>
Also, some (automated) formatting fixes and slight cleanups.<br>
<br>
Modified:<br>
    cfe/trunk/lib/Format/Format.cpp<br>
    cfe/trunk/unittests/Format/FormatTest.cpp<br>
<br>
Modified: cfe/trunk/lib/Format/Format.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=170873&r1=170872&r2=170873&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=170873&r1=170872&r2=170873&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Format/Format.cpp (original)<br>
+++ cfe/trunk/lib/Format/Format.cpp Fri Dec 21 03:41:31 2012<br>
@@ -34,6 +34,7 @@<br>
     TT_TemplateCloser,<br>
     TT_BinaryOperator,<br>
     TT_UnaryOperator,<br>
+    TT_TrailingUnaryOperator,<br>
     TT_OverloadedOperator,<br>
     TT_PointerOrReference,<br>
     TT_ConditionalExpr,<br>
@@ -550,8 +551,8 @@<br>
<br>
     determineTokenTypes();<br>
     bool IsObjCMethodDecl =<br>
-      (Line.Tokens.size() > 0 &&<br>
-        (Annotations[0].Type == TokenAnnotation::TT_ObjCMethodSpecifier));<br>
+        (Line.Tokens.size() > 0 &&<br>
+         (Annotations[0].Type == TokenAnnotation::TT_ObjCMethodSpecifier));<br></blockquote><div><br></div><div>It looks like all the ObjC stuff was just reformatted. The diff would be easier to read if the automated reformatting had been landed in a separate commit.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
     for (int i = 1, e = Line.Tokens.size(); i != e; ++i) {<br>
       TokenAnnotation &Annotation = Annotations[i];<br>
<br>
@@ -561,21 +562,19 @@<br>
       if (Annotation.Type == TokenAnnotation::TT_CtorInitializerColon) {<br>
         Annotation.MustBreakBefore = true;<br>
         Annotation.SpaceRequiredBefore = true;<br>
-      } else if (IsObjCMethodDecl &&<br>
-                 Line.Tokens[i].Tok.is(tok::identifier) &&<br>
-                 (i != e-1) && Line.Tokens[i+1].Tok.is(tok::colon) &&<br>
-                 Line.Tokens[i-1].Tok.is(tok::identifier)) {<br>
+      } else if (IsObjCMethodDecl && Line.Tokens[i].Tok.is(tok::identifier) &&<br>
+                 (i != e - 1) && Line.Tokens[i + 1].Tok.is(tok::colon) &&<br>
+                 Line.Tokens[i - 1].Tok.is(tok::identifier)) {<br>
         Annotation.CanBreakBefore = true;<br>
         Annotation.SpaceRequiredBefore = true;<br>
-      } else if (IsObjCMethodDecl &&<br>
-                 Line.Tokens[i].Tok.is(tok::identifier) &&<br>
-                 Line.Tokens[i-1].Tok.is(tok::l_paren) &&<br>
-                 Line.Tokens[i-2].Tok.is(tok::colon)) {<br>
+      } else if (IsObjCMethodDecl && Line.Tokens[i].Tok.is(tok::identifier) &&<br>
+                 Line.Tokens[i - 1].Tok.is(tok::l_paren) &&<br>
+                 Line.Tokens[i - 2].Tok.is(tok::colon)) {<br>
         // Don't break this identifier as ':' or identifier<br>
         // before it will break.<br>
         Annotation.CanBreakBefore = false;<br>
       } else if (Line.Tokens[i].Tok.is(tok::at) &&<br>
-                 Line.Tokens[i-2].Tok.is(tok::at)) {<br>
+                 Line.Tokens[i - 2].Tok.is(tok::at)) {<br>
         // Don't put two objc's '@' on the same line. This could happen,<br>
         // as in, @optinal @property ...<br>
         Annotation.MustBreakBefore = true;<br>
@@ -584,14 +583,13 @@<br>
           Line.Tokens[0].Tok.isNot(tok::kw_case) && !IsObjCMethodDecl &&<br>
           (i != e - 1);<br>
         // Don't break at ':' if identifier before it can beak.<br>
-        if (IsObjCMethodDecl &&<br>
-            Line.Tokens[i-1].Tok.is(tok::identifier) &&<br>
-            Annotations[i-1].CanBreakBefore)<br>
+        if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::identifier) &&<br>
+            Annotations[i - 1].CanBreakBefore)<br>
           Annotation.CanBreakBefore = false;<br>
-      } else if (Annotations[i - 1].Type ==<br>
-                 TokenAnnotation::TT_ObjCMethodSpecifier)<br>
+      } else if (<br>
+          Annotations[i - 1].Type == TokenAnnotation::TT_ObjCMethodSpecifier) {<br>
         Annotation.SpaceRequiredBefore = true;<br>
-      else if (Annotations[i - 1].Type == TokenAnnotation::TT_UnaryOperator) {<br>
+      } else if (Annotations[i - 1].Type == TokenAnnotation::TT_UnaryOperator) {<br>
         Annotation.SpaceRequiredBefore = false;<br>
       } else if (Annotation.Type == TokenAnnotation::TT_UnaryOperator) {<br>
         Annotation.SpaceRequiredBefore =<br>
@@ -615,17 +613,17 @@<br>
       } else if (Line.Tokens[i].Tok.is(tok::less) &&<br>
                  Line.Tokens[0].Tok.is(tok::hash)) {<br>
         Annotation.SpaceRequiredBefore = true;<br>
-      } else if (IsObjCMethodDecl &&<br>
-                 Line.Tokens[i - 1].Tok.is(tok::r_paren) &&<br>
-                 Line.Tokens[i].Tok.is(tok::identifier))<br>
+      } else if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::r_paren) &&<br>
+                 Line.Tokens[i].Tok.is(tok::identifier)) {<br>
         // Don't space between ')' and <id><br>
         Annotation.SpaceRequiredBefore = false;<br>
-      else if (IsObjCMethodDecl &&<br>
-               Line.Tokens[i - 1].Tok.is(tok::colon) &&<br>
-               Line.Tokens[i].Tok.is(tok::l_paren))<br>
+      } else if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::colon) &&<br>
+                 Line.Tokens[i].Tok.is(tok::l_paren)) {<br>
         // Don't space between ':' and '('<br>
         Annotation.SpaceRequiredBefore = false;<br>
-      else {<br>
+      } else if (Annotation.Type == TokenAnnotation::TT_TrailingUnaryOperator) {<br>
+        Annotation.SpaceRequiredBefore = false;<br>
+      } else {<br>
         Annotation.SpaceRequiredBefore =<br>
             spaceRequiredBetween(Line.Tokens[i - 1].Tok, Line.Tokens[i].Tok);<br>
       }<br>
@@ -657,14 +655,15 @@<br>
           <a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::slashequal))<br>
         AssignmentEncountered = true;<br>
<br>
-      if (<a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::star) || <a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::amp))<br>
+      if (<a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::star) || <a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::amp)) {<br>
         Annotation.Type = determineStarAmpUsage(i, AssignmentEncountered);<br>
-      else if ((<a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::minus) || <a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::plus)) &&<br>
-               Tok.Tok.isAtStartOfLine())<br>
-        Annotation.Type = TokenAnnotation::TT_ObjCMethodSpecifier;<br>
-      else if (isUnaryOperator(i))<br>
+      } else if (<a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::minus) || <a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::plus)) {<br>
+        Annotation.Type = determinePlusMinusUsage(i);<br>
+      } else if (<a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::minusminus) || <a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::plusplus)) {<br>
+        Annotation.Type = determineIncrementUsage(i);<br>
+      } else if (<a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::exclaim)) {<br>
         Annotation.Type = TokenAnnotation::TT_UnaryOperator;<br>
-      else if (isBinaryOperator(Line.Tokens[i]))<br>
+      } else if (isBinaryOperator(Line.Tokens[i]))<br>
         Annotation.Type = TokenAnnotation::TT_BinaryOperator;<br>
       else if (<a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::comment)) {<br>
         StringRef Data(SourceMgr.getCharacterData(Tok.Tok.getLocation()),<br>
@@ -677,29 +676,6 @@<br>
     }<br>
   }<br>
<br>
-  bool isUnaryOperator(unsigned Index) {<br>
-    const Token &Tok = Line.Tokens[Index].Tok;<br>
-<br>
-    // '++', '--' and '!' are always unary operators.<br>
-    if (Tok.is(tok::minusminus) || Tok.is(tok::plusplus) ||<br>
-        Tok.is(tok::exclaim))<br>
-      return true;<br>
-<br>
-    // The other possible unary operators are '+' and '-' as we<br>
-    // determine the usage of '*' and '&' in determineStarAmpUsage().<br>
-    if (Tok.isNot(tok::minus) && Tok.isNot(tok::plus))<br>
-      return false;<br>
-<br>
-    // Use heuristics to recognize unary operators.<br>
-    const Token &PreviousTok = Line.Tokens[Index - 1].Tok;<br>
-    if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) ||<br>
-        PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square))<br>
-      return true;<br>
-<br>
-    // Fall back to marking the token as binary operator.<br>
-    return Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator;<br>
-  }<br>
-<br>
   bool isBinaryOperator(const FormatToken &Tok) {<br>
     switch (Tok.Tok.getKind()) {<br>
     case tok::equal:<br>
@@ -731,7 +707,8 @@<br>
       return TokenAnnotation::TT_UnaryOperator;<br>
<br>
     if (Line.Tokens[Index - 1].Tok.isLiteral() ||<br>
-        Line.Tokens[Index + 1].Tok.isLiteral())<br>
+        Line.Tokens[Index + 1].Tok.isLiteral() ||<br>
+        Line.Tokens[Index + 1].Tok.is(tok::kw_sizeof))<br></blockquote><div><br></div><div>What about kw_alignof, kw___alignof, kw__Alignof?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

       return TokenAnnotation::TT_BinaryOperator;<br>
<br>
     // It is very unlikely that we are going to find a pointer or reference type<br>
@@ -742,8 +719,32 @@<br>
     return TokenAnnotation::TT_PointerOrReference;<br>
   }<br>
<br>
-  bool isIfForOrWhile(Token Tok) {<br>
-    return Tok.is(tok::kw_if) || Tok.is(tok::kw_for) || Tok.is(tok::kw_while);<br>
+  TokenAnnotation::TokenType determinePlusMinusUsage(unsigned Index) {<br>
+    // At the start of the line, +/- specific ObjectiveC method declarations.<br>
+    if (Index == 0)<br>
+      return TokenAnnotation::TT_ObjCMethodSpecifier;<br>
+<br>
+    // Use heuristics to recognize unary operators.<br>
+    const Token &PreviousTok = Line.Tokens[Index - 1].Tok;<br>
+    if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) ||<br>
+        PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square) ||<br>
+        PreviousTok.is(tok::question) || PreviousTok.is(tok::colon))<br>
+      return TokenAnnotation::TT_UnaryOperator;<br>
+<br>
+    // There can't be to consecutive binary operators.<br></blockquote><div><br></div><div>typo to -> two</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

+    if (Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator)<br>
+      return TokenAnnotation::TT_UnaryOperator;<br>
+<br>
+    // Fall back to marking the token as binary operator.<br>
+    return TokenAnnotation::TT_BinaryOperator;<br>
+  }<br>
+<br>
+  /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.<br>
+  TokenAnnotation::TokenType determineIncrementUsage(unsigned Index) {<br>
+    if (Index != 0 && Line.Tokens[Index - 1].Tok.is(tok::identifier))<br>
+      return TokenAnnotation::TT_TrailingUnaryOperator;<br>
+<br>
+    return TokenAnnotation::TT_UnaryOperator;<br>
   }<br>
<br>
   bool spaceRequiredBetween(Token Left, Token Right) {<br>
@@ -778,17 +779,15 @@<br>
       return false;<br>
     if (Left.is(tok::colon) || Right.is(tok::colon))<br>
       return true;<br>
-    if ((Left.is(tok::plusplus) && Right.isAnyIdentifier()) ||<br>
-        (Left.isAnyIdentifier() && Right.is(tok::plusplus)) ||<br>
-        (Left.is(tok::minusminus) && Right.isAnyIdentifier()) ||<br>
-        (Left.isAnyIdentifier() && Right.is(tok::minusminus)))<br>
-      return false;<br>
     if (Left.is(tok::l_paren))<br>
       return false;<br>
     if (Left.is(tok::hash))<br>
       return false;<br>
     if (Right.is(tok::l_paren)) {<br>
-      return !Left.isAnyIdentifier() || isIfForOrWhile(Left);<br>
+      return Left.is(tok::kw_if) || Left.is(tok::kw_for) ||<br>
+          Left.is(tok::kw_while) || Left.is(tok::kw_switch) ||<br>
+          (Left.isNot(tok::identifier) && Left.isNot(tok::kw_sizeof) &&<br>
+           Left.isNot(tok::kw_typeof));<br></blockquote><div><br></div><div>kw_alignof etc?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
     }<br>
     return true;<br>
   }<br>
<br>
Modified: cfe/trunk/unittests/Format/FormatTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=170873&r1=170872&r2=170873&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=170873&r1=170872&r2=170873&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)<br>
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Dec 21 03:41:31 2012<br>
@@ -545,7 +545,7 @@<br>
   verifyFormat("template <typename T> void f() {\n}");<br>
 }<br>
<br>
-TEST_F(FormatTest, UndestandsUnaryOperators) {<br>
+TEST_F(FormatTest, UnderstandsUnaryOperators) {<br>
   verifyFormat("int a = -2;");<br>
   verifyFormat("f(-1, -2, -3);");<br>
   verifyFormat("a[-1] = 5;");<br>
@@ -557,6 +557,11 @@<br>
   verifyFormat("++(a->f());");<br>
   verifyFormat("--(a->f());");<br>
   verifyFormat("if (!(a->f())) {\n}");<br>
+<br>
+  verifyFormat("a-- > b;");<br>
+  verifyFormat("b ? -a : c;");<br>
+  verifyFormat("n * sizeof char16;");<br>
+  verifyFormat("sizeof(char);");<br>
 }<br>
<br>
 TEST_F(FormatTest, UndestandsOverloadedOperators) {<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>