r174863 - Fix formatting of overloaded operator definitions.
Daniel Jasper
djasper at google.com
Mon Feb 11 00:01:18 PST 2013
Author: djasper
Date: Mon Feb 11 02:01:18 2013
New Revision: 174863
URL: http://llvm.org/viewvc/llvm-project?rev=174863&view=rev
Log:
Fix formatting of overloaded operator definitions.
Before:
operatorvoid*();
operator vector< A< A>>();
After:
operator void *();
operator vector<A<A> >();
Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/lib/Format/TokenAnnotator.h
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=174863&r1=174862&r2=174863&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Feb 11 02:01:18 2013
@@ -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::spaceRequiredBetwee
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
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() &&
Modified: cfe/trunk/lib/Format/TokenAnnotator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.h?rev=174863&r1=174862&r2=174863&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.h (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.h Mon Feb 11 02:01:18 2013
@@ -42,7 +42,7 @@ enum TokenType {
TT_ObjCMethodSpecifier,
TT_ObjCProperty,
TT_ObjCSelectorName,
- TT_OverloadedOperator,
+ TT_OverloadedOperatorLParen,
TT_PointerOrReference,
TT_PureVirtualSpecifier,
TT_RangeBasedForLoopColon,
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=174863&r1=174862&r2=174863&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Feb 11 02:01:18 2013
@@ -1541,11 +1541,18 @@ TEST_F(FormatTest, UndestandsOverloadedO
verifyFormat("bool operator()();");
verifyFormat("bool operator[]();");
verifyFormat("operator bool();");
+ verifyFormat("operator int();");
+ verifyFormat("operator void *();");
verifyFormat("operator SomeType<int>();");
+ verifyFormat("operator SomeType<int, int>();");
+ verifyFormat("operator SomeType<SomeType<int> >();");
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<SomeType<int>>();");
}
TEST_F(FormatTest, UnderstandsNewAndDelete) {
More information about the cfe-commits
mailing list