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

Nico Weber nicolasweber at gmx.de
Sat Dec 22 17:07:46 PST 2012


Author: nico
Date: Sat Dec 22 19:07:46 2012
New Revision: 170993

URL: http://llvm.org/viewvc/llvm-project?rev=170993&view=rev
Log:
libFormat: Teach the *& usage heuristic that "return" starts a rhs too.

"return a*b;" was formatted as "return a *b;" and is now formatted as "return a * b;".

Fixes PR14687 partially.


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=170993&r1=170992&r2=170993&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Sat Dec 22 19:07:46 2012
@@ -716,19 +716,21 @@
 
 private:
   void determineTokenTypes() {
-    bool AssignmentEncountered = false;
+    bool IsRHS = false;
     for (int i = 0, e = Line.Tokens.size(); i != e; ++i) {
       TokenAnnotation &Annotation = Annotations[i];
       const FormatToken &Tok = Line.Tokens[i];
 
       if (getBinOpPrecedence(Tok.Tok.getKind(), true, true) == prec::Assignment)
-        AssignmentEncountered = true;
+        IsRHS = true;
+      else if (Tok.Tok.is(tok::kw_return))
+        IsRHS = true;
 
       if (Annotation.Type != TokenAnnotation::TT_Unknown)
         continue;
 
       if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp)) {
-        Annotation.Type = determineStarAmpUsage(i, AssignmentEncountered);
+        Annotation.Type = determineStarAmpUsage(i, IsRHS);
       } else if (Tok.Tok.is(tok::minus) || Tok.Tok.is(tok::plus)) {
         Annotation.Type = determinePlusMinusUsage(i);
       } else if (Tok.Tok.is(tok::minusminus) || Tok.Tok.is(tok::plusplus)) {
@@ -754,12 +756,13 @@
   }
 
   TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index,
-                                                   bool AssignmentEncountered) {
+                                                   bool IsRHS) {
     if (Index == Annotations.size())
       return TokenAnnotation::TT_Unknown;
 
     if (Index == 0 || Line.Tokens[Index - 1].Tok.is(tok::l_paren) ||
         Line.Tokens[Index - 1].Tok.is(tok::comma) ||
+        Line.Tokens[Index - 1].Tok.is(tok::kw_return) ||
         Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator)
       return TokenAnnotation::TT_UnaryOperator;
 
@@ -770,7 +773,7 @@
 
     // It is very unlikely that we are going to find a pointer or reference type
     // definition on the RHS of an assignment.
-    if (AssignmentEncountered)
+    if (IsRHS)
       return TokenAnnotation::TT_BinaryOperator;
 
     return TokenAnnotation::TT_PointerOrReference;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=170993&r1=170992&r2=170993&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sat Dec 22 19:07:46 2012
@@ -592,6 +592,9 @@
   verifyFormat("int a = *b * c;");
   verifyFormat("int a = b * *c;");
   verifyFormat("int main(int argc, char **argv) {\n}");
+  verifyFormat("return 10 * b;");
+  verifyFormat("return *b * *c;");
+  verifyFormat("return a & ~b;");
 
   // FIXME: Is this desired for LLVM? Fix if not.
   verifyFormat("A<int *> a;");





More information about the cfe-commits mailing list