What are the branches at the end of this function. How could I cover them?
LCOV branches at the end of a function
5.5k Views Asked by jsj AtThere are 3 best solutions below

As a super simplistic answer, branches signify the IF/ELSE branch. So for every if/else there's two new branches (that should be covered); and if nested the grow exponentially.
function twoNewBranches() {
if () {
// code
} else {
// code
}
}
function twoNewBranchesNotAparent() {
if () {
// code
}
}
function fourNewBranches() {
if () {
if () {
// code
} else {
// code
}
}
}
• The first function twoNewBranches creates two new branches that would need to be covered
• The second function twoNewBranchesNotAparent also creates two new branches, since you still have to cover the test that doesn’t satisfy the if statement
• The third function fourNewBranches creates four (2^2=4) new branches to cover. Two nested, the parent of the nested, and the hidden else.
Overall keep in mind covering branches, is about covering the conditional statements.

I had the same problem with end brackets which were not covered in a void function;
I found two workarounds:
first add the endbracket to the last functioncall line so they dont show up as individual line
second and better: add random "return;" at the end of the function to force the code to be executed
You are observing the gcc generated code for the destruction of static storage duration (global) variables.
Your coverage shows that the function
foo
has been entered three times, however the counter near the end of the scope shows that the code was executed eight times, including branches that you enquire about.Now you must consider that the compiler puts the header file in the translation unit and that gcov doesn't see your code exactly as it is, but rather as a control flow graph of assembly instruction with branching as the edges of the graph.
Thus the "end of
foo
scope" in the lcov html output is not really the end of thefoo
method scope but rather everything that's included afterfoo
as well in the entire translation unit, including the destruction of global variables that have been declared in the header file.The header itself hasn't been included in the question, but even the most basic
__static_initialization_and_destruction
assembly that gcc generates has a number of branches included.Note that you may have included global variables or you may have not - gcc still might generate this code for every translation unit.
Look at the underlying output of gcov:
And look at the generated assembly, trimmed to clarify the point: