Code coverage with clang++ - code-coverage

Is there a way to find code coverage using clang++ for C++ code.
I know gcov but it works only on the code compiled with GCC.

EDIT:
Clang should be able to generate code coverage these days (taking the same command line arguements as gcc), but you will probably want a newish build and I'm still having trouble running lcov on the output. (see Clang Code Coverage Invalid Output)

I've written a too called kcov which can collect code coverage as long as you compile with debug info (-g), regardless of the compiler.
It also has a plugin for the address-sanitizer coverage generator, although that stuff isn't working very well yet.
I'd say it's a lot more straight-forward to use than gcov+lcov.

Related

generate llvm bytecode for translation unit in clang plugin

Good afternoon. In the clang documentation, I found a way to generate and paste code. So far, I have not used this method for the plugin, but if I understand correctly, it is possible (correct if not right - thanks).
I would like to know if it is possible to change the code at the bytecode level in the plugin, i.e. insert new stmt(clang::Stmt and its derivatives) into existing ones and give them to the compiler?
Pasting code at the source code level (clang::Rewriter) changes the project's source files, so it adds extra work.
Thank you

clang -module-file-info doesn't generate any output

I'm trying to move a cross-compiled CMake project to Clang Modules to see whether compile time reduction is worth it. However, it seems that Clang is generating lots of duplicate modules in it's ModuleCache.
I'd like to figure out why (maybe some CMake config, etc), so I'm trying to run clang -module-file-info on the generated module files.
However, clang's output is just empty whenever I provide a proper module file. Am I doing anything wrong? Is there anything special that I need to take care of?
The files all have a reasonable size (from a few kB to a few MB), look fine in a Hex editor (start with CPCH, have some recognizable strings, etc) and whenever I specify a wrong file (or a file compiled with a different version of clang) I get the appropriate errors.
I've tried with clang 7.0.1 as well as 8.0.0.
I also tried --verbose but that didn't show any problems either.
To answer my own question:
clang doesn't output the stats on the command line, it puts it into a file by default written in the current directory.

Generating a Code-Coverage Test Report for iOS/Swift

Code coverage in Xcode is a testing option supported by LLVM. When you
enable code coverage, LLVM instruments the code to gather coverage
data based on the frequency that methods and functions are called. The
code coverage option can collect data to report on tests of
correctness and of performance, whether unit tests or UI tests.
I would like to generate a code-coverage report via Xcode.
I've looked at gcovr http://gcovr.com.
...or is it more prudent to generate a report via xcodebuild?
Is there a tutorial or guide to generating a report via Xcode 8+?
There is -enableCodeCoverage YES option for xcodebuild. You can easily see possibilities of xcodebuild command by typing xcodebuild --help.
Here are Apple guildelines on code coverage usage: https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/07-code_coverage.html
You can even see the coverage report directly from Xcode.

LLVM compilation process and LLD

I've been trying to make the switch to LLVM, since I'd like to get more into the whole 'software-dev' scene, and it seems like right now, LLVM is the future. I built LLVM/Clang/LLD/compiler-rt/libcxx from source several times now, both with GNU/GCC and LLVM/Clang.
The problem appears when I try to use the newly compiled compilers. From what I can see, clang is using GNU ld rather than LLVM's lld. Is this true?
LLD seems to be a very limited program from the lld -help output, but from what I have read, it is as full featured as ld. I cannot find documentation on how to use it anywhere -- does anyone know where I can find some kind of comprehensive manual on it?
Thank you.
Pass -fuse-ld=lld to clang to make it use lld for linking. By now, it's in very good shape.
You can pass -v or -### to clang to make it print which linker command it runs or would run.
There's no manual for the moment and depending on platform may work well enough for you. That said, if lld were "production ready" we'd have switched clang to using it by default for the various platforms. It's not there yet so I wouldn't suggest you use it for your day to day development.
The LLVM team say that is production ready because FreeBSD can compile and link a lot of things with LLD.
The documentation on the LLD project can be found on http://lld.llvm.org/.
It's written :
LLD is a drop-in replacement for the GNU linkers.
That accepts the same command line arguments and linker scripts as GNU.
So you can use same arguments than GNU LD.
I know this question is old, but there is a newer solution to it:
To use the ld.lld linker when building any llvm target, just pass -DLLVM_ENABLE_LLD=ON in the commandline to cmake.
//Use lld as C and C++ linker.
LLVM_ENABLE_LLD:BOOL=TRUE
For other cmake projects, pass: -DCMAKE_LINKER=/etc/bin/ld.lld

Make an LLVM ModulePass available on clang command line

I have a ModulePass that's working with the opt tool, but I'm having trouble figuring out how to make it available to clang at the command line. My current workflow for using my pass is:
clang -c -emit-llvm [c-source code files]
llvm-link [llvm bitcode files]
opt -load [PassName].so -[pass-name] [linked llvm file]
llc [resulting bitcode file]
gcc [resulting assembler file] -o [target]
I would like to get my pass integrated with the clang command line so that it could be invoked as part of the build of existing software (e.g. c++ standard library) without having to remake the whole build system for each thing I compile. I've seen hints about how to do this, but I haven't been able to put the pieces together into a working setup.
Run an LLVM Pass Automatically with Clang describes exactly what I want, but the method appears to be deprecated in LLVM 3.4 (PassManagerBuilder has been moved to the legacy namespace).
LLVM - Run Own Pass automatically with clang seems to address the basic issue, but I was hoping I could do this without having to modify clang (which seems to be what's suggested there).
What is the best way to make a new pass available from clang using LLVM 3.4?
Clang still uses PassManagerBuilder as of 3.5 (see the PassManagerBuilderWrapper class in BackendUtil.cpp). So I believe extending it with RegisterStandardPasses, as in my blog post, is still the only way to add a pass to Clang's pass manager.
It's frustratingly difficult to find any information about how deprecated the "old" pass manager infrastructure is. But since Clang is still using it, it can't be that deprecated. :)

Resources