microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.45k stars 1.53k forks source link

Support for generic GCC toolchains #6798

Open longnguyen2004 opened 3 years ago

longnguyen2004 commented 3 years ago

Type: LanguageService

Describe the bug

Many different embedded platforms use GCC as their compiler. This presents a problem for the extension, since it only supports x86, x64, arm and arm64, while there are many embedded platforms (just to name a few: AVR and ESP32). It's impossible to keep track of them all, so we should create a generic GCC target, which will only do the minimal things to get IntelliSense working (get C and C++ standards, get include folders, get predefined macros, etc.)

Steps to reproduce

Create a C++ configuration with a compiler that targets an embedded platform.

Expected behavior

Proper IntelliSense for embedded toolchains.

Logs ``` cpptools/didChangeCppProperties Attempting to get defaults from C++ compiler in "compilerPath" property: 'C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe' Querying compiler for default C++ language standard using command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -x c++ -E -dM nul Querying compiler for default C language standard using command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -x c -E -dM nul Querying compiler's default target using command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -dumpmachine Compiler returned default target value: avr Unhandled default compiler target value detected: avr Attempting to get defaults from compiler found on the machine: 'C:\llvm-mingw\bin\gcc.exe' Querying compiler for default C++ language standard using command line: "C:\llvm-mingw\bin\gcc.exe" -x c++ -E -dM nul Querying compiler for default C language standard using command line: "C:\llvm-mingw\bin\gcc.exe" -x c -E -dM nul Querying compiler's default target using command line: "C:\llvm-mingw\bin\gcc.exe" -dumpmachine Unhandled default compiler target value detected: No suitable compiler found. Please set the "compilerPath" in c_cpp_properties.json. Attempting to get defaults from C compiler in "compilerPath" property: 'C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe' Attempting to get defaults from compiler found on the machine: 'C:\llvm-mingw\bin\gcc.exe' No suitable compiler found. Please set the "compilerPath" in c_cpp_properties.json. ```

Screenshots

Unable to locate avr include files because the default include paths aren't set up properly: image

Additional context

Default search paths for avr-gcc:

install: c:\users\nghuu\.platformio\packages\toolchain-atmelavr\bin\../lib/gcc/avr/5.4.0/
programs: =c:/users/nghuu/.platformio/packages/toolchain-atmelavr/bin/../libexec/gcc/avr/5.4.0/;c:/users/nghuu/.platformio/packages/toolchain-atmelavr/bin/../libexec/gcc/;c:/users/nghuu/.platformio/packages/toolchain-atmelavr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/avr/5.4.0/;c:/users/nghuu/.platformio/packages/toolchain-atmelavr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/
libraries: =c:/users/nghuu/.platformio/packages/toolchain-atmelavr/bin/../lib/gcc/avr/5.4.0/;c:/users/nghuu/.platformio/packages/toolchain-atmelavr/bin/../lib/gcc/;c:/users/nghuu/.platformio/packages/toolchain-atmelavr/bin/../lib/gcc/avr/5.4.0/../../../../avr/lib/avr/5.4.0/;c:/users/nghuu/.platformio/packages/toolchain-atmelavr/bin/../lib/gcc/avr/5.4.0/../../../../avr/lib/

The include paths are there, but the extension isn't grabbing it.

sean-mcmanus commented 3 years ago

Similar to https://github.com/microsoft/vscode-cpptools/issues/3222 .

longnguyen2004 commented 3 years ago

Also related to #4653

heartacker commented 3 years ago

yes we need it

longnguyen2004 commented 3 years ago

yes we need it

@heartacker Please upvote this issue so it can be considered.

github-actions[bot] commented 3 years ago

This feature request has received enough votes to be added to our backlog.

Colengms commented 3 years ago

@longnguyen2004 Are you still seeing this issue with a recent version of the extension? I believe 1.2.0-insiders included a change which caused us to give up on compilers that did not successfully return defaults from a simple query without any additional config args. We had to revert that change, as some compilers are unable to provide defaults without additional args.

Based on the log output, I see that we were failing to query avr-gcc as well as llvm-mingw gcc. If that is still an issue in a recent version, it would seem to be due to those compilers diverging from the output format we usually see when querying gcc compilers. We would need to investigate and add support for compilers with unique output, on a per-compiler basis. Since support for an agnostic IntelliSense mode is tracked by https://github.com/microsoft/vscode-cpptools/issues/4653 , we could repurpose this issue to track adding support for avr-gcc, specifically.

