Clangd

From llvm-mos
Revision as of 00:45, 25 September 2022 by Atn34 (talk | contribs) (Update clang + vscode instructions to match what worked for me)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The SDK includes a build of the clangd language server. This allows semantic features of code editors and IDEs that support the Language Server Protocol to work with the SDK.

The specifics of setting up clangd depend on one's choice of editor and build system. There are three general rules, though:

  1. Make sure that your build system is generating a compile_commands.json file. Look for this in your build or output directory.
  2. Make sure that the editor is pointed at the clangd binary included with the SDK, not a system one. The clang included with the SDK supports language extensions that aren't in upstream clang, which can confuse upstream clangd.
  3. Set your editor so that the argument --query-driver=/path/to/llvm-mos-sdk/bin/*clang* is passed to clangd. This authorizes clangd to query the SDK clang about what path a specific SDK target uses. Without this, clangd won't have any idea where to look for target headers.

CMake + Visual Studio Code[edit | edit source]

To set up clangd when building using CMake and editing with Visual Studio Code:

  1. Make sure that you've installed the Clangd extension in the Visual Studio Code marketplace. You should also disable the default C++ extension, since it conflicts with clangd.
  2. Pass -DCMAKE_EXPORT_COMPILE_COMMANDS=ON to CMake when generating the build files. This ensures that a build produces a compile_commands.json file. Note that this only works with the Makefile and Ninja generators.
  3. Build the project.
  4. In Visual Studio Code, open your project, then go to the Settings page. Click the "Workspace" tab and search for "Clangd."
  5. In the setting titled "Clangd: Arguments", add the argument --query-driver=/path/to/llvm-mos-sdk/bin/*clang*, substituting the absolute path to your SDK installation for /path/to/llvm-mos-sdk.
  6. Set "Clangd: Path" to /path/to/llvm-mos-sdk/bin/clangd, again substituting your install path.
  7. Issue the command "clangd: Restart language server". Clangd integration should now work; you can test this by clicking "Go To Definition" on a #includeline. This should take you to the corresponding header in your SDK install.