diff options
author | Daniel Jasper <djasper@google.com> | 2015-01-14 10:48:41 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2015-01-14 10:48:41 +0000 |
commit | 3e3c5ba7b8be117ee3d12afebbd8d1d28b417dc0 (patch) | |
tree | af27bf0c91a29c205b8ce197d5e093cc96d85a39 | |
parent | 56deac07d749e15ccca466656267f5c4331ceb19 (diff) |
clang-format: [Java] Support try blocks with resources.
Before:
try
(SomeResource rs = someFunction()) {
Something();
}
After:
try (SomeResource rs = someFunction()) {
Something();
}
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@225973 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Format/TokenAnnotator.cpp | 3 | ||||
-rw-r--r-- | lib/Format/UnwrappedLineParser.cpp | 4 | ||||
-rw-r--r-- | unittests/Format/FormatTestJava.cpp | 11 |
3 files changed, 17 insertions, 1 deletions
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index d0d888fa4d..4ba3f91969 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1701,7 +1701,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, (Style.SpaceBeforeParens != FormatStyle::SBPO_Never && (Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch, tok::kw_case) || - (Left.isOneOf(tok::kw_catch, tok::kw_new, tok::kw_delete) && + (Left.isOneOf(tok::kw_try, tok::kw_catch, tok::kw_new, + tok::kw_delete) && (!Left.Previous || Left.Previous->isNot(tok::period))) || Left.IsForEachMacro)) || (Style.SpaceBeforeParens == FormatStyle::SBPO_Always && diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 01c8acf0e6..ec04af5231 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -1164,6 +1164,10 @@ void UnwrappedLineParser::parseTryCatch() { nextToken(); } } + // Parse try with resource. + if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) { + parseParens(); + } if (FormatTok->is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); diff --git a/unittests/Format/FormatTestJava.cpp b/unittests/Format/FormatTestJava.cpp index af4199b46f..8d6daa62a5 100644 --- a/unittests/Format/FormatTestJava.cpp +++ b/unittests/Format/FormatTestJava.cpp @@ -361,6 +361,17 @@ TEST_F(FormatTestJava, TryCatchFinally) { "}"); } +TEST_F(FormatTestJava, TryWithResources) { + verifyFormat("try (SomeResource rs = someFunction()) {\n" + " Something();\n" + "}"); + verifyFormat("try (SomeResource rs = someFunction()) {\n" + " Something();\n" + "} catch (SomeException e) {\n" + " HandleException(e);\n" + "}"); +} + TEST_F(FormatTestJava, SynchronizedKeyword) { verifyFormat("synchronized (mData) {\n" " // ...\n" |