0

Due to my job, I have to develop software with only C but not C++. It will be good that when I write class A {}; Vscode will display an error or a warning. Now I use clangd, it will be great if some settings can satisfy.

komonzhang
  • 67
  • 5
  • 1
    Shouldn't Clangd detect the language from the file extension? Unless you override it in `compile_commands.json`. – HolyBlackCat Oct 18 '22 at 11:31
  • 1
    You could specify in your compile_flags.txt (Or another configuration file for clangd) that you are strictly asking for C. – AggelosT Oct 18 '22 at 13:19

1 Answers1

1

Clangd will correctly issue diagnostics for C++-only constructs, if it's parsing your file in C mode.

So it's a matter of making sure clangd is parsing your files in the correct language mode.

If your file's extension is unambiguously a C-language extension (for example, .c), then clang should parse the file in C mode automatically.

If the extension is ambiguous, like .h, then clangd attempts to choose the language heuristically, which can sometimes give a wrong answer. In this case, you can specify the language explicitly in the file's compile command, for example by adding -x c-header to specify "parse as a C header".

One way to do this is using CompileFlags: Add: in the clangd config file. For example, to specify that all .h files in the project are C headers, you might add the following to the project .clangd file:

If:
  PathMatch: .*\.h

CompileFlags:
  Add: [-xc-header]
HighCommander4
  • 50,428
  • 24
  • 122
  • 194