blob: 1555201f0fa1f036bb73fd579c7264eb29dfe89c [file] [log] [blame]
Aaron Ballmana3274e52017-08-11 16:31:51 +00001//===--- ExceptionBaseclassCheck.cpp - clang-tidy--------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Aaron Ballmana3274e52017-08-11 16:31:51 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "ExceptionBaseclassCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
Aaron Ballmana3274e52017-08-11 16:31:51 +000013using namespace clang::ast_matchers;
14
15namespace clang {
16namespace tidy {
17namespace hicpp {
18
19void ExceptionBaseclassCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballmana3274e52017-08-11 16:31:51 +000020 Finder->addMatcher(
Jonas Tothb1efe512018-09-17 13:55:10 +000021 cxxThrowExpr(
Alexander Kornienko976e0c02018-11-25 02:41:01 +000022 unless(has(expr(anyOf(isTypeDependent(), isValueDependent())))),
23 // The thrown value is not derived from 'std::exception'.
24 has(expr(unless(
25 hasType(qualType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
26 isSameOrDerivedFrom(hasName("::std::exception")))))))))),
27 // This condition is always true, but will bind to the
28 // template value if the thrown type is templated.
29 anyOf(has(expr(
30 hasType(substTemplateTypeParmType().bind("templ_type")))),
31 anything()),
32 // Bind to the declaration of the type of the value that
Kazuaki Ishizakidd5571d2020-04-05 15:28:11 +090033 // is thrown. 'anything()' is necessary to always succeed
Alexander Kornienko976e0c02018-11-25 02:41:01 +000034 // in the 'eachOf' because builtin types are not
35 // 'namedDecl'.
36 eachOf(has(expr(hasType(namedDecl().bind("decl")))), anything()))
Aaron Ballmana3274e52017-08-11 16:31:51 +000037 .bind("bad_throw"),
38 this);
39}
40
41void ExceptionBaseclassCheck::check(const MatchFinder::MatchResult &Result) {
42 const auto *BadThrow = Result.Nodes.getNodeAs<CXXThrowExpr>("bad_throw");
Jonas Tothb1efe512018-09-17 13:55:10 +000043 assert(BadThrow && "Did not match the throw expression");
Jonas Toth673dbd12017-08-30 15:59:01 +000044
Stephen Kelly43465bf2018-08-09 22:42:26 +000045 diag(BadThrow->getSubExpr()->getBeginLoc(), "throwing an exception whose "
Jonas Toth673dbd12017-08-30 15:59:01 +000046 "type %0 is not derived from "
47 "'std::exception'")
48 << BadThrow->getSubExpr()->getType() << BadThrow->getSourceRange();
Aaron Ballmana3274e52017-08-11 16:31:51 +000049
Jonas Tothb1efe512018-09-17 13:55:10 +000050 if (const auto *Template =
51 Result.Nodes.getNodeAs<SubstTemplateTypeParmType>("templ_type"))
52 diag(BadThrow->getSubExpr()->getBeginLoc(),
53 "type %0 is a template instantiation of %1", DiagnosticIDs::Note)
54 << BadThrow->getSubExpr()->getType()
55 << Template->getReplacedParameter()->getDecl();
56
57 if (const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl"))
Stephen Kelly43465bf2018-08-09 22:42:26 +000058 diag(TypeDecl->getBeginLoc(), "type defined here", DiagnosticIDs::Note);
Aaron Ballmana3274e52017-08-11 16:31:51 +000059}
60
61} // namespace hicpp
62} // namespace tidy
63} // namespace clang