67

The question is quite clear I think. I'm trying to write a compiler detection header to be able to include in the application information on which compiler was used and which version.

This is part of the code I'm using:

/* GNU C Compiler Detection */
#elif defined __GNUC__
    #ifdef __MINGW32__
        #define COMPILER "MinGW GCC %d.%d.%d"
    #else
        #define COMPILER "GCC %d.%d.%d"
    #endif
    #define COMP_VERSION __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
#endif

Which could be used like this:

printf("  Compiled using " COMPILER "\n", COMP_VERSION);

Is there any way to detect LLVM and its version? And CLANG?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Carla Álvarez
  • 1,077
  • 3
  • 10
  • 9
  • great question, i can't find any doco on it at all – Matt Joiner Oct 24 '09 at 13:43
  • You sometimes need to know if Clang's Integrated Assembler is being used, too. The use case is modern GCC, and the compiler uses Clang as the assembler rather than an old GAS to assemble AESNI, AVX, BMI, etc. You use the Integrated Assembler because Apple's AS and LD are too old to consume assembly produced by the front-ends. – jww Oct 03 '16 at 16:59

8 Answers8

80

The __llvm__ and __clang__ macros are the official way to check for an LLVM compiler (llvm-gcc or clang) or clang, respectively.

__has_feature and __has_builtin are the recommended way of checking for optional compiler features when using clang, they are documented here.

Note that you can find a list of the builtin compiler macros for gcc, llvm-gcc, and clang using:

echo | clang -dM -E -

This preprocesses an empty string and spits out all macros defined by the compiler.

Jens
  • 69,818
  • 15
  • 125
  • 179
Daniel Dunbar
  • 3,465
  • 1
  • 21
  • 13
  • 28
    Note that `__GNUC__` is defined even for clang and llvm-gcc. – pqnet Jul 08 '11 at 10:08
  • My specific answer is `#if __has_builtin(__builtin_available)` https://github.com/firebase/firebase-ios-sdk/commit/927a2cd567bde4d9cbbdd7772f151034b0552a82#diff-22371dc80da60cd2995aa2215306f5ddR191 – Paul Beusterien Sep 18 '18 at 00:45
53

I cannot find an answer here, only links to answers, so for completeness, here is the answer:

__clang__             // set to 1 if compiler is clang
__clang_major__       // integer: major marketing version number of clang
__clang_minor__       // integer: minor marketing version number of clang
__clang_patchlevel__  // integer: marketing patch level of clang
__clang_version__     // string: full version number

I get currently:

__clang__=1
__clang_major__=3
__clang_minor__=2
__clang_patchlevel__=0
__clang_version__="3.2 (tags/RELEASE_32/final)"
Walter
  • 44,150
  • 20
  • 113
  • 196
  • 3
    It's worth mentioning that this is unreliable. Vendors (like Apple) release their own versions of clang with these numbers changed to reflect their own version numbers, which have no relationship to the clang version their compiler is based on. If you're using this to try to determine if something is supported, you may end up with false positives or false negatives (depending on whether the vendor's version numbers are greater than or less than clang's). – nemequ Jun 09 '20 at 01:40
23

For clang, you shouldn't test its version number, you should check for features you want with feature checking macros.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
Chris Lattner
  • 829
  • 4
  • 3
  • 1
    hm, this is a good point. can you provide a link to some official material regarding this? – Matt Joiner Nov 07 '09 at 05:36
  • 1
    @Matt Joiner, I think, Chris himself is some official. Cited from his homepage http://nondot.org/sabre/: "I'm the primary author of the LLVM Compiler Infrastructure". – osgx Feb 23 '11 at 09:57
  • 2
    @osgx: Nevertheless he could provide links and add documentation to increase the usability of his project. – Matt Joiner Feb 24 '11 at 01:41
  • 5
    This doesn't help when working around LLVM bugs. Such as the bug in fastcall support, which was broken circa build 2335 and fixed in build 2336. – pnkfelix Jun 19 '12 at 17:06
  • You'd still need `__clang__` to know the compiler was actually Clang. – rubenvb Mar 22 '13 at 08:12
  • Actually, for basic feature checking you don't need ```__clang__```; if the ```__has_feature``` etc. macros are themselves not defined, then the features are not supported. But for the OP's purpose you would, since that is trying to print out the actual compiler version, and is not testing for features. – Carl Lindberg May 16 '14 at 17:23
10

Snippet from InitPreprocessor.cpp:

  // Compiler version introspection macros.
  DefineBuiltinMacro(Buf, "__llvm__=1");   // LLVM Backend
  DefineBuiltinMacro(Buf, "__clang__=1");  // Clang Frontend

  // Currently claim to be compatible with GCC 4.2.1-5621.
  DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
  DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
  DefineBuiltinMacro(Buf, "__GNUC__=4");
  DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
  DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\"");

I didn't find any way to get the version of llvm and clang itself, though..

Christoph
  • 164,997
  • 36
  • 182
  • 240
  • i guess one could for now rely on the claimed GCC versioned supported for the features, and clang/llvm for extensions – Matt Joiner Oct 25 '09 at 00:37
7

Take a look at the Pre-defined Compiler Macros page, select Compilers->Clang. There is information on many other macros for standards, compilers, libraries, OS, architectures and more.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
3

