Debian Codegraph @

src/checks/manuallevel/qbytearray-conversion-to-c-style.cpp

raw
1 /*
2 Copyright (C) 2025 Ahmad Samir <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "qbytearray-conversion-to-c-style.h"
8 #include "ClazyContext.h"
9 #include "FixItUtils.h"
10 #include "HierarchyUtils.h"
11 #include "QtUtils.h"
12 #include "TypeUtils.h"
13 #include "Utils.h"
14
15 #include <clang/AST/AST.h>
16
17 // #include <iostream>
18
19 using namespace clang;
20
21 void QBytearrayConversionToCStyle::VisitStmt(clang::Stmt *stmt)
22 {
23 if (!stmt) {
24 return;
25 }
26
27 auto memberExpr = dyn_cast<CXXMemberCallExpr>(stmt);
28 if (!memberExpr) {
29 return;
30 }
31
32 auto methodDecl = memberExpr->getMethodDecl();
33 if (!methodDecl) {
34 return;
35 }
36 CXXRecordDecl *className = methodDecl->getParent();
37 if (!className || className->getName() != "QByteArray") {
38 return;
39 }
40
41 constexpr const char byteArrayOp[] = "operator const char *";
42 auto *callee = dyn_cast<MemberExpr>(memberExpr->getCallee());
43 if (!callee || callee->getMemberNameInfo().getName().getAsString() != byteArrayOp) {
44 return;
45 }
46
47 static const std::string msg = "Don't rely on the QByteArray implicit conversion to 'const char *'.";
48
49 SourceRange sr = callee->getSourceRange();
50 if (!sr.isValid()) {
51 return;
52 }
53
54 if (auto *funcCastExpr = clazy::getFirstChildOfType<CXXFunctionalCastExpr>(stmt)) {
55 auto *literal = clazy::getFirstChildOfType<StringLiteral>(funcCastExpr);
56 if (!literal) {
57 return;
58 }
59 CharSourceRange cr = Lexer::getAsCharRange(sr, sm(), lo());
60 if (!cr.isValid()) {
61 return;
62 }
63 std::string_view str = Lexer::getSourceText(cr, sm(), lo());
64 // QByteArrayLiteral("foo") or QByteArray("foo")
65 if (str.back() != ')') {
66 return;
67 }
68
69 auto modifyStr = [&str](const char *name) {
70 str.remove_suffix(1);
71 str.remove_prefix(strlen(name));
72 };
73 // This only works with Qt6 where QByteArrayLiteral is a macro, but not with
74 // Qt5 where QByteArrayLiteral is a lambda.
75 if (str.find("QByteArrayLiteral(") == 0) {
76 modifyStr("QByteArrayLiteral(");
77 CharSourceRange expRange = sm().getExpansionRange(funcCastExpr->getBeginLoc());
78 if (!expRange.isValid()) {
79 return;
80 }
81 std::vector<FixItHint> fixits = {clazy::createReplacement(expRange.getAsRange(), std::string{str})};
82 emitWarning(expRange.getBegin(), msg, fixits);
83 return;
84 } else if (str.find("QByteArray(") == 0) {
85 modifyStr("QByteArray(");
86 std::vector<FixItHint> fixits = {clazy::createReplacement(sr, std::string{str})};
87 emitWarning(sr.getBegin(), msg, fixits);
88 return;
89 }
90 }
91
92 SourceLocation begin = sm().getSpellingLoc(sr.getBegin());
93 SourceLocation end = sm().getSpellingLoc(sr.getEnd());
94 CharSourceRange r = CharSourceRange::getTokenRange(begin, end);
95 if (!r.isValid()) {
96 return;
97 }
98
99 StringRef text = Lexer::getSourceText(r, sm(), lo());
100 if (text.empty()) {
101 return;
102 }
103
104 if (auto *opCallExpr = clazy::getFirstChildOfType<CXXOperatorCallExpr>(stmt)) {
105 if (opCallExpr->getOperator() == clang::OO_Plus) {
106 std::string fixed = "QByteArray{" + std::string{text} + "}.constData()";
107 emitWarning(begin, msg, {clazy::createReplacement(sr, fixed)});
108 return;
109 }
110 }
111
112 std::vector<FixItHint> fixits = {clazy::createReplacement(sr, std::string{text} + ".constData()")};
113 emitWarning(begin, msg, fixits);
114 }