| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef CLAZY_CONTEXT_UTILS_H |
| 8 |
#define CLAZY_CONTEXT_UTILS_H |
| 9 |
|
| 10 |
#include "TypeUtils.h" |
| 11 |
|
| 12 |
#include <clang/AST/Decl.h> |
| 13 |
#include <clang/AST/DeclBase.h> |
| 14 |
#include <clang/AST/DeclCXX.h> |
| 15 |
#include <clang/AST/Stmt.h> |
| 16 |
#include <clang/AST/Type.h> |
| 17 |
#include <llvm/Support/Casting.h> |
| 18 |
|
| 19 |
#include <string> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
namespace clang |
| 23 |
{ |
| 24 |
class ValueDecl; |
| 25 |
class DeclContext; |
| 26 |
class SourceManager; |
| 27 |
class SourceLocation; |
| 28 |
class CXXMethodDecl; |
| 29 |
class ParentMap; |
| 30 |
} |
| 31 |
|
| 32 |
namespace clazy |
| 33 |
{ |
| 34 |
/** |
| 35 |
* Returns true if a decl is inside a function, instead of say a class field. |
| 36 |
* This returns true if "QList<int> l;" is a local variable, instead of being a class field such |
| 37 |
* as struct Foo { QList<int> l; } |
| 38 |
*/ |
| 39 |
inline bool isValueDeclInFunctionContext(const clang::ValueDecl *valueDecl) |
| 40 |
{ |
| 41 |
const auto *context = valueDecl ? valueDecl->getDeclContext() : nullptr; |
| 42 |
return llvm::isa_and_nonnull<clang::FunctionDecl>(context) && !llvm::isa<clang::ParmVarDecl>(valueDecl); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns the list of scopes for a decl context (namespaces, classes, inner classes, etc) |
| 47 |
* The inner ones are at the beginning of the list |
| 48 |
*/ |
| 49 |
std::vector<clang::DeclContext *> contextsForDecl(clang::DeclContext *); |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns the first context for a decl. |
| 53 |
*/ |
| 54 |
inline clang::DeclContext *contextForDecl(clang::Decl *decl) |
| 55 |
{ |
| 56 |
if (!decl) { |
| 57 |
return nullptr; |
| 58 |
} |
| 59 |
|
| 60 |
if (auto *context = llvm::dyn_cast<clang::DeclContext>(decl)) { |
| 61 |
return context; |
| 62 |
} |
| 63 |
|
| 64 |
return decl->getDeclContext(); |
| 65 |
} |
| 66 |
|
| 67 |
inline clang::NamespaceDecl *namespaceForDecl(clang::Decl *decl) |
| 68 |
{ |
| 69 |
if (!decl) { |
| 70 |
return nullptr; |
| 71 |
} |
| 72 |
|
| 73 |
clang::DeclContext *declContext = decl->getDeclContext(); |
| 74 |
while (declContext) { |
| 75 |
if (auto *ns = llvm::dyn_cast<clang::NamespaceDecl>(declContext)) { |
| 76 |
return ns; |
| 77 |
} |
| 78 |
|
| 79 |
declContext = declContext->getParent(); |
| 80 |
} |
| 81 |
|
| 82 |
return nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
inline clang::NamespaceDecl *namespaceForType(clang::QualType q) |
| 86 |
{ |
| 87 |
if (q.isNull()) { |
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
|
| 91 |
q = clazy::pointeeQualType(q); |
| 92 |
// Check if it's a class, struct, union or enum |
| 93 |
clang::TagDecl *rec = q->getAsTagDecl(); |
| 94 |
if (rec) { |
| 95 |
return namespaceForDecl(rec); |
| 96 |
} |
| 97 |
|
| 98 |
// Or maybe it's a typedef to a builtin type: |
| 99 |
const auto *typeDefType = q->getAs<clang::TypedefType>(); |
| 100 |
if (typeDefType) { |
| 101 |
clang::TypedefNameDecl *typedeff = typeDefType->getDecl(); |
| 102 |
return namespaceForDecl(typedeff); |
| 103 |
} |
| 104 |
|
| 105 |
return nullptr; |
| 106 |
} |
| 107 |
|
| 108 |
inline clang::NamespaceDecl *namespaceForFunction(clang::FunctionDecl *func) |
| 109 |
{ |
| 110 |
if (auto *ns = llvm::dyn_cast<clang::NamespaceDecl>(func->getDeclContext())) { |
| 111 |
return ns; |
| 112 |
} |
| 113 |
|
| 114 |
return namespaceForDecl(func); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Returns the first context of type T in which the specified context is in. |
| 119 |
* Contexts are namespaces, classes, inner classes, functions, etc. |
| 120 |
|
| 121 |
*/ |
| 122 |
template<typename T> |
| 123 |
T *firstContextOfType(clang::DeclContext *context) |
| 124 |
{ |
| 125 |
if (!context) { |
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
|
| 129 |
if (llvm::isa<T>(context)) { |
| 130 |
return llvm::cast<T>(context); |
| 131 |
} |
| 132 |
|
| 133 |
return clazy::firstContextOfType<T>(context->getParent()); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Returns fully/semi-fully qualified name for a method, but doesn't over-qualify with namespaces |
| 138 |
* which we're already in. |
| 139 |
* |
| 140 |
* if currentScope == nullptr will return a fully qualified name |
| 141 |
*/ |
| 142 |
|
| 143 |
std::string getMostNeededQualifiedName(const clang::SourceManager &sourceManager, |
| 144 |
clang::CXXMethodDecl *method, |
| 145 |
clang::DeclContext *currentScope, |
| 146 |
clang::SourceLocation usageLoc, |
| 147 |
bool honourUsingDirectives); |
| 148 |
|
| 149 |
/** |
| 150 |
* Returns true, if in a specific context, we can take the address of a method |
| 151 |
* for example doing &ClassName::SomePrivateMember might not be possible if the member is private |
| 152 |
* but might be possible if we're in a context which is friend of ClassName |
| 153 |
* Or it might be protected but context is a derived class |
| 154 |
* |
| 155 |
* When inside a derived class scope it's possible to take the address of a protected base method |
| 156 |
* but only if you qualify it with the derived class name, so &Derived::baseMethod, instead of &Base::baseMethod |
| 157 |
* If this was the case then isSpecialProtectedCase will be true |
| 158 |
*/ |
| 159 |
bool canTakeAddressOf(clang::CXXMethodDecl *method, clang::DeclContext *context, bool &isSpecialProtectedCase); |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
#endif |