In this lab, we are going to add a formatter and a linter to our project to regulate our contributors on their submissions. I think this lab is very interesting since I've always been seeing my submitted code on the repositories that I contributed for Hacktoberfest was checked by a component called Linter and it really helped me to find some stupid mistakes I made (for example, unused variables, misspell). Before this lab, I had already been considering implementing a Linter in my repository. Luckily, we have lab7.
The tools I used
The tools I used are clang-formatter and clang-tidy. As their names suggest, clang-formatter is for code-formatting and clang-tidy is for code-tidying(linting). These tools are pre-built with LLVM(Clang) which means it doesn't require any extra software, and also, this is the reason why I chose them.
Clang-tidy has helped me find many unsafe usages in my code, for example, strcpy() is the string copying function that I used throughout SSGifier, however, this function does not check the boundary of the string it copies, which could potentially cause memory buffer overwritten issue which can be used as a means to exploit our code. Instead, I changed it to strncpy() which copies string with respect to the boundary of the string.
Documents
Setup
Clang-formatter
To set up clang-formatter the only thing we need to do is create a config file named .clang-formatter
. For more convenience, we can use this command clang-format -style=llvm -dump-config > .clang-format
to generate one and customize it based on preferences.
Clang-tidy
Similar to clang-formatter, a config file named .clang-tidy
is also needed for clang-tidy. We can use clang-tidy --dump-config
to generate one, however, I don't recommend using this command to generate because the generated config file has many redundant properties, I would suggest using this config instead.
Run
To run clang-formatter, we should use the command clang-formatter -i -style=file *.cpp *.h
. For the attributes we pass to clang-formatter, -style=file
means we want to load the default config file for formatter to load, -i
means we want to write the formatted lines back to the original files instead of printing it on the terminal. And also, we can pass the files to be checked into clang-formatter, and we can use *.cpp
and *.h
to check all cpp files and header files.
To run clang-tidy, we can use the command clang-tidy --extra-arg="--std=c++17" *.cpp *.h
to check all cpp files and header files. This command will load and apply the config file .clang-tidy
by default. Be careful --extra-arg="--std=c++17"
is very important if you have used any function in C++17, such as std::filesystem
.
Visual Studio Code Integration
Clang-tidy and Clang-format are bundled with a C++ extension plugin on Visual Studio Code. To use them, we need to follow these steps:
Clang-format
- Install C++ extension plugin from
Extensions
tab in Visual Studio Code. - Format document with VS Code's default formatter(
Ctrl + Alt + L
by default) and .clang-format will be used as the guideline of the formatter.
Clang-tidy
- Install C++ extension plugin from
Extensions
tab in Visual Studio Code . - Open action search by pressing
Ctrl+Shift+P
and typeRun Code Analysis
to search the action ofclang-tidy
. - Select
C/C++:Run Code Analysis on All Files
and wait for the checking process to be done. - select PROBLEMS tab on the bottom panel to check the suggested problem by clang-tidy.
Top comments (0)