diff options
author | Daniel Jasper <djasper@google.com> | 2013-01-02 15:46:59 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-01-02 15:46:59 +0000 |
commit | ef5b9c3d38a1f6d0921591cb2749e529a8cc4a2e (patch) | |
tree | 6df00a7270552366135c9e304f8ad8553c8c5642 | |
parent | 1f0754bf761f7d4910348ce3aba867d34473bceb (diff) |
Correctly format pointers and references in casts.
This fixes llvm.org/PR14747.
Before: Type *A = (Type * ) P;
After: Type *A = (Type *) P;
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171390 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Format/Format.cpp | 18 | ||||
-rw-r--r-- | unittests/Format/FormatTest.cpp | 3 |
2 files changed, 14 insertions, 7 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index c08bcf4f5c..60b2f56745 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -822,19 +822,23 @@ private: TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index, bool IsRHS) { if (Index == Annotations.size()) return TokenAnnotation::TT_Unknown; + const FormatToken &PrevToken = Line.Tokens[Index - 1]; + const FormatToken &NextToken = Line.Tokens[Index + 1]; - if (Index == 0 || Line.Tokens[Index - 1].Tok.is(tok::l_paren) || - Line.Tokens[Index - 1].Tok.is(tok::comma) || - Line.Tokens[Index - 1].Tok.is(tok::kw_return) || - Line.Tokens[Index - 1].Tok.is(tok::colon) || + if (Index == 0 || PrevToken.Tok.is(tok::l_paren) || + PrevToken.Tok.is(tok::comma) || PrevToken.Tok.is(tok::kw_return) || + PrevToken.Tok.is(tok::colon) || Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator) return TokenAnnotation::TT_UnaryOperator; - if (Line.Tokens[Index - 1].Tok.isLiteral() || - Line.Tokens[Index + 1].Tok.isLiteral() || - Line.Tokens[Index + 1].Tok.is(tok::kw_sizeof)) + if (PrevToken.Tok.isLiteral() || NextToken.Tok.isLiteral() || + NextToken.Tok.is(tok::kw_sizeof)) return TokenAnnotation::TT_BinaryOperator; + if (NextToken.Tok.is(tok::comma) || NextToken.Tok.is(tok::r_paren) || + NextToken.Tok.is(tok::greater)) + return TokenAnnotation::TT_PointerOrReference; + // It is very unlikely that we are going to find a pointer or reference type // definition on the RHS of an assignment. if (IsRHS) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 22da93ebe3..b52a9fd7c3 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -699,6 +699,9 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyFormat("A<int **> a;"); verifyFormat("A<int *, int *> a;"); verifyFormat("A<int **, int **> a;"); + verifyFormat("Type *A = static_cast<Type *>(P);"); + verifyFormat("Type *A = (Type *) P;"); + verifyFormat("Type *A = (vector<Type *, int *>) P;"); verifyGoogleFormat("int main(int argc, char** argv) {\n}"); verifyGoogleFormat("A<int*> a;"); |