{"id":223,"date":"2014-11-12T07:35:00","date_gmt":"2014-11-12T07:35:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2014\/11\/12\/improvements-to-warnings-in-the-c-compiler\/"},"modified":"2019-02-18T18:05:12","modified_gmt":"2019-02-18T18:05:12","slug":"improvements-to-warnings-in-the-c-compiler","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/improvements-to-warnings-in-the-c-compiler\/","title":{"rendered":"Improvements to Warnings in the C++ Compiler"},"content":{"rendered":"<p>We made a number of improvements to C++ compiler warnings in Visual Studio 2015 Preview.<\/p>\n<h2>The printf family of functions<\/h2>\n<p>The community has made it clear that <strong>printf<\/strong> and its cousins are still heavily used and would benefit from more rigorous diagnostics when used. In Visual Studio 2015 Preview, the compiler will check that the number of arguments you provide to these functions match the number of arguments expected by the format string:<\/p>\n<p><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;printf(<span style=\"color: #a31515\">&#8220;%d %dn&#8221;<span style=\"color: black\">, 1); <span style=\"color: green\">\/\/C4317<\/span><\/span><\/span><\/span><br \/><span style=\"color: black\"><span style=\"font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;printf(<span style=\"color: #a31515\">&#8220;%dn&#8221;<span style=\"color: black\">, 1, 2); <span style=\"color: green\">\/\/C4422<\/span><\/span><\/span><\/span> <\/span><\/p>\n<p>The compiler will emit:<\/p>\n<p><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt\"><span style=\"background-color: white\">warning C4317: &#8216;printf&#8217; : not enough arguments passed for format string<\/span> <\/span><br \/><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">warning C4422: &#8216;printf&#8217; : too many arguments passed for format string<\/span><\/p>\n<p>Furthermore, specifiers that aren&#8217;t allowed to be used in some variants of printf will also emit warnings:<\/p>\n<p><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">int<span style=\"color: black\"> n; <\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;printf_s(<span style=\"color: #a31515\">&#8220;Test %n&#8221;<span style=\"color: black\">, &amp;n); <span style=\"color: green\">\/\/C4426<\/span><\/span><\/span><\/span><\/p>\n<p>Here, <strong>printf_s<\/strong> is the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/239ffwa0.aspx\">security-enhanced<\/a> version of <strong>printf<\/strong>, which disallows <strong>%n<\/strong>. The warning is:<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">warning C4426: &#8216;%n&#8217; is not allowed in the format string of function &#8216;printf_s&#8217;<\/span><\/p>\n<p>In Visual Studio 2015 RTW, we will add warnings that also check the types of the parameters passed to these functions.<\/p>\n<h2>Shadowed variables<\/h2>\n<p>A variable declaration &#8220;shadows&#8221; another if the enclosing scope already contains a variable with the same name. For example:<\/p>\n<p><span style=\"color: blue;font-family: Consolas;font-size: 9pt;background-color: white\">void<span style=\"color: black\"> f(<span style=\"color: blue\">int<span style=\"color: black\"> <span style=\"color: gray\">x<span style=\"color: black\">) <\/span><\/span><\/span><\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">{ <\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">int<span style=\"color: black\"> y;<\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;{ <\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">char<span style=\"color: black\"> x; <span style=\"color: green\">\/\/C4457<\/span><\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">char<span style=\"color: black\"> y; <span style=\"color: green\">\/\/C4456<\/span><\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;} <\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt\"><span style=\"background-color: white\">}<\/span><\/span><\/p>\n<p>The inner declaration of <strong>x<\/strong> shadows the parameter of function <strong>f<\/strong>, so the compiler will emit:<\/p>\n<p><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">warning C4457: declaration of &#8216;x&#8217; hides function parameter<\/span><\/p>\n<p>The inner declaration of <strong>y<\/strong> shadows the declaration of <strong>y<\/strong> in the function scope, so the compiler will emit:<\/p>\n<p><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">warning C4456: declaration of &#8216;y&#8217; hides previous local declaration<\/span><\/p>\n<p>Note that, as before, a variable declaration with the same name as a function parameter but not enclosed in an inner scope triggers an error instead:<\/p>\n<p><span style=\"color: blue;font-family: Consolas;font-size: 9pt;background-color: white\">void<span style=\"color: black\"> f(<span style=\"color: blue\">int<span style=\"color: black\"> <span style=\"color: gray\">x<span style=\"color: black\">) <\/span><\/span><\/span><\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">{ <\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: blue\">char<span style=\"color: black\"> x; <span style=\"color: green\">\/\/C2082<\/span><\/span><\/span><\/span><br \/><span style=\"color: black;font-family: Consolas;font-size: 9pt;background-color: white\">}<\/span><\/p>\n<p>The compiler emits:<\/p>\n<p><span style=\"color: #1e1e1e;font-family: Consolas;font-size: 9pt;background-color: white\">error C2082: redefinition of formal parameter &#8216;x&#8217;<\/span><\/p>\n<h2>Old code?<\/h2>\n<p>If you use <strong>\/WX<\/strong>, these new warnings could cause some of your old projects to no longer build. To work around this, you can use <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/2c8f766e.aspx\">#<strong>pragma warning<\/strong><\/a> or the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/thxezb7y.aspx\"><strong>\/wd<\/strong><\/a><strong> <\/strong>compiler option to disable them. Alternatively, a new compiler option has been added to allow you to control the set of warnings emitted by the compiler, in order to facilitate the compilation of code that you don&#8217;t wish to modify. To suppress warnings introduced after compiler version <em>XX.YY.ZZZZ<\/em>, use <strong>\/Wv:<em>XX.YY.ZZZZ<\/em><\/strong>. <em>XX<\/em> is the major version number of the compiler, which is 15 for Visual Studio 2008, 16 for Visual Studio 2010, 17 for Visual Studio 2012, and 18 for Visual Studio 2013.<\/p>\n<p>However, for projects that are still in active development, we strongly encourage you to keep all new warnings enabled and fix the code instead, because they will likely help you write programs that are more robust and more secure.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We made a number of improvements to C++ compiler warnings in Visual Studio 2015 Preview. The printf family of functions The community has made it clear that printf and its cousins are still heavily used and would benefit from more rigorous diagnostics when used. In Visual Studio 2015 Preview, the compiler will check that the [&hellip;]<\/p>\n","protected":false},"author":299,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[8],"class_list":["post-223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-announcement"],"acf":[],"blog_post_summary":"<p>We made a number of improvements to C++ compiler warnings in Visual Studio 2015 Preview. The printf family of functions The community has made it clear that printf and its cousins are still heavily used and would benefit from more rigorous diagnostics when used. In Visual Studio 2015 Preview, the compiler will check that the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/223","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\/299"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/223\/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=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}