<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - llvm-cov: wrong coverage for a loop statement within switch block"
   href="https://bugs.llvm.org/show_bug.cgi?id=45678">45678</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>llvm-cov: wrong coverage for a loop statement within switch block
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>Runtime Libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>libprofile library
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>yangyibiao@hust.edu.cn
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>$ clang -v
clang version 11.0.0 (/home/yibiao/.cache/yay/llvm-git/llvm-project
871beba234a83a2a02da9dedbd59b91a1bfbd7af)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/9.3.0
Found candidate GCC installation:
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/9.3.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/9.3.0
Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/9.3.0
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/9.3.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64

$ clang -w -O0 -g -fcoverage-mapping -fprofile-instr-generate=small.profraw
small.c; ./a.out; llvm-profdata merge small.profraw -o small.profdata; llvm-cov
show a.out -instr-profile=small.profdata small.c > small.c.lcov; cat
small.c.lcov
    1|       |int main()
    2|      1|{
    3|      1|  int size = 4, ret = 0;
    4|      1|  
    5|      1|  switch (size)
    6|      1|  {
    7|      0|    for(;;)
    8|      3|    {
    9|      3|      ret++;
   10|      3|    case 3:
   11|      3|      ret++;
   12|      4|    case 4:
   13|      4|      if (size <= 1)
   14|      1|        break;
   15|      3|      size -= 1;
   16|      3|    }
   17|      1|  }
   18|      1|  return 0;
   19|      1|}


####
We found that, Line #7 is marked as not execute. However, Line 8,9,10,and 11
are both executed three times. The loop statement should executed at least
three times. 
For example, when we setting breakpoint at this small.c:7, lldb will stopped at
this statement as follows:


$ clang -g small.c; lldb ./a.out
(lldb) target create "./a.out"
Current executable set to '/home/yibiao/a.out' (x86_64).
(lldb) b small.c:7
Breakpoint 1: where = a.out`main + 106 at small.c:7:5, address =
0x000000000040117a
(lldb) run
Process 275921 launched: '/home/yibiao/cv-gcov/a.out' (x86_64)
Process 275921 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 1.1
    frame #0: 0x000000000040117a a.out`main at small.c:7:5
   4      
   5      switch (size)
   6      {
-> 7        for(;;)
   8        {
   9          ret++;
   10       case 3:


$ cat small.c
int main()
{
  int size = 4, ret = 0;

  switch (size)
  {
    for(;;)
    {
      ret++;
    case 3:
      ret++;
    case 4:
      if (size <= 1)
        break;
      size -= 1;
    }
  }
  return 0;
}</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>