aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2013-01-07 19:05:19 +0000
committerNico Weber <nicolasweber@gmx.de>2013-01-07 19:05:19 +0000
commit6092d4ed09a2368871993d9a45717f24394329ce (patch)
tree2c711fa6618bce45d718fb4ad47c9a61d201d651
parent27c2cb24d5e2e9fda68b929c6d03c761196d8e2d (diff)
Formatter: Support @public/@protected/@package/@private.
@package is an Objective-C 2 feature, so turn on ObjC2 as well. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171766 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Format/Format.cpp15
-rw-r--r--lib/Format/UnwrappedLineParser.cpp12
-rw-r--r--unittests/Format/FormatTest.cpp29
3 files changed, 54 insertions, 2 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 7a6b6e2081..5c433834c8 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -510,8 +510,19 @@ private:
if (Newlines == 0 && !Token.IsFirst)
Newlines = 1;
unsigned Indent = Line.Level * 2;
- if ((Token.Tok.is(tok::kw_public) || Token.Tok.is(tok::kw_protected) ||
- Token.Tok.is(tok::kw_private)) &&
+
+ bool IsAccessModifier = false;
+ if (Token.Tok.is(tok::kw_public) || Token.Tok.is(tok::kw_protected) ||
+ Token.Tok.is(tok::kw_private))
+ IsAccessModifier = true;
+ else if (Token.Tok.is(tok::at) && Line.Tokens.size() > 1 &&
+ (Line.Tokens[1].Tok.isObjCAtKeyword(tok::objc_public) ||
+ Line.Tokens[1].Tok.isObjCAtKeyword(tok::objc_protected) ||
+ Line.Tokens[1].Tok.isObjCAtKeyword(tok::objc_package) ||
+ Line.Tokens[1].Tok.isObjCAtKeyword(tok::objc_private)))
+ IsAccessModifier = true;
+
+ if (IsAccessModifier &&
static_cast<int>(Indent) + Style.AccessModifierOffset >= 0)
Indent += Style.AccessModifierOffset;
if (!Line.InPPDirective || Token.HasUnescapedNewline)
diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp
index d7220259b7..adb536324a 100644
--- a/lib/Format/UnwrappedLineParser.cpp
+++ b/lib/Format/UnwrappedLineParser.cpp
@@ -198,6 +198,18 @@ void UnwrappedLineParser::parseStructuralElement() {
int TokenNumber = 0;
switch (FormatTok.Tok.getKind()) {
+ case tok::at:
+ nextToken();
+ switch (FormatTok.Tok.getObjCKeywordID()) {
+ case tok::objc_public:
+ case tok::objc_protected:
+ case tok::objc_package:
+ case tok::objc_private:
+ return parseAccessSpecifier();
+ default:
+ break;
+ }
+ break;
case tok::kw_namespace:
parseNamespace();
return;
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 2ba4765cc7..d3144432d4 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -30,6 +30,7 @@ protected:
LangOpts.CPlusPlus = 1;
LangOpts.CPlusPlus11 = 1;
LangOpts.ObjC1 = 1;
+ LangOpts.ObjC2 = 1;
Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts);
tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
Ranges);
@@ -424,6 +425,34 @@ TEST_F(FormatTest, FormatObjCTryCatch) {
"}");
}
+TEST_F(FormatTest, FormatObjCInterface) {
+ verifyFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
+ "@public\n"
+ " int field1;\n"
+ "@protected\n"
+ " int field2;\n"
+ "@private\n"
+ " int field3;\n"
+ "@package\n"
+ " int field4;\n"
+ "}\n"
+ "+ (id)init;\n"
+ "@end");
+
+ verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
+ " @public\n"
+ " int field1;\n"
+ " @protected\n"
+ " int field2;\n"
+ " @private\n"
+ " int field3;\n"
+ " @package\n"
+ " int field4;\n"
+ "}\n"
+ "+ (id)init;\n"
+ "@end");
+}
+
TEST_F(FormatTest, StaticInitializers) {
verifyFormat("static SomeClass SC = { 1, 'a' };");