summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang-tools-extra/clang-tidy/hicpp/CMakeLists.txt1
-rw-r--r--clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp49
-rw-r--r--clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.h35
-rw-r--r--clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp3
-rw-r--r--clang-tools-extra/docs/ReleaseNotes.rst6
-rw-r--r--clang-tools-extra/docs/clang-tidy/checks/hicpp-exception-baseclass.rst30
-rw-r--r--clang-tools-extra/docs/clang-tidy/checks/list.rst1
-rw-r--r--clang-tools-extra/test/clang-tidy/hicpp-exception-baseclass.cpp42
8 files changed, 167 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/hicpp/CMakeLists.txt b/clang-tools-extra/clang-tidy/hicpp/CMakeLists.txt
index d3fff95e318..e5e709552a1 100644
--- a/clang-tools-extra/clang-tidy/hicpp/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/hicpp/CMakeLists.txt
@@ -1,6 +1,7 @@
set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyHICPPModule
+ ExceptionBaseclassCheck.cpp
NoAssemblerCheck.cpp
HICPPTidyModule.cpp
diff --git a/clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp b/clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp
new file mode 100644
index 00000000000..e528ae92df8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp
@@ -0,0 +1,49 @@
+//===--- ExceptionBaseclassCheck.cpp - clang-tidy--------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "ExceptionBaseclassCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+#include <iostream>
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace hicpp {
+
+void ExceptionBaseclassCheck::registerMatchers(MatchFinder *Finder) {
+ if (!getLangOpts().CPlusPlus)
+ return;
+
+ Finder->addMatcher(
+ cxxThrowExpr(
+ allOf(
+ has(expr(unless(hasType(cxxRecordDecl(
+ isSameOrDerivedFrom(hasName("std::exception"))))))),
+ eachOf(has(expr(hasType(namedDecl().bind("decl")))), anything())))
+ .bind("bad_throw"),
+ this);
+}
+
+void ExceptionBaseclassCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *BadThrow = Result.Nodes.getNodeAs<CXXThrowExpr>("bad_throw");
+ diag(BadThrow->getLocStart(),
+ "throwing an exception whose type is not derived from 'std::exception'")
+ << BadThrow->getSourceRange();
+
+ const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl");
+ if (TypeDecl != nullptr)
+ diag(TypeDecl->getLocStart(), "type defined here", DiagnosticIDs::Note);
+}
+
+} // namespace hicpp
+} // namespace tidy
+} // namespace clang
diff --git a/clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.h b/clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.h
new file mode 100644
index 00000000000..778979ddee6
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.h
@@ -0,0 +1,35 @@
+//===--- ExceptionBaseclassCheck.h - clang-tidy------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_EXCEPTION_BASECLASS_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_EXCEPTION_BASECLASS_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace hicpp {
+
+/// Check for thrown exceptions and enforce they are all derived from std::exception.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/hicpp-exception-baseclass.html
+class ExceptionBaseclassCheck : public ClangTidyCheck {
+public:
+ ExceptionBaseclassCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace hicpp
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_EXCEPTION_BASECLASS_H
diff --git a/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp b/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp
index a70f706ef39..321d8791a51 100644
--- a/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp
@@ -24,6 +24,7 @@
#include "../readability/BracesAroundStatementsCheck.h"
#include "../readability/FunctionSizeCheck.h"
#include "../readability/IdentifierNamingCheck.h"
+#include "ExceptionBaseclassCheck.h"
#include "NoAssemblerCheck.h"
namespace clang {
@@ -35,6 +36,8 @@ public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<readability::BracesAroundStatementsCheck>(
"hicpp-braces-around-statements");
+ CheckFactories.registerCheck<ExceptionBaseclassCheck>(
+ "hicpp-exception-baseclass");
CheckFactories.registerCheck<google::ExplicitConstructorCheck>(
"hicpp-explicit-conversions");
CheckFactories.registerCheck<readability::FunctionSizeCheck>(
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 9084d8bda99..870c2587ac5 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -82,6 +82,12 @@ Improvements to clang-tidy
Finds cases where integer division in a floating point context is likely to
cause unintended loss of precision.
+- New `hicpp-exception-baseclass
+ <http://clang.llvm.org/extra/clang-tidy/checks/hicpp-exception-baseclass.html>`_ check
+
+ Ensures that all exception will be instances of ``std::exception`` and classes
+ that are derived from it.
+
- New `readability-static-accessed-through-instance
<http://clang.llvm.org/extra/clang-tidy/checks/readability-static-accessed-through-instance.html>`_ check
diff --git a/clang-tools-extra/docs/clang-tidy/checks/hicpp-exception-baseclass.rst b/clang-tools-extra/docs/clang-tidy/checks/hicpp-exception-baseclass.rst
new file mode 100644
index 00000000000..42563aeaee3
--- /dev/null
+++ b/clang-tools-extra/docs/clang-tidy/checks/hicpp-exception-baseclass.rst
@@ -0,0 +1,30 @@
+.. title:: clang-tidy - hicpp-exception-baseclass
+
+hicpp-exception-baseclass
+=========================
+
+Ensure that every value that in a ``throw`` expression is an instance of
+``std::exception``.
+
+This enforces `rule 15.1 <http://www.codingstandard.com/section/15-1-throwing-an-exception/>`_
+of the High Integrity C++ Coding Standard.
+
+.. code-block:: c++
+
+ class custom_exception {};
+
+ void throwing() noexcept(false) {
+ // Problematic throw expressions.
+ throw int(42);
+ throw custom_exception();
+ }
+
+ class mathematical_error : public std::exception {};
+
+ void throwing2() noexcept(false) {
+ // These kind of throws are ok.
+ throw mathematical_error();
+ throw std::runtime_error();
+ throw std::exception();
+ }
+
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst
index 3f1c57aface..1d99a4ed90e 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -63,6 +63,7 @@ Clang-Tidy Checks
google-runtime-operator
google-runtime-references
hicpp-braces-around-statements (redirects to readability-braces-around-statements) <hicpp-braces-around-statements>
+ hicpp-exception-baseclass
hicpp-explicit-conversions (redirects to google-explicit-constructor) <hicpp-explicit-conversions>
hicpp-function-size (redirects to readability-function-size) <hicpp-function-size>
hicpp-invalid-access-moved (redirects to misc-use-after-move) <hicpp-invalid-access-moved>
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
+}
OpenPOWER on IntegriCloud