10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
20 CheckAnonymousTemporaries(Options.get(
"CheckThrowTemporaries", true)),
21 WarnOnLargeObject(Options.get(
"WarnOnLargeObject", false)),
24 Options.get(
"MaxSize", std::numeric_limits<uint64_t>::max())),
25 MaxSize(MaxSizeOptions) {}
28 Finder->addMatcher(cxxThrowExpr().bind(
"throw"),
this);
29 Finder->addMatcher(cxxCatchStmt().bind(
"catch"),
this);
34 Options.store(Opts,
"CheckThrowTemporaries",
true);
35 Options.store(Opts,
"WarnOnLargeObjects", WarnOnLargeObject);
36 Options.store(Opts,
"MaxSize", MaxSizeOptions);
40 const MatchFinder::MatchResult &Result) {
41 diagnoseThrowLocations(Result.Nodes.getNodeAs<CXXThrowExpr>(
"throw"));
42 diagnoseCatchLocations(Result.Nodes.getNodeAs<CXXCatchStmt>(
"catch"),
46bool ThrowByValueCatchByReferenceCheck::isFunctionParameter(
47 const DeclRefExpr *DeclRefExpr) {
48 return isa<ParmVarDecl>(DeclRefExpr->getDecl());
51bool ThrowByValueCatchByReferenceCheck::isCatchVariable(
52 const DeclRefExpr *DeclRefExpr) {
53 auto *ValueDecl = DeclRefExpr->getDecl();
54 if (
auto *VarDecl = dyn_cast<clang::VarDecl>(ValueDecl))
55 return VarDecl->isExceptionVariable();
59bool ThrowByValueCatchByReferenceCheck::isFunctionOrCatchVar(
60 const DeclRefExpr *DeclRefExpr) {
61 return isFunctionParameter(DeclRefExpr) || isCatchVariable(DeclRefExpr);
64void ThrowByValueCatchByReferenceCheck::diagnoseThrowLocations(
65 const CXXThrowExpr *ThrowExpr) {
68 auto *SubExpr = ThrowExpr->getSubExpr();
71 auto QualType = SubExpr->getType();
72 if (QualType->isPointerType()) {
75 auto *Inner = SubExpr->IgnoreParenImpCasts();
76 if (isa<StringLiteral>(Inner))
79 auto *DeclRef = dyn_cast<DeclRefExpr>(Inner);
80 if (DeclRef && isCatchVariable(DeclRef)) {
83 diag(SubExpr->getBeginLoc(),
"throw expression throws a pointer; it should "
84 "throw a non-pointer value instead");
99 if (CheckAnonymousTemporaries) {
101 auto *CurrentSubExpr = SubExpr->IgnoreImpCasts();
102 const auto *VariableReference = dyn_cast<DeclRefExpr>(CurrentSubExpr);
103 const auto *ConstructorCall = dyn_cast<CXXConstructExpr>(CurrentSubExpr);
107 if (VariableReference)
108 Emit = !isFunctionOrCatchVar(VariableReference);
109 else if (ConstructorCall &&
110 ConstructorCall->getConstructor()->isCopyOrMoveConstructor()) {
117 auto *CurrentSubExpr = (*ArgIter)->IgnoreImpCasts();
118 if (CurrentSubExpr->isLValue()) {
119 if (
auto *Tmp = dyn_cast<DeclRefExpr>(CurrentSubExpr))
120 Emit = !isFunctionOrCatchVar(Tmp);
121 else if (isa<CallExpr>(CurrentSubExpr))
126 diag(SubExpr->getBeginLoc(),
127 "throw expression should throw anonymous temporary values instead");
131void ThrowByValueCatchByReferenceCheck::diagnoseCatchLocations(
132 const CXXCatchStmt *CatchStmt, ASTContext &Context) {
135 auto CaughtType = CatchStmt->getCaughtType();
136 if (CaughtType.isNull())
138 auto *VarDecl = CatchStmt->getExceptionDecl();
139 if (
const auto *PT = CaughtType.getCanonicalType()->getAs<PointerType>()) {
140 const char *DiagMsgCatchReference =
141 "catch handler catches a pointer value; "
142 "should throw a non-pointer value and "
143 "catch by reference instead";
146 if (!PT->getPointeeType()->isAnyCharacterType())
147 diag(VarDecl->getBeginLoc(), DiagMsgCatchReference);
148 }
else if (!CaughtType->isReferenceType()) {
149 const char *DiagMsgCatchReference =
"catch handler catches by value; "
150 "should catch by reference instead";
154 if (!CaughtType.isTrivialType(Context)) {
155 diag(VarDecl->getBeginLoc(), DiagMsgCatchReference);
156 }
else if (WarnOnLargeObject) {
162 if (MaxSize == std::numeric_limits<uint64_t>::max())
163 MaxSize = Context.getTypeSize(Context.getSizeType());
164 if (Context.getTypeSize(CaughtType) > MaxSize)
165 diag(VarDecl->getBeginLoc(), DiagMsgCatchReference);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
ThrowByValueCatchByReferenceCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
llvm::StringMap< ClangTidyValue > OptionMap