annotate clang-tools-extra/clang-tidy/hicpp/ExceptionBaseclassCheck.cpp @ 160:dbfec6499728

...
author anatofuz
date Wed, 18 Mar 2020 19:11:03 +0900
parents 1d019706d866
children 0572611fdcc8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- ExceptionBaseclassCheck.cpp - clang-tidy--------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "ExceptionBaseclassCheck.h"
anatofuz
parents:
diff changeset
10 #include "clang/AST/ASTContext.h"
anatofuz
parents:
diff changeset
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 using namespace clang::ast_matchers;
anatofuz
parents:
diff changeset
14
anatofuz
parents:
diff changeset
15 namespace clang {
anatofuz
parents:
diff changeset
16 namespace tidy {
anatofuz
parents:
diff changeset
17 namespace hicpp {
anatofuz
parents:
diff changeset
18
anatofuz
parents:
diff changeset
19 void ExceptionBaseclassCheck::registerMatchers(MatchFinder *Finder) {
anatofuz
parents:
diff changeset
20 if (!getLangOpts().CPlusPlus)
anatofuz
parents:
diff changeset
21 return;
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 Finder->addMatcher(
anatofuz
parents:
diff changeset
24 cxxThrowExpr(
anatofuz
parents:
diff changeset
25 unless(has(expr(anyOf(isTypeDependent(), isValueDependent())))),
anatofuz
parents:
diff changeset
26 // The thrown value is not derived from 'std::exception'.
anatofuz
parents:
diff changeset
27 has(expr(unless(
anatofuz
parents:
diff changeset
28 hasType(qualType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
anatofuz
parents:
diff changeset
29 isSameOrDerivedFrom(hasName("::std::exception")))))))))),
anatofuz
parents:
diff changeset
30 // This condition is always true, but will bind to the
anatofuz
parents:
diff changeset
31 // template value if the thrown type is templated.
anatofuz
parents:
diff changeset
32 anyOf(has(expr(
anatofuz
parents:
diff changeset
33 hasType(substTemplateTypeParmType().bind("templ_type")))),
anatofuz
parents:
diff changeset
34 anything()),
anatofuz
parents:
diff changeset
35 // Bind to the declaration of the type of the value that
anatofuz
parents:
diff changeset
36 // is thrown. 'anything()' is necessary to always suceed
anatofuz
parents:
diff changeset
37 // in the 'eachOf' because builtin types are not
anatofuz
parents:
diff changeset
38 // 'namedDecl'.
anatofuz
parents:
diff changeset
39 eachOf(has(expr(hasType(namedDecl().bind("decl")))), anything()))
anatofuz
parents:
diff changeset
40 .bind("bad_throw"),
anatofuz
parents:
diff changeset
41 this);
anatofuz
parents:
diff changeset
42 }
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 void ExceptionBaseclassCheck::check(const MatchFinder::MatchResult &Result) {
anatofuz
parents:
diff changeset
45 const auto *BadThrow = Result.Nodes.getNodeAs<CXXThrowExpr>("bad_throw");
anatofuz
parents:
diff changeset
46 assert(BadThrow && "Did not match the throw expression");
anatofuz
parents:
diff changeset
47
anatofuz
parents:
diff changeset
48 diag(BadThrow->getSubExpr()->getBeginLoc(), "throwing an exception whose "
anatofuz
parents:
diff changeset
49 "type %0 is not derived from "
anatofuz
parents:
diff changeset
50 "'std::exception'")
anatofuz
parents:
diff changeset
51 << BadThrow->getSubExpr()->getType() << BadThrow->getSourceRange();
anatofuz
parents:
diff changeset
52
anatofuz
parents:
diff changeset
53 if (const auto *Template =
anatofuz
parents:
diff changeset
54 Result.Nodes.getNodeAs<SubstTemplateTypeParmType>("templ_type"))
anatofuz
parents:
diff changeset
55 diag(BadThrow->getSubExpr()->getBeginLoc(),
anatofuz
parents:
diff changeset
56 "type %0 is a template instantiation of %1", DiagnosticIDs::Note)
anatofuz
parents:
diff changeset
57 << BadThrow->getSubExpr()->getType()
anatofuz
parents:
diff changeset
58 << Template->getReplacedParameter()->getDecl();
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 if (const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl"))
anatofuz
parents:
diff changeset
61 diag(TypeDecl->getBeginLoc(), "type defined here", DiagnosticIDs::Note);
anatofuz
parents:
diff changeset
62 }
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64 } // namespace hicpp
anatofuz
parents:
diff changeset
65 } // namespace tidy
anatofuz
parents:
diff changeset
66 } // namespace clang