[LLVMdev] Proposal for better assertions in LLVM

Talin viridia at gmail.com
Tue Jul 26 10:41:51 PDT 2011


The assertions in LLVM would be a lot more useful if they could print out
not only the source code of the expression that failed, but also print the
values of the various arguments. To that end, I have an idea for an improved
assert macro which would use raw_ostream. It would look something like this:

   ASSERT_STRM(Ty == STy, "Expected " << Ty << " but got " <<
STy->getElementType(0));

This would transform to:

   if (!(Ty == STy))
     AssertionFailureStream(__FILE__, __LINE__) <<
       "Expected " << Ty << " but got " << STy->getElementType(0);

The AssertionFailureStream is a subtype of raw_ostream whose destructor
calls abort() after printing the contents of the stream to stderr. (I use a
similar technique in my frontend.)

As you can see, this ought to work with any argument type that can work with
raw_ostream. And it shouldn't cost anything if the assert doesn't fail.

The actual definition of the macro would be something like this:

  #define ASSERT_STRM(cond, args) \
    if (!(cond)) AssertionFailureStream(__FILE__, __LINE__) << args

Note that there's no trailing semicolon, as this is supplied at the point
where the macro is invoked.

What do you think?

-- 
-- Talin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110726/55fc6174/attachment.html>


More information about the llvm-dev mailing list