This is a text-only version of the following page on https://raymii.org: --- Title : Run one specific clang-tidy check on your entire codebase Author : Remy van Elst Date : 05-04-2021 URL : https://raymii.org/s/snippets/Run_one_specific_clang-tidy_check_on_your_codebase.html Format : Markdown/HTML --- Recently I did a major refactor on a piece of code that involved thousands of lines of code which were in one way or another related to string handling. All of the code handled `char*` (C style character pointer arrays) and the concept of `const` or ownership was literally unknown in that part of the codebase. The refactored code uses `std::string`'s, but due to the legacy nature, a large number of methods returned `nullptr`'s instead of empty strings (like `""`). I understand why this was done, but finding all those instances and the fact it only gives a runtime error was a bit of a bummer.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.

Luckily `clang-tidy` is here to save the day. In my IDE, CLion, it gives a warning when you return a `nullptr`. It however does that only in the file you're currently editing, and since we're talking millions of files, I wasn't going to open them by hand. You can run `clang-tidy` easily on one file, and it's not hard to run it on an entire codebase as well, using the script `run-clang-tidy.py`, provided in their packages. This snippet shows you how to run [one specific][4] `clang-tidy` check, in my case, `bugprone-string-constructor`, on a (cmake and C++) codebase. Here's the clang-tidy message in CLion: ![screenshot][3] ### Example code with undefined behaviour This is an example piece of code demonstrating the behavior: #include #include class Example { public: std::string getName() { return nullptr; } }; int main() { Example ex; std::cout << "Example: " << ex.getName() << std::endl; return 0; } If you try to run the above code example, you'll get a runtime error: terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid Opinions on `nullptr` and `std::string` [differ][1] [depending][2] on who you ask, but as of now it's not possible to construct a `std::string` with a `nullptr`. ### Run clang-tidy on you entire codebase Make sure you have `clang-tidy` installed: apt install clang-tidy Navigate into your project folder: cd my/cpp/project If you haven't already, create a build folder (`mkdir build; cd build`) and run `cmake` with an extra flag to create the compilation database for `clang-tidy`: cmake .. -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Debug In the build folder, run `run-clang-tidy`. It might be a different command (`run-clang-tidy.py` or `run-clang-tidy-VERSIONNUMBER`) depending on your distro's packaging preference. run-clang-tidy -extra-arg=-Wno-unknown-warning-option -checks='-*,bugprone-string-constructor' 2>&1 | tee -a clang-tidy-result This will take a while, when the command is finished, you can look at the results, or in the file `clang-tidy-result`. In my case it gave specific filenames and line numbers where it found the undefined behavior. The `-extra-arg` was required due to some other compiler extension flag for our code, you can probably omit that. The `-checks='-*'` disables all checks, the next `,bugprone-string-constructor` enables [only the specific string check][4] I want to run. You can add more specific checks, separate them by a comma. An example with just 2 checks enabled: -checks='-*,bugprone-string-constructor,bugprone-string-integer-assignment' An up to date list of `clang-tidy` checks can be [found on the LLVM website][5]. [1]: https://web.archive.org/web/20180302201006/https://stackoverflow.com/questions/49058133/why-doesnt-stdstring-take-a-null-pointer [2]: https://web.archive.org/web/20200412122443/https://stackoverflow.com/questions/10771864/assign-a-nullptr-to-a-stdstring-is-safe [3]: /s/inc/img/nullptr-string.png [4]: https://web.archive.org/web/20210406070324/https://clang.llvm.org/extra/clang-tidy/checks/bugprone-string-constructor.html [5]: https://clang.llvm.org/extra/clang-tidy/ --- License: All the text on this website is free as in freedom unless stated otherwise. This means you can use it in any way you want, you can copy it, change it the way you like and republish it, as long as you release the (modified) content under the same license to give others the same freedoms you've got and place my name and a link to this site with the article as source. This site uses Google Analytics for statistics and Google Adwords for advertisements. You are tracked and Google knows everything about you. Use an adblocker like ublock-origin if you don't want it. All the code on this website is licensed under the GNU GPL v3 license unless already licensed under a license which does not allows this form of licensing or if another license is stated on that page / in that software: This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Just to be clear, the information on this website is for meant for educational purposes and you use it at your own risk. I do not take responsibility if you screw something up. Use common sense, do not 'rm -rf /' as root for example. If you have any questions then do not hesitate to contact me. See https://raymii.org/s/static/About.html for details.