Validation C code in AS project by SonarQube

We are attempting to analyze the quality of our C code in the project using SonarQube, but this is more challenging for C code, and we have not yet achieved success. Our Pipeline (Azure) includes a Build Wrapper step designed to encapsulate the build process. However, since BR.AS.Build.exe serves as the top-level component that incorporates not only the C compiler but also others beneath it, this approach does not function as intended. Is there a way to extract a “Compilation Database” from the BR project, which could then be used for analysis via SonarQube? Or is there another way how to check quality of C code in BR project?

You could take a look at : GitHub - br-automation-community/BnR-DevOps-Package: Contains materials for implementing DevOps practices for Automation Studio project development.

Testing/Template Files/StaticCodeAnalysis contains powershell scripts used together with the clang-tidy tool (LLVM Download Page).

The script generates a compile_command.json database and a file_list.txt which contains information about needed include files for the different c-files, which clang-tidy needs.

Perhaps this is similar to what SonarQube is using.

3 Likes

Hi @Lukas_Hubeny, I’m curious about the result of code quality testing. Can you share with us if you achieved your goal and how?

Here is the result of my efforts:
I was able to a get compilation database with StaticCodeAnalysis package. Files compile_commands.json and file_list.txt was created in project in \Temp folder after build), but I had two problems..
a) use the shorthand switch -config= instead of --config-file= in RunClangTidy.bat (in the Clang-Tidy call) - AI advise
b) the environment variable BR_HOME was not known so I defined out path for BR home location in helpermod.psm1 file

clang-tidy.exe ended with the error I attach in the picture.. Since the compilation database is important to me for SonarQube, I don’t mind the clang-tidy.exe error :slight_smile:
Unfortunately, I can’t confirm yet that the compilation database works with SonarQube, because I have some problems with the Pipeline and Build project on our server. Anyway, thanks for providing the package that creates the compilation database. :slight_smile:

Defined configuration defined as parameter in Post-build action did get somewhere where needed…

The only problem I known off is that the commandline can get to long, because all files form the file_list.txt are added to the commandline.

You could change the script by calling clang-tidy seperately for every file form file_list.txt.

# Read file list
if (Test-Path $FileListFile) {
	$RawFiles = Get-Content -Path $FileListFile -Raw
	$Files = $RawFiles -split '\s+'   # Split on any whitespace
		foreach ($file in $Files) {
			if ($file.Trim()) {
			& "$ClangTidyPath" -p="$TempPath" --config-file="$ConfigFile" "$file"
		}
	}
} else {
    Write-Host "File list not found: $FileListFile"
}
1 Like

ok I marked your answer as a solution. Feel free to update us when you fix your problem with pipeline on your server. Any know-how (howTos) can be shared also as a separate article in Share Info & Ideas. I think there would be couple of people interested about it :slight_smile:

1 Like