Formatter: Detect ObjC message expressions after 'in' in loop
Before:
for (id foo in[self getStuffFor : bla]) {
}
Now:
for (id foo in [self getStuffFor:bla]) {
}
"in" is treated as loop keyword if the line starts with "for", and as a
regular identifier else. To check for "in", its IdentifierInfo is handed
through a few layers.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174889 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index 8f2e00e..1e814b2 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -78,9 +78,10 @@
/// into template parameter lists.
class AnnotatingParser {
public:
- AnnotatingParser(SourceManager &SourceMgr, Lexer &Lex, AnnotatedLine &Line)
+ AnnotatingParser(SourceManager &SourceMgr, Lexer &Lex, AnnotatedLine &Line,
+ IdentifierInfo &Ident_in)
: SourceMgr(SourceMgr), Lex(Lex), Line(Line), CurrentToken(&Line.First),
- KeywordVirtualFound(false) {
+ KeywordVirtualFound(false), Ident_in(Ident_in) {
Contexts.push_back(Context(1, /*IsExpression=*/ false));
Contexts.back().LookForFunctionName = Line.MustBeDeclaration;
}
@@ -195,6 +196,7 @@
!Parent || Parent->is(tok::colon) || Parent->is(tok::l_square) ||
Parent->is(tok::l_paren) || Parent->is(tok::kw_return) ||
Parent->is(tok::kw_throw) || isUnaryOperator(*Parent) ||
+ Parent->Type == TT_ObjCForIn ||
getBinOpPrecedence(Parent->FormatTok.Tok.getKind(), true, true) >
prec::Unknown;
bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at);
@@ -383,6 +385,11 @@
case tok::kw_template:
parseTemplateDeclaration();
break;
+ case tok::identifier:
+ if (Line.First.is(tok::kw_for) &&
+ Tok->FormatTok.Tok.getIdentifierInfo() == &Ident_in)
+ Tok->Type = TT_ObjCForIn;
+ break;
default:
break;
}
@@ -684,6 +691,7 @@
AnnotatedLine &Line;
AnnotatedToken *CurrentToken;
bool KeywordVirtualFound;
+ IdentifierInfo &Ident_in;
};
/// \brief Parses binary expressions by inserting fake parenthesis based on
@@ -763,7 +771,7 @@
};
void TokenAnnotator::annotate(AnnotatedLine &Line) {
- AnnotatingParser Parser(SourceMgr, Lex, Line);
+ AnnotatingParser Parser(SourceMgr, Lex, Line, Ident_in);
Line.Type = Parser.parseLine();
if (Line.Type == LT_Invalid)
return;