Skip to content

Commit 2b8a861

Browse files
committed
chore: tidy up unhandled exceptions
1 parent a55a87c commit 2b8a861

File tree

6 files changed

+52
-66
lines changed

6 files changed

+52
-66
lines changed

include/mrdox/Support/Error.hpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ class [[nodiscard]] MRDOX_DECL
119119
source_location loc =
120120
source_location::current());
121121

122+
/** Constructor.
123+
124+
The constructed object will always
125+
indicate a failure, even if the message
126+
in the exception is empty.
127+
*/
128+
explicit Error(std::exception const& ex);
129+
122130
/** Constructor.
123131
124132
This constructs a new error from a list
@@ -889,16 +897,6 @@ fatal(
889897

890898
} // report
891899

892-
//------------------------------------------------
893-
894-
/** Report an unhandled exception
895-
*/
896-
MRDOX_DECL
897-
[[noreturn]]
898-
void
899-
reportUnhandledException(
900-
std::exception const& ex);
901-
902900
} // mrdox
903901
} // clang
904902

lib/Support/Error.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ Error(
8080
loc_ = loc;
8181
}
8282

83+
Error::
84+
Error(
85+
std::exception const& ex)
86+
{
87+
std::string_view s(ex.what());
88+
if(s.empty())
89+
s = "unknown exception";
90+
reason_ = s;
91+
message_ = s;
92+
}
93+
8394
Error::
8495
Error(
8596
std::vector<Error> const& errors,
@@ -224,20 +235,5 @@ call_impl(
224235

225236
} // report
226237

227-
//------------------------------------------------
228-
229-
void
230-
reportUnhandledException(
231-
std::exception const& ex)
232-
{
233-
namespace sys = llvm::sys;
234-
235-
std::lock_guard<llvm::sys::Mutex> lock(reportMutex_);
236-
llvm::errs() <<
237-
"Unhandled exception: " << ex.what() << '\n';
238-
sys::PrintStackTrace(llvm::errs());
239-
std::exit(EXIT_FAILURE);
240-
}
241-
242238
} // mrdox
243239
} // clang

lib/Support/ExecutorGroup.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,8 @@ run(std::unique_lock<std::mutex> lock)
125125
}
126126
catch(std::exception const& ex)
127127
{
128-
// Any exception which is not
129-
// derived from Exception should
130-
// be reported and terminate
131-
// the process immediately.
132-
reportUnhandledException(ex);
128+
lock.lock();
129+
impl_->errors.emplace(Error(ex));
133130
}
134131
}
135132
}

lib/Support/ThreadPool.cpp

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,14 @@ post(
7575
[sp = std::make_shared<
7676
any_callable<void(void)>>(std::move(f))]
7777
{
78-
try
79-
{
80-
(*sp)();
81-
}
82-
catch(std::exception const& ex)
83-
{
84-
reportUnhandledException(ex);
85-
}
78+
// do NOT catch exceptions here
79+
(*sp)();
8680
});
8781
return;
8882
}
8983

90-
try
91-
{
92-
f();
93-
}
94-
catch(std::exception const& ex)
95-
{
96-
reportUnhandledException(ex);
97-
}
84+
// do NOT catch exceptions here
85+
f();
9886
}
9987

10088
//------------------------------------------------
@@ -180,11 +168,8 @@ post(
180168
}
181169
catch(std::exception const& ex)
182170
{
183-
// Any exception which is not
184-
// derived from Error should
185-
// be reported and terminate
186-
// the process immediately.
187-
reportUnhandledException(ex);
171+
std::lock_guard<std::mutex> lock(impl_->mutex);
172+
impl_->errors.emplace(Error(ex));
188173
}
189174
});
190175
return;
@@ -200,9 +185,8 @@ post(
200185
}
201186
catch(std::exception const& ex)
202187
{
203-
reportUnhandledException(ex);
188+
impl_->errors.emplace(Error(ex));
204189
}
205-
return;
206190
}
207191

208192
} // mrdox

test/TestMain.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ int test_main(int argc, char const* const* argv)
9292
return EXIT_SUCCESS;
9393
}
9494

95+
static void reportUnhandledException(
96+
std::exception const& ex)
97+
{
98+
namespace sys = llvm::sys;
99+
100+
llvm::errs() <<
101+
"Unhandled exception: " << ex.what() << '\n';
102+
sys::PrintStackTrace(llvm::errs());
103+
}
104+
95105
} // mrdox
96106
} // clang
97107

@@ -103,16 +113,12 @@ int main(int argc, char** argv)
103113
}
104114
catch(clang::mrdox::Exception const& ex)
105115
{
106-
// Any exception derived from Exception should
107-
// be caught and handled, and never make it here.
116+
// thrown Exception should never get here.
108117
clang::mrdox::reportUnhandledException(ex);
109-
return EXIT_FAILURE;
110118
}
111119
catch(std::exception const& ex)
112120
{
113-
// Any exception not derived from Exception which
114-
// makes it here must be reported and exit the program.
115121
clang::mrdox::reportUnhandledException(ex);
116-
return EXIT_FAILURE;
117122
}
123+
return EXIT_FAILURE;
118124
}

tool/ToolMain.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ int mrdox_main(int argc, char const** argv)
8282
return EXIT_SUCCESS;
8383
}
8484

85+
static void reportUnhandledException(
86+
std::exception const& ex)
87+
{
88+
namespace sys = llvm::sys;
89+
90+
llvm::errs() <<
91+
"Unhandled exception: " << ex.what() << '\n';
92+
sys::PrintStackTrace(llvm::errs());
93+
}
94+
8595
} // mrdox
8696
} // clang
8797

@@ -93,17 +103,12 @@ int main(int argc, char const** argv)
93103
}
94104
catch(clang::mrdox::Exception const& ex)
95105
{
96-
// Any exception derived from Exception should
97-
// be caught and handled, and never make it here.
106+
// thrown Exception should never get here.
98107
clang::mrdox::reportUnhandledException(ex);
99-
MRDOX_UNREACHABLE();
100108
}
101109
catch(std::exception const& ex)
102110
{
103-
// Any exception not derived from Exception which
104-
// makes it here must be reported and terminate the
105-
// process immediately.
106111
clang::mrdox::reportUnhandledException(ex);
107-
return EXIT_FAILURE;
108112
}
113+
return EXIT_FAILURE;
109114
}

0 commit comments

Comments
 (0)