2017-08-12 00:31:51 +08:00
|
|
|
//===--- 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(
|
[clang-tidy] Improve hicpp-exception-baseclass to handle generic code better
Summary:
This patch is a followup to the first revision D36583, that had problems with
generic code and its diagnostic messages, which were found by @lebedev.ri
Reviewers: alexfh, aaron.ballman, lebedev.ri, hokein
Reviewed By: aaron.ballman, lebedev.ri
Subscribers: klimek, sbenza, cfe-commits, JDevlieghere, lebedev.ri, xazax.hun
Differential Revision: https://reviews.llvm.org/D37060
llvm-svn: 312134
2017-08-30 23:59:01 +08:00
|
|
|
cxxThrowExpr(allOf(has(expr(unless(hasType(qualType(hasCanonicalType(
|
|
|
|
hasDeclaration(cxxRecordDecl(isSameOrDerivedFrom(
|
|
|
|
hasName("std::exception")))))))))),
|
|
|
|
has(expr(unless(cxxUnresolvedConstructExpr()))),
|
|
|
|
eachOf(has(expr(hasType(namedDecl().bind("decl")))),
|
|
|
|
anything())))
|
2017-08-12 00:31:51 +08:00
|
|
|
.bind("bad_throw"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExceptionBaseclassCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *BadThrow = Result.Nodes.getNodeAs<CXXThrowExpr>("bad_throw");
|
[clang-tidy] Improve hicpp-exception-baseclass to handle generic code better
Summary:
This patch is a followup to the first revision D36583, that had problems with
generic code and its diagnostic messages, which were found by @lebedev.ri
Reviewers: alexfh, aaron.ballman, lebedev.ri, hokein
Reviewed By: aaron.ballman, lebedev.ri
Subscribers: klimek, sbenza, cfe-commits, JDevlieghere, lebedev.ri, xazax.hun
Differential Revision: https://reviews.llvm.org/D37060
llvm-svn: 312134
2017-08-30 23:59:01 +08:00
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(BadThrow->getSubExpr()->getBeginLoc(), "throwing an exception whose "
|
[clang-tidy] Improve hicpp-exception-baseclass to handle generic code better
Summary:
This patch is a followup to the first revision D36583, that had problems with
generic code and its diagnostic messages, which were found by @lebedev.ri
Reviewers: alexfh, aaron.ballman, lebedev.ri, hokein
Reviewed By: aaron.ballman, lebedev.ri
Subscribers: klimek, sbenza, cfe-commits, JDevlieghere, lebedev.ri, xazax.hun
Differential Revision: https://reviews.llvm.org/D37060
llvm-svn: 312134
2017-08-30 23:59:01 +08:00
|
|
|
"type %0 is not derived from "
|
|
|
|
"'std::exception'")
|
|
|
|
<< BadThrow->getSubExpr()->getType() << BadThrow->getSourceRange();
|
2017-08-12 00:31:51 +08:00
|
|
|
|
|
|
|
const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl");
|
|
|
|
if (TypeDecl != nullptr)
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(TypeDecl->getBeginLoc(), "type defined here", DiagnosticIDs::Note);
|
2017-08-12 00:31:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace hicpp
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|