2016-07-14 17:46:07 +08:00
|
|
|
============
|
|
|
|
Clang-Rename
|
|
|
|
============
|
|
|
|
|
|
|
|
.. contents::
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
.. toctree::
|
|
|
|
:maxdepth: 1
|
|
|
|
|
|
|
|
|
2016-08-04 02:15:07 +08:00
|
|
|
:program:`clang-rename` is a C++ refactoring tool. Its purpose is to perform
|
|
|
|
efficient renaming actions in large-scale projects such as renaming classes,
|
|
|
|
functions, variables, arguments, namespaces etc.
|
2016-07-14 17:46:07 +08:00
|
|
|
|
|
|
|
The tool is in a very early development stage, so you might encounter bugs and
|
|
|
|
crashes. Submitting reports with information about how to reproduce the issue
|
|
|
|
to `the LLVM bugtracker <https://llvm.org/bugs>`_ will definitely help the
|
|
|
|
project. If you have any ideas or suggestions, you might want to put a feature
|
|
|
|
request there.
|
|
|
|
|
2016-08-02 16:51:26 +08:00
|
|
|
Using Clang-Rename
|
2016-07-14 17:46:07 +08:00
|
|
|
==================
|
|
|
|
|
|
|
|
:program:`clang-rename` is a `LibTooling
|
2019-01-23 03:19:48 +08:00
|
|
|
<https://clang.llvm.org/docs/LibTooling.html>`_-based tool, and it's easier to
|
2016-07-14 17:46:07 +08:00
|
|
|
work with if you set up a compile command database for your project (for an
|
|
|
|
example of how to do this see `How To Setup Tooling For LLVM
|
2019-01-23 03:19:48 +08:00
|
|
|
<https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html>`_). You can also
|
2016-08-02 16:51:26 +08:00
|
|
|
specify compilation options on the command line after `--`:
|
2016-07-14 17:46:07 +08:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
$ clang-rename -offset=42 -new-name=foo test.cpp -- -Imy_project/include -DMY_DEFINES ...
|
|
|
|
|
|
|
|
|
|
|
|
To get an offset of a symbol in a file run
|
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
|
|
|
$ grep -FUbo 'foo' file.cpp
|
|
|
|
|
|
|
|
|
2016-08-10 15:13:29 +08:00
|
|
|
The tool currently supports renaming actions inside a single translation unit
|
2016-08-10 02:20:41 +08:00
|
|
|
only. It is planned to extend the tool's functionality to support multi-TU
|
|
|
|
renaming actions in the future.
|
|
|
|
|
|
|
|
:program:`clang-rename` also aims to be easily integrated into popular text
|
|
|
|
editors, such as Vim and Emacs, and improve the workflow of users.
|
|
|
|
|
|
|
|
Although a command line interface exists, it is highly recommended to use the
|
|
|
|
text editor interface instead for better experience.
|
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
You can also identify one or more symbols to be renamed by giving the fully
|
|
|
|
qualified name:
|
2016-08-02 17:51:31 +08:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
$ clang-rename -qualified-name=foo -new-name=bar test.cpp
|
2016-08-02 17:51:31 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
Renaming multiple symbols at once is supported, too. However,
|
|
|
|
:program:`clang-rename` doesn't accept both `-offset` and `-qualified-name` at
|
|
|
|
the same time. So, you can either specify multiple `-offset` or
|
|
|
|
`-qualified-name`.
|
2016-08-02 17:51:31 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
.. code-block:: console
|
2016-08-10 02:20:41 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
$ clang-rename -offset=42 -new-name=bar1 -offset=150 -new-name=bar2 test.cpp
|
2016-08-10 02:20:41 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
or
|
2016-08-10 02:20:41 +08:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
$ clang-rename -qualified-name=foo1 -new-name=bar1 -qualified-name=foo2 -new-name=bar2 test.cpp
|
2016-08-10 02:20:41 +08:00
|
|
|
|
2016-09-14 21:23:14 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
Alternatively, {offset | qualified-name} / new-name pairs can be put into a YAML
|
|
|
|
file:
|
2016-09-14 21:23:14 +08:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
---
|
|
|
|
- Offset: 42
|
2016-09-16 16:45:19 +08:00
|
|
|
NewName: bar1
|
|
|
|
- Offset: 150
|
|
|
|
NewName: bar2
|
2016-09-14 21:23:14 +08:00
|
|
|
...
|
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
or
|
2016-09-14 21:23:14 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
.. code-block:: yaml
|
2016-09-14 21:00:36 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
---
|
|
|
|
- QualifiedName: foo1
|
|
|
|
NewName: bar1
|
|
|
|
- QualifiedName: foo2
|
|
|
|
NewName: bar2
|
|
|
|
...
|
2016-09-14 21:00:36 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
That way you can avoid spelling out all the names as command line arguments:
|
2016-09-14 21:23:14 +08:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
$ clang-rename -input=test.yaml test.cpp
|
2016-09-14 21:23:14 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
:program:`clang-rename` offers the following options:
|
2016-09-14 21:23:14 +08:00
|
|
|
|
|
|
|
.. code-block:: console
|
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
$ clang-rename --help
|
|
|
|
USAGE: clang-rename [subcommand] [options] <source0> [... <sourceN>]
|
2016-09-14 21:23:14 +08:00
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
|
|
|
|
Generic Options:
|
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
-help - Display available options (-help-hidden for more)
|
|
|
|
-help-list - Display list of available options (-help-list-hidden for more)
|
|
|
|
-version - Display the version of this program
|
2016-09-14 21:23:14 +08:00
|
|
|
|
2016-09-16 16:45:19 +08:00
|
|
|
clang-rename common options:
|
2016-09-14 21:23:14 +08:00
|
|
|
|
|
|
|
-export-fixes=<filename> - YAML file to store suggested fixes in.
|
2016-09-16 16:45:19 +08:00
|
|
|
-extra-arg=<string> - Additional argument to append to the compiler command line
|
2019-12-24 20:06:24 +08:00
|
|
|
Can be used several times.
|
2016-09-16 16:45:19 +08:00
|
|
|
-extra-arg-before=<string> - Additional argument to prepend to the compiler command line
|
2019-12-24 20:06:24 +08:00
|
|
|
Can be used several times.
|
2017-06-02 17:32:28 +08:00
|
|
|
-force - Ignore nonexistent qualified names.
|
2016-09-14 21:23:14 +08:00
|
|
|
-i - Overwrite edited <file>s.
|
|
|
|
-input=<string> - YAML file to load oldname-newname pairs from.
|
|
|
|
-new-name=<string> - The new name to change the symbol to.
|
|
|
|
-offset=<uint> - Locates the symbol by offset as opposed to <line>:<column>.
|
2016-09-16 16:45:19 +08:00
|
|
|
-p=<string> - Build path
|
|
|
|
-pl - Print the locations affected by renaming to stderr.
|
|
|
|
-pn - Print the found symbol's name prior to renaming to stderr.
|
|
|
|
-qualified-name=<string> - The fully qualified name of the symbol.
|
2016-07-14 17:46:07 +08:00
|
|
|
|
2016-08-02 16:51:26 +08:00
|
|
|
Vim Integration
|
|
|
|
===============
|
2016-07-14 17:46:07 +08:00
|
|
|
|
|
|
|
You can call :program:`clang-rename` directly from Vim! To set up
|
|
|
|
:program:`clang-rename` integration for Vim see
|
2019-01-30 00:37:27 +08:00
|
|
|
`clang/tools/clang-rename/clang-rename.py
|
|
|
|
<https://github.com/llvm/llvm-project/blob/master/clang/tools/clang-rename/clang-rename.py>`_.
|
2016-07-14 17:46:07 +08:00
|
|
|
|
|
|
|
Please note that **you have to save all buffers, in which the replacement will
|
|
|
|
happen before running the tool**.
|
2016-07-27 22:23:47 +08:00
|
|
|
|
|
|
|
Once installed, you can point your cursor to symbols you want to rename, press
|
2016-08-03 04:29:47 +08:00
|
|
|
`<leader>cr` and type new desired name. The `<leader> key
|
|
|
|
<http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_3)#Map_leader>`_
|
2016-07-27 22:26:03 +08:00
|
|
|
is a reference to a specific key defined by the mapleader variable and is bound
|
2016-07-27 22:23:47 +08:00
|
|
|
to backslash by default.
|
2016-08-02 16:51:26 +08:00
|
|
|
|
|
|
|
Emacs Integration
|
|
|
|
=================
|
|
|
|
|
|
|
|
You can also use :program:`clang-rename` while using Emacs! To set up
|
|
|
|
:program:`clang-rename` integration for Emacs see
|
|
|
|
`clang-rename/tool/clang-rename.el
|
2019-01-30 00:37:27 +08:00
|
|
|
<https://github.com/llvm/llvm-project/blob/master/clang/tools/clang-rename/clang-rename.el>`_.
|
2016-08-02 16:51:26 +08:00
|
|
|
|
|
|
|
Once installed, you can point your cursor to symbols you want to rename, press
|
|
|
|
`M-X`, type `clang-rename` and new desired name.
|
|
|
|
|
|
|
|
Please note that **you have to save all buffers, in which the replacement will
|
|
|
|
happen before running the tool**.
|