xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/modernize/use-noexcept-opt.cpp (revision e8a3ddafe063c970df9c23e803812369abde4c82)
189a1d03eSRichard // RUN: %check_clang_tidy -std=c++11,c++14 %s modernize-use-noexcept %t -- \
2*e8a3ddafSNathan James // RUN:   -config="{CheckOptions: {modernize-use-noexcept.UseNoexceptFalse: false}}" \
389a1d03eSRichard // RUN:   -- -fexceptions
489a1d03eSRichard // This test is not run in C++17 or later because dynamic exception
589a1d03eSRichard // specifications were removed in C++17.
689a1d03eSRichard 
789a1d03eSRichard using size_t = __SIZE_TYPE__;
889a1d03eSRichard class A {};
989a1d03eSRichard class B {};
1089a1d03eSRichard 
1189a1d03eSRichard void foo() throw();
1289a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
1389a1d03eSRichard // CHECK-FIXES: void foo() noexcept;
1489a1d03eSRichard 
1589a1d03eSRichard void bar() throw(...);
1689a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: dynamic exception specification 'throw(...)' is deprecated; consider removing it instead [modernize-use-noexcept]
1789a1d03eSRichard // CHECK-FIXES: void bar() ;
1889a1d03eSRichard 
1989a1d03eSRichard void k() throw(int(int));
2089a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: dynamic exception specification 'throw(int(int))' is deprecated; consider removing it instead [modernize-use-noexcept]
2189a1d03eSRichard // CHECK-FIXES: void k() ;
2289a1d03eSRichard 
2389a1d03eSRichard // Shouldn't crash due to llvm_unreachable in canThrow() on EST_Uninstantiated
2489a1d03eSRichard template <int> class c { void *operator new(size_t) throw (int);};
s()2589a1d03eSRichard void s() { c<1> doesnt_crash; }
2689a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-2]]:53: warning: dynamic exception specification 'throw (int)' is deprecated; consider removing it instead [modernize-use-noexcept]
2789a1d03eSRichard 
foobar()2889a1d03eSRichard void foobar() throw(A, B)
2989a1d03eSRichard {}
3089a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-2]]:15: warning: dynamic exception specification 'throw(A, B)' is deprecated; consider removing it instead [modernize-use-noexcept]
3189a1d03eSRichard // CHECK-FIXES: void foobar()
3289a1d03eSRichard 
baz(int=(throw A (),0))3389a1d03eSRichard void baz(int = (throw A(), 0)) throw(A, B) {}
3489a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: dynamic exception specification 'throw(A, B)' is deprecated; consider removing it instead [modernize-use-noexcept]
3589a1d03eSRichard // CHECK-FIXES: void baz(int = (throw A(), 0)) {}
3689a1d03eSRichard 
3789a1d03eSRichard void g(void (*fp)(void) throw());
3889a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
3989a1d03eSRichard // CHECK-FIXES: void g(void (*fp)(void) noexcept);
4089a1d03eSRichard 
4189a1d03eSRichard void f(void (*fp)(void) throw(int)) throw(char);
4289a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: dynamic exception specification 'throw(int)' is deprecated; consider removing it instead [modernize-use-noexcept]
4389a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-2]]:37: warning: dynamic exception specification 'throw(char)' is deprecated; consider removing it instead [modernize-use-noexcept]
4489a1d03eSRichard // CHECK-FIXES: void f(void (*fp)(void) ) ;
4589a1d03eSRichard 
4689a1d03eSRichard #define THROW throw
4789a1d03eSRichard void h(void (*fp)(void) THROW(int)) THROW(char);
4889a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: dynamic exception specification 'THROW(int)' is deprecated; consider removing it instead [modernize-use-noexcept]
4989a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-2]]:37: warning: dynamic exception specification 'THROW(char)' is deprecated; consider removing it instead [modernize-use-noexcept]
5089a1d03eSRichard // CHECK-FIXES: void h(void (*fp)(void) ) ;
5189a1d03eSRichard 
5289a1d03eSRichard void j() throw(int(int) throw(void(void) throw(int)));
5389a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: dynamic exception specification 'throw(int(int) throw(void(void) throw(int)))' is deprecated; consider removing it instead [modernize-use-noexcept]
5489a1d03eSRichard // CHECK-FIXES: void j() ;
5589a1d03eSRichard 
5689a1d03eSRichard class Y {
5789a1d03eSRichard   Y() throw() = default;
5889a1d03eSRichard };
5989a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
6089a1d03eSRichard // CHECK-FIXES: Y() noexcept = default;
6189a1d03eSRichard 
6289a1d03eSRichard struct Z {
6389a1d03eSRichard   void operator delete(void *ptr) throw();
6489a1d03eSRichard   void operator delete[](void *ptr) throw(int);
~ZZ6589a1d03eSRichard   ~Z() throw(int) {}
6689a1d03eSRichard };
6789a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-4]]:35: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
6889a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-4]]:37: warning: dynamic exception specification 'throw(int)' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
6989a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-4]]:8: warning: dynamic exception specification 'throw(int)' is deprecated; consider using 'noexcept(false)' instead [modernize-use-noexcept]
7089a1d03eSRichard // CHECK-FIXES: void operator delete(void *ptr) noexcept;
7189a1d03eSRichard // CHECK-FIXES: void operator delete[](void *ptr) noexcept(false);
7289a1d03eSRichard // CHECK-FIXES: ~Z() noexcept(false) {}
7389a1d03eSRichard 
7489a1d03eSRichard struct S {
7589a1d03eSRichard   void f() throw();
7689a1d03eSRichard };
7789a1d03eSRichard void f(void (S::*)() throw());
7889a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-3]]:12: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
7989a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-2]]:22: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
8089a1d03eSRichard // CHECK-FIXES: void f() noexcept;
8189a1d03eSRichard // CHECK-FIXES: void f(void (S::*)() noexcept);
8289a1d03eSRichard 
8389a1d03eSRichard typedef void (*fp)(void (*fp2)(int) throw());
8489a1d03eSRichard // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: dynamic exception specification 'throw()' is deprecated; consider using 'noexcept' instead [modernize-use-noexcept]
8589a1d03eSRichard // CHECK-FIXES: typedef void (*fp)(void (*fp2)(int) noexcept);
8689a1d03eSRichard 
8789a1d03eSRichard // Should not trigger a replacement.
titi()8889a1d03eSRichard void titi() noexcept {}
toto()8989a1d03eSRichard void toto() noexcept(true) {}
9089a1d03eSRichard 
9189a1d03eSRichard // Should not trigger a replacement.
9289a1d03eSRichard void bad()
9389a1d03eSRichard #if !__has_feature(cxx_noexcept)
9489a1d03eSRichard     throw()
9589a1d03eSRichard #endif
9689a1d03eSRichard   ;
97