[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / attr-weak.cpp
blobc6272ef5786efd0a4096117dd4c679a4b9459eb0
1 // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify -std=c++11 %s
2 // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify -std=c++11 %s -fexperimental-new-constant-interpreter
4 static int test0 __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
5 static void test1() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
7 namespace test2 __attribute__((weak)) { // expected-warning {{'weak' attribute only applies to variables, functions, and classes}}
10 namespace {
11 int test3 __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
12 void test4() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
15 struct Test5 {
16 static void test5() __attribute__((weak)); // no error
19 namespace {
20 struct Test6 {
21 static void test6() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
25 // GCC rejects the instantiation with the internal type, but some existing
26 // code expects it. It is also not that different from giving hidden visibility
27 // to parts of a template that have explicit default visibility, so we accept
28 // this.
29 template <class T> struct Test7 {
30 void test7() __attribute__((weak)) {}
31 static int var __attribute__((weak));
33 template <class T>
34 int Test7<T>::var;
35 namespace { class Internal {}; }
36 template struct Test7<Internal>;
37 template struct Test7<int>;
39 class __attribute__((weak)) Test8 {}; // OK
41 __attribute__((weak)) auto Test9 = Internal(); // expected-error {{weak declaration cannot have internal linkage}}
43 [[gnu::weak]] void weak_function();
44 struct WithWeakMember {
45 [[gnu::weak]] void weak_method();
46 [[gnu::weak]] virtual void virtual_weak_method();
48 constexpr bool weak_function_is_non_null = &weak_function != nullptr; // expected-error {{must be initialized by a constant expression}}
49 // expected-note@-1 {{comparison against address of weak declaration '&weak_function' can only be performed at runtime}}
50 constexpr bool weak_method_is_non_null = &WithWeakMember::weak_method != nullptr; // expected-error {{must be initialized by a constant expression}}
51 // expected-note@-1 {{comparison against pointer to weak member 'WithWeakMember::weak_method' can only be performed at runtime}}
52 // GCC accepts this and says the result is always non-null. That's consistent
53 // with the ABI rules for member pointers, but seems unprincipled, so we do not
54 // follow it for now.
55 // TODO: Consider warning on such comparisons, as they do not test whether the
56 // virtual member function is present.
57 constexpr bool virtual_weak_method_is_non_null = &WithWeakMember::virtual_weak_method != nullptr; // expected-error {{must be initialized by a constant expression}}
58 // expected-note@-1 {{comparison against pointer to weak member 'WithWeakMember::virtual_weak_method' can only be performed at runtime}}
60 // Check that no warnings are emitted.
61 extern "C" int g0;
62 extern int g0 __attribute__((weak_import));
64 extern "C" int g1 = 0; // expected-note {{previous definition is here}}
65 extern int g1 __attribute__((weak_import)); // expected-warning {{attribute declaration must precede definition}}