| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2018 Sergio Martins <[email protected]> |
| 3 |
SPDX-FileCopyrightText: 2024 Alexander Lohnau <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "fully-qualified-moc-types.h" |
| 9 |
#include "AccessSpecifierManager.h" |
| 10 |
#include "ClazyContext.h" |
| 11 |
#include "HierarchyUtils.h" |
| 12 |
#include "StringUtils.h" |
| 13 |
#include "TypeUtils.h" |
| 14 |
|
| 15 |
#include <clang/AST/Decl.h> |
| 16 |
#include <clang/AST/DeclCXX.h> |
| 17 |
#include <clang/AST/Expr.h> |
| 18 |
#include <clang/AST/ExprCXX.h> |
| 19 |
#include <clang/AST/Stmt.h> |
| 20 |
#include <clang/AST/Type.h> |
| 21 |
#include <clang/Basic/IdentifierTable.h> |
| 22 |
#include <clang/Basic/LLVM.h> |
| 23 |
#include <clang/Basic/SourceManager.h> |
| 24 |
#include <clang/Lex/Token.h> |
| 25 |
#include <llvm/ADT/ArrayRef.h> |
| 26 |
#include <llvm/ADT/StringRef.h> |
| 27 |
#include <llvm/Support/Casting.h> |
| 28 |
|
| 29 |
using namespace clang; |
| 30 |
|
| 31 |
/* |
| 32 |
* When we get the raw string from the lexer, ensure we remove unneeded whitespaces to get a proper representation of the expression |
| 33 |
*/ |
| 34 |
static std::string normalizeType(const std::string &type) |
| 35 |
{ |
| 36 |
std::string out; |
| 37 |
bool lastWasSpace = false; |
| 38 |
|
| 39 |
for (size_t i = 0; i < type.size(); ++i) { |
| 40 |
const char c = type[i]; |
| 41 |
|
| 42 |
if (isspace(static_cast<unsigned char>(c))) { |
| 43 |
lastWasSpace = true; |
| 44 |
continue; // skip now, decide later |
| 45 |
} |
| 46 |
|
| 47 |
// Remove space before certain punctuation that is relevant here |
| 48 |
if ((c == '<' || c == '>' || c == ',') && !out.empty() && out.back() == ' ') { |
| 49 |
out.pop_back(); |
| 50 |
} |
| 51 |
|
| 52 |
// Add a space if needed (mandatory space between keywords/identifiers) |
| 53 |
if (lastWasSpace) { |
| 54 |
if (!out.empty()) { |
| 55 |
const char prev = out.back(); |
| 56 |
// insert a space if previous and current chars are alphanumeric or underscores |
| 57 |
if ((isalnum(prev) || prev == '_') && (isalnum(c) || c == '_')) { |
| 58 |
out += ' '; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
out += c; |
| 64 |
|
| 65 |
// Add exactly one space after comma |
| 66 |
if (c == ',') { |
| 67 |
out += ' '; |
| 68 |
} |
| 69 |
|
| 70 |
lastWasSpace = false; |
| 71 |
} |
| 72 |
|
| 73 |
return out; |
| 74 |
} |
| 75 |
|
| 76 |
std::string FullyQualifiedMocTypes::writtenType(const ParmVarDecl *param) const |
| 77 |
{ |
| 78 |
TypeSourceInfo *TSI = param->getTypeSourceInfo(); |
| 79 |
if (!TSI) |
| 80 |
return {}; |
| 81 |
|
| 82 |
TypeLoc TL = TSI->getTypeLoc(); |
| 83 |
while (true) { |
| 84 |
if (auto PtrLoc = TL.getAs<PointerTypeLoc>()) { |
| 85 |
TL = PtrLoc.getPointeeLoc(); |
| 86 |
} else if (auto RefLoc = TL.getAs<ReferenceTypeLoc>()) { |
| 87 |
TL = RefLoc.getPointeeLoc(); |
| 88 |
} else { |
| 89 |
break; // reached underlying type |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
CharSourceRange CSR = CharSourceRange::getTokenRange(TL.getSourceRange()); |
| 94 |
StringRef text = Lexer::getSourceText(CSR, sm(), lo()); |
| 95 |
return normalizeType(text.str()); |
| 96 |
} |
| 97 |
|
| 98 |
void FullyQualifiedMocTypes::VisitDecl(clang::Decl *decl) |
| 99 |
{ |
| 100 |
auto *method = dyn_cast<CXXMethodDecl>(decl); |
| 101 |
if (!method) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
const AccessSpecifierManager *accessSpecifierManager = m_context->accessSpecifierManager; |
| 106 |
if (!accessSpecifierManager) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
if (handleQ_PROPERTY(method)) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
if (method->isThisDeclarationADefinition() && !method->hasInlineBody()) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
QtAccessSpecifierType qst = accessSpecifierManager->qtAccessSpecifierType(method); |
| 119 |
if (qst != QtAccessSpecifier_Signal && qst != QtAccessSpecifier_Slot && qst != QtAccessSpecifier_Invokable) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
for (auto *param : method->parameters()) { |
| 124 |
QualType t = clazy::pointeeQualType(param->getType()); |
| 125 |
const std::string typeName = writtenType(param); |
| 126 |
std::string qualifiedTypeName; |
| 127 |
if (!typeIsFullyQualified(t, /*by-ref*/ qualifiedTypeName, typeName)) { |
| 128 |
SourceRange fixitRange = param->getTypeSourceInfo()->getTypeLoc().getSourceRange(); |
| 129 |
// We don't want to include the & or * characters for the fixit range |
| 130 |
if (param->getType()->isReferenceType() || param->getType()->isPointerType()) { |
| 131 |
fixitRange = SourceRange(fixitRange.getBegin(), fixitRange.getEnd().getLocWithOffset(-1)); |
| 132 |
} |
| 133 |
std::vector fixits{FixItHint::CreateReplacement(fixitRange, qualifiedTypeName)}; |
| 134 |
std::string warning = accessSpecifierManager->qtAccessSpecifierTypeStr(qst).str() + " arguments need to be fully-qualified"; |
| 135 |
emitWarning(param->getTypeSpecStartLoc(), warning, fixits); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if (qst == QtAccessSpecifier_Slot || qst == QtAccessSpecifier_Invokable) { |
| 140 |
QualType returnT = method->getReturnType(); |
| 141 |
SourceRange returnTypeSourceRange = method->getReturnTypeSourceRange(); |
| 142 |
if (returnT->isReferenceType() || returnT->isPointerType()) { |
| 143 |
returnTypeSourceRange = SourceRange(returnTypeSourceRange.getBegin(), returnTypeSourceRange.getEnd().getLocWithOffset(-1)); |
| 144 |
} |
| 145 |
StringRef returnText = Lexer::getSourceText(CharSourceRange::getTokenRange(returnTypeSourceRange), sm(), lo()); |
| 146 |
const std::string typeName = normalizeType(returnText.str()); |
| 147 |
std::string qualifiedTypeName; |
| 148 |
if (!typeName.empty() && !typeIsFullyQualified(clazy::pointeeQualType(returnT), /*by-ref*/ qualifiedTypeName, typeName)) { |
| 149 |
if (method->getReturnType()->isReferenceType() || method->getReturnType()->isPointerType()) { |
| 150 |
// We don't want to include the & or * characters for the fixit range |
| 151 |
returnTypeSourceRange = SourceRange(returnTypeSourceRange.getBegin(), returnTypeSourceRange.getEnd().getLocWithOffset(-1)); |
| 152 |
} |
| 153 |
std::string warning = accessSpecifierManager->qtAccessSpecifierTypeStr(qst).str() + " return types need to be fully-qualified"; |
| 154 |
std::vector fixits{FixItHint::CreateReplacement(returnTypeSourceRange, qualifiedTypeName)}; |
| 155 |
emitWarning(returnTypeSourceRange.getBegin(), warning, fixits); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
std::string FullyQualifiedMocTypes::getQualifiedNameOfType(const Type *ptr, bool resolveTemplateArgs) const |
| 161 |
{ |
| 162 |
if (ptr->isEnumeralType() && ptr->getAs<EnumType>()) { |
| 163 |
return ptr->getAs<EnumType>()->getDecl()->getQualifiedNameAsString(); |
| 164 |
} |
| 165 |
if (auto *typedefDecl = ptr->getAs<TypedefType>(); typedefDecl && typedefDecl->getDecl()) { |
| 166 |
return typedefDecl->getDecl()->getQualifiedNameAsString(); |
| 167 |
} else if (auto templateSpec = ptr->getAs<TemplateSpecializationType>(); templateSpec && resolveTemplateArgs) { |
| 168 |
return resolveTemplateType(templateSpec, false); |
| 169 |
} else if (auto templateSpec = ptr->getAs<TemplateSpecializationType>()) { |
| 170 |
// In case one uses a typedef with generics, like QVector<QString> in Qt6 |
| 171 |
// The docs indicate getAsTemplateDecl might be null - so be prepared for that |
| 172 |
if (auto *decl = templateSpec->getTemplateName().getAsTemplateDecl()) { |
| 173 |
return decl->getQualifiedNameAsString(); |
| 174 |
} |
| 175 |
} else if (auto recordDecl = ptr->getAsRecordDecl()) { |
| 176 |
return recordDecl->getQualifiedNameAsString(); |
| 177 |
} |
| 178 |
// The lexer logic we use to collect the comparison string from the source code has no spaces between the type and * or & |
| 179 |
if (ptr->isPointerType()) { |
| 180 |
return getQualifiedNameOfType(ptr->getPointeeType().getTypePtr(), true) + "*"; |
| 181 |
} |
| 182 |
if (ptr->isReferenceType()) { |
| 183 |
return getQualifiedNameOfType(ptr->getPointeeType().getTypePtr(), true) + "&"; |
| 184 |
} |
| 185 |
return QualType::getFromOpaquePtr(ptr).getAsString(lo()); |
| 186 |
} |
| 187 |
|
| 188 |
// In Qt5, the type would contain lots of unneeded parameters: QDBusPendingReply<bool, void, void, void, void, void, void, void> |
| 189 |
// Thus we need to do all of the shenanigans below |
| 190 |
// specType->getCanonicalTypeInternal().getAsString(m_astContext.getPrintingPolicy()) |
| 191 |
std::string FullyQualifiedMocTypes::resolveTemplateType(const clang::TemplateSpecializationType *ptr, bool checkElabType) const |
| 192 |
{ |
| 193 |
std::string str = getQualifiedNameOfType(ptr, checkElabType); |
| 194 |
str += "<"; |
| 195 |
bool firstArg = true; |
| 196 |
for (auto arg : ptr->template_arguments()) { // We reconstruct the type with the explicitly specified template params |
| 197 |
if (!firstArg) { |
| 198 |
str += ", "; |
| 199 |
} |
| 200 |
firstArg = false; |
| 201 |
if (arg.getKind() == TemplateArgument::Expression) { |
| 202 |
// std::bitset<int(8)>, here int(8) is an expression! |
| 203 |
llvm::raw_string_ostream outputStream(str); |
| 204 |
arg.print(astContext()->getPrintingPolicy(), outputStream, true); |
| 205 |
} else { |
| 206 |
QualType argType = arg.getAsType(); |
| 207 |
if (argType.isConstQualified()) { |
| 208 |
str += "const "; |
| 209 |
} |
| 210 |
str += getQualifiedNameOfType(argType.getTypePtr(), true); |
| 211 |
} |
| 212 |
} |
| 213 |
str += ">"; |
| 214 |
return str; |
| 215 |
} |
| 216 |
|
| 217 |
bool FullyQualifiedMocTypes::typeIsFullyQualified(QualType t, std::string &qualifiedTypeName, const std::string &typeName) const |
| 218 |
{ |
| 219 |
qualifiedTypeName.clear(); |
| 220 |
if (auto *ptr = t.getTypePtrOrNull(); ptr && (ptr->isRecordType() || ptr->isEnumeralType())) { |
| 221 |
if (typeName == "QPrivateSignal") { |
| 222 |
return true; |
| 223 |
} |
| 224 |
|
| 225 |
if (auto specType = ptr->getAs<TemplateSpecializationType>(); specType && !ptr->getAs<TypedefType>()) { |
| 226 |
qualifiedTypeName = resolveTemplateType(specType, false); |
| 227 |
} else if (auto recordDecl = ptr->getAsRecordDecl(); recordDecl && recordDecl->isInAnonymousNamespace()) { |
| 228 |
return true; |
| 229 |
} else { |
| 230 |
qualifiedTypeName = getQualifiedNameOfType(ptr, true); |
| 231 |
} |
| 232 |
return qualifiedTypeName.empty() || typeName == qualifiedTypeName; |
| 233 |
} |
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
bool FullyQualifiedMocTypes::isGadget(CXXRecordDecl *record) const |
| 238 |
{ |
| 239 |
SourceLocation startLoc = record->getBeginLoc(); |
| 240 |
for (const SourceLocation &loc : m_qgadgetMacroLocations) { |
| 241 |
if (sm().getFileID(loc) != sm().getFileID(startLoc)) { |
| 242 |
continue; // Different file |
| 243 |
} |
| 244 |
|
| 245 |
if (sm().isBeforeInSLocAddrSpace(startLoc, loc) && sm().isBeforeInSLocAddrSpace(loc, record->getEndLoc())) { |
| 246 |
return true; // We found a Q_GADGET after start and before end, it's ours. |
| 247 |
} |
| 248 |
} |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
bool FullyQualifiedMocTypes::handleQ_PROPERTY(CXXMethodDecl *method) |
| 253 |
{ |
| 254 |
if (clazy::name(method) != "qt_static_metacall" || !method->hasBody() || method->getDefinition() != method) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
/** |
| 258 |
* Basically diffed a .moc file with and without a namespaced property, |
| 259 |
* the difference is one reinterpret_cast, under an if (_c == QMetaObject::ReadProperty), so |
| 260 |
* that's what we cheak. |
| 261 |
* |
| 262 |
* The AST doesn't have the Q_PROPERTIES as they expand to nothing, so we have |
| 263 |
* to do it this way. |
| 264 |
*/ |
| 265 |
|
| 266 |
auto ifs = clazy::getStatements<IfStmt>(method->getBody()); |
| 267 |
|
| 268 |
for (auto *iff : ifs) { |
| 269 |
auto *bo = dyn_cast<BinaryOperator>(iff->getCond()); |
| 270 |
if (!bo) { |
| 271 |
continue; |
| 272 |
} |
| 273 |
|
| 274 |
auto enumRefs = clazy::getStatements<DeclRefExpr>(bo->getRHS()); |
| 275 |
if (enumRefs.size() == 1) { |
| 276 |
auto *enumerator = dyn_cast<EnumConstantDecl>(enumRefs.at(0)->getDecl()); |
| 277 |
if (enumerator && clazy::name(enumerator) == "ReadProperty") { |
| 278 |
auto switches = clazy::getStatements<SwitchStmt>(iff); // we only want the reinterpret_casts that are inside switches |
| 279 |
for (auto *s : switches) { |
| 280 |
auto reinterprets = clazy::getStatements<CXXReinterpretCastExpr>(s); |
| 281 |
for (auto *reinterpret : reinterprets) { |
| 282 |
QualType qt = clazy::pointeeQualType(reinterpret->getTypeAsWritten()); |
| 283 |
if (auto *record = qt->getAsCXXRecordDecl(); !record || !isGadget(record)) { |
| 284 |
continue; |
| 285 |
} |
| 286 |
|
| 287 |
QualType castType = clazy::pointeeQualType(reinterpret->getTypeAsWritten()).getUnqualifiedType(); |
| 288 |
std::string nameAsWritten = castType.getUnqualifiedType().getAsString(); |
| 289 |
std::string qualifiedName; |
| 290 |
|
| 291 |
if (!typeIsFullyQualified(qt, qualifiedName, nameAsWritten)) { |
| 292 |
// warn in the cxxrecorddecl, since we don't want to warn in the .moc files. |
| 293 |
// Ideally we would do some cross checking with the Q_PROPERTIES, but that's not in the AST |
| 294 |
emitWarning(method->getParent()->getBeginLoc(), |
| 295 |
"Q_PROPERTY of type " + nameAsWritten + " should use full qualification (" + qualifiedName + ")"); |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
return true; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return true; // true, so processing doesn't continue, it's a qt_static_metacall, nothing interesting here unless the properties above |
| 305 |
} |
| 306 |
|
| 307 |
void FullyQualifiedMocTypes::VisitMacroExpands(const clang::Token &MacroNameTok, const clang::SourceRange &range, const MacroInfo *) |
| 308 |
{ |
| 309 |
IdentifierInfo *ii = MacroNameTok.getIdentifierInfo(); |
| 310 |
if (ii && ii->getName() == "Q_GADGET") { |
| 311 |
registerQ_GADGET(range.getBegin()); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
void FullyQualifiedMocTypes::registerQ_GADGET(SourceLocation loc) |
| 316 |
{ |
| 317 |
m_qgadgetMacroLocations.push_back(loc); |
| 318 |
} |