microsoft / vscode-cpptools

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

Intellisense with latest standards (e.g. C++20) works at the beginning, but forgets the standard as soon as the file is edited #10953

Closed codimoc closed 1 year ago

codimoc commented 1 year ago

Environment

Bug Summary and Steps to Reproduce

Bug Summary: When setting Intellisense to use C++ 20 standard and displaying a file containing keywords like "concept" or "requires" (which are features in C++20) it works. As soon as the file is edited again, it seems that it is losing the standard and it shows errors on "concept" as missing symbol. Basically it shows errors as if the standard is back to C++ 17. Any action to reload the workspace or to re-save the file .vscode/settings resolves the errors. However they come back as soon as the file is re-edited.

Steps to reproduce:

  1. set Intellisense standard as C++20, for example in settings.json with "C_Cpp.default.cppStandard": "c++20"
  2. write a cpp files containing C++ features, e.g.:
    
    #include <concepts> 

template concept Numeric = std::integral|| std::floating_point;

3. initially all fine. Then add any line and the errors squiggles appears (e.g. concept is not recognized)
4. re-edit or resave settings.json, or reload workspace and errors disappear
5. Reproducible over and over 

Expected behavior:
Intellisense should continue to work on C++20 standard if this is set

### Configuration and Logs

```shell
settings.json:

{
    "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
    "C_Cpp.default.cppStandard": "c++20"
}

no c_cpp_poperty.json but it happened also with this file set

Other Extensions

No response

Additional context

No response

sean-mcmanus commented 1 year ago

Can you run the C/C++: Log Diagnostics command before and after the IntelliSense mode seems to change and see if the Standard Version changes from c++20 to c++17 in the logging? Also, in that logging do you see c++17 in the "Custom configurations:" sections, such as in compilerFragments? Checking if the issue repros when "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools" is removed could also help.

codimoc commented 1 year ago

Thanks Sean. Today it seems that the error squiggles stay there also after a "Reload windows". They are not displayed only when the folder is loaded on start-up. However, apart from date, time and process id, the C++ log diagnostic is basically the same. Here is a copy: Thx

`-------- Diagnostics - 15/05/2023, 19:36:17 Version: 1.15.4 Current Configuration: { "name": "Linux", "includePath": [ "/home/gualtiero/proj/Cpp/cpp_features/" ], "defines": [], "compilerPath": "/usr/bin/clang", "cStandard": "c17", "compilerPathInCppPropertiesJson": "/usr/bin/clang", "cppStandard": "c++20", "intelliSenseMode": "linux-gcc-x64", "intelliSenseModeIsExplicit": true, "cStandardIsExplicit": false, "cppStandardIsExplicit": true, "mergeConfigurations": false, "compilerPathIsExplicit": false, "configurationProvider": "ms-vscode.cmake-tools", "browse": { "path": [ "${workspaceFolder}/" ], "limitSymbolsToIncludedHeaders": true } } Custom browse configuration: { "browsePath": [ "/home/gualtiero/proj/Cpp/cpp_features/build/CMakeFiles", "/home/gualtiero/proj/Cpp/cpp_features/src" ], "compilerPath": "/usr/bin/g++", "compilerArgs": [], "compilerFragments": [ "-g", "-std=gnu++2a" ] } Custom configurations: [ /home/gualtiero/proj/Cpp/cpp_features/src/concepts.cpp ] { "includePath": [], "defines": [], "compilerPath": "/usr/bin/g++", "compilerArgs": [], "compilerFragments": [ "-g", "-std=gnu++2a" ] } Translation Unit Mappings: [ /home/gualtiero/proj/Cpp/cpp_features/src/concepts.cpp ]: /home/gualtiero/proj/Cpp/cpp_features/src/concepts.cpp Translation Unit Configurations: [ /home/gualtiero/proj/Cpp/cpp_features/src/concepts.cpp ]: Process ID: 4164 Memory Usage: 95 MB Compiler Path: /usr/bin/g++ Includes: /usr/include/c++/10 /usr/include/x86_64-linux-gnu/c++/10 /usr/include/c++/10/backward /usr/lib/gcc/x86_64-linux-gnu/10/include /usr/local/include /usr/include/x86_64-linux-gnu /usr/include Standard Version: c++20 IntelliSense Mode: linux-gcc-x64 Other Flags: --g++ --gnu_version=100201 Total Memory Usage: 95 MB

------- Workspace parsing diagnostics ------- Number of files discovered (not excluded): 20836

