clang-tools 22.0.0git
ThrowKeywordMissingCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14using namespace clang::ast_matchers::internal;
15
16namespace clang::tidy::bugprone {
17
19 const VariadicDynCastAllOfMatcher<Stmt, AttributedStmt> AttributedStmt;
20 // Matches an 'expression-statement', as defined in [stmt.expr]/1.
21 // Not to be confused with the similarly-named GNU extension, the
22 // statement expression.
23 const auto ExprStmt = [&](const Matcher<Expr> &InnerMatcher) {
24 return expr(hasParent(stmt(anyOf(doStmt(), whileStmt(), forStmt(),
25 compoundStmt(), ifStmt(), switchStmt(),
26 labelStmt(), AttributedStmt()))),
27 InnerMatcher);
28 };
29
30 Finder->addMatcher(
31 ExprStmt(
32 cxxConstructExpr(hasType(cxxRecordDecl(anyOf(
33 matchesName("[Ee]xception|EXCEPTION"),
34 hasAnyBase(hasType(hasCanonicalType(recordType(hasDeclaration(
35 cxxRecordDecl(matchesName("[Ee]xception|EXCEPTION"))
36 .bind("base")))))))))))
37 .bind("temporary-exception-not-thrown"),
38 this);
39}
40
41void ThrowKeywordMissingCheck::check(const MatchFinder::MatchResult &Result) {
42 const auto *TemporaryExpr =
43 Result.Nodes.getNodeAs<Expr>("temporary-exception-not-thrown");
44
45 diag(TemporaryExpr->getBeginLoc(), "suspicious exception object created but "
46 "not thrown; did you mean 'throw %0'?")
47 << TemporaryExpr->getType().getBaseTypeIdentifier()->getName();
48
49 if (const auto *BaseDecl = Result.Nodes.getNodeAs<Decl>("base"))
50 diag(BaseDecl->getLocation(),
51 "object type inherits from base class declared here",
52 DiagnosticIDs::Note);
53}
54
55} // namespace clang::tidy::bugprone
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override