Index: clang-tidy/hicpp/CMakeLists.txt =================================================================== --- clang-tidy/hicpp/CMakeLists.txt +++ clang-tidy/hicpp/CMakeLists.txt @@ -1,6 +1,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyHICPPModule + ExceptionBaseclassCheck.cpp NoAssemblerCheck.cpp HICPPTidyModule.cpp Index: clang-tidy/hicpp/ExceptionBaseclassCheck.h =================================================================== --- /dev/null +++ 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 Index: clang-tidy/hicpp/ExceptionBaseclassCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/hicpp/ExceptionBaseclassCheck.cpp @@ -0,0 +1,41 @@ +//===--- 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" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace hicpp { + +void ExceptionBaseclassCheck::registerMatchers(MatchFinder *Finder) { + if (!getLangOpts().CPlusPlus) + return; + + Finder->addMatcher( + cxxThrowExpr(has(expr(unless(hasType(cxxRecordDecl( + isSameOrDerivedFrom(hasName("std::exception")))))))) + .bind("bad_throw"), + this); +} + +void ExceptionBaseclassCheck::check(const MatchFinder::MatchResult &Result) { + const auto *BadThrow = Result.Nodes.getNodeAs("bad_throw"); + + diag(BadThrow->getLocStart(), + "throwing value which type is not derived from std::exception") + << SourceRange(BadThrow->getLocStart(), BadThrow->getLocEnd()); +} + +} // namespace hicpp +} // namespace tidy +} // namespace clang Index: clang-tidy/hicpp/HICPPTidyModule.cpp =================================================================== --- clang-tidy/hicpp/HICPPTidyModule.cpp +++ clang-tidy/hicpp/HICPPTidyModule.cpp @@ -23,6 +23,7 @@ #include "../modernize/UseOverrideCheck.h" #include "../readability/FunctionSizeCheck.h" #include "../readability/IdentifierNamingCheck.h" +#include "ExceptionBaseclassCheck.h" #include "NoAssemblerCheck.h" namespace clang { @@ -32,6 +33,8 @@ class HICPPModule : public ClangTidyModule { public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck( + "hicpp-exception-baseclass"); CheckFactories.registerCheck( "hicpp-explicit-conversions"); CheckFactories.registerCheck( Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -57,6 +57,12 @@ Improvements to clang-tidy -------------------------- +- New `hicpp-exception-baseclass + `_ check + + This check ensures that all exception will be instances of ``std::exception`` and classes + that are derived from it. + - Renamed checks to use correct term "implicit conversion" instead of "implicit cast" and modified messages and option names accordingly: Index: docs/clang-tidy/checks/hicpp-exception-baseclass.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/hicpp-exception-baseclass.rst @@ -0,0 +1,29 @@ +.. 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 `_ +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(); + } + Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -30,7 +30,6 @@ cert-msc30-c (redirects to cert-msc50-cpp) cert-msc50-cpp cert-oop11-cpp (redirects to misc-move-constructor-init) - cppcoreguidelines-c-copy-assignment-signature cppcoreguidelines-interfaces-global-init cppcoreguidelines-no-malloc cppcoreguidelines-pro-bounds-array-to-pointer-decay @@ -61,6 +60,7 @@ google-runtime-member-string-references google-runtime-operator google-runtime-references + hicpp-exception-baseclass hicpp-explicit-conversions (redirects to google-explicit-constructor) hicpp-function-size (redirects to readability-function-size) hicpp-invalid-access-moved (redirects to misc-use-after-move) Index: test/clang-tidy/hicpp-exception-baseclass.cpp =================================================================== --- /dev/null +++ test/clang-tidy/hicpp-exception-baseclass.cpp @@ -0,0 +1,40 @@ +// 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 value which type is not derived from std::exception + } catch (int e) { + } + throw int(42); // Bad +// CHECK-MESSAGES: [[@LINE-1]]:3: warning: throwing value which type is not derived from std::exception + + try { + throw non_derived_exception(); // Some class is not allowed +// CHECK-MESSAGES: [[@LINE-1]]:5: warning: throwing value which type is not derived from std::exception + } catch (non_derived_exception &e) { + } + throw non_derived_exception(); // Bad +// CHECK-MESSAGES: [[@LINE-1]]:3: warning: throwing value which type is not derived from std::exception +} + +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 +}