diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -401,7 +401,7 @@ if (!Try->is(tok::kw_try)) return false; auto &Next = *(Tokens.end() - 1); - if (Next->isOneOf(tok::l_brace, tok::colon)) + if (Next->isOneOf(tok::l_brace, tok::colon, tok::hash, tok::comment)) return false; if (Tokens.size() > 2) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2743,6 +2743,43 @@ verifyFormat("int catch, size;"); verifyFormat("catch = foo();"); verifyFormat("if (catch < size) {\n return true;\n}"); + + FormatStyle Style = getLLVMStyle(); + Style.BreakBeforeBraces = FormatStyle::BS_Custom; + Style.BraceWrapping.AfterFunction = true; + Style.BraceWrapping.BeforeCatch = true; + verifyFormat("try {\n" + " int bar = 1;\n" + "}\n" + "catch (...) {\n" + " int bar = 1;\n" + "}", + Style); + verifyFormat("#if NO_EX\n" + "try\n" + "#endif\n" + "{\n" + "}\n" + "#if NO_EX\n" + "catch (...) {\n" + "}", + Style); + verifyFormat("try /* abc */ {\n" + " int bar = 1;\n" + "}\n" + "catch (...) {\n" + " int bar = 1;\n" + "}", + Style); + verifyFormat("try\n" + "// abc\n" + "{\n" + " int bar = 1;\n" + "}\n" + "catch (...) {\n" + " int bar = 1;\n" + "}", + Style); } TEST_F(FormatTest, FormatSEHTryCatch) {