r185418 - Fix formatting of long declarations with const type.

Daniel Jasper djasper at google.com
Tue Jul 2 02:47:30 PDT 2013


Author: djasper
Date: Tue Jul  2 04:47:29 2013
New Revision: 185418

URL: http://llvm.org/viewvc/llvm-project?rev=185418&view=rev
Log:
Fix formatting of long declarations with const type.

Before (exceeding the column limit):
LoooooooooooooooooooooooooooooooooooooooongType const LoooooooooooooooooooooooooooooooooooooooongVariable;

After:
LoooooooooooooooooooooooooooooooooooooooongType const
LoooooooooooooooooooooooooooooooooooooooongVariable;

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

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=185418&r1=185417&r2=185418&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Jul  2 04:47:29 2013
@@ -585,13 +585,7 @@ private:
     }
 
     if (Current.Type == TT_Unknown) {
-      if (Current.Previous && Current.is(tok::identifier) &&
-          ((Current.Previous->is(tok::identifier) &&
-            Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() ==
-                tok::pp_not_keyword) ||
-           isSimpleTypeSpecifier(*Current.Previous) ||
-           Current.Previous->Type == TT_PointerOrReference ||
-           Current.Previous->Type == TT_TemplateCloser)) {
+      if (isStartOfName(Current)) {
         Contexts.back().FirstStartOfName = &Current;
         Current.Type = TT_StartOfName;
         NameFound = true;
@@ -666,6 +660,33 @@ private:
     }
   }
 
+  /// \brief Take a guess at whether \p Tok starts a name of a function or
+  /// variable declaration.
+  ///
+  /// This is a heuristic based on whether \p Tok is an identifier following
+  /// something that is likely a type.
+  bool isStartOfName(const FormatToken &Tok) {
+    if (Tok.isNot(tok::identifier) || Tok.Previous == NULL)
+      return false;
+
+    // Skip "const" as it does not have an influence on whether this is a name.
+    FormatToken *PreviousNotConst = Tok.Previous;
+    while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const))
+      PreviousNotConst = PreviousNotConst->Previous;
+
+    if (PreviousNotConst == NULL)
+      return false;
+
+    bool IsPPKeyword =
+        PreviousNotConst->is(tok::identifier) && PreviousNotConst->Previous &&
+        PreviousNotConst->Previous->is(tok::hash);
+
+    return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
+           PreviousNotConst->Type == TT_PointerOrReference ||
+           PreviousNotConst->Type == TT_TemplateCloser ||
+           isSimpleTypeSpecifier(*PreviousNotConst);
+  }
+
   /// \brief Return the type of the given token assuming it is * or &.
   TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) {
     const FormatToken *PrevToken = Tok.getPreviousNoneComment();

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=185418&r1=185417&r2=185418&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Jul  2 04:47:29 2013
@@ -3451,11 +3451,16 @@ TEST_F(FormatTest, BreaksLongDeclaration
   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
                "    LoooooooooooooooooooooooooooooooooooooooongVariable;",
                getGoogleStyle());
+  verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
+               "    LoooooooooooooooooooooooooooooooooooooooongVariable;",
+               getGoogleStyle());
   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
                getGoogleStyle());
   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
+  verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
+               "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
 
   // FIXME: Without the comment, this breaks after "(".
   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"





More information about the cfe-commits mailing list