Skip to content

Commit 362d37a

Browse files
Update clang tidy Contributing guide (#106672)
Update the documentation to direct new users to the Github instead of the discontinued Phabricator archive. Also details more ways and information regarding clang-query usage. Partially resolves/disclaims #106656 and #106663 as per discussion in https://discourse.llvm.org/t/inconsistency-between-hasdescendant-in-clang-query-and-clang-libtooling-matchers/80799/. Also updates the out-of-tree guide. For context, I recently went through the Contributing guide while writing #102299, and many of these updates were from my experience trying to follow the guide. e.g. I was trying to link the shared library of an out-of-tree check as SHARED in CMake and encountered duplicate symbols like _ZTIN5clang4tidy14ClangTidyCheckE. It wasn't until I saw 84f137a that I found out I had to use MODULE. I also encountered the clang-query difference which was a surprise as the documentation said the two matchers were "virtually identical". Also, the -header-filter thing tripped me out until I found #25590 and #91400. Usually, when people say restrict and filter, they mean filter out (since -header-filter instead includes/filters in said headers).
1 parent b719c92 commit 362d37a

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

clang-tools-extra/docs/clang-tidy/Contributing.rst

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,15 @@ Writing a clang-tidy Check
127127

128128
So you have an idea of a useful check for :program:`clang-tidy`.
129129

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
132132
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.
134135

135-
.. _Getting Started with LLVM: https://llvm.org/docs/GettingStarted.html
136+
.. _Getting Started with the LLVM System: https://llvm.org/docs/GettingStarted.html
136137
.. _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
138139

139140
Next, you need to decide which module the check belongs to. Modules
140141
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
336337
The quickest way to prototype your matcher is to use :program:`clang-query` to
337338
interactively build up your matcher. For complicated matchers, build up a matching
338339
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.
346358

347359
Creating private matchers
348360
^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -646,10 +658,13 @@ directory. The path to this directory is available in a lit test with the varia
646658
Out-of-tree check plugins
647659
-------------------------
648660

661+
649662
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
651665
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``.
653668

654669
The plugin can be loaded by passing `-load` to `clang-tidy` in addition to the
655670
names of the checks to enable.
@@ -664,6 +679,19 @@ compiled against the version of clang-tidy that will be loading the plugin.
664679
The plugins can use threads, TLS, or any other facilities available to in-tree
665680
code which is accessible from the external headers.
666681

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+
667695
Running clang-tidy on LLVM
668696
--------------------------
669697

@@ -688,10 +716,10 @@ warnings and errors. The script provides multiple configuration flags.
688716

689717
* To restrict the files examined you can provide one or more regex arguments
690718
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`
692720
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.
695723

696724
* To apply suggested fixes ``-fix`` can be passed as an argument. This gathers
697725
all changes in a temporary directory and applies them. Passing ``-format``
@@ -758,4 +786,4 @@ There is only one argument that controls profile storage:
758786

759787
* If you run :program:`clang-tidy` from within ``/foo`` directory, and specify
760788
``-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

Comments
 (0)