New Feature Request: Show Clang AST when code in C/C++
vscode-clangd
supports Show AST
for selected code.
This enables user to have a straightforward sight in compiler's view. It's especially useful when the code includes token injection by preprocessor(e.g. #define
s and #include
s) or by static reflection in C++26.
Example of this feature:
Suppose we have two files: enum.defs
for enumeration definition and enum_like_class.cxx
for type defination.
// enum.defs
#ifndef ENUM_VALUE
#warning "You may forget to define `ENUM_VALUE(enum_name, ...)` before include this file"
#else
ENUM_VALUE(Cat, 0)
ENUM_VALUE(Dog, 3)
#endif
// enum_like_class.cxx
#include <cstdint>
#include <stdexcept>
#include <string_view>
#include <print>
// These macros may be in other file.
#define trivial_class class [[clang::trivial_abi]]
#define may_throw(...)
#define ctor
#define dtor
trivial_class enum_like_class
{
private:
using base_type = std::uint8_t;
base_type m_value{};
public:
#define ENUM_VALUE(name, ...) static constexpr base_type name = (__VA_ARGS__);
#include "enum.defs" // What happens here?
#undef ENUM_VALUE
#define ENUM_VALUE(name, ...) [static_cast<base_type>(__VA_ARGS__)] = #name,
static constexpr std::string_view enum_names[] = { // NOLINT
#include "enum.defs" // What happens here?
};
#undef ENUM_VALUE
public:
constexpr ctor enum_like_class(enum_like_class const&) noexcept = default;
constexpr ctor enum_like_class(enum_like_class&&) noexcept = default;
constexpr auto operator=(enum_like_class const&) noexcept -> enum_like_class& = default;
constexpr auto operator=(enum_like_class&&) noexcept -> enum_like_class& = default;
constexpr dtor ~enum_like_class() = default;
constexpr explicit
ctor enum_like_class(base_type value)
may_throw(std::out_of_range)
: m_value{value}
{
constexpr std::size_t size = std::size(enum_names);
if (m_value >= size || enum_names[m_value].empty()) {
m_value = {};
throw std::out_of_range("enum_like_class::enum_like_class");
}
}
constexpr explicit
operator base_type()
const noexcept
{
return this->to_base();
}
[[nodiscard]] constexpr
auto to_base()
const noexcept
-> base_type
{
return m_value;
}
[[nodiscard]] constexpr
auto to_string_view()
const noexcept
-> std::string_view
{
return enum_names[m_value];
}
};
auto main()
-> int
{
enum_like_class e{enum_like_class::Cat};
std::println("e: {}", e.to_string_view());
return 0;
}
Inside the editor, we can see the definition of each enumeration and other things directly.

Why not use “Show Assembly”?
Because the structure of the source code will be hard to identify. When the code involves templates, some, or most, of code are not mapped to any assembly.
Issue of solutions like “C++ Insights”:
It tries to transform the source code to something “equivalent” and does give some help in some cases here.
But not all features in C++ have a trivial and strict equivalent form. And It's too heavy and slow for projects with more than exactly one small file. This should be a feature orthogonal to Show AST
.
Is it easy to implement?
May be true for projects using clang. Currently, the CLion is using clangd
in some scenarios. Adding this feature officially may be not so hard.
请先登录再写评论。