Search code examples
c++clang++libtooling

Error when parsing system headers with RecursiveASTVisitor


I am building a standalone tool with libtooling. I have a basic boilerplate code for a FrontendAction, Consumer and a Visitor. The visitor only visits ClassTemplateSpecializationDecl where I just save some information. Everything works fine, but if I #include <string> in the file I am parsing I got an error: 'stddef.h' file not found.

I thought the compiler couldnt find some system headers but the input file I am parsing compiles without any errors with the clang++ command.


Solution

  • It's a very typical error for libTooling-based tools: https://clang.llvm.org/docs/FAQ.html#id3

    Some header files (stddef.h, stdarg.h, and others) are shipped with Clang — these are called builtin includes. Clang searches for them in a directory relative to the location of the clang binary. If you moved the clang binary, you need to move the builtin headers, too.

    If you use CMake you can add the following code to install Clang's headers:

    set(CLANG_BUILTIN_HEADERS_DIR "${LLVM_LIBRARY_DIR}/clang")
    
    install(
      DIRECTORY ${CLANG_BUILTIN_HEADERS_DIR}
      DESTINATION lib
      FILES_MATCHING PATTERN "*.h"
      )