brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · c6272ef Raw
66 lines · cpp
1// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify -std=c++11 %s2// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify -std=c++11 %s -fexperimental-new-constant-interpreter3 4static int test0 __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}5static void test1() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}6 7namespace test2 __attribute__((weak)) { // expected-warning {{'weak' attribute only applies to variables, functions, and classes}}8}9 10namespace {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}}13}14 15struct Test5 {16  static void test5() __attribute__((weak)); // no error17};18 19namespace {20  struct Test6 {21    static void test6() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}22  };23}24 25// GCC rejects the instantiation with the internal type, but some existing26// code expects it. It is also not that different from giving hidden visibility27// to parts of a template that have explicit default visibility, so we accept28// this.29template <class T> struct Test7 {30  void test7() __attribute__((weak)) {}31  static int var __attribute__((weak));32};33template <class T>34int Test7<T>::var;35namespace { class Internal {}; }36template struct Test7<Internal>;37template struct Test7<int>;38 39class __attribute__((weak)) Test8 {}; // OK40 41__attribute__((weak)) auto Test9 = Internal(); // expected-error {{weak declaration cannot have internal linkage}}42 43[[gnu::weak]] void weak_function();44struct WithWeakMember {45  [[gnu::weak]] void weak_method();46  [[gnu::weak]] virtual void virtual_weak_method();47};48constexpr 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}}50constexpr 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 consistent53// with the ABI rules for member pointers, but seems unprincipled, so we do not54// follow it for now.55// TODO: Consider warning on such comparisons, as they do not test whether the56// virtual member function is present.57constexpr 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}}59 60// Check that no warnings are emitted.61extern "C" int g0;62extern int g0 __attribute__((weak_import));63 64extern "C" int g1 = 0; // expected-note {{previous definition is here}}65extern int g1 __attribute__((weak_import)); // expected-warning {{attribute declaration must precede definition}}66