r352125 - [clang-format] square parens with one token are not Objective-C message sends
Alex Lorenz via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 24 15:07:59 PST 2019
Author: arphaman
Date: Thu Jan 24 15:07:58 2019
New Revision: 352125
URL: http://llvm.org/viewvc/llvm-project?rev=352125&view=rev
Log:
[clang-format] square parens with one token are not Objective-C message sends
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 commit fixes it by ensuring that `[ token ]` is not annotated as an
Objective-C message send.
rdar://45504376
Differential Revision: https://reviews.llvm.org/D56226
Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTestObjC.cpp
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=352125&r1=352124&r2=352125&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Jan 24 15:07:58 2019
@@ -494,9 +494,14 @@ private:
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->Previous &&
+ CurrentToken->Previous->Previous == Left)) &&
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. It
+ // also can't be composed of just one token, unless it's a macro that
+ // will be expanded to more tokens.
// FIXME: Do we incorrectly label ":" with this?
StartsObjCMethodExpr = false;
Left->Type = TT_Unknown;
Modified: cfe/trunk/unittests/Format/FormatTestObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestObjC.cpp?rev=352125&r1=352124&r2=352125&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestObjC.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestObjC.cpp Thu Jan 24 15:07:58 2019
@@ -165,6 +165,20 @@ TEST(FormatTestObjCStyle, DetectsObjCInH
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);
+
+ Style = getStyle("LLVM", "a.h", "none",
+ "static const char *names[] = {[0] EQ \"foo\",\n"
+ "[kBar] EQ \"bar\"};");
+ ASSERT_TRUE((bool)Style);
+ EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
+}
+
TEST_F(FormatTestObjC, FormatObjCTryCatch) {
verifyFormat("@try {\n"
" f();\n"
More information about the cfe-commits
mailing list