]> granicus.if.org Git - clang/commitdiff
clang-format: Improve detection of macros annotating functions.
authorDaniel Jasper <djasper@google.com>
Mon, 18 May 2015 13:47:23 +0000 (13:47 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 18 May 2015 13:47:23 +0000 (13:47 +0000)
Before:
  ASSERT("aaaaaaaaaaaaaaa")
      << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
      << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;

After:
  ASSERT("aaaaaaaaaaaaaaa") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                            << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;

Also cleanup implementation a bit and only mark closing parenthesis of
these annotations.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@237567 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/ContinuationIndenter.cpp
lib/Format/FormatToken.h
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 20626a622f84f63135a23896634af96af31d7197..623e086992639df7979855b00f43a5a485029833 100644 (file)
@@ -213,7 +213,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
     // different LineFormatter would be used otherwise.
     if (Previous.ClosesTemplateDeclaration)
       return true;
-    if (Previous.is(TT_FunctionAnnotation) && Previous.is(tok::r_paren))
+    if (Previous.is(TT_FunctionAnnotationRParen))
       return true;
     if (Previous.is(TT_LeadingJavaAnnotation) && Current.isNot(tok::l_paren) &&
         Current.isNot(TT_LeadingJavaAnnotation))
@@ -492,9 +492,9 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
       !PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
       (PreviousNonComment->isNot(TT_TemplateCloser) ||
        Current.NestingLevel != 0) &&
-      !PreviousNonComment->isOneOf(TT_BinaryOperator, TT_FunctionAnnotation,
-                                   TT_JavaAnnotation,
-                                   TT_LeadingJavaAnnotation) &&
+      !PreviousNonComment->isOneOf(
+          TT_BinaryOperator, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
+          TT_LeadingJavaAnnotation) &&
       Current.isNot(TT_BinaryOperator) && !PreviousNonComment->opensScope())
     State.Stack.back().BreakBeforeParameter = true;
 
@@ -574,9 +574,9 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
     return State.Stack.back().VariablePos;
   if ((PreviousNonComment &&
        (PreviousNonComment->ClosesTemplateDeclaration ||
-        PreviousNonComment->isOneOf(TT_AttributeParen, TT_FunctionAnnotation,
-                                    TT_JavaAnnotation,
-                                    TT_LeadingJavaAnnotation))) ||
+        PreviousNonComment->isOneOf(
+            TT_AttributeParen, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
+            TT_LeadingJavaAnnotation))) ||
       (!Style.IndentWrappedFunctionNames &&
        NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName)))
     return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent);
index c934398b1a92b32cdef5e19e3c8a0021bd370193..9325d0d52e5a6234bc36a7095bb13ccb9b6d79ac 100644 (file)
@@ -42,7 +42,7 @@ enum TokenType {
   TT_DesignatedInitializerPeriod,
   TT_DictLiteral,
   TT_ForEachMacro,
-  TT_FunctionAnnotation,
+  TT_FunctionAnnotationRParen,
   TT_FunctionDeclarationName,
   TT_FunctionLBrace,
   TT_FunctionTypeLParen,
index abd9be6d223b6687304402d003114322f47cb30e..f4b2f00655636e8f6ef27a161403d512d4c137e3 100644 (file)
@@ -493,8 +493,7 @@ private:
       if (Line.MustBeDeclaration && Contexts.size() == 1 &&
           !Contexts.back().IsExpression && Line.First->isNot(TT_ObjCProperty) &&
           (!Tok->Previous ||
-           !Tok->Previous->isOneOf(tok::kw_decltype, TT_LeadingJavaAnnotation,
-                                   TT_FunctionAnnotation)))
+           !Tok->Previous->isOneOf(tok::kw_decltype, TT_LeadingJavaAnnotation)))
         Line.MightBeFunctionDecl = true;
       break;
     case tok::l_square:
@@ -916,16 +915,18 @@ private:
       } else {
         Current.Type = TT_LineComment;
       }
-    } else if (Current.is(tok::l_paren) && Current.Previous &&
-               Current.Previous->is(TT_FunctionAnnotation)) {
-      Current.Type = TT_FunctionAnnotation;
     } else if (Current.is(tok::r_paren)) {
       if (rParenEndsCast(Current))
         Current.Type = TT_CastRParen;
-      if (Current.MatchingParen &&
-          Current.MatchingParen->is(TT_FunctionAnnotation) && Current.Next &&
+      if (Current.MatchingParen && Current.Next &&
+          !Current.Next->isBinaryOperator() &&
           !Current.Next->isOneOf(tok::semi, tok::colon))
-        Current.Type = TT_FunctionAnnotation;
+        if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
+          if (BeforeParen->is(tok::identifier) &&
+              BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
+              (!BeforeParen->Previous ||
+               BeforeParen->Previous->ClosesTemplateDeclaration))
+            Current.Type = TT_FunctionAnnotationRParen;
     } else if (Current.is(tok::at) && Current.Next) {
       if (Current.Next->isStringLiteral()) {
         Current.Type = TT_ObjCStringLiteral;
@@ -976,11 +977,6 @@ private:
                                            TT_LeadingJavaAnnotation)) {
         Current.Type = Current.Previous->Type;
       }
-    } else if (Current.is(tok::identifier) &&
-               Current.TokenText == Current.TokenText.upper() &&
-               (!Current.Previous ||
-                Current.Previous->ClosesTemplateDeclaration)) {
-      Current.Type = TT_FunctionAnnotation;
     }
   }
 
index f41928fe725273beb588fffb6efe39fe9ca36737..00c23b5bcc10c20a3312eec5243d896c256158bc 100644 (file)
@@ -4046,6 +4046,10 @@ TEST_F(FormatTest, FunctionAnnotations) {
   verifyFormat("template <typename T>\n"
                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
                "string OldFunction(const string &parameter) {}");
+
+  // Not function annotations.
+  verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
+               "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
 }
 
 TEST_F(FormatTest, BreaksDesireably) {