{"id":2650,"date":"2013-08-13T12:10:59","date_gmt":"2013-08-13T01:10:59","guid":{"rendered":"http:\/\/blog.mozilla.org\/nnethercote\/?p=2650"},"modified":"2013-08-13T12:10:59","modified_gmt":"2013-08-13T01:10:59","slug":"using-include-what-you-use","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/nnethercote\/2013\/08\/13\/using-include-what-you-use\/","title":{"rendered":"Using include-what-you-use"},"content":{"rendered":"<p><a href=\"http:\/\/code.google.com\/p\/include-what-you-use\/\">include-what-you-use<\/a> (a.k.a. IWYU) is a clang tool that tells you which #include statements should be added and removed from a file.\u00a0 Nicholas Cameron used it to <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=903816\">speed up the building of gfx\/layers by 12.5%<\/a>.\u00a0 I&#8217;ve also used it <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=634839\">quite<\/a> <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=888768\">a<\/a> <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=902917\">bit <\/a>within SpiderMonkey;\u00a0 I&#8217;ve seen smaller build speed improvements but I&#8217;ve also been doing it in chunks over time.\u00a0 Ms2ger started a <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=888768\">tracking bug<\/a> for all places where IWYU has been used in Mozilla code.<\/p>\n<p>Ehsan <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=903816#c15\">asked<\/a> for instructions on setting up IWYU.\u00a0 There are <a href=\"http:\/\/code.google.com\/p\/include-what-you-use\/wiki\/InstructionsForUsers\">official instructions<\/a>, but I thought it might be helpful to document exactly what I did.<\/p>\n<p>First, here is how I installed clang, based on <a href=\"http:\/\/ehsanakhgari.org\/blog\/2011-10-17\/why-you-should-switch-clang-today-and-how\">Ehsan&#8217;s instructions<\/a>.\u00a0 I put the source code under <code>$HOME\/local\/src<\/code> and installed the build under <code>$HOME\/local<\/code>.<\/p>\n<pre>  mkdir $HOME\/local\/src\r\n  cd $HOME\/local\/src\r\n  svn co http:\/\/llvm.org\/svn\/llvm-project\/llvm\/trunk llvm\r\n  cd llvm\/tools\r\n  svn co http:\/\/llvm.org\/svn\/llvm-project\/cfe\/trunk clang\r\n  cd ..\/..\r\n  mkdir build\r\n  cd build\/\r\n  ..\/configure --enable-optimized --disable-assertions --prefix=$HOME\/local\r\n  make\r\n  sudo make install<\/pre>\n<p>Then I followed the <a href=\"http:\/\/code.google.com\/p\/include-what-you-use\/wiki\/InstructionsForUsers#Building_in-tree\">&#8220;Building in-tree&#8221; instructions<\/a>.\u00a0 The first part is to get the IWYU code.<\/p>\n<pre>   cd $HOME\/local\/src\/llvm\/tools\/clang\/tools\r\n   svn checkout http:\/\/include-what-you-use.googlecode.com\/svn\/trunk\/ include-what-you-use<\/pre>\n<p>The second part was to do the following steps.<\/p>\n<ul>\n<li>Edit tools\/clang\/tools\/Makefile and add |include-what-you-use| to the DIRS variable.<\/li>\n<li>Edit tools\/clang\/tools\/CMakeLists.txt and add |add_subdirectory(include-what-you-use)|.<\/li>\n<li>Re-build clang as per the above instructions.<\/li>\n<\/ul>\n<p>After that, configure a Mozilla tree for a clang build and then run <code>make<\/code> with the following options: <code>-j1 -k CXX=\/home\/njn\/local\/src\/llvm\/build\/Release\/bin\/include-what-you-use<\/code>. I&#8217;m not certain if the <code>-j1<\/code> is necessary, but since IWYU spits out lots of output, it seemed wise. The <code>-k<\/code> tells <code>make<\/code> to keep building even after errors; for some reason, IWYU triggers compilation failure on every file it looks at.<\/p>\n<p>Pipe the output to a file, and you&#8217;ll see lots of stuff like this.<\/p>\n<pre>..\/jsarray.h should add these lines:\r\n#include &lt;stdint.h&gt;                     \/\/ for uint32_t\r\n#include &lt;sys\/types.h&gt;                  \/\/ for int32_t\r\n#include \"dist\/include\/js\/RootingAPI.h\"  \/\/ for HandleObject, Handle, etc\r\n#include \"jsapi.h\"                      \/\/ for Value, HandleObject, etc\r\n#include \"jsfriendapi.h\"                \/\/ for JSID_TO_ATOM\r\n#include \"jstypes.h\"                    \/\/ for JSBool\r\n#include \"vm\/String.h\"                  \/\/ for JSAtom\r\nnamespace JS { class Value; }\r\nnamespace js { class ExclusiveContext; }\r\nstruct JSContext;\r\n\r\n..\/jsarray.h should remove these lines:\r\n\r\nThe full include-list for ..\/jsarray.h:\r\n#include &lt;stdint.h&gt;                     \/\/ for uint32_t\r\n#include &lt;sys\/types.h&gt;                  \/\/ for int32_t\r\n#include \"dist\/include\/js\/RootingAPI.h\"  \/\/ for HandleObject, Handle, etc\r\n#include \"jsapi.h\"                      \/\/ for Value, HandleObject, etc\r\n#include \"jsfriendapi.h\"                \/\/ for JSID_TO_ATOM\r\n#include \"jsobj.h\"                      \/\/ for JSObject (ptr only), etc\r\n#include \"jspubtd.h\"                    \/\/ for jsid\r\n#include \"jstypes.h\"                    \/\/ for JSBool\r\n#include \"vm\/String.h\"                  \/\/ for JSAtom\r\nnamespace JS { class Value; }\r\nnamespace js { class ArrayObject; }  \/\/ lines 44-44\r\nnamespace js { class ExclusiveContext; }\r\nstruct JSContext;<\/pre>\n<p>I focused on addressing the &#8220;should remove these lines&#8221; #includes, and I did it manually. There&#8217;s also a <a href=\"https:\/\/bugzilla.mozilla.org\/attachment.cgi?id=789028\">script<\/a> you can use to automatically do everything for you;\u00a0 I don&#8217;t know how well it works.<\/p>\n<p>Note that IWYU&#8217;s output is just plain wrong about 5% of the time &#8212; i.e. it says you can remove #includes that you clearly cannot.\u00a0 (A lot of the time this seems to be because it hasn&#8217;t realized that a macro is needed.)\u00a0 I also found that, while it produced output for all .cpp files, it only produced output for some of the .h files.\u00a0 No idea why. Finally, it doesn&#8217;t know about local idioms; in particular, if you have platform-dependent code, its suggestions are often terrible because it only sees the files for the platform you are building on.<\/p>\n<p>Good luck!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>include-what-you-use (a.k.a. IWYU) is a clang tool that tells you which #include statements should be added and removed from a file.\u00a0 Nicholas Cameron used it to speed up the building of gfx\/layers by 12.5%.\u00a0 I&#8217;ve also used it quite a bit within SpiderMonkey;\u00a0 I&#8217;ve seen smaller build speed improvements but I&#8217;ve also been doing it [&hellip;]<\/p>\n","protected":false},"author":139,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4561,30],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/posts\/2650"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/users\/139"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/comments?post=2650"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/posts\/2650\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/media?parent=2650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/categories?post=2650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/nnethercote\/wp-json\/wp\/v2\/tags?post=2650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}