<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/149105>149105</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang] Semantics of `__attribute__((used))` differ from GCC
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          can-leh-emmtrix
      </td>
    </tr>
</table>

<pre>
    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)

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzkVT2P4zYQ_TVUM1iDoj5sFyq88vmKpIuBRSqDIkcWcxQpiKPdTX59QEnnjRfnIEWKADEIGBpy5r15fMTIEMzVIVaseGbFMZETdX6slHRPFrsn7HsazXvSeP179dKhgykYdwVW8stFEo2mmQgvFyZ2TOymgJqJfVwlB-OC0QgSCPvBSkImajAUIGAvHRkVQJu2xREapDdEB1_rGqTToKx01w2cO4TWW-vfIiRhIFAyIPiJholCJDEHTQAlrUUdUd8iSeX7wVjU8Gaou5V1nrpY6QdHZkSWHRifV8mXpYYhforMOGUnjcCyWgXSxrPsy3L2e3PzlpUhwHnZWz5qYNtnxg_D1FijFgQAgECSjALjaG5s1m-_noXY3RwRn_oTImqbPa9F4m9EmkYH_CPKtseFGvz9Fd0RUd5FJEcQ0qXU_f6rNxo8dThe2skpMt79hfOCuD3eMh-rcsOpWVbPwezAskNIgWXHT8EPZW6FY2YvjbsXDGDONI5uuT_i-qHRvWor_--3vpqgeK4Xj4zw5X2wfsQRfjbuGyuOTOw6oiFEKHFi4nT1uvGWNn68MnH6g4kTFvrlPTRnXKRm_HDuTABpgweLUgcgv_ofHYE17lv0ZoOdfDV-Gjfw0hmLQHePQHmN89nw2MXzKwPtMUTHP3gQj7z-b9r5zkf_Gy_-14z4U_v13GL5q1t0TnSV6X22lwlW6bYQ5bYQfJd0lc4xkzk2vFUll226zZu8bNt9kSnkZcMTUwkuCr5NyzRPM7HbKJmnWhf5tlByv2s5yzn20tiNta99ZJCYECas0nyf8iKxskEb5kEjxGpVEWfOWMWEp2a6BpZzawKFjxJkyM7TackojvDLbXz49h8NonXKtKPvo_WTabTVJ9UMdVOzUb5n4hSh17-nYfS_oSImTnMrgYnT2s1rJf4MAAD__7jYL2M">