Skip to content

Commit 40e771c

Browse files
[clang-format][regression][PR47461] ifdef causes catch to be seen as a function
https://bugs.llvm.org/show_bug.cgi?id=47461 The following change {D80940} caused a regression in code which ifdef's around the try and catch block cause incorrect brace placement around the catch ``` try { } catch (...) { // This is not a small function bar = 1; } } ``` The brace after the catch will be placed on a newline Reviewed By: curdeius Differential Revision: https://reviews.llvm.org/D87291
1 parent abe0d85 commit 40e771c

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ bool FormatTokenLexer::tryTransformTryUsageForC() {
401401
if (!Try->is(tok::kw_try))
402402
return false;
403403
auto &Next = *(Tokens.end() - 1);
404-
if (Next->isOneOf(tok::l_brace, tok::colon))
404+
if (Next->isOneOf(tok::l_brace, tok::colon, tok::hash, tok::comment))
405405
return false;
406406

407407
if (Tokens.size() > 2) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,43 @@ TEST_F(FormatTest, FormatTryAsAVariable) {
27432743
verifyFormat("int catch, size;");
27442744
verifyFormat("catch = foo();");
27452745
verifyFormat("if (catch < size) {\n return true;\n}");
2746+
2747+
FormatStyle Style = getLLVMStyle();
2748+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2749+
Style.BraceWrapping.AfterFunction = true;
2750+
Style.BraceWrapping.BeforeCatch = true;
2751+
verifyFormat("try {\n"
2752+
" int bar = 1;\n"
2753+
"}\n"
2754+
"catch (...) {\n"
2755+
" int bar = 1;\n"
2756+
"}",
2757+
Style);
2758+
verifyFormat("#if NO_EX\n"
2759+
"try\n"
2760+
"#endif\n"
2761+
"{\n"
2762+
"}\n"
2763+
"#if NO_EX\n"
2764+
"catch (...) {\n"
2765+
"}",
2766+
Style);
2767+
verifyFormat("try /* abc */ {\n"
2768+
" int bar = 1;\n"
2769+
"}\n"
2770+
"catch (...) {\n"
2771+
" int bar = 1;\n"
2772+
"}",
2773+
Style);
2774+
verifyFormat("try\n"
2775+
"// abc\n"
2776+
"{\n"
2777+
" int bar = 1;\n"
2778+
"}\n"
2779+
"catch (...) {\n"
2780+
" int bar = 1;\n"
2781+
"}",
2782+
Style);
27462783
}
27472784

27482785
TEST_F(FormatTest, FormatSEHTryCatch) {

0 commit comments

Comments
 (0)