arphaman created this revision. arphaman added reviewers: benhamilton, krasimir, djasper. Herald added subscribers: dexonsmith, jkorous.
The commit r322690 introduced support for ObjC detection in header files. Unfortunately some C headers that use designated initializers are now incorrectly detected as Objective-C. This patch fixes it by ensuring `[ ... ]` is not annotated as an Objective-C message send when the expression is followed by `=` as it's not a commonly used Objective-C pattern. rdar://45504376 Repository: rC Clang https://reviews.llvm.org/D56226 Files: lib/Format/TokenAnnotator.cpp unittests/Format/FormatTestObjC.cpp Index: unittests/Format/FormatTestObjC.cpp =================================================================== --- unittests/Format/FormatTestObjC.cpp +++ unittests/Format/FormatTestObjC.cpp @@ -166,6 +166,14 @@ EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); } +TEST(FormatTestObjCStyle, AvoidDetectingDesignatedInitializersAsObjCInHeaders) { + auto Style = getStyle("LLVM", "a.h", "none", + "static const char *names[] = {[0] = \"foo\",\n" + "[kBar] = \"bar\"};"); + ASSERT_TRUE((bool)Style); + EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language); +} + TEST_F(FormatTestObjC, FormatObjCTryCatch) { verifyFormat("@try {\n" " f();\n" Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -495,9 +495,12 @@ if (CurrentToken->is(tok::r_square)) { if (IsCpp11AttributeSpecifier) CurrentToken->Type = TT_AttributeSquare; - else if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) && + else if (CurrentToken->Next && + (CurrentToken->Next->is(tok::l_paren) || + CurrentToken->Next->is(tok::equal)) && Left->is(TT_ObjCMethodExpr)) { - // An ObjC method call is rarely followed by an open parenthesis. + // An ObjC method call is rarely followed by an open parenthesis or + // an assignment. // FIXME: Do we incorrectly label ":" with this? StartsObjCMethodExpr = false; Left->Type = TT_Unknown;
Index: unittests/Format/FormatTestObjC.cpp =================================================================== --- unittests/Format/FormatTestObjC.cpp +++ unittests/Format/FormatTestObjC.cpp @@ -166,6 +166,14 @@ EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); } +TEST(FormatTestObjCStyle, AvoidDetectingDesignatedInitializersAsObjCInHeaders) { + auto Style = getStyle("LLVM", "a.h", "none", + "static const char *names[] = {[0] = \"foo\",\n" + "[kBar] = \"bar\"};"); + ASSERT_TRUE((bool)Style); + EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language); +} + TEST_F(FormatTestObjC, FormatObjCTryCatch) { verifyFormat("@try {\n" " f();\n" Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -495,9 +495,12 @@ if (CurrentToken->is(tok::r_square)) { if (IsCpp11AttributeSpecifier) CurrentToken->Type = TT_AttributeSquare; - else if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) && + else if (CurrentToken->Next && + (CurrentToken->Next->is(tok::l_paren) || + CurrentToken->Next->is(tok::equal)) && Left->is(TT_ObjCMethodExpr)) { - // An ObjC method call is rarely followed by an open parenthesis. + // An ObjC method call is rarely followed by an open parenthesis or + // an assignment. // FIXME: Do we incorrectly label ":" with this? StartsObjCMethodExpr = false; Left->Type = TT_Unknown;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits