开源软件名称:andreasfertig/cppinsights开源软件地址:https://github.com/andreasfertig/cppinsights开源编程语言:C++ 89.5%开源软件介绍:C++ Insights - See your source code with the eyes of a compiler.ContentsWhatC++ Insights is a clang-based tool which does a source to source transformation. Its goal is it to make things visible which 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;
Base& b = d;
} Nothing special and of course it compiles. This is the compilers view on it: class Base {
/* public: inline constexpr Base() noexcept; */
/* public: inline ~Base(); */
/* public: inline constexpr Base(const Base &); */
/* public: inline constexpr Base(Base &&); */
};
class Derived : public Base {
/* public: inline constexpr Derived() noexcept; */
/* public: inline constexpr Derived(const Derived &); */
/* public: inline constexpr Derived(Derived &&); */
};
int main(){
Derived d;
Base& b = static_cast<Base&>(d);
} You can see all the compiler provided functions. Also the downcast from I do not claim to get all the things right. This is just the initial version of insights I consider good enough to hand it to the public. Keep also in mind that is solely based on clang and its understanding of the AST. WhyC++ Insights is a clang-based tool which does a source to source transformation. Its goal is to make things visible which 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. Some time ago 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 start thinking how it would be, if we could see with the eyes of the compiler. Sure, there is an AST-dump at least for clang. With tools like Compiler Explorer we can see what code the compiler generates from a C++ source snippet. However, what we see is assembler. Neither the AST nor the Compiler Explorer output is in the language I write code and therefore I'm most familiar with. 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 able to 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 ended up with doing a lot more as initially planned. It shows where operators are
invoked, places in which the compiler does some casting. C++ Insights is able to deduce the type behind Still, there is work to do. I do not claim to get all the things right. This is just the initial version of C++ Insights I consider good enough to hand it to the public. Also, keep in mind that it is solely based on clang and my understanding of C++ and the AST. You can see, for example the transformation of a lamda, range-based for-loop or auto. Of course, you can transform any other C++ snippet. See yourself, C++ Insights is available online: cppinsights.io. BuildingC++ Insights can be build inside the clang-source tree or outside. Building on WindowsBuilding on Arch LinuxTo build with See #186 for an explanation of why
Building outside clangYou need to have a clang installation in the search path.
The resulting binary (insights) can be found in the build-folder. Building inside clangFor building it inside the clang-source tree, assuming you have your source-tree already prepared:
Then build clang as you normally do. cmake optionsThere are a couple of options which can be enable with cmake:
CevelopUse it with
Then in Cevelop Import -> General -> Existing Project into Workspace. Select UsageUsing C++ Insights is fairly simple:
Things get complicated when it comes to the system include paths. There are path hard-coded in the binary which seem
to come from the compiler C++ Insights was build with. To help with that check out scripts/getinclude.py. It tries to
collect the system include paths from the compiler. Without an option it uses Here is an example:
The script can be used together with C++ Insights:
There is also another GitHub project which sets up a docker container with the latest C++ Insights version in it: C++ Insights - Docker An extension for Visual Studio Code is available at the VS Code marketplace: C++ Insights - VSCode Extension. CompatibilityCurrently, the most recent stable version of clang is supported as well as the current development branch. ToDo'sSee TODO. Get Involved
SupportIf you like to support the project consider submitting a patch. Another alternative is to become a Patreon supporter. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论