{"id":31106,"date":"2022-09-20T15:00:33","date_gmt":"2022-09-20T15:00:33","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=31106"},"modified":"2022-09-20T12:21:56","modified_gmt":"2022-09-20T12:21:56","slug":"microsoft-cpp-code-analysis-warnings-with-key-events","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/microsoft-cpp-code-analysis-warnings-with-key-events\/","title":{"rendered":"Microsoft C++ Code Analysis Warnings with Key Events"},"content":{"rendered":"<h1 aria-level=\"1\"><span data-contrast=\"none\">Introduction<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">To make your C++ coding experience as safe as possible, the Microsoft C++ Code Analysis has added new checks and improved existing ones to help you prevent bugs before they find their way into your products. Some of the checks work harder than others, analyzing the code deeper by simulating runtime behavior. As such, they can find defects that are harder to find through syntactic checks alone or through data flow analysis.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">It is natural that developers find it harder to find the root causes for some of these more complex warnings. Imagine a warning where the effect of the bug is detected tens or hundreds of lines below the root cause of the bug, and the code against which the defect is reported looks completely legitimate. How would you be able to quickly find the root cause and fix it?<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">In this blog post, we would like to reintroduce an existing Visual Studio feature that has been there to help with this challenge, and to highlight the big improvements we have made to the feature which will make it easier and more compelling to use.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<h1 aria-level=\"1\"><span data-contrast=\"none\">Challenges with Code Analysis Warning<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">Let\u2019s review the usual challenges we have when a static code analysis check tells us there is a defect in our code. It can be best demonstrated with some example code with a known defect. The following example is intentionally made convoluted enough to demonstrate the challenge of finding the root cause for warnings from a path-sensitive check.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\"> 1\u00a0\u00a0 #include &lt;Windows.h&gt;\u00a0\r\n\u00a02\u00a0\u00a0\u00a0\r\n\u00a03\u00a0\u00a0 int test();\u00a0\r\n\u00a04\u00a0\u00a0\u00a0\r\n\u00a05\u00a0\u00a0 void t1()\u00a0\r\n\u00a06\u00a0\u00a0 {\u00a0\r\n\u00a07\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 VARIANT v1;\u00a0\r\n\u00a08\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 VARIANT v2;\u00a0\r\n\u00a09\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 VARIANT v3{};\u00a0\r\n10\u00a0\u00a0\u00a0\r\n11\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 VARIANT* pv = &amp;v3;\u00a0\r\n12\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 VARIANT* pvTmp = nullptr;\u00a0\r\n13\u00a0\u00a0\u00a0\r\n14\u00a0\u00a0\u00a0\u00a0\u00a0 VariantInit(&amp;v1);\u00a0\r\n15\u00a0\u00a0\u00a0\r\n16\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (test() != 0)\u00a0\r\n17\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pvTmp = &amp;v1;\u00a0\r\n18\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else\u00a0\r\n19\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pvTmp = &amp;v2;\u00a0\r\n20\u00a0\u00a0\u00a0\u00a0\r\n21\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 switch (test())\u00a0\r\n22\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\u00a0\r\n23\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 1:\u00a0\r\n24\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case 2:\u00a0\r\n25\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pv = pvTmp;\u00a0\r\n26\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\u00a0\r\n27\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 default:\u00a0\r\n28\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pv = &amp;v3;\u00a0\r\n29\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\u00a0\r\n30\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0\r\n31\u00a0\u00a0\u00a0\r\n32\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 VariantClear(pv); \/\/ Warning C33001 here\u00a0\r\n33\u00a0\u00a0 }<\/pre>\n<p><span data-contrast=\"auto\">Running code analysis on this code will give a few warnings. One of them will be this:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<pre class=\"prettyprint\">test.cpp(32) : warning C33001: VARIANT 'pv' was cleared when it was uninitialized (expression 'pv').: Lines: 5, 7, 8, 9, 11, 12, 14, 16, 19, 21, 25, 26, 32<\/pre>\n<p><span data-contrast=\"auto\">In summary, clearing a <code>VARIANT<\/code> object that was not properly initialized can result in many random problems, including crashes, memory errors, etc. More information about this specific check can be found at <\/span><a href=\"https:\/\/docs.microsoft.com\/bs-latn-ba\/cpp\/code-quality\/c33001?view=msvc-160\"><span data-contrast=\"none\">c33001 | Microsoft Docs<\/span><\/a><span data-contrast=\"auto\">. But for this discussion, let\u2019s focus on the root cause of the defect \u2013 why does the check say we are clearing a <code>VARIANT<\/code> object that is not initialized?<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">The line of code to which the warning points us is completely legitimate: we must clear the <code>VARIANT<\/code> object before we are done with it. Then what is the problem? It must be that somewhere along the code flow that reaches line 32, the <code>VARIANT<\/code> object that <code>pv<\/code> points to has not been initialized.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Let\u2019s try to find out where the root cause is. First, we will need to find out which <code>VARIANT<\/code> object <code>pv<\/code> points to. Then follow the code flow to see why that object has never been initialized. To figure those out, we will need to trace back the code flow from where the defect is reported. Because it is likely dependent on the path that the check analyzed, we will use the line numbers provided in the warning. That seems doable. However, that doesn\u2019t seem to be the most efficient way. Imagine a similar bug is in an excessively big function. A similar warning may come with tens or hundreds of lines of code to trace back. Many of us would feel offended by such a warning, rather than helped.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">This blog post is not about finding root cause by tracking back source code manually like this. So, before going any further along this path, let\u2019s see if there is any help available.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<h1 aria-level=\"1\"><span data-contrast=\"none\">Code Analysis Warning with Key Events<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">It may not be well known, but selected warnings from some of MSVC&#8217;s C++ static analysis checks that have been there for long time always had additional information that helps with tracing back your code to identify the root causes. This additional information is called \u201cKey Events.\u201d A Key Event is associated with a step in the analysis code flow. It explains what has happened in that specific step. During analysis, these checks add Key Event candidates to the steps where important or interesting events occur, e.g., when a new buffer is created, etc. As a check finds a defect, it collects all relevant Key Events along the code flow that led to the defect and adds them to the warning. Some common events are also added as Key Events &#8211; e.g., when a relevant variable is initialized or updated, when code flow branches, etc.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Here is an example of a warning with Key Events from one of those older checks as they are presented in the Error List. You could get these by clicking on the expansion triangle icon in the left-most column of the warning:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/C6001_KeyEvents.png\"><img decoding=\"async\" class=\"wp-image-31110 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/C6001_KeyEvents.png\" alt=\"Image of C6001 warning with Key Events\" width=\"841\" height=\"213\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/C6001_KeyEvents.png 1157w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/C6001_KeyEvents-300x76.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/C6001_KeyEvents-1024x259.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/C6001_KeyEvents-768x194.png 768w\" sizes=\"(max-width: 841px) 100vw, 841px\" \/><\/a><\/p>\n<p><span data-contrast=\"auto\">As you can see, these Key Events add rich information to the warning, making it very easy to follow the code and understand what is going on along the code flow of the analysis.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<h1 aria-level=\"1\"><span data-contrast=\"none\">Improvements to Key Events<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">We believe in the value of Key Events for our customers. So, we decided to remove the hinderances in making it available for more checks. We also made it a lot easier to use.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">To solve the first problem, we added equivalent support to our newer engine, allowing many newer checks to add Key Events to their warnings with little effort. This was done earlier, and new checks have been adding Key Events to their warnings. As of the release of Visual Studio 2022 version 17.4 Preview 2 we have augmented many checks with Key Events, including warnings for:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"auto\">Enum misuse as buffer index: C33010 and C33011<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span data-contrast=\"auto\">VARIANT object misuse: C33001 ~ C33005<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><span data-contrast=\"auto\">Concurrency problems: C26100 ~ C26167<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<\/ul>\n<p><span data-contrast=\"auto\">We will add Key Events to even more warnings in the upcoming Visual Studio releases.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559685&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<h1 aria-level=\"2\"><span data-contrast=\"none\">Improved Key Event presentation in Visual Studio<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:40,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">Visual Studio 2022 version 17.4 Preview 2 comes with big improvements in presentation of the Key Events.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Key Events used to be presented inside the Error List window as shown earlier. Usually, real estate of Error List is very limited to present a warning with a large number of Key Events. Also, it was hard to interact with the Key Events in the list, due to missing features and bugs. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">First, we redesigned the way Visual Studio presents the Key Events for MSVC C++ static analysis warnings. Key Events will now be presented outside of Error List window, in their own window as follows:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p aria-level=\"2\"><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Visual-Studio-built-in-Key-Events-window-2.png\"><img decoding=\"async\" class=\"size-full wp-image-31116 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Visual-Studio-built-in-Key-Events-window-2.png\" alt=\"Image Visual Studio built in Key Events window 2\" width=\"1682\" height=\"1227\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Visual-Studio-built-in-Key-Events-window-2.png 1682w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Visual-Studio-built-in-Key-Events-window-2-300x219.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Visual-Studio-built-in-Key-Events-window-2-1024x747.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Visual-Studio-built-in-Key-Events-window-2-768x560.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Visual-Studio-built-in-Key-Events-window-2-1536x1120.png 1536w\" sizes=\"(max-width: 1682px) 100vw, 1682px\" \/><\/a><\/p>\n<p><span style=\"font-size: 1rem;\" data-contrast=\"auto\">You can open this window by selecting the warning with double clicking or pressing enter key, or clicking the small window icon on the left-most column of the warning entry in the Error List. This window is like any other tabbed window &#8211; it can be docked, floated, resized, or added to the tabbed windows. Selecting a Key Event in this window highlights the corresponding source line. Keyboard navigation among the Key Events is improved in this window, and source code highlighting follows the selected Key Event.<\/span><span style=\"font-size: 1rem;\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:257}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">This will make it a lot easier to review the Key Events and navigate through them along with the corresponding source code, often greatly reducing the time to find the root cause of the defect. For the example code and the warning, now it is a lot easier to tell <code>pv<\/code> is pointing to <code>pvTmp<\/code>, and <code>pvTmp<\/code> is pointing to <code>v2<\/code>, which was never initialized in the code flow.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:257}\">\u00a0<\/span><\/p>\n<h1 aria-level=\"2\"><span data-contrast=\"none\">Presentation of Key Events through SARIF Viewer Extension<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:40,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">We have teamed up with the developers of the SARIF Viewer Extension for Visual Studio. Some of you may have tried its older versions before. If you haven\u2019t tried it before, now is a good time to get the latest release and give it a try, as we believe it will greatly enhance your productivity with the improvements we made to the extension.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<h2 aria-level=\"3\"><span data-contrast=\"none\">Visualization of Key Events in SARIF Explorer window<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:40,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">Through close collaboration among related teams, we greatly improved the functionality of the SARIF Viewer Extension for Visual Studio. When installed and enabled this extension can now display Key Events in its SARIF Explorer window without requiring you to find and open the SARIF file. You just need to select the warning that has the small window icon on the left-most column by <span style=\"font-size: 1rem;\" data-contrast=\"auto\">double clicking or pressing the enter key, or clicking the small window icon<\/span>. Visual Studio will let SARIF Viewer Extension display the Key Events, instead of its own pop-up window:<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\"><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-SARIF-Explorer-window-of-SARIF-Viewere-Extension-for-Visual-Studio.png\"><img decoding=\"async\" class=\"size-full wp-image-31117 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-SARIF-Explorer-window-of-SARIF-Viewere-Extension-for-Visual-Studio.png\" alt=\"Image Key Events presentation in SARIF Explorer window of SARIF Viewere Extension for Visual Studio\" width=\"2447\" height=\"1381\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-SARIF-Explorer-window-of-SARIF-Viewere-Extension-for-Visual-Studio.png 2447w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-SARIF-Explorer-window-of-SARIF-Viewere-Extension-for-Visual-Studio-300x169.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-SARIF-Explorer-window-of-SARIF-Viewere-Extension-for-Visual-Studio-1024x578.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-SARIF-Explorer-window-of-SARIF-Viewere-Extension-for-Visual-Studio-768x433.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-SARIF-Explorer-window-of-SARIF-Viewere-Extension-for-Visual-Studio-1536x867.png 1536w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-SARIF-Explorer-window-of-SARIF-Viewere-Extension-for-Visual-Studio-2048x1156.png 2048w\" sizes=\"(max-width: 2447px) 100vw, 2447px\" \/><\/a><\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">If you select a Key Event in the SARIF Explorer window, the corresponding source code in the Text Editor window will be highlighted with slightly darker color. If you select source code that is highlighted in the Text Editor window, the corresponding Key Event will be highlighted in the SARIF Explorer window.<\/span><\/p>\n<h2 aria-level=\"3\"><span data-contrast=\"none\">Source code adornment with Key Events\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">There is even greater news. The SARIF Viewer Extension for VS now displays Key Events right next to the source code they belong to. This will make it a lot easier to find the root cause by allowing review of the source code along with detailed explanation on what the check discovered or assumed at each of the steps of the analysis path that leads to the defect. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Here is an example:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\"> <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-Text-Editor-window-by-SARIF-Viewere-Extension-for-Visual-Studio.png\"><img decoding=\"async\" class=\"size-full wp-image-31118 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-Text-Editor-window-by-SARIF-Viewere-Extension-for-Visual-Studio.png\" alt=\"Image Key Events presentation in Text Editor window by SARIF Viewere Extension for Visual Studio\" width=\"2441\" height=\"1380\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-Text-Editor-window-by-SARIF-Viewere-Extension-for-Visual-Studio.png 2441w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-Text-Editor-window-by-SARIF-Viewere-Extension-for-Visual-Studio-300x170.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-Text-Editor-window-by-SARIF-Viewere-Extension-for-Visual-Studio-1024x579.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-Text-Editor-window-by-SARIF-Viewere-Extension-for-Visual-Studio-768x434.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-Text-Editor-window-by-SARIF-Viewere-Extension-for-Visual-Studio-1536x868.png 1536w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Key-Events-presentation-in-Text-Editor-window-by-SARIF-Viewere-Extension-for-Visual-Studio-2048x1158.png 2048w\" sizes=\"(max-width: 2441px) 100vw, 2441px\" \/><\/a><\/span><\/p>\n<p>&nbsp;<\/p>\n<h1 aria-level=\"1\"><span data-contrast=\"none\">Setting up environment<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">To get the essential help from Key Events, you don\u2019t need to do anything. Visual Studio comes with full features to present and let you interact with Key Events. Just install or upgrade your Visual Studio to VS 2022 17.4 Preview 2 or later. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:257}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">To enjoy the new Key Events feature to its full extent in your day-to-day work, also install or upgrade your SARIF Viewer Extension to the latest version. Go to \u201cExtensions &gt; Manage Extensions,\u201d and search for \u201cMicrosoft SARIF Viewer 2022\u201d. As of this writing, version 3.0.98.16906 is available. Any version newer than that should work:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:257}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:257}\"><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Microsoft-SARIF-Viewer-Extension-in-Manage-Extensions-dialog.png\"><img decoding=\"async\" class=\"wp-image-31119 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Microsoft-SARIF-Viewer-Extension-in-Manage-Extensions-dialog.png\" alt=\"Image Microsoft SARIF Viewer Extension in Manage Extensions dialog\" width=\"952\" height=\"255\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Microsoft-SARIF-Viewer-Extension-in-Manage-Extensions-dialog.png 1373w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Microsoft-SARIF-Viewer-Extension-in-Manage-Extensions-dialog-300x80.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Microsoft-SARIF-Viewer-Extension-in-Manage-Extensions-dialog-1024x274.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/Microsoft-SARIF-Viewer-Extension-in-Manage-Extensions-dialog-768x206.png 768w\" sizes=\"(max-width: 952px) 100vw, 952px\" \/><\/a>\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">To enable or disable \u201ctext adornment\u201d with Key Events, please go to \u201cTools &gt; Options\u2026 &gt; SARIF Viewer\u201d and select\/unselect \u201cEnable Key Event text adornments in the editor\u201d option. It is enabled by default:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:257}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:257}\"><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/SARIF-Viewer-Key-Event-Text-Adornment-option-in-Options-Dialog.png\"><img decoding=\"async\" class=\"wp-image-31120 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/SARIF-Viewer-Key-Event-Text-Adornment-option-in-Options-Dialog.png\" alt=\"Image SARIF Viewer Key Event Text Adornment option in Options Dialog\" width=\"920\" height=\"479\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/SARIF-Viewer-Key-Event-Text-Adornment-option-in-Options-Dialog.png 1473w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/SARIF-Viewer-Key-Event-Text-Adornment-option-in-Options-Dialog-300x156.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/SARIF-Viewer-Key-Event-Text-Adornment-option-in-Options-Dialog-1024x533.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2022\/09\/SARIF-Viewer-Key-Event-Text-Adornment-option-in-Options-Dialog-768x400.png 768w\" sizes=\"(max-width: 920px) 100vw, 920px\" \/><\/a>\u00a0<\/span><\/p>\n<h1 aria-level=\"1\"><span data-contrast=\"none\">Conclusion<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">This is not the final destination for the Key Events feature. Instead, it is only a new starting point for greater improvements. For example, we are working on making the Key Events more relevant to the defect, e.g., by eliminating older assignments that do not contribute to the defect. We are also looking into better navigation support among relevant Key Events. As some of you may already be hoping, we\u2019d love to bring the SARIF Viewer Extension experience into Visual Studio, without requiring installation of the extension.<\/span><\/p>\n<h1 aria-level=\"1\"><span data-contrast=\"none\">Send us your feedback<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">We are very much interested in your feedback to continue to improve this experience. The comments below are open. Feedback can also be shared through the <\/span><a href=\"https:\/\/developercommunity.visualstudio.com\/spaces\/8\/index.html\"><span data-contrast=\"none\">Developer Community<\/span><\/a><span data-contrast=\"auto\">. You can also reach us on Twitter (<\/span><a href=\"https:\/\/twitter.com\/visualc\"><span data-contrast=\"none\">@VisualC<\/span><\/a><span data-contrast=\"auto\">), or via email at <\/span><a href=\"mailto:visualcpp@microsoft.com\"><span data-contrast=\"none\">visualcpp@microsoft.com<\/span><\/a><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction\u00a0 To make your C++ coding experience as safe as possible, the Microsoft C++ Code Analysis has added new checks and improved existing ones to help you prevent bugs before they find their way into your products. Some of the checks work harder than others, analyzing the code deeper by simulating runtime behavior. As such, [&hellip;]<\/p>\n","protected":false},"author":39446,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,230],"tags":[119,3926,163],"class_list":["post-31106","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","category-new-feature","tag-code-analysis","tag-key-events","tag-static-analysis"],"acf":[],"blog_post_summary":"<p>Introduction\u00a0 To make your C++ coding experience as safe as possible, the Microsoft C++ Code Analysis has added new checks and improved existing ones to help you prevent bugs before they find their way into your products. Some of the checks work harder than others, analyzing the code deeper by simulating runtime behavior. As such, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/31106","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\/39446"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=31106"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/31106\/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=31106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=31106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=31106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}