diff --git a/compiler-rt/lib/profile/GCDAProfiling.c b/compiler-rt/lib/profile/GCDAProfiling.c --- a/compiler-rt/lib/profile/GCDAProfiling.c +++ b/compiler-rt/lib/profile/GCDAProfiling.c @@ -616,6 +616,12 @@ fn_list_insert(&writeout_fn_list, fn); } +#ifndef _WIN32 +// __attribute__((destructor)) and destructors whose priorities are greater than +// 100 run before this function and can thus be tracked. Compatible with GCC 7 +// onwards. +__attribute__((destructor(100))) +#endif COMPILER_RT_VISIBILITY void llvm_writeout_files(void) { struct fn_node *curr = writeout_fn_list.head; @@ -626,10 +632,6 @@ } curr = curr->next; } -} - -COMPILER_RT_VISIBILITY -void llvm_delete_writeout_function_list(void) { fn_list_remove(&writeout_fn_list); } @@ -710,8 +712,9 @@ /* Make sure we write out the data and delete the data structures. */ atexit(llvm_delete_reset_function_list); atexit(llvm_delete_flush_function_list); - atexit(llvm_delete_writeout_function_list); +#ifdef _WIN32 atexit(llvm_writeout_files); +#endif } } diff --git a/compiler-rt/test/profile/Posix/gcov-destructor.c b/compiler-rt/test/profile/Posix/gcov-destructor.c new file mode 100644 --- /dev/null +++ b/compiler-rt/test/profile/Posix/gcov-destructor.c @@ -0,0 +1,26 @@ +/// Test that destructors and destructors whose priorities are greater than 100 are tracked. +// RUN: mkdir -p %t.dir && cd %t.dir +// RUN: %clang --coverage %s -o %t +// RUN: rm -f gcov-destructor.gcda && %run %t +// RUN: llvm-cov gcov -t gcov-destructor.gcda | FileCheck %s + +__attribute__((constructor)) // CHECK: -: [[@LINE]]:__attribute__ +void constructor() { // CHECK-NEXT: 1: [[@LINE]]: +} + +/// Runs before __llvm_gcov_writeout. +__attribute__((destructor)) // CHECK: -: [[@LINE]]:__attribute__ +void destructor() { // CHECK-NEXT: 1: [[@LINE]]: +} + +__attribute__((destructor(101))) // CHECK: -: [[@LINE]]:__attribute__ +void destructor_101() { // CHECK-NEXT: 1: [[@LINE]]: +} + +/// Runs after __llvm_gcov_writeout. +__attribute__((destructor(99))) // CHECK: -: [[@LINE]]:__attribute__ +void destructor_99() { // CHECK-NEXT: #####: [[@LINE]]: +} + +int main() { +}