xref: /aosp_15_r20/external/clang/test/SemaCXX/attr-weak.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify -std=c++11 %s
2*67e74705SXin Li 
3*67e74705SXin Li static int test0 __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
4*67e74705SXin Li static void test1() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
5*67e74705SXin Li 
6*67e74705SXin Li namespace test2 __attribute__((weak)) { // expected-warning {{'weak' attribute only applies to variables, functions and classes}}
7*67e74705SXin Li }
8*67e74705SXin Li 
9*67e74705SXin Li namespace {
10*67e74705SXin Li   int test3 __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
11*67e74705SXin Li   void test4() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
12*67e74705SXin Li }
13*67e74705SXin Li 
14*67e74705SXin Li struct Test5 {
15*67e74705SXin Li   static void test5() __attribute__((weak)); // no error
16*67e74705SXin Li };
17*67e74705SXin Li 
18*67e74705SXin Li namespace {
19*67e74705SXin Li   struct Test6 {
20*67e74705SXin Li     static void test6() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
21*67e74705SXin Li   };
22*67e74705SXin Li }
23*67e74705SXin Li 
24*67e74705SXin Li // GCC rejects the instantiation with the internal type, but some existing
25*67e74705SXin Li // code expects it. It is also not that different from giving hidden visibility
26*67e74705SXin Li // to parts of a template that have explicit default visibility, so we accept
27*67e74705SXin Li // this.
28*67e74705SXin Li template <class T> struct Test7 {
test7Test729*67e74705SXin Li   void test7() __attribute__((weak)) {}
30*67e74705SXin Li   static int var __attribute__((weak));
31*67e74705SXin Li };
32*67e74705SXin Li template <class T>
33*67e74705SXin Li int Test7<T>::var;
34*67e74705SXin Li namespace { class Internal {}; }
35*67e74705SXin Li template struct Test7<Internal>;
36*67e74705SXin Li template struct Test7<int>;
37*67e74705SXin Li 
38*67e74705SXin Li class __attribute__((weak)) Test8 {}; // OK
39*67e74705SXin Li 
40*67e74705SXin Li __attribute__((weak)) auto Test9 = Internal(); // expected-error {{weak declaration cannot have internal linkage}}
41