pp-engine.cpp 38.9 KB
Newer Older
1
2
/**************************************************************************
**
con's avatar
con committed
3
4
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 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
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
  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.
*/

#include "pp.h"
50
#include "pp-cctype.h"
hjk's avatar
hjk committed
51

con's avatar
con committed
52
53
#include <Lexer.h>
#include <Token.h>
54
#include <Literals.h>
55
#include <cctype>
56

con's avatar
con committed
57
#include <QtDebug>
58
#include <algorithm>
59
#include <cctype>
con's avatar
con committed
60

Roberto Raggi's avatar
Roberto Raggi committed
61
62
63
64
65
66
namespace CPlusPlus {

struct Value
{
    enum Kind {
        Kind_Long,
67
        Kind_ULong
Roberto Raggi's avatar
Roberto Raggi committed
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
    };

    Kind kind;

    union {
        long l;
        unsigned long ul;
    };


    Value()
        : kind(Kind_Long), l(0)
    { }

    inline bool is_ulong () const
    { return kind == Kind_ULong; }

    inline void set_ulong (unsigned long v)
    {
        ul = v;
        kind = Kind_ULong;
    }

    inline void set_long (long v)
    {
        l = v;
        kind = Kind_Long;
    }

    inline bool is_zero () const
    { return l == 0; }

#define PP_DEFINE_BIN_OP(name, op) \
    inline Value operator op(const Value &other) const \
    { \
        Value v = *this; \
        if (v.is_ulong () || other.is_ulong ()) \
            v.set_ulong (v.ul op other.ul); \
        else \
            v.set_long (v.l op other.l); \
        return v; \
    }

    PP_DEFINE_BIN_OP(op_add, +)
    PP_DEFINE_BIN_OP(op_sub, -)
    PP_DEFINE_BIN_OP(op_mult, *)
    PP_DEFINE_BIN_OP(op_div, /)
    PP_DEFINE_BIN_OP(op_mod, %)
    PP_DEFINE_BIN_OP(op_lhs, <<)
    PP_DEFINE_BIN_OP(op_rhs, >>)
    PP_DEFINE_BIN_OP(op_lt, <)
    PP_DEFINE_BIN_OP(op_gt, >)
    PP_DEFINE_BIN_OP(op_le, <=)
    PP_DEFINE_BIN_OP(op_ge, >=)
    PP_DEFINE_BIN_OP(op_eq, ==)
    PP_DEFINE_BIN_OP(op_ne, !=)
    PP_DEFINE_BIN_OP(op_bit_and, &)
    PP_DEFINE_BIN_OP(op_bit_or, |)
    PP_DEFINE_BIN_OP(op_bit_xor, ^)
    PP_DEFINE_BIN_OP(op_and, &&)
    PP_DEFINE_BIN_OP(op_or, ||)

#undef PP_DEFINE_BIN_OP
};

} // end of namespace CPlusPlus


con's avatar
con committed
136
137
using namespace CPlusPlus;

Roberto Raggi's avatar
Roberto Raggi committed
138

con's avatar
con committed
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
namespace {

class RangeLexer
{
    const Token *first;
    const Token *last;
    Token trivial;

public:
    inline RangeLexer(const Token *first, const Token *last)
        : first(first), last(last)
    {
        // WARN: `last' must be a valid iterator.
        trivial.offset = last->offset;
    }

    inline operator bool() const
    { return first != last; }

    inline bool isValid() const
    { return first != last; }

    inline int size() const
    { return std::distance(first, last); }

    inline const Token *dot() const
    { return first; }

    inline const Token &operator*() const
    {
        if (first != last)
            return *first;

        return trivial;
    }

    inline const Token *operator->() const
    {
        if (first != last)
            return first;

        return &trivial;
    }

    inline RangeLexer &operator++()
    {
        ++first;
        return *this;
    }
};

class ExpressionEvaluator
{
    ExpressionEvaluator(const ExpressionEvaluator &other);
    void operator = (const ExpressionEvaluator &other);

public:
    ExpressionEvaluator(Environment *env)
        : env(env), _lex(0)
    { }

    Value operator()(const Token *firstToken, const Token *lastToken,
                     const QByteArray &source)
    {
        this->source = source;
        const Value previousValue = switchValue(Value());
        RangeLexer tmp(firstToken, lastToken);
        RangeLexer *previousLex = _lex;
        _lex = &tmp;
        process_expression();
        _lex = previousLex;
        return switchValue(previousValue);
    }

protected:
    Value switchValue(const Value &value)
    {
        Value previousValue = _value;
        _value = value;
        return previousValue;
    }

    bool isTokenDefined() const
    {
        if ((*_lex)->isNot(T_IDENTIFIER))
            return false;
        const QByteArray spell = tokenSpell();
        if (spell.size() != 7)
            return false;
        return spell == "defined";
    }

    QByteArray tokenSpell() const
    {
        const QByteArray text = QByteArray::fromRawData(source.constData() + (*_lex)->offset,
234
                                                        (*_lex)->f.length);
con's avatar
con committed
235
236
237
238
239
240
241
242
        return text;
    }

    bool process_expression()
    { return process_constant_expression(); }

