-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQtUtils.cpp
161 lines (125 loc) · 4.73 KB
/
QtUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
This file is part of the clazy static checker.
Copyright (C) 2015-2016 Sergio Martins <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "clazy_stl.h"
#include "QtUtils.h"
#include "Utils.h"
#include "TypeUtils.h"
#include "MacroUtils.h"
#include "HierarchyUtils.h"
#include "StringUtils.h"
#include <clang/AST/AST.h>
using namespace std;
using namespace clang;
bool QtUtils::isQtIterableClass(clang::CXXRecordDecl *record)
{
if (!record)
return false;
return isQtIterableClass(record->getQualifiedNameAsString());
}
const vector<string> & QtUtils::qtContainers()
{
static const vector<string> classes = { "QListSpecialMethods", "QList", "QVector", "QVarLengthArray", "QMap",
"QHash", "QMultiMap", "QMultiHash", "QSet", "QStack", "QQueue", "QString",
"QByteArray", "QSequentialIterable", "QAssociativeIterable", "QJsonArray", "QLinkedList" };
return classes;
}
bool QtUtils::isQtIterableClass(const string &className)
{
const auto &classes = qtContainers();
return clazy_std::contains(classes, className);
}
bool QtUtils::isQtAssociativeContainer(clang::CXXRecordDecl *record)
{
if (!record)
return false;
return isQtAssociativeContainer(record->getNameAsString());
}
bool QtUtils::isQtAssociativeContainer(const string &className)
{
static const vector<string> classes = { "QSet", "QMap", "QHash" };
return clazy_std::contains(classes, className);
}
bool QtUtils::isBootstrapping(const clang::CompilerInstance &ci)
{
return MacroUtils::isPredefined(ci, "QT_BOOTSTRAPPED");
}
bool QtUtils::isQObject(CXXRecordDecl *decl)
{
if (!decl)
return false;
if (decl->getName() == "QObject")
return true;
return clazy_std::any_of(decl->bases(), [](CXXBaseSpecifier base) {
const Type *type = base.getType().getTypePtr();
return type && QtUtils::isQObject(type->getAsCXXRecordDecl());
});
}
bool QtUtils::isConvertibleTo(const Type *source, const Type *target)
{
if (!source || !target)
return false;
if (source->isPointerType() ^ target->isPointerType())
return false;
if (source == target)
return true;
if (source->getPointeeCXXRecordDecl() && source->getPointeeCXXRecordDecl() == target->getPointeeCXXRecordDecl())
return true;
if (source->isIntegerType() && target->isIntegerType())
return true;
if (source->isFloatingType() && target->isFloatingType())
return true;
return false;
}
bool QtUtils::isInForeach(const clang::CompilerInstance &ci, clang::SourceLocation loc)
{
return MacroUtils::isInAnyMacro(ci, loc, { "Q_FOREACH", "foreach" });
}
bool QtUtils::isJavaIterator(CXXRecordDecl *record)
{
if (!record)
return false;
static const vector<string> names = { "QHashIterator", "QMapIterator", "QSetIterator", "QListIterator",
"QVectorIterator", "QLinkedListIterator", "QStringListIterator" };
return clazy_std::contains(names, record->getNameAsString());
}
bool QtUtils::isJavaIterator(CXXMemberCallExpr *call)
{
if (!call)
return false;
return isJavaIterator(call->getRecordDecl());
}
clang::ValueDecl *QtUtils::signalSenderForConnect(clang::CallExpr *call)
{
if (!call || call->getNumArgs() < 1)
return nullptr;
Expr *firstArg = call->getArg(0);
auto declRef = isa<DeclRefExpr>(firstArg) ? cast<DeclRefExpr>(firstArg) : HierarchyUtils::getFirstChildOfType2<DeclRefExpr>(firstArg);
if (!declRef)
return nullptr;
return declRef->getDecl();
}
bool QtUtils::isTooBigForQList(clang::QualType qt, const clang::CompilerInstance &ci)
{
return (int)ci.getASTContext().getTypeSize(qt) <= TypeUtils::sizeOfPointer(ci, qt);
}
bool QtUtils::isQtContainer(QualType t, LangOptions lo)
{
const string typeName = StringUtils::simpleTypeName(t, lo);
return clazy_std::any_of(QtUtils::qtContainers(), [typeName] (const string &container) {
return container == typeName;
});
}