[clang-tools-extra] r310727 - Add hicpp-exception-baseclass to the HIC++ module.

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 11 09:31:51 PDT 2017


Author: aaronballman
Date: Fri Aug 11 09:31:51 2017
New Revision: 310727

URL: http://llvm.org/viewvc/llvm-project?rev=310727&view=rev
Log:
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.

Added:
    clang-tools-extra/trunk/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp
    clang-tools-extra/trunk/clang-tidy/hicpp/ExceptionBaseclassCheck.h
    clang-tools-extra/trunk/docs/clang-tidy/checks/hicpp-exception-baseclass.rst
    clang-tools-extra/trunk/test/clang-tidy/hicpp-exception-baseclass.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/hicpp/CMakeLists.txt
    clang-tools-extra/trunk/clang-tidy/hicpp/HICPPTidyModule.cpp
    clang-tools-extra/trunk/docs/ReleaseNotes.rst
    clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst

Modified: clang-tools-extra/trunk/clang-tidy/hicpp/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/hicpp/CMakeLists.txt?rev=310727&r1=310726&r2=310727&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/hicpp/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/hicpp/CMakeLists.txt Fri Aug 11 09:31:51 2017
@@ -1,6 +1,7 @@
 set(LLVM_LINK_COMPONENTS support)
 
 add_clang_library(clangTidyHICPPModule
+  ExceptionBaseclassCheck.cpp
   NoAssemblerCheck.cpp
   HICPPTidyModule.cpp
 

Added: clang-tools-extra/trunk/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp?rev=310727&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp Fri Aug 11 09:31:51 2017
@@ -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

Added: clang-tools-extra/trunk/clang-tidy/hicpp/ExceptionBaseclassCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/hicpp/ExceptionBaseclassCheck.h?rev=310727&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/hicpp/ExceptionBaseclassCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/hicpp/ExceptionBaseclassCheck.h Fri Aug 11 09:31:51 2017
@@ -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

Modified: clang-tools-extra/trunk/clang-tidy/hicpp/HICPPTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/hicpp/HICPPTidyModule.cpp?rev=310727&r1=310726&r2=310727&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/hicpp/HICPPTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/hicpp/HICPPTidyModule.cpp Fri Aug 11 09:31:51 2017
@@ -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>(

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=310727&r1=310726&r2=310727&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Fri Aug 11 09:31:51 2017
@@ -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
 

Added: clang-tools-extra/trunk/docs/clang-tidy/checks/hicpp-exception-baseclass.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/hicpp-exception-baseclass.rst?rev=310727&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/hicpp-exception-baseclass.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/hicpp-exception-baseclass.rst Fri Aug 11 09:31:51 2017
@@ -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();
+  }
+  

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst?rev=310727&r1=310726&r2=310727&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Fri Aug 11 09:31:51 2017
@@ -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>

Added: clang-tools-extra/trunk/test/clang-tidy/hicpp-exception-baseclass.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/hicpp-exception-baseclass.cpp?rev=310727&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/hicpp-exception-baseclass.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/hicpp-exception-baseclass.cpp Fri Aug 11 09:31:51 2017
@@ -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
+}




More information about the cfe-commits mailing list