1a3274e54SAaron Ballman //===--- ExceptionBaseclassCheck.cpp - clang-tidy--------------------------===//
2a3274e54SAaron Ballman //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a3274e54SAaron Ballman //
7a3274e54SAaron Ballman //===----------------------------------------------------------------------===//
8a3274e54SAaron Ballman
9a3274e54SAaron Ballman #include "ExceptionBaseclassCheck.h"
10a3274e54SAaron Ballman #include "clang/AST/ASTContext.h"
11a3274e54SAaron Ballman #include "clang/ASTMatchers/ASTMatchFinder.h"
12a3274e54SAaron Ballman
13a3274e54SAaron Ballman using namespace clang::ast_matchers;
14a3274e54SAaron Ballman
15*7d2ea6c4SCarlos Galvez namespace clang::tidy::hicpp {
16a3274e54SAaron Ballman
registerMatchers(MatchFinder * Finder)17a3274e54SAaron Ballman void ExceptionBaseclassCheck::registerMatchers(MatchFinder *Finder) {
18a3274e54SAaron Ballman Finder->addMatcher(
19b1efe51dSJonas Toth cxxThrowExpr(
20b1efe51dSJonas Toth unless(has(expr(anyOf(isTypeDependent(), isValueDependent())))),
21b1efe51dSJonas Toth // The thrown value is not derived from 'std::exception'.
22976e0c07SAlexander Kornienko has(expr(unless(
23976e0c07SAlexander Kornienko hasType(qualType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
24b1efe51dSJonas Toth isSameOrDerivedFrom(hasName("::std::exception")))))))))),
25b1efe51dSJonas Toth // This condition is always true, but will bind to the
26b1efe51dSJonas Toth // template value if the thrown type is templated.
27976e0c07SAlexander Kornienko anyOf(has(expr(
28976e0c07SAlexander Kornienko hasType(substTemplateTypeParmType().bind("templ_type")))),
29b1efe51dSJonas Toth anything()),
30b1efe51dSJonas Toth // Bind to the declaration of the type of the value that
31dd5571d5SKazuaki Ishizaki // is thrown. 'anything()' is necessary to always succeed
32b1efe51dSJonas Toth // in the 'eachOf' because builtin types are not
33b1efe51dSJonas Toth // 'namedDecl'.
34976e0c07SAlexander Kornienko eachOf(has(expr(hasType(namedDecl().bind("decl")))), anything()))
35a3274e54SAaron Ballman .bind("bad_throw"),
36a3274e54SAaron Ballman this);
37a3274e54SAaron Ballman }
38a3274e54SAaron Ballman
check(const MatchFinder::MatchResult & Result)39a3274e54SAaron Ballman void ExceptionBaseclassCheck::check(const MatchFinder::MatchResult &Result) {
40a3274e54SAaron Ballman const auto *BadThrow = Result.Nodes.getNodeAs<CXXThrowExpr>("bad_throw");
41b1efe51dSJonas Toth assert(BadThrow && "Did not match the throw expression");
42673dbd1aSJonas Toth
4343465bf3SStephen Kelly diag(BadThrow->getSubExpr()->getBeginLoc(), "throwing an exception whose "
44673dbd1aSJonas Toth "type %0 is not derived from "
45673dbd1aSJonas Toth "'std::exception'")
46673dbd1aSJonas Toth << BadThrow->getSubExpr()->getType() << BadThrow->getSourceRange();
47a3274e54SAaron Ballman
48b1efe51dSJonas Toth if (const auto *Template =
49b1efe51dSJonas Toth Result.Nodes.getNodeAs<SubstTemplateTypeParmType>("templ_type"))
50b1efe51dSJonas Toth diag(BadThrow->getSubExpr()->getBeginLoc(),
51b1efe51dSJonas Toth "type %0 is a template instantiation of %1", DiagnosticIDs::Note)
52b1efe51dSJonas Toth << BadThrow->getSubExpr()->getType()
53bcd9ba2bSMatheus Izvekov << Template->getReplacedParameter();
54b1efe51dSJonas Toth
55b1efe51dSJonas Toth if (const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl"))
5643465bf3SStephen Kelly diag(TypeDecl->getBeginLoc(), "type defined here", DiagnosticIDs::Note);
57a3274e54SAaron Ballman }
58a3274e54SAaron Ballman
59*7d2ea6c4SCarlos Galvez } // namespace clang::tidy::hicpp
60