    bool process_primary()
    {
243
        if ((*_lex)->is(T_NUMERIC_LITERAL)) {
244
245
246
247
248
249
250
251
252
            int base = 10;
            const QByteArray spell = tokenSpell();
            if (spell.at(0) == '0') {
                if (spell.size() > 1 && (spell.at(1) == 'x' || spell.at(1) == 'X'))
                    base = 16;
                else
                    base = 8;
            }
            _value.set_long(tokenSpell().toLong(0, base));
con's avatar
con committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
            ++(*_lex);
            return true;
        } else if (isTokenDefined()) {
            ++(*_lex);
            if ((*_lex)->is(T_IDENTIFIER)) {
                _value.set_long(env->resolve(tokenSpell()) != 0);
                ++(*_lex);
                return true;
            } else if ((*_lex)->is(T_LPAREN)) {
                ++(*_lex);
                if ((*_lex)->is(T_IDENTIFIER)) {
                    _value.set_long(env->resolve(tokenSpell()) != 0);
                    ++(*_lex);
                    if ((*_lex)->is(T_RPAREN)) {
                        ++(*_lex);
                        return true;
                    }
                }
                return false;
            }
            return true;
        } else if ((*_lex)->is(T_IDENTIFIER)) {
            _value.set_long(0);
            ++(*_lex);
            return true;
        } else if ((*_lex)->is(T_MINUS)) {
            ++(*_lex);
            process_primary();
            _value.set_long(- _value.l);
            return true;
        } else if ((*_lex)->is(T_PLUS)) {
            ++(*_lex);
            process_primary();
            return true;
        } else if ((*_lex)->is(T_EXCLAIM)) {
            ++(*_lex);
            process_primary();
            _value.set_long(_value.is_zero());
            return true;
        } else if ((*_lex)->is(T_LPAREN)) {
            ++(*_lex);
            process_expression();
            if ((*_lex)->is(T_RPAREN))
                ++(*_lex);
            return true;
        }

        return false;
    }

    bool process_multiplicative()
    {
        process_primary();

        while ((*_lex)->is(T_STAR) || (*_lex)->is(T_SLASH) || (*_lex)->is(T_PERCENT)) {
            const Token op = *(*_lex);
            ++(*_lex);

            const Value left = _value;
            process_primary();

            if (op.is(T_STAR)) {
                _value = left * _value;
            } else if (op.is(T_SLASH)) {
                if (_value.is_zero())
                    _value.set_long(0);
                else
                    _value = left / _value;
            } else if (op.is(T_PERCENT)) {
                if (_value.is_zero())
                    _value.set_long(0);
                else
                    _value = left % _value;
            }
        }

        return true;
    }

    bool process_additive()
    {
        process_multiplicative();

        while ((*_lex)->is(T_PLUS) || (*_lex)->is(T_MINUS)) {
            const Token op = *(*_lex);
            ++(*_lex);

            const Value left = _value;
            process_multiplicative();

            if (op.is(T_PLUS))
                _value = left + _value;
            else if (op.is(T_MINUS))
                _value = left - _value;
        }

        return true;
    }

    bool process_shift()
    {
        process_additive();

        while ((*_lex)->is(T_MINUS_MINUS) || (*_lex)->is(T_GREATER_GREATER)) {
            const Token op = *(*_lex);
            ++(*_lex);

            const Value left = _value;
            process_additive();

            if (op.is(T_MINUS_MINUS))
                _value = left << _value;
            else if (op.is(T_GREATER_GREATER))
                _value = left >> _value;
        }

        return true;
    }

    bool process_relational()
    {
        process_shift();

        while ((*_lex)->is(T_LESS)    || (*_lex)->is(T_LESS_EQUAL) ||
               (*_lex)->is(T_GREATER) || (*_lex)->is(T_GREATER_EQUAL)) {
            const Token op = *(*_lex);
            ++(*_lex);

            const Value left = _value;
            process_shift();

            if (op.is(T_LESS))
                _value = left < _value;
            else if (op.is(T_LESS_EQUAL))
                _value = left <= _value;
            else if (op.is(T_GREATER))
                _value = left > _value;
            else if (op.is(T_GREATER_EQUAL))
                _value = left >= _value;
        }

        return true;
    }

    bool process_equality()
    {
        process_relational();

        while ((*_lex)->is(T_EXCLAIM_EQUAL) || (*_lex)->is(T_EQUAL_EQUAL)) {
            const Token op = *(*_lex);
            ++(*_lex);

            const Value left = _value;
            process_relational();

            if (op.is(T_EXCLAIM_EQUAL))
                _value = left != _value;
            else if (op.is(T_EQUAL_EQUAL))
                _value = left == _value;
        }

        return true;
    }

    bool process_and()
    {
        process_equality();

        while ((*_lex)->is(T_AMPER)) {
            const Token op = *(*_lex);
            ++(*_lex);

            const Value left = _value;
            process_equality();

            _value = left & _value;
        }

        return true;
    }

    bool process_xor()
    {
        process_and();

        while ((*_lex)->is(T_CARET)) {
            const Token op = *(*_lex);
            ++(*_lex);

            const Value left = _value;
            process_and();

            _value = left ^ _value;
        }

        return true;
    }

    bool process_or()
    {
        process_xor();

455
        while ((*_lex)->is(T_PIPE)) {
con's avatar
con committed
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
            const Token op = *(*_lex);
            ++(*_lex);

            const Value left = _value;
            process_xor();

            _value = left | _value;
        }

        return true;
    }

    bool process_logical_and()
    {
        process_or();

        while ((*_lex)->is(T_AMPER_AMPER)) {
            const Token op = *(*_lex);
            ++(*_lex);

            const Value left = _value;
            process_or();

            _value = left && _value;
        }

        return true;
    }

    bool process_logical_or()
    {
        process_logical_and();

        while ((*_lex)->is(T_PIPE_PIPE)) {
            const Token op = *(*_lex);
            ++(*_lex);

            const Value left = _value;
            process_logical_and();

            _value = left || _value;
        }

        return true;
    }

