[cfe-dev] Objective-C++11: concerns about adding static_assert support to @interface / @implementation?

Nico Weber via cfe-dev cfe-dev at lists.llvm.org
Fri Mar 8 09:30:44 PST 2019


On Fri, Mar 8, 2019 at 12:08 PM Arthur O'Dwyer <arthur.j.odwyer at gmail.com>
wrote:

> On Fri, Mar 8, 2019 at 11:11 AM Nico Weber via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
>
>> Hi,
>>
>> I'm trying to enable -Wextra-semi in Chromium. I'm almost there; the last
>> remaining issue is that we have a macro THREAD_CHECKER(name) that expands
>> to nothing in release builds but to `ThreadChecker name` in debug builds.
>> It's supposed to be used like
>>
>> class C {
>>   THREAD_CHECKER(checker_);
>> };
>>
>> [...] Since this is a declaration, this triggers -Wextra-semi. I figured
>> the way to spell the usual `do {} while (0)` you tag on the end of a
>> statement macro to silence this warning could be `static_assert(1, "")`, so
>> I changed THREAD_CHECKER() to expand to that in release builds instead of
>> nothing.
>>
>
> For what little it's worth, my opinion is that supporting
> static_assert/_Static_assert in Objective-C++/Objective-C sounds like a
> great idea.
>
> However, if you need something for Chromium that works right now and in
> previous versions of Clang too, have you considered
>

Thanks for the suggestion! I see I didn't explain this very well; for Obj-C
we have an Obj-C class that does

@interface A {
  THREAD_CHECKER(checker_);
}
@end

and static_assert() isn't supported there. (Neither are friend functions
nor static constexpr fields, since Objective-C doesn't have the friend
syntax or static instance variables at class scope).


>
>     #define THREAD_CHECKER(name) friend void __dummy()
>
> or
>
>     #define THREAD_CHECKER(name) static constexpr int __dummy = 0
>
> The latter should work in most places (as long as you only need one per
> scope, or you could do `__COUNTER__` tricks if you need several in the same
> scope). The former should work as long as you only ever need it in class
> bodies.
> (Bikeshedding the appropriate spelling of `__dummy` is left as an exercise
> for Chromium's maintainers.)
>
> –Arthur
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190308/a36b28de/attachment.html>


More information about the cfe-dev mailing list