summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang/lib/Format/FormatToken.cpp3
-rw-r--r--clang/lib/Format/FormatToken.h8
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp7
-rw-r--r--clang/unittests/Format/FormatTest.cpp11
4 files changed, 22 insertions, 7 deletions
diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp
index 748e2fd84a7..16600f24ee8 100644
--- a/clang/lib/Format/FormatToken.cpp
+++ b/clang/lib/Format/FormatToken.cpp
@@ -92,7 +92,8 @@ static unsigned CodePointsBetween(const FormatToken *Begin,
void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
// FIXME: At some point we might want to do this for other lists, too.
- if (!Token->MatchingParen || Token->isNot(tok::l_brace))
+ if (!Token->MatchingParen || Token->isNot(tok::l_brace) ||
+ Token->NestingLevel != 0)
return;
FormatToken *ItemBegin = Token->Next;
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index a8d54ef2640..fa676b39736 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -97,8 +97,8 @@ struct FormatToken {
BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
CanBreakBefore(false), ClosesTemplateDeclaration(false),
ParameterCount(0), PackingKind(PPK_Inconclusive), TotalLength(0),
- UnbreakableTailLength(0), BindingStrength(0), SplitPenalty(0),
- LongestObjCSelectorName(0), FakeRParens(0),
+ UnbreakableTailLength(0), BindingStrength(0), NestingLevel(0),
+ SplitPenalty(0), LongestObjCSelectorName(0), FakeRParens(0),
StartsBinaryExpression(false), EndsBinaryExpression(false),
LastInChainOfCalls(false), PartOfMultiVariableDeclStmt(false),
MatchingParen(NULL), Previous(NULL), Next(NULL),
@@ -207,6 +207,10 @@ struct FormatToken {
/// operator precedence, parenthesis nesting, etc.
unsigned BindingStrength;
+ /// \brief The nesting level of this token, i.e. the number of surrounding (),
+ /// [], {} or <>.
+ unsigned NestingLevel;
+
/// \brief Penalty for inserting a line break before this token.
unsigned SplitPenalty;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d4880a472d8..d22facb9220 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -529,6 +529,7 @@ private:
if (CurrentToken != NULL) {
determineTokenType(*CurrentToken);
CurrentToken->BindingStrength = Contexts.back().BindingStrength;
+ CurrentToken->NestingLevel = Contexts.size() - 1;
}
if (CurrentToken != NULL)
@@ -1141,8 +1142,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
return 3;
if (Left.Type == TT_StartOfName)
return 20;
- if (InFunctionDecl && Right.BindingStrength == 1)
- // FIXME: Clean up hack of using BindingStrength to find top-level names.
+ if (InFunctionDecl && Right.NestingLevel == 0)
return Style.PenaltyReturnTypeOnItsOwnLine;
return 200;
}
@@ -1396,9 +1396,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
return true;
} else if (Right.Previous->ClosesTemplateDeclaration &&
Right.Previous->MatchingParen &&
- Right.Previous->MatchingParen->BindingStrength == 1 &&
+ Right.Previous->MatchingParen->NestingLevel == 0 &&
Style.AlwaysBreakTemplateDeclarations) {
- // FIXME: Fix horrible hack of using BindingStrength to find top-level <>.
return true;
} else if (Right.Type == TT_CtorInitializerComma &&
Style.BreakConstructorInitializersBeforeComma &&
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 2860bbbedd6..e2f8d2fa65c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -4854,6 +4854,17 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
" { aaaaaaaaaaaaaaaaaaaaa },\n"
" { aaaaaaaaaaaaaaaaa } };",
getLLVMStyleWithColumns(60));
+
+ // No column layout for nested lists.
+ // FIXME: For some nested lists, we can do better.
+ verifyFormat(
+ "SomeStruct my_struct_array = {\n"
+ " { aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
+ " aaaaaaaaaaaaa, aaaaaaa, aaa },\n"
+ " { aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa },\n"
+ " { aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa },\n"
+ "};");
}
TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
OpenPOWER on IntegriCloud