Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang-tidy broken with llvm v19.1.3 #196344

Open
4 tasks done
lukeshingles opened this issue Nov 1, 2024 · 19 comments
Open
4 tasks done

clang-tidy broken with llvm v19.1.3 #196344

lukeshingles opened this issue Nov 1, 2024 · 19 comments
Labels
bug Reproducible Homebrew/homebrew-core bug help wanted Task(s) needing PRs from the community or maintainers

Comments

@lukeshingles
Copy link
Contributor

lukeshingles commented Nov 1, 2024

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

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

Verification

  • My brew doctor output says Your system is ready to brew. and am still able to reproduce my issue.
  • I ran brew update and am still able to reproduce my issue.
  • I have resolved all warnings from brew doctor and that did not fix my problem.
  • I searched for recent similar issues at https://github.com/Homebrew/homebrew-core/issues?q=is%3Aissue and found no duplicates.

What were you trying to do (and why)?

clang-tidy is no longer finding system headers since the llvm 19.1.3 update (#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 lukeshingles added the bug Reproducible Homebrew/homebrew-core bug label Nov 1, 2024
@lukeshingles lukeshingles changed the title clang-tidy v19.1.3 broken clang-tidy broken with llvm v19.1.3 Nov 1, 2024
@lukeshingles
Copy link
Contributor Author

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
Copy link
Member

carlocab commented Nov 1, 2024

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

As a workaround, try

xcrun clang-tidy helloworld.cpp

@Congyuwang

This comment was marked as off-topic.

@lukeshingles

This comment was marked as off-topic.

@carlocab

This comment was marked as off-topic.

@Bo98
Copy link
Member

Bo98 commented Nov 3, 2024

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
Copy link
Member

carlocab commented Nov 3, 2024

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

This comment was marked as resolved.

@carlocab
Copy link
Member

carlocab commented Nov 6, 2024

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
Copy link
Member

Bo98 commented Nov 6, 2024

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

@carlocab
Copy link
Member

@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

This comment was marked as off-topic.

@Congyuwang

This comment was marked as off-topic.

@carlocab

This comment was marked as off-topic.

@Congyuwang

This comment was marked as off-topic.

@carlocab
Copy link
Member

The missing InstalledDir might be a red herring. That can be fixed with:

Patch
diff --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp
index af18194ae0fe..493d9657f988 100644
--- a/clang/lib/Tooling/CompilationDatabase.cpp
+++ b/clang/lib/Tooling/CompilationDatabase.cpp
@@ -251,18 +251,16 @@ static bool stripPositionalArgs(std::vector<const char *> Args,
       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
       &*DiagOpts, &DiagClient, false);
 
-  // The clang executable path isn't required since the jobs the driver builds
-  // will not be executed.
-  std::unique_ptr<driver::Driver> NewDriver(new driver::Driver(
-      /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(),
-      Diagnostics));
-  NewDriver->setCheckInputsExist(false);
-
   // This becomes the new argv[0]. The value is used to detect libc++ include
   // dirs on Mac, it isn't used for other platforms.
   std::string Argv0 = GetClangToolCommand();
   Args.insert(Args.begin(), Argv0.c_str());
 
+  std::unique_ptr<driver::Driver> NewDriver(new driver::Driver(
+      Argv0, llvm::sys::getDefaultTargetTriple(),
+      Diagnostics));
+  NewDriver->setCheckInputsExist(false);
+
   // By adding -c, we force the driver to treat compilation as the last phase.
   // It will then issue warnings via Diagnostics about un-used options that
   // would have been used for linking. If the user provided a compiler name as

Unfortunately, this produces different errors, and doesn't seem to change the include path:

clang-tidy helloworld.cpp -- -v
❯ ~LLVM/build/bin/clang-tidy helloworld.cpp -- -v
clang version 20.0.0git (https://github.com/llvm/llvm-project.git c4d656a4e992648f3490536336c230041c74dc38)
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /Users/carlocab/github/llvm-project/build/bin
Build config: +assertions
Configuration file: /Users/carlocab/github/llvm-project/build/etc/clang/arm64-apple-darwin24.1.0.cfg
System configuration file directory: /Users/carlocab/github/llvm-project/build/etc/clang
User configuration file directory: /Users/carlocab/.config/clang
clang version 20.0.0git (https://github.com/llvm/llvm-project.git c4d656a4e992648f3490536336c230041c74dc38)
Target: arm64-apple-darwin24.1.0
Thread model: posix
InstalledDir: /Users/carlocab/github/llvm-project/build/bin
Build config: +assertions
Configuration file: /Users/carlocab/github/llvm-project/build/etc/clang/arm64-apple-darwin24.1.0.cfg
System configuration file directory: /Users/carlocab/github/llvm-project/build/etc/clang
User configuration file directory: /Users/carlocab/.config/clang
clang Invocation:
 "/Users/carlocab/github/llvm-project/build/bin/clang-tool" "-cc1" "-triple" "arm64-apple-macosx15.0.0" "-Wundef-prefix=TARGET_OS_" "-Werror=undef-prefix" "-Wdeprecated-objc-isa-usage" "-Werror=deprecated-objc-isa-usage" "-fsyntax-only" "-disable-free" "-clear-ast-before-backend" "-main-file-name" "helloworld.cpp" "-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" "+ccpp" "-target-feature" "+complxnum" "-target-feature" "+crc" "-target-feature" "+dotprod" "-target-feature" "+flagm" "-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=/tmp/clang-tidy-cpp" "-target-linker-version" "1115.7.3" "-v" "-fcoverage-compilation-dir=/tmp/clang-tidy-cpp" "-resource-dir" "/Users/carlocab/github/llvm-project/build/lib/clang/20" "-internal-isystem" "/Users/carlocab/github/llvm-project/build/bin/../include/c++/v1" "-internal-isystem" "/usr/local/include" "-internal-isystem" "/Users/carlocab/github/llvm-project/build/lib/clang/20/include" "-internal-externc-isystem" "/usr/include" "-fdeprecated-macro" "-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" "-fcxx-exceptions" "-fexceptions" "-fmax-type-align=16" "-fcolor-diagnostics" "-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-x" "c++" "/tmp/clang-tidy-cpp/helloworld.cpp"

clang -cc1 version 20.0.0git based upon LLVM 20.0.0git default target arm64-apple-darwin24.1.0
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/include"
ignoring nonexistent directory "/System/Library/SubFrameworks"
#include "..." search starts here:
#include <...> search starts here:
 /Users/carlocab/github/llvm-project/build/bin/../include/c++/v1
 /Users/carlocab/github/llvm-project/build/lib/clang/20/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
8 errors generated.
Error while processing /tmp/clang-tidy-cpp/helloworld.cpp.
/Users/carlocab/github/llvm-project/build/bin/../include/c++/v1/__locale_dir/support/bsd_like.h:21:10: error: 'time.h' file not found [clang-diagnostic-error]
   21 | #include <time.h>
      |          ^~~~~~~~
/Users/carlocab/github/llvm-project/build/bin/../include/c++/v1/__mbstate_t.h:51:4: error: "We don't know how to get the definition of mbstate_t without <wchar.h> on your platform." [clang-diagnostic-error]
   51 | #  error "We don't know how to get the definition of mbstate_t without <wchar.h> on your platform."
      |    ^
/Users/carlocab/github/llvm-project/build/bin/../include/c++/v1/stdlib.h:143:30: error: unknown type name 'ldiv_t' [clang-diagnostic-error]
  143 | inline _LIBCPP_HIDE_FROM_ABI ldiv_t div(long __x, long __y) _NOEXCEPT { return ::ldiv(__x, __y); }
      |                              ^
/Users/carlocab/github/llvm-project/build/bin/../include/c++/v1/stdlib.h:143:82: error: no member named 'ldiv' in the global namespace [clang-diagnostic-error]
  143 | inline _LIBCPP_HIDE_FROM_ABI ldiv_t div(long __x, long __y) _NOEXCEPT { return ::ldiv(__x, __y); }
      |                                                                                ~~^
/Users/carlocab/github/llvm-project/build/bin/../include/c++/v1/stdlib.h:145:30: error: unknown type name 'lldiv_t' [clang-diagnostic-error]
  145 | inline _LIBCPP_HIDE_FROM_ABI lldiv_t div(long long __x, long long __y) _NOEXCEPT { return ::lldiv(__x, __y); }
      |                              ^
/Users/carlocab/github/llvm-project/build/bin/../include/c++/v1/stdlib.h:145:93: error: no member named 'lldiv' in the global namespace [clang-diagnostic-error]
  145 | inline _LIBCPP_HIDE_FROM_ABI lldiv_t div(long long __x, long long __y) _NOEXCEPT { return ::lldiv(__x, __y); }
      |                                                                                           ~~^
/Users/carlocab/github/llvm-project/build/bin/../include/c++/v1/string.h:94:102: error: unknown type name 'size_t'; did you mean 'std::size_t'? [clang-diagnostic-error]
   94 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_PREFERRED_OVERLOAD const void* memchr(const void* __s, int __c, size_t __n) {
      |                                                                                                      ^
/Users/carlocab/github/llvm-project/build/bin/../include/c++/v1/__cstddef/size_t.h:20:7: note: 'std::size_t' declared here
   20 | using size_t = decltype(sizeof(int));
      |       ^
/Users/carlocab/github/llvm-project/build/bin/../include/c++/v1/string.h:97:90: error: unknown type name 'size_t'; did you mean 'std::size_t'? [clang-diagnostic-error]
   97 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_PREFERRED_OVERLOAD void* memchr(void* __s, int __c, size_t __n) {
      |                                                                                          ^
/Users/carlocab/github/llvm-project/build/bin/../include/c++/v1/__cstddef/size_t.h:20:7: note: 'std::size_t' declared here
   20 | using size_t = decltype(sizeof(int));
      |       ^
Found compiler error(s).

Weirdly enough, dropping the -v flag produces the original error:

clang-tidy helloworld.cpp
❯ ~LLVM/build/bin/clang-tidy helloworld.cpp
1 error generated.
Error while processing /tmp/clang-tidy-cpp/helloworld.cpp.
helloworld.cpp:2:10: error: 'iostream' file not found [clang-diagnostic-error]
    2 | #include <iostream>
      |          ^~~~~~~~~~
Found compiler error(s).

But the source of all these errors should be the same.

@carlocab
Copy link
Member

carlocab commented Nov 24, 2024

Ah, that'll be because the config files aren't loaded until https://github.com/llvm/llvm-project/blob/0c21ed48f40b2a6e3aa7e5d1873cf2455e847786/clang/lib/Driver/Driver.cpp#L1249-L1256.

Edit: No, I don't think that's it, since BuildCompilation is later called at https://github.com/llvm/llvm-project/blob/0c21ed48f40b2a6e3aa7e5d1873cf2455e847786/clang/lib/Tooling/CompilationDatabase.cpp#L283. Not sure what's wrong then.

@carlocab
Copy link
Member

Ah, wait. I forgot to add the right config file in the configured location. If I create one with the appropriate --sysroot flag, doing

path/to/clang-tidy helloworld.cpp -- -v

succeeds. But dropping the -- -v doesn't. Not really sure why passing the extra flags changes clang-tidy's behaviour here, but... 🤷

carlocab added a commit that referenced this issue Dec 8, 2024
If you try to target a macOS version that is older than the host
machine, our config files will point to an SDK that is likely not
available on the host.[^1]

We can fix this by defaulting to the unversioned SDK in these cases.

[^1]: See, for example, #196344 (comment)
carlocab added a commit that referenced this issue Dec 10, 2024
If you try to target a macOS version that is older than the host
machine, our config files will point to an SDK that is likely not
available on the host.[^1]

We can fix this by defaulting to the unversioned SDK in these cases.

[^1]: See, for example, #196344 (comment)
carlocab added a commit that referenced this issue Dec 10, 2024
If you try to target a macOS version that is older than the host
machine, our config files will point to an SDK that is likely not
available on the host.[^1]

We can fix this by defaulting to the unversioned SDK in these cases.

[^1]: See, for example, #196344 (comment)
carlocab added a commit that referenced this issue Dec 12, 2024
If you try to target a macOS version that is older than the host
machine, our config files will point to an SDK that is likely not
available on the host.[^1]

We can fix this by defaulting to the unversioned SDK in these cases.

[^1]: See, for example, #196344 (comment)

This comment has been minimized.

@github-actions github-actions bot added the stale No recent activity label Dec 16, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 24, 2024
@carlocab carlocab reopened this Dec 24, 2024
@carlocab carlocab added help wanted Task(s) needing PRs from the community or maintainers and removed stale No recent activity labels Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Reproducible Homebrew/homebrew-core bug help wanted Task(s) needing PRs from the community or maintainers
Projects
None yet
Development

No branches or pull requests

5 participants