ccls icon indicating copy to clipboard operation
ccls copied to clipboard

Wrong diagnostics on macOS w/ clang10

Open flanggut opened this issue 4 years ago • 25 comments

Observed behavior

  • compile_commands.json
[
  {
    "directory": "/Users/flanggut/tmp/ccls",
    "file": "/Users/flanggut/tmp/ccls/main.cpp",
    "arguments": [
      "/usr/local/opt/llvm/bin/clang++",
      "-std=c++17",
      "-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
      "-isystem /Library/Developer/CommandLineTools/usr/lib/clang/11.0.3/include/",
      "-I.",
      "main.cpp"
    ]
  }
]
  • main.cpp
#include <chrono>
auto clk() {
  return std::chrono::high_resolution_clock::now();
}
int main(int /*argc*/, char** /*argv*/) {
  auto clk0 = clk();
}

ccls --index=. -v=2 Correctly identifies and and indexes all headers.

/usr/local/opt/llvm/bin/clang++ --driver-mode=g++ -std=c++17 -isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -isystem /Library/Developer/CommandLineTools/usr/lib/clang/11.0.3/include/ -I. main.cpp

File compiles without issues.

In my editor ccls claims that <chrono> include cannot be found under ./chrono, note that the error is always shown for the include path -I., if I change that to -I.. the error will show that ../chrono cannot be found.

This only happens since clang/llvm10 and does not happen if I compile ccls against clang/llvm9.

Expected behavior

No diagnostics should be shown in an editor.

Steps to reproduce

  1. brew install ccls (recently updated to use llvm10)
  2. Open file in editor w/ ccls as language server

System information

  • ccls version (git describe --tags --long): brew info ccls
  • clang version: clang version 10.0.0
  • OS: x86_64-apple-darwin19.4.0
  • Editor: nvim / vscode
  • Language client (and version): coc.nvim / vscode-ccls

flanggut avatar Apr 27 '20 11:04 flanggut

same problem

thinkiny avatar Apr 27 '20 12:04 thinkiny

Same, and I can confirm that I'm setting the resourcesDir in my ccls initializer options. Check https://github.com/MaskRay/ccls/issues/611

For me this also occurs since the latest homebrew update.

sunbubble avatar Apr 27 '20 18:04 sunbubble

It might be a weird interaction in the homebrew version of things. I managed to build a version using the official llvm release that runs without issues. If anyone needs a workaround for now:

  1. Download the official llvm release from https://releases.llvm.org/download.html#10.0.0 (direct link) and unpack.
  2. Manually install ccls HEAD version in homebrew
brew uninstall ccls
brew install ccls --HEAD -i

this will open a terminal in a temp directory that allows you to "manually" compile and install ccls within homebrew. In there I ran

cmake -H. -BRelease -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/Users/flanggut/local/clang+llvm-10.0.0-x86_64-apple-darwin/ -DCMAKE_INSTALL_PREFIX=/usr/local/Cellar/ccls/HEAD-c5acf62 -DUSE_SYSTEM_RAPIDJSON=OFF -DCMAKE_CXX_COMPILER=/Users/flanggut/local/clang+llvm-10.0.0-x86_64-apple-darwin/bin/clang++

pointing cmake directly to the official llvm release (make sure the install prefix points to the right directory, homebrew will tell you where it expects the installed files). Then you just do

cd Release
make install
exit

And homebrew automatically links ccls if you've installed it to the right directory.

flanggut avatar May 03 '20 14:05 flanggut

@flanggut, sorry if this is a stupid question, but did you try this with the homebrew version of llvm?

sunbubble avatar May 03 '20 14:05 sunbubble

That was the first thing I tried, unfortunately it didn't work for me :/

flanggut avatar May 03 '20 14:05 flanggut

Weird. Could there be a problem with the packaging of llvm for homebrew?

sunbubble avatar May 03 '20 14:05 sunbubble

Maybe, I haven't looked into how the homebrew version of llvm is built.

flanggut avatar May 03 '20 14:05 flanggut

I'm having a similar issue. Running MacOS Catalina with Apple clang version 11.0.3

T-Skinner avatar May 11 '20 06:05 T-Skinner

@flanggut Thank you! This workaround resolves my issue in https://github.com/MaskRay/ccls/issues/191#issuecomment-631892313

