C++ Insights - See your code through a compiler's eyes

Uses LLVM/Clang, Ninja and LLD to build.


C++ Insights - See your code through a compiler's eyes


C++ Insights is a tool based on clang that transforms your source code into the source code seen by the compiler.

The aim is to make things visible, which normally and intentionally happen behind the scenes. It’s about the magic the compiler does to make things work. Or looking through a compiler’s classes.

For example:

Suppose you have this code:

#include <iostream>

int main(){
  std::string hello {"Hello, World!"};
  std::cout << hello << '\n';
  return EXIT_SUCCESS;
}

In fact this code looks like this by your compiler:

#include <iostream>

int main()
{
  std::string hello = std::basic_string<char>{"Hello, World!", std::allocator<char>()};
  std::operator<<(std::operator<<(std::cout, hello), '\n');
  return 0;
}


Installation and Use

C++ Insights is available online, but if you want to build and install it on your system, there are some details. You will need the following construction tools:

  • CMake
  • LLVM/clang++, it only compiles with it.
  • Ninja
  • lld , this is the most important of all, C++ Insights reports parameters to the linker that GNU ld is not supported. 😞

Example in Ubuntu:

sudo apt install clang++ lld ninja-build cmake

Then just clone and build with the following command:

git clone https://github.com/andreasfertig/cppinsights.git
mkdir build && cd build
cmake -G"Ninja" ../cppinsights
ninja

If you can’t, try this:

Note that the linker and the compiler are informed via parameter to CMake, if you want to reinforce it, run the commands before cmake: export CXX=$(which clang++) and for the linker: env LD=$ (which lld) .

git clone https://github.com/andreasfertig/cppinsights
cppinsights cd
mkdir build && cd build

cmake -G"Ninja" -DCMAKE_LINKER=$(which lld) -DCMAKE_CXX_COMPILER=$(which clang++) -DCMAKE_PREFIX_PATH=$(which clang++ | sed 's/\/clang++//g') ..
ninja

You can still try with make: cmake -G "Unix Makefiles" .. plus the other parameters above.

I thought about compiling with these “closed loop” tools… Anyway, good luck compiling, you’ll need it!

If you managed to compile and install it, the use is very simple, for example to analyze a code:

insights <YOUR_CPP_FILE> -- -std=c++17

If you were NOT able to compile and install, relax! You can use it online at:

https://cppinsights.io/

I’m sure it’s a lot easier! 😃

For more information, also visit GitHub:

https://github.com/andreasfertig/cppinsights


cpp cppdaily


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles