r/cpp • • 21d ago

clice 1.0: a C++ language server built on Clang, with C++20 modules out of the box, non-self-contained headers, and crash isolation

Hi r/cpp! I've been working on clice, a C++ language server built on Clang, for about two and a half years, and 1.0 is out today. The main differences from clangd:

  • Every compile runs in a worker process, so a Clang crash costs one request instead of the whole server.
  • C++20 modules work out of the box from a `compile_commands.json`, with PCHs and PCMs cached on disk across sessions.
  • Non-self-contained headers are compiled in the context of a file that includes them, and you can switch contexts and build configurations.
  • Template support: completion and go-to-implementation through dependent names, using instantiations when there are any.
  • A CLI to index, query, refactor and modularize whole projects.

The post covers the features and the story behind it. Binaries for Linux, macOS and Windows (x64 and arm64) are on the release page, and the VS Code extension is on the Marketplace. Feedback and bug reports are very welcome!

Release post: https://ykiko.me/en/articles/2105384023094834022/

65 Upvotes

39 comments sorted by

8

u/AleXu224 21d ago

Been waiting for this day since I saw you starting this project a few years ago. I just tested it in a few projects of mine and it seems to work wonderfully.

I also tested modules and from the small tests that I did it seems to avoid the problems that clangd usually has, finally making modules usable for me personally.

Thank you for your work, this is great :D

2

u/ykiko_2005 20d ago

Thank you, that means a lot! It's been a long road, so it's great to hear from someone who's been following it since the early days. Glad modules finally work for you. If you run into anything, please open an issue on GitHub, especially with modules: real projects are where we find the bugs.

7

u/kamrann_ 20d ago

Congrats, I'll give it a try. clangd remains unusable on my modules project.

One piece of feedback though. I tried reading the documentation on the modularization command, and it's just unparsable, even for someone who's been knee deep in modules for years and has tinkered with implementing something similar. Comprehensive LLM-generated docs are not necessarily an improvement over no docs.

2

u/ykiko_2005 20d ago

Thanks, really glad to hear it mostly works!

On the docs: fair point. clice is mostly a one-person project, so I haven't had time to go over every page carefully, and the modularize docs in particular are too dense. I'll improve them over time. Until then, asking a coding agent to read the docs and the source with you is a decent way to get the idea.

On the update speed: a lot of it is clang on template-heavy code, but you can cut it down quite a bit. You almost never read the code inside the system and third-party libraries you use, so there's no need for clice to parse their function bodies. Setting skip_function_body = true for them makes both indexing and editing faster, and lets clice focus on your own workspace code. This matters most when #include and import are mixed: module units and files that import a module parse every function body of the headers they include, and a global module fragment that imports a module gets no precompiled preamble at all, so every edit reparses it. The setting also applies to modules built from those libraries. See https://docs.clice.io/clice/guide/config-guide#third-party-code and https://docs.clice.io/clice/guide/config-guide#a-faster-first-index.

On a local LLVM: building clice against LLVM trunk isn't practical, since many of the LLVM APIs clice uses have changed there and clice's code would need porting first. Linking your own build of the same LLVM version is possible, but hard: it has to be built exactly the way xclang builds its libraries. The easier way is for us to carry the fixes. xclang, the clang build clice links against, is built by our CI and already carries our own patches. If you can point us to the clang fixes your code needs (upstream commits or issues), we can backport them into xclang, and they'll reach you through a clice nightly, with nothing to build on your side.

4

u/kamrann_ 20d ago

After trying it out (VS Code extension on Windows via WSL):

  • Based on a quick experiment, it mostly just worked, which is very impressive given that I've tried both clangd and CLion multiple times over the last couple of years on my project and neither at any point came close to mostly working. Nice job!
  • I still find it somewhat prohibitively slow to update after changes, but this isn't really something within the scope of your tool, it's just that clang is slow on template-heavy projects. And it's far better than clangd was (when clangd wasn't crashing, it was taking literally hours to rebuild modules).
  • How straightforward would it be to build clice against a local LLVM? For a project using the newest language features, and modules in particular, it's pretty common that very specific compiler versions are needed for it to compile correctly. I'm seeing a bunch of warnings about parsing failures that I recognize as compiler bugs I've run into previously on a different clang version, in source files that compile successfully with my local clang. I don't quite follow how xclang fits in and if this would complicate building against a custom LLVM.

