Learning how to use Static Analysis tools

JLi - Nov 5 '21 - - Dev Community

So this week we learned about static analysis tools such as formatters and linters. I also got a little prior experience with using these analysis tools because of my Hacktoberfest PRs, two of the repositories I made PRs had these tools and also had bots on their repo that automatically checked that these tools were ran.

For my LENNAH SSG, I decided to use Clang for both the formatter and linter. I chose to use clang because it had both the formatter and linter in one package for C++ and it look extremely user friendly, especially their Visual Studio extension. Clang also has commands you can use in the command line to run both the formatter and linter if you don't want to use extensions. However, this took me a bit of time figuring out how to do, so I will give a quick explanation on how to set it up to simply the process.

Installing + Running Clang for Command Line

  1. Download the build here Image description
  2. Then when running the installer make sure to add LLVM to the system PATH image
  3. Once installed, in the terminal go to the project folder and then you can use the following commands to format or find lint.
$ clang-format -style=file LennahSSG/*.cpp LennahSSG/*.h
Enter fullscreen mode Exit fullscreen mode
$ clang-tidy --config="" LennahSSG/*.cpp LennahSSG/*.h --extra-arg=-std=c++17 --extra-arg=-xc++ --
Enter fullscreen mode Exit fullscreen mode

Installing Clang Extension for Visual Studio

  1. Open Visual Studio and go to the Extensions drop down at the top.
  2. Open Manage Extensions and search for Clang Power Tools.
  3. Then download and restart Visual Studio.
  4. You will have new buttons in the top of your bar that will allow you to format, tidy, and use other features Clang provides. For more information you can check out their getting started guide on their website.

Formatting and Removing Lint from LENNAH

When I ran the formatter and linter I found out that there were a good handful of simple changes that actually made my code nicer to read. The biggest change was the use of additional whitespace before lines. Specifically for parts where I am outputting to a file using << whenever I made a new line it wouldn't line up with the first line. For example:

    outputFile << "</ul>\n"
        << "</body>\n"
        << "</html>\n";
Enter fullscreen mode Exit fullscreen mode

But after the formatter it made the readability much better and more satisfying to look at:

    outputFile << "</ul>\n"
               << "</body>\n"
               << "</html>\n";
Enter fullscreen mode Exit fullscreen mode

This was a small change but something I didn't really think much about because of how Visual Studio's base spacing worked. I think this is a really nice fix that improves the code greatly, even though it is just simple spacing.

As for the Linter, there was only one piece of lint in my code and it was an extra boolean variable that I forgot to remove from a previous iteration of a function. That was very nice because it was just a small variable surrounded by many other variables so it would have taken me a long time to notice that, were it not for the Linter.

This was a great learning experience for setting up Static Analysis tools, and it really shows how powerful they can be. I will always be using these tools, especially the extensions, from now on because they are so easy to use and they make quality of life so much better. The way I have been writing code before is like using Microsoft Word without spell/grammar check, literally making it so much harder for myself. So be sure to do yourself a solid and look into static analysis tools for your own projects if you haven't already. Until next time take care!

. . . . . . . . . . . . . . . . . . . . . .