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))
82 diag(SubExpr->getBeginLoc(),
"throw expression throws a pointer; it should "
83 "throw a non-pointer value instead");
98 if (CheckAnonymousTemporaries) {
100 auto *CurrentSubExpr = SubExpr->IgnoreImpCasts();
101 const auto *VariableReference = dyn_cast<DeclRefExpr>(CurrentSubExpr);
102 const auto *ConstructorCall = dyn_cast<CXXConstructExpr>(CurrentSubExpr);
106 if (VariableReference)
107 Emit = !isFunctionOrCatchVar(VariableReference);
108 else if (ConstructorCall &&
109 ConstructorCall->getConstructor()->isCopyOrMoveConstructor()) {
116 auto *CurrentSubExpr = (*ArgIter)->IgnoreImpCasts();
117 if (CurrentSubExpr->isLValue()) {
118 if (
auto *Tmp = dyn_cast<DeclRefExpr>(CurrentSubExpr))
119 Emit = !isFunctionOrCatchVar(Tmp);
120 else if (isa<CallExpr>(CurrentSubExpr))
125 diag(SubExpr->getBeginLoc(),
126 "throw expression should throw anonymous temporary values instead");
130void ThrowByValueCatchByReferenceCheck::diagnoseCatchLocations(
131 const CXXCatchStmt *CatchStmt, ASTContext &Context) {
134 auto CaughtType = CatchStmt->getCaughtType();
135 if (CaughtType.isNull())
137 auto *VarDecl = CatchStmt->getExceptionDecl();
138 if (
const auto *PT = CaughtType.getCanonicalType()->getAs<PointerType>()) {
139 const char *DiagMsgCatchReference =
140 "catch handler catches a pointer value; "
141 "should throw a non-pointer value and "
142 "catch by reference instead";
145 if (!PT->getPointeeType()->isAnyCharacterType())
146 diag(VarDecl->getBeginLoc(), DiagMsgCatchReference);
147 }
else if (!CaughtType->isReferenceType()) {
148 const char *DiagMsgCatchReference =
"catch handler catches by value; "
149 "should catch by reference instead";
153 if (!CaughtType.isTrivialType(Context)) {
154 diag(VarDecl->getBeginLoc(), DiagMsgCatchReference);
155 }
else if (WarnOnLargeObject) {
161 if (MaxSize == std::numeric_limits<uint64_t>::max())
162 MaxSize = Context.getTypeSize(Context.getSizeType());
163 if (Context.getTypeSize(CaughtType) > MaxSize)
164 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