2018-02-15 17:08:51 +08:00
|
|
|
//===--- ThrowKeywordMissingCheck.cpp - clang-tidy-------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-02-15 17:08:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ThrowKeywordMissingCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace bugprone {
|
|
|
|
|
|
|
|
void ThrowKeywordMissingCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
auto CtorInitializerList =
|
|
|
|
cxxConstructorDecl(hasAnyConstructorInitializer(anything()));
|
|
|
|
|
|
|
|
Finder->addMatcher(
|
2020-12-28 09:54:04 +08:00
|
|
|
cxxConstructExpr(
|
|
|
|
hasType(cxxRecordDecl(
|
|
|
|
isSameOrDerivedFrom(matchesName("[Ee]xception|EXCEPTION")))),
|
2021-12-15 23:59:04 +08:00
|
|
|
unless(anyOf(
|
|
|
|
hasAncestor(
|
|
|
|
stmt(anyOf(cxxThrowExpr(), callExpr(), returnStmt()))),
|
|
|
|
hasAncestor(decl(anyOf(varDecl(), fieldDecl()))),
|
|
|
|
hasAncestor(expr(cxxNewExpr(hasAnyPlacementArg(anything())))),
|
|
|
|
allOf(hasAncestor(CtorInitializerList),
|
|
|
|
unless(hasAncestor(cxxCatchStmt()))))))
|
2018-02-15 17:08:51 +08:00
|
|
|
.bind("temporary-exception-not-thrown"),
|
2020-12-28 09:54:04 +08:00
|
|
|
this);
|
2018-02-15 17:08:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThrowKeywordMissingCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *TemporaryExpr =
|
|
|
|
Result.Nodes.getNodeAs<Expr>("temporary-exception-not-thrown");
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(TemporaryExpr->getBeginLoc(), "suspicious exception object created but "
|
2018-02-15 17:08:51 +08:00
|
|
|
"not thrown; did you mean 'throw %0'?")
|
|
|
|
<< TemporaryExpr->getType().getBaseTypeIdentifier()->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace bugprone
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|