Issue 149105
Summary [clang] Semantics of `__attribute__((used))` differ from GCC
Labels clang
Assignees
Reporter can-leh-emmtrix
    When using `__attribute__((used))` inside a template, its semantics differ between GCC and clang. The following test case outputs `test is called` when compiled with GCC and nothing when compiled with clang:

```cpp
#include <cstdio>

template <class T>
class C {
public:
    static int test() {
 puts("test is called");
        return 0;
    }

 __attribute__((used))
    static const int s1;

    static void other_function() {}
};

template <class T>
const int C<T>::s1 = C<T>::test();

int main() {
    C<int>::other_function();
    return 0;
}
```

[Compiler Explorer Link](https://godbolt.org/z/e5dWxsbTe)

This also leads to different linking behaviour. While the following code links when compiled with clang, it does not when compiled with GCC:

```cpp
template <class T>
class C {
public:
    static int test();

    __attribute__((used))
    static const int s1;

    static void other_function() {}
};

template <class T>
const int C<T>::s1 = C<T>::test();

int main() {
 C<int>::other_function();
    return 0;
}
```

[Compiler Explorer Link](https://godbolt.org/z/KfGTfe6Yn)

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to