r180264 - Improve clang-format's memoization behavior.
Daniel Jasper
djasper at google.com
Thu Apr 25 06:31:52 PDT 2013
Author: djasper
Date: Thu Apr 25 08:31:51 2013
New Revision: 180264
URL: http://llvm.org/viewvc/llvm-project?rev=180264&view=rev
Log:
Improve clang-format's memoization behavior.
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.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/lib/Format/TokenAnnotator.h
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=180264&r1=180263&r2=180264&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Apr 25 08:31:51 2013
@@ -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;
}
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=180264&r1=180263&r2=180264&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Apr 25 08:31:51 2013
@@ -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;
Modified: cfe/trunk/lib/Format/TokenAnnotator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.h?rev=180264&r1=180263&r2=180264&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.h (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.h Thu Apr 25 08:31:51 2013
@@ -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;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=180264&r1=180263&r2=180264&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Apr 25 08:31:51 2013
@@ -1702,8 +1702,30 @@ TEST_F(FormatTest, ConstructorInitialize
" : 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";
More information about the cfe-commits
mailing list