Homebrew / homebrew-core

🍻 Default formulae for the missing package manager for macOS (or Linux)
https://brew.sh
BSD 2-Clause "Simplified" License
13.79k stars 12.45k forks source link

clang-tidy broken with llvm v19.1.3 #196344

Open lukeshingles opened 3 weeks ago

lukeshingles commented 3 weeks ago

brew gist-logs <formula> link OR brew config AND brew doctor output

https://gist.github.com/lukeshingles/b1477f4b5eaf327af99940ec848213b4

Verification

What were you trying to do (and why)?

clang-tidy is no longer finding system headers since the llvm 19.1.3 update (https://github.com/Homebrew/homebrew-core/pull/196094)

What happened (include all command output)?

$ clang-tidy helloworld.cpp
4 warnings and 1 error generated.
Error while processing /Users/luke/test/helloworld.cpp.
warning: -lunwind: 'linker' input unused [clang-diagnostic-unused-command-line-argument]
warning: argument unused during compilation: '-L/opt/homebrew/opt/llvm/lib' [clang-diagnostic-unused-command-line-argument]
warning: argument unused during compilation: '-L/opt/homebrew/opt/llvm/lib/c++' [clang-diagnostic-unused-command-line-argument]
warning: argument unused during compilation: '-L/opt/homebrew/opt/llvm/lib/unwind' [clang-diagnostic-unused-command-line-argument]
helloworld.cpp:1:10: error: 'iostream' file not found [clang-diagnostic-error]
    1 | #include <iostream>
      |          ^~~~~~~~~~
Found compiler error(s).

What did you expect to happen?

Using llvm 19.1.2 formula, the output is:

$ clang-tidy helloworld.cpp
4 warnings generated.
warning: -lunwind: 'linker' input unused [clang-diagnostic-unused-command-line-argument]
warning: argument unused during compilation: '-L/opt/homebrew/opt/llvm/lib' [clang-diagnostic-unused-command-line-argument]
warning: argument unused during compilation: '-L/opt/homebrew/opt/llvm/lib/c++' [clang-diagnostic-unused-command-line-argument]
warning: argument unused during compilation: '-L/opt/homebrew/opt/llvm/lib/unwind' [clang-diagnostic-unused-command-line-argument]

(Some warnings but no error finding \<iostream>).

Step-by-step reproduction instructions (by running brew commands)

// helloworld.cpp
#include <iostream>

int main() {
  std::cout << "Hello World!";
  return 0;
}
pip install compiledb
compiledb make helloworld
clang-tidy helloworld.cpp
lukeshingles commented 3 weeks ago

I get the same errors when I run clang-tidy from pip (latest version is 19.1.0) when brew llvm 19.1.3 is installed.

carlocab commented 3 weeks ago

Oh, that's annoying. Looks like clang-tidy does not use Clang config files.

As a workaround, try

xcrun clang-tidy helloworld.cpp
Congyuwang commented 2 weeks ago

Similar problem, but with clang. With 19.1.3, clang can't find stuff like #include<string>.

Now I'll have to manually add --sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk for things to work.

I wonder if this is as expected. Or what is the right way to fix this system-header-missing problem?

lukeshingles commented 2 weeks ago

For me, I didn’t have any problem compiling with clang++. Does my simple example compile?

carlocab commented 2 weeks ago

@Congyuwang, I can't reproduce your issue so I'll need to know:

Bo98 commented 2 weeks ago

Looks like clang-tidy does not use Clang config files.

For the clang-tidy issue: looks like it's not resolving the config directory correctly:

$ /opt/homebrew/opt/llvm/bin/clang++ -v test.cpp
Homebrew clang version 19.1.3
Target: arm64-apple-darwin23.6.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/llvm/19.1.3/bin
Configuration file: /opt/homebrew/etc/clang/arm64-apple-darwin23.cfg
System configuration file directory: /opt/homebrew/etc/clang
User configuration file directory: /Users/bo/.config/clang
 "/opt/homebrew/Cellar/llvm/19.1.3/bin/clang-19" -cc1 ...

$ /opt/homebrew/opt/llvm/bin/clang-tidy test.cpp
Homebrew clang version 19.1.3
Target: arm64-apple-darwin23.6.0
Thread model: posix
InstalledDir: 
System configuration file directory: ../../../../etc/clang
User configuration file directory: /Users/bo/.config/clang
ignoring nonexistent directory "../include/c++/v1"
ignoring nonexistent directory "/usr/include/c++/v1"
clang Invocation:
 "c++" "-cc1" ...
carlocab commented 2 weeks ago

For the clang-tidy issue: looks like it's not resolving the config directory correctly:

This seems to be why:

InstalledDir:

It doesn't know where the driver is, so it can't resolve the path to the config file.

xakep8 commented 2 weeks ago

For the clang-tidy issue: looks like it's not resolving the config directory correctly:

This seems to be why:

InstalledDir:

It doesn't know where the driver is, so it can't resolve the path to the config file.

would it work to use a custom prefix flag with path while building to fix issue?

carlocab commented 2 weeks ago

would it work to use a custom prefix flag with path while building to fix issue?

Not quite, since InstalledDir is not populated by the build-time install prefix. For clang, this is populated here (and then printed here). clang-tidy needs to do something similar.

In any case, we also do set CMAKE_INSTALL_PREFIX when building, so not sure there is much more to do in that regard.

Bo98 commented 2 weeks ago

LLVM is designed to be relocatable so it does not bake in install prefixes.

carlocab commented 1 week ago

@Congyuwang, I think your issue should have been fixed by #197410. You'll need to brew update && brew reinstall llvm to pick up the fix. (Or do brew update && brew postinstall llvm if you're only doing native builds.)

Congyuwang commented 1 week ago

@carlocab Not working. Running the following

main.c

int main() {
    return 0;
}

build.sh

eval "$(brew shellenv)"
export LLD=${HOMEBREW_PREFIX}/opt/lld/bin/ld64.lld
export CC=${HOMEBREW_PREFIX}/opt/llvm/bin/clang
${CC} -O3 --target=arm64-apple-macosx10.14 -fuse-ld=${LLD} -o main main.c -v

Run ./build.sh

We get

Homebrew clang version 19.1.3
Target: arm64-apple-macosx10.14
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/llvm/19.1.3/bin
Configuration file: /opt/homebrew/etc/clang/arm64-apple-macosx10.14.cfg
System configuration file directory: /opt/homebrew/etc/clang
User configuration file directory: /Users/congyuwang/.config/clang
 "/opt/homebrew/Cellar/llvm/19.1.3/bin/clang-19" -cc1 -triple arm64-apple-macosx10.14.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -dumpdir main- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -funwind-tables=1 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -fbuiltin-headers-in-system-modules -fdefine-target-os-macros -target-cpu apple-m1 -target-feature +zcm -target-feature +zcz -target-feature +v8.4a -target-feature +aes -target-feature +altnzcv -target-feature +ccdp -target-feature +complxnum -target-feature +crc -target-feature +dotprod -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +fptoint -target-feature +fullfp16 -target-feature +jsconv -target-feature +lse -target-feature +neon -target-feature +pauth -target-feature +perfmon -target-feature +predres -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +sb -target-feature +sha2 -target-feature +sha3 -target-feature +specrestrict -target-feature +ssbs -target-abi darwinpcs -debugger-tuning=lldb -fdebug-compilation-dir=/Users/congyuwang/Projects/CppProjects/brew-clang-debug -target-linker-version 1115.7.3 -v -fcoverage-compilation-dir=/Users/congyuwang/Projects/CppProjects/brew-clang-debug -resource-dir /opt/homebrew/Cellar/llvm/19.1.3/lib/clang/19 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/local/include -internal-isystem /opt/homebrew/Cellar/llvm/19.1.3/lib/clang/19/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include -O3 -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fmax-type-align=16 -fcolor-diagnostics -vectorize-loops -vectorize-slp -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/folders/fc/bckw5h_s4c9fz9dlgltf7b840000gn/T/main-b0ef43.o -x c main.c
clang -cc1 version 19.1.3 based upon LLVM 19.1.3 default target arm64-apple-darwin24.1.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /opt/homebrew/Cellar/llvm/19.1.3/lib/clang/19/include
End of search list.
 "/opt/homebrew/opt/lld/bin/ld64.lld" -demangle -lto_library /opt/homebrew/Cellar/llvm/19.1.3/lib/libLTO.dylib -dynamic -arch arm64 -platform_version macos 11.0.0 11.0.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -mllvm -enable-linkonceodr-outlining -o main /var/folders/fc/bckw5h_s4c9fz9dlgltf7b840000gn/T/main-b0ef43.o -lSystem /opt/homebrew/Cellar/llvm/19.1.3/lib/clang/19/lib/darwin/libclang_rt.osx.a
ld64.lld: error: library not found for -lSystem
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Seems like MacOSX10.14.sdk is not found.

However, specifying --sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk compiles successfully.

Congyuwang commented 1 week ago
❯ ls /Library/Developer/CommandLineTools/SDKs/
MacOSX.sdk     MacOSX12.1.sdk MacOSX12.3.sdk MacOSX14.5.sdk MacOSX14.sdk   MacOSX15.1.sdk MacOSX15.sdk

I only have the above. MacOS 15.1, Xcode 16.1. There is no MacOSX10.14.sdk.

Whereas we have

❯ cat /opt/homebrew/etc/clang/arm64-apple-macosx10.14.cfg
--sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk
carlocab commented 1 week ago

@Congyuwang, don't use --target. Use -mmacos-version-min. See discussion at #197532.

Congyuwang commented 1 week ago

@Congyuwang, don't use --target. Use -mmacos-version-min. See discussion at #197532.

I must say that this maybe the correct way to go. But I didn’t add the target flag myself, but it is added by some (I need further investigation to see) upstream dependencies. So this incorrect use of target for the purpose can be quite common.