From 35d2dc765e8e69f6710104f5fd6109a1f49c085e Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 11 Feb 2013 08:01:18 +0000 Subject: [PATCH] Fix formatting of overloaded operator definitions. Before: operatorvoid*(); operator vector< A< A>>(); After: operator void *(); operator vector >(); llvm-svn: 174863 --- clang/lib/Format/TokenAnnotator.cpp | 28 ++++++++++++--------------- clang/lib/Format/TokenAnnotator.h | 2 +- clang/unittests/Format/FormatTest.cpp | 7 +++++++ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 44146aab9bcb..aecc24ca9c3e 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -369,19 +369,13 @@ public: Tok->Type = TT_BinaryOperator; break; case tok::kw_operator: - if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) { - CurrentToken->Type = TT_OverloadedOperator; - next(); - if (CurrentToken != NULL && CurrentToken->is(tok::r_paren)) { - CurrentToken->Type = TT_OverloadedOperator; - next(); - } - } else { - while (CurrentToken != NULL && CurrentToken->isNot(tok::l_paren)) { - CurrentToken->Type = TT_OverloadedOperator; - next(); - } + while (CurrentToken && CurrentToken->isNot(tok::l_paren)) { + if (CurrentToken->is(tok::star) || CurrentToken->is(tok::amp)) + CurrentToken->Type = TT_PointerOrReference; + consumeToken(); } + if (CurrentToken) + CurrentToken->Type = TT_OverloadedOperatorLParen; break; case tok::question: parseConditional(); @@ -962,6 +956,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, const AnnotatedToken &Tok) { + if (Tok.FormatTok.Tok.getIdentifierInfo() && + Tok.Parent->FormatTok.Tok.getIdentifierInfo()) + return true; // Never ever merge two identifiers. if (Line.Type == LT_ObjCMethodDecl) { if (Tok.Parent->Type == TT_ObjCMethodSpecifier) return true; @@ -977,10 +974,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, return true; if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen) return true; - if (Tok.Type == TT_OverloadedOperator) - return Tok.is(tok::identifier) || Tok.is(tok::kw_new) || - Tok.is(tok::kw_delete) || Tok.is(tok::kw_bool); - if (Tok.Parent->Type == TT_OverloadedOperator) + if (Tok.Parent->FormatTok.Tok.is(tok::kw_operator)) + return false; + if (Tok.Type == TT_OverloadedOperatorLParen) return false; if (Tok.is(tok::colon)) return Line.First.isNot(tok::kw_case) && !Tok.Children.empty() && diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h index 8815d799b09f..cc86bf8b85c3 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -42,7 +42,7 @@ enum TokenType { TT_ObjCMethodSpecifier, TT_ObjCProperty, TT_ObjCSelectorName, - TT_OverloadedOperator, + TT_OverloadedOperatorLParen, TT_PointerOrReference, TT_PureVirtualSpecifier, TT_RangeBasedForLoopColon, diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 12a37159f41f..1865b5059c41 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -1541,11 +1541,18 @@ TEST_F(FormatTest, UndestandsOverloadedOperators) { verifyFormat("bool operator()();"); verifyFormat("bool operator[]();"); verifyFormat("operator bool();"); + verifyFormat("operator int();"); + verifyFormat("operator void *();"); verifyFormat("operator SomeType();"); + verifyFormat("operator SomeType();"); + verifyFormat("operator SomeType >();"); verifyFormat("void *operator new(std::size_t size);"); verifyFormat("void *operator new[](std::size_t size);"); verifyFormat("void operator delete(void *ptr);"); verifyFormat("void operator delete[](void *ptr);"); + + verifyGoogleFormat("operator void*();"); + verifyGoogleFormat("operator SomeType>();"); } TEST_F(FormatTest, UnderstandsNewAndDelete) {