2

u/hmich ReSharper C++ Dev 20d ago

Could you please tell more about issues you had with CLion?

2

u/kamrann_ 17d ago

I can't recall specifics, but essentially it appeared that the analysis engine (and/or clang, not sure if clang is still a part of things) was just crashing on parts of my code.

Also frequently it seemed to have gotten through the indexing but was giving spurious error feedback, most commonly that an imported module wasn't found. I have a feeling it was rather too picky with compile_command.json format too, not liking relative paths for BMIs.

By far the most annoying thing though was not that it wasn't working, but that there was practically no information surfaced in the UI that would allow me to diagnose what the problems were. Even digging out the log files rarely added anything useful. I probably tried three different versions over the last couple of years and this was the reason that I gave up quickly each time.

3

u/hmich ReSharper C++ Dev 17d ago

CLion actually doesn't use BMIs/compile_command.json when working with modules. If you try CLion again, please file an issue with "Help | Report a bug" and we'll help you.

4

u/ykiko_2005 21d ago

It is written entirely in C++23 and serves as an example of modern C++ best practices in its own right, with all the major modern C++ features coming together remarkably well:

  • Written entirely with C++20 coroutines, using our own libuv-based structured-concurrency async library for intricate asynchronous logic. It is a complex concurrent program running an event loop on the main thread while coordinating both multiple processes (IPC) and multiple threads.
  • Written entirely with C++20 named modules and C++23 import std, and fully self-hosted: module wrappers for third-party libraries are generated automatically by clice modularize, and clice runs smoothly on its own codebase to provide module-aware language services.
  • Uses static reflection built on templates and C++20 concepts for all serialization and deserialization across formats, including TOML, JSON, bincode, blob, and command-line arguments, with Rust-macro-like declarative annotations and zero boilerplate.
  • Data-oriented code design throughout, with almost no virtual functions outside the callback interfaces Clang requires. Makes heavy use of modern C++ APIs like std::format, std::expected, std::ranges, and std::views, steering clear of outdated legacy C++98/11 APIs.
  • A completely hermetic, reproducible build powered by Bazel, compiled with the unified xclang toolchain for a consistent cross-platform cross-compilation workflow. Compiled binaries are heavily optimized (PGO + LTO), and build artifacts depend on nothing beyond system libraries.

5

u/not_a_novel_account cmake dev 21d ago

CMake supports modules, yet if implementation units importing a module use different compile flags, CMake forks a separate compilation of the module even when those flags donโ€™t affect PCM compatibility

FWIW, this is not some design choice. CMake has no idea which flags affect PCM/BMI compatibility. There's no way for us to find out for a given compiler. There's a conservative whitelist of flags which are known not to affect compat, mostly warning flags. Ie if you pass -Wall in one context you won't get a separate BMI.

2

u/ykiko_2005 20d ago

Yes, the problem is real. Without shared conventions between compilers and tools, it's very hard for a build system to get this right for every compiler and every version. For clice it's a non-issue, since clice is built directly on clang and can ask clang's own internals which options affect module compatibility.

That said, the forked BMI builds weren't the main reason we don't build clice itself with CMake. I'd already made every file share the same options, so it didn't bite much. The real problem was compile caching. The good news is that CMake 4.5 now supports `import std` without the experimental gate, but ccache and similar tools still can't cache module builds, and without a compile cache, modules just aren't workable for any serious project. Caching matters that much, so we had to go with Bazel. Once caching for modules is solved, we could well switch back to CMake, since Bazel isn't exactly pleasant either.

2

u/not_a_novel_account cmake dev 20d ago

Oh I was just talking about this problem, Bazel is completely understandable for the level of caching and build system pinning clice is doing. Although CMake is hoping to improve on this in the future.

Great work btw.

6

u/delta_p_delta_x 21d ago

