r174840 - Formatter: Initial support for ObjC dictionary literals.
Nico Weber
nicolasweber at gmx.de
Sun Feb 10 12:35:35 PST 2013
Author: nico
Date: Sun Feb 10 14:35:35 2013
New Revision: 174840
URL: http://llvm.org/viewvc/llvm-project?rev=174840&view=rev
Log:
Formatter: Initial support for ObjC dictionary literals.
Before:
@{
foo:
bar
}
;
Now:
@{ foo : bar };
parseBracedList() already does the right thing from an UnwrappedLineParser
perspective, so check for "@{" in all loops that process constructs that can
contain expressions and call parseBracedList() if found.
Modified:
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=174840&r1=174839&r2=174840&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Sun Feb 10 14:35:35 2013
@@ -188,7 +188,8 @@ bool UnwrappedLineParser::parseLevel(boo
return Error;
}
-bool UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels) {
+bool UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
+ unsigned AddLevels) {
assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected");
nextToken();
@@ -265,6 +266,10 @@ void UnwrappedLineParser::parseStructura
switch (FormatTok.Tok.getKind()) {
case tok::at:
nextToken();
+ if (FormatTok.Tok.is(tok::l_brace)) {
+ parseBracedList();
+ break;
+ }
switch (FormatTok.Tok.getObjCKeywordID()) {
case tok::objc_public:
case tok::objc_protected:
@@ -344,6 +349,11 @@ void UnwrappedLineParser::parseStructura
do {
++TokenNumber;
switch (FormatTok.Tok.getKind()) {
+ case tok::at:
+ nextToken();
+ if (FormatTok.Tok.is(tok::l_brace))
+ parseBracedList();
+ break;
case tok::kw_enum:
parseEnum();
break;
@@ -457,6 +467,11 @@ void UnwrappedLineParser::parseParens()
Line->Level -= 1;
break;
}
+ case tok::at:
+ nextToken();
+ if (FormatTok.Tok.is(tok::l_brace))
+ parseBracedList();
+ break;
default:
nextToken();
break;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=174840&r1=174839&r2=174840&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sun Feb 10 14:35:35 2013
@@ -2546,9 +2546,21 @@ TEST_F(FormatTest, ObjCLiterals) {
"NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
- // FIXME: Array and dictionary literals need more work.
verifyFormat("@{");
-
+ verifyFormat("@{}");
+ verifyFormat("@{ @\"one\" : @1 }");
+ verifyFormat("return @{ @\"one\" : @1 };");
+ verifyFormat("@{ @\"one\" : @1, }");
+ verifyFormat("@{ @\"one\" : @{ @2 : @1 } }");
+ verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }");
+ verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }");
+ verifyFormat("[self setDict:@{}");
+ verifyFormat("[self setDict:@{ @1 : @2 }");
+ verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);");
+ verifyFormat(
+ "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };");
+
+ // FIXME: Nested and multi-line array and dictionary literals need more work.
}
TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
More information about the cfe-commits
mailing list