https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110561

            Bug ID: 110561
           Summary: gcov counts closing bracket in a function as
                    executable, lowering coverage statistics
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: gcov-profile
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carlosgalvezp at gmail dot com
                CC: marxin at gcc dot gnu.org
  Target Milestone: ---

Created attachment 55481
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55481&action=edit
Line 18 wrongly reported as lacking code coverage

Hi!

We are using GCC trunk on Ubuntu 22.04
(d08f2e4f74583e27002368989bba197f8eb7f6d2).

Consider the following code example:

#include <vector>
#include <string>

std::string joinPath(std::vector<std::string> const& path_parts)
{
    std::string path{};
    for (auto const& path_part : path_parts)
    {
        if (!path.empty())
        {
            path += "/";
        }

        path += path_part;
    }

    return path;
}

int main()
{
  std::vector<std::string> strings = {"foo", "bar"};
  joinPath(strings);
}

Following this process (taken from here:
https://medium.com/@xianpeng.shen/use-gcov-and-lcov-to-perform-code-coverage-testing-for-c-c-projects-c85708b91c78)
to compute coverage:

$ g++ main.cpp --coverage -o main
$ ./main
$ gcov main.cpp
$ lcov --capture --directory . --output-file coverage.info
$ genhtml coverage.info --output-directory out

Leads to a coverage report that says that line 18 is missing code coverage,
which is incorrect since it's just the closing bracket of the function (see
attached picture).

I can see that things go wrong already at the gcov output, so it's not a
rendering/LCOV issue:

$ cat main.cpp.gcov 
        -:    0:Source:main.cpp
        -:    0:Graph:main.gcno
        -:    0:Data:main.gcda
        -:    0:Runs:1
        -:    1:#include <vector>
        -:    2:#include <string>
        -:    3:
        1:    4:std::string joinPath(std::vector<std::string> const&
path_parts)
        -:    5:{
        1:    6:    std::string path{};
        3:    7:    for (auto const& path_part : path_parts)
        -:    8:    {
        2:    9:        if (!path.empty())
        -:   10:        {
        1:   11:            path += "/";
        -:   12:        }
        -:   13:
        2:   14:        path += path_part;
        -:   15:    }
        -:   16:
        1:   17:    return path;
    =====:   18:}
        -:   19:
        1:   20:int main()
        -:   21:{
        2:   22:  std::vector<std::string> strings = {"foo", "bar"};
        1:   23:  joinPath(strings);
        1:   24:}

This problem does not happen always, just on particular cases (I haven't been
able to establish a pattern).

Do you know what could be the problem? Thanks!

Reply via email to