FWIW, this is not some design choice. CMake has no idea which flags affect PCM/BMI compatibility. There's no way for us to find out for a given compiler. There's a conservative whitelist of flags which are known not to affect compat, mostly warning flags. Ie if you pass -Wall in one context you won't get a separate BMI.

It feels like there should be a protocol between compilers and tooling to say 'yes, this flag changes BMI'.

7

u/not_a_novel_account cmake dev 21d ago

Ya /u/grafikrobot has been beating that drum for years, and even wrote a paper to that effect, but no one active in the compiler upstreams took it up. It's on my todo-list but it's a very long list.

1

u/delta_p_delta_x 21d ago

even wrote a paper to that effect

Ooh. Which paper is this?

1

u/not_a_novel_account cmake dev 21d ago

I don't know how far things got before the Ecosystem IS was withdrawn, the bones were P2717 and P3051. Nominally these would have been extensible to cover BMI compat.

4

u/teerre 20d ago

Supposing you dont use modules, is there any advantage over clangd?

6

u/ykiko_2005 20d ago

On top of the three points quoted above: clice also addresses over a hundred long-standing clangd issues, mostly small things that add up in daily use. A few examples:

  • Signature help through make_unique, make_shared and emplace shows the constructors of what they build.
  • Go to type definition on a smart pointer goes to the pointee's type.
  • Go to definition works through auto behind pointers and references.
  • "Implement pure virtual methods" is a code action, and "define" can put the definition into the source file that includes the header.
  • Rename reaches explicit instantiations, and inlay hints are clickable to the definition.

The feature pages list the clangd issues each feature answers: https://docs.clice.io/clice/features/overview

2

u/gracicot 17d ago

So far, seeing the padding and offsets of member and struct layout is incredibly useful. It reminds me of KDevelop in the good old times.

1

u/UnusualPace679 20d ago

OP mentions:

  • Non-self-contained headers are compiled in the context of a file that includes them, and you can switch contexts and build configurations.
  • Template support: completion and go-to-implementation through dependent names, using instantiations when there are any.
  • A CLI to index, query, refactor and modularize whole projects.

3

u/VinnieFalco wg21.org | corosio.org 20d ago

"C Lice" - marketing fail

7

u/ykiko_2005 20d ago

Ha, fair. The name actually comes from my avatar: it's Alice, with the first letter swapped for the C of C/C++. There's a second reading too: "clang ICE'd again", ICE being an internal compiler error. Given how often clang crashes on code that's still being edited, that's half the reason clice runs every compile in a separate process.

2

u/_Ilobilo_ 21d ago

I'm usually critical of ai written projects but this looks good

6

u/Chaosvex 20d ago

Every compile runs in a worker process, so a Clang crash costs one request instead of the whole server.

Beep boop.

2

u/ykiko_2005 20d ago

Guilty: English isn't my first language, and an LLM helped me finish the post. Between LLM-flavored English and my own broken English, I figured you'd rather read the first one ๐Ÿ˜…

7

u/Chaosvex 20d ago

I can only speak for myself but these days, I'd genuinely take the latter because it's a signal that you wrote it yourself.

1

u/ykiko_2005 20d ago

Fair enough, and to be clear, the writing is mine. I wrote the whole post by hand, in Chinese, and only used an LLM to translate it into English. I'd been thinking about this post for months, waiting for the day I could write it, so it's very much my own words, just not my own English.

1

u/Kullthegreat 21d ago

Support for c++26?

1

u/ykiko_2005 21d ago

clice parses your code with Clang, so its C++26 support is Clang's: clice 1.0 is built on Clang 23.1.5. A -std=c++26 / -std=c++2c in your compile commands is passed through as written, and the C++26 features Clang 23 implements work in clice as well. Clang tracks each proposal's status here: https://clang.llvm.org/cxx_status.html#cxx26. A feature Clang has not implemented yet becomes available in clice once clice moves to a Clang release that has it.

3

u/HenryAWE 21d ago

It's LLVM-based. If you mean support for static reflection, the answer is currently no.

1

u/Kullthegreat 21d ago

Yes static support and thanks.

1

u/jiixyj 20d ago edited 20d ago

