Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -verify %s
      2 
      3 // Make sure we don't produce invalid IR.
      4 // RUN: %clang_cc1 -emit-llvm-only %s
      5 
      6 namespace test1 {
      7   static void foo(); // expected-warning {{function 'test1::foo' has internal linkage but is not defined}}
      8   template <class T> static void bar(); // expected-warning {{function 'test1::bar<int>' has internal linkage but is not defined}}
      9 
     10   void test() {
     11     foo(); // expected-note {{used here}}
     12     bar<int>(); // expected-note {{used here}}
     13   }
     14 }
     15 
     16 namespace test2 {
     17   namespace {
     18     void foo(); // expected-warning {{function 'test2::(anonymous namespace)::foo' has internal linkage but is not defined}}
     19     extern int var; // expected-warning {{variable 'test2::(anonymous namespace)::var' has internal linkage but is not defined}}
     20     template <class T> void bar(); // expected-warning {{function 'test2::(anonymous namespace)::bar<int>' has internal linkage but is not defined}}
     21   }
     22   void test() {
     23     foo(); // expected-note {{used here}}
     24     var = 0; // expected-note {{used here}}
     25     bar<int>(); // expected-note {{used here}}
     26   }
     27 }
     28 
     29 namespace test3 {
     30   namespace {
     31     void foo();
     32     extern int var;
     33     template <class T> void bar();
     34   }
     35 
     36   void test() {
     37     foo();
     38     var = 0;
     39     bar<int>();
     40   }
     41 
     42   namespace {
     43     void foo() {}
     44     int var = 0;
     45     template <class T> void bar() {}
     46   }
     47 }
     48 
     49 namespace test4 {
     50   namespace {
     51     struct A {
     52       A(); // expected-warning {{function 'test4::(anonymous namespace)::A::A' has internal linkage but is not defined}}
     53       ~A();// expected-warning {{function 'test4::(anonymous namespace)::A::~A' has internal linkage but is not defined}}
     54       virtual void foo(); // expected-warning {{function 'test4::(anonymous namespace)::A::foo' has internal linkage but is not defined}}
     55       virtual void bar() = 0;
     56       virtual void baz(); // expected-warning {{function 'test4::(anonymous namespace)::A::baz' has internal linkage but is not defined}}
     57     };
     58   }
     59 
     60   void test(A &a) {
     61     a.foo(); // expected-note {{used here}}
     62     a.bar();
     63     a.baz(); // expected-note {{used here}}
     64   }
     65 
     66   struct Test : A {
     67     Test() {} // expected-note 2 {{used here}}
     68   };
     69 }
     70 
     71 // rdar://problem/9014651
     72 namespace test5 {
     73   namespace {
     74     struct A {};
     75   }
     76 
     77   template <class N> struct B {
     78     static int var; // expected-warning {{variable 'test5::B<test5::(anonymous namespace)::A>::var' has internal linkage but is not defined}}
     79     static void foo(); // expected-warning {{function 'test5::B<test5::(anonymous namespace)::A>::foo' has internal linkage but is not defined}}
     80   };
     81 
     82   void test() {
     83     B<A>::var = 0; // expected-note {{used here}}
     84     B<A>::foo(); // expected-note {{used here}}
     85   }
     86 }
     87 
     88 namespace test6 {
     89   template <class T> struct A {
     90     static const int zero = 0;
     91     static const int one = 1;
     92     static const int two = 2;
     93 
     94     int value;
     95 
     96     A() : value(zero) {
     97       value = one;
     98     }
     99   };
    100 
    101   namespace { struct Internal; }
    102 
    103   void test() {
    104     A<Internal> a;
    105     a.value = A<Internal>::two;
    106   }
    107 }
    108 
    109 // We support (as an extension) private, undefined copy constructors when
    110 // a temporary is bound to a reference even in C++98. Similarly, we shouldn't
    111 // warn about this copy constructor being used without a definition.
    112 namespace PR9323 {
    113   namespace {
    114     struct Uncopyable {
    115       Uncopyable() {}
    116     private:
    117       Uncopyable(const Uncopyable&); // expected-note {{declared private here}}
    118     };
    119   }
    120   void f(const Uncopyable&) {}
    121   void test() {
    122     f(Uncopyable()); // expected-warning {{C++98 requires an accessible copy constructor}}
    123   };
    124 }
    125 
    126 
    127 namespace std { class type_info; };
    128 namespace cxx11_odr_rules {
    129   // Note: the way this test is written isn't really ideal, but there really
    130   // isn't any other way to check that the odr-used logic for constants
    131   // is working without working implicit capture in lambda-expressions.
    132   // (The more accurate used-but-not-defined warning is the only other visible
    133   // effect of accurate odr-used computation.)
    134   //
    135   // Note that the warning in question can trigger in cases some people would
    136   // consider false positives; hopefully that happens rarely in practice.
    137   //
    138   // FIXME: Suppressing this test while I figure out how to fix a bug in the
    139   // odr-use marking code.
    140 
    141   namespace {
    142     struct A {
    143       static const int unused = 10;
    144       static const int used1 = 20; // xpected-warning {{internal linkage}}
    145       static const int used2 = 20; // xpected-warning {{internal linkage}}
    146       virtual ~A() {}
    147     };
    148   }
    149 
    150   void a(int,int);
    151   A& p(const int&) { static A a; return a; }
    152 
    153   // Check handling of default arguments
    154   void b(int = A::unused);
    155 
    156   void tests() {
    157     // Basic test
    158     a(A::unused, A::unused);
    159 
    160     // Check that nesting an unevaluated or constant-evaluated context does
    161     // the right thing.
    162     a(A::unused, sizeof(int[10]));
    163 
    164     // Check that the checks work with unevaluated contexts
    165     (void)sizeof(p(A::used1));
    166     (void)typeid(p(A::used1)); // xpected-note {{used here}}
    167 
    168     // Misc other testing
    169     a(A::unused, 1 ? A::used2 : A::used2); // xpected-note {{used here}}
    170     b();
    171   }
    172 }
    173 
    174 
    175 namespace OverloadUse {
    176   namespace {
    177     void f();
    178     void f(int); // expected-warning {{function 'OverloadUse::(anonymous namespace)::f' has internal linkage but is not defined}}
    179   }
    180   template<void x()> void t(int*) { x(); }
    181   template<void x(int)> void t(long*) { x(10); } // expected-note {{used here}}
    182   void g() { long a; t<f>(&a); }
    183 }
    184 
    185 namespace test7 {
    186   typedef struct {
    187     void bar();
    188     void foo() {
    189       bar();
    190     }
    191   } A;
    192 }
    193 
    194 namespace test8 {
    195   typedef struct {
    196     void bar(); // expected-warning {{function 'test8::(anonymous struct)::bar' has internal linkage but is not defined}}
    197     void foo() {
    198       bar(); // expected-note {{used here}}
    199     }
    200   } *A;
    201 }
    202 
    203 namespace test9 {
    204   namespace {
    205     struct X {
    206       virtual void notused() = 0;
    207       virtual void used() = 0; // expected-warning {{function 'test9::(anonymous namespace)::X::used' has internal linkage but is not defined}}
    208     };
    209   }
    210   void test(X &x) {
    211     x.notused();
    212     x.X::used(); // expected-note {{used here}}
    213   }
    214 }
    215 
    216 namespace test10 {
    217   namespace {
    218     struct X {
    219       virtual void notused() = 0;
    220       virtual void used() = 0; // expected-warning {{function 'test10::(anonymous namespace)::X::used' has internal linkage but is not defined}}
    221 
    222       void test() {
    223         notused();
    224         (void)&X::notused;
    225         (this->*&X::notused)();
    226         X::used();  // expected-note {{used here}}
    227       }
    228     };
    229     struct Y : X {
    230       using X::notused;
    231     };
    232   }
    233 }
    234 
    235 namespace test11 {
    236   namespace {
    237     struct A {
    238       virtual bool operator()() const = 0;
    239       virtual void operator!() const = 0;
    240       virtual bool operator+(const A&) const = 0;
    241       virtual int operator[](int) const = 0;
    242       virtual const A* operator->() const = 0;
    243       int member;
    244     };
    245 
    246     struct B {
    247       bool operator()() const;  // expected-warning {{function 'test11::(anonymous namespace)::B::operator()' has internal linkage but is not defined}}
    248       void operator!() const;  // expected-warning {{function 'test11::(anonymous namespace)::B::operator!' has internal linkage but is not defined}}
    249       bool operator+(const B&) const;  // expected-warning {{function 'test11::(anonymous namespace)::B::operator+' has internal linkage but is not defined}}
    250       int operator[](int) const;  // expected-warning {{function 'test11::(anonymous namespace)::B::operator[]' has internal linkage but is not defined}}
    251       const B* operator->() const;  // expected-warning {{function 'test11::(anonymous namespace)::B::operator->' has internal linkage but is not defined}}
    252       int member;
    253     };
    254   }
    255 
    256   void test1(A &a1, A &a2) {
    257     a1();
    258     !a1;
    259     a1 + a2;
    260     a1[0];
    261     (void)a1->member;
    262   }
    263 
    264   void test2(B &b1, B &b2) {
    265     b1();  // expected-note {{used here}}
    266     !b1;  // expected-note {{used here}}
    267     b1 + b2;  // expected-note {{used here}}
    268     b1[0];  // expected-note {{used here}}
    269     (void)b1->member;  // expected-note {{used here}}
    270   }
    271 }
    272 
    273 namespace test12 {
    274   class T1 {}; class T2 {}; class T3 {}; class T4 {}; class T5 {}; class T6 {};
    275   class T7 {};
    276 
    277   namespace {
    278     struct Cls {
    279       virtual void f(int) = 0;
    280       virtual void f(int, double) = 0;
    281       void g(int);  // expected-warning {{function 'test12::(anonymous namespace)::Cls::g' has internal linkage but is not defined}}
    282       void g(int, double);
    283       virtual operator T1() = 0;
    284       virtual operator T2() = 0;
    285       virtual operator T3&() = 0;
    286       operator T4();  // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator T4' has internal linkage but is not defined}}
    287       operator T5();  // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator T5' has internal linkage but is not defined}}
    288       operator T6&();  // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator test12::T6 &' has internal linkage but is not defined}}
    289     };
    290 
    291     struct Cls2 {
    292       Cls2(T7);  // expected-warning {{function 'test12::(anonymous namespace)::Cls2::Cls2' has internal linkage but is not defined}}
    293     };
    294   }
    295 
    296   void test(Cls &c) {
    297     c.f(7);
    298     c.g(7);  // expected-note {{used here}}
    299     (void)static_cast<T1>(c);
    300     T2 t2 = c;
    301     T3 &t3 = c;
    302     (void)static_cast<T4>(c); // expected-note {{used here}}
    303     T5 t5 = c;  // expected-note {{used here}}
    304     T6 &t6 = c;  // expected-note {{used here}}
    305 
    306     Cls2 obj1((T7()));  // expected-note {{used here}}
    307   }
    308 }
    309 
    310 namespace test13 {
    311   namespace {
    312     struct X {
    313       virtual void f() { }
    314     };
    315 
    316     struct Y : public X {
    317       virtual void f() = 0;
    318 
    319       virtual void g() {
    320         X::f();
    321       }
    322     };
    323   }
    324 }
    325 
    326 namespace test14 {
    327   extern "C" const int foo;
    328 
    329   int f() {
    330     return foo;
    331   }
    332 }
    333