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:
/usr/local/include
/opt/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.1.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks (framework directory)

My local toolchain is based on Xcode:

$ clang++ -v
clang version 5.0.1 (http://llvm.org/git/clang.git 232230afd349ceeb784720d2266e2288523d871f) (http://llvm.org/git/llvm.git 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/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.1.0/include/emmintrin.h:2261:19: 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 OBJC_NEW_PROPERTIES 1
#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/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/stdlib.h:162:54: fatal error: redefinition of parameter ‘restrict’
size_t mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/cdefs.h:209:20: 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.

Steven

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:

…/clang-llvm/build_release/lib/clang/6.0.0/include

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,
Eberhard

Yes, that is correct. See also:

http://lists.llvm.org/pipermail/cfe-dev/2012-December/026621.html
http://lists.llvm.org/pipermail/cfe-dev/2018-April/057683.html

Hope that helps!

Cheers