[cfe-dev] Dealing with clang API changes in a plugin

David Chisnall David.Chisnall at cl.cam.ac.uk
Mon Dec 8 09:24:13 PST 2014


On 8 Dec 2014, at 16:58, Ehsan Akhgari <ehsan.akhgari at gmail.com> wrote:

> 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<ASTConsumer>.  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")
if (${LLVM_CONFIG} STREQUAL "LLVM_CONFIG-NOTFOUND")
    message(SEND_ERROR "llvm-config not found, please manually set path with -  DLLVM_CONFIG")
endif()
message(STATUS "Using llvm-config: ${LLVM_CONFIG}")

# Define LLVM version macros so that we can support multiple versions in the    source.
exec_program(${LLVM_CONFIG}
    ARGS --version
    OUTPUT_VARIABLE LLVM_VER)

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)
#else
  // Code that works with 3.5
#endif

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).

David





More information about the cfe-dev mailing list