| 1 | /// This test fails on Mac (https://bugs.llvm.org/show_bug.cgi?id=38134) |
| 2 | // XFAIL: darwin |
| 3 | |
| 4 | // RUN: mkdir -p %t.d && cd %t.d |
| 5 | |
| 6 | // RUN: %clang -E -DSHARED %s -o shared.c -dumpdir ./ |
| 7 | // RUN: %clang --coverage -fPIC -shared shared.c -o libfunc.so -dumpdir ./ |
| 8 | // RUN: test -f shared.gcno |
| 9 | |
| 10 | /// Test the case where we exit abruptly after calling __gcov_dump, which means we don't write out the counters at exit. |
| 11 | // RUN: %clang -DEXIT_ABRUPTLY -DSHARED_CALL_BEFORE_FLUSH -DSHARED_CALL_AFTER_FLUSH --coverage %s -L%t.d -rpath %t.d -lfunc -o %t -dumpdir ./ |
| 12 | // RUN: test -f gcov-shared-flush.gcno |
| 13 | |
| 14 | // RUN: rm -f gcov-shared-flush.gcda shared.gcda |
| 15 | // RUN: %run %t |
| 16 | // RUN: llvm-cov gcov -t gcov-shared-flush.gcda | FileCheck %s --check-prefix=NO_WRITEOUT |
| 17 | // RUN: llvm-cov gcov -t shared.gcda | FileCheck %s --check-prefix=SHARED |
| 18 | |
| 19 | // NO_WRITEOUT: -: [[#%u,L:]]:#ifdef EXIT_ABRUPTLY |
| 20 | // NO_WRITEOUT-NEXT: 1: [[#%u,L+1]]: _exit(0); |
| 21 | |
| 22 | // SHARED: 1: {{[[0-9]+}}:void foo(int n) |
| 23 | |
| 24 | /// Test the case where we exit normally and we have a call to the shared library function before __gcov_dump. |
| 25 | // RUN: %clang -DSHARED_CALL_BEFORE_FLUSH --coverage %s -L%t.d -rpath %t.d -lfunc -o %t -dumpdir ./ |
| 26 | // RUN: test -f gcov-shared-flush.gcno |
| 27 | |
| 28 | // RUN: rm -f gcov-shared-flush.gcda shared.gcda |
| 29 | // RUN: %run %t |
| 30 | // RUN: llvm-cov gcov -t gcov-shared-flush.gcda | FileCheck %s --check-prefix=BEFORE |
| 31 | // RUN: llvm-cov gcov -t shared.gcda | FileCheck %s --check-prefix=SHARED_ONCE |
| 32 | |
| 33 | // BEFORE: -: {{[0-9]+}}:#ifdef SHARED_CALL_BEFORE_FLUSH |
| 34 | // BEFORE-NEXT: 1: {{[0-9]+}}: foo(1); |
| 35 | // BEFORE: 1: {{[0-9]+}}: __gcov_dump(); |
| 36 | // BEFORE-NEXT: 1: {{[0-9]+}}: __gcov_reset(); |
| 37 | // BEFORE: -: {{[0-9]+}}:#ifdef SHARED_CALL_AFTER_FLUSH |
| 38 | // BEFORE-NEXT: -: {{[0-9]+}}: foo(1); |
| 39 | // BEFORE: 1: {{[0-9]+}}: bar(5); |
| 40 | |
| 41 | // SHARED_ONCE: 1: {{[0-9]+}}:void foo(int n) |
| 42 | |
| 43 | // # Test the case where we exit normally and we have a call to the shared library function after __gcov_dump. |
| 44 | // RUN: %clang -DSHARED_CALL_AFTER_FLUSH --coverage %s -L%t.d -rpath %t.d -lfunc -o %t -dumpdir ./ |
| 45 | // RUN: test -f gcov-shared-flush.gcno |
| 46 | |
| 47 | // RUN: rm -f gcov-shared-flush.gcda shared.gcda |
| 48 | // RUN: %run %t |
| 49 | // RUN: llvm-cov gcov -t gcov-shared-flush.gcda | FileCheck %s --check-prefix=AFTER |
| 50 | // RUN: llvm-cov gcov -t shared.gcda > 2s.txt |
| 51 | |
| 52 | // AFTER: -: {{[0-9]+}}:#ifdef SHARED_CALL_BEFORE_FLUSH |
| 53 | // AFTER-NEXT: -: {{[0-9]+}}: foo(1); |
| 54 | // AFTER: 1: {{[0-9]+}}: __gcov_dump(); |
| 55 | // AFTER-NEXT: 1: {{[0-9]+}}: __gcov_reset(); |
| 56 | // AFTER: -: {{[0-9]+}}:#ifdef SHARED_CALL_AFTER_FLUSH |
| 57 | // AFTER-NEXT: 1: {{[0-9]+}}: foo(1); |
| 58 | // AFTER: 1: {{[0-9]+}}: bar(5); |
| 59 | |
| 60 | // # Test the case where we exit normally and we have calls to the shared library function before and after __gcov_dump. |
| 61 | // RUN: %clang -DSHARED_CALL_BEFORE_FLUSH -DSHARED_CALL_AFTER_FLUSH --coverage %s -L%t.d -rpath %t.d -lfunc -o %t -dumpdir ./ |
| 62 | // RUN: test -f gcov-shared-flush.gcno |
| 63 | |
| 64 | // RUN: rm -f gcov-shared-flush.gcda shared.gcda |
| 65 | // RUN: %run %t |
| 66 | // RUN: llvm-cov gcov -t gcov-shared-flush.gcda | FileCheck %s --check-prefix=BEFORE_AFTER |
| 67 | // RUN: llvm-cov gcov -t shared.gcda | FileCheck %s --check-prefix=SHARED_TWICE |
| 68 | |
| 69 | // BEFORE_AFTER: -: {{[0-9]+}}:#ifdef SHARED_CALL_BEFORE_FLUSH |
| 70 | // BEFORE_AFTER-NEXT: 1: {{[0-9]+}}: foo(1); |
| 71 | // BEFORE_AFTER: 1: {{[0-9]+}}: __gcov_dump(); |
| 72 | // BEFORE_AFTER-NEXT: 1: {{[0-9]+}}: __gcov_reset(); |
| 73 | // BEFORE_AFTER: -: {{[0-9]+}}:#ifdef SHARED_CALL_AFTER_FLUSH |
| 74 | // BEFORE_AFTER-NEXT: 1: {{[0-9]+}}: foo(1); |
| 75 | // BEFORE_AFTER: 1: {{[0-9]+}}: bar(5); |
| 76 | |
| 77 | // SHARED_TWICE: 2: {{[0-9]+}}:void foo(int n) |
| 78 | |
| 79 | #ifdef SHARED |
| 80 | void foo(int n) { |
| 81 | } |
| 82 | #else |
| 83 | extern void foo(int n); |
| 84 | extern void __gcov_dump(void); |
| 85 | extern void __gcov_reset(void); |
| 86 | extern void _exit(int) __attribute__((noreturn)); |
| 87 | |
| 88 | int bar1 = 0; |
| 89 | int bar2 = 1; |
| 90 | |
| 91 | void bar(int n) { |
| 92 | if (n % 5 == 0) |
| 93 | bar1++; |
| 94 | else |
| 95 | bar2++; |
| 96 | } |
| 97 | |
| 98 | int main(int argc, char *argv[]) { |
| 99 | #ifdef SHARED_CALL_BEFORE_FLUSH |
| 100 | foo(1); |
| 101 | #endif |
| 102 | |
| 103 | bar(n: 5); |
| 104 | __gcov_dump(); |
| 105 | __gcov_reset(); |
| 106 | bar(n: 5); |
| 107 | |
| 108 | #ifdef SHARED_CALL_AFTER_FLUSH |
| 109 | foo(1); |
| 110 | #endif |
| 111 | |
| 112 | #ifdef EXIT_ABRUPTLY |
| 113 | _exit(0); |
| 114 | #endif |
| 115 | |
| 116 | bar(n: 5); |
| 117 | return 0; |
| 118 | } |
| 119 | #endif |
| 120 | |