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
5class A {};
6class B {};
7
8void 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
12template <typename T>
13void foo() throw();
14void 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
18void 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
22void 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
26void 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
31void 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
35void 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
39void 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
45void 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
50void 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
54class 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
60struct Z {
61 void operator delete(void *ptr) throw();
62 void operator delete[](void *ptr) throw(int);
63 ~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
72struct S {
73 void f() throw();
74};
75void 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
81template <typename T>
82struct ST {
83 void foo() throw();
84};
85template <typename T>
86void 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
92typedef 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.
97void titi() noexcept {}
98void toto() noexcept(true) {}
99
100// Should not trigger a replacement.
101void bad()
102#if !__has_feature(cxx_noexcept)
103 throw()
104#endif
105 ;
106

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/use-noexcept.cpp