[cfe-commits] r171766 - in /cfe/trunk: lib/Format/Format.cpp lib/Format/UnwrappedLineParser.cpp unittests/Format/FormatTest.cpp
Nico Weber
nicolasweber at gmx.de
Mon Jan 7 11:05:19 PST 2013
Author: nico
Date: Mon Jan 7 13:05:19 2013
New Revision: 171766
URL: http://llvm.org/viewvc/llvm-project?rev=171766&view=rev
Log:
Formatter: Support @public/@protected/@package/@private.
@package is an Objective-C 2 feature, so turn on ObjC2 as well.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=171766&r1=171765&r2=171766&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Jan 7 13:05:19 2013
@@ -510,8 +510,19 @@
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)
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=171766&r1=171765&r2=171766&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Jan 7 13:05:19 2013
@@ -198,6 +198,18 @@
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;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=171766&r1=171765&r2=171766&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan 7 13:05:19 2013
@@ -30,6 +30,7 @@
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, 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' };");
More information about the cfe-commits
mailing list