-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConnectCallChecker.cpp
182 lines (145 loc) · 4.72 KB
/
ConnectCallChecker.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "ConnectCallChecker.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/Expr.h"
#include "clang/Frontend/CompilerInstance.h"
#include "ConnectCall.h"
namespace
{
TypeDeclaration connectNormalizedType(const TypeDeclaration &decl)
{
// This converts "const Foo &" or "const Foo" in to just "Foo"
const bool isUnredirected = decl.indirections().empty();
const bool isSimpleReference = (decl.indirections().size() == 1) &&
(decl.indirections().front() == Indirection::Reference);
if (decl.isConst() && (isUnredirected || isSimpleReference))
{
return TypeDeclaration(false, decl.typeName(), std::vector<Indirection>());
}
return decl;
}
bool argumentsMatch(const MetaMethodSignature &connectRef, const MetaMethodSignature &methodDecl)
{
auto methodDeclArgIt = methodDecl.arguments().begin();
for(const MetaMethodArgument &connectRefArg : connectRef.arguments())
{
if (methodDeclArgIt == methodDecl.arguments().end())
{
// We have more args than the method decl
return false;
}
if (connectNormalizedType(connectRefArg.type()) != connectNormalizedType(methodDeclArgIt->type()))
{
// Incompatible arguments
return false;
}
methodDeclArgIt++;
}
// Any remaining args on the method decl must have defaults
for(; methodDeclArgIt != methodDecl.arguments().end(); methodDeclArgIt++)
{
if (!methodDeclArgIt->hasDefault())
{
return false;
}
}
return true;
}
bool signaturesConnectable(const MetaMethodSignature &sender, const MetaMethodSignature &receiver)
{
auto senderArgIt = sender.arguments().begin();
for(const MetaMethodArgument &receiverArg : receiver.arguments())
{
if (senderArgIt == sender.arguments().end())
{
// We're trying to receive more args than are being sent
return false;
}
if (connectNormalizedType(senderArgIt->type()) != connectNormalizedType(receiverArg.type()))
{
return false;
}
senderArgIt++;
}
// Looks good
return true;
}
}
ConnectCallChecker::ConnectCallChecker(clang::CompilerInstance &instance) :
ConnectCallVisitor(instance),
mReporter(instance.getSourceManager())
{
}
bool ConnectCallChecker::VisitConnectCall(const ConnectCall &call)
{
const MetaMethodRef sendMethod(call.sendMethod());
const MetaMethodRef receiveMethod(call.receiveMethod());
if (!sendMethod.isValid())
{
mReporter.report(call) << "Can't parse raw slot reference" << ("\"" + sendMethod.rawString() + "\"");
}
if (!receiveMethod.isValid())
{
mReporter.report(call) << "Can't parse raw signal or slot reference" << ("\"" + receiveMethod.rawString() + "\"");
}
if (!sendMethod.isValid() || !receiveMethod.isValid())
{
// Doesn't make sense to continue
return true;
}
if (!referencedMethodExists(call.sender(), sendMethod))
{
mReporter.report(call) << "Can't find sender" << sendMethod.spelling() << "on" << call.sender()->getQualifiedNameAsString();
}
if (!referencedMethodExists(call.receiver(), receiveMethod))
{
mReporter.report(call) << "Can't find receiver" << receiveMethod.spelling() << "on" << call.receiver()->getQualifiedNameAsString();
}
if (sendMethod.type() != MetaMethodType::Signal)
{
mReporter.report(call) << "Sender" << sendMethod.spelling() << "is not a signal";
}
if (!signaturesConnectable(sendMethod.signature(), receiveMethod.signature()))
{
mReporter.report(call) << "Sender" << sendMethod.spelling() << "not compatible with receiver" << receiveMethod.spelling();
}
return true;
}
bool ConnectCallChecker::referencedMethodExists(const clang::CXXRecordDecl *recordDecl, const MetaMethodRef &ref)
{
// Try all our methods
for(auto it = recordDecl->method_begin(); it != recordDecl->method_end(); it++)
{
if (it->getNameAsString() == ref.signature().methodName())
{
const clang::CXXMethodDecl *decl = *it;
// Parse the declaration
MetaMethodSignature parsedDecl(decl, compilerInstance());
if (!parsedDecl.isValid())
{
// Dump the source text
const clang::SourceManager &sm = compilerInstance().getSourceManager();
const clang::SourceRange range = decl->getSourceRange();
const std::string sourceText(
sm.getCharacterData(range.getBegin()),
sm.getCharacterData(range.getEnd()) + 1);
mReporter.report(decl) << "Can't parse signal or slot declaration" << ("\"" + sourceText + "\"");
continue;
}
if (argumentsMatch(ref.signature(), parsedDecl))
{
return true;
}
}
}
// Try our superclasses recursively
for(auto it = recordDecl->bases_begin(); it != recordDecl->bases_end(); it++)
{
clang::CXXRecordDecl *baseDecl = it->getType()->getAsCXXRecordDecl();
if (baseDecl && referencedMethodExists(baseDecl, ref))
{
return true;
}
}
return false;
}