7 ms·
C++ Insights – See your source code with the eyes of a compiler
- johnea 3y agoI wonder how helpful it is for g++? 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. 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. 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). So it seems a tool like this is really useful, but unfortunately almost inherently compiler specific. Maybe GNU can port this to g++?
- ColonelPhantom 3y agoThe readme indicates that it uses system headers (G++ libstdc++) by default. Since it's source-to-source, I don't think being based on clang actually matters for anything unless you happen to use GCC-specific extensions. I would also expect clang and gcc to behave similary when cutting through abstraction. I'm not sure, but it also seems like the output code should be able to compile? Then you can probably use g++ for that step.
- khuey 3y ago> 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. What specific features do you have in mind? I generally believe this is because DWARF and debuggers haven't been keeping up, not because of inherent limitations.
- meindnoch 3y agoWhat do you mean exactly? Like the inability of putting a breakpoint on the loop condition of a range-based for loop?
- pjmlp 3y agoPretty easy in VC++ debugger.
- pjmlp 3y agoEven C doesn't map any longer when taking into consideration -O3, UB, vector units, and compiler replaced standard library calls. I miss the part of what a modern debug lacks.
- jstimpfle 3y agoDon't debug -O3, or -O2 for that matter. Debug a separate debug build without optimizations, or don't heavily optimize your release build if you want to debug that (you know that obviously). Even with optimizations disabled, debugging C++ can be quite painful. Only add a little template stuff to the source code, and a few "zero-cost" methods like indexing operators or cast operators, and it quickly becomes painful to step through code. There should be a way to avoid stepping through boilerplate methods, but apart from specifying string patterns to exclude methods by name -- not inline with the method but in a separate debugging configuration (which is very very annoying). The only solution I know is to use C++ features very lightly. Any other practical options? Check this link and the twitter posts linked there, other well known developers have this issue: https://developercommunity.visualstudio.com/t/std::move-and-similar-functions-resu/1681875?q=%5BFixed+In%3A+Visual+Studio+2022+version+17.5+Preview+2%5D https://developercommunity.visualstudio.com/t/std::move-and-...
- pjmlp 3y agoUse modern IDEs with features like Just My Code, which already help a lot. https://learn.microsoft.com/en-us/visualstudio/debugger/just-my-code?view=vs-2022#BKMK_C___Just_My_Code https://learn.microsoft.com/en-us/visualstudio/debugger/just... Also, there are scenarios where debugging without optimizations isn't an option. The issue with std::move has been solved for a while and was a VC++ bug, not C++. https://devblogs.microsoft.com/visualstudio/a-year-of-cpp-improvements-in-visual-studio/ https://devblogs.microsoft.com/visualstudio/a-year-of-cpp-im...
- jstimpfle 3y agoCan Just My Code help if it is precisely my code that I want to skip over? I believe it does not -- IIUC it works based on modules i.e. the DLL that contains the machine code. IOW it doesn't even help if I move the "zero-cost" stuff to a separate source file -- templates and other stuff that have to be instanciated or inlined won't be separable from the code that I want to debug based on the module they're running in. > Also, there are scenarios where debugging without optimizations isn't an option. There are scenarios where debugging isn't an option in the first place. Doesn't mean that I should have to suffer in the typical case where debugging with no or only light optimizations is perfectly viable. An acceptable solution I guess would be to have a [DebuggerHidden] function attribute as I found for C#. Didn't find one for C++.
- nurettin 3y agoSounds like a huge undertaking, but why not use clang for introspection and gcc for compiling? It is still useful.
- rymiel 3y agogcc lacks usable tooling, in the past it was a conscious decision to not allow any other program to "steal" the work of gcc. Every single tool for c++ is built around clang, because clang is actually built to allow other tools to use it
- ColonelPhantom 3y agoHonestly, 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 :) 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).
- jaredwy 3y agocompiler explorer/clang can do this. https://clang.llvm.org/docs/UsersManual.html#options-to-emit-optimization-reports https://clang.llvm.org/docs/UsersManual.html#options-to-emit... Gcc also has similar options
- ColonelPhantom 3y agoOoh nice, I completely forgot that that existed. When you say the compiler explorer can do it, do you mean that it will 'inline' the output in a similar way to the assembly i.e. you can view the results 'within' the source code?
- linhns 3y agoI think Haskell creators never meant to have users care about those abstractions. It's more about what, not how, and given its terseness and power, I'd be happy to be a normal user.
- ReleaseCandidat 3y ago> I love using Haskell, but have no idea what kind of machine code GHC spits out at the end There is GHC's core, an indermediate representation of your code (mostly "just" desugared Haskell), which is comparable to Andreas' C++ Insights. Which you can get by passing `-ddump-simpl` with additional flags to disable displaying everything. A list of other internal GHC data to dump is https://downloads.haskell.org/~ghc/7.8.4/docs/html/users_guide/options-debugging.html https://downloads.haskell.org/~ghc/7.8.4/docs/html/users_gui... But getting an idea what assembly is finally generated is still hard (at least for mé). Or, to rephrase, to know if "everything" is finally unpacked and strict or not, and if not: why not. Btw. IMHO still the best book to read (of course not up-to-date, but to get the basic concepts of GHC) is https://www.microsoft.com/en-us/research/publication/the-implementation-of-functional-programming-languages-2/ https://www.microsoft.com/en-us/research/publication/the-imp... https://www.microsoft.com/en-us/research/uploads/prod/1987/01/slpj-book-1987-r90.pdf https://www.microsoft.com/en-us/research/uploads/prod/1987/0...
- corysama 3y agoC++ Insights is available online at https://cppinsights.io/ https://cppinsights.io/ It is also available at a touch of a button within the most excellent https://godbolt.org/ https://godbolt.org/ along side the button that takes your code sample to https://quick-bench.com/ https://quick-bench.com/ Those sites and https://cppreference.com/ https://cppreference.com/ are what I'm using constantly while coding. I recently discovered https://whitebox.systems/ https://whitebox.systems/ 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.
- donadigo 3y agoI'll add to that my own Visual Studio extension that improves runtime debugging by showing you which lines are actively executing and what changes happened line-by-line: https://d-0.dev https://d-0.dev It integrates with the VS debugger completely and does not require any changes to one's codebase. It's also available on the marketplace if you want to try it out: https://marketplace.visualstudio.com/items?itemName=donadigo.d0 https://marketplace.visualstudio.com/items?itemName=donadigo...
- azmr 3y agoHey, thanks for the mention of WhiteBox! I'm the lead dev on it. The currently-available version is focused on a "write-based" workflow for C and a subset of C++: JIT-compiling, running and auto-debugging the function you're working on in the background whenever you edit it. It shows a timeline of how the data in your function changes, with the intent being to give you feedback as immediately as possible. We're currently working on a "read-focused" workflow, which is basically about making WhiteBox also work as a timeline debugger (for EXEs compiled from any language) with a few extra tricks up its sleeve... Feel free to ask any questions either here or on our discord: https://chat.whitebox.systems https://chat.whitebox.systems
- Linda231 3y ago[dead]
- DanielBen6 3y ago[flagged]
- yukIttEft 3y agoI wish there was a way to show the code that is generated when using co_return or co_await e.g. https://cppinsights.io/lnk?code=Ly8gaHR0cHM6Ly93d3cuc2NzLnN0YW5mb3JkLmVkdS9+ZG0vYmxvZy9jKystY29yb3V0aW5lcy5odG1sCgojaW5jbHVkZSA8Y29uY2VwdHM+CiNpbmNsdWRlIDxjb3JvdXRpbmU+CiNpbmNsdWRlIDxleGNlcHRpb24+CiNpbmNsdWRlIDxpb3N0cmVhbT4KCnN0cnVjdCBSZXR1cm5PYmplY3QgewogIHN0cnVjdCBwcm9taXNlX3R5cGUgewogICAgUmV0dXJuT2JqZWN0IGdldF9yZXR1cm5fb2JqZWN0KCkgeyByZXR1cm4ge307IH0KICAgIHN0ZDo6c3VzcGVuZF9uZXZlciBpbml0aWFsX3N1c3BlbmQoKSB7IHJldHVybiB7fTsgfQogICAgc3RkOjpzdXNwZW5kX25ldmVyIGZpbmFsX3N1c3BlbmQoKSBub2V4Y2VwdCB7IHJldHVybiB7fTsgfQogICAgdm9pZCB1bmhhbmRsZWRfZXhjZXB0aW9uKCkge30KICB9Owp9OwoKc3RydWN0IEF3YWl0ZXIgewogIHN0ZDo6Y29yb3V0aW5lX2hhbmRsZTw+ICpocF87CiAgY29uc3RleHByIGJvb2wgYXdhaXRfcmVhZHkoKSBjb25zdCBub2V4Y2VwdCB7IHJldHVybiBmYWxzZTsgfQogIHZvaWQgYXdhaXRfc3VzcGVuZChzdGQ6OmNvcm91dGluZV9oYW5kbGU8PiBoKSB7ICpocF8gPSBoOyB9CiAgY29uc3RleHByIHZvaWQgYXdhaXRfcmVzdW1lKCkgY29uc3Qgbm9leGNlcHQge30KfTsKClJldHVybk9iamVjdApjb3VudGVyKHN0ZDo6Y29yb3V0aW5lX2hhbmRsZTw+ICpjb250aW51YXRpb25fb3V0KQp7CiAgQXdhaXRlciBhe2NvbnRpbnVhdGlvbl9vdXR9OwogIGZvciAodW5zaWduZWQgaSA9IDA7OyArK2kpIHsKICAgIGNvX2F3YWl0IGE7CiAgICBzdGQ6OmNvdXQgPDwgImNvdW50ZXI6ICIgPDwgaSA8PCBzdGQ6OmVuZGw7CiAgfQp9Cgp2b2lkCm1haW4xKCkKewogIHN0ZDo6Y29yb3V0aW5lX2hhbmRsZTw+IGg7CiAgY291bnRlcigmaCk7CiAgZm9yIChpbnQgaSA9IDA7IGkgPCAzOyArK2kpIHsKICAgIHN0ZDo6Y291dCA8PCAiSW4gbWFpbjEgZnVuY3Rpb25cbiI7CiAgICBoKCk7CiAgfQogIGguZGVzdHJveSgpOwp9Cg==&insightsOptions=cpp20&std=cpp20&rev=1.0 https://cppinsights.io/lnk?code=Ly8gaHR0cHM6Ly93d3cuc2NzLnN0...
- andreasfertig 3y agoSometimes, wishes come true! Just open the drop-down to select the C++ standard. Below, there is a section "More Transformations". Enable "Show coroutine transformation", and you will get the transformation you're looking for: https://cppinsights.io/s/4d8c3fc1 https://cppinsights.io/s/4d8c3fc1 Disclaimer: I'm the author.
- yukIttEft 3y agoThis is very helpful, thx.
- nurettin 3y agoIt looks like a perfect candidate for a vs code extension.
- dailykoder 3y agoI use neovim btw
- andreasfertig 3y agoThanks to another contributor there is a vim extensions: https://github.com/Freed-Wu/cppinsights.vim https://github.com/Freed-Wu/cppinsights.vim
- gpderetta 3y agoNow do Emacs. 2 minutes od searching didn't hit anything :)
- andreasfertig 3y agoSorry, I don't know about an Emacs plugin. All the plugins/extensions I'm aware of are listed in the Readme.md: https://github.com/andreasfertig/cppinsights/#c-insights--vim https://github.com/andreasfertig/cppinsights/#c-insights--vi... I'm happy to add an entry for Emacs once somebody develops a plugin for that editor.
- andreasfertig 3y agoThanks to a contributor there is such an extension: https://marketplace.visualstudio.com/items?itemName=devtbi.vscode-cppinsights https://marketplace.visualstudio.com/items?itemName=devtbi.v...
- scrubs 3y agoGreat git Readme! Practical and helpful. I'm def interested. Thanks!