Debian Codegraph @

src/checks/level1/readlock-detaching.cpp

raw
1 /*
2 Copyright (C) 2025 Author <your@email>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "readlock-detaching.h"
8 #include "QtUtils.h"
9 #include "TypeUtils.h"
10 #include "Utils.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/ExprCXX.h"
13 #include "clang/AST/RecursiveASTVisitor.h"
14 #include "clang/Basic/OperatorKinds.h"
15
16 using namespace clang;
17 using namespace clang::ast_matchers;
18
19 template<typename T>
20 const T *getParentOfTypeRecursive(const DynTypedNode &node, ASTContext &context, int depth = 0)
21 {
22 if (depth > 20) // avoid infinite recursion
23 return nullptr;
24
25 if (const T *result = node.get<T>()) {
26 return result;
27 }
28 auto parents = context.getParents(node);
29 for (const auto &parent : parents) {
30 if (const T *result = parent.get<T>())
31 return result;
32 if (const T *recursive = getParentOfTypeRecursive<T>(parent, context, depth + 1))
33 return recursive;
34 }
35
36 return nullptr;
37 }
38
39 bool isWithinRange(const SourceRange callLocRange, SourceRange range, const SourceManager &SM)
40 {
41 return !SM.isBeforeInTranslationUnit(callLocRange.getBegin(), range.getBegin()) && !SM.isBeforeInTranslationUnit(range.getEnd(), callLocRange.getEnd());
42 }
43
44 using namespace clang;
45
46 class MemberCallVisitor : public RecursiveASTVisitor<MemberCallVisitor>
47 {
48 public:
49 explicit MemberCallVisitor(ASTContext *Context, CheckBase *check, SourceRange lockRange)
50 : Context(Context)
51 , check(check)
52 , lockRange(lockRange)
53 {
54 }
55
56 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const *call)
57 {
58 // We only care about members for now
59 if (!llvm::isa<MemberExpr>(call->getArg(0))) {
60 return true;
61 }
62
63 std::string className = "";
64 if (const auto *memberExpr = dyn_cast<MemberExpr>(call->getArg(0))) {
65 QualType qt = memberExpr->getType(); // 'QMap<QString, QString>'
66 if (qt->isRecordType()) {
67 className = qt->getAs<RecordType>()->getDecl()->getNameAsString();
68 }
69 }
70 if (className.empty()) {
71 return true;
72 }
73
74 const auto methods = clazy::detachingMethods();
75 const auto method = methods.find(className);
76 if (method == methods.end()) {
77 return true;
78 }
79 const auto methodName = std::string("operator") + getOperatorSpelling(call->getOperator());
80 if (std::ranges::find(method->second, methodName) == method->second.end()) {
81 return true;
82 }
83
84 if (!isWithinRange(call->getSourceRange(), lockRange, Context->getSourceManager())) {
85 return true;
86 }
87
88 check->emitWarning(call, "Possibly detaching a member while inside of a read-only mutex scope", true);
89
90 return true;
91 }
92 bool VisitCXXMemberCallExpr(CXXMemberCallExpr const *call)
93 {
94 const auto recordName = call->getRecordDecl()->getNameAsString();
95 // We only care about members for now
96 const bool isLocker = recordName == "QReadLocker" || recordName == "QReadWriteLock" || recordName == "QBasicReadWriteLock";
97 if (!llvm::isa<MemberExpr>(call->getImplicitObjectArgument()) && !isLocker) {
98 return true;
99 }
100
101 const auto &methods = clazy::detachingMethods();
102 const auto methodName = clazy::name(call->getMethodDecl());
103 if (isLocker && methodName == "unlock") {
104 lockRange.setEnd(call->getEndLoc());
105 return true;
106 }
107
108 const auto method = methods.find(recordName);
109
110 if (method == methods.end()) {
111 return true;
112 }
113 if (std::ranges::find(method->second, methodName) == method->second.end()) {
114 return true;
115 }
116
117 if (!isWithinRange(call->getSourceRange(), lockRange, Context->getSourceManager())) {
118 return true;
119 }
120
121 check->emitWarning(call, "Possibly detaching a member while inside of a read-only mutex scope", true);
122
123 return true;
124 }
125
126 private:
127 ASTContext *const Context;
128 CheckBase *const check;
129 SourceRange lockRange;
130 };
131
132 class ReadlockDetaching_Callback : public ClazyAstMatcherCallback
133 {
134 public:
135 using ClazyAstMatcherCallback::ClazyAstMatcherCallback;
136
137 void run(const MatchFinder::MatchResult &result) override
138 {
139 const auto getSorroundingCompondStmt = [&result](auto &expr) -> const CompoundStmt * {
140 const auto parents = result.Context->getParents(expr);
141 for (auto parent : parents) {
142 if (auto surroundingFnc = getParentOfTypeRecursive<CompoundStmt>(parent, *result.Context)) {
143 return surroundingFnc;
144 }
145 }
146 return nullptr;
147 };
148
149 if (const auto *constructExpr = result.Nodes.getNodeAs<CXXConstructExpr>("qreadlockerCtor")) {
150 if (const auto surroundingStmt = getSorroundingCompondStmt(*constructExpr)) {
151 MemberCallVisitor visitor(result.Context, m_check, SourceRange(constructExpr->getBeginLoc(), surroundingStmt->getEndLoc()));
152 visitor.TraverseStmt(const_cast<CompoundStmt *>(surroundingStmt));
153 }
154 }
155
156 if (const auto lockCall = result.Nodes.getNodeAs<CXXMemberCallExpr>("qreadwritelockCall")) {
157 const auto surroundingStmt = getSorroundingCompondStmt(*lockCall);
158 // The end location for sure needs to be adjusted within the visitor. We just assume it is going to be in the same block the lock was locked
159 MemberCallVisitor visitor(result.Context, m_check, SourceRange(lockCall->getBeginLoc(), surroundingStmt->getEndLoc()));
160 visitor.TraverseStmt(const_cast<CompoundStmt *>(surroundingStmt));
161 }
162 }
163 };
164
165 void ReadlockDetaching::registerASTMatchers(MatchFinder &finder)
166 {
167 m_astMatcherCallBack = std::make_unique<ReadlockDetaching_Callback>(this);
168 finder.addMatcher(cxxConstructExpr(hasType(cxxRecordDecl(hasName("QReadLocker")))).bind("qreadlockerCtor"), m_astMatcherCallBack.get());
169 finder.addMatcher(
170 cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("QReadWriteLock")))), callee(cxxMethodDecl(hasName("lockForRead")))).bind("qreadwritelockCall"),
171 m_astMatcherCallBack.get());
172 }