Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / attr-weak.cpp
blobf065bfd9483f8a127f4add93d6ecf988f4b5d08d
1 // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify -std=c++11 %s
3 static int test0 __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
4 static void test1() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
6 namespace test2 __attribute__((weak)) { // expected-warning {{'weak' attribute only applies to variables, functions, and classes}}
9 namespace {
10 int test3 __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
11 void test4() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
14 struct Test5 {
15 static void test5() __attribute__((weak)); // no error
18 namespace {
19 struct Test6 {
20 static void test6() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
24 // GCC rejects the instantiation with the internal type, but some existing
25 // code expects it. It is also not that different from giving hidden visibility
26 // to parts of a template that have explicit default visibility, so we accept
27 // this.
28 template <class T> struct Test7 {
29 void test7() __attribute__((weak)) {}
30 static int var __attribute__((weak));
32 template <class T>
33 int Test7<T>::var;
34 namespace { class Internal {}; }
35 template struct Test7<Internal>;
36 template struct Test7<int>;
38 class __attribute__((weak)) Test8 {}; // OK
40 __attribute__((weak)) auto Test9 = Internal(); // expected-error {{weak declaration cannot have internal linkage}}
42 [[gnu::weak]] void weak_function();
43 struct WithWeakMember {
44 [[gnu::weak]] void weak_method();
45 [[gnu::weak]] virtual void virtual_weak_method();
47 constexpr bool weak_function_is_non_null = &weak_function != nullptr; // expected-error {{must be initialized by a constant expression}}
48 // expected-note@-1 {{comparison against address of weak declaration '&weak_function' can only be performed at runtime}}
49 constexpr bool weak_method_is_non_null = &WithWeakMember::weak_method != nullptr; // expected-error {{must be initialized by a constant expression}}
50 // expected-note@-1 {{comparison against pointer to weak member 'WithWeakMember::weak_method' can only be performed at runtime}}
51 // GCC accepts this and says the result is always non-null. That's consistent
52 // with the ABI rules for member pointers, but seems unprincipled, so we do not
53 // follow it for now.
54 // TODO: Consider warning on such comparisons, as they do not test whether the
55 // virtual member function is present.
56 constexpr bool virtual_weak_method_is_non_null = &WithWeakMember::virtual_weak_method != nullptr; // expected-error {{must be initialized by a constant expression}}
57 // expected-note@-1 {{comparison against pointer to weak member 'WithWeakMember::virtual_weak_method' can only be performed at runtime}}