Debian Codegraph @

src/checks/level2/virtual-call-ctor.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected]
3 SPDX-FileContributor: Sérgio Martins <[email protected]>
4
5 SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9
10 #include "virtual-call-ctor.h"
11 #include "ClazyContext.h"
12 #include "clazy_stl.h"
13
14 #include <clang/AST/DeclBase.h>
15 #include <clang/AST/DeclCXX.h>
16 #include <clang/AST/Expr.h>
17 #include <clang/AST/ExprCXX.h>
18 #include <clang/AST/Stmt.h>
19 #include <clang/Basic/LLVM.h>
20 #include <clang/Basic/SourceLocation.h>
21 #include <llvm/Support/Casting.h>
22
23 using namespace clang;
24
25 namespace clazy
26 {
27 void getChildsIgnoreLambda(clang::Stmt *stmt, std::vector<CXXMemberCallExpr *> &result_list, int depth = -1)
28 {
29 if (!stmt || dyn_cast<LambdaExpr>(stmt)) {
30 return;
31 }
32
33 auto *cexpr = llvm::dyn_cast<CXXMemberCallExpr>(stmt);
34 if (cexpr) {
35 result_list.push_back(cexpr);
36 }
37
38 if (depth > 0 || depth == -1) {
39 if (depth > 0) {
40 --depth;
41 }
42 for (auto *child : stmt->children()) {
43 getChildsIgnoreLambda(child, result_list, depth);
44 }
45 }
46 }
47 }
48
49 void VirtualCallCtor::VisitDecl(Decl *decl)
50 {
51 auto *ctorDecl = dyn_cast<CXXConstructorDecl>(decl);
52 auto *dtorDecl = dyn_cast<CXXDestructorDecl>(decl);
53 if (!ctorDecl && !dtorDecl) {
54 return;
55 }
56
57 Stmt *ctorOrDtorBody = ctorDecl ? ctorDecl->getBody() : dtorDecl->getBody();
58 if (!ctorOrDtorBody) {
59 return;
60 }
61
62 CXXRecordDecl *classDecl = ctorDecl ? ctorDecl->getParent() : dtorDecl->getParent();
63
64 std::vector<Stmt *> processedStmts;
65 SourceLocation loc = containsVirtualCall(classDecl, ctorOrDtorBody, processedStmts);
66 if (loc.isValid()) {
67 if (ctorDecl) {
68 emitWarning(decl->getBeginLoc(), "Calling pure virtual function in CTOR");
69 } else {
70 emitWarning(decl->getBeginLoc(), "Calling pure virtual function in DTOR");
71 }
72 emitWarning(loc, "Called here");
73 }
74 }
75
76 SourceLocation VirtualCallCtor::containsVirtualCall(clang::CXXRecordDecl *classDecl, clang::Stmt *stmt, std::vector<Stmt *> &processedStmts)
77 {
78 if (!stmt) {
79 return {};
80 }
81
82 // already processed ? we don't want recurring calls
83 if (clazy::contains(processedStmts, stmt)) {
84 return {};
85 }
86
87 processedStmts.push_back(stmt);
88
89 std::vector<CXXMemberCallExpr *> memberCalls;
90
91 // Ignore lambdas, as they are usually used in connects. Might introduce true-negatives. Possible solution would be to check if the lambda is in a connect,
92 // timer, invokeMethod, etc.
93 clazy::getChildsIgnoreLambda(stmt, memberCalls);
94
95 for (CXXMemberCallExpr *callExpr : memberCalls) {
96 CXXMethodDecl *memberDecl = callExpr->getMethodDecl();
97 if (!memberDecl || !isa<CXXThisExpr>(callExpr->getImplicitObjectArgument())) {
98 continue;
99 }
100
101 if (memberDecl->getParent() == classDecl) {
102 if (memberDecl->isPureVirtual()) {
103 return callExpr->getBeginLoc();
104 }
105 if (containsVirtualCall(classDecl, memberDecl->getBody(), processedStmts).isValid()) {
106 return callExpr->getBeginLoc();
107 }
108 }
109 }
110
111 return {};
112 }