summaryrefslogtreecommitdiff
path: root/SlotCall.cpp
blob: 4ef6b8568e670cd6e853a8ca93f56991a5c08ca7 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright (c) 2016 Novasys Ingénierie.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>

#include <string>

#include "clang_headers.h"

#include "AddActionCall.h"
#include "ConnectCall.h"
#include "DisconnectCall.h"
#include "SingleShotCall.h"
#include "SlotCall.h"

using namespace std;
using namespace clang;

const list<SlotCall::processor> SlotCall::processors = list<SlotCall::processor>({
    AddActionCall::processCallExpr,
    ConnectCall::processCallExpr,
    DisconnectCall::processCallExpr,
    SingleShotCall::processCallExpr,
});

void
SlotCall::processCallExpr(Context &context, const CallExpr *expr)
{
    bool matches;

    for (processor p : processors) {
        matches = p(context, expr);

        if (matches) {
            return;
        }
    }
}

bool
SlotCall::isnotspace(int c)
{
    return !isspace(c);
}

void
SlotCall::ltrim(string &s)
{
    s.erase(s.begin(), find_if(s.begin(), s.end(), isnotspace));
}
                                     
void
SlotCall::rtrim(string &s)
{
    s.erase(find_if(s.rbegin(), s.rend(), isnotspace).base(), s.end());
}

void
SlotCall::trim(string &s)
{
    ltrim(s);
    rtrim(s);
}

string
SlotCall::getSource(const Expr *expr)
{
    return Lexer::getSourceText(CharSourceRange::getTokenRange(expr->getSourceRange()),
                                _context._ci.getSourceManager(),
                                _context._ci.getLangOpts()).str();
}

string
SlotCall::getArgument(const string &s)
{
    size_t start, end;
    string arg;

    start = s.find("(");

    if (start == string::npos) {
        return "";
    }

    start++;
    end = s.find("(", start);

    if (end == string::npos) {
        return "";
    }

    arg = s.substr(start, end - start);
    trim(arg);
    return arg;
}

string
SlotCall::getRawType(const Expr *expr)
{
    const CXXRecordDecl *decl;

    decl = expr->IgnoreImpCasts()->getType()->getPointeeCXXRecordDecl();

    if (decl == nullptr) {
        return "";
    }

    return decl->getName();
}

bool SlotCall::checkSuppressionFromQtObjectHeader(const Expr *expr)
{
    SourceLocation fileLoc = _context.getSourceMgr().getFileLoc(expr->getLocStart());
    string s = _context.getSourceMgr().getFilename(fileLoc).str();

    // Not as accurate as possible, but should do the job for everyone
    return (s.find("/qobject.h") != string::npos);
}

bool
SlotCall::checkSuppression(const Expr *expr)
{
    if (checkSuppressionFromQtObjectHeader(expr)) {
        return true;
    }

    return false;
}

string
SlotCall::getSignal(const Expr *sender, const Expr *signal)
{
    string source, type, arg, result;
    size_t pos;

    type = getRawType(sender);

    if (type.empty()) {
        _context.warn(sender->getLocStart(), "unable to identify type");
        goto out;
    }

    source = getSource(signal);
    pos = source.find("SIGNAL");

    if (pos == string::npos) {
        if (!checkSuppression(signal)) {
            _context.warn(signal->getLocStart(), "no SIGNAL macro");
        }

        goto out;
    }

    arg = getArgument(source.substr(pos));

    if (arg.empty()) {
        _context.warn(signal->getLocStart(), "malformed SIGNAL macro usage");
        goto out;
    }

    result.append("&").append(type).append("::").append(arg);
    trim(result);

out:
    return result;
}

string
SlotCall::getSlot(const Expr *receiver, const Expr *slot)
{
    string source, type, arg, result;
    size_t pos;

    type = getRawType(receiver);

    if (type.empty()) {
        _context.warn(receiver->getLocStart(), "unable to identify type");
        goto out;
    }

    source = getSource(slot);
    pos = source.find("SLOT");

    if (pos == string::npos) {
        if (!checkSuppression(slot)) {
            _context.warn(slot->getLocStart(), "no SLOT macro");
        }

        goto out;
    }

    arg = getArgument(source.substr(pos));

    if (arg.empty()) {
        _context.warn(slot->getLocStart(), "malformed SLOT macro usage");
        goto out;
    }

    result.append("&").append(type).append("::").append(arg);
    trim(result);

out:
    return result;
}

void
SlotCall::replace(const clang::Expr *expr, const string &s)
{
    pair<SourceLocation, SourceLocation> loc_pair =
        _context.getSourceMgr().getExpansionRange(expr->getLocStart());
    SourceRange range(loc_pair.first, loc_pair.second);
    bool error = _context.ReplaceText(range, s);

    if (error) {
        _context.note(expr->getLocStart(), "unable to transform expression");
    }
}