diff --git a/docs/src/content/docs/guides/fairmq-tidy.mdx b/docs/src/content/docs/guides/fairmq-tidy.mdx index 4605d722..04289d79 100644 --- a/docs/src/content/docs/guides/fairmq-tidy.mdx +++ b/docs/src/content/docs/guides/fairmq-tidy.mdx @@ -1,24 +1,24 @@ --- title: 'Check code with fairmq-tidy' -description: 'Find the deprecated FairMQ* type names in your project with fairmq-tidy, fix them, and run the check at each build and in CI.' +description: 'Find deprecated FairMQ names and APIs in your project with fairmq-tidy, apply the fixes, and run the checks at each build and in CI.' sidebar: order: 11 -code: [fairmq/tidy/ModernizeNonNamespacedTypes.h, fairmq/tidy/Tool.h] +code: [fairmq/tidy/Module.cxx, fairmq/tidy/runTidy.cxx] --- import { Steps } from '@astrojs/starlight/components'; -This guide runs `fairmq-tidy` on a CMake project that uses FairMQ. The result is a list of all places that use a deprecated `FairMQ*` type name. A CMake target then checks the code at each build. The reference of the tool is on [fairmq-tidy](/tools/fairmq-tidy/). +This guide runs `fairmq-tidy` on a CMake project that uses FairMQ. The result is a list of all places that use a deprecated FairMQ name or API, and a fix for most of them. A CMake target then checks the code at each build. The reference of the tool is on [fairmq-tidy](/tools/fairmq-tidy/). ## Before you start - You need a FairMQ installation with the tool. Build FairMQ with `-DBUILD_TIDY_TOOL=ON`, see [Build fairmq-tidy](/tools/fairmq-tidy/#build-fairmq-tidy). - Your project must use the Makefile or the Ninja generator of CMake. The other generators do not write a compilation database. -Make sure that the tool runs: +Make sure that the tool runs and knows the FairMQ checks: ```sh -fairmq-tidy --help +fairmq-tidy --checks='-*,fairmq-*' --list-checks ``` ## Make a compilation database @@ -39,94 +39,72 @@ The tool reads the compile command of each file, with the include paths and flag The database has an entry for each source file. A header file has no entry. The tool checks a header when it checks a source file that includes the header. -## Run the check +## Run the checks -1. Go to the source directory of your project. The tool finds a relative file name from the current directory. - -2. Check one file: +1. Check one file with the FairMQ checks only: ```sh - fairmq-tidy -p build src/mydevice.cxx + fairmq-tidy -p build --checks='-*,fairmq-*' src/mydevice.cxx ``` -3. Check all source files. Give several file names: + Without `--checks`, the tool also runs the checks of your `.clang-tidy` file, or the default checks of clang-tidy if there is no such file. + +2. Check all source files. Give several file names: ```sh - fairmq-tidy -p build src/*.cxx + fairmq-tidy -p build --checks='-*,fairmq-*' src/*.cxx ``` - With more than one file, the tool prints a line before each file. An example is `[1/3] Processing file /home/me/myproject/src/mydevice.cxx.` - -4. Write the output to a file. The tool writes to the standard error stream: - - ```sh - fairmq-tidy -p build src/*.cxx 2> tidy.log - ``` +3. To also see the findings in your headers, add a header filter, for example `--header-filter='src/.*'`. ## Read the output -Each finding has a warning line, the source line with a marker under the old name, and a fix-it hint with the new name: +Each finding has a warning line, the source line with a marker, and the fix if the check has one: ```txt -/home/me/myproject/src/mydevice.cxx:3:25: warning: Modernize non-namespaced type 'FairMQDevice' with 'fair::mq::Device'. [fairmq-modernize-nonnamespaced-types] +/home/me/myproject/src/mydevice.cxx:3:25: warning: 'FairMQDevice' is deprecated; use 'fair::mq::Device' [fairmq-modernize-nonnamespaced-types] 3 | class MyDevice : public FairMQDevice | ^~~~~~~~~~~~ | fair::mq::Device -/home/me/myproject/src/mydevice.cxx:5:21: warning: Modernize non-namespaced type 'FairMQMessagePtr' with 'fair::mq::MessagePtr' (aka 'unique_ptr'). [fairmq-modernize-nonnamespaced-types] - 5 | bool HandleData(FairMQMessagePtr& msg, int); - | ^~~~~~~~~~~~~~~~ - | fair::mq::MessagePtr ``` -- `/home/me/myproject/src/mydevice.cxx:3:25` is the file, the line and the column of the old name. The file name is the name in the compilation database. CMake writes absolute paths. -- `'FairMQDevice'` is the old name. `'fair::mq::Device'` is the new name. For some names, `(aka …)` shows the full type. -- `[fairmq-modernize-nonnamespaced-types]` is the name of the check. Use it to search the log. -- With Clang 18, most findings appear two times at the same location. The example shows one copy. Fix each finding one time. -- After each file, the tool prints the number of warnings, for example `4 warnings generated.` This number includes the second copies. -- A finding in a header appears for each source file that includes the header. - -A finding in `runFairMQDevice.h` of FairMQ means that your code includes this old header. Do not change the FairMQ header. Move your code to `runDevice.h`, see [Replace runFairMQDevice.h](/guides/migrate-names/#replace-runfairmqdeviceh). - -The tool does not find all old names. For example, it does not find `fairmq_free_fn`, `InputMsgCallback` and the old headers. See [Names that the check does not find](/tools/fairmq-tidy/#names-that-the-check-does-not-find). +- `/home/me/myproject/src/mydevice.cxx:3:25` is the file, the line and the column. +- `[fairmq-modernize-nonnamespaced-types]` is the name of the check. The [list of checks](/tools/fairmq-tidy/#checks) tells what each check finds. +- The last line under the marker is the fix. ## Apply the fixes -`fairmq-tidy` does not change your files. Do these steps for each finding: - -1. Open the file at the line and the column of the warning. +1. Commit your changes or make a copy, so that you can see the changes of the tool. -2. Replace the old name with the text of the fix-it hint, for example `FairMQDevice` with `fair::mq::Device`. +2. Run the checks with `--fix`: -3. Replace the old `#include` lines. Use the table [Headers](/guides/migrate-names/#headers). + ```sh + fairmq-tidy -p build --checks='-*,fairmq-*' --fix src/*.cxx + ``` -4. Run `fairmq-tidy` again. Continue until it prints no warning. +3. Look at the changes, for example with `git diff`. -5. Build your project with `-Werror=deprecated-declarations`. The compiler then stops at the old names that the tool does not find. +4. Run `fairmq-tidy` again. Fix by hand the findings that have no fix. + +5. Build your project with `-Werror=deprecated-declarations`. The compiler then stops at the old names that the tool does not find, see [Move from the FairMQ* names](/guides/migrate-names/). -To get a list of all fixes, add the Clang option `-fdiagnostics-parseable-fixits`: +To check many files in parallel, use `run-clang-tidy` of LLVM: ```sh -fairmq-tidy -p build --extra-arg=-fdiagnostics-parseable-fixits src/*.cxx 2>&1 | grep '^fix-it:' | sort -u +run-clang-tidy -clang-tidy-binary fairmq-tidy -checks='-*,fairmq-*' -p build -fix ``` -```txt -fix-it:"/home/me/myproject/src/mydevice.cxx":{3:25-3:37}:"fair::mq::Device" -fix-it:"/home/me/myproject/src/mydevice.cxx":{5:21-5:37}:"fair::mq::MessagePtr" -``` - -Each line gives the file, the range of the old name (`line:column-line:column`) and the new text. `sort -u` removes the second copy of each finding. - ## Check the code at each build -The CMake module `FairMQTidy.cmake` adds the check to a target. The build then runs the tool after it builds the target. The check includes only the source files of the target that end in `.cpp` or `.cxx`. +The CMake module `FairMQTidy.cmake` adds the checks to a target. The build then runs the tool after it builds the target. The checks include only the source files of the target that end in `.cpp` or `.cxx`. @@ -166,42 +144,37 @@ For each source file, the build prints a line like this one and then the finding fairmq-tidy: Analyzing source file 'src/mydevice.cxx' of target 'mydevice' ``` -To build one target and run its check, build the target `fairmq-tidy-`: +To build one target and run its checks, build the target `fairmq-tidy-`: ```sh cmake --build build --target fairmq-tidy-mydevice ``` -The build runs the check again each time the target changes. Findings do not stop the build. If the tool fails on a file, the build fails. - ## Fail a CI job on findings -The exit code of `fairmq-tidy` is `0` also when it finds old names. To fail a CI job on findings, search the build log for the name of the check: +The exit code of `fairmq-tidy` is `0` also when it finds something. Make the findings errors with `--warnings-as-errors`: ```sh -set -eo pipefail -cmake --build build 2>&1 | tee build.log -if grep -q "fairmq-modernize-nonnamespaced-types" build.log; then - echo "fairmq-tidy found deprecated FairMQ names" - exit 1 -fi +fairmq-tidy -p build --checks='-*,fairmq-*' --warnings-as-errors='fairmq-*' src/*.cxx ``` -`set -eo pipefail` stops the script with the exit code of `cmake --build`, so that a failed build also fails the job. +With `fairmq_target_tidy()`, give the option in `EXTRA_ARGS`. A finding then stops the build: + +```cmake +fairmq_target_tidy(TARGET mydevice EXTRA_ARGS --warnings-as-errors=fairmq-*) +``` ## Problems that can occur | Message | Cause | Solution | | --- | --- | --- | | `CMAKE_EXPORT_COMPILE_COMMANDS is not enabled. Skipping.` | The variable is off when `fairmq_target_tidy()` runs. | Set `CMAKE_EXPORT_COMPILE_COMMANDS` to `ON` before the call. | -| `Skipping . Compile command not found.` (exit code 2) | The file has no entry in the compilation database. | Check the `-p` directory. Give source files of the project, not headers. | -| `error: no such file or directory: '-Wno-everything'` | The compilation database has no entry for the file, for example a header. Clang 18 takes the compile command of a similar file. The tool checks the file, but it also shows the compiler warnings. | Give the source files that the database lists. | -| `fatal error: '
' file not found`, then `Error while processing .` (exit code 1) | Clang does not find a header. | Add the include path with `--extra-arg=-I`. For the headers of Clang itself, add `--extra-arg-before=-I$(clang -print-resource-dir)/include`. | -| `Error while trying to load a compilation database` and `Running without flags.` | The tool did not find `compile_commands.json`. | Give the build directory with `-p`. | -| `no such file or directory: '…$<1:…'` in the CMake build | `fairmq_target_tidy()` breaks the command line when its list of extra arguments has two or more items. With GCC, the function always adds one item. | Do not use `EXTRA_ARGS` with GCC. With other compilers, give at most one argument. See [The CMake function](/tools/fairmq-tidy/#the-cmake-function-fairmq_target_tidy). | +| `Error while trying to load a compilation database` | The tool did not find `compile_commands.json`. | Give the build directory with `-p`. | +| `fatal error: '
' file not found` | Clang does not find a header. | Add the include path with `--extra-arg=-I`. | +| A warning of a check that you did not select, for example `clang-analyzer-*` | The command line has no `--checks`, and the tool runs the checks of the configuration. | Give `--checks='-*,fairmq-*'`. | ## Related pages -- [fairmq-tidy](/tools/fairmq-tidy/): the check, the command line, the exit codes and the CMake function. +- [fairmq-tidy](/tools/fairmq-tidy/): the checks, the command line and the CMake function. - [Move from the FairMQ* names](/guides/migrate-names/): all old names and their replacements. - [Use FairMQ in a CMake project](/start/cmake/): `find_package(FairMQ)` and the targets. diff --git a/docs/src/content/docs/guides/migrate-names.mdx b/docs/src/content/docs/guides/migrate-names.mdx index 45bfffdc..98275273 100644 --- a/docs/src/content/docs/guides/migrate-names.mdx +++ b/docs/src/content/docs/guides/migrate-names.mdx @@ -35,9 +35,9 @@ class MyDevice : public fair::mq::Device -1. Replace the old `#include` lines. Use the table [Headers](#headers). +1. Replace the old `#include` lines. Use the table [Headers](#headers). [fairmq-tidy](/guides/fairmq-tidy/) can do this step for you. -2. Replace the old type names. Use the table [Type names](#type-names). [fairmq-tidy](/guides/fairmq-tidy/) finds most of them for you. +2. Replace the old type names. Use the table [Type names](#type-names). [fairmq-tidy](/guides/fairmq-tidy/) can do this step for you. 3. If your `main()` comes from `runFairMQDevice.h`, move to `runDevice.h`. See [Replace runFairMQDevice.h](#replace-runfairmqdeviceh). @@ -103,17 +103,17 @@ Each old header includes the new header, so the old `#include` lines still work. ## Type names -The table is in the order of the headers that declare the deprecated alias. The column fairmq-tidy shows if [the check](/tools/fairmq-tidy/#names-that-the-check-finds) finds the name (with Clang 18). +The table is in the order of the headers that declare the deprecated alias. The column fairmq-tidy shows if [the check `fairmq-modernize-nonnamespaced-types`](/tools/fairmq-tidy/#fairmq-modernize-nonnamespaced-types) finds and fixes the name. | Old name | New name | Declared in | fairmq-tidy | | --- | --- | --- | --- | | `FairMQChannel` | [`fair::mq::Channel`](/api/fair/mq/Channel/) | `Channel.h` | yes | | `FairMQChannelMap` | [`fair::mq::ChannelMap`](/api/fair/mq/#ChannelMap) | `Device.h` | yes | -| `InputMsgCallback` | [`fair::mq::InputMsgCallback`](/api/fair/mq/#InputMsgCallback) | `Device.h` | no | -| `InputMultipartCallback` | [`fair::mq::InputMultipartCallback`](/api/fair/mq/#InputMultipartCallback) | `Device.h` | no | +| `InputMsgCallback` | [`fair::mq::InputMsgCallback`](/api/fair/mq/#InputMsgCallback) | `Device.h` | yes | +| `InputMultipartCallback` | [`fair::mq::InputMultipartCallback`](/api/fair/mq/#InputMultipartCallback) | `Device.h` | yes | | `FairMQDevice` | [`fair::mq::Device`](/api/fair/mq/Device/) | `Device.h` | yes | | `fair::mq::FairMQMemoryResource` | [`fair::mq::MemoryResource`](/api/fair/mq/MemoryResource/) | `MemoryResources.h` | no | -| `fairmq_free_fn` | [`fair::mq::FreeFn`](/api/fair/mq/#FreeFn) | `Message.h` | no | +| `fairmq_free_fn` | [`fair::mq::FreeFn`](/api/fair/mq/#FreeFn) | `Message.h` | yes | | `FairMQMessage` | [`fair::mq::Message`](/api/fair/mq/Message/) | `Message.h` | yes | | `FairMQMessagePtr` | [`fair::mq::MessagePtr`](/api/fair/mq/#MessagePtr) | `Message.h` | yes | | `FairMQParts` | [`fair::mq::Parts`](/api/fair/mq/Parts/) | `Parts.h` | yes | @@ -133,7 +133,7 @@ The table is in the order of the headers that declare the deprecated alias. The | `FairMQUnmanagedRegion` | [`fair::mq::UnmanagedRegion`](/api/fair/mq/UnmanagedRegion/) | `UnmanagedRegion.h` | yes | | `FairMQUnmanagedRegionPtr` | [`fair::mq::UnmanagedRegionPtr`](/api/fair/mq/#UnmanagedRegionPtr) | `UnmanagedRegion.h` | yes | | `FairMQRegionConfig` | [`fair::mq::RegionConfig`](/api/fair/mq/RegionConfig/) | `UnmanagedRegion.h` | yes | -| `FairMQDevicePtr` | `std::unique_ptr` as the return type of `getDevice()` | `runFairMQDevice.h` | yes, as `fair::mq::Device *` | +| `FairMQDevicePtr` | `std::unique_ptr` as the return type of `getDevice()` | `runFairMQDevice.h` | no | All old names are global, except `fair::mq::FairMQMemoryResource`. All names in the table except `FairMQDevicePtr` have the attribute `[[deprecated]]`. @@ -183,13 +183,13 @@ The compiler warns for these members too. The column "Declared in" names the hea The CMake package is `FairMQ`. Link your targets with `FairMQ::FairMQ`. The executables of FairMQ are imported targets with the same prefix, for example `FairMQ::fairmq-bsampler`. See [Use FairMQ in a CMake project](/start/cmake/). -## Automate the search with fairmq-tidy +## Automate the move with fairmq-tidy -`fairmq-tidy` finds the uses of most old type names and shows the new name for each one. It does not look at the `#include` lines and the members, and it does not change your files. See [Check code with fairmq-tidy](/guides/fairmq-tidy/). +`fairmq-tidy` finds the old headers, most old type names and `Device::fChannels`. With `--fix`, it replaces them in your files. See [Check code with fairmq-tidy](/guides/fairmq-tidy/) and the [list of checks](/tools/fairmq-tidy/#checks). ## Related pages -- [Check code with fairmq-tidy](/guides/fairmq-tidy/): find the old type names in a project. -- [fairmq-tidy](/tools/fairmq-tidy/): the check, the command line and the CMake function. +- [Check code with fairmq-tidy](/guides/fairmq-tidy/): find and fix the old names in a project. +- [fairmq-tidy](/tools/fairmq-tidy/): the checks, the command line and the CMake function. - [Your first device](/start/first-device/): a device with the current API. - [Use FairMQ in a CMake project](/start/cmake/): the package and its targets. diff --git a/docs/src/content/docs/reference/cmake.mdx b/docs/src/content/docs/reference/cmake.mdx index 1d71de15..ea5453e8 100644 --- a/docs/src/content/docs/reference/cmake.mdx +++ b/docs/src/content/docs/reference/cmake.mdx @@ -55,12 +55,12 @@ The error also occurs when the value `ON` is in the cache from an earlier config | Package | Version | Needed when | Kind | | --- | --- | --- | --- | | Threads | any | `BUILD_FAIRMQ` | public | -| FairLogger | 1.6.0 or later | `BUILD_FAIRMQ` or `BUILD_TIDY_TOOL` | public | -| Boost | 1.66 or later, but not 1.88 | `BUILD_FAIRMQ` or `BUILD_TIDY_TOOL` | public | +| FairLogger | 1.6.0 or later | `BUILD_FAIRMQ` | public | +| Boost | 1.66 or later, but not 1.88 | `BUILD_FAIRMQ` | public | | ZeroMQ | 4.1.4 or later | `BUILD_FAIRMQ` | private | | PicoSHA2 | bundled | `BUILD_FAIRMQ` | bundled | | GoogleTest | bundled | `BUILD_TESTING` | bundled, or private with `USE_EXTERNAL_GTEST` | -| LLVM, Clang, CLI11 | any | `BUILD_TIDY_TOOL` | private | +| LLVM, Clang with the clang-tidy libraries | 17 to 20 (tested) | `BUILD_TIDY_TOOL` | private | | FairCMakeModules | 0.2 or later | always | private, bundled if CMake does not find it | The Boost components are `container`, `program_options`, `filesystem`, `date_time` and `regex`. @@ -148,7 +148,7 @@ FairMQ joins the selected sanitizers in one flag, for example `-fsanitize=addres With `RUN_STATIC_ANALYSIS`, CMake also searches `include-what-you-use` (shown as `iwyu` in the summary) and sets `CMAKE_CXX_INCLUDE_WHAT_YOU_USE` when it finds the program. -[`cmake/FairMQTidy.cmake`](https://github.com/FairRootGroup/FairMQ/blob/dev/cmake/FairMQTidy.cmake) defines `fairmq_target_tidy(TARGET [EXTRA_ARGS ] [CLANG_EXECUTABLE ])`. It runs `fairmq-tidy` on the `.cpp` and `.cxx` files of a target. The installed package has this module when it has the component `tidy_tool`. See [Check code with fairmq-tidy](/guides/fairmq-tidy/). +[`cmake/FairMQTidy.cmake`](https://github.com/FairRootGroup/FairMQ/blob/dev/cmake/FairMQTidy.cmake) defines `fairmq_target_tidy(TARGET [CHECKS ] [EXTRA_ARGS ...])`. It runs `fairmq-tidy` on the `.cpp` and `.cxx` files of a target. The installed package has this module when it has the component `tidy_tool`. See [Check code with fairmq-tidy](/guides/fairmq-tidy/). ## Other variables diff --git a/docs/src/content/docs/reference/glossary.mdx b/docs/src/content/docs/reference/glossary.mdx index 66507379..209a9cdc 100644 --- a/docs/src/content/docs/reference/glossary.mdx +++ b/docs/src/content/docs/reference/glossary.mdx @@ -114,7 +114,7 @@ the devices. See [fairmq-shmmonitor](/tools/shmmonitor/). ### fairmq-tidy -A Clang tool that finds the deprecated `FairMQ*` type names in your code. See [fairmq-tidy](/tools/fairmq-tidy/) and +`clang-tidy` with checks for FairMQ. It finds deprecated FairMQ names and APIs in your code and fixes most of them. See [fairmq-tidy](/tools/fairmq-tidy/) and [Check code with fairmq-tidy](/guides/fairmq-tidy/). ## H diff --git a/docs/src/content/docs/start/cmake.mdx b/docs/src/content/docs/start/cmake.mdx index b7749d4b..c3f89f8a 100644 --- a/docs/src/content/docs/start/cmake.mdx +++ b/docs/src/content/docs/start/cmake.mdx @@ -202,8 +202,8 @@ The function adds the target `fairmq-tidy-hello` to the default build. After CMa | Argument | What it does | | --- | --- | | `TARGET ` | The target to check. Required. | -| `EXTRA_ARGS ` | One more argument for `fairmq-tidy`. With GCC, do not use it: the function adds its own argument, and two arguments break the command. This is a known defect. | -| `CLANG_EXECUTABLE ` | The program `clang`. If your compiler is GCC, the function runs `clang -print-resource-dir` to find the Clang headers. Without this argument, it uses the target `clang` if it exists, or `clang` from `PATH`. | +| `CHECKS ` | The checks to run. Default: `fairmq-*`. | +| `EXTRA_ARGS ...` | More arguments for `fairmq-tidy`, for example `--fix` or `--warnings-as-errors=fairmq-*`. | The function needs `CMAKE_EXPORT_COMPILE_COMMANDS` set to `ON`. If it is not `ON`, the function writes a warning and adds no target. See [Check code with fairmq-tidy](/guides/fairmq-tidy/) for the checks and the output. diff --git a/docs/src/content/docs/start/install.mdx b/docs/src/content/docs/start/install.mdx index 8ec1f0eb..0f7f651f 100644 --- a/docs/src/content/docs/start/install.mdx +++ b/docs/src/content/docs/start/install.mdx @@ -24,7 +24,7 @@ The CI of FairMQ builds and tests FairMQ on Linux (Ubuntu 24.04) with GCC 12, 13 | [PicoSHA2](https://github.com/okdshin/PicoSHA2) | — | The build always uses a bundled copy. | | [GoogleTest](https://github.com/google/googletest) | — | Only for the tests. The build uses a bundled copy by default. | | git | — | CMake reads the version of FairMQ from git and gets the bundled copies with git. | -| LLVM, Clang, [CLI11](https://github.com/CLIUtils/CLI11) | — | Only for the tool `fairmq-tidy`. | +| LLVM, Clang | 17 (tested: 17 to 20) | Only for the tool `fairmq-tidy`. It needs the clang-tidy libraries too. | The bundled copies are git submodules in the folder `extern/` of the repository. When CMake needs a bundled copy and its folder is empty, CMake runs `git submodule update --init` for it. @@ -127,7 +127,7 @@ If a dependency is not in a system directory, give its installation prefix to CM | `CMAKE_INSTALL_PREFIX` | `/usr/local` | Sets the installation directory. Set it when you configure, because the installed start scripts of the examples contain this path. | | `BUILD_TESTING` | `OFF` | Builds the tests. | | `BUILD_EXAMPLES` | `ON` | Builds the examples. `cmake --install` also installs them and their start scripts. | -| `BUILD_TIDY_TOOL` | `OFF` | Builds the experimental tool [`fairmq-tidy`](/tools/fairmq-tidy/). It needs LLVM, Clang and CLI11. | +| `BUILD_TIDY_TOOL` | `OFF` | Builds the experimental tool [`fairmq-tidy`](/tools/fairmq-tidy/). It needs LLVM and Clang. | | `FAIRMQ_CHANNEL_DEFAULT_AUTOBIND` | not set | Sets the default of the channel property `autoBind`. If the option is not set, the default is `true`. | | `FAIRMQ_DEBUG_MODE` | `OFF` | The transport `shmem` records its messages, so that `fairmq-shmmonitor --debug` can list them. This can decrease the performance. | | `USE_EXTERNAL_GTEST` | `OFF` | Uses an installed GoogleTest instead of the bundled copy. | diff --git a/docs/src/content/docs/tools/fairmq-tidy.mdx b/docs/src/content/docs/tools/fairmq-tidy.mdx index a09a7c79..ac7c024f 100644 --- a/docs/src/content/docs/tools/fairmq-tidy.mdx +++ b/docs/src/content/docs/tools/fairmq-tidy.mdx @@ -1,110 +1,83 @@ --- title: 'fairmq-tidy' -description: 'A Clang tool that finds the deprecated FairMQ* type names in your code. Its check, build, command line, exit codes and CMake function.' +description: 'clang-tidy with the fairmq-* checks built in. The checks, the build, the command line and the CMake function.' sidebar: order: 3 -code: [fairmq/tidy/Tool.h, fairmq/tidy/ModernizeNonNamespacedTypes.h] +code: [fairmq/tidy/Module.cxx, fairmq/tidy/runTidy.cxx] --- import Snippet from '../../../components/Snippet.astro'; -`fairmq-tidy` is a static analysis tool for code that uses FairMQ. It parses your source files with the Clang libraries, finds the deprecated `FairMQ*` type aliases, and shows the `fair::mq::` type that replaces each one. - -:::note -The CMake summary of FairMQ marks the tool as experimental. CMake does not build it by default. -::: +`fairmq-tidy` is a static analysis tool for code that uses FairMQ. It is `clang-tidy` with the `fairmq-*` checks built in. The checks find deprecated FairMQ names and APIs in your code, and most checks also give a fix that the tool can apply. For a step-by-step procedure, see [Check code with fairmq-tidy](/guides/fairmq-tidy/). This page is the reference. ## How the tool works -`fairmq-tidy` is a program that uses Clang LibTooling. [`runTool.cpp`](https://github.com/FairRootGroup/FairMQ/blob/dev/fairmq/tidy/runTool.cpp) parses the command line. Then [`Tool::run()`](/source/fairmq/tidy/Tool.h/#L48-L81) processes each source file in these steps: +The `fairmq-*` checks are a clang-tidy module ([Module.cxx](/source/fairmq/tidy/Module.cxx/)). `fairmq-tidy` is the `clang-tidy` driver of LLVM with this module linked in. Thus `fairmq-tidy` accepts all `clang-tidy` options, reads `.clang-tidy` files, applies fixes with `--fix` and knows the upstream checks, for example `modernize-*`. One parse of a file runs the upstream checks and the `fairmq-*` checks. -1. It gets the compile command of the file from the compilation database. -2. It adds `-std=c++17` if the command has no `-std=` option. -3. It appends `-Wno-everything`. This turns off the compiler warnings, so that the output shows the findings of the check. -4. It parses the file and runs the check on the syntax tree. +[`runTidy.cxx`](/source/fairmq/tidy/runTidy.cxx/) adds two arguments before it calls the driver: - +- `--checks=fairmq-*`, if the command line has no `--checks` option. clang-tidy adds this value to the `Checks` of the configuration. If there is no `.clang-tidy` file, the configuration also enables the default checks of clang-tidy (`clang-diagnostic-*` and `clang-analyzer-*`). +- `--extra-arg-before=-resource-dir=`, if the command line has no resource directory. `` is the resource directory of the LLVM installation that FairMQ was built with. It contains the builtin headers of Clang, for example ``. Without it, Clang does not find these headers when the compiler of the project is GCC. -## The check fairmq-modernize-nonnamespaced-types + -The tool has one check, `fairmq-modernize-nonnamespaced-types` ([ModernizeNonNamespacedTypes.h](/source/fairmq/tidy/ModernizeNonNamespacedTypes.h/#L20-L69)). It finds each place in the code that names a type through a `using` alias with these two properties: +## Checks -- The qualified name of the alias contains `::FairMQ`. This is true for a global alias like `FairMQDevice`, and for an alias in a namespace or a class. -- The aliased type, as Clang prints it, starts with `fair::mq::`. - - - -For each place, the check prints a warning with a fix-it hint. The hint replaces the alias with the aliased type, for example `FairMQDevice` with `fair::mq::Device`. The check does not find `typedef` declarations, only `using` aliases. The tool does not change your files. - -### Names that the check finds - -With Clang 18, the check finds these aliases of the FairMQ headers. The second column is the text of the fix-it hint. - -| Alias | Fix-it replacement | Declared in | +| Check | Finds | Fix | | --- | --- | --- | -| `FairMQChannel` | `fair::mq::Channel` | `Channel.h` | -| `FairMQChannelMap` | `fair::mq::ChannelMap` | `Device.h` | -| `FairMQDevice` | `fair::mq::Device` | `Device.h` | -| `FairMQMessage` | `fair::mq::Message` | `Message.h` | -| `FairMQMessagePtr` | `fair::mq::MessagePtr` | `Message.h` | -| `FairMQParts` | `fair::mq::Parts` | `Parts.h` | -| `FairMQPoller` | `fair::mq::Poller` | `Poller.h` | -| `FairMQPollerPtr` | `fair::mq::PollerPtr` | `Poller.h` | -| `FairMQProgOptions` | `fair::mq::ProgOptions` | `ProgOptionsFwd.h` | -| `FairMQSocket` | `fair::mq::Socket` | `Socket.h` | -| `FairMQSocketPtr` | `fair::mq::SocketPtr` | `Socket.h` | -| `FairMQTransportFactory` | `fair::mq::TransportFactory` | `TransportFactory.h` | -| `FairMQTransportFactoryError` | `fair::mq::TransportFactoryError` | `TransportFactory.h` | -| `FairMQRegionEvent` | `fair::mq::RegionEvent` | `UnmanagedRegion.h` | -| `FairMQRegionInfo` | `fair::mq::RegionInfo` | `UnmanagedRegion.h` | -| `FairMQRegionBlock` | `fair::mq::RegionBlock` | `UnmanagedRegion.h` | -| `FairMQRegionCallback` | `fair::mq::RegionCallback` | `UnmanagedRegion.h` | -| `FairMQRegionBulkCallback` | `fair::mq::RegionBulkCallback` | `UnmanagedRegion.h` | -| `FairMQRegionEventCallback` | `fair::mq::RegionEventCallback` | `UnmanagedRegion.h` | -| `FairMQUnmanagedRegion` | `fair::mq::UnmanagedRegion` | `UnmanagedRegion.h` | -| `FairMQUnmanagedRegionPtr` | `fair::mq::UnmanagedRegionPtr` | `UnmanagedRegion.h` | -| `FairMQRegionConfig` | `fair::mq::RegionConfig` | `UnmanagedRegion.h` | -| `FairMQDevicePtr` | `fair::mq::Device *` | `runFairMQDevice.h` | +| `fairmq-modernize-channels-access` | Uses of the deprecated member `fair::mq::Device::fChannels` | `fChannels.at(name).at(index)` becomes `GetChannel(name, index)`. Other uses become `GetChannels()`. | +| `fairmq-modernize-deprecated-headers` | `#include` lines of the deprecated headers, for example `` | The new header, for example `` | +| `fairmq-modernize-nonnamespaced-types` | Uses of the deprecated global type aliases, for example `FairMQDevice` or `InputMsgCallback` | The namespaced type, for example `fair::mq::Device` | -`FairMQDevicePtr` is not deprecated, but the check finds it. The fix-it gives a raw pointer. The new `getDevice()` of `` returns a `std::unique_ptr` instead. See [Replace runFairMQDevice.h](/guides/migrate-names/#replace-runfairmqdeviceh). +The checks report a name in a macro, but they do not fix it. Like all clang-tidy checks, they show findings in headers only if the header matches `--header-filter=`. -### Names that the check does not find +### fairmq-modernize-nonnamespaced-types -Replace these deprecated names by hand. The compiler warns for them. See [Move from the FairMQ* names](/guides/migrate-names/). +The check finds each use of a type alias with these two properties: -| Name | Replacement | Why the check does not find it | -| --- | --- | --- | -| `fairmq_free_fn` | `fair::mq::FreeFn` | The name does not contain `FairMQ`. | -| `InputMsgCallback` | `fair::mq::InputMsgCallback` | The name does not contain `FairMQ`. | -| `InputMultipartCallback` | `fair::mq::InputMultipartCallback` | The name does not contain `FairMQ`. | -| `fair::mq::FairMQMemoryResource` | `fair::mq::MemoryResource` | The alias is written as `MemoryResource`, without `fair::mq::`. Clang 18 does not print the namespace. | -| `` and the other old headers | `` and so on | The check looks at types, not at `#include` lines. | -| Deprecated functions, for example `Parts::AtRef()` | See the migration guide | The check looks at type names only. | +- The alias is declared in the global namespace and has the attribute `[[deprecated]]`. +- The fully qualified name of the aliased type starts with `fair::mq::`. -### Output - -The tool writes its output to the standard error stream. Each finding has a warning line, the source line, a marker under the alias, and the fix-it hint: +Thus the check finds a new deprecated alias without a change to the check. Each use gives one warning: ```txt -src/mydevice.cxx:3:25: warning: Modernize non-namespaced type 'FairMQDevice' with 'fair::mq::Device'. [fairmq-modernize-nonnamespaced-types] +src/mydevice.cxx:3:25: warning: 'FairMQDevice' is deprecated; use 'fair::mq::Device' [fairmq-modernize-nonnamespaced-types] 3 | class MyDevice : public FairMQDevice | ^~~~~~~~~~~~ | fair::mq::Device ``` -- For an alias of a template type, the message adds the full type, for example `'fair::mq::MessagePtr' (aka 'unique_ptr')`. -- With Clang 18, the tool prints most findings two times at the same location. A name before `::`, for example `FairMQDevice::Foo()`, appears one time. -- A finding in a header shows the path of the header. The tool reports it for each source file that includes the header, also for headers in system include directories. -- The last line counts the warnings, for example `10 warnings generated.` -- With `--extra-arg=-fdiagnostics-parseable-fixits`, Clang adds a machine-readable line for each fix-it: +### fairmq-modernize-deprecated-headers -```txt -fix-it:"src/mydevice.cxx":{3:25-3:37}:"fair::mq::Device" -``` +The check finds `#include` lines in the source file that name one of the 10 deprecated headers. Each header has a successor: -The line gives the file, the range to replace (`line:column-line:column`, the end column is after the last character), and the new text. +| Deprecated header | Successor | +| --- | --- | +| `FairMQChannel.h` | `fairmq/Channel.h` | +| `FairMQDevice.h` | `fairmq/Device.h` | +| `FairMQLogger.h` | `fairlogger/Logger.h` | +| `FairMQMessage.h` | `fairmq/Message.h` | +| `FairMQParts.h` | `fairmq/Parts.h` | +| `FairMQPoller.h` | `fairmq/Poller.h` | +| `FairMQProgOptions.h` | `fairmq/ProgOptions.h` | +| `FairMQSocket.h` | `fairmq/Socket.h` | +| `FairMQTransportFactory.h` | `fairmq/TransportFactory.h` | +| `FairMQUnmanagedRegion.h` | `fairmq/UnmanagedRegion.h` | + +The check compares the file name without the directory, so `` and `` are also found. The fix keeps the `<>` or `""` of the line. The check looks only at the source file, not at the headers that it includes. + +### fairmq-modernize-channels-access + +The check finds uses of `fChannels` outside of `fair::mq::Device`: + +| Code | Fix | +| --- | --- | +| `fChannels.at("data").at(0)` | `GetChannel("data", 0)` | +| `this->fChannels.at(name).at(i)` | `this->GetChannel(name, i)` | +| `fChannels.count("data")` | `GetChannels().count("data")` | +| `for (auto& kv : fChannels)` | `for (auto& kv : GetChannels())` | ## Build fairmq-tidy @@ -115,70 +88,61 @@ cmake -S FairMQ -B build -DBUILD_TIDY_TOOL=ON cmake --build build ``` -The option needs these packages ([FairMQDependencies.cmake](https://github.com/FairRootGroup/FairMQ/blob/dev/cmake/FairMQDependencies.cmake)): +The tool needs the development files of LLVM and Clang, and the static libraries of clang-tidy (`clangTidy`, `clangTidyMain`). CMake finds Clang next to the LLVM that it found. The CI builds the tool with LLVM 17, 18, 19 and 20. On Ubuntu, install these packages, with `N` the LLVM version: -| Package | Why | -| --- | --- | -| LLVM | `find_package(LLVM)`. The tool links the shared library target `LLVM`. | -| Clang | `find_package(Clang)`. The tool links the shared library target `clang-cpp`. | -| CLI11 | `find_package(CLI11)` is required. The tool does not use it yet. | -| FairLogger 1.6.0 or newer, Boost 1.66 or newer (not 1.88) | CMake looks for them for `BUILD_FAIRMQ` or `BUILD_TIDY_TOOL`. | +```sh +apt install llvm-N-dev llvm-N-tools clang-N libclang-N-dev libclang-cppN-dev clang-tools-N libzstd-dev libedit-dev libcurl4-openssl-dev +``` -If CMake does not find LLVM or Clang, add their installation prefix to `CMAKE_PREFIX_PATH`. The [fairmq/tidy/CMakeLists.txt](https://github.com/FairRootGroup/FairMQ/blob/dev/fairmq/tidy/CMakeLists.txt) builds the executable `fairmq-tidy` with C++17. +To use an LLVM installation that is not on the default search path, give its CMake directory: + +```sh +cmake -S FairMQ -B build -DBUILD_TIDY_TOOL=ON -DLLVM_DIR=/usr/lib/llvm-18/lib/cmake/llvm +``` With `BUILD_TIDY_TOOL=ON`, the installation also contains: - `bin/fairmq-tidy`, exported as the imported target `FairMQ::fairmq-tidy`. CMake installs the exported targets only with `BUILD_FAIRMQ=ON` (the default). -- `share/fairmq/cmake/FairMQTidy.cmake`, the CMake module with [`fairmq_target_tidy()`](#the-cmake-function-fairmq_target_tidy). The directory is `${CMAKE_INSTALL_DATADIR}/fairmq/cmake`. +- `share/fairmq/cmake/FairMQTidy.cmake`, the CMake module with [`fairmq_target_tidy()`](#the-cmake-function-fairmq_target_tidy). - The package component `tidy_tool`. -To check the code of FairMQ itself, also set `-DRUN_FAIRMQ_TIDY=ON`. FairMQ then calls `fairmq_target_tidy()` for its library, its executables and its tests. +With `BUILD_TESTING=ON`, the tests `tidy.` run each check on a test file in `test/tidy/` and compare the result with [FileCheck](https://llvm.org/docs/CommandGuide/FileCheck.html). They need `FileCheck` from the package `llvm-N-tools`. To check the code of FairMQ itself, also set `-DRUN_FAIRMQ_TIDY=ON`. FairMQ then calls `fairmq_target_tidy()` for its library, its executables and its tests. ## Command line ```txt -fairmq-tidy [options] [... ] +fairmq-tidy [options] [... ] [-- ] ``` +The command line is the command line of [clang-tidy](https://clang.llvm.org/extra/clang-tidy/). These options are used most: + | Option | Description | | --- | --- | -| `-p ` | The directory that contains `compile_commands.json`, for example the CMake build directory. Without `-p`, the tool looks for the file in all parent directories of the first source file. | -| `--extra-arg=` | Append an argument to the compile command. Give the option several times for several arguments. | -| `--extra-arg-before=` | Add an argument at the start of the compile command. | -| `--help`, `--help-list` | Print the options and stop. | -| `--version` | Print the version of LLVM and stop. | +| `-p ` | The directory that contains `compile_commands.json`, for example the CMake build directory. | +| `--checks=` | The checks to run, for example `-*,fairmq-*`. Without this option, `fairmq-tidy` adds `fairmq-*` to the checks of the configuration. | +| `--fix` | Apply the fixes to the files. | +| `--warnings-as-errors=` | Make the warnings of these checks errors. The exit code is then not `0` if the checks find something. | +| `--export-fixes=` | Write the fixes to a YAML file for `clang-apply-replacements`. | +| `--extra-arg=`, `--extra-arg-before=` | Add an argument to the end or to the start of the compile command. | +| `--list-checks` | Print the enabled checks and stop. | -The positional arguments are the source files to check. The tool looks each file up in the compilation database. An absolute path must point into the source tree of the build. A relative path must be relative to the current directory, and the current directory must be in the source tree. Without a source file, the tool does nothing. - -If the tool finds no compilation database, it prints an error, then `Running without flags.`, and checks the files without compile flags. Then include paths are missing, and the tool can fail on the first `#include`. - -## Exit codes - -| Code | Meaning | -| --- | --- | -| `0` | The tool processed all files. Findings are warnings: they do not change the exit code. | -| `1` | The command line is not valid, or Clang reported an error for a file, for example a missing header. The tool prints `Error while processing .` | -| `2` | No error, but the tool skipped at least one file without a compile command. It prints `Skipping . Compile command not found.` | - -If the database does not list a file, for example a header, Clang 18 takes the compile command of a similar file from the database. Clang then reads the appended `-Wno-everything` as a file name and prints `error: no such file or directory: '-Wno-everything'`. The tool still checks the file, shows the compiler warnings too, and returns `0`. With Clang 18, `Skipping …` comes when the database has no similar file, for example an empty database. - -`runTool.cpp` returns `EXIT_FAILURE` when it cannot parse the command line. Otherwise it returns the result of `clang::tooling::ClangTool::run()`. - - +`run-clang-tidy` of LLVM runs `fairmq-tidy` in parallel on all files of a compilation database: `run-clang-tidy -clang-tidy-binary fairmq-tidy -p build`. ## The CMake function fairmq_target_tidy The module [`cmake/FairMQTidy.cmake`](https://github.com/FairRootGroup/FairMQ/blob/dev/cmake/FairMQTidy.cmake) defines one function: ```cmake -fairmq_target_tidy(TARGET [EXTRA_ARGS ] [CLANG_EXECUTABLE ]) +fairmq_target_tidy(TARGET [CHECKS ] [EXTRA_ARGS ...]) ``` | Argument | Description | | --- | --- | | `TARGET` | Required. A library or executable target. The function checks its sources with the extension `.cpp` or `.cxx`. | -| `EXTRA_ARGS` | Optional. One argument for the command line of `fairmq-tidy`, for example `--extra-arg=-DMY_MACRO`. The keyword takes one value: the function ignores more values after it. See the caution below. | -| `CLANG_EXECUTABLE` | Optional. The `clang` executable that gives the resource directory when the compiler is GCC. Default: the target `clang` if it exists, otherwise `clang` from `PATH`. | +| `CHECKS` | Optional. The checks to run. Default: `fairmq-*`. The function gives `--checks=-*,`, so the `Checks` of a `.clang-tidy` file have no effect. | +| `EXTRA_ARGS` | Optional. More arguments for `fairmq-tidy`, for example `--fix` or `--warnings-as-errors=fairmq-*`. | + +The argument `CLANG_EXECUTABLE` of earlier versions has no effect. To use the module in your project, find FairMQ with the component and include the module. The package configuration adds `share/fairmq/cmake` to `CMAKE_MODULE_PATH`: @@ -195,21 +159,10 @@ fairmq_target_tidy(TARGET mydevice) The function does these things: - If `CMAKE_EXPORT_COMPILE_COMMANDS` is off, it prints the author warning `CMAKE_EXPORT_COMPILE_COMMANDS is not enabled. Skipping.` and does nothing. It also skips with a warning if `TARGET` is missing or is not a target. -- If `CMAKE_CXX_COMPILER_ID` is `GNU`, it runs `clang -print-resource-dir` one time and keeps the result in the cache variable `fairmq_tidy_clang_resource_dir`. Then it adds `--extra-arg-before=-I/include`, which gives Clang its own header directory. -- For each source file, it adds a custom command: `fairmq-tidy -p=${PROJECT_BINARY_DIR} [extra arguments] `. The command runs in `CMAKE_CURRENT_SOURCE_DIR`. It writes the stamp file `-.fairmq-tidy` into `CMAKE_CURRENT_BINARY_DIR`. +- For each source file, it adds a custom command: `fairmq-tidy -p=${PROJECT_BINARY_DIR} --checks=-*, [extra arguments] `. The command runs in `CMAKE_CURRENT_SOURCE_DIR`. It writes the stamp file `-.fairmq-tidy` into `CMAKE_CURRENT_BINARY_DIR`. - It adds the custom target `fairmq-tidy-` to `ALL` (each `:` in the target name becomes `-`). So the normal build runs the check after it builds the target, and again each time the target changes. -During the build, CMake prints one line for each file, then the output of the tool: - -```txt -[ 80%] fairmq-tidy: Analyzing source file 'mydevice.cxx' of target 'mydevice' -``` - -Findings do not stop the build. If `fairmq-tidy` returns `1` or `2`, the build fails. - -:::caution[EXTRA_ARGS with more than one argument] -The function puts the extra arguments into a generator expression without quotes. If the value is a list of two or more arguments, for example `"--extra-arg=-DA;--extra-arg=-DB"`, CMake splits the expression, the command line is broken, and the build fails. With GCC, the function always adds one argument to the list. So with GCC, any `EXTRA_ARGS` value breaks the command. With other compilers, give at most one argument in `EXTRA_ARGS`. -::: +Findings do not stop the build. With `EXTRA_ARGS --warnings-as-errors=fairmq-*`, a finding stops the build. ## Related pages