]> granicus.if.org Git - clang/commitdiff
clang-format: [Java] Support try blocks with resources.
authorDaniel Jasper <djasper@google.com>
Wed, 14 Jan 2015 10:48:41 +0000 (10:48 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 14 Jan 2015 10:48:41 +0000 (10:48 +0000)
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

lib/Format/TokenAnnotator.cpp
lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestJava.cpp

index d0d888fa4d4bdfa88d5a81e9c058d924826da30d..4ba3f91969776b1b74326b830eabd0a5d12cdf79 100644 (file)
@@ -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 &&
index 01c8acf0e662e02182c20b3fd8e7a410ba6ba90f..ec04af5231be785db5bd541d4e46dd2da6fd285b 100644 (file)
@@ -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);
index af4199b46fc1e8de11d5d4b5270e751d5036e9b7..8d6daa62a599fb4fe50a0f929a7899cff955e449 100644 (file)
@@ -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"