1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
//===--- ExceptionBaseclassCheck.cpp - clang-tidy--------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ExceptionBaseclassCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang::tidy::hicpp {
void ExceptionBaseclassCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
cxxThrowExpr(
unless(has(expr(anyOf(isTypeDependent(), isValueDependent())))),
// The thrown value is not derived from 'std::exception'.
has(expr(unless(
hasType(qualType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
isSameOrDerivedFrom(hasName("::std::exception")))))))))),
// This condition is always true, but will bind to the
// template value if the thrown type is templated.
anyOf(has(expr(
hasType(substTemplateTypeParmType().bind("templ_type")))),
anything()),
// Bind to the declaration of the type of the value that
// is thrown. 'anything()' is necessary to always succeed
// in the 'eachOf' because builtin types are not
// 'namedDecl'.
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");
assert(BadThrow && "Did not match the throw expression");
diag(BadThrow->getSubExpr()->getBeginLoc(), "throwing an exception whose "
"type %0 is not derived from "
"'std::exception'")
<< BadThrow->getSubExpr()->getType() << BadThrow->getSourceRange();
if (const auto *Template =
Result.Nodes.getNodeAs<SubstTemplateTypeParmType>("templ_type"))
diag(BadThrow->getSubExpr()->getBeginLoc(),
"type %0 is a template instantiation of %1", DiagnosticIDs::Note)
<< BadThrow->getSubExpr()->getType()
<< Template->getReplacedParameter();
if (const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl"))
diag(TypeDecl->getBeginLoc(), "type defined here", DiagnosticIDs::Note);
}
} // namespace clang::tidy::hicpp
|