Remix.run Logo
▲ C++ Insights – See your source code with the eyes of a Compiler(github.com)
107 points by rramadass 2 days ago | 17 comments
▲jsrcout 3 hours ago | parent | next [-]

Definitely trying this out. I've always had the problem of "but what is the compiler thinking?" with C++ in particular.

▲alankarmisra 4 hours ago | parent | prev | next [-]

Ooo I love these things. I wrote a C++ -> Clang -> JS transpiler so I could show value change states on top of the C++ source code in html and allow people to step through the whole state change. They can also vary the input and see the values change cause the JS layer could recompute the values - which is why I bothered with the transpiling - otherwise I could have just output the state changes and parsed them as CSV for the steps. Fun project!

https://youtu.be/ZPGysUX62OY

▲StilesCrisis 5 hours ago | parent | prev | next [-]

I wish the readme example showed a lambda capture. Now that's where compiler magic REALLY shows up.

▲rramadass 3 hours ago | parent | next [-]

You can see it by typing in the sample code in the tool here - https://news.ycombinator.com/item?id=49928362

▲mrlonglong 3 hours ago | parent | prev [-]

I second this.

▲rramadass 3 hours ago | parent [-]

https://news.ycombinator.com/item?id=49945197

▲tweedler290 an hour ago | parent | prev | next [-]

thats crazy. i made this https://github.com/oooscoos/Benzi which does the same thing but is language agnostic (supports 9 other languages). urs probs goes in deeper into c++ than mine tho

▲abbefaria27 8 minutes ago | parent [-]

This is very different and doesn’t relate to what the linked repo does.

▲f1shy 3 hours ago | parent | prev | next [-]

I must be the only one who (while I like the tool) feel a little strange, somewhere between sad, angry, annoyed and disappointed (can’t really express it), that a language needs (or at least is of good use) such a tool.

Like: I wish what is the first interpretation of my code to be obvious. Is not about advanced optimization, or nitty gritty compiler tricks, is about the language. This tool is useful in any C++ compiler. How did we end with such a complex language?

I mean (sorry, very personal take) the programming language is for me a tool to help me think about a problem and its solution. In this era of LLM more than before, is not a tool to get the machine to do something, is a language for formalizing ideas, problems and solutions, like math. Should not be so obscure, that you need to be a lawyer to understand it, right? Or I am very much old and totally in the weeds with my ideas?

▲kccqzy 2 hours ago | parent | next [-]

No a language doesn’t need it. It’s an educational tool for those new to the language or new to a particular language feature. With practice language features just become automatic so you don’t think about them when thinking about a problem.

In the README example, the C++ practitioner just doesn’t think about those automatically synthesized copy constructors and such for POD types, nor do they think about the fact that assignment is desugared to operator= or that an assignment can involve a static_cast.

▲f1shy 2 hours ago | parent [-]

>> needs (or at least is of good use) such a tool.

Of course there are people who do not need that tool. The thing is, other languages doesn’t need such an explanation tool at all, is very much enough with reading a small book. I personally would like C++ to not really need it, but clearly as I stated: maybe “the only one”

▲esikich 2 hours ago | parent | prev | next [-]

In what language is compiling simple? Do you not like compiled languages? I'm not sure what you're complaining about here.

▲f1shy 2 hours ago | parent [-]

Lisp is simple to compile. Even C (at the level this tool works anyway), also basic, and maybe other 100 languages (again, not the whole compiler, but what this tool shows).

Compilation per se is not difficult. Doing it with good error recovery, good feedback in case of error, and the optimization is difficult.

This tool does not tell me those parts (for that is gotbolt) this is about how the first layer of interpretation works, and not for making compilers, but for understanding what the code actually means, because is not obvious at first sight.

▲rramadass 21 minutes ago | parent | prev | next [-]

You have misunderstood the language and the tool.

Compilers for all languages have the concept of "lowering" the level of source-language abstractions from high-level to low-level viz. Written Source -> Simpler Source -> AST -> IR -> Machine Code. One type is "desugaring" i.e. transforming complex syntax to simpler ones so that the following stages of compilation have to operate only on a smaller and hence more manageable language subset.

This tool shows both desugaring and other lowering (eg. implicit transformations, type deductions etc.) within the C++ source language by regenerating the source from the AST thus giving you full visibility until that stage in the compilation process.

PS: Explanatory video presentation by the author - https://www.youtube.com/watch?v=VJ6ZvDRYzNE&t=558s

▲rs545837 2 hours ago | parent | prev [-]

I don't think it's only C++ though, Python decorators, Rust macros, almost every language's compiler write code you never see. C++ just does more of it and makes it harder to guess. Being able to look at the expanded version is useful in any language that does this.

▲rramadass 2 days ago | parent | prev | next [-]

The tool can be found at - https://cppinsights.io/

▲IshKebab 3 hours ago | parent | prev [-]

This is what let me understand lambdas. Great tool.