Coverage of LLVM/Clang source code - clang

I am building LLVM and Clang. What option should I pass in cmake to enable coverage of LLVM and Clang source code.
Note, I want to get the coverage of LLVM source code itself. For example, if I run clang t.c, I want to see which parts of LLVM have been run.

use make ENABLE_COVERAGE=1 after 'cmake'

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.

not able to generate controll flow graphs for big programs using LLVM clang

I want to create a software-generated control flow diagram for this code using LLVM CFG https://github.com/inmcm/Simon_Speck_Ciphers/blob/master/C/speck.c
But when I followed the instructions given on the LLVM webpage (https://www.programmersought.com/article/61364910575/), and typed the following command: clang -S -emit-llvm simon.c -o g.ll
Terminal shows this error message:
clang: error: -emit-llvm cannot be used when linking
I am new to this tool and Linux, can someone please help?
I was able to generate the llvm IR and the CFG dot files for the above mentioned speck.c file. The message cannot be used when linking appears only when you haven't passed any special flags for output like -S, -c, etc. so I'm guessing you have made some typo in the command.
You can try using clang -save-temps speck.c command, which would generate the bitcode (.bc) file for the LLVM IR, which can be used in any place where the .ll file is used.

Compile Clang with alternative sysroot

I am compiling clang 11.1.0 from source and I want it to be configured to use an alternative sysroot by default, such that when I compile a program using clang it will take the system headers and libraries from this alternative sysroot path by default.
For example suppose I have /path/to/my/toolchain as my sysroot.
When I configured and built gcc I used the options --with-sysroot=/path/to/my/toolchain and --with-native-headers=/path/to/my/toolchain. Now every time I compile a program with gcc it takes the system headers/libraries automatically from /path/to/my/toolchain.
I want to do the same with clang. The only way I could get it working so far is to have the option -isysroot /path/to/my/toolchain every time I compile a program. Is there a way to specify the sysroot path when compiling clang so that it becomes the default sysroot when running clang?
Use DEFAULT_SYSROOT cmake variable. E.g. cmake -DDEFAULT_SYSROOT=<your_sysroot_path>

Use clang to translate Linux kernel source code into LLVM bitcode

I am trying to use LLVM to do some static analysis on some programs. Specifically, I am targeting Linux kernel source code. However, in the first step of translating the source code of one single file in Linux kernel to LLVM bitcode file, I got stuck.
The command I am using is:
clang -Iinclude/path -c -emit-llvm one_single_file_in_linux_kernel \
-o bitcode_file.bc
The search path for clang is correct, but there are other errors, such as __always_inline not found. I think there should be some other flags I should use, but I don't know.
Has anyone have encountered this problem?

Using scan-build command for clang code analysis

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 ...

Resources