clang-format: [Java] Support try/catch/finally blocks.
authorDaniel Jasper <djasper@google.com>
Sun, 2 Nov 2014 19:21:48 +0000 (19:21 +0000)
committerDaniel Jasper <djasper@google.com>
Sun, 2 Nov 2014 19:21:48 +0000 (19:21 +0000)
llvm-svn: 221104

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

index 256fc28..aa1bfdc 100644 (file)
@@ -1174,7 +1174,8 @@ void UnwrappedLineParser::parseTryCatch() {
     --Line->Level;
   }
   while (FormatTok->is(tok::kw_catch) ||
-         (Style.Language == FormatStyle::LK_JavaScript &&
+         ((Style.Language == FormatStyle::LK_Java ||
+           Style.Language == FormatStyle::LK_JavaScript) &&
           FormatTok->TokenText == "finally")) {
     nextToken();
     while (FormatTok->isNot(tok::l_brace)) {
index 73a1848..62b3e92 100644 (file)
@@ -127,5 +127,31 @@ TEST_F(FormatTestJava, StringConcatenation) {
                "                    + \"cde\";");
 }
 
+TEST_F(FormatTestJava, TryCatchFinally) {
+  verifyFormat("try {\n"
+               "  Something();\n"
+               "} catch (SomeException e) {\n"
+               "  HandleException(e);\n"
+               "}");
+  verifyFormat("try {\n"
+               "  Something();\n"
+               "} finally {\n"
+               "  AlwaysDoThis();\n"
+               "}");
+  verifyFormat("try {\n"
+               "  Something();\n"
+               "} catch (SomeException e) {\n"
+               "  HandleException(e);\n"
+               "} finally {\n"
+               "  AlwaysDoThis();\n"
+               "}");
+
+  verifyFormat("try {\n"
+               "  Something();\n"
+               "} catch (SomeException | OtherException e) {\n"
+               "  HandleException(e);\n"
+               "}");
+}
+
 } // end namespace tooling
 } // end namespace clang