longnguyen2004 commented 3 years ago

Although IntelliSense still doesn't recognize the avr target, at least the include paths are indexed correctly now, which is what I need

Attempting to get defaults from C++ compiler in "compilerPath" property: 'C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe'
Querying compiler for default C++ language standard using command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -x c++ -E -dM nul
Detected language standard version: gnu++14
Querying compiler for default C language standard using command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -x c -E -dM nul
Detected language standard version: gnu11
Querying compiler's default target using command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -dumpmachine
Compiler returned default target value: avr

Unhandled default compiler target value detected: avr

Compiler query command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -mmcu=atmega328p -std=c++11 -Wp,-v -E -dD -x c++ -m64  nul
Compiler does not support 64-bit. Falling back to 32-bit intelliSenseMode.
Compiler query command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -mmcu=atmega328p -std=c++11 -Wp,-v -E -dD -x c++ -m32  nul
Failed to query compiler. Falling back to no bitness.
Compiler query command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -mmcu=atmega328p -std=c++11 -Wp,-v -E -dD -x c++   nul
Attempting to get defaults from C compiler in "compilerPath" property: 'C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe'
Compiler query command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -mmcu=atmega328p -std=c11 -Wp,-v -E -dD -x c -m64  nul
Compiler does not support 64-bit. Falling back to 32-bit intelliSenseMode.
Compiler query command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -mmcu=atmega328p -std=c11 -Wp,-v -E -dD -x c -m32  nul
Failed to query compiler. Falling back to no bitness.
Compiler query command line: "C:/Users/nghuu/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe" -mmcu=atmega328p -std=c11 -Wp,-v -E -dD -x c   nul
  Folder: C:/USERS/NGHUU/.PLATFORMIO/PACKAGES/TOOLCHAIN-ATMELAVR/AVR/INCLUDE/ will be indexed
  Folder: C:/USERS/NGHUU/.PLATFORMIO/PACKAGES/TOOLCHAIN-ATMELAVR/LIB/GCC/AVR/7.3.0/INCLUDE/ will be indexed
  Folder: C:/USERS/NGHUU/.PLATFORMIO/PACKAGES/TOOLCHAIN-ATMELAVR/LIB/GCC/AVR/7.3.0/INCLUDE-FIXED/ will be indexed
...

I think all we need is a generic gcc/clang mode that disables the target and bitness detection, and it's good to go. Although not related, llvm-mingw is working great as well

Attempting to get defaults from C++ compiler in "compilerPath" property: 'C:\llvm-mingw\bin\clang-12.exe'
Querying compiler for default C++ language standard using command line: "C:\\llvm-mingw\\bin\\clang-12.exe" -x c++ -E -dM nul
Detected language standard version: c++14
Querying compiler for default C language standard using command line: "C:\\llvm-mingw\\bin\\clang-12.exe" -x c -E -dM nul
Detected language standard version: c17
Querying compiler's default target using command line: "C:\llvm-mingw\bin\clang-12.exe" -dumpmachine
Compiler returned default target value: x86_64-w64-windows-gnu

Compiler query command line: "C:\\llvm-mingw\\bin\\clang-12.exe" -glldb -lc++ -lunwind -LC:\vcpkg\installed\x64-mingw-dynamic\lib -lcpr -lcurl -std=c++17 -Wp,-v -E -dD -x c++ -m64 -fno-blocks nul
Attempting to get defaults from C compiler in "compilerPath" property: 'C:\llvm-mingw\bin\clang-12.exe'
Compiler query command line: "C:\\llvm-mingw\\bin\\clang-12.exe" -glldb -lc++ -lunwind -LC:\vcpkg\installed\x64-mingw-dynamic\lib -lcpr -lcurl -std=c17 -Wp,-v -E -dD -x c -m64 -fno-blocks nul
  Folder: C:/LLVM-MINGW/INCLUDE/ will be indexed
  Folder: C:/LLVM-MINGW/LIB/CLANG/12.0.0/INCLUDE/ will be indexed
...
4wrxb commented 2 years ago

I'm not sure if this is related, but we use clang's --gcc-toolchain option to specify a replacement for sysroot includes (which are very old in our env). compile_commands.json reflects this option (and I see it printed in log-diagnostics under the command entry), but the includes listed still include the default system includes instead.

I tried adding the --gcc-toolchain option to the configuration as a compiler argument as well, but it has no effect either.