================
@@ -4895,6 +4895,188 @@ TEST_F(FormatTest, FormatTryCatch) {
verifyIncompleteFormat("try {} catch (");
}
+TEST_F(FormatTest, TryMacros) {
+ FormatStyle Style = getLLVMStyle();
+ Style.TryMacros.push_back("KJ_TRY");
+ Style.TryMacros.push_back("JSG_TRY");
+ Style.CatchMacros.push_back("KJ_CATCH");
+ Style.CatchMacros.push_back("JSG_CATCH");
+
+ // Basic try-catch macro formatting (catch attaches to closing brace).
+ // No space before '(' — macros look like macro invocations.
+ verifyFormat("KJ_TRY {\n"
+ " doStuff();\n"
+ "} KJ_CATCH(e) {\n"
+ " handleError();\n"
+ "}",
+ Style);
+
+ // Try macro with arguments (like JSG_TRY(js)).
+ verifyFormat("JSG_TRY(js) {\n"
+ " doStuff();\n"
+ "} JSG_CATCH(e) {\n"
+ " handleError();\n"
+ "}",
+ Style);
+
+ // Variadic catch macro (like JSG_CATCH(exception, ...)).
+ verifyFormat("JSG_TRY(js) {\n"
+ " doStuff();\n"
+ "} JSG_CATCH(e, js) {\n"
+ " handleError();\n"
+ "}",
+ Style);
+
+ // Catch macro without arguments.
+ verifyFormat("KJ_TRY {\n"
+ " doStuff();\n"
+ "} KJ_CATCH {\n"
+ " handleError();\n"
+ "}",
+ Style);
+
+ // Multiple catch blocks.
+ verifyFormat("KJ_TRY {\n"
+ " doStuff();\n"
+ "} KJ_CATCH(e1) {\n"
+ " handleError1();\n"
+ "} KJ_CATCH(e2) {\n"
+ " handleError2();\n"
+ "}",
+ Style);
+
+ // With BeforeCatch = true, catch goes on its own line.
+ FormatStyle WrappedStyle = Style;
+ WrappedStyle.BraceWrapping.BeforeCatch = true;
+ WrappedStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
+ verifyFormat("KJ_TRY {\n"
+ " doStuff();\n"
+ "}\n"
+ "KJ_CATCH(e) {\n"
+ " handleError();\n"
+ "}",
+ WrappedStyle);
+
+ // Native try/catch is unaffected by TryMacros/CatchMacros.
+ verifyFormat("try {\n"
+ " doStuff();\n"
+ "} catch (const std::exception &e) {\n"
+ " handleError();\n"
+ "}",
+ Style);
+
+ // Try macro with arguments and BeforeCatch = true.
+ verifyFormat("JSG_TRY(js) {\n"
+ " doStuff();\n"
+ "}\n"
+ "JSG_CATCH(e, js) {\n"
+ " handleError();\n"
+ "}",
+ WrappedStyle);
+
+ // Empty try-catch bodies.
+ verifyFormat("KJ_TRY {\n"
+ "} KJ_CATCH(e) {\n"
+ "}",
+ Style);
+
+ // Nested try-catch macros.
+ verifyFormat("KJ_TRY {\n"
+ " KJ_TRY {\n"
+ " doStuff();\n"
+ " } KJ_CATCH(inner) {\n"
+ " handleInner();\n"
+ " }\n"
+ "} KJ_CATCH(outer) {\n"
+ " handleOuter();\n"
+ "}",
+ Style);
+
+ // Comment between try macro and brace.
+ verifyFormat("KJ_TRY /* comment */ {\n"
+ "} KJ_CATCH(e) {\n"
+ "}",
+ Style);
+
+ // Incomplete try-catch macro.
+ verifyIncompleteFormat("KJ_TRY {} KJ_CATCH(", Style);
+
+ // Brace style: Stroustrup (break before catch).
----------------
HazardyKnusperkeks wrote:
There's no real value in testing all brace wrappings here. If the macro is
correctly identified and handled once correctly, then all other styles should
also follow.
https://github.com/llvm/llvm-project/pull/183352
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits