Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -285,6 +285,16 @@ else Left->PackingKind = PPK_OnePerLine; + if (MightBeObjCForRangeLoop) { + FormatToken *ForInToken = Left; + while (ForInToken && ForInToken != CurrentToken) { + if (ForInToken->is(Keywords.kw_in)) { + ForInToken->Type = TT_ObjCForIn; + break; + } + ForInToken = ForInToken->Next; + } + } next(); return true; } @@ -303,8 +313,6 @@ Contexts.back().IsExpression = false; if (CurrentToken->isOneOf(tok::semi, tok::colon)) MightBeObjCForRangeLoop = false; - if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) - CurrentToken->Type = TT_ObjCForIn; // When we discover a 'new', we set CanBeExpression to 'false' in order to // parse the type correctly. Reset that after a comma. if (CurrentToken->is(tok::comma)) Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -11999,6 +11999,31 @@ EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); } +TEST_F(FormatTest, GuessLanguageWithForIn) { + EXPECT_EQ(FormatStyle::LK_Cpp, + guessLanguage("foo.h", "for (Foo *x = 0; x != in; x++) {}")); + EXPECT_EQ(FormatStyle::LK_ObjC, + guessLanguage("foo.h", "for (Foo *x in bar) {}")); + EXPECT_EQ(FormatStyle::LK_ObjC, + guessLanguage("foo.h", "for (Foo *x in [bar baz]) {}")); + EXPECT_EQ(FormatStyle::LK_ObjC, + guessLanguage("foo.h", "for (Foo *x in [bar baz:blech]) {}")); + EXPECT_EQ( + FormatStyle::LK_ObjC, + guessLanguage("foo.h", "for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {}")); + EXPECT_EQ(FormatStyle::LK_ObjC, + guessLanguage("foo.h", "for (Foo *x in [bar baz:^{[uh oh];}]) {}")); + EXPECT_EQ(FormatStyle::LK_Cpp, + guessLanguage("foo.h", "Foo *x; for (x = 0; x != in; x++) {}")); + EXPECT_EQ(FormatStyle::LK_ObjC, + guessLanguage("foo.h", "Foo *x; for (x in y) {}")); + EXPECT_EQ( + FormatStyle::LK_Cpp, + guessLanguage( + "foo.h", + "for (const Foo& baz = in.value(); !baz.at_end(); ++baz) {}")); +} + } // end namespace } // end namespace format } // end namespace clang