{"id":33777,"date":"2024-04-03T17:55:03","date_gmt":"2024-04-03T17:55:03","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=33777"},"modified":"2024-04-03T19:15:54","modified_gmt":"2024-04-03T19:15:54","slug":"how-copilot-is-being-used-by-the-time-travel-debugging-team-for-repetitive-c-coding","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/how-copilot-is-being-used-by-the-time-travel-debugging-team-for-repetitive-c-coding\/","title":{"rendered":"How Copilot is being used by the Time Travel Debugging team for repetitive C++ coding"},"content":{"rendered":"<h3 aria-level=\"2\"><span data-contrast=\"none\">Background<\/span><span data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:160,&quot;335559739&quot;:80,&quot;335559740&quot;:278}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">Ken Sykes and Juan Carlos Arevalo Baeza (JCAB) are both Principal Software Engineers who work on Time Travel Debugging at Microsoft. They are part of the team that maintains and develops the Windows Debugger (WinDbg) and related technologies. Their codebase is developed with C++ and CMake and they primarily use VS Code for day-to-day development of their code.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:278}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">They have been integrating GitHub Copilot and GitHub Copilot Chat into their C++ development in VS Code and have found many useful workflows for the AI pair programming tool. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:278}\">\u00a0<\/span><\/p>\n<hr \/>\n<p style=\"text-align: center;\"><strong>Download GitHub Copilot<\/strong><\/p>\n<p>To access GitHub Copilot and Copilot Chat, you will need an active subscription to GitHub Copilot.\u00a0If you\u2019re just getting started, please check out\u00a0<a href=\"https:\/\/code.visualstudio.com\/docs\/copilot\/getting-started-chat\" target=\"_blank\" rel=\"noopener\">the VS Code documentation<\/a>.<\/p>\n<hr \/>\n<p><i><span data-contrast=\"auto\">This blog post series has been written in partnership with Ken and JCAB to highlight their workflows with Copilot and Copilot Chat respectively and inspire other C++ developers on potential use cases to integrate with their code.<\/span><\/i><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:278}\">\u00a0<\/span><\/p>\n<hr \/>\n<h3>How we use Copilot for C++<\/h3>\n<p><span data-contrast=\"auto\">Copilot provides AI-generated autocomplete-style suggestions as we code. Copilot has greatly helped our team with reaching new levels of productivity when interacting with our large codebases.\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:278}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">For example, it has been good at following our coding styles for repetitive tasks. We highly recommend Copilot to save you some time with repetitive typing.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:278}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">This has been widely applicable to many areas of our C++ workflows, from writing test code to implementing classes. Two specific examples that have been greatly impactful in that we will expand upon below are <\/span><b><span data-contrast=\"auto\">adding additional methods to a class<\/span><\/b><span data-contrast=\"auto\"> and <\/span><b><span data-contrast=\"auto\">generating classes after migrating library dependencies.<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:278}\">\u00a0<\/span><\/p>\n<p><span class=\"TextRun SCXW152403903 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun CommentStart SCXW152403903 BCX8\">You can follow along with the examples in this sample repo<\/span><span class=\"NormalTextRun SCXW152403903 BCX8\">: <\/span><\/span><a class=\"Hyperlink SCXW152403903 BCX8\" href=\"https:\/\/github.com\/KenSykes\/ExampleLibraryConversion\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"FieldRange SCXW152403903 BCX8\"><span class=\"TextRun Underlined SCXW152403903 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW152403903 BCX8\" data-ccp-charstyle=\"Hyperlink\">KenSykes\/ExampleLibraryConversion: Demonstration of Copilot code completion (github.com)<\/span><\/span><\/span><\/a><span class=\"TextRun SCXW152403903 BCX8\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW152403903 BCX8\">. Please note that Copilot suggestions are probabilistic, not deterministic, and you may not see the exact same suggestions as you follow along.<\/span><\/span><span class=\"EOP SCXW152403903 BCX8\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:278}\">\u00a0<\/span><\/p>\n<h5><em>Adding additional methods to a class<\/em><\/h5>\n<p><span style=\"text-decoration: underline;\">Reference code<\/span>: <a href=\"https:\/\/github.com\/KenSykes\/ExampleLibraryConversion\/blob\/main\/ExampleLibraryConversion.cpp\"><i><span data-contrast=\"none\">Branch: main, file: ExampleLibraryConversion.cpp<\/span><\/i><\/a><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:278}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">One of our favorite ways to use Copilot is to have it follow existing coding patterns to save typing. For example, we have a class that interacts with <code>dbghelp.dll<\/code> through function pointers (GetProcAddress-based delay loading). Recently we needed to add some additional dbghelp methods to this class, such as <\/span><code>SymInitializeW<\/code><span data-contrast=\"auto\">. All we need to do is go to the places that define existing structs, press enter and start to type, and Copilot will dynamically update to fill in the rest. Then, we can edit down the results to get to the desired outcome.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:278}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/SymInitializev2-ezgif.com-crop-1.gif\"><img decoding=\"async\" class=\"size-full wp-image-33866 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/SymInitializev2-ezgif.com-crop-1.gif\" alt=\"a GIF representing GH Copilot automatically helping fill in the SymInitializeW method declaration after typing in decltype.\" width=\"995\" height=\"308\" \/><\/a><\/p>\n<p>After adding this new class member, we can scroll up to the constructor and notice that Copilot is able to offer a valid initialization suggestion for us.<\/p>\n<p><span style=\"color: #ff0000;\"><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/InitializeSymInitialize-ezgif.com-crop.gif\"><img decoding=\"async\" class=\"alignnone size-full wp-image-33900\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/InitializeSymInitialize-ezgif.com-crop.gif\" alt=\"Copilot automatically generated the constructor for the SymInitialize function generated.\" width=\"1675\" height=\"187\" \/><\/a><\/span><\/p>\n<p>Then, add the prototype for the wrapper and Copilot can offer a reasonable implementation.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Initializev2SymInitialize-ezgif.com-crop-1.gif\"><img decoding=\"async\" class=\"alignnone size-full wp-image-33864\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Initializev2SymInitialize-ezgif.com-crop-1.gif\" alt=\"A GIF showcasing once you type out an Initialize function in the struct, GitHub Copilot is able to fill in the necessary information below to initialize the object\" width=\"1011\" height=\"281\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>A more complex example of Copilot helping with initializations can be viewed below for now a new Load Module object.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/SymLoadModule-ezgif.com-crop.gif\"><img decoding=\"async\" class=\"size-full wp-image-33868 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/SymLoadModule-ezgif.com-crop.gif\" alt=\"A gif showcasing now declaring a new type of object called SymLoadModules and copilot assisting with the initialization\" width=\"963\" height=\"235\" \/><\/a><\/p>\n<p>In this example, Copilot can generate the implementation of the method with great default options. These default options are also commented by Copilot to document what the zeros and nullptrs mean.<a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/SymLoadModules2-ezgif.com-crop-1.gif\"><img decoding=\"async\" class=\"size-full wp-image-33870 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/SymLoadModules2-ezgif.com-crop-1.gif\" alt=\"A Gif showcasing the initialization of a function Load Module. Once the function has been initialized, Copilot filled in the implementation of the method and commented on each default option recommended\" width=\"934\" height=\"598\" \/><\/a><\/p>\n<h5><em>Generating code when migrating libraries<\/em><\/h5>\n<p><span style=\"text-decoration: underline;\">Reference code:<\/span> <a href=\"https:\/\/github.com\/KenSykes\/ExampleLibraryConversion\/blob\/convert_to_nlohmann\/ExampleLibraryConversion.cpp\"><em>Branch: convert_to_nlohmann, file:ExampleLibraryConversion.cpp<\/em><\/a><\/p>\n<p>We have found Copilot helpful with managing libraries in our repository and refactoring code. Specifically, we previously used the rapidJSON library, managed through a <code>vcpkg.json<\/code> file, to serialize our structs into JSON.<\/p>\n<p>An example of such a struct of information can be viewed here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/example-struct.png\"><img decoding=\"async\" class=\" wp-image-33871 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/example-struct.png\" alt=\"An example struct of system information such as major\/minor version, and struct of processor architecture related information\" width=\"678\" height=\"759\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/example-struct.png 901w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/example-struct-268x300.png 268w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/example-struct-768x860.png 768w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><\/a><\/p>\n<p>The original rapidJSON code was hand-written and took hours to do (including time to become familiar with the library). While producing this blog, we found that Copilot could generate much of the code correctly once some rapidJSON boilerplate code is provided. See how easily Copilot can generate the JSON object using pre-determined o and s objects for rapidJSON:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Generate-rapidJSON.gif\"><img decoding=\"async\" class=\"alignnone size-full wp-image-33873\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Generate-rapidJSON.gif\" alt=\"a GIF showcasing generating all the objects under object o and object s using the struct information above and rapidJSON library construct with Copilot.\" width=\"1530\" height=\"844\" \/><\/a><\/p>\n<p>We recently had a desire to update this library dependency from rapidJSON to nlohmann-json, since the nlohmann-json library is better-maintained. \u00a0To start updating the code, all we needed to do was update our <code>vcpkg.json<\/code> dependencies to have a nlohmann-json entry and include this new dependency in our cpp file per official library documentation.<\/p>\n<p>From there, Copilot was able to produce 80% of the code for this process with the proper formatting for nlohmann-json, letting the team focus on reviewing the generated code rather than implementing the fixes in applicable areas.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/JSONGeneratev2-ezgif.com-crop-1.gif\"><img decoding=\"async\" class=\"alignnone size-full wp-image-33894\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/JSONGeneratev2-ezgif.com-crop-1.gif\" alt=\"Image JSONGeneratev2 ezgif com crop\" width=\"1530\" height=\"664\" \/><\/a><\/p>\n<h5><em>Generating code based on input data<\/em><\/h5>\n<p><span style=\"text-decoration: underline;\">Reference code:<\/span> <a href=\"https:\/\/github.com\/KenSykes\/ExampleLibraryConversion\/blob\/convert_to_nlohmann\/ExampleParser.cpp\"><em>Branch: convert_to_nlohmann, File: ExampleParser.cpp<\/em><\/a><\/p>\n<p>Copilot can also save you time writing parsing code. Given some sample input data pasted in a comment, Copilot can generate C++ structures and parsing code for you. To demonstrate this, we started with the nlohmann JSON parser, added a conversion for <code>std::string<\/code>, and then pasted some sample JSON from openweathermap.org in a comment.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/example-JSON-data.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-33895\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/example-JSON-data.png\" alt=\"An example of JSON data from openweather.org passed into a comment in the code\" width=\"781\" height=\"1042\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/example-JSON-data.png 781w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/example-JSON-data-225x300.png 225w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/example-JSON-data-768x1025.png 768w\" sizes=\"(max-width: 781px) 100vw, 781px\" \/><\/a><\/p>\n<p>Now, watch Copilot go to work. Move cursor below the comment and hit Enter. You will start to get suggestions for code. Keep pressing Tab and Enter as Copilot writes out all the structures to represent the data and the nlohmann <code>to_json()<\/code> \/ <code>from_json()<\/code> methods, culminating in a <code>ParseWeatherData()<\/code> method that can fully parse the sample text.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/LibraryParserv2-ezgif.com-crop-1.gif\"><img decoding=\"async\" class=\"alignnone size-full wp-image-33897\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/LibraryParserv2-ezgif.com-crop-1.gif\" alt=\"An example of Copilot generating all the data for to_json and from_json functions for the library parser based on the weather data commented.\" width=\"1171\" height=\"1040\" \/><\/a><\/p>\n<p>The full Copilot-generated code can be found <a href=\"https:\/\/github.com\/KenSykes\/ExampleLibraryConversion\/blob\/convert_to_nlohmann\/ExampleParser.cpp\">here<\/a>.<\/p>\n<p>If you look closely that the Weather field of WeatherData is a single element but the JSON is an array, so a <code>std::vector&lt;&gt;<\/code> would have been a better choice. Things like this are easy to fix up on your own, or perhaps add more representative JSON samples to the comment to give Copilot more context.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/weatherdatastruct.png\"><img decoding=\"async\" class=\"size-full wp-image-33898 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/weatherdatastruct.png\" alt=\"Image weatherdatastruct\" width=\"421\" height=\"595\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/weatherdatastruct.png 421w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/weatherdatastruct-212x300.png 212w\" sizes=\"(max-width: 421px) 100vw, 421px\" \/><\/a><\/p>\n<hr \/>\n<h3><\/h3>\n<h3>Wrap up<\/h3>\n<p><strong>A big thanks to Ken and JCAB from the Time Travel Debugging Team for collaborating with us on this blog post!<\/strong><\/p>\n<p>Copilot can provide you with AI-powered assistance at many points in your C++ development, such as migrating libraries and initializing new methods. Download Copilot and try out utilizing it in your areas of repetitive coding! Please note that responses are generated by AI, so we recommend reviewing any responses provided and iterating when necessary.<\/p>\n<p>Our team is working hard on improving C++ integrations, so please let us know any improvements you\u2019d like to see to your C++ workflows. Additionally, let us know what other types of content you\u2019d like to see regarding use cases or additional information about Copilot.<\/p>\n<p>We welcome all types of feedback on your experience with the product. The comments below are open, or you can find us via email at\u00a0<a href=\"mailto:visualcpp@microsoft.com\">visualcpp@microsoft.com<\/a>\u00a0or via Twitter at\u00a0<a href=\"https:\/\/twitter.com\/visualc\" target=\"_blank\" rel=\"noopener\">@VisualC<\/a>.<\/p>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Background\u00a0 Ken Sykes and Juan Carlos Arevalo Baeza (JCAB) are both Principal Software Engineers who work on Time Travel Debugging at Microsoft. They are part of the team that maintains and develops the Windows Debugger (WinDbg) and related technologies. Their codebase is developed with C++ and CMake and they primarily use VS Code for day-to-day [&hellip;]<\/p>\n","protected":false},"author":85413,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,3940],"tags":[],"class_list":["post-33777","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","category-copilot"],"acf":[],"blog_post_summary":"<p>Background\u00a0 Ken Sykes and Juan Carlos Arevalo Baeza (JCAB) are both Principal Software Engineers who work on Time Travel Debugging at Microsoft. They are part of the team that maintains and develops the Windows Debugger (WinDbg) and related technologies. Their codebase is developed with C++ and CMake and they primarily use VS Code for day-to-day [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/33777","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/85413"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=33777"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/33777\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=33777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=33777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=33777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}