| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "ContextUtils.h" |
| 8 |
#include "TypeUtils.h" |
| 9 |
#include "clazy_stl.h" |
| 10 |
|
| 11 |
#include <clang/AST/Decl.h> |
| 12 |
#include <clang/AST/DeclBase.h> |
| 13 |
#include <clang/AST/DeclCXX.h> |
| 14 |
#include <clang/AST/DeclFriend.h> |
| 15 |
#include <clang/Basic/LLVM.h> |
| 16 |
#include <clang/Basic/SourceLocation.h> |
| 17 |
#include <clang/Basic/SourceManager.h> |
| 18 |
#include <clang/Basic/Specifiers.h> |
| 19 |
#include <llvm/Support/raw_ostream.h> |
| 20 |
|
| 21 |
using namespace clang; |
| 22 |
|
| 23 |
std::vector<DeclContext *> clazy::contextsForDecl(DeclContext *currentScope) |
| 24 |
{ |
| 25 |
std::vector<DeclContext *> decls; |
| 26 |
decls.reserve(20); // jump-start |
| 27 |
while (currentScope) { |
| 28 |
decls.push_back(currentScope); |
| 29 |
currentScope = currentScope->getParent(); |
| 30 |
} |
| 31 |
|
| 32 |
return decls; |
| 33 |
} |
| 34 |
|
| 35 |
static std::string nameForContext(DeclContext *context) |
| 36 |
{ |
| 37 |
if (auto *ns = dyn_cast<NamespaceDecl>(context)) { |
| 38 |
return ns->getNameAsString(); |
| 39 |
} |
| 40 |
if (auto *rec = dyn_cast<CXXRecordDecl>(context)) { |
| 41 |
return rec->getNameAsString(); |
| 42 |
} else if (auto *method = dyn_cast<CXXMethodDecl>(context)) { |
| 43 |
return method->getNameAsString(); |
| 44 |
} else if (isa<TranslationUnitDecl>(context)) { |
| 45 |
return {}; |
| 46 |
} else { |
| 47 |
llvm::errs() << "Unhandled kind: " << context->getDeclKindName() << "\n"; |
| 48 |
} |
| 49 |
|
| 50 |
return {}; |
| 51 |
} |
| 52 |
|
| 53 |
std::string clazy::getMostNeededQualifiedName(const SourceManager &sourceManager, |
| 54 |
CXXMethodDecl *method, |
| 55 |
DeclContext *currentScope, |
| 56 |
SourceLocation usageLoc, |
| 57 |
bool honourUsingDirectives) |
| 58 |
{ |
| 59 |
if (!currentScope) { |
| 60 |
return method->getQualifiedNameAsString(); |
| 61 |
} |
| 62 |
|
| 63 |
// All namespaces, classes, inner class qualifications |
| 64 |
auto methodContexts = clazy::contextsForDecl(method->getDeclContext()); |
| 65 |
|
| 66 |
// Visible scopes in current scope |
| 67 |
auto visibleContexts = clazy::contextsForDecl(currentScope); |
| 68 |
|
| 69 |
// Collect using directives |
| 70 |
std::vector<UsingDirectiveDecl *> usings; |
| 71 |
if (honourUsingDirectives) { |
| 72 |
for (DeclContext *context : visibleContexts) { |
| 73 |
for (UsingDirectiveDecl *directive : context->using_directives()) { |
| 74 |
usings.push_back(directive); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
for (UsingDirectiveDecl *u : usings) { |
| 80 |
NamespaceDecl *ns = u->getNominatedNamespace(); |
| 81 |
if (ns) { |
| 82 |
if (sourceManager.isBeforeInSLocAddrSpace(usageLoc, u->getBeginLoc())) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
visibleContexts.push_back(ns->getFirstDecl()); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
for (DeclContext *context : visibleContexts) { |
| 90 |
if (context != method->getParent()) { // Don't remove the most immediate |
| 91 |
auto it = std::ranges::find_if(methodContexts, [context](DeclContext *c) { |
| 92 |
if (c == context) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
auto *ns1 = dyn_cast<NamespaceDecl>(c); |
| 96 |
auto *ns2 = dyn_cast<NamespaceDecl>(context); |
| 97 |
return ns1 && ns2 && ns1->getQualifiedNameAsString() == ns2->getQualifiedNameAsString(); |
| 98 |
}); |
| 99 |
if (it != methodContexts.end()) { |
| 100 |
methodContexts.erase(it, it + 1); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
std::string neededContexts; |
| 106 |
for (DeclContext *context : methodContexts) { |
| 107 |
neededContexts = nameForContext(context) + "::" + neededContexts; |
| 108 |
} |
| 109 |
|
| 110 |
const std::string result = neededContexts + method->getNameAsString(); |
| 111 |
return result; |
| 112 |
} |
| 113 |
|
| 114 |
bool clazy::canTakeAddressOf(CXXMethodDecl *method, DeclContext *context, bool &isSpecialProtectedCase) |
| 115 |
{ |
| 116 |
isSpecialProtectedCase = false; |
| 117 |
if (!method || !method->getParent()) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
if (method->getAccess() == clang::AccessSpecifier::AS_public) { |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
if (!context) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
CXXRecordDecl *contextRecord = nullptr; |
| 130 |
|
| 131 |
do { |
| 132 |
contextRecord = dyn_cast<CXXRecordDecl>(context); |
| 133 |
context = context->getParent(); |
| 134 |
} while (contextRecord == nullptr && context); |
| 135 |
|
| 136 |
if (!contextRecord) { // If we're not inside a class method we can't take the address of a private/protected method |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
CXXRecordDecl *record = method->getParent(); |
| 141 |
if (record == contextRecord) { |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
// We're inside a method belonging to a class (contextRecord). |
| 146 |
// Is contextRecord a friend of record ? Lets check: |
| 147 |
|
| 148 |
for (auto *fr : record->friends()) { |
| 149 |
TypeSourceInfo *si = fr->getFriendType(); |
| 150 |
if (si) { |
| 151 |
const Type *t = si->getType().getTypePtrOrNull(); |
| 152 |
const CXXRecordDecl *friendClass = t ? t->getAsCXXRecordDecl() : nullptr; |
| 153 |
if (friendClass == contextRecord) { |
| 154 |
return true; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// There's still hope, lets see if the context is nested inside the class we're trying to access |
| 160 |
// Inner classes can access private members of outer classes. |
| 161 |
DeclContext *it = contextRecord; |
| 162 |
do { |
| 163 |
it = it->getParent(); |
| 164 |
if (it == record) { |
| 165 |
return true; |
| 166 |
} |
| 167 |
} while (it); |
| 168 |
|
| 169 |
if (method->getAccess() == clang::AccessSpecifier::AS_private) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
if (method->getAccess() != clang::AccessSpecifier::AS_protected) { // shouldn't happen, must be protected at this point. |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
// For protected there's still hope, since record might be a derived or base class |
| 178 |
if (clazy::derivesFrom(record, contextRecord)) { |
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
if (clazy::derivesFrom(contextRecord, record)) { |
| 183 |
isSpecialProtectedCase = true; |
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
return false; |
| 188 |
} |