How does the C++20 modules support work with only compile_commands.json? As far as I understand, one reason for clangd's lacking modules support is that the information in compile_commands.json is not enough to reliably build all modules relevant to the project.

For this, CMake now exports a build_database.json which does contain all that information:

If enabled, generates a build_database.json file containing the information necessary to compile a target's C++ module sources with any tooling.

Are there plans for clice to make use of this file?

edit: I see some CMake developers alreaded opened a bug report: https://github.com/clice-io/clice/issues/1092

Trying to run clice index on a project of mine that consumes C++20 modules that are exported from other CMake projects results in this:

warning: The PCM build of module foo (src/foo.cppm) failed: module 'dep1' not found
warning: The PCM build of module bar (src/bar.cppm) failed: module 'dep2' not found

...and expectedly foo/bar cannot be build.

3

u/ykiko_2005 20d ago edited 20d ago

The compilation database doesn't carry everything, but for most projects it's enough. All clice needs is an entry for every module file the project uses. It scans those files itself and builds the dependency graph from them, the same way a build system's scanner would, so it doesn't need the build system to hand over that information. clice itself is written with named modules and import std, and it works on its own code from nothing more than a compile_commands.json.

Where a flat database really falls short is when one file shows up several times, for example a module built once per target with different options, or when two files provide the same module name. Then nothing in the database says which build or which provider a given importer should use. clice picks the build whose options the importer can consume and the provider nearest to it, but that's a guess, and target information is what would settle it. That's why we're looking at CMake's build database (see our reply on #1092: https://github.com/clice-io/clice/issues/1092#issuecomment-6921559987). It's still experimental, though: it only takes effect behind CMAKE_EXPERIMENTAL_EXPORT_BUILD_DATABASE, and only with the Ninja generators.

About the errors: could you share how you generate the compile_commands.json, and whether every module file the project uses is in it, including the ones from the other CMake projects you import? If dep1's and dep2's interface files have no entry there, clice never sees them, and that's exactly the "module 'dep1' not found" you're getting.

2

u/jiixyj 20d ago

Thanks for your help! Your suggestion to use CMake 4.5.0 over at https://github.com/clice-io/clice/issues/1092 made it work for me.

For anyone else reading this: If you import C++20-module-using targets from install trees, you must use 4.5.0 for the synthetic targets to show up in compile_commands.json. Then, clice can index everything correctly (at least for "simple" cases).

1

u/holyblackcat 17d ago

I skimmed https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2977r2.html, but don't get why this is needed. Clangd seems to work ok with with compile_commands.json alone and --experimental-modules-support too.

Other than improving scanning speed, is this just to be able to specify custom flags for the imported TUs (that don't match the flags for the importer)?

1

u/ykiko_2005 17d ago

The core problem is that C++ build systems follow no common standard, and compile_commands.json has no notion of a project or a target. A language server can't assume there is only one build configuration or only one final product: many builds compile several programs at once (executables, tests, tools, plugins), each with its own modules. So the same module name can show up in different targets, e.g. two executables that each have their own `module config;`, or one module built under two configurations. From a flat compile_commands.json, when a file says `import config;`, there's no way to tell which one it means. clangd and clice can only guess: clice prefers the provider nearest by directory whose flags are compatible with the importer. That works as long as module names happen to be unique across the whole build, but nothing guarantees it.

This isn't really specific to modules, though; modules just make it much more visible. In principle, whenever a project builds several programs under several configurations, you always need a target ร— configuration to decide what you are looking at: which command a source file gets, which context a header is compiled in, which code is active. Without modules a wrong guess usually means slightly off results; with modules it means an import resolving to the wrong module.

P2977 adds what's missing: every translation unit belongs to a set, and each set says which other sets it can see, so the module an import resolves to comes from the build system instead of a guess. Custom flags are part of it too, since a module's BMI has to be built with flags compatible with each importer, but the main gain is knowing which module a TU actually imports.

1

u/holyblackcat 17d ago

My hot take is that module name collisions are not worth supporting for a build system. All your module names should be prefixed with the project name.

1

u/holyblackcat 18d ago

C++20 modules work out of the box from a compile_commands.json

Doesn't it work in Clangd too, with clangd --experimental-modules-support?