C++ header scan and Code completion

In our current installation of Theia, code completion in C++ does not scan headers in the workspace. Code suggestions only suggests what it sees within the current file. Is it a configuration error from us or not supported?

[original thread by Laurent Perraud]

@simark can probably give details. But generally clangd is missing many features yet, to be a full-featured language server. So there will be ATM some big limitations, for sure.

[Simon Marchi]

Here’s a response on top of my head: I don’t think clangd will automatically scans all headers. But, if clangd has parsed another source file which included a given header, the symbols defined in that header should be present in clangd’s index, and therefore you should be able to see them in the completion results.

So, if you open a second source file which includes the given header, you should be able to see the symbols from that header in the completion results. Alternatively, if you use clangd’s global index (enabled with the –background-index flag), clangd will automatically parse and index all source files mentioned in your compile_commands.json. Therefore, all headers that are included by some source file somewhere should have their symbols available for completion.

If a header is not included anywhere by any source file, I don’t think you will ever see the symbols from that header in the completion items (except maybe if you open it?).

[Laurent Perraud]

Thank you very much for your answer. Where would I set the flag please? We have not gotten to use compile_commands.

[Simon Marchi]

To enable clangd’s background indexing, you can use the cpp.clangdArgs preference (I have it in my user preferences, so it applies to all workspaces). Make sure your version of clangd supports it first, I think you need version >= 8.

To generate a compile_commands.json file, there are different strategies, depending on the build system. If you use cmake, you can have it generated automatically:

https://cmake.org/cmake/help/v3.13/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html

Otherwise, the bear tool can often be used:

[Laurent Perraud]

“cpp.clangdArgs”: "background-index"v or
“cpp.clangdArgs”: “–background-index” please?

[Laurent Perraud]

we use bazel so we will have a little more homework to do

[Simon Marchi]

It would be “cpp.clangdArgs”: “–background-index” (this settings specifies the flags to pass to clangd). You can confirm it works by checking the process list (ps ax | grep clangd, if you are on Linux).

I found this with a quick Google search: https://github.com/grailbio/bazel-compilation-database

[Josh Bradley]

This type of information would be nice if it were added into a new theia-c+±docker example under theia-apps. In general, if there are specific preferences that must be set for any given language to enable some of the standard features users would expect, how would system admins (i.e. docker builders) know what to do? Is there a place besides this chat thread to document these language-specific requirements?

[Simon Marchi]

This is a bit of a project-wide problem, how is the documentation to end user distributed?

For now, we have put the C/C+±related documentation in the cpp package README:

https://github.com/theia-ide/theia/blob/master/packages/cpp/README.md

I will add a mention about –background-index. This switch is relatively new in clangd, but eventually I think we should enable it by default.

[Simon Marchi]

Here’s a PR about it: https://github.com/theia-ide/theia/pull/4196

[Laurent Perraud]

@simark I think I could have a good intermediate step before the compile.json if I could find how to have clangd use the right include path. as an example I can use clang-check as follow: clang-check – -I.

[Simon Marchi]

Yeah, so there are additional options clangd can take to configure compilation flags.

[Simon Marchi]

In particular, this to set custom flags per file: https://github.com/llvm/llvm-project/blob/master/clang-tools-extra/clangd/Protocol.h#L383

[Simon Marchi]

Or even this, to set some flags to use for files that are not found in any compilation database

[Simon Marchi]

[Simon Marchi]

None of them are currently integrated in Theia

[Simon Marchi]

It sounds like the latter would be useful to you? You want all the files in your project to use that include path?

[Simon Marchi]

I think we could add a preference for that (fallbackFlags) and send that value when we initialize clangd

[Simon Marchi]

Would you be interested in trying to implement? It could be a relatively simple first patch to Theia :slight_smile: