Modernizing C++
If you are an experienced C++ developer looking to setup their VSCode environment with clang-tidy or if you're a complete beginner wondering what the heck clang-tidy is, you've come to the right place. IDEs like CLion support clang-tidy quite beautifully in my opinion. It helps you with some helpful suggestions but at the same time it doesn't shove it down your throat. You can choose whether you want to adhere to those standards. Today we will see how we can setup something similar in VSCode.
So what is clang-tidy then?
clang-tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis. clang-tidy is modular and provides a convenient interface for writing new checks. - clang tools documentation
While this can be helpful for an experienced programmer (I guess?) this is utterly useless for a beginner. So I'll tell you what clang-tidy is and what it can do for you.
Simply put...
clang-tidy looks at our code and suggests "better" alternatives. This is especially helpful because C++ is a sea of tools, where even the simplest of tasks can be completed in a multitude of ways, and it's still being updated. The newer C++ 11 features like auto and override, are easy to neglect or miss, even if you're a beginner or an oldie. So whenever clang-tidy sees a part of our code where auto can be incorporated or override can be used, it makes that suggestion to us, and it's our decision to act on it or not.
Consider this code.
It contains a class called Parent with a pure virtual function, which is implemented by its child class Child. This is perfectly valid C++ code, but since C++ 11, the override keyword is used to tell both the developers and the compiler that this function is in fact an override of another. You can see in the screenshot below where I have installed clang-tidy on VS Code, there is a red squiggly under the override.
(modernize-use-override is the check option there. See below for more details on check options). As you can see, at the top of that box, clang-tidy tells us, "annotate this function as override.". And if I click the light bulb icon that appears when I click on the part with the squiggly line, we get,
- I'm assuming you already have VS Code installed already.
- Install LLVM toolchain. Go to their download Github page and select the one suitable for you. (The setups and sources will be grouped by operating system and processor architecture)
- During the installation of LLVM, make sure to tell the setup to add the bin to the path. If you missed this step, refer to the internet on how to setup the path variable when using clang.
- You should install the clangd extension in VS Code. (Refer to the internet if you don't know how)
Down to work now...
- Open vs code in a new folder. Create a file called main.cpp
- Paste the following code into that file and save it. This is the same code I showed you earlier.
- Now create a file called ".clang-tidy" without quotes.
- Paste the following line without angle brackets inside that file and save it.
<Checks: '-*, modernize-*, -modernize-use-trailing-return-type'>
- Now if both files have been saved, you will see the squiggly line under the getId() function and feel free to hover the mouse over it, click it and resolve it.
- If this doesn't work as advertised you should probably restart VS Code. (Just close it and open it again :)
Beware
Even though clang-tidy seems like a great tool, you should always take it with a grain of salt. Too much of anything is bad right?
Even though clang-tidy assures optimization, you should also consider that it may sometimes obstruct the readability of the code, which makes a huge impact on the quality of the code. You should never compromise the readability of your code for an unnecessary optimization. Remember, "Premature optimization is the root of all evil".
Also whenever you make a change in your code adhering to clang-tidy, only accept their fix if you understand what it changes and why they suggest it over your own code. If you don't understand why, maybe search the internet. But even after some research you still don't get it, you probably shouldn't make the change. Better ignore it.
More about clang-tidy
The clang-tidy extension we installed, looks at the .clang-tidy file to determine, which suggestions it should make. The line <Checks: '-*, modernize-*, -modernize-use-trailing-return-type'> is the simplest form.
Now note that there are a huuuuge amount of checks, and if we use all of them, we will never get anything done. That one line you pasted on the .clang-tidy file, tells clang-tidy what it should check in our code.
The first argument, '-*' tells that first disable all the checks. Then, modernize-* means activate all the checks belonging to the modernize group. Modernize is a list of checks to encourage the use of C++ 11 features. Here is a list of a few of them. You can see all of them here. Some of the following are self explanatory and some are explained below.
- modernize-avoid-c-arrays
- Discourage the use of c-arrays and encourage the use of std::array
- modernize-make-shared and modernize-make-unique
- Encouraging making shared pointers and unique pointers using std::make_shared and std::make_unique respectively
- modernize-pass-by-value
- Reminding users that return value optimization exists, and hence passing by value is sometimes better than passing by reference
- modernize-use-emplace
- Reminding users to use emplace instead of push_back and other related functions, to stop unnecessary copying.
These are just a few of them.
Finally what '-modernize-use-trailing-return-type' does is it again removes the modernize-use-trailing-return-type check, because I didn't like it for several reasons. You can remove this one and use this check as well.
So now you get that putting a - in front of a check means it must be removed. And the * means "all".
Good Luck!
With that I bid you farewell, and I'll see you again on tech_salad_sl with more irrelevant knowledge. LOL.
Make sure to refer to the official clang tools documentation here because even though somewhat complicated, it is still the best one.
As usual if you have any comments or complaints, you can comment on the post, or contact me at oshanathpraveen3@gmail.com. See ya later!




Well explained
ReplyDeleteGreatly explained
ReplyDeleteOn point!!❤️
ReplyDeleteVery well explained π₯π₯π₯
ReplyDelete