| 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-2016 Sergio Martins <[email protected]> |
| 6 |
|
| 7 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "missing-typeinfo.h" |
| 11 |
#include "QtUtils.h" |
| 12 |
#include "StringUtils.h" |
| 13 |
#include "TemplateUtils.h" |
| 14 |
#include "TypeUtils.h" |
| 15 |
|
| 16 |
#include <clang/AST/DeclCXX.h> |
| 17 |
#include <clang/AST/DeclTemplate.h> |
| 18 |
#include <clang/AST/Type.h> |
| 19 |
#include <clang/Basic/SourceManager.h> |
| 20 |
#include <llvm/ADT/StringRef.h> |
| 21 |
|
| 22 |
using namespace clang; |
| 23 |
|
| 24 |
/** |
| 25 |
* Returns true if the call is on a java-style iterator class. |
| 26 |
* Returns if sizeof(T) > sizeof(void*), which would make QList<T> inefficient |
| 27 |
*/ |
| 28 |
inline bool checkTooBigForQList(clang::QualType qt, const clang::ASTContext *context) |
| 29 |
{ |
| 30 |
return (int)context->getTypeSize(qt) <= clazy::sizeOfPointer(context, qt); |
| 31 |
} |
| 32 |
|
| 33 |
void MissingTypeInfo::VisitDecl(clang::Decl *decl) |
| 34 |
{ |
| 35 |
ClassTemplateSpecializationDecl *tstdecl = clazy::templateDecl(decl); |
| 36 |
if (!tstdecl) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
const bool isQList = clazy::name(tstdecl) == "QList"; |
| 41 |
const bool isQVector = isQList ? false : clazy::name(tstdecl) == "QVector"; |
| 42 |
|
| 43 |
if (!isQList && !isQVector) { |
| 44 |
registerQTypeInfo(tstdecl); |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
QualType qt2 = clazy::getTemplateArgumentType(tstdecl, 0); |
| 49 |
const Type *t = qt2.getTypePtrOrNull(); |
| 50 |
CXXRecordDecl *record = t ? t->getAsCXXRecordDecl() : nullptr; |
| 51 |
if (!record || !record->getDefinition() || typeHasClassification(qt2)) { |
| 52 |
return; // Don't crash if we only have a fwd decl |
| 53 |
} |
| 54 |
|
| 55 |
const bool isCopyable = qt2.isTriviallyCopyableType(*astContext()); |
| 56 |
const bool isTooBigForQList = isQList && checkTooBigForQList(qt2, astContext()); |
| 57 |
|
| 58 |
if ((isQVector || isTooBigForQList) && isCopyable) { |
| 59 |
if (sm().isInSystemHeader(record->getBeginLoc())) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
std::string typeName = clazy::name(record).str(); |
| 64 |
if (typeName == "QPair") { // QPair doesn't use Q_DECLARE_TYPEINFO, but rather a explicit QTypeInfo. |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
emitWarning(decl, "Missing Q_DECLARE_TYPEINFO: " + typeName); |
| 69 |
if (!m_context->m_isClangTidy) { // Diagnostics are sorted and emitted in order based on location/message. Not helpful in this case |
| 70 |
emitWarning(record, "Type declared here:", false); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void MissingTypeInfo::registerQTypeInfo(ClassTemplateSpecializationDecl *decl) |
| 76 |
{ |
| 77 |
if (clazy::name(decl) == "QTypeInfo") { |
| 78 |
const std::string typeName = clazy::getTemplateArgumentTypeStr(decl, 0, lo(), /**recordOnly=*/true); |
| 79 |
if (!typeName.empty()) { |
| 80 |
m_typeInfos.insert(typeName); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
bool MissingTypeInfo::typeHasClassification(QualType qt) const |
| 86 |
{ |
| 87 |
return m_typeInfos.find(clazy::simpleTypeName(qt, lo())) != m_typeInfos.end(); |
| 88 |
} |