Debian Codegraph @

src/checks/level1/range-loop-detach.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 "range-loop-detach.h"
11 #include "ClazyContext.h"
12 #include "FixItUtils.h"
13 #include "LoopUtils.h"
14 #include "PreProcessorVisitor.h"
15 #include "QtUtils.h"
16 #include "StmtBodyRange.h"
17 #include "TypeUtils.h"
18 #include "Utils.h"
19
20 #include <clang/AST/Decl.h>
21 #include <clang/AST/DeclCXX.h>
22 #include <clang/AST/Expr.h>
23 #include <clang/AST/Stmt.h>
24 #include <clang/AST/StmtCXX.h>
25 #include <clang/AST/Type.h>
26 #include <llvm/Support/Casting.h>
27
28 using namespace clang;
29
30 namespace clazy
31 {
32 /**
33 * Returns true if we can prove the container doesn't detach.
34 * Returns false otherwise, meaning that you can't conclude anything if false is returned.
35 *
36 * For true to be returned, all these conditions must verify:
37 * - Container is a local variable
38 * - It's not passed to any function
39 * - It's not assigned to another variable
40 */
41 bool containerNeverDetaches(const clang::VarDecl *valDecl, StmtBodyRange bodyRange) // clazy:exclude=function-args-by-value
42 {
43 // This helps for bug 367485
44
45 if (!valDecl) {
46 return false;
47 }
48
49 const auto *const context = dyn_cast<FunctionDecl>(valDecl->getDeclContext());
50 if (!context) {
51 return false;
52 }
53
54 bodyRange.body = context->getBody();
55 if (!bodyRange.body) {
56 return false;
57 }
58
59 if (valDecl->hasInit()) {
60 if (const auto *cleanupExpr = dyn_cast<clang::ExprWithCleanups>(valDecl->getInit())) {
61 if (const auto *ce = dyn_cast<clang::CXXConstructExpr>(cleanupExpr->getSubExpr())) {
62 if (!ce->isListInitialization() && !ce->isStdInitListInitialization()) {
63 // When initing via copy or move ctor there's possible detachments.
64 return false;
65 }
66 } else if (dyn_cast<clang::CXXBindTemporaryExpr>(cleanupExpr->getSubExpr())) {
67 return false;
68 }
69 }
70 }
71
72 // TODO1: Being passed to a function as const should be OK
73 if (Utils::isPassedToFunction(bodyRange, valDecl, false)) {
74 return false;
75 }
76
77 return true;
78 }
79 }
80
81 void RangeLoopDetach::VisitStmt(clang::Stmt *stmt)
82 {
83 if (auto *rangeLoop = dyn_cast<CXXForRangeStmt>(stmt)) {
84 processForRangeLoop(rangeLoop);
85 }
86 }
87
88 bool RangeLoopDetach::islvalue(Expr *exp, SourceLocation &endLoc)
89 {
90 if (isa<DeclRefExpr>(exp)) {
91 endLoc = clazy::locForEndOfToken(astContext(), exp->getBeginLoc());
92 return true;
93 }
94
95 if (auto *me = dyn_cast<MemberExpr>(exp)) {
96 auto *decl = me->getMemberDecl();
97 if (!decl || isa<FunctionDecl>(decl)) {
98 return false;
99 }
100
101 endLoc = clazy::locForEndOfToken(astContext(), me->getMemberLoc());
102 return true;
103 }
104
105 return false;
106 }
107
108 void RangeLoopDetach::processForRangeLoop(CXXForRangeStmt *rangeLoop)
109 {
110 Expr *containerExpr = rangeLoop->getRangeInit();
111 if (!containerExpr) {
112 return;
113 }
114
115 QualType qt = containerExpr->getType();
116 const Type *t = qt.getTypePtrOrNull();
117 if (!t || !t->isRecordType()) {
118 return;
119 }
120
121 if (qt.isConstQualified()) { // const won't detach
122 return;
123 }
124
125 auto loopVariableType = rangeLoop->getLoopVariable()->getType();
126 if (!clazy::unrefQualType(loopVariableType).isConstQualified() && loopVariableType->isReferenceType()) {
127 return;
128 }
129
130 CXXRecordDecl *record = t->getAsCXXRecordDecl();
131 if (!clazy::isQtCOWIterableClass(Utils::rootBaseClass(record), m_context->qtNamespace())) {
132 return;
133 }
134
135 StmtBodyRange bodyRange(nullptr, &sm(), rangeLoop->getBeginLoc());
136 if (clazy::containerNeverDetaches(clazy::containerDeclForLoop(rangeLoop, m_context->qtNamespace()), bodyRange)) {
137 return;
138 }
139
140 std::vector<FixItHint> fixits;
141
142 SourceLocation end;
143 if (islvalue(containerExpr, /*by-ref*/ end)) {
144 const PreProcessorVisitor *preProcessorVisitor = m_context->preprocessorVisitor;
145 if (!preProcessorVisitor || preProcessorVisitor->qtVersion() >= 50700) { // qAsConst() was added to 5.7
146 clang::SourceRange exprRange = containerExpr->getSourceRange();
147 llvm::StringRef exprText = Lexer::getSourceText(CharSourceRange::getTokenRange(exprRange.getBegin(), exprRange.getEnd()), sm(), lo());
148 std::string insertion = (lo().CPlusPlus17 ? "std::as_const(" : "qAsConst(") + exprText.str() + ")";
149 fixits.push_back(clazy::createReplacement(exprRange, insertion));
150 }
151 }
152
153 auto *typedefType = t->getAs<TypedefType>(); // Typedefs in internal Qt code, like QStringList should not be resolved
154 const std::string name = typedefType ? typedefType->getDecl()->getNameAsString() : record->getNameAsString();
155 emitWarning(rangeLoop->getBeginLoc(), "c++11 range-loop might detach Qt container (" + name + ')', fixits);
156 }