{"id":5553,"date":"2007-08-03T16:28:00","date_gmt":"2007-08-03T16:28:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2007\/08\/03\/testing-compiler-diagnostics-and-a-cool-ide-tip\/"},"modified":"2019-02-18T18:54:24","modified_gmt":"2019-02-18T18:54:24","slug":"testing-compiler-diagnostics-and-a-cool-ide-tip","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/testing-compiler-diagnostics-and-a-cool-ide-tip\/","title":{"rendered":"Testing Compiler Diagnostics&#8230;and a Cool IDE Tip!"},"content":{"rendered":"<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Hello everyone, this is Jamie Eckman.<span>&nbsp; <\/span>The last time I posted I was an SDET (tester) on the Libraries team.<span>&nbsp; <\/span>I&rsquo;ve since moved to the Compiler team, still as an SDET.<span>&nbsp; <\/span>One thing we see a lot of on both teams is compiler diagnostics: warnings and errors.<\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">One of our Libraries developers, Stephan T. Lavavej, wrote this last year on one of our internal discussion lists: <span class=\"QuoteChar\"><em>Figuring out the mapping from &ldquo;what the compiler is complaining about&rdquo; to &ldquo;what&rsquo;s actually wrong with my code, if anything&rdquo; is like 75% of learning how to program. <\/em><\/span><span class=\"QuoteChar\"><span>Depending on how you learned to program, 75% may or may not be an overstatement, but the point is clear.<span>&nbsp; <\/span>Everyone writes bad code sooner or later, and in the case of new programmers it&rsquo;s sooner (and often).<span>&nbsp; <\/span>For this reason it&rsquo;s important that the errors the compiler emits are correct and clear enough to diagnose the problem.<span>&nbsp; <\/span>Clarity is something we&rsquo;re making a significant investment into improving over the next few releases of Visual Studio.<span>&nbsp; <\/span>For now I&rsquo;ll mostly stick to talking about correctness.<\/span><\/span><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">When I was still on the Libraries team the only direct contact with diagnostics I had was some maintenance of the ATL Server attributes error tests.<span>&nbsp; <\/span>But for the most part I looked at compiler errors from the end-user perspective:<span>&nbsp; <\/span>This test I wrote doesn&rsquo;t compile, why?<span>&nbsp; <\/span>Now that I&rsquo;m on the Compiler team, which owns the bulk of the error text you&rsquo;re likely to see while writing C or C++ code, I&rsquo;ve had a great opportunity to look at up close how our diagnostics work and how we test them.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">The compiler has on the order of 1,000 errors and warnings it can emit.<span>&nbsp; <\/span>We test these like you might expect, each warning or error has one or more test cases associated with it.<span>&nbsp; <\/span>The test cases verify that the diagnostic that&rsquo;s expected is actually emitted and is emitted on the correct line.<span>&nbsp; <\/span>Of course, the compiler is a hugely complex mechanism and problems are bound to slip through the cracks.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">A colleague on our UE team (User Education, responsible for the content on MSDN, among other things) recently came to me with a small list of code snippets from the <\/font><a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/8x5x43k7(VS.80).aspx\"><font face=\"Calibri\" size=\"3\">C\/C++ Build Errors<\/font><\/a><font face=\"Calibri\" size=\"3\"> section of MSDN that weren&rsquo;t producing the promised errors.<span>&nbsp; <\/span>These snippets are pretty useful when you&rsquo;re trying to figure out what a particular error or warning means.<span>&nbsp; <\/span>Oftentimes the simplified code in the snippet can help you spot the problem in your own code.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">So, Tim from the UE team asked me to look at these snippets that weren&rsquo;t working and figure out what was wrong. <span>&nbsp;<\/span>A compiler bug, an expected change we forgot to tell UE about, or something else?<span>&nbsp; <\/span>As an example, here&rsquo;s the first one I looked at:<\/font><\/p>\n<p class=\"MsoNormal\"><span>\/\/ C2062_b.cpp<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>\/\/ compile with: \/c<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>class<\/span><span> DataSent {};<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>struct<\/span><span> MyDataController {<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp; <\/span>MyDataController(<span>int<\/span> age, DataSet* j) {}<span>&nbsp;&nbsp; <\/span><span>\/\/ C2062<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp; <\/span><span>\/\/ try the following line instead<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp; <\/span><span>\/\/ MyDataController(int age, DataSent* j) {}<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>};<\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">This is a simple error, although perhaps not easy to spot.<span>&nbsp; <\/span>The class name &ldquo;DataSent&rdquo; has been mistyped &ldquo;DataSet&rdquo;.<span>&nbsp; <\/span>The expected error is C2062, &ldquo;type &lsquo;DataSet&rsquo; unexpected&rdquo;.<span>&nbsp; <\/span>The error that actually gets emitted (with a recent Orcas compiler) is C2061, &ldquo;syntax error : identifier &lsquo;DataSet&rsquo;&rdquo;.<span>&nbsp; <\/span>Here we see that correctness and clarity often have an interesting relationship when it comes to error messages.<span>&nbsp; <\/span>Although we didn&rsquo;t get the error we expected, the error we did get nonetheless points us directly at the problem.<span>&nbsp; <\/span>I might be able to make the case that this is a compiler bug, but because the new error is functionally just as good as the old error, it&rsquo;s unlikely to be fixed at this stage in the Orcas release cycle.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Now, here&rsquo;s a tip that has abso\nlutely nothing to do with the rest of this post.<span>&nbsp; <\/span>Some of you may already know this, but I haven&rsquo;t seen it posted on vcblog yet.<span>&nbsp; <\/span>I recently started working in a new codebase with a pretty strict set of coding conventions.<span>&nbsp; <\/span>One of the conventions is that nothing should go past column 79.<span>&nbsp; <\/span>It&rsquo;s pretty annoying to watch the column display in the status bar every time you get close to the end of a line, but fortunately there&rsquo;s a nifty hidden feature of Visual Studio that can help. <span>&nbsp;<\/span>Column guides.<span>&nbsp; <\/span>This involves editing the registry, so the standard disclaimer applies: Modify the registry at your own risk!<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Find this key in your registry:<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">HKEY_CURRENT_USERSoftwareMicrosoftVisualStudio9.0Text Editor<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">For Visual Studio 2005 you&rsquo;ll want to use &ldquo;8.0&rdquo; instead of &ldquo;9.0&rdquo;.<span>&nbsp; <\/span>Create a new String Value in the Text Editor key and call it &ldquo;Guides&rdquo;.<span>&nbsp; <\/span>Modify the data for Guides and add something like this:<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">RGB(0, 128, 0) 79<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">This will put a green line at column 79.<span>&nbsp; <\/span>You can have multiple guides:<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">RGB(0, 128, 0) 79, RGB(128, 0, 0) 49<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Make sure you restart Visual Studio after you add these values for the changes to take effect.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Enjoy!<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">&#8211; Jamie<\/font><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone, this is Jamie Eckman.&nbsp; The last time I posted I was an SDET (tester) on the Libraries team.&nbsp; I&rsquo;ve since moved to the Compiler team, still as an SDET.&nbsp; One thing we see a lot of on both teams is compiler diagnostics: warnings and errors. One of our Libraries developers, Stephan T. Lavavej, [&hellip;]<\/p>\n","protected":false},"author":289,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[11],"class_list":["post-5553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-test"],"acf":[],"blog_post_summary":"<p>Hello everyone, this is Jamie Eckman.&nbsp; The last time I posted I was an SDET (tester) on the Libraries team.&nbsp; I&rsquo;ve since moved to the Compiler team, still as an SDET.&nbsp; One thing we see a lot of on both teams is compiler diagnostics: warnings and errors. One of our Libraries developers, Stephan T. Lavavej, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/5553","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\/289"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=5553"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/5553\/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=5553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=5553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=5553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}