1414//
1515// ===----------------------------------------------------------------------===//
1616
17+ #include " llvm/Support/CommandLine.h"
1718#include " swift/Basic/Assertions.h"
1819#undef NDEBUG
1920#include < cassert>
2021#include < iostream>
2122
23+ llvm::cl::opt<bool > AssertContinue (
24+ " assert-continue" , llvm::cl::init(false ),
25+ llvm::cl::desc(" Do not stop on an assertion failure" ));
26+
27+ llvm::cl::opt<bool > AssertHelp (
28+ " assert-help" , llvm::cl::init(false ),
29+ llvm::cl::desc(" Print help for managing assertions" ));
30+
2231int CONDITIONAL_ASSERT_Global_enable_flag =
2332#ifdef NDEBUG
2433 0 ; // Default to `off` in release builds
2534#else
2635 0 ; // TODO: Default to `on` in debug builds
2736#endif
2837
29- [[noreturn]] void ASSERT_failure (const char *expr, const char *file, int line, const char *func) {
38+ void ASSERT_failure (const char *expr, const char *file, int line, const char *func) {
3039 // Only print the last component of `file`
3140 const char *f = file;
3241 for (const char *p = file; *p != ' \0 ' ; p++) {
@@ -35,6 +44,14 @@ int CONDITIONAL_ASSERT_Global_enable_flag =
3544 f = p + 1 ;
3645 }
3746 }
47+
48+ if (AssertHelp) {
49+ ASSERT_help ();
50+ } else {
51+ std::cerr << " Assertion help: -Xllvm -assert-help" << std::endl;
52+ }
53+
54+
3855 // Format here matches that used by `assert` on macOS:
3956 std::cerr
4057 << " Assertion failed: "
@@ -43,9 +60,30 @@ int CONDITIONAL_ASSERT_Global_enable_flag =
4360 << " file " << f << " , "
4461 << " line " << line << " ."
4562 << std::endl;
63+
64+ if (AssertContinue) {
65+ std::cerr << " Continuing after failed assertion (-Xllvm -assert-continue)" << std::endl;
66+ return ;
67+ }
68+
4669 abort ();
4770}
4871
72+ void ASSERT_help () {
73+ static int ASSERT_help_shown = 0 ;
74+ if (ASSERT_help_shown) {
75+ return ;
76+ }
77+ ASSERT_help_shown = 1 ;
78+
79+ std::cerr << std::endl;
80+ std::cerr << " Control assertion behavior with one or more of the following options:" << std::endl;
81+ std::cerr << std::endl;
82+ std::cerr << " -Xllvm -assert-continue" << std::endl;
83+ std::cerr << " Continue after any failed assertion" << std::endl;
84+ std::cerr << std::endl;
85+ }
86+
4987// This has to be callable in the same way as the macro version,
5088// so we can't put it inside a namespace.
5189#undef CONDITIONAL_ASSERT_enabled
0 commit comments