I believe you are right that this is a version issue with homebrew. The problems I experienced happened after no changes to configuration, only version upgrades.

I'm not even sure what a proper fix for this type of issue looks like, it genuinely might be an issue with homebrew llvm

neiljohari avatar May 22 '20 00:05 neiljohari

I think this is related -- I'm having a similar issue finding Arduino.h (installed via Platform.io):

I have #include <Arduino.h> in my code, -I/Users/cweagans/.platformio/packages/framework-arduino-avr/cores/arduino in .ccls in my project root, and Arduino.h definitely exists at /Users/cweagans/.platformio/packages/framework-arduino-avr/cores/arduino/Arduino.h. ccls is telling me that it can't open /Users/cweagans/Code/shopctl/include/Arduino.h though.

I have not tried the workaround posted above, but I'll give it a go when I have some time.

cweagans avatar May 28 '20 21:05 cweagans

Same issue here.

图片

brglng avatar Jun 07 '20 05:06 brglng

@flanggut Thank you so much, been stuck on this for hours. Works like a charm!

However I had the problem mentioned here. clang was complaining about an invalid option -platform_version.

So I had to add -DCMAKE_CXX_FLAGS="-mlinker-version=450".

For people with similar problems: You can find your linker version via ld -v.

This oneliner should do the trick:

ld -v 2>&1 | head -n1 | awk -F "ld64-" '{print int($2)}'

sprmn avatar Jun 08 '20 21:06 sprmn

Yup. Same issue here.

Confirmed on macOS cataline 10.15.5 and clang 11.0.3 with the HEAD version of ccls from homebrew.

krishnakumarg1984 avatar Jul 09 '20 16:07 krishnakumarg1984

@cweagans what works for me is using the compile_commands.json db generated with pio run -t compiledb. I symlink it from its location to the project root.

madeddie avatar Jul 23 '20 19:07 madeddie

Same issue..

@flanggut Thanks, only your solution works for me 👍

dhoon-dev avatar Jul 29 '20 17:07 dhoon-dev

flanggut's solution works! In this case, whenever there is a new version of ccls, do I have to recompile everything? Thanks!

yushangakki avatar Aug 24 '20 20:08 yushangakki

I tried most of the workarounds listed in the various issues linked here and cross-linked to each-other. The one thing that did it for me was to have this .ccls alongside my compile_commands.json:

%compile_commands.json
-isystem/Library/Developer/CommandLineTools/usr/include/
-isystem/Library/Developer/CommandLineTools/usr/include/c++/v1

The part that did it for me was to remove all spaces between '-isystem' and the path. Writing it like -isystem /Library/Deveolper/CommandLineTools/usr/include/ causes the same error.

HealsCodes avatar Nov 27 '20 17:11 HealsCodes

I am no expert on either MacOS or on the .ccls file format: I don't use the former and struggle to understand the details of the latter (I wish it could be replaced with a more standard format such as JSON or similar).

However, I'm pretty sure that the .ccls file requires each argument to be on its own line in the file. If you write this:

-isystem /Library/Developer/CommandLineTools/usr/include/

in .ccls, or if you add this to your compile_commands.json file:

      "-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",

that is the same thing as passing arguments like this to clang:

  clang ... '-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include' ...

(note the quotes here). If you check the clang docs the format for the -isystem option is:

-isystem<directory>
Add directory to SYSTEM include search path

Basically, you're adding the directory /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include (including the leading space character!!!) as a system path. That can explain why these paths don't seem to have any effect: it's equivalent to the path ./ /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include and there is no directory ./ / (space) in your working directory.

You can either remove the space as @Shirk did, or you can put each argument on a separate line / in a separate argument. For .ccls it would be:

-isystem
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include

and for compile_commands.json it would be something like:

      "-isystem",
      "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",

madscientist avatar Nov 27 '20 19:11 madscientist

Note I can't explain why it works with some versions of clang and not with others: there may be two different issues that are being conflated, or else maybe clang's command line option parser changed to become more strict about whitespace.

madscientist avatar Nov 27 '20 19:11 madscientist

Just wanted to provide an update: on macOS Big Sur 11.1, I finally have it working without needing @flanggut's solution (though that solution did save me for the last 9 months).

neiljohari avatar Jan 11 '21 21:01 neiljohari

On MacOS Big Sur 11.4, I meet the similar issue. ccls complains that can't find <stdarg.h>. The core problem here IMO is the option -isysroot. ccls can't parse the search path here.

tan-wei avatar Jun 11 '21 15:06 tan-wei

Ok, @flanggut 's method works for me:

brew uninstall ccls
brew install ccls --HEAD -i

A local compilation will work well.

tan-wei avatar Jun 11 '21 15:06 tan-wei

I am having the same issue on macOS Big Sur 11.6 but when I try @flanggut 's method I encounter the following error during the make install step:

make[2]: *** No rule to make target `/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/lib/libcurses.tbd', needed by `ccls'.  Stop.
make[2]: *** Waiting for unfinished jobs....
[ 97%] Building CXX object CMakeFiles/ccls.dir/src/messages/workspace.cc.o
make[1]: *** [CMakeFiles/ccls.dir/all] Error 2
make: *** [all] Error 2

I am using clang+llvm-12.0.0-x86_64-apple-darwin because with earlier versions I get a bunch of errors during the compilation process. Any help is highly appreciated.

Edit: I managed to solve it by following these steps. However, I still get the same issue while using ccls in my projects.

eagomez2 avatar Sep 23 '21 22:09 eagomez2

https://github.com/MaskRay/ccls/wiki/Build#macos this works for me on arm64 macOS.

#!/bin/sh
exec /opt/homebrew/bin/ccls --init='{"clang":{"extraArgs":[
  "-isystem/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1",
  "-isystem/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.5/include",
  "-isystem/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include",
  "-isystem/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
  "-isystem/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
]}}' "$@"

MaskRay avatar Sep 24 '21 02:09 MaskRay

Hi,

I have an Intel machine with Big Sur and I am using ccls with coc.nvim, so I added the corresponding paths to coc-settings.json as follows:

{
    "languageserver": {
        "ccls": {
            "command": "ccls",
            "filetypes": ["c", "cpp", "objc", "objcpp"],
            "rootPatterns": [".ccls", "compile_commands.json", ".vim/", ".git/", ".hg/"],
            "initializationOptions": {
                "cache": {
                    "directory": "/tmp/ccls"
                },
                // see https://github.com/MaskRay/ccls/issues/191
                "clang": {
                    // from clang -v -fsyntax-only -x c++ /dev/null
                    "extraArgs": [
                        "-isystem/usr/local/include",
                        "-isystem/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1",
                        "-isystem/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.5/include",
                        "-isystem/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
                        "-isystem/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include",
                        "-isystem/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
                    ],
                    //From clang -print-resource-dir
                    "resourceDir": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.5"
                }
            }
        }
    }
}

More concretely, if I do ccls --index=. -v=2 I can see the paths are added, including /usr/local/include that is the one that concerns me, because inside it I have Eigen that cannot be found by ccls. If I add it using:

#include <Eigen/Dense>

I see fatal error: 'Eigen/Dense' file not found. However, if I compile the project it does so correctly and runs fine.

Here is also my compile_commands.json exported using -DCMAKE_EXPORT_COMPILE_COMMANDS where I can also see that both /usr/local/include and /usr/local/include/eigen3 are added.

[
{
  "directory": "/Users/eagomez/Desktop/project/build",
  "command": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -I/usr/local/include -I/Users/eagomez/Desktop/project/source/include -isystem /usr/local/include/eigen3  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -std=gnu++2a -o CMakeFiles/CPPTest.dir/main.cpp.o -c /Users/eagomez/Desktop/project/source/main.cpp",
  "file": "/Users/eagomez/Desktop/project/source/main.cpp"
},
{
  "directory": "/Users/eagomez/Desktop/project/build",
  "command": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -I/usr/local/include -I/Users/eagomez/Desktop/project/source/include -isystem /usr/local/include/eigen3  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -std=gnu++2a -o CMakeFiles/CPPTest.dir/include/ETL.cpp.o -c /Users/eagomez/Desktop/project/source/include/ETL.cpp",
  "file": "/Users/eagomez/Desktop/project/source/include/ETL.cpp"
}
]

Thanks for your help!

eagomez2 avatar Sep 24 '21 10:09 eagomez2