| 1 |
/* |
| 2 |
|
| 3 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 4 |
SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB a KDAB Group company [email protected] |
| 5 |
|
| 6 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "AccessSpecifierManager.h" |
| 10 |
#include "QtUtils.h" |
| 11 |
#include "Utils.h" |
| 12 |
|
| 13 |
#include <clang/AST/DeclBase.h> |
| 14 |
#include <clang/AST/DeclCXX.h> |
| 15 |
#include <clang/AST/DeclTemplate.h> |
| 16 |
#include <clang/Basic/IdentifierTable.h> |
| 17 |
#include <clang/Basic/LLVM.h> |
| 18 |
#include <clang/Basic/LangOptions.h> |
| 19 |
#include <clang/Basic/SourceManager.h> |
| 20 |
#include <clang/Frontend/CompilerInstance.h> |
| 21 |
#include <clang/Lex/PPCallbacks.h> |
| 22 |
#include <clang/Lex/Preprocessor.h> |
| 23 |
#include <clang/Lex/Token.h> |
| 24 |
#include <llvm/Support/Casting.h> |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
#include <utility> |
| 28 |
|
| 29 |
namespace clang |
| 30 |
{ |
| 31 |
class MacroArgs; |
| 32 |
class MacroDefinition; |
| 33 |
} // namespace clang |
| 34 |
|
| 35 |
using namespace clang; |
| 36 |
|
| 37 |
static bool accessSpecifierCompare(const ClazyAccessSpecifier &lhs, const ClazyAccessSpecifier &rhs, const SourceManager &sm) |
| 38 |
{ |
| 39 |
if (lhs.loc.isMacroID() || rhs.loc.isMacroID()) { |
| 40 |
// Q_SIGNALS is special because it hides a "public", which is expanded by this macro. |
| 41 |
// That means that both the Q_SIGNALS macro and the "public" will have the same source location. |
| 42 |
// We do want the "public" to appear before, so check if one has a macro id on it. |
| 43 |
|
| 44 |
SourceLocation realLHSLoc = sm.getFileLoc(lhs.loc); |
| 45 |
SourceLocation realRHSLoc = sm.getFileLoc(rhs.loc); |
| 46 |
if (realLHSLoc == realRHSLoc) { |
| 47 |
return lhs.loc.isMacroID(); |
| 48 |
} |
| 49 |
return realLHSLoc < realRHSLoc; |
| 50 |
} |
| 51 |
|
| 52 |
return lhs.loc < rhs.loc; |
| 53 |
} |
| 54 |
|
| 55 |
static void sorted_insert(ClazySpecifierList &v, const ClazyAccessSpecifier &item, const clang::SourceManager &sm) |
| 56 |
{ |
| 57 |
auto pred = [&sm](const ClazyAccessSpecifier &lhs, const ClazyAccessSpecifier &rhs) { |
| 58 |
return accessSpecifierCompare(lhs, rhs, sm); |
| 59 |
}; |
| 60 |
v.insert(std::upper_bound(v.begin(), v.end(), item, pred), item); |
| 61 |
} |
| 62 |
|
| 63 |
class AccessSpecifierPreprocessorCallbacks : public clang::PPCallbacks |
| 64 |
{ |
| 65 |
public: |
| 66 |
AccessSpecifierPreprocessorCallbacks(const clang::SourceManager &sm, const clang::LangOptions &opts) |
| 67 |
: clang::PPCallbacks() |
| 68 |
, m_sm(sm) |
| 69 |
, m_lo(opts) |
| 70 |
{ |
| 71 |
m_qtAccessSpecifiers.reserve(30); // bootstrap it |
| 72 |
} |
| 73 |
|
| 74 |
AccessSpecifierPreprocessorCallbacks(const AccessSpecifierPreprocessorCallbacks &) = delete; |
| 75 |
|
| 76 |
void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) override |
| 77 |
{ |
| 78 |
IdentifierInfo *ii = MacroNameTok.getIdentifierInfo(); |
| 79 |
if (ii && ii->getName() == "QT_NAMESPACE") { |
| 80 |
const auto tokens = MD->getDefinition().getMacroInfo()->tokens(); |
| 81 |
if (tokens.size() == 1) { |
| 82 |
m_qtNamespace = tokens.front().getIdentifierInfo()->getName(); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &def, SourceRange range, const MacroArgs *) override |
| 88 |
{ |
| 89 |
IdentifierInfo *ii = MacroNameTok.getIdentifierInfo(); |
| 90 |
if (!ii) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
const StringRef name = ii->getName(); |
| 95 |
const bool isSlots = name == "slots" || name == "Q_SLOTS"; |
| 96 |
const bool isSignals = isSlots ? false : (name == "signals" || name == "Q_SIGNALS"); |
| 97 |
|
| 98 |
const bool isSlot = (isSlots || isSignals) ? false : name == "Q_SLOT"; |
| 99 |
const bool isSignal = (isSlots || isSignals || isSlot) ? false : name == "Q_SIGNAL"; |
| 100 |
const bool isInvokable = (isSlots || isSignals || isSlot || isSignal) ? false : name == "Q_INVOKABLE"; |
| 101 |
const bool isScriptable = (isSlot || isSignal || isInvokable) ? false : name == "Q_SCRIPTABLE"; |
| 102 |
if (!isSlots && !isSignals && !isSlot && !isSignal && !isInvokable && !isScriptable) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
SourceLocation loc = range.getBegin(); |
| 107 |
if (loc.isMacroID()) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
if (isSignals || isSlots) { |
| 112 |
QtAccessSpecifierType qtAccessSpecifier = isSlots ? QtAccessSpecifier_Slot : QtAccessSpecifier_Signal; |
| 113 |
m_qtAccessSpecifiers.push_back({loc, clang::AS_none, qtAccessSpecifier}); |
| 114 |
} else { |
| 115 |
// Get the location of the method declaration, so we can compare directly when we visit methods |
| 116 |
loc = Utils::locForNextToken(loc, m_sm, m_lo); |
| 117 |
if (loc.isInvalid()) { |
| 118 |
return; |
| 119 |
} |
| 120 |
if (isSignal) { |
| 121 |
m_individualSignals.push_back(loc.getRawEncoding()); |
| 122 |
} else if (isSlot) { |
| 123 |
m_individualSlots.push_back(loc.getRawEncoding()); |
| 124 |
} else if (isInvokable) { |
| 125 |
m_invokables.push_back(loc.getRawEncoding()); |
| 126 |
} else if (isScriptable) { |
| 127 |
m_scriptables.push_back(loc.getRawEncoding()); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
std::vector<unsigned> m_individualSignals; // Q_SIGNAL |
| 133 |
std::vector<unsigned> m_individualSlots; // Q_SLOT |
| 134 |
std::vector<unsigned> m_invokables; // Q_INVOKABLE |
| 135 |
std::vector<unsigned> m_scriptables; // Q_SCRIPTABLE |
| 136 |
std::string m_qtNamespace; |
| 137 |
ClazySpecifierList m_qtAccessSpecifiers; |
| 138 |
const SourceManager &m_sm; |
| 139 |
const LangOptions &m_lo; |
| 140 |
}; |
| 141 |
|
| 142 |
AccessSpecifierManager::AccessSpecifierManager(Preprocessor &pi, bool exportFixesEnabled) |
| 143 |
: m_sm(pi.getSourceManager()) |
| 144 |
, m_preprocessorCallbacks(new AccessSpecifierPreprocessorCallbacks(m_sm, pi.getLangOpts())) |
| 145 |
, m_fixitsEnabled(exportFixesEnabled) |
| 146 |
{ |
| 147 |
pi.addPPCallbacks(std::unique_ptr<PPCallbacks>(m_preprocessorCallbacks)); |
| 148 |
m_visitsNonQObjects = getenv("CLAZY_ACCESSSPECIFIER_NON_QOBJECT") != nullptr; |
| 149 |
} |
| 150 |
|
| 151 |
ClazySpecifierList &AccessSpecifierManager::entryForClassDefinition(CXXRecordDecl *classDecl) |
| 152 |
{ |
| 153 |
ClazySpecifierList &specifiers = m_specifiersMap[classDecl]; |
| 154 |
return specifiers; |
| 155 |
} |
| 156 |
|
| 157 |
const CXXRecordDecl *AccessSpecifierManager::classDefinitionForLoc(SourceLocation loc) const |
| 158 |
{ |
| 159 |
for (const auto &it : m_specifiersMap) { |
| 160 |
const CXXRecordDecl *record = it.first; |
| 161 |
if (record->getBeginLoc() < loc && loc < record->getEndLoc()) { |
| 162 |
return record; |
| 163 |
} |
| 164 |
} |
| 165 |
return nullptr; |
| 166 |
} |
| 167 |
|
| 168 |
void AccessSpecifierManager::VisitDeclaration(Decl *decl) |
| 169 |
{ |
| 170 |
auto *record = dyn_cast<CXXRecordDecl>(decl); |
| 171 |
if (!record) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
const bool isQObject = clazy::isQObject(record, m_preprocessorCallbacks->m_qtNamespace); |
| 176 |
const bool visits = isQObject || (m_fixitsEnabled && m_visitsNonQObjects); |
| 177 |
|
| 178 |
if (!visits) { |
| 179 |
// We're only interested if it's a QObject. Otherwise it's a waste of cpu cycles, |
| 180 |
// causing a big slowdown. However, the copyable-polymorphic fixit needs to know the locations |
| 181 |
// of "private:", so allow that too. |
| 182 |
|
| 183 |
// The copyable-polymorphic fixit is opt-in, via an env variable CLAZY_ACCESSSPECIFIER_NON_QOBJECT |
| 184 |
// as it's buggy |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
// We got a new record, lets fetch signals and slots that the pre-processor gathered |
| 189 |
ClazySpecifierList &specifiers = entryForClassDefinition(record); |
| 190 |
|
| 191 |
auto it = m_preprocessorCallbacks->m_qtAccessSpecifiers.begin(); |
| 192 |
while (it != m_preprocessorCallbacks->m_qtAccessSpecifiers.end()) { |
| 193 |
if (classDefinitionForLoc((*it).loc) == record) { |
| 194 |
sorted_insert(specifiers, *it, m_sm); |
| 195 |
it = m_preprocessorCallbacks->m_qtAccessSpecifiers.erase(it); |
| 196 |
} else { |
| 197 |
++it; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
// Now lets add the normal C++ access specifiers (public, private etc.) |
| 202 |
|
| 203 |
for (auto *d : record->decls()) { |
| 204 |
auto *accessSpec = dyn_cast<AccessSpecDecl>(d); |
| 205 |
if (!accessSpec || accessSpec->getDeclContext() != record) { |
| 206 |
continue; |
| 207 |
} |
| 208 |
ClazySpecifierList &specifiers = entryForClassDefinition(record); |
| 209 |
sorted_insert(specifiers, {accessSpec->getBeginLoc(), accessSpec->getAccess(), QtAccessSpecifier_None}, m_sm); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
QtAccessSpecifierType AccessSpecifierManager::qtAccessSpecifierType(const CXXMethodDecl *method) const |
| 214 |
{ |
| 215 |
if (!method || method->getBeginLoc().isMacroID()) { |
| 216 |
return QtAccessSpecifier_Unknown; |
| 217 |
} |
| 218 |
|
| 219 |
// We want the declaration that's inside class {}, not the ones that are also a method definition |
| 220 |
// and possibly outside the class |
| 221 |
method = method->getCanonicalDecl(); |
| 222 |
|
| 223 |
const CXXRecordDecl *record = method->getParent(); |
| 224 |
if (!record || isa<clang::ClassTemplateSpecializationDecl>(record) || method->isTemplateInstantiation()) { |
| 225 |
return QtAccessSpecifier_None; |
| 226 |
} |
| 227 |
|
| 228 |
const SourceLocation methodLoc = method->getBeginLoc(); |
| 229 |
|
| 230 |
// Process Q_SIGNAL: |
| 231 |
for (auto signalLoc : m_preprocessorCallbacks->m_individualSignals) { |
| 232 |
if (signalLoc == methodLoc.getRawEncoding()) { |
| 233 |
return QtAccessSpecifier_Signal; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// Process Q_SLOT: |
| 238 |
for (auto slotLoc : m_preprocessorCallbacks->m_individualSlots) { |
| 239 |
if (slotLoc == methodLoc.getRawEncoding()) { |
| 240 |
return QtAccessSpecifier_Slot; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
// Process Q_INVOKABLE: |
| 245 |
for (auto loc : m_preprocessorCallbacks->m_invokables) { |
| 246 |
if (loc == methodLoc.getRawEncoding()) { |
| 247 |
return QtAccessSpecifier_Invokable; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
// Process Q_SLOTS and Q_SIGNALS: |
| 252 |
|
| 253 |
auto it = m_specifiersMap.find(record); |
| 254 |
if (it == m_specifiersMap.cend()) { |
| 255 |
return QtAccessSpecifier_None; |
| 256 |
} |
| 257 |
|
| 258 |
const ClazySpecifierList &accessSpecifiers = it->second; |
| 259 |
|
| 260 |
auto pred = [this](const ClazyAccessSpecifier &lhs, const ClazyAccessSpecifier &rhs) { |
| 261 |
return accessSpecifierCompare(lhs, rhs, m_sm); |
| 262 |
}; |
| 263 |
|
| 264 |
const ClazyAccessSpecifier dummy = { |
| 265 |
methodLoc, // we're only interested in the location |
| 266 |
/*dummy*/ clang::AS_none, |
| 267 |
/*dummy*/ QtAccessSpecifier_None, |
| 268 |
}; |
| 269 |
auto i = std::upper_bound(accessSpecifiers.cbegin(), accessSpecifiers.cend(), dummy, pred); |
| 270 |
if (i == accessSpecifiers.cbegin()) { |
| 271 |
return QtAccessSpecifier_None; |
| 272 |
} |
| 273 |
|
| 274 |
--i; // One before the upper bound is the last access specifier before our method |
| 275 |
return (*i).qtAccessSpecifier; |
| 276 |
} |
| 277 |
|
| 278 |
bool AccessSpecifierManager::isScriptable(const CXXMethodDecl *method) const |
| 279 |
{ |
| 280 |
if (!method) { |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
const SourceLocation methodLoc = method->getBeginLoc(); |
| 285 |
if (methodLoc.isMacroID()) { |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
for (auto loc : m_preprocessorCallbacks->m_scriptables) { |
| 290 |
if (loc == methodLoc.getRawEncoding()) { |
| 291 |
return true; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
llvm::StringRef AccessSpecifierManager::qtAccessSpecifierTypeStr(QtAccessSpecifierType t) const |
| 299 |
{ |
| 300 |
switch (t) { |
| 301 |
case QtAccessSpecifier_None: |
| 302 |
case QtAccessSpecifier_Unknown: |
| 303 |
return ""; |
| 304 |
case QtAccessSpecifier_Slot: |
| 305 |
return "slot"; |
| 306 |
case QtAccessSpecifier_Signal: |
| 307 |
return "signal"; |
| 308 |
case QtAccessSpecifier_Invokable: |
| 309 |
return "invokable"; |
| 310 |
} |
| 311 |
|
| 312 |
return ""; |
| 313 |
} |
| 314 |
|
| 315 |
SourceLocation AccessSpecifierManager::firstLocationOfSection(AccessSpecifier specifier, clang::CXXRecordDecl *decl) const |
| 316 |
{ |
| 317 |
auto it = m_specifiersMap.find(decl); |
| 318 |
if (it == m_specifiersMap.end()) { |
| 319 |
return {}; |
| 320 |
} |
| 321 |
|
| 322 |
for (const auto &record : it->second) { |
| 323 |
if (record.accessSpecifier == specifier) { |
| 324 |
return record.loc; |
| 325 |
} |
| 326 |
} |
| 327 |
return {}; |
| 328 |
} |
| 329 |
|
| 330 |
std::string AccessSpecifierManager::qtNamespace() const |
| 331 |
{ |
| 332 |
return m_preprocessorCallbacks->m_qtNamespace; |
| 333 |
} |