Dealing with clang API changes in a plugin

Hi all,

I have a clang-plugin that overrides the PluginASTAction::CreateASTConsumer function. The return type of this function was changed in trunk (r215323) from ASTConsumer* to std::unique_ptr. I’d like to keep the plugin working for both clang 3.5 and older, and trunk and newer, which means that I somehow need to detect the version of clang from the headers using a preprocessor macro.

Is there a macro supported for this purpose that I can use?


I normally stick this in my CMakeLists.txt for projects that need to work with multiple LLVM versions:

find_program(LLVM_CONFIG NAMES llvm-config DOC "Path to llvm-config utility")
    message(SEND_ERROR "llvm-config not found, please manually set path with - DLLVM_CONFIG")
message(STATUS "Using llvm-config: ${LLVM_CONFIG}")

# Define LLVM version macros so that we can support multiple versions in the source.
    ARGS --version

string(REGEX REPLACE "([0-9]*).([0-9]*).*" "-DLLVM_MAJOR=\\1 -DLLVM_MINOR=\\2" LLVM_VERSION "${LLVM_VER}")

And then add ${LLVM_VERSION} to my C[XX]FLAGS.

I then also use these macros:

#define LLVM_BEFORE(major,minor)\
    ((LLVM_MAJOR < major) || ((LLVM_MAJOR == major) && (LLVM_MINOR < minor)))
#define LLVM_AFTER(major,minor)\
    ((LLVM_MAJOR > major) || ((LLVM_MAJOR == major) && (LLVM_MINOR > minor)))

This lets me do things like:

#if LLVM_BEFORE(3,3)
  // Code with APIs that worked with LLVM 3.2 and earlier
#else if LLVM_AFTER(3.4)
  // Code that works with LLVM 3.5 (and, hopefully, later)
  // Code that works with 3.5

I generally only support releases and ToT tough - this isn't fine grained enough to work with specific svn revisions (and I don't want to clutter my code that much).


If you include clang/Basic/Version.h, you can use CLANG_VERSION_MAJOR and CLANG_VERSION_MINOR. However, I do believe that Apple's clang uses different version numbers from everybody else.