[clang-tidy] Describe modules, link to LLVM development docs, other minor updates

llvm-svn: 261431
This commit is contained in:
Alexander Kornienko 2016-02-20 11:42:13 +00:00
parent 36894dcfed
commit 10f3cf54cb
1 changed files with 46 additions and 11 deletions

View File

@ -328,10 +328,38 @@ Writing a clang-tidy Check
So you have an idea of a useful check for :program:`clang-tidy`.
You need to decide which module the check belongs to. If the check verifies
conformance of the code to a certain coding style, it probably deserves a
separate module and a directory in ``clang-tidy/`` (there are LLVM and Google
modules already).
First, if you're not familiar with LLVM development, read through the `Getting
Started with LLVM`_ document for instructions on setting up your workflow and
the `LLVM Coding Standards`_ document to familiarize yourself with the coding
style used in the project. For code reviews we mostly use `LLVM Phabricator`_.
.. _Getting Started with LLVM: http://llvm.org/docs/GettingStarted.html
.. _LLVM Coding Standards: http://llvm.org/docs/CodingStandards.html
.. _LLVM Phabricator: http://llvm.org/docs/Phabricator.html
Next, you need to decide which module the check belongs to. If the check
verifies conformance of the code to a certain coding style, it probably deserves
a separate module and a directory in ``clang-tidy/``. There are already modules
implementing checks related to:
* `C++ Core Guidelines
<http://reviews.llvm.org/diffusion/L/browse/clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/>`_
* `CERT Secure Coding Standards
<http://reviews.llvm.org/diffusion/L/browse/clang-tools-extra/trunk/clang-tidy/cert/>`_
* `Google Style Guide
<http://reviews.llvm.org/diffusion/L/browse/clang-tools-extra/trunk/clang-tidy/google/>`_
* `LLVM Style
<http://reviews.llvm.org/diffusion/L/browse/clang-tools-extra/trunk/clang-tidy/llvm/>`_
* `modernizing C/C++ code
<http://reviews.llvm.org/diffusion/L/browse/clang-tools-extra/trunk/clang-tidy/modernize/>`_
* potential `performance problems
<http://reviews.llvm.org/diffusion/L/browse/clang-tools-extra/trunk/clang-tidy/performance/>`_
* various `readability issues
<http://reviews.llvm.org/diffusion/L/browse/clang-tools-extra/trunk/clang-tidy/readability/>`_
* and `miscellaneous checks
<http://reviews.llvm.org/diffusion/L/browse/clang-tools-extra/trunk/clang-tidy/misc/>`_
that we couldn't find a better category for.
After choosing the module, you need to create a class for your check:
@ -341,13 +369,21 @@ After choosing the module, you need to create a class for your check:
namespace clang {
namespace tidy {
namespace some_module {
class MyCheck : public ClangTidyCheck {
public:
MyCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
};
} // namespace some_module
} // namespace tidy
} // namespace clang
Constructor of the check receives the ``Name`` and ``Context`` parameters, and
must forward them to the ``ClangTidyCheck`` constructor.
Next, you need to decide whether it should operate on the preprocessor level or
on the AST level. Let's imagine that we need to work with the AST in our check.
In this case we need to override two methods:
@ -363,9 +399,6 @@ In this case we need to override two methods:
void check(ast_matchers::MatchFinder::MatchResult &Result) override;
};
Constructor of the check receives the ``Name`` and ``Context`` parameters, and
must forward them to the ``ClangTidyCheck`` constructor.
In the ``registerMatchers`` method we create an AST Matcher (see `AST Matchers`_
for more information) that will find the pattern in the AST that we want to
inspect. The results of the matching are passed to the ``check`` method, which
@ -380,8 +413,7 @@ can further inspect them and report diagnostics.
}
void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
const CXXConstructorDecl *Ctor =
Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
// Do not be confused: isExplicit means 'explicit' keyword is present,
// isImplicit means that it's a compiler-generated constructor.
if (Ctor->isOutOfLine() || Ctor->isExplicit() || Ctor->isImplicit())
@ -394,7 +426,10 @@ can further inspect them and report diagnostics.
}
(The full code for this check resides in
``clang-tidy/google/ExplicitConstructorCheck.{h,cpp}``).
`clang-tidy/google/ExplicitConstructorCheck.h
<http://reviews.llvm.org/diffusion/L/browse/clang-tools-extra/trunk/clang-tidy/google/ExplicitConstructorCheck.h>`_
and `clang-tidy/google/ExplicitConstructorCheck.cpp
<http://reviews.llvm.org/diffusion/L/browse/clang-tools-extra/trunk/clang-tidy/google/ExplicitConstructorCheck.cpp>`_).
Registering your Check
@ -531,7 +566,7 @@ Running clang-tidy on LLVM
--------------------------
To test a check it's best to try it out on a larger code base. LLVM and Clang
are the natural targets as you already have the source around. The most
are the natural targets as you already have the source code around. The most
convenient way to run :program:`clang-tidy` is with a compile command database;
CMake can automatically generate one, for a description of how to enable it see
`How To Setup Tooling For LLVM`_. Once ``compile_commands.json`` is in place and