Formatter: Correctly detect ObjC message expressions preceded by a comment.

llvm-svn: 174521
This commit is contained in:
Nico Weber 2013-02-06 16:54:35 +00:00
parent eed92fb10c
commit ac9bde236f
2 changed files with 20 additions and 11 deletions

View File

@ -42,12 +42,15 @@ static bool isBinaryOperator(const AnnotatedToken &Tok) {
} }
// Returns the previous token ignoring comments. // Returns the previous token ignoring comments.
static const AnnotatedToken *getPreviousToken(const AnnotatedToken &Tok) { static AnnotatedToken *getPreviousToken(AnnotatedToken &Tok) {
const AnnotatedToken *PrevToken = Tok.Parent; AnnotatedToken *PrevToken = Tok.Parent;
while (PrevToken != NULL && PrevToken->is(tok::comment)) while (PrevToken != NULL && PrevToken->is(tok::comment))
PrevToken = PrevToken->Parent; PrevToken = PrevToken->Parent;
return PrevToken; return PrevToken;
} }
static const AnnotatedToken *getPreviousToken(const AnnotatedToken &Tok) {
return getPreviousToken(const_cast<AnnotatedToken &>(Tok));
}
// Returns the next token ignoring comments. // Returns the next token ignoring comments.
static const AnnotatedToken *getNextToken(const AnnotatedToken &Tok) { static const AnnotatedToken *getNextToken(const AnnotatedToken &Tok) {
@ -181,12 +184,12 @@ public:
// ')' or ']'), or it could be the start of an Objective-C method // ')' or ']'), or it could be the start of an Objective-C method
// expression. // expression.
AnnotatedToken *Left = CurrentToken->Parent; AnnotatedToken *Left = CurrentToken->Parent;
AnnotatedToken *Parent = getPreviousToken(*Left);
bool StartsObjCMethodExpr = bool StartsObjCMethodExpr =
!Left->Parent || Left->Parent->is(tok::colon) || !Parent || Parent->is(tok::colon) || Parent->is(tok::l_square) ||
Left->Parent->is(tok::l_square) || Left->Parent->is(tok::l_paren) || Parent->is(tok::l_paren) || Parent->is(tok::kw_return) ||
Left->Parent->is(tok::kw_return) || Left->Parent->is(tok::kw_throw) || Parent->is(tok::kw_throw) || isUnaryOperator(*Parent) ||
isUnaryOperator(*Left->Parent) || getBinOpPrecedence(Parent->FormatTok.Tok.getKind(), true, true) >
getBinOpPrecedence(Left->Parent->FormatTok.Tok.getKind(), true, true) >
prec::Unknown; prec::Unknown;
if (StartsObjCMethodExpr) { if (StartsObjCMethodExpr) {
@ -208,10 +211,10 @@ public:
// determineStarAmpUsage() thinks that '*' '[' is allocating an // determineStarAmpUsage() thinks that '*' '[' is allocating an
// array of pointers, but if '[' starts a selector then '*' is a // array of pointers, but if '[' starts a selector then '*' is a
// binary operator. // binary operator.
if (Left->Parent != NULL && if (Parent != NULL &&
(Left->Parent->is(tok::star) || Left->Parent->is(tok::amp)) && (Parent->is(tok::star) || Parent->is(tok::amp)) &&
Left->Parent->Type == TT_PointerOrReference) Parent->Type == TT_PointerOrReference)
Left->Parent->Type = TT_BinaryOperator; Parent->Type = TT_BinaryOperator;
} }
Left->MatchingParen = CurrentToken; Left->MatchingParen = CurrentToken;
CurrentToken->MatchingParen = Left; CurrentToken->MatchingParen = Left;

View File

@ -2422,6 +2422,12 @@ TEST_F(FormatTest, FormatObjCMethodExpr) {
" fraction:1.0\n" " fraction:1.0\n"
" respectFlipped:NO\n" " respectFlipped:NO\n"
" hints:nil];"); " hints:nil];");
verifyFormat(
"scoped_nsobject<NSTextField> message(\n"
" // The frame will be fixed up when |-setMessageText:| is called.\n"
" [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
} }
TEST_F(FormatTest, ObjCAt) { TEST_F(FormatTest, ObjCAt) {