    bool process_constant_expression()
    {
        process_logical_or();
        const Value cond = _value;
        if ((*_lex)->is(T_QUESTION)) {
            ++(*_lex);
            process_constant_expression();
            Value left = _value, right;
            if ((*_lex)->is(T_COLON)) {
                ++(*_lex);
                process_constant_expression();
                right = _value;
            }
            _value = ! cond.is_zero() ? left : right;
        }

        return true;
    }

private:
    Environment *env;
    QByteArray source;
    RangeLexer *_lex;
    Value _value;
};

} // end of anonymous namespace


Roberto Raggi's avatar
Roberto Raggi committed
531
Preprocessor::Preprocessor(Client *client, Environment *env)
con's avatar
con committed
532
533
    : client(client),
      env(env),
534
      _expand(env),
535
      _result(0),
536
537
      _markGeneratedTokens(false),
      _expandMacros(true)
con's avatar
con committed
538
539
540
541
{
    resetIfLevel ();
}

542
void Preprocessor::pushState(const State &s)
con's avatar
con committed
543
544
545
546
547
548
549
{
    _savedStates.append(state());
    _source = s.source;
    _tokens = s.tokens;
    _dot = s.dot;
}

550
Preprocessor::State Preprocessor::state() const
con's avatar
con committed
551
552
553
554
555
556
557
558
{
    State state;
    state.source = _source;
    state.tokens = _tokens;
    state.dot = _dot;
    return state;
}

559
void Preprocessor::popState()
con's avatar
con committed
560
561
562
563
564
565
566
567
{
    const State &state = _savedStates.last();
    _source = state.source;
    _tokens = state.tokens;
    _dot = state.dot;
    _savedStates.removeLast();
}

568
569
570
571
572
573
574
575
576
577
578
QByteArray Preprocessor::operator()(const QString &fileName, const QString &source)
{
    const QString previousOriginalSource = _originalSource;
    _originalSource = source;
    const QByteArray bytes = source.toLatin1();
    const QByteArray preprocessedCode = operator()(fileName, bytes);
    _originalSource = previousOriginalSource;
    return preprocessedCode;
}

QByteArray Preprocessor::operator()(const QString &fileName,
579
580
581
                                    const QByteArray &source)
{
    QByteArray preprocessed;
582
    preprocess(fileName, source, &preprocessed);
583
584
585
    return preprocessed;
}

586
587
588
QByteArray Preprocessor::expand(const QByteArray &source)
{
    QByteArray result;
Roberto Raggi's avatar
Roberto Raggi committed
589
    result.reserve(256);
590
591
592
593
    expand(source, &result);
    return result;
}

594
595
void Preprocessor::expand(const QByteArray &source, QByteArray *result)
{
596
597
    if (result)
        _expand(source, result);
598
599
600
601
602
603
604
605
}

void Preprocessor::expand(const char *first, const char *last, QByteArray *result)
{
    const QByteArray source = QByteArray::fromRawData(first, last - first);
    return expand(source, result);
}

606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
void Preprocessor::out(const QByteArray &text)
{
    if (_result)
        _result->append(text);
}

void Preprocessor::out(char ch)
{
    if (_result)
        _result->append(ch);
}

void Preprocessor::out(const char *s)
{
    if (_result)
        _result->append(s);
}

624
625
626
627
628
629
630
631
632
633
bool Preprocessor::expandMacros() const
{
    return _expandMacros;
}

void Preprocessor::setExpandMacros(bool expandMacros)
{
    _expandMacros = expandMacros;
}

634
Preprocessor::State Preprocessor::createStateFromSource(const QByteArray &source) const
con's avatar
con committed
635
636
637
638
639
640
641
642
643
644
645
646
647
648
{
    State state;
    state.source = source;
    Lexer lex(state.source.constBegin(), state.source.constEnd());
    lex.setScanKeywords(false);
    Token tok;
    do {
        lex(&tok);
        state.tokens.append(tok);
    } while (tok.isNot(T_EOF_SYMBOL));
    state.dot = state.tokens.constBegin();
    return state;
}

649
void Preprocessor::processNewline(bool force)
650
{
651
    if (! force && env->currentLine == _dot->lineno)
652
653
        return;

654
    if (force || env->currentLine > _dot->lineno) {
655
656
657
658
        out("\n# ");
        out(QByteArray::number(_dot->lineno));
        out(' ');
        out('"');
659
        out(env->currentFile.toUtf8());
660
661
        out('"');
        out('\n');
662
    } else {
Roberto Raggi's avatar
Roberto Raggi committed
663
        for (unsigned i = env->currentLine; i < _dot->lineno; ++i)
664
            out('\n');
665
666
    }

Roberto Raggi's avatar
Roberto Raggi committed
667
    env->currentLine = _dot->lineno;
668
669
670
671
672
673
674
675
676
677
678
679
}

void Preprocessor::processSkippingBlocks(bool skippingBlocks,
                                         TokenIterator start, TokenIterator /*end*/)
{
    if (! client)
        return;

    if (skippingBlocks != _skipping[iflevel]) {
        unsigned offset = start->offset;

        if (_skipping[iflevel]) {
680
            if (_dot->f.newline)
681
682
683
684
685
686
687
688
689
690
691
692
693
                ++offset;

            client->startSkippingBlocks(offset);

        } else {
            if (offset)
                --offset;

            client->stopSkippingBlocks(offset);
        }
    }
}

