Aaron Ballman | a3274e5 | 2017-08-11 16:31:51 +0000 | [diff] [blame] | 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 | |
| 10 | #include "ExceptionBaseclassCheck.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | |
Aaron Ballman | a3274e5 | 2017-08-11 16:31:51 +0000 | [diff] [blame] | 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( |
Jonas Toth | b1efe51 | 2018-09-17 13:55:10 +0000 | [diff] [blame^] | 25 | cxxThrowExpr( |
| 26 | allOf( |
| 27 | unless(has(expr(anyOf(isTypeDependent(), isValueDependent())))), |
| 28 | // The thrown value is not derived from 'std::exception'. |
| 29 | has(expr(unless(hasType( |
| 30 | qualType(hasCanonicalType(hasDeclaration(cxxRecordDecl( |
| 31 | isSameOrDerivedFrom(hasName("::std::exception")))))))))), |
| 32 | // This condition is always true, but will bind to the |
| 33 | // template value if the thrown type is templated. |
| 34 | anyOf(has(expr(hasType( |
| 35 | substTemplateTypeParmType().bind("templ_type")))), |
| 36 | anything()), |
| 37 | // Bind to the declaration of the type of the value that |
| 38 | // is thrown. 'anything()' is necessary to always suceed |
| 39 | // in the 'eachOf' because builtin types are not |
| 40 | // 'namedDecl'. |
| 41 | eachOf(has(expr(hasType(namedDecl().bind("decl")))), anything()))) |
Aaron Ballman | a3274e5 | 2017-08-11 16:31:51 +0000 | [diff] [blame] | 42 | .bind("bad_throw"), |
| 43 | this); |
| 44 | } |
| 45 | |
| 46 | void ExceptionBaseclassCheck::check(const MatchFinder::MatchResult &Result) { |
| 47 | const auto *BadThrow = Result.Nodes.getNodeAs<CXXThrowExpr>("bad_throw"); |
Jonas Toth | b1efe51 | 2018-09-17 13:55:10 +0000 | [diff] [blame^] | 48 | assert(BadThrow && "Did not match the throw expression"); |
Jonas Toth | 673dbd1 | 2017-08-30 15:59:01 +0000 | [diff] [blame] | 49 | |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 50 | diag(BadThrow->getSubExpr()->getBeginLoc(), "throwing an exception whose " |
Jonas Toth | 673dbd1 | 2017-08-30 15:59:01 +0000 | [diff] [blame] | 51 | "type %0 is not derived from " |
| 52 | "'std::exception'") |
| 53 | << BadThrow->getSubExpr()->getType() << BadThrow->getSourceRange(); |
Aaron Ballman | a3274e5 | 2017-08-11 16:31:51 +0000 | [diff] [blame] | 54 | |
Jonas Toth | b1efe51 | 2018-09-17 13:55:10 +0000 | [diff] [blame^] | 55 | if (const auto *Template = |
| 56 | Result.Nodes.getNodeAs<SubstTemplateTypeParmType>("templ_type")) |
| 57 | diag(BadThrow->getSubExpr()->getBeginLoc(), |
| 58 | "type %0 is a template instantiation of %1", DiagnosticIDs::Note) |
| 59 | << BadThrow->getSubExpr()->getType() |
| 60 | << Template->getReplacedParameter()->getDecl(); |
| 61 | |
| 62 | if (const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl")) |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 63 | diag(TypeDecl->getBeginLoc(), "type defined here", DiagnosticIDs::Note); |
Aaron Ballman | a3274e5 | 2017-08-11 16:31:51 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace hicpp |
| 67 | } // namespace tidy |
| 68 | } // namespace clang |