【Hacker News搬运】C++见解–用编译器的眼光查看源代码
-
Title: C++ Insights – See your source code with the eyes of a compiler
C++见解–用编译器的眼光查看源代码
Text:
Url: https://github.com/andreasfertig/cppinsights
C++ Insights 是一个基于 Clang 的工具,它进行源代码到源代码的转换。该工具的目标是使通常有意为之且发生在幕后的内容变得可见。它关注的是编译器为我们使其工作所做的魔法。例如,对于以下代码: ```cpp class Base { }; class Derived : public Base { }; int main() { Derived d; Derived d2 = d; d2 = d; Base& b = d; }
编译器看到的版本是:
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; }
可以看到编译器提供的特殊成员函数以及从派生类 Derived 到基类 Base 的向下转换。
C++ Insights 旨在生成可编译的代码。但是,并非所有地方都能实现这一点。
C++ Insights 可以从 Clang 源树内或外构建。它可以通过以下命令构建:
git clone https://github.com/andreasfertig/cppinsights.git mkdir build && cd build cmake -G"Ninja" ../cppinsights ninja
此外,C++ Insights 还提供了一些构建选项,例如:
- INSIGHTS_STRIP:构建后剥离洞察。
- INSIGHTS_STATIC:使用静态链接。
- INSIGHTS_COVERAGE:启用代码覆盖。
- INSIGHTS_USE_LIBCPP:在测试中使用 libc++。
- DEBUG:启用调试。
C++ Insights 的使用相当简单:
insights <YOUR_CPP_FILE> -- -std=c++17
但是,当涉及到系统包含路径时,情况会变得复杂。这些路径是硬编码在二进制文件中的,似乎来自 C++ Insights 构建时使用的编译器。为了解决这个问题,可以查看 scripts 目录下的 getinclude.py 脚本。该脚本尝试从编译器收集系统包含路径。如果没有选项,getinclude.py 使用 g++。你也可以传递另一个编译器作为第一个参数。
C++ Insights 还有一个 Vim 插件和 Visual Studio Code 扩展。此外,还可以通过 Homebrew 安装 C++ Insights。
C++ Insights 项目的维护者安德烈亚斯·费尔特ig(Andreas Fertig)还创建了一个 YouTube 频道,每月发布一个视频,使用 C++ Insights 解释 certain C++ constructs,有时也解释 C++ Insights 本身。
如果您发现错误/问题,可以通过提交 GitHub 问题来报告。如果您想支持该项目,可以考虑提交补丁。另一种选择是成为 GitHub 赞助商或 Patreon 支持者。
## Post by: turrini ### Comments: **corysama**: C++ Insights is available online at <a href="https://cppinsights.io/" rel="nofollow">https://cppinsights.io/</a><p>It is also available at a touch of a button within the most excellent <a href="https://godbolt.org/" rel="nofollow">https://godbolt.org/</a><p>along side the button that takes your code sample to <a href="https://quick-bench.com/" rel="nofollow">https://quick-bench.com/</a><p>Those sites and <a href="https://cppreference.com/" rel="nofollow">https://cppreference.com/</a> are what I'm using constantly while coding.<p>I recently discovered <a href="https://whitebox.systems/" rel="nofollow">https://whitebox.systems/</a> It's a local app with a $69 one-time charge. And, it only really works with "C With Classes" style functions. But, it looks promising as another productivity boost. > **corysama**: C++Insights在线提供,网址为<a href=“https://x2F;/;cppnights.io/”rel=“nofollow”>https://x2F/;cppnights.io</a> <p>只需点击最优秀的<a href=“https://;/;godbolt.org/”rel=“nofollow”>https://中的按钮即可使用/;godbolt.org/</a> <p>沿着将代码样本带到<a href=“https://;/;quick bench.com/”rel=“nofollow”>https://的按钮/;quick bench</a> <p>这些网站和<a href=“https://;/;cppreference.com/”rel=“nofollow”>https:///;cppreference.com/</a> 是我所做的;我在编码时不断地使用<p> 我最近发现<a href=“https://;#x2F;whitebox.systems#x2F”rel=“nofollow”>https:///;whitebox.systems</a> 它;这是一款一次性收费69美元的本地应用程序。而且,它只适用于“;C带类”;样式函数。但是,作为另一次生产力提升,它看起来很有希望。 **johnea**: I wonder how helpful it is for g++?<p>Myself and my ancient h/w and s/w engineering friends have been having a discussion recently about how the modern C++ language features are generating code that is not readily debugged at the source level.<p>The compiler has always created assembly that implements the feature described by teh high level language, but histoirically these have had a relatively one-to-one relationship with the source code.<p>However in the world of post C++11 many more abstracted features are supported, and they no longer have any kind of relation to the source in terms of something you could inspect with a debugger (except in assembly).<p>So it seems a tool like this is really useful, but unfortunately almost inherently compiler specific.<p>Maybe GNU can port this to g++? > **johnea**: 我想知道它对g++有多大帮助<p> 我自己和我古老的h/;w和s;w工程界的朋友最近一直在讨论现代C++语言功能是如何生成在源代码级别不容易调试的代码的<p> 编译器总是创建实现高级语言描述的功能的程序集,但从历史上看,这些程序集与源代码具有相对一一对应的关系<p> 然而,在后C++11的世界中,支持更多的抽象功能,并且它们不再与源代码有任何关系,因为您可以使用调试器进行检查(在程序集中除外)<p> 因此,像这样的工具似乎真的很有用,但不幸的是,它几乎本质上是特定于编译器的<p> 也许GNU可以将其移植到g++? **ColonelPhantom**: Honestly, from the title, I expected something that gave actual 'insights' into the compiler, rather than the code. For example, indicating applied and potential (missed) optimizations. But seeing what it was, I was definitely not unpleasantly surprised :)<p>Being able to cut through abstraction is very nice and quite important when you want to understand WHAT you are writing from the machine's perspective (or even in general when you don't know the abstraction yet). I love using Haskell, but have no idea what kind of machine code GHC spits out at the end (something based on the spineless tagless G-machine, an abstraction I barely understand by itself). > **ColonelPhantom**: 老实说,从标题来看,我期待着一些能给你带来真正的;见解;进入编译器,而不是代码。例如,指示应用的和潜在的(遗漏的)优化。但看到它是什么,我绝对没有感到不愉快的惊讶:)<p>当你想了解你在机器上写什么时,能够突破抽象是非常好和非常重要的;s的观点(甚至在一般情况下,当您还不知道抽象时)。我喜欢使用Haskell,但不知道GHC在最后会吐出什么样的机器代码(基于没有骨气的无标签G机器的东西,这是一个我自己几乎无法理解的抽象)。 **Linda231**: [dead] > **Linda231**: 死去的