llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.25k stars 11.66k forks source link

clang-tidy doesn't work correctly when compile_commands.json contains symlink to clang #46804

Open llvmbot opened 4 years ago

llvmbot commented 4 years ago
Bugzilla Link 47460
Version unspecified
OS Linux
Reporter LLVM Bugzilla Contributor
CC @martingalvan

Extended Description

I spent way too much time troubleshooting this, but it was not obvious at first why tidy is complaining.

Let's consider following example: Clang installed with official apt package, created symlink: /usr/bin/clang++ -> ../lib/llvm-10/bin/clang++

Now if you run CMake with clang++ as compiler everything will compile fine. And we have, following entry in compile_commands.json "command": "/usr/bin/clang++ -stdlib=libc++ -o main.o -c main.cpp"

Run clang-tidy -p build main.cpp and it doesn't add /usr/lib/llvm-10/bin/../include/c++/v1 to include dirs... (and fails obviously)

But there is more, if we edit compile_commands.json (or run cmake with fullpath to compiler), so that we have: "command": "/usr/lib/llvm-10/bin/clang++ -stdlib=libc++ -o main.o -c main.cpp" it will work fine...

I can pass full path to cmake, CXX=$(realpath which clang++), and it will work, but this is clunky if you ask me. Especially if one have /usr/bin/c++ symlinked to clang++ and expect things to just work and don't want to manually resolve symlinks.

--

I took quick look and Driver::getInstalledDir() if InstalledDir is empty, returns Dir, which is apparently invocation dir and doesn't resolve symlinks. And later is used and InstalledDir which obviously is wrong in case of symlink.

llvmbot commented 3 years ago

I'm moving this to clang->Tooling as this seems like the issue is in the tooling, all clang-tidy has done is expose this issue.

c52d733c-e84b-463e-ab9a-043eccf24a08 commented 3 years ago

6 months later and this extremely major bug is still there. What's worse, this doesn't seem to happen on clang-tidy 6, but I saw it on 11.

llvmbot commented 4 years ago

This bug probably belongs in clang->Tooling as I suspect other clang-tools will also have this shortfall.

llvmbot commented 4 years ago

Of course CXX=$(realpath which clang++) will not work, because it will resolve to clang instead of clang++, but the idea is to pass full path to CMake with actual installation directory.

CXX=$(realpath which clang++)++ would work :)