Using asserts in LLVM

Hello,

I am testing the new cool LLVM python bindings that Mahadevan wrote over
the past few weeks. I came across a problem in the python binding which
directed me to use of 'cassert'[1] in LLVM itself.

When LLVM users make a mistake in using the library's constructs they
usually hit an assertion. This form of defensive programming is great
for detecting errors as soon as they happen, especially in c++.

However, when I use the python bindings and hit an assertion, I get
kicked out of my shell (which sucks). For example:

Type.function(Type.void(), [Type.void()])

Type.cpp:474: llvm::FunctionType::FunctionType(const llvm::Type*, const
std::vector<const llvm::Type*, std::allocator<const llvm::Type*> >&,
bool): Assertion `(Params[i]->isFirstClassType() ||
isa<OpaqueType>(Params[i])) && "Function arguments must be value
types!"' failed.
Aborted (core dumped)

I would like LLVM to raise an exception which the python wrapper could
catch and pass to my program. I know that the current LLVM code does not
use exceptions. Also, the current stl "cassert.h" implementation is sufficient
for 99% of the developers which write C++ code. One way of solving this is to
rewrite all of the asserts in Python before calling the C++ code. This means
duplication of a ton of code. Another way would be to get around this is to
place a hook over 'abort' in runtime. This is ugly as sin and may not always work.

Would you consider changing the implementation of 'assert' so that it
would throw exceptions rather then abort the program ?

Thank you,
Nadav

1: http://www.ishiboo.com/~nirva/c++/stl-ref/assert.html

This is the right way to go about this.

I would like LLVM to raise an exception which the python wrapper could catch and pass to my program. I know that the current LLVM code does not use exceptions. Also, the current stl "cassert.h" implementation is sufficient for 99% of the developers which write C++ code. [...] Another way would be to get around this is to place a hook over 'abort' in runtime. This is ugly as sin and may not always work.

LLVM does not use exceptions. This is in order to reduce footprint, and since there are no valid failure cases for 95%+ of the compiler anyhow. Given that there are no exception tables, there is no way to safely unwind the stack and recover from a failure; assert has no option but to abort the program.

Would you consider changing the implementation of 'assert' so that it would throw exceptions rather then abort the program ?

No, sorry.

One way of solving this is to rewrite all of the asserts in Python before calling the C++ code. This means duplication of a ton of code.

Aside from writing correct programs :), this is the best option.

— Gordon