@@ -127,14 +127,15 @@ Writing a clang-tidy Check
127
127
128
128
So you have an idea of a useful check for :program: `clang-tidy `.
129
129
130
- First, if you're not familiar with LLVM development, read through the `Getting
131
- Started with LLVM `_ document for instructions on setting up your workflow and
130
+ First, if you're not familiar with LLVM development, read through the `Getting Started
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 mostly use `LLVM Phabricator `_.
133
+ style used in the project. For code reviews we currently use `LLVM Github `_,
134
+ though historically we used Phabricator.
134
135
135
- .. _Getting Started with LLVM : https://llvm.org/docs/GettingStarted.html
136
+ .. _Getting Started with the LLVM System : https://llvm.org/docs/GettingStarted.html
136
137
.. _LLVM Coding Standards : https://llvm.org/docs/CodingStandards.html
137
- .. _LLVM Phabricator : https://llvm.org/docs/Phabricator.html
138
+ .. _LLVM Github : https://github.com/llvm/llvm-project
138
139
139
140
Next, you need to decide which module the check belongs to. Modules
140
141
are located in subdirectories of `clang-tidy/
@@ -336,13 +337,24 @@ a starting point for your test cases. A rough outline of the process looks like
336
337
The quickest way to prototype your matcher is to use :program: `clang-query ` to
337
338
interactively build up your matcher. For complicated matchers, build up a matching
338
339
expression incrementally and use :program: `clang-query `'s ``let `` command to save named
339
- matching expressions to simplify your matcher. Just like breaking up a huge function
340
- into smaller chunks with intention-revealing names can help you understand a complex
341
- algorithm, breaking up a matcher into smaller matchers with intention-revealing names
342
- can help you understand a complicated matcher. Once you have a working matcher, the
343
- C++ API will be virtually identical to your interactively constructed matcher. You can
344
- use local variables to preserve your intention-revealing names that you applied to
345
- nested matchers.
340
+ matching expressions to simplify your matcher.
341
+
342
+ .. code-block :: console
343
+ clang-query> let c1 cxxRecordDecl()
344
+ clang-query> match c1
345
+
346
+ Alternatively, pressing the tab key after a previous matcher's open parentheses would also
347
+ show which matchers can be chained with the previous matcher, though some matchers that work
348
+ may not be listed.
349
+
350
+ Just like breaking up a huge function into smaller chunks with intention-revealing names
351
+ can help you understand a complex algorithm, breaking up a matcher into smaller matchers
352
+ with intention-revealing names can help you understand a complicated matcher.
353
+
354
+ Once you have a working clang-query matcher, the C++ API matchers will be the same or similar
355
+ to your interactively constructed matcher (there can be cases where they differ slightly).
356
+ You can use local variables to preserve your intention-revealing names that you applied
357
+ to nested matchers.
346
358
347
359
Creating private matchers
348
360
^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -646,10 +658,13 @@ directory. The path to this directory is available in a lit test with the varia
646
658
Out-of-tree check plugins
647
659
-------------------------
648
660
661
+
649
662
Developing an out-of-tree check as a plugin largely follows the steps
650
- outlined above. The plugin is a shared library whose code lives outside
663
+ outlined above, including creating a new module and doing the hacks to
664
+ register the module. The plugin is a shared library whose code lives outside
651
665
the clang-tidy build system. Build and link this shared library against
652
- LLVM as done for other kinds of Clang plugins.
666
+ LLVM as done for other kinds of Clang plugins. If using CMake, use the keyword
667
+ ``MODULE `` while invoking ``add_library `` or ``llvm_add_library ``.
653
668
654
669
The plugin can be loaded by passing `-load ` to `clang-tidy ` in addition to the
655
670
names of the checks to enable.
@@ -664,6 +679,19 @@ compiled against the version of clang-tidy that will be loading the plugin.
664
679
The plugins can use threads, TLS, or any other facilities available to in-tree
665
680
code which is accessible from the external headers.
666
681
682
+ Note that testing out-of-tree checks might involve getting ``llvm-lit `` from an LLVM
683
+ installation compiled from source. See `Getting Started with the LLVM System `_ for ways
684
+ to do so.
685
+
686
+ Alternatively, get `lit `_ following the `test-suite guide `_ and get the `FileCheck `_ binary,
687
+ and write a version of `check_clang_tidy.py `_ to suit your needs.
688
+
689
+ .. _Getting Started with the LLVM System : https://llvm.org/docs/GettingStarted.html
690
+ .. _test-suite guide : https://llvm.org/docs/TestSuiteGuide.html
691
+ .. _lit : https://llvm.org/docs/CommandGuide/lit.html
692
+ .. _FileCheck : https://llvm.org/docs/CommandGuide/FileCheck.html
693
+ .. _check_clang_tidy.py : https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/test/clang-tidy/check_clang_tidy.py
694
+
667
695
Running clang-tidy on LLVM
668
696
--------------------------
669
697
@@ -688,10 +716,10 @@ warnings and errors. The script provides multiple configuration flags.
688
716
689
717
* To restrict the files examined you can provide one or more regex arguments
690
718
that the file names are matched against.
691
- ``run-clang-tidy.py clang-tidy/.*Check\.cpp `` will only analyze clang-tidy
719
+ ``run-clang-tidy.py clang-tidy/.*Check\.cpp `` will only analyze ` clang-tidy `
692
720
checks. It may also be necessary to restrict the header files that warnings
693
- are displayed from using the ``-header-filter `` flag. It has the same behavior
694
- as the corresponding :program: `clang-tidy ` flag .
721
+ are displayed from by using the ``-header-filter `` and `` -exclude-header-filter `` flags.
722
+ They have the same behavior as the corresponding :program: `clang-tidy ` flags .
695
723
696
724
* To apply suggested fixes ``-fix `` can be passed as an argument. This gathers
697
725
all changes in a temporary directory and applies them. Passing ``-format ``
@@ -758,4 +786,4 @@ There is only one argument that controls profile storage:
758
786
759
787
* If you run :program: `clang-tidy ` from within ``/foo `` directory, and specify
760
788
``-store-check-profile=. ``, then the profile will still be saved to
761
- ``/foo/<ISO8601-like timestamp>-example.cpp.json ``
789
+ ``/foo/<ISO8601-like timestamp>-example.cpp.json ``
0 commit comments