clang-highlight reads C/C++ code and outputs semantic tokens that can be used for syntax highlighting and interactive links. It is able to understand your code perfectly since it uses clang to lex the input and generate an AST.
This tool is meant for static use cases (e.g. code documentation) rather than interactive use.
PyPI packaging is work in progress. Stay tuned. You can install locally using
uv
/uvx
.
clang-highlight operates like a clang tool (e.g. clang-tidy). It needs to be
told where your build directory with a compilation database
(compile_commands.json
) can be found.
Use
clang-highlight -p path/to/build path/to/code.cpp > out.html
to directly generate highlighted & linked HTML using m.css styling.
- Pygments/doxygen/...: These general syntax highlighting solutions can only perform very basic lexing and no semantic understanding. Thus, linking to types or even a correct function overload is impossible.
- synth: While close in approach, synth uses libclang instead of the direct Clang C++ API. This comes with several limitations, the foremost one that libclang does not report template instantiations, making semantic annotations of templated code nearly impossible.
- clangd: While providing a semantic token interface, clangd actually lacks understanding of template instantiations. Furthermore, semantic tokens only cover a small portion of total tokens, which have to be lexed & generated by the client itself.
clang -Xclang -ast-dump=json
: Clang's AST dump does not include preprocessor information (and contains much less information than the C++ API provides).