diff options
author | Aaron Ballman <aaron@aaronballman.com> | 2017-08-11 16:31:51 +0000 |
---|---|---|
committer | Aaron Ballman <aaron@aaronballman.com> | 2017-08-11 16:31:51 +0000 |
commit | a3274e5443af14dcc1bf23b22f3f03c0561a74bb (patch) | |
tree | 6b9bcdaead4ff1e9f76b9b93847d01740730ca9d /clang-tools-extra/test/clang-tidy/hicpp-exception-baseclass.cpp | |
parent | 561092f233ed3239f497bab58969a7e9fb2cfae0 (diff) | |
download | bcm5719-llvm-a3274e5443af14dcc1bf23b22f3f03c0561a74bb.tar.gz bcm5719-llvm-a3274e5443af14dcc1bf23b22f3f03c0561a74bb.zip |
Add hicpp-exception-baseclass to the HIC++ module.
This enforces that throwing an exception in C++ requires that exception to inherit from std::exception.
Patch by Jonas Toth.
llvm-svn: 310727
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/hicpp-exception-baseclass.cpp')
-rw-r--r-- | clang-tools-extra/test/clang-tidy/hicpp-exception-baseclass.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/hicpp-exception-baseclass.cpp b/clang-tools-extra/test/clang-tidy/hicpp-exception-baseclass.cpp new file mode 100644 index 00000000000..ac9d9930dcf --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/hicpp-exception-baseclass.cpp @@ -0,0 +1,42 @@ +// RUN: %check_clang_tidy %s hicpp-exception-baseclass %t + +namespace std { +class exception {}; +} // namespace std + +class derived_exception : public std::exception {}; +class non_derived_exception {}; + +void problematic() { + try { + throw int(42); // Built in is not allowed +// CHECK-MESSAGES: [[@LINE-1]]:5: warning: throwing an exception whose type is not derived from 'std::exception' + } catch (int e) { + } + throw int(42); // Bad +// CHECK-MESSAGES: [[@LINE-1]]:3: warning: throwing an exception whose type is not derived from 'std::exception' + + try { + throw non_derived_exception(); // Some class is not allowed +// CHECK-MESSAGES: [[@LINE-1]]:5: warning: throwing an exception whose type is not derived from 'std::exception' +// CHECK-MESSAGES: 8:1: note: type defined here + } catch (non_derived_exception &e) { + } + throw non_derived_exception(); // Bad +// CHECK-MESSAGES: [[@LINE-1]]:3: warning: throwing an exception whose type is not derived from 'std::exception' +// CHECK-MESSAGES: 8:1: note: type defined here +} + +void allowed_throws() { + try { + throw std::exception(); // Ok + } catch (std::exception &e) { // Ok + } + throw std::exception(); + + try { + throw derived_exception(); // Ok + } catch (derived_exception &e) { // Ok + } + throw derived_exception(); // Ok +} |