694
695
696
697
698
699
700
701
702
703
704
bool Preprocessor::markGeneratedTokens(bool markGeneratedTokens,
                                       TokenIterator dot)
{
    bool previous = _markGeneratedTokens;
    _markGeneratedTokens = markGeneratedTokens;

    if (previous != _markGeneratedTokens) {
        if (! dot)
            dot = _dot;

        if (_markGeneratedTokens)
705
            out("\n#gen true");
706
        else
707
            out("\n#gen false");
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726

        processNewline(/*force = */ true);

        const char *begin = _source.constBegin();
        const char *end   = begin;

        if (markGeneratedTokens)
            end += dot->begin();
        else
            end += (dot - 1)->end();

        const char *it = end - 1;
        for (; it != begin - 1; --it) {
            if (*it == '\n')
                break;
        }
        ++it;

        for (; it != end; ++it) {
727
            if (! pp_isspace(*it))
728
                out(' ');
729
730

            else
731
                out(*it);
732
733
734
735
736
737
        }
    }

    return previous;
}

738
void Preprocessor::preprocess(const QString &fileName, const QByteArray &source,
Roberto Raggi's avatar
Roberto Raggi committed
739
                              QByteArray *result)
con's avatar
con committed
740
{
741
742
743
    QByteArray *previousResult = _result;
    _result = result;

con's avatar
con committed
744
745
    pushState(createStateFromSource(source));

746
    const QString previousFileName = env->currentFile;
Roberto Raggi's avatar
Roberto Raggi committed
747
    env->currentFile = fileName;
Roberto Raggi's avatar
Roberto Raggi committed
748

Roberto Raggi's avatar
Roberto Raggi committed
749
750
    const unsigned previousCurrentLine = env->currentLine;
    env->currentLine = 0;
con's avatar
con committed
751
752

    while (true) {
Roberto Raggi's avatar
Roberto Raggi committed
753

754
        if (_dot->f.joined)
Roberto Raggi's avatar
Roberto Raggi committed
755
756
            out("\\");

757
        processNewline();
con's avatar
con committed
758
759
760

        if (_dot->is(T_EOF_SYMBOL)) {
            break;
761

762
        } else if (_dot->is(T_POUND) && (! _dot->f.joined && _dot->f.newline)) {
763
764
            // handle the preprocessor directive

con's avatar
con committed
765
766
767
            TokenIterator start = _dot;
            do {
                ++_dot;
768
            } while (_dot->isNot(T_EOF_SYMBOL) && (_dot->f.joined || ! _dot->f.newline));
con's avatar
con committed
769
770
771
772

            const bool skippingBlocks = _skipping[iflevel];

            processDirective(start, _dot);
773
            processSkippingBlocks(skippingBlocks, start, _dot);
con's avatar
con committed
774
775

        } else if (skipping()) {
776
777
            // skip the current line

con's avatar
con committed
778
779
            do {
                ++_dot;
780
            } while (_dot->isNot(T_EOF_SYMBOL) && (_dot->f.joined || ! _dot->f.newline));
781

con's avatar
con committed
782
        } else {
783

784
            if (_dot->f.whitespace) {
785
786
787
788
789
                unsigned endOfPreviousToken = 0;

                if (_dot != _tokens.constBegin())
                    endOfPreviousToken = (_dot - 1)->end();

790
791
                const unsigned beginOfToken = _dot->begin();

Roberto Raggi's avatar
Roberto Raggi committed
792
                const char *start = _source.constBegin() + endOfPreviousToken;
793
794
                const char *end = _source.constBegin() + beginOfToken;

Roberto Raggi's avatar
Roberto Raggi committed
795
796
797
798
799
800
801
                const char *it = end - 1;
                for (; it != start - 1; --it) {
                    if (*it == '\n')
                        break;
                }
                ++it;

802
                for (; it != end; ++it) {
803
                    if (pp_isspace(*it))
804
                        out(*it);
805
806

                    else
807
                        out(' ');
808
809
                }
            }
con's avatar
con committed
810
811

            if (_dot->isNot(T_IDENTIFIER)) {
812
                out(tokenSpell(*_dot));
con's avatar
con committed
813
                ++_dot;
814

con's avatar
con committed
815
816
817
818
819
            } else {
                const TokenIterator identifierToken = _dot;
                ++_dot; // skip T_IDENTIFIER

                const QByteArray spell = tokenSpell(*identifierToken);
820
                if (! _expandMacros) {
821
822
823
824
825
826
827
828
829
830
831
                    if (! env->isBuiltinMacro(spell)) {
                        Macro *m = env->resolve(spell);
                        if (m && ! m->isFunctionLike()) {
                            QByteArray expandedDefinition;
                            expandObjectLikeMacro(identifierToken, spell, m, &expandedDefinition);
                            if (expandedDefinition.trimmed().isEmpty()) {
                                out(QByteArray(spell.length(), ' '));
                                continue;
                            }
                        }
                    }
832
833
834
                    out(spell);
                    continue;
                }
Roberto Raggi's avatar
Roberto Raggi committed
835

836
                else if (env->isBuiltinMacro(spell))
837
                    expandBuiltinMacro(identifierToken, spell);
838

839
840
841
842
843
                else {
                    if (Macro *m = env->resolve(spell)) {
                        if (! m->isFunctionLike()) {
                            if (0 == (m = processObjectLikeMacro(identifierToken, spell, m)))
                                continue;
844

845
846
847
                            // the macro expansion generated something that looks like
                            // a function-like macro.
                        }
Roberto Raggi's avatar
Roberto Raggi committed
848

849
850
                        // `m' is function-like macro.
                        if (_dot->is(T_LPAREN)) {
851
852
                            QVector<MacroArgumentReference> actuals;
                            collectActualArguments(&actuals);
853

854
                            if (_dot->is(T_RPAREN)) {
855
                                expandFunctionLikeMacro(identifierToken, m, actuals);
856
857
                                continue;
                            }
858
859
860
                        }
                    }

861
                    // it's not a function or object-like macro.
862
                    out(spell);
863
                }
con's avatar
con committed
864
865
866
867
868
            }
        }
    }

    popState();
Roberto Raggi's avatar
Roberto Raggi committed
869

Roberto Raggi's avatar
Roberto Raggi committed
870
871
    env->currentFile = previousFileName;
    env->currentLine = previousCurrentLine;
872
    _result = previousResult;
con's avatar
con committed
873
874
}

