1e5dd7070Spatrick=================================== 2e5dd7070SpatrickHow To Setup Clang Tooling For LLVM 3e5dd7070Spatrick=================================== 4e5dd7070Spatrick 5e5dd7070SpatrickClang Tooling provides infrastructure to write tools that need syntactic 6e5dd7070Spatrickand semantic information about a program. This term also relates to a set 7e5dd7070Spatrickof specific tools using this infrastructure (e.g. ``clang-check``). This 8e5dd7070Spatrickdocument provides information on how to set up and use Clang Tooling for 9e5dd7070Spatrickthe LLVM source code. 10e5dd7070Spatrick 11e5dd7070SpatrickIntroduction 12e5dd7070Spatrick============ 13e5dd7070Spatrick 14e5dd7070SpatrickClang Tooling needs a compilation database to figure out specific build 15e5dd7070Spatrickoptions for each file. Currently it can create a compilation database 16e5dd7070Spatrickfrom the ``compile_commands.json`` file, generated by CMake. When 17e5dd7070Spatrickinvoking clang tools, you can either specify a path to a build directory 18e5dd7070Spatrickusing a command line parameter ``-p`` or let Clang Tooling find this 19e5dd7070Spatrickfile in your source tree. In either case you need to configure your 20e5dd7070Spatrickbuild using CMake to use clang tools. 21e5dd7070Spatrick 22e5dd7070SpatrickSetup Clang Tooling Using CMake and Make 23e5dd7070Spatrick======================================== 24e5dd7070Spatrick 25e5dd7070SpatrickIf you intend to use make to build LLVM, you should have CMake 2.8.6 or 26e5dd7070Spatricklater installed (can be found `here <https://cmake.org>`_). 27e5dd7070Spatrick 28e5dd7070SpatrickFirst, you need to generate Makefiles for LLVM with CMake. You need to 29e5dd7070Spatrickmake a build directory and run CMake from it: 30e5dd7070Spatrick 31e5dd7070Spatrick.. code-block:: console 32e5dd7070Spatrick 33e5dd7070Spatrick $ mkdir your/build/directory 34e5dd7070Spatrick $ cd your/build/directory 35e5dd7070Spatrick $ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources 36e5dd7070Spatrick 37e5dd7070SpatrickIf you want to use clang instead of GCC, you can add 38e5dd7070Spatrick``-DCMAKE_C_COMPILER=/path/to/clang -DCMAKE_CXX_COMPILER=/path/to/clang++``. 39e5dd7070SpatrickYou can also use ``ccmake``, which provides a curses interface to configure 40ec727ea7SpatrickCMake variables. 41e5dd7070Spatrick 42e5dd7070SpatrickAs a result, the new ``compile_commands.json`` file should appear in the 43e5dd7070Spatrickcurrent directory. You should link it to the LLVM source tree so that 44e5dd7070SpatrickClang Tooling is able to use it: 45e5dd7070Spatrick 46e5dd7070Spatrick.. code-block:: console 47e5dd7070Spatrick 48e5dd7070Spatrick $ ln -s $PWD/compile_commands.json path/to/llvm/source/ 49e5dd7070Spatrick 50e5dd7070SpatrickNow you are ready to build and test LLVM using make: 51e5dd7070Spatrick 52e5dd7070Spatrick.. code-block:: console 53e5dd7070Spatrick 54e5dd7070Spatrick $ make check-all 55e5dd7070Spatrick 56*12c85518SrobertSetup Clang Tooling Using CMake on Windows 57*12c85518Srobert========================================== 58*12c85518Srobert 59*12c85518SrobertFor Windows developers, the Visual Studio project generators in CMake do 60*12c85518Srobertnot support `CMAKE_EXPORT_COMPILE_COMMANDS 61*12c85518Srobert<https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html>`_. 62*12c85518SrobertHowever, the Ninja generator does support this variable and can be used 63*12c85518Sroberton Windows to generate a suitable ``compile_commands.json`` that invokes 64*12c85518Srobertthe MSVC compiler. 65*12c85518Srobert 66*12c85518SrobertFirst, you will need to install `Ninja`_. Once installed, the Ninja 67*12c85518Srobertexecutable will need to be in your search path for CMake to locate it. 68*12c85518Srobert 69*12c85518SrobertNext, assuming you already have Visual Studio installed on your machine, you 70*12c85518Srobertneed to have the appropriate environment variables configured so that CMake 71*12c85518Srobertwill locate the MSVC compiler for the Ninja generator. The `documentation 72*12c85518Srobert<https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-170#path_and_environment>`_ 73*12c85518Srobertdescribes the necessary environment variable settings, but the simplest thing 74*12c85518Srobertis to use a `developer command-prompt window 75*12c85518Srobert<https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-170#developer_command_prompt_shortcuts>`_ 76*12c85518Srobertor call a `developer command file 77*12c85518Srobert<https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-170#developer_command_file_locations>`_ 78*12c85518Srobertto set the environment variables appropriately. 79*12c85518Srobert 80*12c85518SrobertNow you can run CMake with the Ninja generator to export a compilation 81*12c85518Srobertdatabase: 82*12c85518Srobert 83*12c85518Srobert.. code-block:: console 84*12c85518Srobert 85*12c85518Srobert C:\> mkdir build-ninja 86*12c85518Srobert C:\> cd build-ninja 87*12c85518Srobert C:\build-ninja> cmake -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources 88*12c85518Srobert 89*12c85518SrobertIt is best to keep your Visual Studio IDE build folder separate from the 90*12c85518SrobertNinja build folder. This prevents the two build systems from negatively 91*12c85518Srobertinteracting with each other. 92*12c85518Srobert 93*12c85518SrobertOnce the ``compile_commands.json`` file has been created by Ninja, you can 94*12c85518Srobertuse that compilation database with Clang Tooling. One caveat is that because 95*12c85518Srobertthere are indirect settings obtained through the environment variables, 96*12c85518Srobertyou may need to run any Clang Tooling executables through a command prompt 97*12c85518Srobertwindow created for use with Visual Studio as described above. An 98*12c85518Srobertalternative, e.g. for using the Visual Studio debugger on a Clang Tooling 99*12c85518Srobertexecutable, is to ensure that the environment variables are also visible 100*12c85518Srobertto the debugger settings. This can be done locally in Visual Studio's 101*12c85518Srobertdebugger configuration locally or globally by launching the Visual Studio 102*12c85518SrobertIDE from a suitable command-prompt window. 103*12c85518Srobert 104e5dd7070SpatrickUsing Clang Tools 105e5dd7070Spatrick================= 106e5dd7070Spatrick 107e5dd7070SpatrickAfter you completed the previous steps, you are ready to run clang tools. If 108e5dd7070Spatrickyou have a recent clang installed, you should have ``clang-check`` in 109e5dd7070Spatrick``$PATH``. Try to run it on any ``.cpp`` file inside the LLVM source tree: 110e5dd7070Spatrick 111e5dd7070Spatrick.. code-block:: console 112e5dd7070Spatrick 113e5dd7070Spatrick $ clang-check tools/clang/lib/Tooling/CompilationDatabase.cpp 114e5dd7070Spatrick 115e5dd7070SpatrickIf you're using vim, it's convenient to have clang-check integrated. Put 116e5dd7070Spatrickthis into your ``.vimrc``: 117e5dd7070Spatrick 118e5dd7070Spatrick:: 119e5dd7070Spatrick 120e5dd7070Spatrick function! ClangCheckImpl(cmd) 121e5dd7070Spatrick if &autowrite | wall | endif 122e5dd7070Spatrick echo "Running " . a:cmd . " ..." 123e5dd7070Spatrick let l:output = system(a:cmd) 124e5dd7070Spatrick cexpr l:output 125e5dd7070Spatrick cwindow 126e5dd7070Spatrick let w:quickfix_title = a:cmd 127e5dd7070Spatrick if v:shell_error != 0 128e5dd7070Spatrick cc 129e5dd7070Spatrick endif 130e5dd7070Spatrick let g:clang_check_last_cmd = a:cmd 131e5dd7070Spatrick endfunction 132e5dd7070Spatrick 133e5dd7070Spatrick function! ClangCheck() 134e5dd7070Spatrick let l:filename = expand('%') 135e5dd7070Spatrick if l:filename =~ '\.\(cpp\|cxx\|cc\|c\)$' 136e5dd7070Spatrick call ClangCheckImpl("clang-check " . l:filename) 137e5dd7070Spatrick elseif exists("g:clang_check_last_cmd") 138e5dd7070Spatrick call ClangCheckImpl(g:clang_check_last_cmd) 139e5dd7070Spatrick else 140e5dd7070Spatrick echo "Can't detect file's compilation arguments and no previous clang-check invocation!" 141e5dd7070Spatrick endif 142e5dd7070Spatrick endfunction 143e5dd7070Spatrick 144e5dd7070Spatrick nmap <silent> <F5> :call ClangCheck()<CR><CR> 145e5dd7070Spatrick 146e5dd7070SpatrickWhen editing a .cpp/.cxx/.cc/.c file, hit F5 to reparse the file. In 147e5dd7070Spatrickcase the current file has a different extension (for example, .h), F5 148e5dd7070Spatrickwill re-run the last clang-check invocation made from this vim instance 149e5dd7070Spatrick(if any). The output will go into the error window, which is opened 150e5dd7070Spatrickautomatically when clang-check finds errors, and can be re-opened with 151e5dd7070Spatrick``:cope``. 152e5dd7070Spatrick 153e5dd7070SpatrickOther ``clang-check`` options that can be useful when working with clang 154e5dd7070SpatrickAST: 155e5dd7070Spatrick 156e5dd7070Spatrick* ``-ast-print`` --- Build ASTs and then pretty-print them. 157e5dd7070Spatrick* ``-ast-dump`` --- Build ASTs and then debug dump them. 158e5dd7070Spatrick* ``-ast-dump-filter=<string>`` --- Use with ``-ast-dump`` or ``-ast-print`` to 159e5dd7070Spatrick dump/print only AST declaration nodes having a certain substring in a 160e5dd7070Spatrick qualified name. Use ``-ast-list`` to list all filterable declaration node 161e5dd7070Spatrick names. 162e5dd7070Spatrick* ``-ast-list`` --- Build ASTs and print the list of declaration node qualified 163e5dd7070Spatrick names. 164e5dd7070Spatrick 165e5dd7070SpatrickExamples: 166e5dd7070Spatrick 167e5dd7070Spatrick.. code-block:: console 168e5dd7070Spatrick 169e5dd7070Spatrick $ clang-check tools/clang/tools/clang-check/ClangCheck.cpp -ast-dump -ast-dump-filter ActionFactory::newASTConsumer 170e5dd7070Spatrick Processing: tools/clang/tools/clang-check/ClangCheck.cpp. 171e5dd7070Spatrick Dumping ::ActionFactory::newASTConsumer: 172e5dd7070Spatrick clang::ASTConsumer *newASTConsumer() (CompoundStmt 0x44da290 </home/alexfh/local/llvm/tools/clang/tools/clang-check/ClangCheck.cpp:64:40, line:72:3> 173e5dd7070Spatrick (IfStmt 0x44d97c8 <line:65:5, line:66:45> 174e5dd7070Spatrick <<<NULL>>> 175e5dd7070Spatrick (ImplicitCastExpr 0x44d96d0 <line:65:9> '_Bool':'_Bool' <UserDefinedConversion> 176e5dd7070Spatrick ... 177e5dd7070Spatrick $ clang-check tools/clang/tools/clang-check/ClangCheck.cpp -ast-print -ast-dump-filter ActionFactory::newASTConsumer 178e5dd7070Spatrick Processing: tools/clang/tools/clang-check/ClangCheck.cpp. 179e5dd7070Spatrick Printing <anonymous namespace>::ActionFactory::newASTConsumer: 180e5dd7070Spatrick clang::ASTConsumer *newASTConsumer() { 181e5dd7070Spatrick if (this->ASTList.operator _Bool()) 182e5dd7070Spatrick return clang::CreateASTDeclNodeLister(); 183e5dd7070Spatrick if (this->ASTDump.operator _Bool()) 184e5dd7070Spatrick return clang::CreateASTDumper(nullptr /*Dump to stdout.*/, 185e5dd7070Spatrick this->ASTDumpFilter); 186e5dd7070Spatrick if (this->ASTPrint.operator _Bool()) 187e5dd7070Spatrick return clang::CreateASTPrinter(&llvm::outs(), this->ASTDumpFilter); 188e5dd7070Spatrick return new clang::ASTConsumer(); 189e5dd7070Spatrick } 190e5dd7070Spatrick 191ec727ea7SpatrickUsing Ninja Build System 192e5dd7070Spatrick======================================= 193e5dd7070Spatrick 194*12c85518SrobertOptionally you can use the `Ninja`_ build system instead of make. It is 195*12c85518Srobertaimed at making your builds faster. Currently this step will require 196*12c85518Srobertbuilding Ninja from sources. 197e5dd7070Spatrick 198e5dd7070SpatrickTo take advantage of using Clang Tools along with Ninja build you need 199e5dd7070Spatrickat least CMake 2.8.9. 200e5dd7070Spatrick 201e5dd7070SpatrickClone the Ninja git repository and build Ninja from sources: 202e5dd7070Spatrick 203e5dd7070Spatrick.. code-block:: console 204e5dd7070Spatrick 205e5dd7070Spatrick $ git clone git://github.com/martine/ninja.git 206e5dd7070Spatrick $ cd ninja/ 207e5dd7070Spatrick $ ./bootstrap.py 208e5dd7070Spatrick 209e5dd7070SpatrickThis will result in a single binary ``ninja`` in the current directory. 210e5dd7070SpatrickIt doesn't require installation and can just be copied to any location 211e5dd7070Spatrickinside ``$PATH``, say ``/usr/local/bin/``: 212e5dd7070Spatrick 213e5dd7070Spatrick.. code-block:: console 214e5dd7070Spatrick 215e5dd7070Spatrick $ sudo cp ninja /usr/local/bin/ 216e5dd7070Spatrick $ sudo chmod a+rx /usr/local/bin/ninja 217e5dd7070Spatrick 218e5dd7070SpatrickAfter doing all of this, you'll need to generate Ninja build files for 219e5dd7070SpatrickLLVM with CMake. You need to make a build directory and run CMake from 220e5dd7070Spatrickit: 221e5dd7070Spatrick 222e5dd7070Spatrick.. code-block:: console 223e5dd7070Spatrick 224e5dd7070Spatrick $ mkdir your/build/directory 225e5dd7070Spatrick $ cd your/build/directory 226e5dd7070Spatrick $ cmake -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources 227e5dd7070Spatrick 228e5dd7070SpatrickIf you want to use clang instead of GCC, you can add 229e5dd7070Spatrick``-DCMAKE_C_COMPILER=/path/to/clang -DCMAKE_CXX_COMPILER=/path/to/clang++``. 230e5dd7070SpatrickYou can also use ``ccmake``, which provides a curses interface to configure 231e5dd7070SpatrickCMake variables in an interactive manner. 232e5dd7070Spatrick 233e5dd7070SpatrickAs a result, the new ``compile_commands.json`` file should appear in the 234e5dd7070Spatrickcurrent directory. You should link it to the LLVM source tree so that 235e5dd7070SpatrickClang Tooling is able to use it: 236e5dd7070Spatrick 237e5dd7070Spatrick.. code-block:: console 238e5dd7070Spatrick 239e5dd7070Spatrick $ ln -s $PWD/compile_commands.json path/to/llvm/source/ 240e5dd7070Spatrick 241e5dd7070SpatrickNow you are ready to build and test LLVM using Ninja: 242e5dd7070Spatrick 243e5dd7070Spatrick.. code-block:: console 244e5dd7070Spatrick 245e5dd7070Spatrick $ ninja check-all 246e5dd7070Spatrick 247e5dd7070SpatrickOther target names can be used in the same way as with make. 248*12c85518Srobert 249*12c85518Srobert.. _Ninja: https://ninja-build.org/ 250