Is there a builtin macro defined when optimisation is enabled in clang?

c, clang, macros, optimization

Solution

The `__OPTIMIZE__` macro also exists on clang and seems to work the same way as in gcc (your example code works fine).

I've not yet found specific documentation about this but I suspect the page you linked lists some clang-specific macros not adopted by gcc. Edit: that's not strictly true as `__COUNTER__` also exists in gcc.

I guess this matter falls into clang's "mission" to be as compatible with gcc as possible:

The Clang driver and language features are intentionally designed to be as compatible with the GNU GCC compiler as reasonably possible, easing migration from GCC to Clang. In most cases, code “just works”.

source: http://clang.llvm.org/docs/UsersManual.html#id4

Also, this little command is useful to list macros used by the compiler:

cc -dM -E -x c [options] /dev/null

It works with gcc, clang and maybe some other compilers.

Edit: looks like it's documented after all... in the code :)

__OPTIMIZE__ is a GNU extension that Clang implements but MSVC does not. Is there a good equivalent there?

source: https://github.com/llvm-mirror/clang/blob/master/include/clang/StaticAnalyzer/Core/PathSensitive/ConstraintManager.h#L84

Problem

When compiling with gcc, the `__OPTIMIZE__` macro is defined when optimisations are turned on (see here). This enables runtime warnings like the following: ``` #ifndef __OPTIMIZE__ printf("[WARNING] COMPILED WITHOUT OPTIMISATIONS\n"); #endif ``` Is there a similar macro for clang? I wasn't able to find one in the documentation here. Or, even better, is there a way to do this that will work across all compilers?

Original source