875
void Preprocessor::collectActualArguments(QVector<MacroArgumentReference> *actuals)
876
{
877
878
    if (_dot->isNot(T_LPAREN))
        return;
879

880
    ++_dot;
881

882
883
884
885
    if (_dot->is(T_RPAREN))
        return;

    actuals->append(collectOneActualArgument());
886

887
    while (_dot->is(T_COMMA)) {
888
        ++_dot;
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919

        actuals->append(collectOneActualArgument());
    }
}

MacroArgumentReference Preprocessor::collectOneActualArgument()
{
    const unsigned position = _dot->begin();

    while (_dot->isNot(T_EOF_SYMBOL)) {
        if (_dot->is(T_COMMA) || _dot->is(T_RPAREN))
            break;

        if (_dot->isNot(T_LPAREN))
            ++_dot;

        else {
            int count = 0;

            for (; _dot->isNot(T_EOF_SYMBOL); ++_dot) {
                if (_dot->is(T_LPAREN))
                    ++count;

                else if (_dot->is(T_RPAREN)) {
                    if (! --count) {
                        ++_dot;
                        break;
                    }
                }
            }
        }
920
    }
921
922
923
924

    const unsigned end = _dot->begin();

    return MacroArgumentReference(position, end - position);
925
926
}

927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
Macro *Preprocessor::processObjectLikeMacro(TokenIterator identifierToken,
                                            const QByteArray &spell,
                                            Macro *m)
{
    QByteArray tmp;
    expandObjectLikeMacro(identifierToken, spell, m, &tmp);

    if (_dot->is(T_LPAREN)) {
        // check if the expension generated a function-like macro.

        m = 0; // reset the active the macro

        pushState(createStateFromSource(tmp));

        if (_dot->is(T_IDENTIFIER)) {
            const QByteArray id = tokenSpell(*_dot);

            if (Macro *macro = env->resolve(id)) {
                if (macro->isFunctionLike())
                    m = macro;
            }
        }

        popState();

        if (m != 0)
            return m;
    }

956
    const bool was = markGeneratedTokens(true, identifierToken);
957
    out(tmp);
958
    (void) markGeneratedTokens(was);
959
960
961
    return 0;
}

962
963
964
965
966
967
968
969
970
void Preprocessor::expandBuiltinMacro(TokenIterator identifierToken,
                                      const QByteArray &spell)
{
    const Macro trivial;

    if (client)
        client->startExpandingMacro(identifierToken->offset,
                                    trivial, spell);

971
    const bool was = markGeneratedTokens(true, identifierToken);
972
    expand(spell, _result);
973
    (void) markGeneratedTokens(was);
974
975
976
977
978

    if (client)
        client->stopExpandingMacro(_dot->offset, trivial);
}

979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
void Preprocessor::expandObjectLikeMacro(TokenIterator identifierToken,
                                         const QByteArray &spell,
                                         Macro *m,
                                         QByteArray *result)
{
    if (client)
        client->startExpandingMacro(identifierToken->offset,
                                    *m, spell);

    m->setHidden(true);
    expand(m->definition(), result);
    m->setHidden(false);

    if (client)
        client->stopExpandingMacro(_dot->offset, *m);
}

996
997
998
void Preprocessor::expandFunctionLikeMacro(TokenIterator identifierToken,
                                           Macro *m,
                                           const QVector<MacroArgumentReference> &actuals)
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
{
    const char *beginOfText = startOfToken(*identifierToken);
    const char *endOfText = endOfToken(*_dot);
    ++_dot; // skip T_RPAREN

    if (client) {
        const QByteArray text =
                QByteArray::fromRawData(beginOfText,
                                        endOfText - beginOfText);

        client->startExpandingMacro(identifierToken->offset,
1010
                                    *m, text, actuals);
1011
1012
    }

1013
    const bool was = markGeneratedTokens(true, identifierToken);
1014
    expand(beginOfText, endOfText, _result);
1015
    (void) markGeneratedTokens(was);
1016
1017
1018
1019
1020

    if (client)
        client->stopExpandingMacro(_dot->offset, *m);
}

1021
const char *Preprocessor::startOfToken(const Token &token) const
con's avatar
con committed
1022
1023
{ return _source.constBegin() + token.begin(); }

1024
const char *Preprocessor::endOfToken(const Token &token) const
con's avatar
con committed
1025
1026
{ return _source.constBegin() + token.end(); }

1027
QByteArray Preprocessor::tokenSpell(const Token &token) const
con's avatar
con committed
1028
1029
{
    const QByteArray text = QByteArray::fromRawData(_source.constBegin() + token.offset,
1030
                                                     token.f.length);
con's avatar
con committed
1031
1032
1033
    return text;
}

1034
QByteArray Preprocessor::tokenText(const Token &token) const
con's avatar
con committed
1035
1036
{
    const QByteArray text(_source.constBegin() + token.offset,
1037
                          token.f.length);
con's avatar
con committed
1038
1039
1040
    return text;
}

