[clangd-dev] VS Code Clangd and CMake integration

Sam McCall via clangd-dev clangd-dev at lists.llvm.org
Tue Nov 27 11:25:38 PST 2018


On Tue, Nov 27, 2018 at 8:09 PM Anton Södergren via clangd-dev <
clangd-dev at lists.llvm.org> wrote:

> Hello! I'm not sure if this is the best place to ask, but it's better to
> get a discussion going rather than just being confused.
>
Right place! Welcome :-)


> I tried using Clangd in Visual Studio Code, but noticed that it doesn't
> integrate well with the CMake Tools extension, which makes it hard to use
> together with a CMake workflow. I would like a way for Clangd to
> automatically get the correct info when CMake runs in the editor.
> Specifically, I want seamless and automatic integration between these two
> extensions without the user having to do any additional config:
>
>    - vscode-clangd (
>    https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
>    )
>    - CMake Tools (
>    https://marketplace.visualstudio.com/items?itemName=vector-of-bool.cmake-tools
>    )
>
> I'm not familiar with CMake Tools, I guess it helps with editing
CMakeLists, CMake configs, and running configure/generate?
(Sorry, I would dig into it, about to head out on vacation)


> I see three possible approaches for this:
>
>    1. Work with the author of CMake Tools to make the extension generate
>    the appropriate info for vscode-clangd
>    2. Make vscode-clangd retrieve the info from CMake Tools somehow
>    3. Create a new extension that acts as a bridge between the two
>
> Or alternatively, some combination of the different approaches.
> I know that the CMake Tools extension integrates well with Microsoft's
> C/C++ extension, so maybe it would be a good idea to do this in a similar
> way that they did it. If my understanding is correct, a separate bridge
> extension was previously used for this, but I think they now use a
> combination of something similar to approach 1 and 2 for the extensions to
> communicate with each other.
> I would be interested to hear what you all think would be the best way to
> go about this, and maybe I can get in touch with vector-of-bools (the
> author of the CMake Tools extension) about it too.
>
> Another question I had regarding this: how does Clangd find the correct
> include paths and other info in the first place? Does it use some kind of
> compilation commands file for this? Is it possible to tell Clangd where to
> look for such a file? I couldn't really find any documentation about this
> when I looked around.
>
So if my understanding of CMake Tools is correct, these questions are very
much related :-)

Clangd works like other clang-based tools. Starting at the source file in
question, it walks up directories until it finds compile_commands.json
<https://clang.llvm.org/docs/JSONCompilationDatabase.html>. This lists the
flags required to compile each file in its scope.

So I think there are three relevant parts here:

   - this file has to be created somehow. CMake can do this, it's
   controlled by CMAKE_EXPORT_COMPILE_COMMANDS
   <https://cmake.org/cmake/help/v3.5/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html>,
   which I believe is on by default now. It writes the files into the root of
   the build tree.
   - the file has to be discovered somehow (if the CMake build tree != the
   source tree). The current best-practice is to manually create a symlink
   src/compile_commands.json -> build/compile_commands.json. I think
   symlinking src/.build to build/ is a slightly more principled approach, but
   something still has to create a symlink. (Maybe we could get CMake or CMake
   Tools to do it?). Or maybe there's some other clever way to find the build
   tree.
   - clangd has to reload the data when appropriate. Today, it caches
   forever :-( Ilya is working on fixing it!

Gotta run, hope this helps!
Patches and ideas welcome :-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/clangd-dev/attachments/20181127/7910e843/attachment.html>


More information about the clangd-dev mailing list