clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
592 stars 97 forks source link

Issues with MinGw compilers. #628

Closed PaNDa2code closed 2 months ago

PaNDa2code commented 2 months ago

Hey there,

I've been using Neovim with clangd for C and C++, and I'm encountering an issue with my configuration when working with C++ files that include windows.h. While I've set up my configuration file config.yaml to use MinGW compilers without any problem for C files, it seems like clangd can't find windows.h when processing C++ files.

Here's my config.yaml:

If:
  PathMatch: .*\.c .*\.h

CompileFlags:
  Compiler: x86_64-w64-mingw32-gcc
  Add: ["-I../headers", "-I$INCLUDE_PATH"]
---

If:
  PathMatch: .*\.cpp .*\.hpp

CompileFlags:
  Compiler: x86_64-w64-mingw32-g++
  Add: ["-I../headers", "-I$INCLUDE_PATH"]
---

However, when I include windows.h in a C++ file and run clangd, it gives me an error saying that the file is not found:

clangd --enable-config --check=path/to/main.cpp                                               
I[17:59:03.566] Debian clangd version 16.0.6 (19)
I[17:59:03.566] Features: linux+grpc
I[17:59:03.566] PID: 1039827
I[17:59:03.566] Working directory: /home/panda
I[17:59:03.566] argv[0]: clangd
I[17:59:03.566] argv[1]: --enable-config
I[17:59:03.566] argv[2]: --check=path/to/main.cpp
I[17:59:03.566] Entering check mode (no LSP server)
I[17:59:03.566] Testing on source file /home/panda/path/to/main.cpp
E[17:59:03.566] config error at /home/panda/.config/clangd/config.yaml:24:0: Config should be a dictionary
I[17:59:03.566] Loading compilation database...
I[17:59:03.567] Failed to find compilation database for /home/panda/path/to/main.cpp
I[17:59:03.567] Generic fallback command is: /usr/lib/llvm-16/bin/clang -resource-dir=/usr/lib/llvm-16/lib/clang/16 -- /home/panda/path/to/main.cpp
I[17:59:03.567] Parsing command...
I[17:59:03.568] internal (cc1) args are: -cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -fcoverage-compilation-dir=/home/panda/path/to -resource-dir /usr/lib/llvm-16/lib/clang/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/x86_64-linux-gnu/c++/13 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/13/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir=/home/panda/path/to -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -no-round-trip-args -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -x c++ /home/panda/path/to/main.cpp
I[17:59:03.568] Building preamble...
I[17:59:03.811] Indexing headers...
I[17:59:03.879] Built preamble of size 3577908 for file /home/panda/path/to/main.cpp version null in 0.31 seconds
E[17:59:03.879] [pp_file_not_found] Line 2: 'windows.h' file not found
I[17:59:03.879] Building AST...
I[17:59:03.884] Indexing AST...
I[17:59:03.885] Building inlay hints
I[17:59:03.885] Building semantic highlighting
I[17:59:03.885] Testing features at each token (may be slow in large files)
I[17:59:03.887] All checks completed, 1 errors

I've tried adjusting the paths and configurations, but haven't had any luck so far. Any suggestions on how to resolve this would be greatly appreciated.

Thanks!

PaNDa2code commented 2 months ago

Hey there,

I realized I forgot to mention that I made changes to the config file. Here's the old configuration, which was working fine only for C:

CompileFlags:
  Compiler: x86_64-w64-mingw32-gcc
  Add: ["-I../headers", "-I$INCLUDE_PATH"]

Now, I encountered an issue with the new config. It's throwing an error message here:

E[17:59:03.566] config error at /home/panda/.config/clangd/config.yaml:24:0: Config should be a dictionary

Do you think this problem is related to my new config? If so, how can I configure clangd to use Mingw compilers for both C and C++?

HighCommander4 commented 2 months ago

It looks like the reason for that error is the trailing ---. A --- starts a new config fragment and clangd doesn't expect a fragment to be empty.

Does removing the trailing --- help?

PaNDa2code commented 2 months ago

I resolved these issues by hardcoding the include paths, That's working well.

If:
  PathMatch: .*\.c
CompileFlags:
  Compiler: x86_64-w64-mingw32-gcc
  Add: ["-I../headers", "-I$INCLUDE_PATH"]
If:
  PathMatch: .*\.cpp
CompileFlags:
  Compiler: x86_64-w64-mingw32-g++
  Add: ["-I../headers", "-I$INCLUDE_PATH", "-I/usr/lib/gcc/x86_64-w64-mingw32/12-win32/include/c++", "-I/usr/lib/gcc/x86_64-w64-mingw32/12-win32/include/c++/x86_64-w64-mingw32", "-I/usr/lib/gcc/x86_64-w64-mingw32/12-win32/include/c++/backward"]
---