clang-tools  8.0.0
ExceptionBaseclassCheck.cpp
Go to the documentation of this file.
1 //===--- ExceptionBaseclassCheck.cpp - clang-tidy--------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace hicpp {
19 
20 void ExceptionBaseclassCheck::registerMatchers(MatchFinder *Finder) {
21  if (!getLangOpts().CPlusPlus)
22  return;
23 
24  Finder->addMatcher(
25  cxxThrowExpr(
26  unless(has(expr(anyOf(isTypeDependent(), isValueDependent())))),
27  // The thrown value is not derived from 'std::exception'.
28  has(expr(unless(
29  hasType(qualType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
30  isSameOrDerivedFrom(hasName("::std::exception")))))))))),
31  // This condition is always true, but will bind to the
32  // template value if the thrown type is templated.
33  anyOf(has(expr(
34  hasType(substTemplateTypeParmType().bind("templ_type")))),
35  anything()),
36  // Bind to the declaration of the type of the value that
37  // is thrown. 'anything()' is necessary to always suceed
38  // in the 'eachOf' because builtin types are not
39  // 'namedDecl'.
40  eachOf(has(expr(hasType(namedDecl().bind("decl")))), anything()))
41  .bind("bad_throw"),
42  this);
43 }
44 
45 void ExceptionBaseclassCheck::check(const MatchFinder::MatchResult &Result) {
46  const auto *BadThrow = Result.Nodes.getNodeAs<CXXThrowExpr>("bad_throw");
47  assert(BadThrow && "Did not match the throw expression");
48 
49  diag(BadThrow->getSubExpr()->getBeginLoc(), "throwing an exception whose "
50  "type %0 is not derived from "
51  "'std::exception'")
52  << BadThrow->getSubExpr()->getType() << BadThrow->getSourceRange();
53 
54  if (const auto *Template =
55  Result.Nodes.getNodeAs<SubstTemplateTypeParmType>("templ_type"))
56  diag(BadThrow->getSubExpr()->getBeginLoc(),
57  "type %0 is a template instantiation of %1", DiagnosticIDs::Note)
58  << BadThrow->getSubExpr()->getType()
59  << Template->getReplacedParameter()->getDecl();
60 
61  if (const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl"))
62  diag(TypeDecl->getBeginLoc(), "type defined here", DiagnosticIDs::Note);
63 }
64 
65 } // namespace hicpp
66 } // namespace tidy
67 } // namespace clang
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//