Snapshot of andreasfertig/cppinsights: 4.7k★, C++, Backend. C++ Insights - See your source code with the eyes of a compiler
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
C++ Insights - See your source code with the eyes of a compiler.
Contents
What
Why
Building
Usage
Get Involved
Support
What
C++ Insights is a Clang-based tool that does a source-to-source
transformation. The goal of C++ Insights is to make things visible that normally and intentionally happen behind the scenes.
It's about the magic the compiler does for us to make things work.
Take this piece of code, for example:
class Base {
};
class Derived : public Base {
};
int main() {
Derived d;
Derived d2 = d;
d2 = d;
Base& b = d;
}
Nothing special, and of course, it compiles. This is the compiler's view on it:
class Base
{
public:
// inline constexpr Base() noexcept = default;
// inline constexpr Base(const Base &) noexcept = default;
// inline constexpr Base & operator=(const Base &) noexcept = default;
};
class Derived : public Base
{
public:
// inline constexpr Derived() noexcept = default;
// inline constexpr Derived(const Derived &) noexcept = default;
// inline constexpr Derived & operator=(const Derived &) noexcept = default;
};
int main()
{
Derived d;
Derived d2 = Derived(d);
d2.operator=(d);
Base & b = static_cast<Base&>(d);
return 0;
}
You can see all the compiler-provided special member functions and the upcast from Derived to Base.
Why
C++ Insights is a Clang-based tool that does a source-to-source transformation.
The goal of C++ Insights is to make things visible that normally and intentionally happen behind the scenes. It's about the magic the compiler does
for us to make things work. Or looking through the classes of a compiler.
In 2017, I started looking into some new things we got with C++11, C++14, and C++17. Amazing things like lambdas, range-based for-loops,
and structured bindings. I put it together in a talk. You can find the slides
and a video online.
However, all that research and some of my training and teaching got me to start thinking about how it would be if we could see with the eyes
of the compiler. Sure, there is an AST dump, at least for Clang. We can see what code the compiler generates from a C++ source snippet with
tools like Compiler Explorer. However, what we see is assembler. Neither the AST nor the Compiler Explorer output is in the language I write
code. Hence, I'm not very familiar with this output. Plus, when teaching students C++, showing an AST and explaining that it is all there was
not quite satisfying for me.
I started to write a Clang-based tool that can transform a range-based for-loop into the compiler-internal version. Then, I did the same
for structured bindings and lambdas. In the end, I did much more than initially planned. It shows where operators are
invoked and places in which the compiler does some casting. C++ Insights can deduce the type behind auto or decltype. The goal
is to produce compilable code. However, this is not possible in all places.
You can see, for example, the transformation of a lambda, range-based for-loop, or auto. Of course, you can transform any other C++ snippet.
See yourself. C++ Insights is available online: cppinsights.io.
Still, there is work to do.
I do not claim to get all the things right. I'm also working on supporting features from new standards, like C++20, at the moment.
Please remember that C++ Insights is based on Clang and its understanding of the AST.
I did a couple of talks about C++ Insights since I released C++ Insights. For example, at C++ now. Here are the slides and the video.
Building
C++ Insights can be built inside or outside the Clang source tree.
Building on Windows
See Readme_Windows.md
Building on Arch Linux
To build with extra/clang use the following extra flags: -DINSIGHTS_USE_SYSTEM_INCLUDES=off -DCLANG_LINK_CLANG_DYLIB=on -DLLVM_LINK_LLVM_DYLIB=on
See https://github.com/andreasfertig/cppinsights/issues/186 for an explanation of why INSIGHTS_USE_SYSTEM_INCLUDES needs to be turned off.
extra/clang and extra/llvm provide /usr/lib/{libclangAST.so,libLLVM*.a,libLLVM.so}. libclangAST.so needs libLLVM.so and there would be a conflict if libLLVM*.a (instead of libLLVM.so) are linked. See https://bugs.archlinux.org/task/60512
Building outside Clang
You need to have a Clang installation in the search path.
Then, in Cevelop Import -> General -> Existing Project into Workspace. Select build_eclipse. Enjoy editing with
Cevelop.
Usage
Using C++ Insights is fairly simple:
insights <YOUR_CPP_FILE> -- -std=c++17
Things get complicated when it comes to the system-include paths. These paths are hard-coded in the binary, which seems
to come from the compiler C++ Insights was built with. To help with that, check out scripts/getinclude.py. The script tries to
collect the system-include paths from the compiler. Without an option, getinclude.py uses g++. You can also pass another compiler
as a first argument.
In case you have a custom build of the GCC compiler, for example, gcc-11.2.0, and NOT installed in the compiler in the default system path, then after building, Clang fails to find the correct libstdc++ path (GCC's STL). If you run into this situation, you can use "--gcc-toolchain=/path/GCC-1x.x.x/installed/path" to tell Clang/C++ Insights the location of the STL:
Here "${GCC_11_2_0_INSTALL_PATH}" is the installation directory of your customized-built GCC. The option for Clang is described here.
Ready to use Docker container
There is also another GitHub project that sets up a docker container with the latest C++ Insights version in it: C++
Insights - Docker
Plugins / Extensions / Packages
C++ Insights @ Vim
A plugin for Vim is available at
here.
C++ Insights @ Neovim
A plugin for Neovim is available at
here.
C++ Insights @ VSCode
An extension for Visual Studio Code is available at the VS Code marketplace: C++
Insights - VSCode Extension.
C++ Insights @ brew
At least for macOS, you can install C++ Insights via Homebrew thanks to this formular:
brew install cppinsights
Shell autocomplete
There are completion scripts for bash, zsh and fish. They autocomplete the C++ Insights options (including their descriptions) as well as the Clang options provided after --.
When building from source, the completion scripts are installed automatically into the shell-specific directories under share/ (configurable via CMAKE_INSTALL_PREFIX):
If you install C++ Insights yourself (or just want the script), you can download them individually:
cd <YOUR_BASH_COMPLETION.D>
curl -o insights https://github.com/andreasfertig/cppinsights/tree/main/scripts/bash-autocomplete.sh
Compatibility
I aim for the repository to compile with the latest version of Clang and at least the one before. The website tries to
stay close to the latest release of Clang. However, due to certain issues (building Clang for Windows), the website's
version is often delayed by a few months.
C++ Insights @ YouTube
I created a YouTube channel where I release a new video each month. In
these videos, I use C++ Insights to show and explain certain C++ constructs, and sometimes I explain C++ Insights as well.
ToDo's
See TODO.
Get Involved
Report bugs/issues by submitting a GitHub issue.
Submit contributions using pull requests. See Contributing
Support
If you like to support the project, consider submitting a patch. Another alternative is to become a GitHub Sponsor or a Patreon supporter.
How active is development on andreasfertig/cppinsights?
The most recent commit recorded on andreasfertig/cppinsights was 5 months ago, based on the GitHub push timestamp. The repository has 271 forks — one of the better signals of community interest.
How does andreasfertig/cppinsights compare to other Backend projects?
andreasfertig/cppinsights is tracked by TopGit in the Backend category, with 4.7k GitHub stars and written in C++. Browse the Backend topic page on TopGit to compare it against similar projects by stars and activity.
How many stars does andreasfertig/cppinsights have?
andreasfertig/cppinsights has 4.7k GitHub stars — refresh the page for the live number, or check github.com/andreasfertig/cppinsights. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is andreasfertig/cppinsights open source?
Yes — andreasfertig/cppinsights ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/andreasfertig/cppinsights.
What is andreasfertig/cppinsights?
andreasfertig/cppinsights (andreasfertig/cppinsights) is a C++ project on GitHub. From the project's own README: C++ Insights - See your source code with the eyes of a compiler
Where do I read more about andreasfertig/cppinsights?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/andreasfertig/cppinsights is the definitive source.
Read full README in the tab above.
Curious whether cppinsights is right for you?
Let ChatGPT, Claude, or Perplexity look into it — click below and see what AI actually says about cppinsights.