microsoft / vscode-cpptools

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

Header-only IntelliSense should use language ID delivered with `didOpen` instead of defaulting to C++ #9138

Open JAicewizard opened 2 years ago

JAicewizard commented 2 years ago

Bug type: Language Service

Describe the bug

Steps to reproduce

  1. Open x/y.h
  2. See error

Expected behavior No squiggly lines

Code sample and logs

------- Workspace parsing diagnostics ------- Number of files discovered (not excluded): 26971 Number of files parsed: 3712

Colengms commented 2 years ago

Hi @JAicewizard . By default, when a .h file is opened and there is no source file (.cpp, .c, etc.) to associate it with, the C/C++ extension treats the file as containing C++ code. I don't believe the restrict keyword is supported in C++. Note this comment in the C++ specification.

I believe you could work around the issue if you add a .c file that includes that header. That .c file should be found when opening the header, and the header would be interpreted as containing C code instead of C++ code.

Does that resolve your issue?

JAicewizard commented 2 years ago

That does "fix" the issue, but I have explicitly set the language to C, not C++. screenshot-2022-04-04-205227

So it is probably the case that the the language server misinterprets the file even though I told VScode explicitly that this is a C file.

Colengms commented 2 years ago

Hi @JAicewizard . Thanks. We'll use this issue to track addressing that. In the meanwhile, it would also be possible to work around the issue by setting a file association in settings, like so:

{
    "files.associations": {
        "test.h": "c"
    }
}
JAicewizard commented 2 years ago

Yup this seems to work, even though vscode is now telling me it is in C++ mode, the c/c++ extension seems to be in C mode now. Maybe there is something not working with the language mode in vscode? Or maybe it doesnt do what I expect it to do.

sean-mcmanus commented 2 years ago

Yeah, it seems like we may not be responding correctly to C versus C++ language mode changes from the status bar. I filed an issue at https://github.com/microsoft/vscode-cpptools/issues/9141 .