115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall// RUN: %clang_cc1 -fsyntax-only -verify %s
215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall// Make sure we don't produce invalid IR.
415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall// RUN: %clang_cc1 -emit-llvm-only %s
515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallnamespace test1 {
715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  static void foo(); // expected-warning {{function 'test1::foo' has internal linkage but is not defined}}
815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  template <class T> static void bar(); // expected-warning {{function 'test1::bar<int>' has internal linkage but is not defined}}
915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
1015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  void test() {
1115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    foo(); // expected-note {{used here}}
1215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    bar<int>(); // expected-note {{used here}}
1315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
1415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
1515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
1615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallnamespace test2 {
1715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  namespace {
1815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    void foo(); // expected-warning {{function 'test2::<anonymous namespace>::foo' has internal linkage but is not defined}}
1915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    extern int var; // expected-warning {{variable 'test2::<anonymous namespace>::var' has internal linkage but is not defined}}
2015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    template <class T> void bar(); // expected-warning {{function 'test2::<anonymous namespace>::bar<int>' has internal linkage but is not defined}}
2115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
2215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  void test() {
2315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    foo(); // expected-note {{used here}}
2415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    var = 0; // expected-note {{used here}}
2515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    bar<int>(); // expected-note {{used here}}
2615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
2715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
2815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
2915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallnamespace test3 {
3015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  namespace {
3115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    void foo();
3215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    extern int var;
3315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    template <class T> void bar();
3415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
3515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
3615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  void test() {
3715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    foo();
3815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    var = 0;
3915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    bar<int>();
4015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
4115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
4215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  namespace {
4315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    void foo() {}
4415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    int var = 0;
4515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    template <class T> void bar() {}
4615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
4715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
4815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
4915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallnamespace test4 {
5015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  namespace {
5115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    struct A {
5215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      A(); // expected-warning {{function 'test4::<anonymous namespace>::A::A' has internal linkage but is not defined}}
5315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      ~A();// expected-warning {{function 'test4::<anonymous namespace>::A::~A' has internal linkage but is not defined}}
5415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      virtual void foo(); // expected-warning {{function 'test4::<anonymous namespace>::A::foo' has internal linkage but is not defined}}
5515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      virtual void bar() = 0;
5615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall      virtual void baz(); // expected-warning {{function 'test4::<anonymous namespace>::A::baz' has internal linkage but is not defined}}
5715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    };
5815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
5915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
6015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  void test(A &a) {
6115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    a.foo(); // expected-note {{used here}}
6215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    a.bar();
6315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    a.baz(); // expected-note {{used here}}
6415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
6515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
6615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  struct Test : A {
6715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    Test() {} // expected-note 2 {{used here}}
6815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  };
6915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
7015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
7115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall// rdar://problem/9014651
7215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCallnamespace test5 {
7315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  namespace {
7415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    struct A {};
7515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
7615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
7715e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  template <class N> struct B {
7815e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    static int var; // expected-warning {{variable 'test5::B<test5::<anonymous>::A>::var' has internal linkage but is not defined}}
7915e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    static void foo(); // expected-warning {{function 'test5::B<test5::<anonymous>::A>::foo' has internal linkage but is not defined}}
8015e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  };
8115e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall
8215e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  void test() {
8315e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    B<A>::var = 0; // expected-note {{used here}}
8415e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall    B<A>::foo(); // expected-note {{used here}}
8515e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall  }
8615e310a3b970b64a84cb30f0005bc396b4d978cbJohn McCall}
8777efc680156bc28d3c0d2e70f156904f91328b21John McCall
8877efc680156bc28d3c0d2e70f156904f91328b21John McCallnamespace test6 {
8977efc680156bc28d3c0d2e70f156904f91328b21John McCall  template <class T> struct A {
9077efc680156bc28d3c0d2e70f156904f91328b21John McCall    static const int zero = 0;
9177efc680156bc28d3c0d2e70f156904f91328b21John McCall    static const int one = 1;
9277efc680156bc28d3c0d2e70f156904f91328b21John McCall    static const int two = 2;
9377efc680156bc28d3c0d2e70f156904f91328b21John McCall
9477efc680156bc28d3c0d2e70f156904f91328b21John McCall    int value;
9577efc680156bc28d3c0d2e70f156904f91328b21John McCall
9677efc680156bc28d3c0d2e70f156904f91328b21John McCall    A() : value(zero) {
9777efc680156bc28d3c0d2e70f156904f91328b21John McCall      value = one;
9877efc680156bc28d3c0d2e70f156904f91328b21John McCall    }
9977efc680156bc28d3c0d2e70f156904f91328b21John McCall  };
10077efc680156bc28d3c0d2e70f156904f91328b21John McCall
10177efc680156bc28d3c0d2e70f156904f91328b21John McCall  namespace { struct Internal; }
10277efc680156bc28d3c0d2e70f156904f91328b21John McCall
10377efc680156bc28d3c0d2e70f156904f91328b21John McCall  void test() {
10477efc680156bc28d3c0d2e70f156904f91328b21John McCall    A<Internal> a;
10577efc680156bc28d3c0d2e70f156904f91328b21John McCall    a.value = A<Internal>::two;
10677efc680156bc28d3c0d2e70f156904f91328b21John McCall  }
10777efc680156bc28d3c0d2e70f156904f91328b21John McCall}
10830028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth
10930028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth// We support (as an extension) private, undefined copy constructors when
11030028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth// a temporary is bound to a reference even in C++98. Similarly, we shouldn't
11130028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth// warn about this copy constructor being used without a definition.
11230028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruthnamespace PR9323 {
11330028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth  namespace {
11430028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth    struct Uncopyable {
11530028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth      Uncopyable() {}
11630028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth    private:
11730028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth      Uncopyable(const Uncopyable&); // expected-note {{declared private here}}
11830028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth    };
11930028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth  }
12030028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth  void f(const Uncopyable&) {}
12130028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth  void test() {
12230028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth    f(Uncopyable()); // expected-warning {{C++98 requires an accessible copy constructor}}
12330028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth  };
12430028234f38945981ebf9c8a2cb915fc2f9a63a4Chandler Carruth}
125