Sean Silva | 93ca021 | 2012-12-13 01:10:46 +0000 | [diff] [blame^] | 1 | =================================== |
| 2 | How To Setup Clang Tooling For LLVM |
| 3 | =================================== |
| 4 | |
| 5 | Clang Tooling provides infrastructure to write tools that need syntactic |
| 6 | and semantic infomation about a program. This term also relates to a set |
| 7 | of specific tools using this infrastructure (e.g. ``clang-check``). This |
| 8 | document provides information on how to set up and use Clang Tooling for |
| 9 | the LLVM source code. |
| 10 | |
| 11 | Introduction |
| 12 | ============ |
| 13 | |
| 14 | Clang Tooling needs a compilation database to figure out specific build |
| 15 | options for each file. Currently it can create a compilation database |
| 16 | from the ``compilation_commands.json`` file, generated by CMake. When |
| 17 | invoking clang tools, you can either specify a path to a build directory |
| 18 | using a command line parameter ``-p`` or let Clang Tooling find this |
| 19 | file in your source tree. In either case you need to configure your |
| 20 | build using CMake to use clang tools. |
| 21 | |
| 22 | Setup Clang Tooling Using CMake and Make |
| 23 | ======================================== |
| 24 | |
| 25 | If you intend to use make to build LLVM, you should have CMake 2.8.6 or |
| 26 | later installed (can be found `here <http://cmake.org>`_). |
| 27 | |
| 28 | First, you need to generate Makefiles for LLVM with CMake. You need to |
| 29 | make a build directory and run CMake from it: |
| 30 | |
| 31 | :: |
| 32 | |
| 33 | mkdir your/build/directory |
| 34 | cd your/build/directory |
| 35 | cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources |
| 36 | |
| 37 | If you want to use clang instead of GCC, you can add |
| 38 | ``-DCMAKE_C_COMPILER=/path/to/clang -DCMAKE_CXX_COMPILER=/path/to/clang++``. |
| 39 | You can also use ccmake, which provides a curses interface to configure |
| 40 | CMake variables for lazy people. |
| 41 | |
| 42 | As a result, the new ``compile_commands.json`` file should appear in the |
| 43 | current directory. You should link it to the LLVM source tree so that |
| 44 | Clang Tooling is able to use it: |
| 45 | |
| 46 | :: |
| 47 | |
| 48 | ln -s $PWD/compile_commands.json path/to/llvm/source/ |
| 49 | |
| 50 | Now you are ready to build and test LLVM using make: |
| 51 | |
| 52 | :: |
| 53 | |
| 54 | make check-all |
| 55 | |
| 56 | Using Clang Tools |
| 57 | ================= |
| 58 | |
| 59 | After you completed the previous steps, you are ready to run clang |
| 60 | tools. If you have a recent clang installed, you should have |
| 61 | ``clang-check`` in $PATH. Try to run it on any .cpp file inside the LLVM |
| 62 | source tree: |
| 63 | |
| 64 | :: |
| 65 | |
| 66 | clang-check tools/clang/lib/Tooling/CompilationDatabase.cpp |
| 67 | |
| 68 | If you're using vim, it's convenient to have clang-check integrated. Put |
| 69 | this into your .vimrc: |
| 70 | |
| 71 | :: |
| 72 | |
| 73 | function! ClangCheckImpl(cmd) |
| 74 | if &autowrite | wall | endif |
| 75 | echo "Running " . a:cmd . " ..." |
| 76 | let l:output = system(a:cmd) |
| 77 | cexpr l:output |
| 78 | cwindow |
| 79 | let w:quickfix_title = a:cmd |
| 80 | if v:shell_error != 0 |
| 81 | cc |
| 82 | endif |
| 83 | let g:clang_check_last_cmd = a:cmd |
| 84 | endfunction |
| 85 | |
| 86 | function! ClangCheck() |
| 87 | let l:filename = expand('%') |
| 88 | if l:filename =~ '\.\(cpp\|cxx\|cc\|c\)$' |
| 89 | call ClangCheckImpl("clang-check " . l:filename) |
| 90 | elseif exists("g:clang_check_last_cmd") |
| 91 | call ClangCheckImpl(g:clang_check_last_cmd) |
| 92 | else |
| 93 | echo "Can't detect file's compilation arguments and no previous clang-check invocation!" |
| 94 | endif |
| 95 | endfunction |
| 96 | |
| 97 | nmap <silent> <F5> :call ClangCheck()<CR><CR> |
| 98 | |
| 99 | When editing a .cpp/.cxx/.cc/.c file, hit F5 to reparse the file. In |
| 100 | case the current file has a different extension (for example, .h), F5 |
| 101 | will re-run the last clang-check invocation made from this vim instance |
| 102 | (if any). The output will go into the error window, which is opened |
| 103 | automatically when clang-check finds errors, and can be re-opened with |
| 104 | ``:cope``. |
| 105 | |
| 106 | Other ``clang-check`` options that can be useful when working with clang |
| 107 | AST: |
| 108 | |
| 109 | - ``-ast-print`` - Build ASTs and then pretty-print them. |
| 110 | - ``-ast-dump`` - Build ASTs and then debug dump them. |
| 111 | - ``-ast-dump-filter=<string>`` - Use with ``-ast-dump`` or |
| 112 | ``-ast-print`` to dump/print only AST declaration nodes having a |
| 113 | certain substring in a qualified name. Use ``-ast-list`` to list all |
| 114 | filterable declaration node names. |
| 115 | - ``-ast-list`` - Build ASTs and print the list of declaration node |
| 116 | qualified names. |
| 117 | |
| 118 | Examples: |
| 119 | |
| 120 | :: |
| 121 | |
| 122 | $ clang-check tools/clang/tools/clang-check/ClangCheck.cpp -ast-dump -ast-dump-filter ActionFactory::newASTConsumer |
| 123 | Processing: tools/clang/tools/clang-check/ClangCheck.cpp. |
| 124 | Dumping ::ActionFactory::newASTConsumer: |
| 125 | clang::ASTConsumer *newASTConsumer() (CompoundStmt 0x44da290 </home/alexfh/local/llvm/tools/clang/tools/clang-check/ClangCheck.cpp:64:40, line:72:3> |
| 126 | (IfStmt 0x44d97c8 <line:65:5, line:66:45> |
| 127 | <<<NULL>>> |
| 128 | (ImplicitCastExpr 0x44d96d0 <line:65:9> '_Bool':'_Bool' <UserDefinedConversion> |
| 129 | ... |
| 130 | $ clang-check tools/clang/tools/clang-check/ClangCheck.cpp -ast-print -ast-dump-filter ActionFactory::newASTConsumer |
| 131 | Processing: tools/clang/tools/clang-check/ClangCheck.cpp. |
| 132 | Printing <anonymous namespace>::ActionFactory::newASTConsumer: |
| 133 | clang::ASTConsumer *newASTConsumer() { |
| 134 | if (this->ASTList.operator _Bool()) |
| 135 | return clang::CreateASTDeclNodeLister(); |
| 136 | if (this->ASTDump.operator _Bool()) |
| 137 | return clang::CreateASTDumper(this->ASTDumpFilter); |
| 138 | if (this->ASTPrint.operator _Bool()) |
| 139 | return clang::CreateASTPrinter(&llvm::outs(), this->ASTDumpFilter); |
| 140 | return new clang::ASTConsumer(); |
| 141 | } |
| 142 | |
| 143 | (Experimental) Using Ninja Build System |
| 144 | ======================================= |
| 145 | |
| 146 | Optionally you can use the `Ninja <https://github.com/martine/ninja>`_ |
| 147 | build system instead of make. It is aimed at making your builds faster. |
| 148 | Currently this step will require building Ninja from sources and using a |
| 149 | development version of CMake. |
| 150 | |
| 151 | To take advantage of using Clang Tools along with Ninja build you need |
| 152 | at least CMake 2.8.9. At the moment CMake 2.8.9 is still under |
| 153 | development, so you can get latest development sources and build it |
| 154 | yourself: |
| 155 | |
| 156 | :: |
| 157 | |
| 158 | git clone git://cmake.org/cmake.git |
| 159 | cd cmake |
| 160 | ./bootstrap |
| 161 | make |
| 162 | sudo make install |
| 163 | |
| 164 | Having the correct version of CMake, you can clone the Ninja git |
| 165 | repository and build Ninja from sources: |
| 166 | |
| 167 | :: |
| 168 | |
| 169 | git clone git://github.com/martine/ninja.git |
| 170 | cd ninja/ |
| 171 | ./bootstrap.py |
| 172 | |
| 173 | This will result in a single binary ``ninja`` in the current directory. |
| 174 | It doesn't require installation and can just be copied to any location |
| 175 | inside ``$PATH``, say ``/usr/local/bin/``: |
| 176 | |
| 177 | :: |
| 178 | |
| 179 | sudo cp ninja /usr/local/bin/ |
| 180 | sudo chmod a+rx /usr/local/bin/ninja |
| 181 | |
| 182 | After doing all of this, you'll need to generate Ninja build files for |
| 183 | LLVM with CMake. You need to make a build directory and run CMake from |
| 184 | it: |
| 185 | |
| 186 | :: |
| 187 | |
| 188 | mkdir your/build/directory |
| 189 | cd your/build/directory |
| 190 | cmake -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources |
| 191 | |
| 192 | If you want to use clang instead of GCC, you can add |
| 193 | ``-DCMAKE_C_COMPILER=/path/to/clang -DCMAKE_CXX_COMPILER=/path/to/clang++``. |
| 194 | You can also use ccmake, which provides a curses interface to configure |
| 195 | CMake variables in an interactive manner. |
| 196 | |
| 197 | As a result, the new ``compile_commands.json`` file should appear in the |
| 198 | current directory. You should link it to the LLVM source tree so that |
| 199 | Clang Tooling is able to use it: |
| 200 | |
| 201 | :: |
| 202 | |
| 203 | ln -s $PWD/compile_commands.json path/to/llvm/source/ |
| 204 | |
| 205 | Now you are ready to build and test LLVM using Ninja: |
| 206 | |
| 207 | :: |
| 208 | |
| 209 | ninja check-all |
| 210 | |
| 211 | Other target names can be used in the same way as with make. |