1041
void Preprocessor::processDirective(TokenIterator firstToken, TokenIterator lastToken)
con's avatar
con committed
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
{
    RangeLexer tk(firstToken, lastToken);
    ++tk; // skip T_POUND

    if (tk->is(T_IDENTIFIER)) {
        const QByteArray directive = tokenSpell(*tk);
        switch (PP_DIRECTIVE_TYPE d = classifyDirective(directive)) {
        case PP_DEFINE:
            if (! skipping())
                processDefine(firstToken, lastToken);
            break;

        case PP_INCLUDE:
        case PP_INCLUDE_NEXT:
1056
        case PP_IMPORT:
con's avatar
con committed
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
            if (! skipping())
                processInclude(d == PP_INCLUDE_NEXT, firstToken, lastToken);
            break;

        case PP_UNDEF:
            if (! skipping())
                processUndef(firstToken, lastToken);
            break;

        case PP_ELIF:
            processElif(firstToken, lastToken);
            break;

        case PP_ELSE:
            processElse(firstToken, lastToken);
            break;

        case PP_ENDIF:
            processEndif(firstToken, lastToken);
            break;

        case PP_IF:
            processIf(firstToken, lastToken);
            break;

        case PP_IFDEF:
        case PP_IFNDEF:
            processIfdef(d == PP_IFNDEF, firstToken, lastToken);
            break;

        default:
            break;
        } // switch
    }
}

1093
QVector<Token> Preprocessor::tokenize(const QByteArray &text) const
con's avatar
con committed
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
{
    QVector<Token> tokens;
    Lexer lex(text.constBegin(), text.constEnd());
    lex.setScanKeywords(false);
    Token tk;
    do {
        lex(&tk);
        tokens.append(tk);
    } while (tk.isNot(T_EOF_SYMBOL));
    return tokens;
}

1106
1107
void Preprocessor::processInclude(bool, TokenIterator firstToken,
                                  TokenIterator lastToken, bool acceptMacros)
con's avatar
con committed
1108
{
1109
1110
1111
    if (! client)
        return; // nothing to do.

con's avatar
con committed
1112
1113
1114
1115
1116
    RangeLexer tk(firstToken, lastToken);
    ++tk; // skip T_POUND
    ++tk; // skip `include|nclude_next'

    if (acceptMacros && tk->is(T_IDENTIFIER)) {
Roberto Raggi's avatar
Roberto Raggi committed
1117
        // ### TODO: implement me
con's avatar
con committed
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
#if 0
        QByteArray name;
        name.reserve(256);
        MacroExpander expandInclude(env);
        expandInclude(startOfToken(tokens.at(2)),
                      startOfToken(tokens.last()),
                      &name);
        const QByteArray previousSource = switchSource(name);
        //processInclude(skipCurentPath, tokenize(name), /*accept macros=*/ false);
        (void) switchSource(previousSource);
#endif
1129

con's avatar
con committed
1130
    } else if (tk->is(T_LESS)) {
1131

con's avatar
con committed
1132
        TokenIterator start = tk.dot();
1133

con's avatar
con committed
1134
1135
1136
1137
        for (; tk->isNot(T_EOF_SYMBOL); ++tk) {
            if (tk->is(T_GREATER))
                break;
        }
1138

con's avatar
con committed
1139
1140
1141
        const char *beginOfPath = endOfToken(*start);
        const char *endOfPath = startOfToken(*tk);

1142
        QString fn = string(beginOfPath, endOfPath - beginOfPath);
1143
        client->sourceNeeded(fn, Client::IncludeGlobal, firstToken->lineno);
con's avatar
con committed
1144
1145

    } else if (tk->is(T_ANGLE_STRING_LITERAL) || tk->is(T_STRING_LITERAL)) {
1146

con's avatar
con committed
1147
1148
1149
1150
        const QByteArray spell = tokenSpell(*tk);
        const char *beginOfPath = spell.constBegin();
        const char *endOfPath = spell.constEnd();
        const char quote = *beginOfPath;
1151

con's avatar
con committed
1152
        if (beginOfPath + 1 != endOfPath && ((quote == '"' && endOfPath[-1] == '"') ||
1153
                                              (quote == '<' && endOfPath[-1] == '>'))) {
con's avatar
con committed
1154

1155
            QString fn = string(beginOfPath + 1, spell.length() - 2);
1156
            client->sourceNeeded(fn, Client::IncludeLocal, firstToken->lineno);
con's avatar
con committed
1157
1158
1159
1160
        }
    }
}

