{"id":62,"date":"2017-06-02T18:54:01","date_gmt":"2017-06-02T18:54:01","guid":{"rendered":"http:\/\/cppdepend.com\/wordpress\/?p=62"},"modified":"2017-06-26T22:48:10","modified_gmt":"2017-06-26T22:48:10","slug":"detect-the-obfuscated-names-in-a-cc-project","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/detect-the-obfuscated-names-in-a-cc-project\/","title":{"rendered":"Detect the obfuscated names in a C\/C++ project"},"content":{"rendered":"<p>How many times do you already discover a code like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-69\" src=\"http:\/\/cppdepend.com\/wordpress\/wp-content\/uploads\/2017\/06\/obfuscatednames.png\" alt=\"obfuscatednames\" width=\"460\" height=\"136\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2017\/06\/obfuscatednames.png 460w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2017\/06\/obfuscatednames-300x89.png 300w\" sizes=\"auto, (max-width: 460px) 85vw, 460px\" \/><\/p>\n<p>Maybe in some cases it&#8217;s not a big issue to have such code. But\u00a0if this coding habit is used many times by the developers, it will cost a lot to the company. Each new comer who needs to debug or add a new feature will spent a lot of time to understand the existing codebase.<\/p>\n<p>How to detect these obfuscated names to refactor them?<!--more--><\/p>\n<p><strong>1- Using the clang-query and the \u00a0AST Matchers<\/strong><\/p>\n<p>Clang\u2019s LibASTMatchers is a powerful library to match nodes of the AST and execute code that uses the matched nodes. Combined with\u00a0<a class=\"reference internal\" href=\"http:\/\/clang.llvm.org\/docs\/LibTooling.html\"><span class=\"doc\">LibTooling<\/span><\/a>, LibASTMatchers helps to write code-to-code transformation tools or query tools.<\/p>\n<p>The <a href=\"https:\/\/github.com\/llvm-mirror\/clang-tools-extra\/tree\/master\/clang-query\">clang-query<\/a> tool is based on LibASTMatchers and provides an easy way to query the AST nodes of a specific source file.<\/p>\n<p>For example if we want to detect the functions where the name contains only one charachter, we can execute this ast matcher:<\/p>\n<pre class=\"default prettyprint prettyprinted\"><code><span class=\"str\">functionDecl(matchesName(\"^[a-zA-Z]$\"))<\/span><\/code><\/pre>\n<p>The AST Machers are a powerful way to query the codebase, here&#8217;s a quick definition from the <a href=\"https:\/\/clang.llvm.org\/docs\/LibASTMatchers.html\">AST Matchers documentation<\/a>.<\/p>\n<p>AST matchers are predicates on nodes in the AST. Matchers are created by calling creator functions that allow building up a tree of matchers, where inner matchers are used to make the match more specific.<\/p>\n<p>For example, to create a matcher that matches all class or union declarations in the AST of a translation unit, you can call <a class=\"reference external\" href=\"https:\/\/clang.llvm.org\/docs\/LibASTMatchersReference.html#recordDecl0Anchor\">recordDecl()<\/a>. To narrow the match down, for example to find all class or union declarations with the name \u201c<code class=\"docutils literal\"><span class=\"pre\">Foo<\/span><\/code>\u201d, insert a <a class=\"reference external\" href=\"https:\/\/clang.llvm.org\/docs\/LibASTMatchersReference.html#hasName0Anchor\">hasName<\/a> matcher: the call <code class=\"docutils literal\"><span class=\"pre\">recordDecl(hasName(\"Foo\"))<\/span><\/code> returns a matcher that matches classes or unions that are named \u201c<code class=\"docutils literal\"><span class=\"pre\">Foo<\/span><\/code>\u201d, in any namespace. By default, matchers that accept multiple inner matchers use an implicit <a class=\"reference external\" href=\"https:\/\/clang.llvm.org\/docs\/LibASTMatchersReference.html#allOf0Anchor\">allOf()<\/a>. This allows further narrowing down the match, for example to match all classes that are derived from \u201c<code class=\"docutils literal\"><span class=\"pre\">Bar<\/span><\/code>\u201d: <code class=\"docutils literal\"><span class=\"pre\">recordDecl(hasName(\"Foo\"),<\/span> <span class=\"pre\">isDerivedFrom(\"Bar\"))<\/span><\/code>.<\/p>\n<p><strong>2- Using CppDepend and CQLinq<\/strong><\/p>\n<p>CppDepend comes with CQLinq, a code query language to query your codebase.<\/p>\n<p>CQLinq defines a few predefined domains to query on including: <b>Types<\/b> ; <b>Methods<\/b> ; <b>Fields<\/b> ; <b>Namespaces<\/b> ; <b>Projects<\/b><\/p>\n<p>These domains enumerate not only all code elements of the code base queried, but also all <b>third-party<\/b> code elements used by the code base (like for example the type <b>string<\/b> and all methods and fields of the type <b>string<\/b> that are used by the code base).<\/p>\n<p>The syntax is as simple as:<\/p>\n<pre><span style=\"color: #0000ff;\">from<\/span>\u00a0<span style=\"color: #000000;\">m<\/span>\u00a0<span style=\"color: #0000ff;\">in<\/span>\u00a0<b>Methods<\/b>\u00a0<span style=\"color: #0000ff;\">where<\/span>\u00a0<span style=\"color: #000000;\">m<\/span><span style=\"color: #000000;\">.<\/span><span style=\"color: #000000;\">NbLinesOfCode<\/span>\u00a0<span style=\"color: #000000;\">&gt;<\/span>\u00a0<b>30<\/b>\u00a0<span style=\"color: #0000ff;\">select<\/span>\u00a0<span style=\"color: #000000;\">m<\/span>\r\n<\/pre>\n<p>And to detect the functions where the names contains only one character we can execute the following CQLinq query:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-72\" src=\"http:\/\/cppdepend.com\/wordpress\/wp-content\/uploads\/2017\/06\/Untitled2.png\" alt=\"Untitled2\" width=\"445\" height=\"338\" srcset=\"https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2017\/06\/Untitled2.png 445w, https:\/\/cppdepend.com\/blog\/wp-content\/uploads\/2017\/06\/Untitled2-300x228.png 300w\" sizes=\"auto, (max-width: 445px) 85vw, 445px\" \/><\/p>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>Do not underestimate the importance of the symbol names, it helps to make the codebase easy to understand and maintains. It&#8217;s very interesting to automate your build process and detect the bad named symbols to improve your codebase code quality.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How many times do you already discover a code like this: Maybe in some cases it&#8217;s not a big issue to have such code. But\u00a0if this coding habit is used many times by the developers, it will cost a lot to the company. Each new comer who needs to debug or add a new feature &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/detect-the-obfuscated-names-in-a-cc-project\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Detect the obfuscated names in a C\/C++ project&#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":[3],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cplusplus"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/62","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=62"}],"version-history":[{"count":13,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":77,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions\/77"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}