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

How can I set CompileFlags #619

Closed DYAAL closed 2 months ago

DYAAL commented 2 months ago

How can I set CompileFlags: Remove: in a plugin? Can you help me figure out if there's a way to set CompileFlags: in a plugin, or do I have to generate a .clangd file to set this content?

HighCommander4 commented 2 months ago

Config file features like CompileFlags: Remove: only work with an actual file.

For plugins, clangd does support an LSP protocol extension described here, which allows a client to provide entire compile commands (not edits to compile commands like "add" or "remove"). If using this protocol extension, it would take the place of the server using compile_commands.json altogether. (So for example, you can load the compile_commands.json yourself in the plugin, make the edits to it that you want, and send the resulting commands to clangd using the protocol extension.)

DYAAL commented 2 months ago

Config file features like CompileFlags: Remove: only work with an actual file.

For plugins, clangd does support an LSP protocol extension described here, which allows a client to provide entire compile commands (not edits to compile commands like "add" or "remove"). If using this protocol extension, it would take the place of the server using compile_commands.json altogether. (So for example, you can load the compile_commands.json yourself in the plugin, make the edits to it that you want, and send the resulting commands to clangd using the protocol extension.)

Since my compile_commands.json is generated by cmake, and my project may involve some cross-compilation issues, there are many compilation commands that clangd does not recognize, resulting in errors. So, modifying the compile-time commands is not practical for me, and I don't want to change them. Instead, I just want to suppress these error messages. There is a method online suggesting adding a .clangd file with the following configuration: CompileFlags: Remove: [-mno-direct-extern-access, -fconserve-stack, -fno-allow-store-data-races, -mfunction-return=thunk-extern, -mindirect-branch-cs-prefix, -mindirect-branch-register, -mindirect-branch=thunk-extern, -mskip-rax-setup, -mpreferred-stack-boundary=3, -mno-fp-ret-in-387, -mthumb-interwork] However, doing this each time is a bit troublesome. What I'm thinking is to configure this issue once and for all in the plugin. 如果您能看懂中文可以直接用中文回复我。 Thanks a lot.

HighCommander4 commented 2 months ago

There is a method online suggesting adding a .clangd file with the following configuration: CompileFlags: Remove: [-mno-direct-extern-access, -fconserve-stack, -fno-allow-store-data-races, -mfunction-return=thunk-extern, -mindirect-branch-cs-prefix, -mindirect-branch-register, -mindirect-branch=thunk-extern, -mskip-rax-setup, -mpreferred-stack-boundary=3, -mno-fp-ret-in-387, -mthumb-interwork] However, doing this each time is a bit troublesome.

Note that there is also a user config file (e.g. .config/clangd/config.yaml on Linux; see https://clangd.llvm.org/config.html#files for details) where you can specify config options that should apply to all projects in a single place.

DYAAL commented 2 months ago

There is a method online suggesting adding a .clangd file with the following configuration: CompileFlags: Remove: [-mno-direct-extern-access, -fconserve-stack, -fno-allow-store-data-races, -mfunction-return=thunk-extern, -mindirect-branch-cs-prefix, -mindirect-branch-register, -mindirect-branch=thunk-extern, -mskip-rax-setup, -mpreferred-stack-boundary=3, -mno-fp-ret-in-387, -mthumb-interwork] However, doing this each time is a bit troublesome.

Note that there is also a user config file (e.g. .config/clangd/config.yaml on Linux; see https://clangd.llvm.org/config.html#files for details) where you can specify config options that should apply to all projects in a single place. ———————————————————————————————————————— It's strange that there doesn't seem to be a config.yaml file on my Windows computer. I'm not sure if it's because of an update, but overall, I think it would be really helpful if the plugin provided this feature. image

HighCommander4 commented 2 months ago

You have to create the user config file, it's not pre-created.