1762bb9d0ad20320b9f97a841dce57ba5e8e48b07Richard Smith// RUN: %clang_cc1 -std=c++11 -fexceptions -fcxx-exceptions -fsyntax-only -verify %s
27acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
37acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl// Simple parser tests, dynamic specification.
47acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
57acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redlnamespace dyn {
67acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
77acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  struct X { };
87acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
97acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  struct Y { };
107acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
117acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  void f() throw() { }
127acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
137acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  void g(int) throw(X) { }
147acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
157acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  void h() throw(X, Y) { }
167acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
177acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  class Class {
187acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    void foo() throw (X, Y) { }
197acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  };
207acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
217acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  void (*fptr)() throw();
227acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
237acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl}
247acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
257acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl// Simple parser tests, noexcept specification.
267acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
277acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redlnamespace noex {
287acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
2960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  void f1() noexcept { }
3060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  void f2() noexcept (true) { }
3160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  void f3() noexcept (false) { }
3260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  void f4() noexcept (1 < 2) { }
337acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
3460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  class CA1 {
357acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    void foo() noexcept { }
367acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl    void bar() noexcept (true) { }
377acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  };
387acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
3960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  void (*fptr1)() noexcept;
4060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  void (*fptr2)() noexcept (true);
417acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
427acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl}
437acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
4460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redlnamespace mix {
457acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
467acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  void f() throw(int) noexcept { } // expected-error {{cannot have both}}
477acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl  void g() noexcept throw(int) { } // expected-error {{cannot have both}}
487acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl
497acafd032e145dbdbbed9274ca57ec2c86b912bcSebastian Redl}
5060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
5160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl// Sema tests, noexcept specification
5260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
5360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redlnamespace noex {
5460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
5560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  struct A {};
5660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
5760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  void g1() noexcept(A()); // expected-error {{not contextually convertible}}
58a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith  void g2(bool b) noexcept(b); // expected-error {{argument to noexcept specifier must be a constant expression}} expected-note {{read of non-const variable 'b'}} expected-note {{here}}
5960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
6060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl}
613617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregor
623617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregornamespace noexcept_unevaluated {
635c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor  template<typename T> bool f(T) {
643617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregor    T* x = 1;
653617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregor  }
663617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregor
673617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregor  template<typename T>
68e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  void g(T x) noexcept((sizeof(T) == sizeof(int)) || noexcept(f(x))) { }
693617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregor
703617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregor  void h() {
713617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregor    g(1);
723617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregor  }
733617e198aa89d4aa0921343a22b96823a63f8a58Douglas Gregor}
745c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor
755c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregornamespace PR11084 {
765c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor  template<int X> struct A {
77282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    static int f() noexcept(1/X) { return 10; }  // expected-error{{argument to noexcept specifier must be a constant expression}} expected-note{{division by zero}}
785c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor  };
795c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor
80176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  template<int X> void f() {
81176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    int (*p)() noexcept(1/X); // expected-error{{argument to noexcept specifier must be a constant expression}} expected-note{{division by zero}}
82176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  };
83176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
84176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void g() {
85176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    A<0>::f(); // expected-note{{in instantiation of exception specification for 'f'}}
86176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    f<0>(); // expected-note{{in instantiation of function template specialization}}
87176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
885c340e803ed2e384ff47f3e560df253515c92e1eDouglas Gregor}
89