r185687 - Improve detection for preventing certain kind of formatting patterns.
Daniel Jasper
djasper at google.com
Fri Jul 5 02:14:35 PDT 2013
Author: djasper
Date: Fri Jul 5 04:14:35 2013
New Revision: 185687
URL: http://llvm.org/viewvc/llvm-project?rev=185687&view=rev
Log:
Improve detection for preventing certain kind of formatting patterns.
This is a better implementation of r183097. The main purpose is to
prevent certain constructs to be formatted "like a block of text".
Before:
aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa = new aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
After:
aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa =
new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(
bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] =
(*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
Modified:
cfe/trunk/lib/Format/Format.cpp
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=185687&r1=185686&r2=185687&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Jul 5 04:14:35 2013
@@ -278,7 +278,7 @@ public:
State.ParenLevel = 0;
State.StartOfStringLiteral = 0;
State.StartOfLineLevel = State.ParenLevel;
- State.LowestCallLevel = State.ParenLevel;
+ State.LowestLevelOnLine = State.ParenLevel;
State.IgnoreStackForComparison = false;
// The first token has already been indented and thus consumed.
@@ -434,8 +434,8 @@ private:
/// \brief The \c ParenLevel at the start of this line.
unsigned StartOfLineLevel;
- /// \brief The lowest \c ParenLevel of "." or "->" on the current line.
- unsigned LowestCallLevel;
+ /// \brief The lowest \c ParenLevel on the current line.
+ unsigned LowestLevelOnLine;
/// \brief The start column of the string literal, if we're in a string
/// literal sequence, 0 otherwise.
@@ -473,8 +473,8 @@ private:
return ParenLevel < Other.ParenLevel;
if (StartOfLineLevel != Other.StartOfLineLevel)
return StartOfLineLevel < Other.StartOfLineLevel;
- if (LowestCallLevel != Other.LowestCallLevel)
- return LowestCallLevel < Other.LowestCallLevel;
+ if (LowestLevelOnLine != Other.LowestLevelOnLine)
+ return LowestLevelOnLine < Other.LowestLevelOnLine;
if (StartOfStringLiteral != Other.StartOfStringLiteral)
return StartOfStringLiteral < Other.StartOfStringLiteral;
if (IgnoreStackForComparison || Other.IgnoreStackForComparison)
@@ -580,7 +580,7 @@ private:
Current.Type != TT_DesignatedInitializerPeriod)
State.Stack.back().LastSpace += Current.CodePointCount;
State.StartOfLineLevel = State.ParenLevel;
- State.LowestCallLevel = State.ParenLevel;
+ State.LowestLevelOnLine = State.ParenLevel;
// Any break on this level means that the parent level has been broken
// and we need to avoid bin packing there.
@@ -685,13 +685,14 @@ private:
State.Stack.back().FirstLessLess = State.Column;
if (Current.is(tok::question))
State.Stack.back().QuestionColumn = State.Column;
- if (Current.isOneOf(tok::period, tok::arrow)) {
- State.LowestCallLevel = std::min(State.LowestCallLevel, State.ParenLevel);
- if (Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
- State.Stack.back().StartOfFunctionCall =
- Current.LastInChainOfCalls ? 0
- : State.Column + Current.CodePointCount;
- }
+ if (!Current.opensScope() && !Current.closesScope())
+ State.LowestLevelOnLine =
+ std::min(State.LowestLevelOnLine, State.ParenLevel);
+ if (Current.isOneOf(tok::period, tok::arrow) &&
+ Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
+ State.Stack.back().StartOfFunctionCall =
+ Current.LastInChainOfCalls ? 0
+ : State.Column + Current.CodePointCount;
if (Current.Type == TT_CtorInitializerColon) {
// Indent 2 from the column, so:
// SomeClass::SomeClass()
@@ -1047,7 +1048,8 @@ private:
// SomeParameter, OtherParameter).DoSomething(
// ...
// As they hide "DoSomething" and are generally bad for readability.
- if (Previous.opensScope() && State.LowestCallLevel < State.StartOfLineLevel)
+ if (Previous.opensScope() &&
+ State.LowestLevelOnLine < State.StartOfLineLevel)
return false;
return !State.Stack.back().NoLineBreak;
}
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=185687&r1=185686&r2=185687&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Jul 5 04:14:35 2013
@@ -3083,6 +3083,15 @@ TEST_F(FormatTest, WrapsTemplateDeclarat
verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
" ccccccccccccccccccccccccccccccccccccccccccccccc);");
+ verifyFormat(
+ "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
+ " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbb);",
+ getLLVMStyleWithColumns(72));
}
TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
@@ -3488,6 +3497,11 @@ TEST_F(FormatTest, FormatsCasts) {
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
+ verifyFormat(
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] =\n"
+ " (*cccccccccccccccc)[\n"
+ " dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
}
TEST_F(FormatTest, FormatsFunctionTypes) {
More information about the cfe-commits
mailing list