1161
void Preprocessor::processDefine(TokenIterator firstToken, TokenIterator lastToken)
con's avatar
con committed
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
{
    RangeLexer tk(firstToken, lastToken);

    if (tk.size() < 3)
        return; // nothing to do

    ++tk; // skip T_POUND
    ++tk; // skip T_DEFINE

    if (tk->isNot(T_IDENTIFIER)) {
        // ### warning expected an `identifier'
        return;
    }

    Macro macro;
Roberto Raggi's avatar
Roberto Raggi committed
1177
1178
    macro.setFileName(env->currentFile);
    macro.setLine(env->currentLine);
1179
    macro.setName(tokenText(*tk));
con's avatar
con committed
1180
1181
    ++tk; // skip T_IDENTIFIER

1182
    if (tk->is(T_LPAREN) && ! tk->f.whitespace) {
con's avatar
con committed
1183
        // a function-like macro definition
1184
        macro.setFunctionLike(true);
con's avatar
con committed
1185
1186
1187

        ++tk; // skip T_LPAREN
        if (tk->is(T_IDENTIFIER)) {
1188
            macro.addFormal(tokenText(*tk));
con's avatar
con committed
1189
1190
1191
1192
1193
            ++tk; // skip T_IDENTIFIER
            while (tk->is(T_COMMA)) {
                ++tk;// skip T_COMMA
                if (tk->isNot(T_IDENTIFIER))
                    break;
1194
                macro.addFormal(tokenText(*tk));
con's avatar
con committed
1195
1196
1197
1198
1199
                ++tk; // skip T_IDENTIFIER
            }
        }

        if (tk->is(T_DOT_DOT_DOT)) {
1200
            macro.setVariadic(true);
con's avatar
con committed
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
            ++tk; // skip T_DOT_DOT_DOT
        }

        if (tk->isNot(T_RPAREN)) {
            // ### warning expected `)'
            return;
        }

        ++tk; // skip T_RPAREN
    }

1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
    if (isQtReservedWord(macro.name())) {
        QByteArray macroId = macro.name();

        if (macro.isFunctionLike()) {
            macroId += '(';
            bool fst = true;
            foreach (const QByteArray formal, macro.formals()) {
                if (! fst)
                    macroId += ", ";
                fst = false;
                macroId += formal;
            }
            macroId += ')';
con's avatar
con committed
1225
1226
        }

1227
1228
        macro.setDefinition(macroId);
    } else {
1229
        // ### make me fast!
con's avatar
con committed
1230
        const char *startOfDefinition = startOfToken(*tk);
1231
        const char *endOfDefinition = endOfToken(lastToken[- 1]);
1232
1233
1234
1235
1236
        QByteArray definition(startOfDefinition,
                              endOfDefinition - startOfDefinition);
        definition.replace("\\\n", " ");
        definition.replace('\n', ' ');
        macro.setDefinition(definition.trimmed());
con's avatar
con committed
1237
1238
    }

Roberto Raggi's avatar
Roberto Raggi committed
1239
    env->bind(macro);
con's avatar
con committed
1240

Roberto Raggi's avatar
Roberto Raggi committed
1241
1242
    if (client)
        client->macroAdded(macro);
con's avatar
con committed
1243
1244
}

1245
void Preprocessor::processIf(TokenIterator firstToken, TokenIterator lastToken)
con's avatar
con committed
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
{
    RangeLexer tk(firstToken, lastToken);

    ++tk; // skip T_POUND
    ++tk; // skipt `if'

    if (testIfLevel()) {
        const char *first = startOfToken(*tk);
        const char *last = startOfToken(*lastToken);

        MacroExpander expandCondition (env);
        QByteArray condition;
        condition.reserve(256);
        expandCondition(first, last, &condition);

        QVector<Token> tokens = tokenize(condition);

        const Value result = evalExpression(tokens.constBegin(),
                                            tokens.constEnd() - 1,
                                            condition);

        _true_test[iflevel] = ! result.is_zero ();
        _skipping[iflevel]  =   result.is_zero ();
    }
}

1272
void Preprocessor::processElse(TokenIterator firstToken, TokenIterator lastToken)
con's avatar
con committed
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
{
    RangeLexer tk(firstToken, lastToken);

    if (iflevel == 0 && !skipping ()) {
        // std::cerr << "*** WARNING #else without #if" << std::endl;
    } else if (iflevel > 0 && _skipping[iflevel - 1]) {
        _skipping[iflevel] = true;
    } else {
        _skipping[iflevel] = _true_test[iflevel];
    }
}

1285
void Preprocessor::processElif(TokenIterator firstToken, TokenIterator lastToken)
con's avatar
con committed
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
{
    RangeLexer tk(firstToken, lastToken);
    ++tk; // skip T_POUND
    ++tk; // skipt `elif'

    if (! (iflevel > 0)) {
        // std::cerr << "*** WARNING: " << __FILE__ << __LINE__ << std::endl;
    } else if (iflevel == 0 && !skipping()) {
        // std::cerr << "*** WARNING #else without #if" << std::endl;
    } else if (!_true_test[iflevel] && !_skipping[iflevel - 1]) {
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310

        const char *first = startOfToken(*tk);
        const char *last = startOfToken(*lastToken);

        MacroExpander expandCondition (env);
        QByteArray condition;
        condition.reserve(256);
        expandCondition(first, last, &condition);

        QVector<Token> tokens = tokenize(condition);

        const Value result = evalExpression(tokens.constBegin(),
                                            tokens.constEnd() - 1,
                                            condition);

con's avatar
con committed
1311
1312
1313
1314
1315
1316
1317
        _true_test[iflevel] = ! result.is_zero ();
        _skipping[iflevel]  =   result.is_zero ();
    } else {
        _skipping[iflevel] = true;
    }
}

1318
void Preprocessor::processEndif(TokenIterator, TokenIterator)
con's avatar
con committed
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
{
    if (iflevel == 0 && !skipping()) {
        // std::cerr << "*** WARNING #endif without #if" << std::endl;
    } else {
        _skipping[iflevel] = false;
        _true_test[iflevel] = false;

        --iflevel;
    }
}

1330
void Preprocessor::processIfdef(bool checkUndefined,
1331
1332
                                TokenIterator firstToken,
                                TokenIterator lastToken)
con's avatar
con committed
1333
1334
1335
1336
1337
1338
1339
1340
{
    RangeLexer tk(firstToken, lastToken);

    ++tk; // skip T_POUND
    ++tk; // skip `ifdef'
    if (testIfLevel()) {
        if (tk->is(T_IDENTIFIER)) {
            const QByteArray macroName = tokenSpell(*tk);
Roberto Raggi's avatar
Roberto Raggi committed
1341
            bool value = env->resolve(macroName) != 0 || env->isBuiltinMacro(macroName);
con's avatar
con committed
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351

            if (checkUndefined)
                value = ! value;

            _true_test[iflevel] =   value;
            _skipping [iflevel] = ! value;
        }
    }
}

