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
7 // RUN: %clang --coverage -fPIC -shared shared.c -o libfunc.so
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
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
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
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
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
foo(int n)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
87 int bar1 = 0;
88 int bar2 = 1;
89
bar(int n)90 void bar(int n) {
91 if (n % 5 == 0)
92 bar1++;
93 else
94 bar2++;
95 }
96
main(int argc,char * argv[])97 int main(int argc, char *argv[]) {
98 #ifdef SHARED_CALL_BEFORE_FLUSH
99 foo(1);
100 #endif
101
102 bar(5);
103 __gcov_dump();
104 __gcov_reset();
105 bar(5);
106
107 #ifdef SHARED_CALL_AFTER_FLUSH
108 foo(1);
109 #endif
110
111 #ifdef EXIT_ABRUPTLY
112 _exit(0);
113 #endif
114
115 bar(5);
116 return 0;
117 }
118 #endif
119