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
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 0000000..e528ae9
--- /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