Note that if you're using llvm to hack on bytecode, and thus #includeing llvm include files, you can check the macros in llvm/Config/llvm-config.h. And concretely:

/* Major version of the LLVM API */
#define LLVM_VERSION_MAJOR 3

/* Minor version of the LLVM API */
#define LLVM_VERSION_MINOR 8

/* Patch version of the LLVM API */
#define LLVM_VERSION_PATCH 0

/* LLVM version string */
#define LLVM_VERSION_STRING "3.8.0"
Hugh Perkins
  • 7,975
  • 7
  • 63
  • 71
2

I agree that the best choice is to use has feature macroses, not version macroses. Example with boost:

#include <boost/config.hpp>

#if defined(BOOST_NO_CXX11_NOEXCEPT)
 #if defined(BOOST_MSVC)
  #define MY_NOEXCEPT throw()
 #else
  #define MY_NOEXCEPT
 #endif
#else
 #define MY_NOEXCEPT noexcept
#endif

void my_noexcept_function() MY_NOEXCEPT; // it's example, use BOOST_NOEXCEPT (:

But anyway, if you need compiler version, you can use boost.predef:

#include <iostream>
#include <boost/predef.h>

int main() {
#if (BOOST_COMP_CLANG)
  std::cout << BOOST_COMP_CLANG_NAME << "-" << BOOST_COMP_CLANG << std::endl;
#else
  std::cout << "Unknown compiler" << std::endl;
#endif
  return 0;
}

Output examples:

Clang-30400000
Clang-50000000
1

Everyone who has answered that the right thing do it is to use feature detection macros like __has_feature, __has_builtin, etc., is right. If that's possible for your use case, that's what you should do.

That said, there are times when clang doesn't expose anything specific to what you're trying to check. For example, there is no way to tell if a particular SSE/AVX or NEON function is available (and yes, new ones are added from time to time; the instructions the CPU supports are fixed, but sometimes new functions using existing instructions are added to plug a hole in the API). Or maybe there was a bug and clang was generating incorrect machine code. We actually run into these types of issues a fairly often in the SIMDe project.

Unfortunately you can't rely on __clang_major__/__clang_minor__/__clang_patchlevel__. Vendors like Apple take clang and repackage it as their own compiler with their own version numbers, and when they do they generally also change the __clang_*__ versions to match their compiler version, not upstream clang's. For example, Apple clang 4.0 is really a repackaged clang 3.1, but they set __clang_major__ to 4 and __clang_minor__ to 0.

The best solution I've found is to use the feature detection code to detect completely unrelated features which just so happen to be added in the same version as what you really want to detect. I've been doing this for a while, but earlier today I finally put together a header to keep all that logic in one place. It's part of SIMDe, but has no dependencies whatsoever and is public domain (CC0). I'll probably forget to update this answer with any improvements in the future, so please check the repo for the current version, but here is what it looks like right now:

#if !defined(SIMDE_DETECT_CLANG_H)
#define SIMDE_DETECT_CLANG_H 1

#if defined(__clang__) && !defined(SIMDE_DETECT_CLANG_VERSION)
#  if __has_warning("-Wimplicit-const-int-float-conversion")
#    define SIMDE_DETECT_CLANG_VERSION 110000
#  elif __has_warning("-Wmisleading-indentation")
#    define SIMDE_DETECT_CLANG_VERSION 100000
#  elif defined(__FILE_NAME__)
#    define SIMDE_DETECT_CLANG_VERSION 90000
#  elif __has_warning("-Wextra-semi-stmt") || __has_builtin(__builtin_rotateleft32)
#    define SIMDE_DETECT_CLANG_VERSION 80000
#  elif __has_warning("-Wc++98-compat-extra-semi")
#    define SIMDE_DETECT_CLANG_VERSION 70000
#  elif __has_warning("-Wpragma-pack")
#    define SIMDE_DETECT_CLANG_VERSION 60000
#  elif __has_warning("-Wasm-ignored-qualifier")
#    define SIMDE_DETECT_CLANG_VERSION 50000
#  elif __has_attribute(diagnose_if)
#    define SIMDE_DETECT_CLANG_VERSION 40000
#  elif __has_warning("-Wcomma")
#    define SIMDE_DETECT_CLANG_VERSION 30900
#  elif __has_warning("-Wmicrosoft")
#    define SIMDE_DETECT_CLANG_VERSION 30800
#  else
#    define SIMDE_DETECT_CLANG_VERSION 1
#  endif
#endif /* defined(__clang__) && !defined(SIMDE_DETECT_CLANG_VERSION) */

#if defined(SIMDE_DETECT_CLANG_VERSION)
#  define SIMDE_DETECT_CLANG_VERSION_CHECK(major, minor, revision) (SIMDE_DETECT_CLANG_VERSION >= ((major * 10000) + (minor * 1000) + (revision)))
#  define SIMDE_DETECT_CLANG_VERSION_NOT(major, minor, revision) SIMDE_DETECT_CLANG_VERSION_CHECK(major, minor, revision)
#else
#  define SIMDE_DETECT_CLANG_VERSION_CHECK(major, minor, revision) (0)
#  define SIMDE_DETECT_CLANG_VERSION_NOT(major, minor, revision) (1)
#endif

#endif /* !defined(SIMDE_DETECT_CLANG_H) */
nemequ
  • 16,623
  • 1
  • 43
  • 62