@@ -19,11 +19,11 @@ check, the rest of this document explains how to do this.
19
19
20
20
There are a few tools particularly useful when developing clang-tidy checks:
21
21
* ``add_new_check.py `` is a script to automate the process of adding a new
22
- check, it will create the check, update the CMake file and create a test;
22
+ check; it will create the check, update the CMake file and create a test.
23
23
* ``rename_check.py `` does what the script name suggests, renames an existing
24
- check;
24
+ check.
25
25
* :program: `pp-trace ` logs method calls on `PPCallbacks ` for a source file
26
- and is invaluable in understanding the preprocessor mechanism;
26
+ and is invaluable in understanding the preprocessor mechanism.
27
27
* :program: `clang-query ` is invaluable for interactive prototyping of AST
28
28
matchers and exploration of the Clang AST;
29
29
* `clang-check `_ with the ``-ast-dump `` (and optionally ``-ast-dump-filter ``)
@@ -47,7 +47,7 @@ implemented as a:
47
47
48
48
+ *Clang diagnostic *: if the check is generic enough, targets code patterns that
49
49
most probably are bugs (rather than style or readability issues), can be
50
- implemented effectively and with extremely low false positive rate, it may
50
+ implemented effectively and with extremely low false- positive rate, it may
51
51
make a good Clang diagnostic.
52
52
53
53
+ *Clang static analyzer check *: if the check requires some sort of control flow
@@ -77,7 +77,7 @@ make sure that you enable the ``clang`` and ``clang-tools-extra`` projects to
77
77
build :program: `clang-tidy `.
78
78
Because your new check will have associated documentation, you will also want to install
79
79
`Sphinx <https://www.sphinx-doc.org/en/master/ >`_ and enable it in the CMake configuration.
80
- To save build time of the core Clang libraries you may want to only enable the ``X86 ``
80
+ To save build time of the core Clang libraries, you may want to only enable the ``X86 ``
81
81
target in the CMake configuration.
82
82
83
83
@@ -130,7 +130,7 @@ So you have an idea of a useful check for :program:`clang-tidy`.
130
130
First, if you're not familiar with LLVM development, read through the `Getting Started
131
131
with the LLVM System `_ document for instructions on setting up your workflow and
132
132
the `LLVM Coding Standards `_ document to familiarize yourself with the coding
133
- style used in the project. For code reviews we currently use `LLVM Github `_,
133
+ style used in the project. For code reviews, we currently use `LLVM Github `_,
134
134
though historically we used Phabricator.
135
135
136
136
.. _Getting Started with the LLVM System : https://llvm.org/docs/GettingStarted.html
@@ -141,7 +141,7 @@ Next, you need to decide which module the check belongs to. Modules
141
141
are located in subdirectories of `clang-tidy/
142
142
<https://github.com/llvm/llvm-project/tree/main/clang-tools-extra/clang-tidy/> `_
143
143
and contain checks targeting a certain aspect of code quality (performance,
144
- readability, etc.), certain coding style or standard (Google, LLVM, CERT, etc.)
144
+ readability, etc.), a certain coding style or standard (Google, LLVM, CERT, etc.)
145
145
or a widely used API (e.g. MPI). Their names are the same as the user-facing
146
146
check group names described :ref: `above <checks-groups-table >`.
147
147
@@ -166,7 +166,7 @@ The ``add_new_check.py`` script will:
166
166
* create a documentation file and include it into the
167
167
``docs/clang-tidy/checks/list.rst ``.
168
168
169
- Let's see in more detail at the check class definition:
169
+ Let's look at the check class definition in more detail :
170
170
171
171
.. code-block :: c++
172
172
@@ -200,7 +200,7 @@ In our case the check needs to operate on the AST level and it overrides the
200
200
preprocessor level, we'd need instead to override the ``registerPPCallbacks ``
201
201
method.
202
202
203
- In the ``registerMatchers `` method we create an AST Matcher (see `AST Matchers `_
203
+ In the ``registerMatchers `` method, we create an AST Matcher (see `AST Matchers `_
204
204
for more information) that will find the pattern in the AST that we want to
205
205
inspect. The results of the matching are passed to the ``check `` method, which
206
206
can further inspect them and report diagnostics.
@@ -320,7 +320,7 @@ the ``add_new_check.py`` script:
320
320
Developing your check incrementally
321
321
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
322
322
323
- The best way to develop your check is to start with the simple test cases and increase
323
+ The best way to develop your check is to start with simple test cases and increase
324
324
complexity incrementally. The test file created by the ``add_new_check.py `` script is
325
325
a starting point for your test cases. A rough outline of the process looks like this:
326
326
@@ -393,7 +393,7 @@ good way to catch things you forgot to account for in your matchers. However, t
393
393
LLVM code base may be insufficient for testing purposes as it was developed against a
394
394
particular set of coding styles and quality measures. The larger the corpus of code
395
395
the check is tested against, the higher confidence the community will have in the
396
- check's efficacy and false positive rate.
396
+ check's efficacy and false- positive rate.
397
397
398
398
Some suggestions to ensure your check is robust:
399
399
@@ -406,10 +406,10 @@ Some suggestions to ensure your check is robust:
406
406
- Define template classes that contain code matched by your check.
407
407
- Define template specializations that contain code matched by your check.
408
408
- Test your check under both Windows and Linux environments.
409
- - Watch out for high false positive rates. Ideally, a check would have no false
409
+ - Watch out for high false- positive rates. Ideally, a check would have no false
410
410
positives, but given that matching against an AST is not control- or data flow-
411
- sensitive, a number of false positives are expected. The higher the false
412
- positive rate, the less likely the check will be adopted in practice.
411
+ sensitive, a number of false positives are expected. The higher the
412
+ false- positive rate, the less likely the check will be adopted in practice.
413
413
Mechanisms should be put in place to help the user manage false positives.
414
414
- There are two primary mechanisms for managing false positives: supporting a
415
415
code pattern which allows the programmer to silence the diagnostic in an ad
@@ -428,10 +428,10 @@ Documenting your check
428
428
The ``add_new_check.py `` script creates entries in the
429
429
`release notes <https://clang.llvm.org/extra/ReleaseNotes.html >`_, the list of
430
430
checks and a new file for the check documentation itself. It is recommended that you
431
- have a concise summation of what your check does in a single sentence that is repeated
431
+ have a concise summary of what your check does in a single sentence that is repeated
432
432
in the release notes, as the first sentence in the doxygen comments in the header file
433
433
for your check class and as the first sentence of the check documentation. Avoid the
434
- phrase "this check" in your check summation and check documentation.
434
+ phrase "this check" in your check summary and check documentation.
435
435
436
436
If your check relates to a published coding guideline (C++ Core Guidelines, MISRA, etc.)
437
437
or style guide, provide links to the relevant guideline or style guide sections in your
@@ -443,10 +443,10 @@ If there are exceptions or limitations to your check, document them thoroughly.
443
443
will help users understand the scope of the diagnostics and fix-its provided by the check.
444
444
445
445
Building the target ``docs-clang-tools-html `` will run the Sphinx documentation generator
446
- and create documentation HTML files in the tools/clang/tools/extra/docs/html directory in
446
+ and create HTML documentation files in the tools/clang/tools/extra/docs/html directory in
447
447
your build tree. Make sure that your check is correctly shown in the release notes and the
448
448
list of checks. Make sure that the formatting and structure of your check's documentation
449
- looks correct.
449
+ look correct.
450
450
451
451
452
452
Registering your Check
@@ -503,11 +503,11 @@ Configuring Checks
503
503
504
504
If a check needs configuration options, it can access check-specific options
505
505
using the ``Options.get<Type>("SomeOption", DefaultValue) `` call in the check
506
- constructor. In this case the check should also override the
506
+ constructor. In this case, the check should also override the
507
507
``ClangTidyCheck::storeOptions `` method to make the options provided by the
508
508
check discoverable. This method lets :program: `clang-tidy ` know which options
509
509
the check implements and what the current values are (e.g. for the
510
- ``-dump-config `` command line option).
510
+ ``-dump-config `` command- line option).
511
511
512
512
.. code-block :: c++
513
513
@@ -576,7 +576,7 @@ typically the basic ``CHECK`` forms (``CHECK-MESSAGES`` and ``CHECK-FIXES``)
576
576
are sufficient for clang-tidy tests. Note that the `FileCheck `_
577
577
documentation mostly assumes the default prefix (``CHECK ``), and hence
578
578
describes the directive as ``CHECK: ``, ``CHECK-SAME: ``, ``CHECK-NOT: ``, etc.
579
- Replace ``CHECK `` by either ``CHECK-FIXES `` or ``CHECK-MESSAGES `` for
579
+ Replace ``CHECK `` with either ``CHECK-FIXES `` or ``CHECK-MESSAGES `` for
580
580
clang-tidy tests.
581
581
582
582
An additional check enabled by ``check_clang_tidy.py `` ensures that
@@ -590,7 +590,7 @@ appropriate ``RUN`` line in the ``test/clang-tidy`` directory. Use
590
590
diagnostic messages and fixed code.
591
591
592
592
It's advised to make the checks as specific as possible to avoid checks matching
593
- to incorrect parts of the input. Use ``[[@LINE+X]] ``/``[[@LINE-X]] ``
593
+ incorrect parts of the input. Use ``[[@LINE+X]] ``/``[[@LINE-X]] ``
594
594
substitutions and distinct function and variable names in the test code.
595
595
596
596
Here's an example of a test using the ``check_clang_tidy.py `` script (the full
@@ -606,7 +606,7 @@ source code is at `test/clang-tidy/checkers/google/readability-casting.cpp`_):
606
606
// CHECK-FIXES: int b = a;
607
607
}
608
608
609
- To check more than one scenario in the same test file use
609
+ To check more than one scenario in the same test file, use
610
610
``-check-suffix=SUFFIX-NAME `` on ``check_clang_tidy.py `` command line or
611
611
``-check-suffixes=SUFFIX-NAME-1,SUFFIX-NAME-2,... ``.
612
612
With ``-check-suffix[es]=SUFFIX-NAME `` you need to replace your ``CHECK-* ``
@@ -631,15 +631,15 @@ There are many dark corners in the C++ language, and it may be difficult to make
631
631
your check work perfectly in all cases, especially if it issues fix-it hints. The
632
632
most frequent pitfalls are macros and templates:
633
633
634
- 1. code written in a macro body/template definition may have a different meaning
635
- depending on the macro expansion/template instantiation;
636
- 2. multiple macro expansions/template instantiations may result in the same code
634
+ 1. Code written in a macro body/template definition may have a different meaning
635
+ depending on the macro expansion/template instantiation.
636
+ 2. Multiple macro expansions/template instantiations may result in the same code
637
637
being inspected by the check multiple times (possibly, with different
638
638
meanings, see 1), and the same warning (or a slightly different one) may be
639
639
issued by the check multiple times; :program: `clang-tidy ` will deduplicate
640
640
_identical_ warnings, but if the warnings are slightly different, all of them
641
- will be shown to the user (and used for applying fixes, if any);
642
- 3. making replacements to a macro body/template definition may be fine for some
641
+ will be shown to the user (and used for applying fixes, if any).
642
+ 3. Making replacements to a macro body/template definition may be fine for some
643
643
macro expansions/template instantiations, but easily break some other
644
644
expansions/instantiations.
645
645
@@ -667,7 +667,7 @@ code quality and catch potential issues. While :program:`clang-tidy` is not
667
667
currently enforced in CI, following this practice helps maintain code
668
668
consistency and prevent common errors.
669
669
670
- Here's useful command to check your staged changes:
670
+ Here's a useful command to check your staged changes:
671
671
672
672
.. code-block :: console
673
673
@@ -698,7 +698,7 @@ names of the checks to enable.
698
698
699
699
$ clang-tidy --checks=-*,my-explicit-constructor -list-checks -load myplugin.so
700
700
701
- There is no expectations regarding ABI and API stability, so the plugin must be
701
+ There are no expectations regarding ABI and API stability, so the plugin must be
702
702
compiled against the version of clang-tidy that will be loading the plugin.
703
703
704
704
The plugins can use threads, TLS, or any other facilities available to in-tree
@@ -720,10 +720,10 @@ and write a version of `check_clang_tidy.py`_ to suit your needs.
720
720
Running clang-tidy on LLVM
721
721
--------------------------
722
722
723
- To test a check it's best to try it out on a larger code base. LLVM and Clang
723
+ To test a check, it's best to try it out on a larger code base. LLVM and Clang
724
724
are the natural targets as you already have the source code around. The most
725
725
convenient way to run :program: `clang-tidy ` is with a compile command database;
726
- CMake can automatically generate one, for a description of how to enable it see
726
+ CMake can automatically generate one; for a description of how to enable it, see
727
727
`How To Setup Clang Tooling For LLVM `_. Once ``compile_commands.json `` is in
728
728
place and a working version of :program: `clang-tidy ` is in ``PATH `` the entire
729
729
code base can be analyzed with ``clang-tidy/tool/run-clang-tidy.py ``. The script
@@ -735,18 +735,18 @@ warnings and errors. The script provides multiple configuration flags.
735
735
736
736
737
737
* The default set of checks can be overridden using the ``-checks `` argument,
738
- taking the identical format as :program: `clang-tidy ` does. For example
738
+ taking the identical format as :program: `clang-tidy ` does. For example,
739
739
``-checks=-*,modernize-use-override `` will run the ``modernize-use-override ``
740
740
check only.
741
741
742
- * To restrict the files examined you can provide one or more regex arguments
742
+ * To restrict the files examined, you can provide one or more regex arguments
743
743
that the file names are matched against.
744
744
``run-clang-tidy.py clang-tidy/.*Check\.cpp `` will only analyze `clang-tidy `
745
745
checks. It may also be necessary to restrict the header files that warnings
746
746
are displayed from by using the ``-header-filter `` and ``-exclude-header-filter `` flags.
747
747
They have the same behavior as the corresponding :program: `clang-tidy ` flags.
748
748
749
- * To apply suggested fixes ``-fix `` can be passed as an argument. This gathers
749
+ * To apply suggested fixes, ``-fix `` can be passed as an argument. This gathers
750
750
all changes in a temporary directory and applies them. Passing ``-format ``
751
751
will run clang-format over changed lines.
752
752
@@ -795,7 +795,7 @@ There is only one argument that controls profile storage:
795
795
796
796
* ``-store-check-profile=<prefix> ``
797
797
798
- By default reports are printed in tabulated format to stderr. When this option
798
+ By default, reports are printed in tabulated format to stderr. When this option
799
799
is passed, these per-TU profiles are instead stored as JSON.
800
800
If the prefix is not an absolute path, it is considered to be relative to the
801
801
directory from where you have run :program: `clang-tidy `. All ``. `` and ``.. ``
0 commit comments