From: Nico Weber Date: Tue, 23 Jan 2018 16:30:56 +0000 (+0000) Subject: clang-format: Support formatting Java 8 interface default methods. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4eae234b3069c843eb8c3a0701cc93c9a4be395a;p=clang clang-format: Support formatting Java 8 interface default methods. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@323218 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index b8608dcac9..3fac75dc6f 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -333,7 +333,18 @@ void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { nextToken(); addUnwrappedLine(); break; - case tok::kw_default: + case tok::kw_default: { + unsigned StoredPosition = Tokens->getPosition(); + FormatToken *Next = Tokens->getNextToken(); + FormatTok = Tokens->setPosition(StoredPosition); + if (Next && Next->isNot(tok::colon)) { + // default not followed by ':' is not a case label; treat it like + // an identifier. + parseStructuralElement(); + break; + } + // Else, if it is 'default:', fall through to the case handling. + } case tok::kw_case: if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) { @@ -1032,8 +1043,12 @@ void UnwrappedLineParser::parseStructuralElement() { // 'default: string' field declaration. break; nextToken(); - parseLabel(); - return; + if (FormatTok->is(tok::colon)) { + parseLabel(); + return; + } + // e.g. "default void f() {}" in a Java interface. + break; case tok::kw_case: if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) // 'case: string' field declaration. diff --git a/unittests/Format/FormatTestJava.cpp b/unittests/Format/FormatTestJava.cpp index 2f376f765d..1d471b73ee 100644 --- a/unittests/Format/FormatTestJava.cpp +++ b/unittests/Format/FormatTestJava.cpp @@ -144,6 +144,7 @@ TEST_F(FormatTestJava, ClassDeclarations) { verifyFormat("public interface SomeInterface {\n" " void doStuff(int theStuff);\n" " void doMoreStuff(int moreStuff);\n" + " default void doStuffWithDefault() {}\n" "}"); verifyFormat("@interface SomeInterface {\n" " void doStuff(int theStuff);\n"