https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97919
Bug ID: 97919 Summary: [GCOV]volatile member in struct lead to incorrect code coverage Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: gcov-profile Assignee: unassigned at gcc dot gnu.org Reporter: yangyibiao at outlook dot com CC: marxin at gcc dot gnu.org Target Milestone: --- $ cat test.c static unsigned int s_irq[10]; static struct S { volatile unsigned int slock; unsigned int irq; } sp[10]; static __inline__ __attribute__((always_inline)) int f1(int irq) { return ((irq == 2) ? 9 : irq); } void f(void) { struct S *up; int i; for (i=0, up=sp; i<10; i++, up++) up->irq = f1(s_irq[i]); } int main(void){ f(); } $ gcc -O0 --coverage test.c; ./a.out; gcov test.c; cat test.c.gcov File 'test.c' Lines executed:100.00% of 6 Creating 'test.c.gcov' -: 0:Source:test.c -: 0:Graph:test.gcno -: 0:Data:test.gcda -: 0:Runs:1 -: 1:static unsigned int s_irq[10]; -: 2: -: 3:static struct S { -: 4: volatile unsigned int slock; -: 5: unsigned int irq; -: 6:} sp[10]; -: 7: -: 8:static __inline__ __attribute__((always_inline)) int f1(int irq) -: 9:{ 10*: 10: return ((irq == 2) ? 9 : irq); -: 11:} -: 12: 1: 13:void f(void) -: 14:{ -: 15: struct S *up; -: 16: int i; -: 17: 11: 18: for (i=0, up=sp; i<10; i++, up++) 20: 19: up->irq = f1(s_irq[i]); 1: 20:} -: 21: 1: 22:int main(void){ f(); } ################ We can found that Line 19 is wrongly marked as executed 20 times. When debug this program in debugger, it only hit 10 time. $ gcc --version; gcov --version gcc (GCC) 10.0.1 20200419 (experimental) Copyright (C) 2020 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. gcov (GCC) 10.0.1 20200419 (experimental) Copyright (C) 2020 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.