Skip to content
Snippets Groups Projects
Commit d216a8f7 authored by Daniel Jasper's avatar Daniel Jasper
Browse files

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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@232042 91177308-0d34-0410-b5e6-96231b3b80d8
parent bcf99497
No related branches found
No related tags found
No related merge requests found
...@@ -868,6 +868,9 @@ void UnwrappedLineParser::parseStructuralElement() { ...@@ -868,6 +868,9 @@ void UnwrappedLineParser::parseStructuralElement() {
case tok::l_square: case tok::l_square:
parseSquare(); parseSquare();
break; break;
case tok::kw_new:
parseNew();
break;
default: default:
nextToken(); nextToken();
break; break;
...@@ -1274,6 +1277,31 @@ void UnwrappedLineParser::parseNamespace() { ...@@ -1274,6 +1277,31 @@ void UnwrappedLineParser::parseNamespace() {
// FIXME: Add error handling. // 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() { void UnwrappedLineParser::parseForOrWhileLoop() {
assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) || assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
FormatTok->IsForEachMacro) && FormatTok->IsForEachMacro) &&
......
...@@ -95,6 +95,7 @@ private: ...@@ -95,6 +95,7 @@ private:
void parseCaseLabel(); void parseCaseLabel();
void parseSwitch(); void parseSwitch();
void parseNamespace(); void parseNamespace();
void parseNew();
void parseAccessSpecifier(); void parseAccessSpecifier();
void parseEnum(); void parseEnum();
void parseJavaEnumBody(); void parseJavaEnumBody();
......
...@@ -153,6 +153,19 @@ TEST_F(FormatTestJava, ClassDeclarations) { ...@@ -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) { TEST_F(FormatTestJava, EnumDeclarations) {
verifyFormat("enum SomeThing { ABC, CDE }"); verifyFormat("enum SomeThing { ABC, CDE }");
verifyFormat("enum SomeThing {\n" verifyFormat("enum SomeThing {\n"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment