annotate clang-tools-extra/docs/clang-include-fixer.rst @ 173:0572611fdcc8 llvm10 llvm12

reorgnization done
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 11:55:54 +0900
parents 1d019706d866
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 ===================
anatofuz
parents:
diff changeset
2 Clang-Include-Fixer
anatofuz
parents:
diff changeset
3 ===================
anatofuz
parents:
diff changeset
4
anatofuz
parents:
diff changeset
5 .. contents::
anatofuz
parents:
diff changeset
6
anatofuz
parents:
diff changeset
7 One of the major nuisances of C++ compared to other languages is the manual
anatofuz
parents:
diff changeset
8 management of ``#include`` directives in any file.
anatofuz
parents:
diff changeset
9 :program:`clang-include-fixer` addresses one aspect of this problem by providing
anatofuz
parents:
diff changeset
10 an automated way of adding ``#include`` directives for missing symbols in one
anatofuz
parents:
diff changeset
11 translation unit.
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 While inserting missing ``#include``, :program:`clang-include-fixer` adds
anatofuz
parents:
diff changeset
14 missing namespace qualifiers to all instances of an unidentified symbol if
anatofuz
parents:
diff changeset
15 the symbol is missing some prefix namespace qualifiers.
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 Setup
anatofuz
parents:
diff changeset
18 =====
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 To use :program:`clang-include-fixer` two databases are required. Both can be
anatofuz
parents:
diff changeset
21 generated with existing tools.
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 - Compilation database. Contains the compiler commands for any given file in a
anatofuz
parents:
diff changeset
24 project and can be generated by CMake, see `How To Setup Tooling For LLVM`_.
anatofuz
parents:
diff changeset
25 - Symbol index. Contains all symbol information in a project to match a given
anatofuz
parents:
diff changeset
26 identifier to a header file.
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 Ideally both databases (``compile_commands.json`` and
anatofuz
parents:
diff changeset
29 ``find_all_symbols_db.yaml``) are linked into the root of the source tree they
anatofuz
parents:
diff changeset
30 correspond to. Then the :program:`clang-include-fixer` can automatically pick
anatofuz
parents:
diff changeset
31 them up if called with a source file from that tree. Note that by default
anatofuz
parents:
diff changeset
32 ``compile_commands.json`` as generated by CMake does not include header files,
anatofuz
parents:
diff changeset
33 so only implementation files can be handled by tools.
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 .. _How To Setup Tooling For LLVM: https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
anatofuz
parents:
diff changeset
36
anatofuz
parents:
diff changeset
37 Creating a Symbol Index From a Compilation Database
anatofuz
parents:
diff changeset
38 ---------------------------------------------------
anatofuz
parents:
diff changeset
39
anatofuz
parents:
diff changeset
40 The include fixer contains :program:`find-all-symbols`, a tool to create a
anatofuz
parents:
diff changeset
41 symbol database in YAML format from a compilation database by parsing all
anatofuz
parents:
diff changeset
42 source files listed in it. The following list of commands shows how to set up a
anatofuz
parents:
diff changeset
43 database for LLVM, any project built by CMake should follow similar steps.
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 .. code-block:: console
anatofuz
parents:
diff changeset
46
anatofuz
parents:
diff changeset
47 $ cd path/to/llvm-build
anatofuz
parents:
diff changeset
48 $ ninja find-all-symbols // build find-all-symbols tool.
anatofuz
parents:
diff changeset
49 $ ninja clang-include-fixer // build clang-include-fixer tool.
anatofuz
parents:
diff changeset
50 $ ls compile_commands.json # Make sure compile_commands.json exists.
anatofuz
parents:
diff changeset
51 compile_commands.json
anatofuz
parents:
diff changeset
52 $ path/to/llvm/source/clang-tools-extra/clang-include-fixer/find-all-symbols/tool/run-find-all-symbols.py
anatofuz
parents:
diff changeset
53 ... wait as clang indexes the code base ...
anatofuz
parents:
diff changeset
54 $ ln -s $PWD/find_all_symbols_db.yaml path/to/llvm/source/ # Link database into the source tree.
anatofuz
parents:
diff changeset
55 $ ln -s $PWD/compile_commands.json path/to/llvm/source/ # Also link compilation database if it's not there already.
anatofuz
parents:
diff changeset
56 $ cd path/to/llvm/source
anatofuz
parents:
diff changeset
57 $ /path/to/clang-include-fixer -db=yaml path/to/file/with/missing/include.cpp
anatofuz
parents:
diff changeset
58 Added #include "foo.h"
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 Integrate with Vim
anatofuz
parents:
diff changeset
61 ------------------
anatofuz
parents:
diff changeset
62 To run `clang-include-fixer` on a potentially unsaved buffer in Vim. Add the
anatofuz
parents:
diff changeset
63 following key binding to your ``.vimrc``:
anatofuz
parents:
diff changeset
64
anatofuz
parents:
diff changeset
65 .. code-block:: console
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 noremap <leader>cf :pyf path/to/llvm/source/clang-tools-extra/clang-include-fixer/tool/clang-include-fixer.py<cr>
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 This enables `clang-include-fixer` for NORMAL and VISUAL mode. Change
anatofuz
parents:
diff changeset
70 `<leader>cf` to another binding if you need clang-include-fixer on a different
anatofuz
parents:
diff changeset
71 key. The `<leader> key
anatofuz
parents:
diff changeset
72 <http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_3)#Map_leader>`_
anatofuz
parents:
diff changeset
73 is a reference to a specific key defined by the mapleader variable and is bound
anatofuz
parents:
diff changeset
74 to backslash by default.
anatofuz
parents:
diff changeset
75
anatofuz
parents:
diff changeset
76 Make sure vim can find :program:`clang-include-fixer`:
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 - Add the path to :program:`clang-include-fixer` to the PATH environment variable.
anatofuz
parents:
diff changeset
79 - Or set ``g:clang_include_fixer_path`` in vimrc: ``let g:clang_include_fixer_path=path/to/clang-include-fixer``
anatofuz
parents:
diff changeset
80
anatofuz
parents:
diff changeset
81 You can customize the number of headers being shown by setting
anatofuz
parents:
diff changeset
82 ``let g:clang_include_fixer_maximum_suggested_headers=5``
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 Customized settings in `.vimrc`:
anatofuz
parents:
diff changeset
85
anatofuz
parents:
diff changeset
86 - ``let g:clang_include_fixer_path = "clang-include-fixer"``
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 Set clang-include-fixer binary file path.
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 - ``let g:clang_include_fixer_maximum_suggested_headers = 3``
anatofuz
parents:
diff changeset
91
anatofuz
parents:
diff changeset
92 Set the maximum number of ``#includes`` to show. Default is 3.
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 - ``let g:clang_include_fixer_increment_num = 5``
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 Set the increment number of #includes to show every time when pressing ``m``.
anatofuz
parents:
diff changeset
97 Default is 5.
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 - ``let g:clang_include_fixer_jump_to_include = 0``
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 Set to 1 if you want to jump to the new inserted ``#include`` line. Default is
anatofuz
parents:
diff changeset
102 0.
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 - ``let g:clang_include_fixer_query_mode = 0``
anatofuz
parents:
diff changeset
105
anatofuz
parents:
diff changeset
106 Set to 1 if you want to insert ``#include`` for the symbol under the cursor.
anatofuz
parents:
diff changeset
107 Default is 0. Compared to normal mode, this mode won't parse the source file
anatofuz
parents:
diff changeset
108 and only search the symbol from database, which is faster than normal mode.
anatofuz
parents:
diff changeset
109
anatofuz
parents:
diff changeset
110 See ``clang-include-fixer.py`` for more details.
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 Integrate with Emacs
anatofuz
parents:
diff changeset
113 --------------------
anatofuz
parents:
diff changeset
114 To run `clang-include-fixer` on a potentially unsaved buffer in Emacs.
anatofuz
parents:
diff changeset
115 Ensure that Emacs finds ``clang-include-fixer.el`` by adding the directory
anatofuz
parents:
diff changeset
116 containing the file to the ``load-path`` and requiring the `clang-include-fixer`
anatofuz
parents:
diff changeset
117 in your ``.emacs``:
anatofuz
parents:
diff changeset
118
anatofuz
parents:
diff changeset
119 .. code-block:: console
anatofuz
parents:
diff changeset
120
anatofuz
parents:
diff changeset
121 (add-to-list 'load-path "path/to/llvm/source/clang-tools-extra/clang-include-fixer/tool/"
anatofuz
parents:
diff changeset
122 (require 'clang-include-fixer)
anatofuz
parents:
diff changeset
123
anatofuz
parents:
diff changeset
124 Within Emacs the tool can be invoked with the command
anatofuz
parents:
diff changeset
125 ``M-x clang-include-fixer``. This will insert the header that defines the
anatofuz
parents:
diff changeset
126 first undefined symbol; if there is more than one header that would define the
anatofuz
parents:
diff changeset
127 symbol, the user is prompted to select one.
anatofuz
parents:
diff changeset
128
anatofuz
parents:
diff changeset
129 To include the header that defines the symbol at point, run
anatofuz
parents:
diff changeset
130 ``M-x clang-include-fixer-at-point``.
anatofuz
parents:
diff changeset
131
anatofuz
parents:
diff changeset
132 Make sure Emacs can find :program:`clang-include-fixer`:
anatofuz
parents:
diff changeset
133
anatofuz
parents:
diff changeset
134 - Either add the parent directory of :program:`clang-include-fixer` to the PATH
anatofuz
parents:
diff changeset
135 environment variable, or customize the Emacs user option
anatofuz
parents:
diff changeset
136 ``clang-include-fixer-executable`` to point to the file name of the program.
anatofuz
parents:
diff changeset
137
anatofuz
parents:
diff changeset
138 How it Works
anatofuz
parents:
diff changeset
139 ============
anatofuz
parents:
diff changeset
140
anatofuz
parents:
diff changeset
141 To get the most information out of Clang at parse time,
anatofuz
parents:
diff changeset
142 :program:`clang-include-fixer` runs in tandem with the parse and receives
anatofuz
parents:
diff changeset
143 callbacks from Clang's semantic analysis. In particular it reuses the existing
anatofuz
parents:
diff changeset
144 support for typo corrections. Whenever Clang tries to correct a potential typo
anatofuz
parents:
diff changeset
145 it emits a callback to the include fixer which then looks for a corresponding
anatofuz
parents:
diff changeset
146 file. At this point rich lookup information is still available, which is not
anatofuz
parents:
diff changeset
147 available in the AST at a later stage.
anatofuz
parents:
diff changeset
148
anatofuz
parents:
diff changeset
149 The identifier that should be typo corrected is then sent to the database, if a
anatofuz
parents:
diff changeset
150 header file is returned it is added as an include directive at the top of the
anatofuz
parents:
diff changeset
151 file.
anatofuz
parents:
diff changeset
152
anatofuz
parents:
diff changeset
153 Currently :program:`clang-include-fixer` only inserts a single include at a
anatofuz
parents:
diff changeset
154 time to avoid getting caught in follow-up errors. If multiple `#include`
anatofuz
parents:
diff changeset
155 additions are desired the program can be rerun until a fix-point is reached.