r237562 - clang-format: Support function annotations in macros.

Daniel Jasper djasper at google.com
Mon May 18 02:47:25 PDT 2015


Author: djasper
Date: Mon May 18 04:47:22 2015
New Revision: 237562

URL: http://llvm.org/viewvc/llvm-project?rev=237562&view=rev
Log:
clang-format: Support function annotations in macros.

Before:
  DEPRECATED("Use NewClass::NewFunction instead.") string
      OldFunction(const string &parameter) {}

After:
  DEPRECATED("Use NewClass::NewFunction instead.")
  string OldFunction(const string &parameter) {}

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/FormatToken.h
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=237562&r1=237561&r2=237562&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Mon May 18 04:47:22 2015
@@ -208,8 +208,13 @@ bool ContinuationIndenter::mustBreak(con
     return true;
 
   if (Current.NestingLevel == 0 && !Current.isTrailingComment()) {
+    // Always break after "template <...>" and leading annotations. This is only
+    // for cases where the entire line does not fit on a single line as a
+    // different LineFormatter would be used otherwise.
     if (Previous.ClosesTemplateDeclaration)
       return true;
+    if (Previous.is(TT_FunctionAnnotation) && Previous.is(tok::r_paren))
+      return true;
     if (Previous.is(TT_LeadingJavaAnnotation) && Current.isNot(tok::l_paren) &&
         Current.isNot(TT_LeadingJavaAnnotation))
       return true;
@@ -487,7 +492,8 @@ unsigned ContinuationIndenter::addTokenO
       !PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
       (PreviousNonComment->isNot(TT_TemplateCloser) ||
        Current.NestingLevel != 0) &&
-      !PreviousNonComment->isOneOf(TT_BinaryOperator, TT_JavaAnnotation,
+      !PreviousNonComment->isOneOf(TT_BinaryOperator, TT_FunctionAnnotation,
+                                   TT_JavaAnnotation,
                                    TT_LeadingJavaAnnotation) &&
       Current.isNot(TT_BinaryOperator) && !PreviousNonComment->opensScope())
     State.Stack.back().BreakBeforeParameter = true;
@@ -568,7 +574,8 @@ unsigned ContinuationIndenter::getNewLin
     return State.Stack.back().VariablePos;
   if ((PreviousNonComment &&
        (PreviousNonComment->ClosesTemplateDeclaration ||
-        PreviousNonComment->isOneOf(TT_AttributeParen, TT_JavaAnnotation,
+        PreviousNonComment->isOneOf(TT_AttributeParen, TT_FunctionAnnotation,
+                                    TT_JavaAnnotation,
                                     TT_LeadingJavaAnnotation))) ||
       (!Style.IndentWrappedFunctionNames &&
        NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName)))

Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=237562&r1=237561&r2=237562&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Mon May 18 04:47:22 2015
@@ -42,6 +42,7 @@ enum TokenType {
   TT_DesignatedInitializerPeriod,
   TT_DictLiteral,
   TT_ForEachMacro,
+  TT_FunctionAnnotation,
   TT_FunctionDeclarationName,
   TT_FunctionLBrace,
   TT_FunctionTypeLParen,

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=237562&r1=237561&r2=237562&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon May 18 04:47:22 2015
@@ -493,7 +493,8 @@ 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)))
+           !Tok->Previous->isOneOf(tok::kw_decltype, TT_LeadingJavaAnnotation,
+                                   TT_FunctionAnnotation)))
         Line.MightBeFunctionDecl = true;
       break;
     case tok::l_square:
@@ -915,9 +916,16 @@ 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 &&
+          !Current.Next->isOneOf(tok::semi, tok::colon))
+        Current.Type = TT_FunctionAnnotation;
     } else if (Current.is(tok::at) && Current.Next) {
       if (Current.Next->isStringLiteral()) {
         Current.Type = TT_ObjCStringLiteral;
@@ -968,6 +976,11 @@ 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;
     }
   }
 

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=237562&r1=237561&r2=237562&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon May 18 04:47:22 2015
@@ -4040,6 +4040,14 @@ TEST_F(FormatTest, BreaksFunctionDeclara
       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
 }
 
+TEST_F(FormatTest, FunctionAnnotations) {
+  verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
+               "string OldFunction(const string &parameter) {}");
+  verifyFormat("template <typename T>\n"
+               "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
+               "string OldFunction(const string &parameter) {}");
+}
+
 TEST_F(FormatTest, BreaksDesireably) {
   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"





More information about the cfe-commits mailing list