1 // RUN: %check_clang_tidy -std=c++11,c++14 %s modernize-use-noexcept %t -- -- -fexceptions
2 // This test is not run in C++17 or later because dynamic exception
3 // specifications were removed in C++17.
4 
5 class A {};
6 class B {};
7 
8 void foo() throw();
9 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
10 // CHECK-FIXES: void foo() noexcept;
11 
12 template <typename T>
13 void foo() throw();
footest()14 void footest() { foo<int>(); foo<double>(); }
15 // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
16 // CHECK-FIXES: void foo() noexcept;
17 
18 void bar() throw(...);
19 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: dynamic exception specification 'throw(...)' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
20 // CHECK-FIXES: void bar() noexcept(false);
21 
22 void k() throw(int(int));
23 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: dynamic exception specification 'throw(int(int))' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
24 // CHECK-FIXES: void k() noexcept(false);
25 
foobar()26 void foobar() throw(A, B)
27 {}
28 // CHECK-MESSAGES: :[[@LINE-2]]:15: warning: dynamic exception specification 'throw(A, B)' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
29 // CHECK-FIXES: void foobar() noexcept(false)
30 
baz(int=(throw A (),0))31 void baz(int = (throw A(), 0)) throw(A, B) {}
32 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: dynamic exception specification 'throw(A, B)' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
33 // CHECK-FIXES: void baz(int = (throw A(), 0)) noexcept(false) {}
34 
35 void g(void (*fp)(void) throw());
36 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
37 // CHECK-FIXES: void g(void (*fp)(void) noexcept);
38 
39 void f(void (*fp)(void) throw(int)) throw(char);
40 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: dynamic exception specification 'throw(int)' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
41 // CHECK-MESSAGES: :[[@LINE-2]]:37: warning: dynamic exception specification 'throw(char)' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
42 // CHECK-FIXES: void f(void (*fp)(void) noexcept(false)) noexcept(false);
43 
44 #define THROW throw
45 void h(void (*fp)(void) THROW(int)) THROW(char);
46 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: dynamic exception specification 'THROW(int)' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
47 // CHECK-MESSAGES: :[[@LINE-2]]:37: warning: dynamic exception specification 'THROW(char)' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
48 // CHECK-FIXES: void h(void (*fp)(void) noexcept(false)) noexcept(false);
49 
50 void j() throw(int(int) throw(void(void) throw(int)));
51 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: dynamic exception specification 'throw(int(int) throw(void(void) throw(int)))' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
52 // CHECK-FIXES: void j() noexcept(false);
53 
54 class Y {
55   Y() throw() = default;
56 };
57 // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
58 // CHECK-FIXES: Y() noexcept = default;
59 
60 struct Z {
61   void operator delete(void *ptr) throw();
62   void operator delete[](void *ptr) throw(int);
~ZZ63   ~Z() throw(int) {}
64 };
65 // CHECK-MESSAGES: :[[@LINE-4]]:35: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
66 // CHECK-MESSAGES: :[[@LINE-4]]:37: warning: dynamic exception specification 'throw(int)' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
67 // CHECK-MESSAGES: :[[@LINE-4]]:8: warning: dynamic exception specification 'throw(int)' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
68 // CHECK-FIXES: void operator delete(void *ptr) noexcept;
69 // CHECK-FIXES: void operator delete[](void *ptr) noexcept(false);
70 // CHECK-FIXES: ~Z() noexcept(false) {}
71 
72 struct S {
73   void f() throw();
74 };
75 void f(void (S::*)() throw());
76 // CHECK-MESSAGES: :[[@LINE-3]]:12: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
77 // CHECK-MESSAGES: :[[@LINE-2]]:22: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
78 // CHECK-FIXES: void f() noexcept;
79 // CHECK-FIXES: void f(void (S::*)() noexcept);
80 
81 template <typename T>
82 struct ST {
83   void foo() throw();
84 };
85 template <typename T>
86 void ft(void (ST<T>::*)() throw());
87 // CHECK-MESSAGES: :[[@LINE-4]]:14: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
88 // CHECK-MESSAGES: :[[@LINE-2]]:27: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
89 // CHECK-FIXES: void foo() noexcept;
90 // CHECK-FIXES: void ft(void (ST<T>::*)() noexcept);
91 
92 typedef void (*fp)(void (*fp2)(int) throw());
93 // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
94 // CHECK-FIXES: typedef void (*fp)(void (*fp2)(int) noexcept);
95 
96 // Should not trigger a replacement.
titi()97 void titi() noexcept {}
toto()98 void toto() noexcept(true) {}
99 
100 // Should not trigger a replacement.
101 void bad()
102 #if !__has_feature(cxx_noexcept)
103     throw()
104 #endif
105   ;
106