[cfe-dev] [LLVMdev] Compile time checks and GCC's '__warning__' attribute
coles.david at gmail.com
coles.david at gmail.com
Wed May 7 08:14:08 PDT 2014
On Wed, May 7, 2014 at 5:15 AM, Richard Smith <richard at metafoo.co.uk> wrote:
> [Moving from llvmdev to cfe-dev]
>
Ah. Apologies for the wrong list.
> __builtin_object_size can be used as a constant expression in some cases
> (essentially, when its operand is a constant expression).
>
I was actually a little surprised that it's not always a constant
expression - I would have assumed that if it wasn't a constant expression,
the worst it would do is turn into a (size_t) -1 for type 0.
I'm guessing the reason is that this can only be determined at a later
stage (after optimization).
> enable_if should do the job here; it's designed for exactly this sort of
> check. Something like:
>
> void *memcpy(void *dest, const void *src, size_t n)
> __attribute__((enable_if(__builtin_object_size(dest, 0) < n, "selected
> if dest is too small"),
> unavailable("dest of memcpy is smaller than size of
> buffer to copy"),
> overloadable));
> void *memcpy(void *dest, const void *src, size_t n)
> __attribute__((enable_if(__builtin_object_size(src, 0) < n, "selected if
> src is too small"),
> unavailable("src of memcpy is smaller than size of buffer
> to copy"),
> overloadable));
> void *memcpy(void *dest, const void *src, size_t n) asm("memcpy")
> __attribute__((overloadable));
>
> ... should work.
>
Ah, awesome. Will certainly try this out.
I was kind of hoping for something that would have been somewhat portable
between GCC and Clang (even if it involved some macro magic).
Hopefully GCC picks up these attributes - feels like a much more explicit
mechanism than "did this function call get optimized out". About the only
downside I see is that "overloadable" probably means name mangling, though
the docs mention that can be avoided by using "static inline".
Cheers!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140508/e3c79e39/attachment.html>
More information about the cfe-dev
mailing list