PreprocessorEnvironment.cpp 7.48 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
con's avatar
con committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
  Copyright 2005 Roberto Raggi <roberto@kdevelop.org>

  Permission to use, copy, modify, distribute, and sell this software and its
  documentation for any purpose is hereby granted without fee, provided that
  the above copyright notice appear in all copies and that both that
  copyright notice and this permission notice appear in supporting
  documentation.

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

52
53
#include "PreprocessorEnvironment.h"
#include "Macro.h"
54
#include <QDebug>
con's avatar
con committed
55
56
#include <cstring>

57
using namespace CPlusPlus;
con's avatar
con committed
58

hjk's avatar
hjk committed
59
Environment::Environment()
con's avatar
con committed
60
    : currentLine(0),
61
      hideNext(false),
con's avatar
con committed
62
63
64
65
66
67
68
69
      _macros(0),
      _allocated_macros(0),
      _macro_count(-1),
      _hash(0),
      _hash_count(401)
{
}

hjk's avatar
hjk committed
70
Environment::~Environment()
con's avatar
con committed
71
72
73
74
75
76
77
78
79
80
{
    if (_macros) {
        qDeleteAll(firstMacro(), lastMacro());
        free(_macros);
    }

    if (_hash)
        free(_hash);
}

hjk's avatar
hjk committed
81
82
83
84
unsigned Environment::macroCount() const
{
    return _macro_count + 1;
}
con's avatar
con committed
85

hjk's avatar
hjk committed
86
87
88
89
Macro *Environment::macroAt(unsigned index) const
{
    return _macros[index];
}
con's avatar
con committed
90
91
92

Macro *Environment::bind(const Macro &__macro)
{
93
    Q_ASSERT(! __macro.name().isEmpty());
con's avatar
con committed
94
95

    Macro *m = new Macro (__macro);
96
    m->_hashcode = hashCode(m->name());
con's avatar
con committed
97
98
99
100
101
102
103

    if (++_macro_count == _allocated_macros) {
        if (! _allocated_macros)
            _allocated_macros = 401;
        else
            _allocated_macros <<= 1;

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
104
        _macros = reinterpret_cast<Macro **>(realloc(_macros, sizeof(Macro *) * _allocated_macros));
con's avatar
con committed
105
106
107
108
109
110
111
    }

    _macros[_macro_count] = m;

    if (! _hash || _macro_count > (_hash_count >> 1)) {
        rehash();
    } else {
112
113
        const unsigned h = m->_hashcode % _hash_count;
        m->_next = _hash[h];
con's avatar
con committed
114
115
116
117
118
119
        _hash[h] = m;
    }

    return m;
}

120
121
122
123
124
125
126
void Environment::addMacros(const QList<Macro> &macros)
{
    foreach (const Macro &macro, macros) {
        bind(macro);
    }
}

127
Macro *Environment::remove(const ByteArrayRef &name)
con's avatar
con committed
128
129
{
    Macro macro;
130
    macro.setName(name.toByteArray());
131
132
133
    macro.setHidden(true);
    macro.setFileName(currentFile);
    macro.setLine(currentLine);
Roberto Raggi's avatar
Roberto Raggi committed
134
    return bind(macro);
con's avatar
con committed
135
136
}

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
void Environment::reset()
{
    if (_macros) {
        qDeleteAll(firstMacro(), lastMacro());
        free(_macros);
    }

    if (_hash)
        free(_hash);

    _macros = 0;
    _allocated_macros = 0;
    _macro_count = -1;
    _hash = 0;
    _hash_count = 401;
}

154
bool Environment::isBuiltinMacro(const ByteArrayRef &s)
con's avatar
con committed
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
{
    if (s.length() != 8)
        return false;

    if (s[0] == '_') {
        if (s[1] == '_') {
            if (s[2] == 'D') {
                if (s[3] == 'A') {
                    if (s[4] == 'T') {
                        if (s[5] == 'E') {
                            if (s[6] == '_') {
                                if (s[7] == '_') {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            else if (s[2] == 'F') {
                if (s[3] == 'I') {
                    if (s[4] == 'L') {
                        if (s[5] == 'E') {
                            if (s[6] == '_') {
                                if (s[7] == '_') {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            else if (s[2] == 'L') {
                if (s[3] == 'I') {
                    if (s[4] == 'N') {
                        if (s[5] == 'E') {
                            if (s[6] == '_') {
                                if (s[7] == '_') {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            else if (s[2] == 'T') {
                if (s[3] == 'I') {
                    if (s[4] == 'M') {
                        if (s[5] == 'E') {
                            if (s[6] == '_') {
                                if (s[7] == '_') {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
218
219
220
221
222
223
Environment::iterator Environment::firstMacro() const
{ return _macros; }

Environment::iterator Environment::lastMacro() const
{ return _macros + _macro_count + 1; }

224
Macro *Environment::resolve(const ByteArrayRef &name) const
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
{
    if (! _macros)
        return 0;

    Macro *it = _hash[hashCode(name) % _hash_count];
    for (; it; it = it->_next) {
        if (it->name() != name)
            continue;
        else if (it->isHidden())
            return 0;
        else break;
    }
    return it;
}

240
unsigned Environment::hashCode(const QByteArray &s)
con's avatar
con committed
241
242
243
244
245
246
247
248
249
{
    unsigned hash_value = 0;

    for (int i = 0; i < s.size (); ++i)
        hash_value = (hash_value << 5) - hash_value + s.at (i);

    return hash_value;
}

250
unsigned Environment::hashCode(const ByteArrayRef &s)
251
252
253
254
255
256
257
258
259
{
    unsigned hash_value = 0;

    for (int i = 0; i < s.length(); ++i)
        hash_value = (hash_value << 5) - hash_value + s.at(i);

    return hash_value;
}

con's avatar
con committed
260
261
262
263
264
265
266
void Environment::rehash()
{
    if (_hash) {
        free(_hash);
        _hash_count <<= 1;
    }

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
267
    _hash = reinterpret_cast<Macro **>(calloc(_hash_count, sizeof(Macro *)));
con's avatar
con committed
268

Roberto Raggi's avatar
Cleanup    
Roberto Raggi committed
269
270
    for (iterator it = firstMacro(); it != lastMacro(); ++it) {
        Macro *m = *it;
271
272
        const unsigned h = m->_hashcode % _hash_count;
        m->_next = _hash[h];
con's avatar
con committed
273
274
275
        _hash[h] = m;
    }
}
276
277
278
279
280
281
282
283

void Environment::dump() const
{
    for (iterator it = firstMacro(); it != lastMacro(); ++it) {
        Macro *m = *it;
        qDebug() << m->decoratedName();
    }
}