r325221 - [clang-format] Improve ObjC headers detection
Jacek Olesiak via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 15 00:47:57 PST 2018
Author: jolesiak
Date: Thu Feb 15 00:47:56 2018
New Revision: 325221
URL: http://llvm.org/viewvc/llvm-project?rev=325221&view=rev
Log:
[clang-format] Improve ObjC headers detection
Summary: Detect ObjC characteristic types when they start a line and add additional keywords.
Reviewers: benhamilton
Reviewed By: benhamilton
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D43124
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTestObjC.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=325221&r1=325220&r2=325221&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Feb 15 00:47:56 2018
@@ -1440,7 +1440,9 @@ private:
"NSAffineTransform",
"NSArray",
"NSAttributedString",
+ "NSBundle",
"NSCache",
+ "NSCalendar",
"NSCharacterSet",
"NSCountedSet",
"NSData",
@@ -1466,6 +1468,7 @@ private:
"NSMutableString",
"NSNumber",
"NSNumberFormatter",
+ "NSObject",
"NSOrderedSet",
"NSPoint",
"NSPointerArray",
@@ -1475,17 +1478,19 @@ private:
"NSSet",
"NSSize",
"NSString",
+ "NSTimeZone",
"NSUInteger",
"NSURL",
"NSURLComponents",
"NSURLQueryItem",
"NSUUID",
+ "NSValue",
};
for (auto &Line : AnnotatedLines) {
- for (FormatToken *FormatTok = Line->First->Next; FormatTok;
+ for (FormatToken *FormatTok = Line->First; FormatTok;
FormatTok = FormatTok->Next) {
- if ((FormatTok->Previous->is(tok::at) &&
+ if ((FormatTok->Previous && FormatTok->Previous->is(tok::at) &&
(FormatTok->isObjCAtKeyword(tok::objc_interface) ||
FormatTok->isObjCAtKeyword(tok::objc_implementation) ||
FormatTok->isObjCAtKeyword(tok::objc_protocol) ||
Modified: cfe/trunk/unittests/Format/FormatTestObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestObjC.cpp?rev=325221&r1=325220&r2=325221&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestObjC.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestObjC.cpp Thu Feb 15 00:47:56 2018
@@ -113,10 +113,6 @@ TEST(FormatTestObjCStyle, DetectsObjCInH
ASSERT_TRUE((bool)Style);
EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
- Style = getStyle("{}", "a.h", "none", "extern NSString *kFoo;\n");
- ASSERT_TRUE((bool)Style);
- EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
-
Style =
getStyle("{}", "a.h", "none", "typedef NS_ENUM(NSInteger, Foo) {};\n");
ASSERT_TRUE((bool)Style);
@@ -126,10 +122,6 @@ TEST(FormatTestObjCStyle, DetectsObjCInH
ASSERT_TRUE((bool)Style);
EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
- Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n");
- ASSERT_TRUE((bool)Style);
- EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
-
Style =
getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }\n");
ASSERT_TRUE((bool)Style);
@@ -154,6 +146,23 @@ TEST(FormatTestObjCStyle, DetectsObjCInH
"inline void Foo() { int foo[] = {1, 2, 3}; }\n");
ASSERT_TRUE((bool)Style);
EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
+
+ // ObjC characteristic types.
+ Style = getStyle("{}", "a.h", "none", "extern NSString *kFoo;\n");
+ ASSERT_TRUE((bool)Style);
+ EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+
+ Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n");
+ ASSERT_TRUE((bool)Style);
+ EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+
+ Style = getStyle("{}", "a.h", "none", "NSObject *Foo();\n");
+ ASSERT_TRUE((bool)Style);
+ EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+
+ Style = getStyle("{}", "a.h", "none", "NSSet *Foo();\n");
+ ASSERT_TRUE((bool)Style);
+ EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
}
TEST_F(FormatTestObjC, FormatObjCTryCatch) {
More information about the cfe-commits
mailing list