magic __has_feature macro

Hi All,

As Clang "grows up", it will continue to get more features. I'd like to add new "__has_feature" builtin function-like macro that evaluates to 0 or 1 if clang has a specified feature. This would allow us to have something like this:

#if defined(__has_feature) && __has_feature(whatever)
...
#endif

... where "whatever" is an identifier. The list of accepted identifiers would be listed in the extensions manual.

I want to add this, because I really hate the proliferation of random predefines macros that the preprocessor ends up accreting. Does anyone have any objection to this?

-Chris

What does it evaluate to if clang doesn't have the specified feature? :slight_smile:

Sounds like a good idea.

-Eli

Hi All,

As Clang "grows up", it will continue to get more features. I'd like
to add new "__has_feature" builtin function-like macro that evaluates
to 0 or 1 if clang has a specified feature.

What does it evaluate to if clang doesn't have the specified feature? :slight_smile:

haha. :slight_smile:

Sounds like a good idea.

Ok, my proposed use of if defined won't work because of the parsing structure of pp conditionals. However, something like this will work:

#ifndef __has_feature
#define __has_feature(x) 0
#endif

// ...

#if __has_feature(x)
  ... whatever ...
#endif

I also plan to add a __has_builtin(x) as well, so that we can write things like:

#if __has_builtin(__builtin_trap)
   __builtin_trap();
#else
   abort();
#endif

Look, no autoconf, and no fragile compiler version checks! :slight_smile:

I'll start working on this.

-Chris

Great idea, Chris!

Ok, I just committed the last patch, please let me know if you notice any weirdness. Documentation here:
http://clang.llvm.org/docs/LanguageExtensions.html#feature_check

Whenever we add new attributes or other stuff, we should add them to __has_feature. Also, it would probably make sense to add __has_feature support for various C++'0x sub-features that we already support like static_assert. Please make sure that these are documented in the extensions manual when they are added though.

-Chris