Aaron Ballman | a3274e5 | 2017-08-11 16:31:51 +0000 | [diff] [blame] | 1 | //===--- ExceptionBaseclassCheck.cpp - clang-tidy--------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Aaron Ballman | a3274e5 | 2017-08-11 16:31:51 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "ExceptionBaseclassCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | |
Aaron Ballman | a3274e5 | 2017-08-11 16:31:51 +0000 | [diff] [blame] | 13 | using namespace clang::ast_matchers; |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
| 17 | namespace hicpp { |
| 18 | |
| 19 | void ExceptionBaseclassCheck::registerMatchers(MatchFinder *Finder) { |
| 20 | if (!getLangOpts().CPlusPlus) |
| 21 | return; |
| 22 | |
| 23 | Finder->addMatcher( |
Jonas Toth | b1efe51 | 2018-09-17 13:55:10 +0000 | [diff] [blame] | 24 | cxxThrowExpr( |
Alexander Kornienko | 976e0c0 | 2018-11-25 02:41:01 +0000 | [diff] [blame] | 25 | unless(has(expr(anyOf(isTypeDependent(), isValueDependent())))), |
| 26 | // The thrown value is not derived from 'std::exception'. |
| 27 | has(expr(unless( |
| 28 | hasType(qualType(hasCanonicalType(hasDeclaration(cxxRecordDecl( |
| 29 | isSameOrDerivedFrom(hasName("::std::exception")))))))))), |
| 30 | // This condition is always true, but will bind to the |
| 31 | // template value if the thrown type is templated. |
| 32 | anyOf(has(expr( |
| 33 | hasType(substTemplateTypeParmType().bind("templ_type")))), |
| 34 | anything()), |
| 35 | // Bind to the declaration of the type of the value that |
| 36 | // is thrown. 'anything()' is necessary to always suceed |
| 37 | // in the 'eachOf' because builtin types are not |
| 38 | // 'namedDecl'. |
| 39 | eachOf(has(expr(hasType(namedDecl().bind("decl")))), anything())) |
Aaron Ballman | a3274e5 | 2017-08-11 16:31:51 +0000 | [diff] [blame] | 40 | .bind("bad_throw"), |
| 41 | this); |
| 42 | } |
| 43 | |
| 44 | void ExceptionBaseclassCheck::check(const MatchFinder::MatchResult &Result) { |
| 45 | const auto *BadThrow = Result.Nodes.getNodeAs<CXXThrowExpr>("bad_throw"); |
Jonas Toth | b1efe51 | 2018-09-17 13:55:10 +0000 | [diff] [blame] | 46 | assert(BadThrow && "Did not match the throw expression"); |
Jonas Toth | 673dbd1 | 2017-08-30 15:59:01 +0000 | [diff] [blame] | 47 | |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 48 | diag(BadThrow->getSubExpr()->getBeginLoc(), "throwing an exception whose " |
Jonas Toth | 673dbd1 | 2017-08-30 15:59:01 +0000 | [diff] [blame] | 49 | "type %0 is not derived from " |
| 50 | "'std::exception'") |
| 51 | << BadThrow->getSubExpr()->getType() << BadThrow->getSourceRange(); |
Aaron Ballman | a3274e5 | 2017-08-11 16:31:51 +0000 | [diff] [blame] | 52 | |
Jonas Toth | b1efe51 | 2018-09-17 13:55:10 +0000 | [diff] [blame] | 53 | if (const auto *Template = |
| 54 | Result.Nodes.getNodeAs<SubstTemplateTypeParmType>("templ_type")) |
| 55 | diag(BadThrow->getSubExpr()->getBeginLoc(), |
| 56 | "type %0 is a template instantiation of %1", DiagnosticIDs::Note) |
| 57 | << BadThrow->getSubExpr()->getType() |
| 58 | << Template->getReplacedParameter()->getDecl(); |
| 59 | |
| 60 | if (const auto *TypeDecl = Result.Nodes.getNodeAs<NamedDecl>("decl")) |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 61 | diag(TypeDecl->getBeginLoc(), "type defined here", DiagnosticIDs::Note); |
Aaron Ballman | a3274e5 | 2017-08-11 16:31:51 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | } // namespace hicpp |
| 65 | } // namespace tidy |
| 66 | } // namespace clang |