Using scan-build command for clang code analysis - clang

I have installed scan-build/clang version 2.9 on Ubuntu desktop. I build my C++ source code there using make . As it said scan-build would analyze a project which is built using make if you give
scan-build make
to
but after the make i see a message
scan-build: Removing '/tmp/scan-build-2013-10-16-1' because it contains no reports.
Also tried
scan-build --use-c++=/use/bin/clang++ make
Q1 - What am i doing wrong/missing here. How to use scan-build to analyze all source files.
Q2 - Is there any option to use clang++ --analyze myfile.cpp
to analyze single source file. But it gives an error about a header file included not found 'fatal' error' my.h
what is the option to clang analyze to point it to the folder having header files.

As for Q2, you should be able to use:
scan-build clang++ -c myfile.cpp
or what you suggested:
clang++ --analyze myfile.cpp
but you need to make sure that the compiler knows about all the includes and libraries (you should be able to successfully compile myfile.cpp to an object file without analysis). That includes especially the -I directories.
There is also the -o option to scan-build, which specifies the target directory for HTML report files. Subdirectories will be created as needed to represent separate "runs" of the analyzer. If this option is not specified, a directory is created in /tmp to store the reports, as you already know.
Another useful option would be -v (verbose), which should print any errors that the analyzer might run into.
Last but not least, you should use the analysis with debug builds where the optimization is disabled, but more importantly where the symbols are not stripped.
Not sure if it helps, let me know ...

Related

Bazel C++ code coverage with a custom coverage tool (Squish Coco)

I couldn't find anything in the official documentation on this. We're using Squish Coco for code coverage and can't use lcov for certification reasons, which is the only tool supported by bazel coverage.
We're trying to get this running in Bazel. For this we added a toolchain for the Squish Coco Wrappers. Which seems to be the standard way to add different compilers, shown here https://bazel.build/tutorials/ccp-toolchain-config.
However linking fails with undefined reference to '__cs_tb...', because Bazel deletes all the instrumentation files (*.o.csmes and *.csmes). The same toolchain for clang or gcc works, as presented in the tutorial.
Is there any way to tell Bazel to keep any files with the ending *.csmes? I'd like to declare that cc_library produces .o files and maybe ( not always, depends on instrumentation options) *.o.csmes. cc_binary amd cc_test may create no extension files and .csmes files.

Running clang static checker at project level (iOS app) rather than file by file (xcodebuild)

After solving this Omitted code blocks from clang AST for ObjectiveC, I've tried it on a small Objective C .m file along with an appropriate compile_commands.json and it works properly and I get the entire syntax tree.
Now I'm trying to see if it's possible to run it on the entire xcodebuild
[
{
"directory" : "/Users/xx/Desktop/iOSApplication",
"command" : "xcodebuild clean build CODE_SIGN_IDENTITY=\"\" CODE_SIGNING_REQUIRED=NO -project /Users/xx/Desktop/iOSApplication/iOSApplication.xcodeproj/",
"file" : "iOSApplication.xcodeproj"
}
]
When I tried to then run clang-check -ast-dump /Users/xx/Desktop/iOSApplication/iOSApplication.xcodeproj it gives me the errors error: unknown argument: '-project' and error: unable to handle compilation, expected exactly one compiler job in ''
Is it actually possible to run the AST based checker on the entire xcodeproject? Or how should I go about compiling the files 1 at a time?
I've managed to generate the compile_commands.json by following this guide here http://docs.oclint.org/en/stable/guide/xcodebuild.html
However, I'd still like to be able to run my RecursiveASTVisitor on the entire projects. Or alternatively, pass in the xcode project and enumerate all the source files would probably work too.
Anyone has ideas how to go about passing entire xcodebuild project as parameter for RecursiveASTVisitor?
Right now I'm running my ASTVisitor like this ./MyASTChecker ~/Desktop/directory/sample1.m but I'd like to make it do something like ./MyASTChecker ~/Desktop/directory/sampleproject.xcodeproj
The way I do it is at compile time using scan-build. This works for me with cmake/make based projects.
scan-build --use-analyzer=clang -enable-checker <checker_name> make
This will read the makefile and build everything in there while running the specified checker on each file as it's compiled. If you only want to build with some flags or a specific target, you can do this:
scan-build --use-analyzer=clang -enable-checker <checker_name> make <build_options>
If you instead have a cmake based project. You might first want to generate a makefile in a build directory. I do this for that:
cmake <path_to_cmakelists> -DCMAKE_CXX_COMPILER=c++-analyzer -DCMAKE_CC_COMPILER=ccc-analyzer
This followed by scan-build from above will compile and run checker on the source files while each file is being compiled.
I have only tried this with CMAKE / MAKE but should work with xcode like this:
scan-build --use-analyzer=clang -enable-checker <checker_name> xcodebuild
and with build options
scan-build --use-analyzer=clang -enable-checker <checker_name> xcodebuild <build_options>
You can read more about scan-build here

Clang flag to suppress creation of files?

Is there a CLI flag for clang that will go through the entire compilation process, including assembling and linking, printing all errors or warnings, but will not create files on the disk?
In particular, I don't need any .out files, nor object (.o) files.
When you're generating a single file, you can use the -o option to choose the output file, so -o /dev/null will just get rid of the output without generating any file. You'll still get warnings and errors printed to stderr.
This works in both gcc and clang.

is Gcov an application based coverage or system based coverage

Is Gcov an application based coverage or system based coverage
.gcda files doesn't get generated for all the files. the gcda files are created only for few source files. gcno files are created for all the source files complied with the gcov complilation flags. My project also has QT based modules for which i don't get .gcda files.
Question here is : is gcov application based coverage tool or system based coverage tool ??
I have added the -fprofile-arcs -ftest-coverage in CFLAGS LDFLAGS and CXXFLAGS.
Compiling your code with -fprofile-arcs -ftest-coverage will generate .gcno files.
After executing your binary .gcda will be generated.So in your case some of the binary will not be executed . If .gcno files are w.r.t files then surely it will generate .gcda after executing your binary.
To answer your question, you need to understand how gcov work. Just read this (essentially there are four steps):
How do the code coverage options of GCC work?
To summarize even further, basically gcc will compile and "instrument" the functions so every execution of the function will increase or update some counters. In this way, you can get functions statistics when the program end.
To answer your question: you have just compile some C program with gcov, but did not compile Qt with gcov, but only link to it as a Qt library, so definitely there will be no gcov related statistics.
So it is not application nor system based generated statistics, but is depending on which C files which have gcov enabled during compilation.

how to exclude third party lib / dll from getting analyzed in CLANG scan-build xcodebuild?

I have used some open-source code and third party libs in my project and want to exclude that code from getting analyzed while analyzing my project using scan-build file.
I know we can #ifndef clang_analyzer use this macro to suppress the code from getting analysed, but I dont want to copy paste this in all the files.
OR
Is there any way so that the report which get generated after analysis using scan-build command, not to show the warnings/error generated from some set of files?
thanks in advance.
Use --exclude [1] [2] option (available since 2018)
--exclude
Do not run static analyzer against files found in this directory
(You can specify this option multiple times). Could be useful when
project contains 3rd party libraries.
Same is applicable for Python implementation of scan-build ($ pip install scan-build) - https://github.com/rizsotto/scan-build

Resources