@@ -19,11 +19,11 @@ check, the rest of this document explains how to do this.
1919
2020There are a few tools particularly useful when developing clang-tidy checks:
2121 * ``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.
2323 * ``rename_check.py `` does what the script name suggests, renames an existing
24- check;
24+ check.
2525 * :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.
2727 * :program: `clang-query ` is invaluable for interactive prototyping of AST
2828 matchers and exploration of the Clang AST;
2929 * `clang-check `_ with the ``-ast-dump `` (and optionally ``-ast-dump-filter ``)
@@ -47,7 +47,7 @@ implemented as a:
4747
4848+ *Clang diagnostic *: if the check is generic enough, targets code patterns that
4949 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
5151 make a good Clang diagnostic.
5252
5353+ *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
7777build :program: `clang-tidy `.
7878Because your new check will have associated documentation, you will also want to install
7979`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 ``
8181target in the CMake configuration.
8282
8383
@@ -130,7 +130,7 @@ So you have an idea of a useful check for :program:`clang-tidy`.
130130First, if you're not familiar with LLVM development, read through the `Getting Started
131131with the LLVM System `_ document for instructions on setting up your workflow and
132132the `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 `_,
134134though historically we used Phabricator.
135135
136136.. _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
141141are located in subdirectories of `clang-tidy/
142142<https://github.com/llvm/llvm-project/tree/main/clang-tools-extra/clang-tidy/> `_
143143and 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.)
145145or a widely used API (e.g. MPI). Their names are the same as the user-facing
146146check group names described :ref: `above <checks-groups-table >`.
147147
@@ -166,7 +166,7 @@ The ``add_new_check.py`` script will:
166166 * create a documentation file and include it into the
167167 ``docs/clang-tidy/checks/list.rst ``.
168168
169- Let's see in more detail at the check class definition:
169+ Let's look at the check class definition in more detail :
170170
171171.. code-block :: c++
172172
@@ -200,7 +200,7 @@ In our case the check needs to operate on the AST level and it overrides the
200200preprocessor level, we'd need instead to override the ``registerPPCallbacks ``
201201method.
202202
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 `_
204204for more information) that will find the pattern in the AST that we want to
205205inspect. The results of the matching are passed to the ``check `` method, which
206206can further inspect them and report diagnostics.
@@ -320,7 +320,7 @@ the ``add_new_check.py`` script:
320320Developing your check incrementally
321321^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
322322
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
324324complexity incrementally. The test file created by the ``add_new_check.py `` script is
325325a starting point for your test cases. A rough outline of the process looks like this:
326326
@@ -393,7 +393,7 @@ good way to catch things you forgot to account for in your matchers. However, t
393393LLVM code base may be insufficient for testing purposes as it was developed against a
394394particular set of coding styles and quality measures. The larger the corpus of code
395395the 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.
397397
398398Some suggestions to ensure your check is robust:
399399
@@ -406,10 +406,10 @@ Some suggestions to ensure your check is robust:
406406- Define template classes that contain code matched by your check.
407407- Define template specializations that contain code matched by your check.
408408- 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
410410 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.
413413 Mechanisms should be put in place to help the user manage false positives.
414414- There are two primary mechanisms for managing false positives: supporting a
415415 code pattern which allows the programmer to silence the diagnostic in an ad
@@ -428,10 +428,10 @@ Documenting your check
428428The ``add_new_check.py `` script creates entries in the
429429`release notes <https://clang.llvm.org/extra/ReleaseNotes.html >`_, the list of
430430checks 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
432432in the release notes, as the first sentence in the doxygen comments in the header file
433433for 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.
435435
436436If your check relates to a published coding guideline (C++ Core Guidelines, MISRA, etc.)
437437or 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.
443443will help users understand the scope of the diagnostics and fix-its provided by the check.
444444
445445Building 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
447447your build tree. Make sure that your check is correctly shown in the release notes and the
448448list of checks. Make sure that the formatting and structure of your check's documentation
449- looks correct.
449+ look correct.
450450
451451
452452Registering your Check
@@ -503,11 +503,11 @@ Configuring Checks
503503
504504If a check needs configuration options, it can access check-specific options
505505using 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
507507``ClangTidyCheck::storeOptions `` method to make the options provided by the
508508check discoverable. This method lets :program: `clang-tidy ` know which options
509509the check implements and what the current values are (e.g. for the
510- ``-dump-config `` command line option).
510+ ``-dump-config `` command- line option).
511511
512512.. code-block :: c++
513513
@@ -576,7 +576,7 @@ typically the basic ``CHECK`` forms (``CHECK-MESSAGES`` and ``CHECK-FIXES``)
576576are sufficient for clang-tidy tests. Note that the `FileCheck `_
577577documentation mostly assumes the default prefix (``CHECK ``), and hence
578578describes 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
580580clang-tidy tests.
581581
582582An additional check enabled by ``check_clang_tidy.py `` ensures that
@@ -590,7 +590,7 @@ appropriate ``RUN`` line in the ``test/clang-tidy`` directory. Use
590590diagnostic messages and fixed code.
591591
592592It'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]] ``
594594substitutions and distinct function and variable names in the test code.
595595
596596Here'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`_):
606606 // CHECK-FIXES: int b = a;
607607 }
608608
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
610610``-check-suffix=SUFFIX-NAME `` on ``check_clang_tidy.py `` command line or
611611``-check-suffixes=SUFFIX-NAME-1,SUFFIX-NAME-2,... ``.
612612With ``-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
631631your check work perfectly in all cases, especially if it issues fix-it hints. The
632632most frequent pitfalls are macros and templates:
633633
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
637637 being inspected by the check multiple times (possibly, with different
638638 meanings, see 1), and the same warning (or a slightly different one) may be
639639 issued by the check multiple times; :program: `clang-tidy ` will deduplicate
640640 _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
643643 macro expansions/template instantiations, but easily break some other
644644 expansions/instantiations.
645645
@@ -667,7 +667,7 @@ code quality and catch potential issues. While :program:`clang-tidy` is not
667667currently enforced in CI, following this practice helps maintain code
668668consistency and prevent common errors.
669669
670- Here's useful command to check your staged changes:
670+ Here's a useful command to check your staged changes:
671671
672672.. code-block :: console
673673
@@ -698,7 +698,7 @@ names of the checks to enable.
698698
699699 $ clang-tidy --checks=-*,my-explicit-constructor -list-checks -load myplugin.so
700700
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
702702compiled against the version of clang-tidy that will be loading the plugin.
703703
704704The 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.
720720Running clang-tidy on LLVM
721721--------------------------
722722
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
724724are the natural targets as you already have the source code around. The most
725725convenient 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
727727`How To Setup Clang Tooling For LLVM `_. Once ``compile_commands.json `` is in
728728place and a working version of :program: `clang-tidy ` is in ``PATH `` the entire
729729code 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.
735735
736736
737737* 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,
739739 ``-checks=-*,modernize-use-override `` will run the ``modernize-use-override ``
740740 check only.
741741
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
743743 that the file names are matched against.
744744 ``run-clang-tidy.py clang-tidy/.*Check\.cpp `` will only analyze `clang-tidy `
745745 checks. It may also be necessary to restrict the header files that warnings
746746 are displayed from by using the ``-header-filter `` and ``-exclude-header-filter `` flags.
747747 They have the same behavior as the corresponding :program: `clang-tidy ` flags.
748748
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
750750 all changes in a temporary directory and applies them. Passing ``-format ``
751751 will run clang-format over changed lines.
752752
@@ -795,7 +795,7 @@ There is only one argument that controls profile storage:
795795
796796* ``-store-check-profile=<prefix> ``
797797
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
799799 is passed, these per-TU profiles are instead stored as JSON.
800800 If the prefix is not an absolute path, it is considered to be relative to the
801801 directory from where you have run :program: `clang-tidy `. All ``. `` and ``.. ``
0 commit comments