Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-tidy can't locate stdlib headers

Today I've built clang-tidy from sources, I've built it using clang++. After it has been built I've created a symlink to the executable like this:

ln -s /path/to/build/bin/clang-tidy /usr/local/bin/clang-tidy

Then I've tried to use clang-tidy with cmake on simple project (single .cpp file containing printing helloworld code). This is how my cmake file looks like:

project(Test)
cmake_minimum_required(VERSION 3.12.0 FATAL_ERROR)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
set(CMAKE_CXX_STANDARD 17)
set(CXX_STANDARD_REQUIRED)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_CXX_CLANG_TIDY
  clang-tidy;
  -checks=*;)

add_executable(Test
    helloworld.cpp)

I read somewhere that clang-tidy with cmake only works with Unix Makefiles & Ninja generators (or possibly some else). I usually use xcode generator, but I'm little familiar with those 2 so i didn't really care about others. I've tried to generate and build project with both Unix Makefiles and Ninja, but with both I get this error:

/Users/xxxxxxx/Dev/VSCodePlayground/helloworld.cpp:2:10: error: 'string' file not found [clang-diagnostic-error]

I found some info, that this is probably caused because clang can't find libc++/stdlib headers. So I've tried suggested compilation with -v argument (which succeeded without errors) and got this output on include dirs:

#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)

If I understand the issue is that clang-tidy doesn't know the location of headers of libc++ while clang does, am I right? But how should I resolve this and what caused this problem?

like image 495
mezo Avatar asked Oct 28 '22 07:10

mezo


1 Answers

If I understand the issue is that clang-tidy doesn't know the location of headers of libc++ while clang does, am I right?

I happen to stumble at the very same issue. And it is really not obvious at first why compilation invoked by clang-tidy doesn't detect installed libc++.

It turns out if compile_commands.json contains symlink to clang, it will fail to find libc++. It searches relative to "installation dir", which is clang binary location, but doesn't follow symlinks. And it can't find it.

Reported here: https://bugs.llvm.org/show_bug.cgi?id=47460

As a workaround, you can pass full path to compiler to CMake so generated compile_commands.json will be "compatible" with clang-tidy. The path need to be from actual install directory, note that clang++ is still symlink to clang++ and it is important, but make sure that directory is correct.

At least this was the issue in my case on Ubuntu box. Yours looks very similar, so it is likely the same issue. Although I have no idea how XCode manages toolchains, so it might be something else entirely :)

like image 132
Kacper Michajłow Avatar answered Nov 18 '22 11:11

Kacper Michajłow