`

sean-mcmanus commented 1 year ago

Hmm...I'm not reproducing the issue and your logging looks okay. One potential thing to try is setting C_Cpp.intelliSenseCacheSize to 0. Otherwise, I'll see if anyone else on my team can repro this.

codimoc commented 1 year ago

Thanks Sean. C_Cpp.intelliSenseCacheSize=0 does not solve. To be more precise, as soon as the settings file is changed, the squiggles disappear as if it is working. For any editing of the active cpp file, the squiggles come back. I wonder: do you have any configuration where you declare a concept in VS C++ and it does not show any error?

Basically, is it possible to have the following without Intellisense squiggles:

include \<concepts>

template\<typename T> concept Numeric = std::integral\<T> || std::floating_point\<T>;

Thanks

sean-mcmanus commented 1 year ago

Yes, that normally works (it works for us). We're not sure yet why you're seeing an error.

codimoc commented 1 year ago

hmm, that is strange. Have you tried to write a test file test.cpp, with the two lines above. Now, assuming no squiggles, adding an empty line at the end. That, or any other edit, generate the errors. Notice that it only happens with this feature, C++17 features behave well. Basically I am migrating out of Eclipse CDT and so far VS Code is the best, apart from this. You guys are doing a great work!

sean-mcmanus commented 1 year ago

It works for me with a nearly identical setup -- the only difference I see is I'm using gcc 10.4 instead of 10.2 -- can you upgrade your gcc to 10.4? It's possible there could be a bug with our handling of 10.2.

codimoc commented 1 year ago

ok, I will see if I can install on Debian 11. Thx

codimoc commented 1 year ago

Hmm, built gcc 10.4 from source, gcc -v shows 10.4 now and in config: --gnu_version=100400

However the problems remains :(

...And again "Reload Window" or re-saving settings solves the problem until the next edit...

And this is also interesting. When I play with the settings:

  1. "C_Cpp.default.cppStandard": "" -> errors as expected with concept
  2. "C_Cpp.default.cppStandard": "c++20" -> error disappear
  3. edit test.cpp file ->error reappears
  4. "C_Cpp.default.cppStandard": "" -> errors as expected with concept
  5. "C_Cpp.default.cppStandard": "c++20" -> error disappear
  6. ....(cycle on and on)

So this suggest that c++20 works at the begging but then is dropped for any edit action. Really weird

sean-mcmanus commented 1 year ago

Do you still have the configurationProvider set? It sounds like you do not.

Are you editing a .cpp or .h file?

codimoc commented 1 year ago

Tried both h and cpp, same problem I have re-added configurationProvider in local settings: { "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools" } same problem

Could you please send me a copy of all the settings files you are using: main config setting, local settings and, if present, c_cpp_properties.json? Also, shall I use this file (c_cpp_properties.json) or just the local settings and config settings? Not clear to me

sean-mcmanus commented 1 year ago

@codimoc Unfortunately, our team is still unable to repro this. We're using "C_Cpp.default.cppStandard": "c++20".

The fact that it shows c++20 in the Log Diagnostics logging but the errors are still occurring seems to imply there is some unexpected parsing error.

Can you run /usr/bin/g++ -std=gnu++2a -E ./test.cpp > ./test.out.cpp to generate a preprocessed file and then open the file and see if there are any IntelliSense errors in that file and if the bug repros with that file still? That will rule out any issue related to the headers or our processing of the header.

codimoc commented 1 year ago

No squiggles/ errors in test.out.cpp, even after editing. The file has many lines at the top (I guess from #include concepts) and ends with:

# 2 "./test.cpp" 2

# 2 "./test.cpp" template concept Numeric = std::integral|| std::floating_point;

So it looks like as if Intellisense gets confused in parsing my \<concepts> while gcc does a good job when it spits it out at the top. Could it be the depth of the cache when parsing many nested #includes?

Maybe this could be a clue. When I check the output for "JSON Language Server" in the bottom output tab, it shows only one line: cancelled. Is this normal?

codimoc commented 1 year ago

Wow! I have finally resolved the problem. It was very insidious, and this is the story on how I got it solved:

I have another Linux box with OpenSuse 15.4. Opensuse tends to have old versions, so my gcc was still v7, but I upgraded from source to v10.4, in line with my Debian laptop. The C++ extension was the same version on the two machines, while VS on OpenSuse was a few main versions behind (but this was not the problem). Now, I wrote my test.cpp with the "concept" declaration and c++20 standard for Intellisense: on this machine it all worked fine. Then, by comparing the configuration logs, and trying to make them match with each other, I removed a few setting and realized that my Debian defaults to a clang compiler (not gcc which is the default for the other machine) and that I had a clang installation on Debian. I uninstalled clang and now all my problems on Debian are solved. Hurrah!!

Thanks a lot for the support and the many suggestion given. You helped a lot understanding how it all hang toghether. This case can now be closed. :)