Debian Codegraph @

src/checks/detachingbase.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 SPDX-FileCopyrightText: 2025 Alexander Lohnau <[email protected]>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9 */
10
11 #include "detachingbase.h"
12 #include "QtUtils.h"
13 #include "StringUtils.h"
14 #include "clazy_stl.h"
15
16 #include <clang/AST/DeclCXX.h>
17 #include <llvm/ADT/StringRef.h>
18
19 #include <unordered_map>
20
21 using namespace clang;
22
23 DetachingBase::DetachingBase(const std::string &name, Options options)
24 : CheckBase(name, options)
25 {
26 }
27
28 std::vector<FixItHint> DetachingBase::getFixitHints(StringRef className, StringRef functionName, CallExpr *callExpr) const
29 {
30 std::vector<FixItHint> hints;
31 const auto &withCounterparts = clazy::detachingMethodsWithConstCounterParts();
32 const auto &functionsIt = withCounterparts.find(className.str());
33
34 if (functionsIt != withCounterparts.end()) {
35 const auto functions = functionsIt->second;
36 const auto it = std::ranges::find_if(functions, [functionName](const auto &pair) {
37 return pair.first == functionName;
38 });
39 if (it == functions.end() || it->first == it->second) {
40 // No fixit possible
41 } else if (auto *memberCall = dyn_cast<CXXMemberCallExpr>(callExpr)) {
42 if (auto *callee = dyn_cast<MemberExpr>(memberCall->getCallee())) {
43 hints.emplace_back(FixItHint::CreateReplacement(callee->getMemberLoc(), it->second));
44 }
45 } else if (auto *operatorCall = dyn_cast<CXXOperatorCallExpr>(callExpr); operatorCall && it->first == "operator[]") {
46 StringRef argText = Lexer::getSourceText(CharSourceRange::getTokenRange(operatorCall->getArg(1)->getSourceRange()), sm(), lo());
47 SourceLocation right = operatorCall->getEndLoc();
48
49 // Clang SourcRanges are very confusing. Operator calls from a chain yielded the same beginLoc, causing the replacement fixit to be broken
50 // The end position is reliable - use it and go backwards till finding the correct token
51 SourceLocation left = Lexer::GetBeginningOfToken(right, sm(), lo());
52 while (left.isValid() && left != operatorCall->getBeginLoc()) {
53 if (Token tok; !Lexer::getRawToken(left, tok, sm(), lo(), true) && tok.is(tok::l_square)) {
54 break;
55 }
56 left = left.getLocWithOffset(-1);
57 }
58
59 std::string operatorRepleacement = ("." + it->second + "(" + argText + ")").str();
60 hints.emplace_back(FixItHint::CreateReplacement(SourceRange(left, right), operatorRepleacement));
61 }
62 }
63 return hints;
64 }
65
66 bool DetachingBase::isDetachingMethod(CXXMethodDecl *method, DetachingMethodType detachingMethodType) const
67 {
68 if (!method) {
69 return false;
70 }
71
72 const CXXRecordDecl *record = method->getParent();
73 if (!record) {
74 return false;
75 }
76
77 StringRef className = clazy::name(record);
78 StringRef methodName = clazy::name(method);
79
80 if (detachingMethodType == DetachingMethod) {
81 const auto &methodsByType = clazy::detachingMethods();
82 auto it = methodsByType.find(className.str());
83 if (it != methodsByType.cend()) {
84 const auto &methods = it->second;
85 return clazy::contains(methods, methodName);
86 }
87 }
88
89 const auto &methodsByType = clazy::detachingMethodsWithConstCounterParts();
90
91 auto it = methodsByType.find(className.str());
92 if (it != methodsByType.cend()) {
93 const auto &methods = it->second;
94 return std::ranges::any_of(methods, [&methodName](auto pair) {
95 return pair.first == methodName;
96 });
97 }
98
99 return false;
100 }