]> granicus.if.org Git - clang/commitdiff
Improve clang-format's memoization behavior.
authorDaniel Jasper <djasper@google.com>
Thu, 25 Apr 2013 13:31:51 +0000 (13:31 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 25 Apr 2013 13:31:51 +0000 (13:31 +0000)
Deeply nested expressions basically break clang-format's memoization.
This patch slightly improves the situations and makes expressions like

  aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(
      aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(
          aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(
              aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(
                  aaaaa(aaaaa())))))))))))))))))))))))))))))))))))))));

work.

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

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

index 7d2041cd3c21c5d23dcce160471c92f3f254864e..a76e0d48a49f742ae2a5b6ad1b609a965ce34207 100644 (file)
@@ -569,6 +569,15 @@ private:
       State.Stack.push_back(
           ParenState(NewIndent, State.Stack.back().LastSpace, AvoidBinPacking,
                      State.Stack.back().NoLineBreak));
+
+      if (Current.NoMoreTokensOnLevel && Current.FakeLParens.empty()) {
+        // This parenthesis was the last token possibly making use of Indent and
+        // LastSpace of the next higher ParenLevel. Thus, erase them to acieve
+        // better memoization results.
+        State.Stack[State.Stack.size() - 2].Indent = 0;
+        State.Stack[State.Stack.size() - 2].LastSpace = 0;
+      }
+
       ++State.ParenLevel;
     }
 
index 276921795aeb1dc1438eec3c2688ac8ba9c1a67b..3e67182f871b6fd2f7f3536d0b90774f295727d4 100644 (file)
@@ -155,6 +155,8 @@ private:
       }
 
       if (CurrentToken->is(tok::r_paren)) {
+        if (CurrentToken->Parent->closesScope())
+          CurrentToken->Parent->MatchingParen->NoMoreTokensOnLevel = true;
         Left->MatchingParen = CurrentToken;
         CurrentToken->MatchingParen = Left;
 
index 0dde1fa903bb5973ff005c5d1501375cb66b88a7..fbb65b62549ba61d39ccde2ccb9c50ccbaac255c 100644 (file)
@@ -77,7 +77,7 @@ public:
         ParameterCount(0), BindingStrength(0), SplitPenalty(0),
         LongestObjCSelectorName(0), Parent(NULL),
         FakeRParens(0), LastInChainOfCalls(false),
-        PartOfMultiVariableDeclStmt(false) {}
+        PartOfMultiVariableDeclStmt(false), NoMoreTokensOnLevel(false) {}
 
   bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); }
 
@@ -184,6 +184,19 @@ public:
   /// Only set if \c Type == \c TT_StartOfName.
   bool PartOfMultiVariableDeclStmt;
 
+  /// \brief Set to \c true for "("-tokens if this is the last token other than
+  /// ")" in the next higher parenthesis level.
+  ///
+  /// If this is \c true, no more formatting decisions have to be made on the
+  /// next higher parenthesis level, enabling optimizations.
+  ///
+  /// Example:
+  /// \code
+  /// aaaaaa(aaaaaa());
+  ///              ^  // Set to true for this parenthesis.
+  /// \endcode
+  bool NoMoreTokensOnLevel;
+
   /// \brief Returns the previous token ignoring comments.
   AnnotatedToken *getPreviousNoneComment() const;
 
index 97d4df8e5e5d8ca01503e8f17109a8c80fba3cfd..055b9d1cb281355c3890288ef665087f3d248a40 100644 (file)
@@ -1702,8 +1702,30 @@ TEST_F(FormatTest, ConstructorInitializers) {
                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
                OnePerLine);
+}
+
+TEST_F(FormatTest, MemoizationTests) {
+  // This breaks if the memoization lookup does not take \c Indent and
+  // \c LastSpace into account.
+  verifyFormat(
+      "extern CFRunLoopTimerRef\n"
+      "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
+      "                     CFTimeInterval interval, CFOptionFlags flags,\n"
+      "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
+      "                     CFRunLoopTimerContext *context);");
+
+  // Deep nesting somewhat works around our memoization.
+  verifyFormat(
+      "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
+      "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
+      "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
+      "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
+      "                aaaaa())))))))))))))))))))))))))))))))))))))));",
+      getLLVMStyleWithColumns(65));
 
   // This test takes VERY long when memoization is broken.
+  FormatStyle OnePerLine = getLLVMStyle();
+  OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
   OnePerLine.BinPackParameters = false;
   std::string input = "Constructor()\n"
                       "    : aaaa(a,\n";