r232042 - clang-format: [Java] Support anonymous classes after = and return.
Daniel Jasper
djasper at google.com
Thu Mar 12 07:44:29 PDT 2015
Author: djasper
Date: Thu Mar 12 09:44:29 2015
New Revision: 232042
URL: http://llvm.org/viewvc/llvm-project?rev=232042&view=rev
Log:
clang-format: [Java] Support anonymous classes after = and return.
Before:
A a = new A(){public String toString(){return "NotReallyA";
}
}
;
After:
A a = return new A() {
public String toString() {
return "NotReallyA";
}
};
This fixes llvm.org/PR22878.
Modified:
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.h
cfe/trunk/unittests/Format/FormatTestJava.cpp
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=232042&r1=232041&r2=232042&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Thu Mar 12 09:44:29 2015
@@ -868,6 +868,9 @@ void UnwrappedLineParser::parseStructura
case tok::l_square:
parseSquare();
break;
+ case tok::kw_new:
+ parseNew();
+ break;
default:
nextToken();
break;
@@ -1274,6 +1277,31 @@ void UnwrappedLineParser::parseNamespace
// FIXME: Add error handling.
}
+void UnwrappedLineParser::parseNew() {
+ assert(FormatTok->is(tok::kw_new) && "'new' expected");
+ nextToken();
+ if (Style.Language != FormatStyle::LK_Java)
+ return;
+
+ // In Java, we can parse everything up to the parens, which aren't optional.
+ do {
+ // There should not be a ;, { or } before the new's open paren.
+ if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
+ return;
+
+ // Consume the parens.
+ if (FormatTok->is(tok::l_paren)) {
+ parseParens();
+
+ // If there is a class body of an anonymous class, consume that as child.
+ if (FormatTok->is(tok::l_brace))
+ parseChildBlock();
+ return;
+ }
+ nextToken();
+ } while (!eof());
+}
+
void UnwrappedLineParser::parseForOrWhileLoop() {
assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
FormatTok->IsForEachMacro) &&
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=232042&r1=232041&r2=232042&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Thu Mar 12 09:44:29 2015
@@ -95,6 +95,7 @@ private:
void parseCaseLabel();
void parseSwitch();
void parseNamespace();
+ void parseNew();
void parseAccessSpecifier();
void parseEnum();
void parseJavaEnumBody();
Modified: cfe/trunk/unittests/Format/FormatTestJava.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJava.cpp?rev=232042&r1=232041&r2=232042&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJava.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJava.cpp Thu Mar 12 09:44:29 2015
@@ -153,6 +153,19 @@ TEST_F(FormatTestJava, ClassDeclarations
"}");
}
+TEST_F(FormatTestJava, AnonymousClasses) {
+ verifyFormat("return new A() {\n"
+ " public String toString() {\n"
+ " return \"NotReallyA\";\n"
+ " }\n"
+ "};");
+ verifyFormat("A a = new A() {\n"
+ " public String toString() {\n"
+ " return \"NotReallyA\";\n"
+ " }\n"
+ "};");
+}
+
TEST_F(FormatTestJava, EnumDeclarations) {
verifyFormat("enum SomeThing { ABC, CDE }");
verifyFormat("enum SomeThing {\n"
More information about the cfe-commits
mailing list