In my concurrency class, I use more than 60 programs. Most of the programs consist of a single source file. I’m quite curious if the static code analysis tool CppDepend can find any issues in my sources. Let me try it out.
My Special Use-Case
My use case is unique for two reasons. First, I don’t expect too many issues with my examples for one reason: I give quite often concurrency classes and use these examples in my classes; therefore, I had many code reviewer. Second, my programs are most of the times not really sophisticated and quite short. They should only serve one purpose: show the particular concurrency features in isolation.
Due to my unique circumstances, here are my steps to get the static code analysis with CppDepend.
- Generate a CMake file for Visual Studio, GCC, and Clang
- Generate a Visual Studio Project from CMake
- Import the Visual Studio Project project into CppDepend
- Make the code analysis in CppDepend
Here we go.
1. Generate a CMake file for Visual Studio, GCC, and Clang
My customers work with Visual Studio, GCC, or Clang. To provide a way to compile all C++ sources to executables automatically, I use CMake. In sum, I have more than 300 C++ sources files in various directories. Each source file should become an executable. Writing a CMakeList.txt manually for each directory would be a boring step. Additionally, I would have to adapt it each time, if I modified the file names. Terrible!. To automate the boring stuff, I wrote a small python script generateCMakeFile.py. generateCMakeFile.py generates a CMakeList.txt file for the directory, in which I invoke it.
Here it is.
To understand the program, you should start with the three print statements add the end. The brown font goes directly into CMakeList.txt.
Using the generateCMakeFile.py is straightforward. I copied the generateCMakeFile.py to the directory, I wanted to use it. This step is not necessary but make the commandline easier to write.
Finally, this is the autogenerate CMakeList.txt.
2. Generate a Visual Studio Project from CMake
To make my CppDepend life more comfortable, I switch from Linux to Windows and continue with Visual Studio.
This step creates a 64-bit Visual Studio Project in the directory “C:\Users\raine\build“.
3. Import the Visual Studio Project project into CppDepend
Now, it’s time to start CppDepend and create a new project Concurrency:
Next, I add Visual Studio to my new project.
One step is still missing. I have to press the analyse button. It took my eight cores a few second to analyse the source files.
4. Make the code analysis in CppDepend
As I already assumed it, the most checks are green:
Of course, I’m only interested in the red (Error) and yellow (Warning) ones.
- Quality Gates:
- This issue boils down to mainly one function: The method is too big, has too many local variables, and is poorly documented. What I liked about CppDepend was that it shows me the metric I violated.
- Code Smells:
- The previous break of the Quality Gates was also a Code Smell; therefore, I can ignore this warning.
- Object Oriented Design:
- Only one of the thirty rules was violated: three of my constructors taking one argument were not explicit. Honestly, this is a rule I often preach in my seminars. Shame on me.
- This is also an issue I should fix. I passed a std::string by copy.
- Dead Code:
- This violation was funny. The violation was that the destructor of the singleton was never called. To my excuse, I only used a singleton to explain the thread-safe initialisation of a variable.
- Naming Conventions:
- I’m not a big fan of Hungarian notation. Therefore, I wouldn’t prefix an instance variable with a m_ or a static with a s_.
Here is my short resume. I put not too much effort into the analysis of CppDepend but more in establishing an automated process to create the Visual Studio solution as input for CppDepend. Regardless, I found immediately a few big issues which I’m going to fix. It’s quite comfortable to fix these issues because a double-click on the Error/Warning in CppDepend opens the source file in Visual Studio.
Although I only scratched at the surface of CppDepend, the analysis of my concurrency source files impressed me. I will, therefore, invest in the future more time to better understand the various metrics of CppDepend and apply them to my other source files. My gut feeling is that I can draw many benefits from CppDepend.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,