{"id":1197,"date":"2013-04-06T16:37:07","date_gmt":"2013-04-06T06:37:07","guid":{"rendered":"http:\/\/brnz.org\/hbr\/?p=1197"},"modified":"2013-04-06T16:37:07","modified_gmt":"2013-04-06T06:37:07","slug":"compiler-assisted-code-cleaning-thursday-2013-03-28","status":"publish","type":"post","link":"https:\/\/brnz.org\/hbr\/?p=1197","title":{"rendered":"Compiler-assisted code cleaning, Thursday 2013-03-28"},"content":{"rendered":"<p>This was another day of no runtime bugs. Instead of a picture, I&#8217;ll describe a specific(ish) case of using the compiler to catch all uses of a particular pattern in the code.<\/p>\n<pre escaped=\"true\" lang=\"cpp\">class Foo {};\r\n\r\n\/\/ Bar is one of many classes that inherit from Foo\r\nclass Bar : public Foo {};\r\n\r\n\/\/ Baz is one of many classes that inherit from Bar\r\nclass Baz : public Bar {};\r\n\r\nDECLARE_EXTRA_STUFF_FOR_CLASS(Baz);<\/pre>\n<p><span style=\"line-height: 1.714285714; font-size: 1rem;\">So we have something like the above. The <strong>DECLARE_EXTRA_STUFF_FOR_CLASS()<\/strong> macro is used for some but not all classes that inherit directly or indirectly from <strong>Foo<\/strong>.<\/span><\/p>\n<p>There have recently been changes within a system that means that <strong>DECLARE_EXTRA_STUFF_FOR_CLASS()<\/strong> is now never needed for any class that inherits from <strong>Bar<\/strong> &#8212; but it is not invalid for it to remain used in the codebase. Anything that makes use of the <strong>EXTRA_STUFF<\/strong> that the macro provides will still work, it&#8217;s just extra unneeded code which I&#8217;d like to clean up.<\/p>\n<p>I could audit this by hand &#8212; find every class inheriting from <strong>Bar<\/strong>, check to see if there&#8217;s a macro definition for that class somewhere and perform the subsequent clean up, but I&#8217;m lazier than that.<\/p>\n<p>I could write a script that finds classes that inherit from <strong>Bar<\/strong>, and then search for that class name with the macro, but that also seems like to much work. (A simple regex sweep through the codebase won&#8217;t trivially identify <strong>class Asd : public Baz {}<\/strong> as inheriting from <strong>Bar<\/strong>). I&#8217;m lazier than this.<\/p>\n<p>What I can do is break the code expanded by the macro if it is passed a subclass of <strong>Bar<\/strong>. To do that, I can do something like the following:<\/p>\n<pre escaped=\"true\" lang=\"cpp\">class Foo              { <strong>public:  static void Foo(){}<\/strong> };\r\nclass Bar : public Foo { <strong>private: static void Foo();<\/strong> };\r\n\/\/ Add a line to the macro...\r\nDECLARE_EXTRA_STUFF_FOR_CLASS(class_name) \\\r\n\u00a0 \u00a0 extra_stuff; \\\r\n\u00a0 \u00a0 <strong>class_name::Foo(); \\<\/strong>\r\n\u00a0 \u00a0 more_extra_stuff<\/pre>\n<p>Done. Anywhere where the macro is invoked for a class inheriting from <strong>Bar<\/strong> will trigger a compile error. Walk through the errors, delete calls to that macro, rewrite (i.e. delete) code as necessary.<\/p>\n<p>End result: less code. I count that as a win.<\/p>\n<p>(In my specific case, I made an already existing public method declared in <strong>Foo<\/strong> private in <strong>Bar<\/strong> because there is no need to call it from or for classes in that part of the hierarchy. This has the added advantage of not polluting the list of <strong>Foo<\/strong>&#8216;s members with no functionality of the class. Additionally, I didn&#8217;t call the function within the macro but instead took it&#8217;s address.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This was another day of no runtime bugs. Instead of a picture, I&#8217;ll describe a specific(ish) case of using the compiler to catch all uses of a particular pattern in the code. class Foo {}; \/\/ Bar is one of many classes that inherit from Foo class Bar : public Foo {}; \/\/ Baz is &hellip; <a href=\"https:\/\/brnz.org\/hbr\/?p=1197\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Compiler-assisted code cleaning, Thursday 2013-03-28&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1197"}],"collection":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1197"}],"version-history":[{"count":6,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1197\/revisions"}],"predecessor-version":[{"id":1244,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1197\/revisions\/1244"}],"wp:attachment":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}