llvm-project/clang/test/Modules/static-initializer.cppm
Chuanqi Xu 259eaa6878
[C++20] [Modules] Fix the duplicated static initializer problem (#114193)
Reproducer:

```
//--- a.cppm
export module a;
int func();
static int a = func();

//--- a.cpp
import a;
```

The `func()` should only execute once. However, before this patch we
will somehow import `static int a` from a.cppm incorrectly and
initialize that again.

This is super bad and can introduce serious runtime behaviors.

And also surprisingly, it looks like the root cause of the problem is
simply some oversight choosing APIs.
2024-10-30 17:27:04 +08:00

19 lines
455 B
C++

// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/a.cppm -emit-module-interface -o %t/a.pcm
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/a.cpp -fmodule-file=a=%t/a.pcm -emit-llvm -o - | FileCheck %t/a.cpp
//--- a.cppm
export module a;
int func();
static int a = func();
//--- a.cpp
import a;
// CHECK-NOT: internal global
// CHECK-NOT: __cxx_global_var_init