summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp
diff options
context:
space:
mode:
authorAaron Ballman <aaron@aaronballman.com>2017-08-11 16:31:51 +0000
committerAaron Ballman <aaron@aaronballman.com>2017-08-11 16:31:51 +0000
commita3274e5443af14dcc1bf23b22f3f03c0561a74bb (patch)
tree6b9bcdaead4ff1e9f76b9b93847d01740730ca9d /clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp
parent561092f233ed3239f497bab58969a7e9fb2cfae0 (diff)
downloadbcm5719-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/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp49
1 files changed, 49 insertions, 0 deletions
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
OpenPOWER on IntegriCloud