1352
void Preprocessor::processUndef(TokenIterator firstToken, TokenIterator lastToken)
con's avatar
con committed
1353
1354
1355
1356
1357
1358
1359
1360
{
    RangeLexer tk(firstToken, lastToken);

    ++tk; // skip T_POUND
    ++tk; // skip `undef'

    if (tk->is(T_IDENTIFIER)) {
        const QByteArray macroName = tokenText(*tk);
Roberto Raggi's avatar
Roberto Raggi committed
1361
        const Macro *macro = env->remove(macroName);
con's avatar
con committed
1362

Roberto Raggi's avatar
Roberto Raggi committed
1363
1364
        if (client && macro)
            client->macroAdded(*macro);
con's avatar
con committed
1365
1366
1367
    }
}

1368
void Preprocessor::resetIfLevel ()
con's avatar
con committed
1369
1370
1371
1372
1373
1374
{
    iflevel = 0;
    _skipping[iflevel] = false;
    _true_test[iflevel] = false;
}

1375
Preprocessor::PP_DIRECTIVE_TYPE Preprocessor::classifyDirective(const QByteArray &directive) const
con's avatar
con committed
1376
{
1377
    switch (directive.size())
con's avatar
con committed
1378
1379
    {
    case 2:
1380
        if (directive[0] == 'i' && directive[1] == 'f')
con's avatar
con committed
1381
1382
1383
1384
            return PP_IF;
        break;

    case 4:
1385
        if (directive[0] == 'e' && directive == "elif")
con's avatar
con committed
1386
            return PP_ELIF;
1387
        else if (directive[0] == 'e' && directive == "else")
con's avatar
con committed
1388
1389
1390
1391
            return PP_ELSE;
        break;

    case 5:
1392
        if (directive[0] == 'i' && directive == "ifdef")
con's avatar
con committed
1393
            return PP_IFDEF;
1394
        else if (directive[0] == 'u' && directive == "undef")
con's avatar
con committed
1395
            return PP_UNDEF;
1396
        else if (directive[0] == 'e' && directive == "endif")
con's avatar
con committed
1397
1398
1399
1400
            return PP_ENDIF;
        break;

    case 6:
1401
        if (directive[0] == 'i' && directive == "ifndef")
con's avatar
con committed
1402
            return PP_IFNDEF;
1403
        else if (directive[0] == 'i' && directive == "import")
1404
            return PP_IMPORT;
1405
        else if (directive[0] == 'd' && directive == "define")
con's avatar
con committed
1406
1407
1408
1409
            return PP_DEFINE;
        break;

    case 7:
1410
        if (directive[0] == 'i' && directive == "include")
con's avatar
con committed
1411
1412
1413
1414
            return PP_INCLUDE;
        break;

    case 12:
1415
        if (directive[0] == 'i' && directive == "include_next")
con's avatar
con committed
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
            return PP_INCLUDE_NEXT;
        break;

    default:
        break;
    }

    return PP_UNKNOWN_DIRECTIVE;
}

1426
bool Preprocessor::testIfLevel()
con's avatar
con committed
1427
1428
1429
1430
1431
1432
1433
{
    const bool result = !_skipping[iflevel++];
    _skipping[iflevel] = _skipping[iflevel - 1];
    _true_test[iflevel] = false;
    return result;
}

1434
int Preprocessor::skipping() const
con's avatar
con committed
1435
1436
{ return _skipping[iflevel]; }

1437
Value Preprocessor::evalExpression(TokenIterator firstToken, TokenIterator lastToken,
1438
                                   const QByteArray &source) const
con's avatar
con committed
1439
{
Roberto Raggi's avatar
Roberto Raggi committed
1440
    ExpressionEvaluator eval(env);
con's avatar
con committed
1441
1442
1443
1444
    const Value result = eval(firstToken, lastToken, source);
    return result;
}

1445
bool Preprocessor::isQtReservedWord(const QByteArray &macroId) const
con's avatar
con committed
1446
1447
1448
1449
{
    const int size = macroId.size();
    if      (size == 9 && macroId.at(0) == 'Q' && macroId == "Q_SIGNALS")
        return true;
1450
1451
    else if (size == 9 && macroId.at(0) == 'Q' && macroId == "Q_FOREACH")
        return true;
con's avatar
con committed
1452
1453
    else if (size == 7 && macroId.at(0) == 'Q' && macroId == "Q_SLOTS")
        return true;
1454
1455
1456
1457
    else if (size == 8 && macroId.at(0) == 'Q' && macroId == "Q_SIGNAL")
        return true;
    else if (size == 6 && macroId.at(0) == 'Q' && macroId == "Q_SLOT")
        return true;
con's avatar
con committed
1458
1459
1460
1461
1462
1463
    else if (size == 6 && macroId.at(0) == 'S' && macroId == "SIGNAL")
        return true;
    else if (size == 4 && macroId.at(0) == 'S' && macroId == "SLOT")
        return true;
    else if (size == 7 && macroId.at(0) == 's' && macroId == "signals")
        return true;
1464
1465
    else if (size == 7 && macroId.at(0) == 'f' && macroId == "foreach")
        return true;
con's avatar
con committed
1466
1467
1468
1469
    else if (size == 5 && macroId.at(0) == 's' && macroId == "slots")
        return true;
    return false;
}
1470
1471
1472
1473
1474
1475
1476
1477
1478

QString Preprocessor::string(const char *first, int length) const
{
    if (_originalSource.isEmpty())
        return QString::fromUtf8(first, length);

    const int position = first - _source.constData();
    return _originalSource.mid(position, length);
}