blob: a58842869204d07278ac6381ac7dd8006a8aa7fd [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) {
20 if (!getLangOpts().CPlusPlus)
21 return;
22
23 Finder->addMatcher(
Jonas Tothb1efe512018-09-17 13:55:10 +000024 cxxThrowExpr(
Alexander Kornienko976e0c02018-11-25 02:41:01 +000025 unless(has(expr(anyOf(isTypeDependent(), isValueDependent())))),
26 // The thrown value is not derived from 'std::exception'.
27 has(expr(unless(
28 hasType(qualType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
29 isSameOrDerivedFrom(hasName("::std::exception")))))))))),
30 // This condition is always true, but will bind to the
31 // template value if the thrown type is templated.
32 anyOf(has(expr(
33 hasType(substTemplateTypeParmType().bind("templ_type")))),
34 anything()),
35 // Bind to the declaration of the type of the value that
36 // is thrown. 'anything()' is necessary to always suceed
37 // in the 'eachOf' because builtin types are not
38 // 'namedDecl'.
39 eachOf(has(expr(hasType(namedDecl().bind("decl")))), anything()))
Aaron Ballmana3274e52017-08-11 16:31:51 +000040 .bind("bad_throw"),
41 this);
42}
43
44void ExceptionBaseclassCheck::check(const MatchFinder::MatchResult &Result) {
45 const auto *BadThrow = Result.Nodes.getNodeAs<CXXThrowExpr>("bad_throw");
Jonas Tothb1efe512018-09-17 13:55:10 +000046 assert(BadThrow && "Did not match the throw expression");
Jonas Toth673dbd12017-08-30 15:59:01 +000047
Stephen Kelly43465bf2018-08-09 22:42:26 +000048 diag(BadThrow->getSubExpr()->getBeginLoc(), "throwing an exception whose "
Jonas Toth673dbd12017-08-30 15:59:01 +000049 "type %0 is not derived from "
50 "'std::exception'")
51 << BadThrow->getSubExpr()->getType() << BadThrow->getSourceRange();
Aaron Ballmana3274e52017-08-11 16:31:51 +000052
Jonas Tothb1efe512018-09-17 13:55:10 +000053 if (const auto *Template =
54 Result.Nodes.getNodeAs<SubstTemplateTypeParmType>("templ_type"))
55 diag(BadThrow->getSubExpr()->getBeginLoc(),
56 "type %0 is a template instantiation of %1", DiagnosticIDs::Note)
57 << BadThrow->getSubExpr()->getType()
58 << Template->getReplacedParameter()->getDecl();
59
60 if (const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl"))
Stephen Kelly43465bf2018-08-09 22:42:26 +000061 diag(TypeDecl->getBeginLoc(), "type defined here", DiagnosticIDs::Note);
Aaron Ballmana3274e52017-08-11 16:31:51 +000062}
63
64} // namespace hicpp
65} // namespace tidy
66} // namespace clang