{"id":160,"date":"2017-06-28T15:24:09","date_gmt":"2017-06-28T15:24:09","guid":{"rendered":"http:\/\/cppdepend.com\/wordpress\/?p=160"},"modified":"2018-02-02T18:28:06","modified_gmt":"2018-02-02T18:28:06","slug":"what-clang-can-tell-you-about-your-visual-c-projects","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/what-clang-can-tell-you-about-your-visual-c-projects\/","title":{"rendered":"What Clang can tell you about your Visual C++ projects?"},"content":{"rendered":"<p>Each compiler could report after the build many warnings. These warnings won\u2019t keep your code from compiling except if you decide to treat them as errors. Don\u2019t hesitate to take a look as these warnings instead of ignoring them. Indeed compiler warnings are often indicators of future bugs that you would see only at runtime.<\/p>\n<p>Clang is a C\/C++\/Objective C compiler with many interesting features, here are some major end user features:<\/p>\n<ul>\n<li>Fast compiles and low memory usage.<\/li>\n<li>Expressive diagnostics.<\/li>\n<li>GCC compatibility.<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p><span id=\"more-246\"><\/span>In this post we will focus on the Expressive diagnostics feature. and how we can get these diagnostics for Visual C++ projects.<\/p>\n<p>The clang team aims to provide as clear and expressive error messages as possible. They try to make it as user friendly as they can for a command line compiler. To do so,\u00a0clang should pinpoint exactly what is wrong in the code. This is done through a diagnostics engine which processes the error information into a user friendly message.<\/p>\n<p>To discover the Clang diagnostics benefits, let\u2019s compile\u00a0a minimal C++ source code with both Visual C++ and Clang, and compare between the warnings reported. All warnings will be enabled for the two compilers.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/clang11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-249\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/clang11.png\" alt=\"clang1\" width=\"597\" height=\"297\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The Microsoft c++ compiler report the following warning<\/p>\n<ul>\n<li>warning C4100: \u2018n\u2019 : unreferenced formal parameter<\/li>\n<\/ul>\n<p>Clang report three\u00a0warnings:<\/p>\n<ul>\n<li>no previous prototype for function \u2018Print\u2019<\/li>\n<li>\u00a0use of old-style cast<\/li>\n<li>unused parameter \u2018n\u2019<\/li>\n<\/ul>\n<p>What\u2019s interesting with Clang is that it reports also diagnostics related to coding best practices, for example the warning \u201cno previous prototype for function Print\u201d is reported because Print is not declared as static function so it could be used in another source file, and in this case it\u2019s recommended\u00a0that the declaration exist in an include file.<\/p>\n<p>And to have more concrete idea about the Clang diagnostics, let\u2019s compile a C++ open source project with both Clang and Microsoft compiler\u00a0to\u00a0discover\u00a0what kind of warnings are reported by them.<\/p>\n<p>We take as example the \u201c7 zip\u201d project and\u00a0compile only the module Gui.vcproj. we will focus only on warnings related to\u00a0the project source code and not the external include files.<\/p>\n<p><strong><em>The Microsoft compiler report\u00a0these kinds of warnings<\/em><\/strong><\/p>\n<ul>\n<li>Bytes padding added after data member.<\/li>\n<li>Conversion issues.<\/li>\n<li>Function not inlined.<\/li>\n<li>Class has virtual functions, but destructor is not virtual.<\/li>\n<li>Catch(\u2026) semantics changed since Visual C++ 7.1; structured exceptions (SEH) are no longer caught.<\/li>\n<li>Cast issues.<\/li>\n<li>Enumerator \u2018kInfo\u2019 in switch of enum \u2018NCommandType::EEnum\u2019 is not explicitly handled by a case label.<\/li>\n<li>\u2018GetVersionExA\u2019: was declared deprecated.<\/li>\n<\/ul>\n<p><strong><em>The Clang compiler \u00a0report these kind of warnings:<\/em><\/strong><\/p>\n<ul>\n<li>Use of old-style cast<\/li>\n<li>Missing field \u2018MinLen\u2019 initializer<\/li>\n<li>Macro is not used<\/li>\n<li>Declaration requires a global constructor<\/li>\n<li>Declaration requires an exit-time destructor<\/li>\n<li>Conversion issues<\/li>\n<li>Cast issues<\/li>\n<li>enumeration values \u2018kWithoutPrompt\u2019, \u2018kAutoRename\u2019, and \u2018kAutoRenameExisting\u2019 not handled in switch<\/li>\n<li>Operand of ? changes signedness: \u2018const int\u2019 to \u2018unsigned int\u2019<\/li>\n<li>Declaration shadows a local variable<\/li>\n<li>ISO C++11 does not allow conversion from string literal to \u2018LPSTR\u2019 (aka \u2018char *\u2019)<\/li>\n<li>No previous prototype for function<\/li>\n<li>Comparison issues<\/li>\n<li>\u2018GetVersionExA\u2019: was declared deprecated<\/li>\n<li>dynamic exception specifications are deprecated<\/li>\n<li>\u2018&amp;&amp;\u2019 within \u2018||\u2019<\/li>\n<li>\u2018CBenchRandomGenerator\u2019 has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit<\/li>\n<li>Delete called on \u2018CBenchmarkInStream\u2019 that has virtual functions but non-virtual destructor<\/li>\n<li>\u2018CBenchmarkInStream\u2019 has virtual functions but non-virtual destructor<\/li>\n<li>Equality comparison result unused<\/li>\n<li>Explicitly assigning value of variable of type \u2018INT_PTR\u2019 (aka \u2018int\u2019) to itself<\/li>\n<li>Add explicit braces to avoid dangling else<\/li>\n<\/ul>\n<p>As we can observe Clang reports more interesting warnings than the Microsoft compiler, some of them are generally reported by static analysis tools, and it\u2019s recommended to take seriously these warnings, they could reveal many bugs.<\/p>\n<p><strong>How to get the Clang diagnostics for your Visual C++ projects?<\/strong><\/p>\n<p>The Clang diagnostics helps the developers to improve their C\/C++ souce code quality, and it will be useful if we can get them for our Visual C++ projects.<\/p>\n<p>The first alternative is to compile your projects using Clang, here&#8217;s an <a href=\"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2017\/03\/07\/use-any-c-compiler-with-visual-studio\/\">msdn post<\/a> to show you how to use Clang as compiler in VS.<\/p>\n<p>The second alternative is to analyze your visual studio projects using <a href=\"http:\/\/www.cppdepend.com\/\">CppDepend<\/a> which uses Clang as front end parser, and report all its diagnostics. CppDepend is <a href=\"http:\/\/ossperks.com\/\">free for the open source community<\/a>.<\/p>\n<p>The following query give us all the Clang diagnostics:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/clang6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-250\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/clang6.png\" alt=\"clang6\" width=\"535\" height=\"537\" \/><\/a><\/p>\n<p>The Gui project from 7-Zip application has only 10k line of code and 1186 diagnostics, for medium and big projects you could have thousands of diagnostics, and in this case it will be useful to filter them, and get only some kind of warnings.<\/p>\n<p>For example we can modify the previous query and\u00a0get only warnings related to deprecated usage.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/clang8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-251\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/clang8.png\" alt=\"clang8\" width=\"512\" height=\"390\" \/><\/a><\/p>\n<p>Conclusion:<\/p>\n<p>Warnings are often indicators of future bugs, it\u2019s recommended to not ignore them. And Clang\u00a0reports many interesting diagnostics, some of them are related to coding best practices, and removing these warnings will contribute to improve the quality of you project source code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Each compiler could report after the build many warnings. These warnings won\u2019t keep your code from compiling except if you decide to treat them as errors. Don\u2019t hesitate to take a look as these warnings instead of ignoring them. Indeed compiler warnings are often indicators of future bugs that you would see only at runtime. &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/what-clang-can-tell-you-about-your-visual-c-projects\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;What Clang can tell you about your Visual C++ projects?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7,25],"class_list":["post-160","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-clang"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=160"}],"version-history":[{"count":5,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":170,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/160\/revisions\/170"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}