Errors in local system headers when using clang libTooling tool

Hello cfe-dev,

I’m using clang libTooling for creating a GUI developer tool, currently with clang 6.0. The tool makes use of the local system headers when analyzing source code. From my local compiler I get these system header paths:

$ clang++ -x c++ -v -E /dev/null

#include <…> search starts here:
/Applications/ (framework directory)

My local toolchain is based on Xcode:

$ clang++ -v
clang version 5.0.1 ( 232230afd349ceeb784720d2266e2288523d871f) ( cbc2c76b286c38a1fd006543b6b224c06cd96df1)
Target: x86_64-apple-darwin17.5.0
Thread model: posix
InstalledDir: /usr/local/bin

My tool usually works fine, and most source code can be analysed without problems. But sometimes there are errors like this one:

/Applications/ fatal error: use of undeclared identifier ‘__builtin_ia32_pavgb128’
return (__m128i)__builtin_ia32_pavgb128((__v16qi)__a, (__v16qi)__b);
error: use of undeclared identifier ‘__builtin_ia32_pavgb128’

I found out that this problem might be related to the preprocessor definitions of my tool. My tool either uses the wrong ones or misses some. I can get a list from my local compiler and make my tool use them:

$ clang++ -x c++ -E -dM /dev/null
#define _LP64 1
#define APPLE_CC 6000
#define APPLE 1
#define __ATOMIC_ACQUIRE 2
#define __ATOMIC_ACQ_REL 4
#define __ATOMIC_CONSUME 1
#define __ATOMIC_RELAXED 0
#define __ATOMIC_RELEASE 3

But that just switches the problem to some other location:

/Applications/ fatal error: redefinition of parameter ‘restrict’
size_t mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
/Applications/ note: expanded from macro ‘__restrict’
#define __restrict restrict
error: redefinition of parameter ‘restrict’

I couldn’t find a good resource explaining me this situation yet. What am I doing wrong here?
Is this a problem of wrong/missing preprocessor definitions?
Or does my clang 6.0 tool not match with my local clang 5.0.1 toolchain’s system headers?
Do I need to ship certain system headers with my tool?

Thank you,
Eberhard Gräther

Hi Eberhard

The compiler headers (the ones in */usr/lib/clang/*/include) are rev-locked with the compiler. You cannot mismatch between different versions.

On the other hand, as long as you are not using compiler builtin functions, you can switch to use the compiler headers from clang 6.0 instead of the ones come with your local toolchain. That should solve your problem.


Hello Steven,

I was a little confused by your response at first, but I think I figured it out.

In my llvm build directory I found the compiler headers matching the clang version of my tool:


If I use the path to those in the system header include paths of my tool then the errors are gone.
I guess that means I need to ship those headers with my tool and make sure they are used instead of the local ones?

Thank you,

Yes, that is correct. See also:

Hope that helps!