summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2018-01-23 16:30:56 +0000
committerNico Weber <nicolasweber@gmx.de>2018-01-23 16:30:56 +0000
commitc29f83b79e748e36f9af06adde8953e9c009b35c (patch)
tree88e5b6599504831ef9029b75e5f12db9087e702d
parente76f2171f98a9efd88c36dbd05a86984a17dec1b (diff)
downloadbcm5719-llvm-c29f83b79e748e36f9af06adde8953e9c009b35c.tar.gz
bcm5719-llvm-c29f83b79e748e36f9af06adde8953e9c009b35c.zip
clang-format: Support formatting Java 8 interface default methods.
llvm-svn: 323218
-rw-r--r--clang/lib/Format/UnwrappedLineParser.cpp21
-rw-r--r--clang/unittests/Format/FormatTestJava.cpp1
2 files changed, 19 insertions, 3 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index b8608dcac9c..3fac75dc6fe 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/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/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp
index 2f376f765d6..1d471b73ee6 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/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"
OpenPOWER on IntegriCloud