Debian Codegraph @

src/checks/level0/writing-to-temporary.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 "writing-to-temporary.h"
11 #include "Utils.h"
12 #include "clazy_stl.h"
13
14 #include <clang/AST/Decl.h>
15 #include <clang/AST/DeclCXX.h>
16 #include <clang/AST/Expr.h>
17 #include <clang/AST/Stmt.h>
18 #include <clang/AST/Type.h>
19 #include <clang/Basic/LLVM.h>
20 #include <llvm/Support/Casting.h>
21
22 #include <vector>
23
24 using namespace clang;
25
26 WritingToTemporary::WritingToTemporary(const std::string &name, Options options)
27 : CheckBase(name, options)
28
29 {
30 m_filesToIgnore = {"qstring.h"};
31 }
32
33 static bool isDisallowedClass(const std::string &className)
34 {
35 static const std::vector<std::string> disallowed = {
36 "QTextCursor",
37 "QDomElement",
38 "KConfigGroup",
39 "QWebElement",
40 "QScriptValue",
41 "QTextLine",
42 "QTextBlock",
43 "QDomNode",
44 "QJSValue",
45 "QTextTableCell",
46 };
47 return clazy::contains(disallowed, className);
48 }
49
50 static bool isDisallowedMethod(const std::string &name)
51 {
52 static const std::vector<std::string> disallowed = {"QColor::getCmyk", "QColor::getCmykF"};
53 return clazy::contains(disallowed, name);
54 }
55
56 static bool isKnownType(const std::string &className)
57 {
58 static const std::vector<std::string> types = {
59 "QList", "QVector", "QMap", "QHash", "QString", "QSet", "QByteArray", "QUrl", "QVarLengthArray", "QLinkedList", "QRect",
60 "QRectF", "QBitmap", "QVector2D", "QVector3D", "QVector4D", "QSize", "QSizeF", "QSizePolicy", "QPoint", "QPointF", "QColor",
61 };
62
63 return clazy::contains(types, className);
64 }
65
66 void WritingToTemporary::VisitStmt(clang::Stmt *stmt)
67 {
68 auto *callExpr = dyn_cast<CallExpr>(stmt);
69 if (!callExpr) {
70 return;
71 }
72
73 if (shouldIgnoreFile(stmt->getBeginLoc())) {
74 return;
75 }
76
77 // For a chain like getFoo().setBar(), returns {setBar(), getFoo()}
78 std::vector<CallExpr *> callExprs = Utils::callListForChain(callExpr); // callExpr is the call to setBar()
79 if (callExprs.size() < 2) {
80 return;
81 }
82
83 CallExpr *firstCallToBeEvaluated = callExprs.at(callExprs.size() - 1); // This is the call to getFoo()
84 FunctionDecl *firstFunc = firstCallToBeEvaluated->getDirectCallee();
85 if (!firstFunc) {
86 return;
87 }
88
89 CallExpr *secondCallToBeEvaluated = callExprs.at(callExprs.size() - 2); // This is the call to setBar()
90 FunctionDecl *secondFunc = secondCallToBeEvaluated->getDirectCallee();
91 if (!secondFunc) {
92 return;
93 }
94
95 auto *secondMethod = dyn_cast<CXXMethodDecl>(secondFunc);
96 if (!secondMethod || secondMethod->isConst() || secondMethod->isStatic()) {
97 return;
98 }
99
100 CXXRecordDecl *record = secondMethod->getParent();
101 if (!record || isDisallowedClass(record->getNameAsString())) {
102 return;
103 }
104
105 QualType qt = firstFunc->getReturnType();
106 const Type *firstFuncReturnType = qt.getTypePtrOrNull();
107 if (!firstFuncReturnType || firstFuncReturnType->isPointerType() || firstFuncReturnType->isReferenceType()) {
108 return;
109 }
110
111 qt = secondFunc->getReturnType();
112 const Type *secondFuncReturnType = qt.getTypePtrOrNull();
113 if (!secondFuncReturnType || !secondFuncReturnType->isVoidType()) {
114 return;
115 }
116
117 if (!isOptionSet("widen-criteria") && !isKnownType(record->getNameAsString()) && !clazy::startsWith(secondFunc->getNameAsString(), "set")) {
118 return;
119 }
120
121 const std::string &methodName = secondMethod->getQualifiedNameAsString();
122 if (isDisallowedMethod(methodName)) {
123 return;
124 }
125
126 emitWarning(stmt->getBeginLoc(), "Call to temporary is a no-op: " + methodName);
127 }