aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2013-05-06 08:27:33 +0000
committerDaniel Jasper <djasper@google.com>2013-05-06 08:27:33 +0000
commit53e72cdcc0bc217cab33fa56858a0fe5b94ca453 (patch)
treeb3991f8bbdda8776b6421feae5fd14ba7bbb6e19 /lib
parent10f6f065456a2cfb6c2ab5dfedefb930e5e52e9d (diff)
Change indentation when breaking after a type.
clang-format did not indent any declarations/definitions when breaking after the type. With this change, it indents for all declarations but does not indent for function definitions, i.e.: Before: const SomeLongTypeName& some_long_variable_name; typedef SomeLongTypeName SomeLongTypeAlias; const SomeLongReturnType* SomeLongFunctionName(); const SomeLongReturnType* SomeLongFunctionName() { ... } After: const SomeLongTypeName& some_long_variable_name; typedef SomeLongTypeName SomeLongTypeAlias; const SomeLongReturnType* SomeLongFunctionName(); const SomeLongReturnType* SomeLongFunctionName() { ... } While it might seem inconsistent to indent function declarations, but not definitions, there are two reasons for that: - Function declarations are very similar to declarations of function type variables, so there is another side to consistency to consider. - There can be many function declarations on subsequent lines and not indenting can make them harder to identify. Function definitions are already separated by their body and not indenting makes the function name slighly easier to find. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181187 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/Format/Format.cpp3
-rw-r--r--lib/Format/TokenAnnotator.cpp33
-rw-r--r--lib/Format/TokenAnnotator.h8
3 files changed, 24 insertions, 20 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index a76e0d48a4..d251d4f9e6 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -359,7 +359,8 @@ private:
State.Stack.back().VariablePos != 0) {
State.Column = State.Stack.back().VariablePos;
} else if (Previous.ClosesTemplateDeclaration ||
- (Current.Type == TT_StartOfName && State.ParenLevel == 0)) {
+ (Current.Type == TT_StartOfName && State.ParenLevel == 0 &&
+ Line.StartsDefinition)) {
State.Column = State.Stack.back().Indent;
} else if (Current.Type == TT_ObjCSelectorName) {
if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) {
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index fa7ef78451..17abb01d18 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -245,24 +245,25 @@ private:
}
bool parseBrace() {
- // Lines are fine to end with '{'.
- if (CurrentToken == NULL)
- return true;
- ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
- AnnotatedToken *Left = CurrentToken->Parent;
- while (CurrentToken != NULL) {
- if (CurrentToken->is(tok::r_brace)) {
- Left->MatchingParen = CurrentToken;
- CurrentToken->MatchingParen = Left;
- next();
- return true;
+ if (CurrentToken != NULL) {
+ ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
+ AnnotatedToken *Left = CurrentToken->Parent;
+ while (CurrentToken != NULL) {
+ if (CurrentToken->is(tok::r_brace)) {
+ Left->MatchingParen = CurrentToken;
+ CurrentToken->MatchingParen = Left;
+ next();
+ return true;
+ }
+ if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
+ return false;
+ updateParameterCount(Left, CurrentToken);
+ if (!consumeToken())
+ return false;
}
- if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
- return false;
- updateParameterCount(Left, CurrentToken);
- if (!consumeToken())
- return false;
}
+ // No closing "}" found, this probably starts a definition.
+ Line.StartsDefinition = true;
return true;
}
diff --git a/lib/Format/TokenAnnotator.h b/lib/Format/TokenAnnotator.h
index fbb65b6254..b364082391 100644
--- a/lib/Format/TokenAnnotator.h
+++ b/lib/Format/TokenAnnotator.h
@@ -209,8 +209,8 @@ public:
AnnotatedLine(const UnwrappedLine &Line)
: First(Line.Tokens.front()), Level(Line.Level),
InPPDirective(Line.InPPDirective),
- MustBeDeclaration(Line.MustBeDeclaration),
- MightBeFunctionDecl(false) {
+ MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
+ StartsDefinition(false) {
assert(!Line.Tokens.empty());
AnnotatedToken *Current = &First;
for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(),
@@ -226,7 +226,8 @@ public:
: First(Other.First), Type(Other.Type), Level(Other.Level),
InPPDirective(Other.InPPDirective),
MustBeDeclaration(Other.MustBeDeclaration),
- MightBeFunctionDecl(Other.MightBeFunctionDecl) {
+ MightBeFunctionDecl(Other.MightBeFunctionDecl),
+ StartsDefinition(Other.StartsDefinition) {
Last = &First;
while (!Last->Children.empty()) {
Last->Children[0].Parent = Last;
@@ -242,6 +243,7 @@ public:
bool InPPDirective;
bool MustBeDeclaration;
bool MightBeFunctionDecl;
+ bool StartsDefinition;
};
inline prec::Level getPrecedence(const AnnotatedToken &Tok) {