tst_preprocessor.cpp 29.7 KB
Newer Older
Bill King's avatar
Bill King committed
1
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).
Bill King's avatar
Bill King committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
Bill King's avatar
Bill King committed
8
9
10
11
**
**
** GNU Lesser General Public License Usage
**
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.
Bill King's avatar
Bill King committed
18
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
Bill King's avatar
Bill King 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.
**
Bill King's avatar
Bill King committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
Bill King's avatar
Bill King committed
30
31
32
**
**************************************************************************/

33
34
35
#include <QtTest>
#include <pp.h>

36
//TESTED_COMPONENT=src/libs/cplusplus
37
using namespace CPlusPlus;
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
#define DUMP_OUTPUT(x)     {QByteArray b(x);qDebug("output: [[%s]]", b.replace("\n", "<<\n").constData());}


QByteArray loadSource(const QString &fileName)
{
    QFile inf(fileName);
    if (!inf.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug("Cannot open \"%s\"", fileName.toUtf8().constData());
        return QByteArray();
    }

    QTextStream ins(&inf);
    QString source = ins.readAll();
    inf.close();
    return source.toUtf8();
}

void saveData(const QByteArray &data, const QString &fileName)
{
    QFile inf(fileName);
    if (!inf.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qDebug("Cannot open \"%s\"", fileName.toUtf8().constData());
        return;
    }

    inf.write(data);
    inf.close();
}

Erik Verbruggen's avatar
Erik Verbruggen committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
struct Include
{
    Include(const QString &fileName, Client::IncludeType type, unsigned line)
        : fileName(fileName), type(type), line(line)
    {}

    QString fileName;
    Client::IncludeType type;
    unsigned line;
};
QDebug &operator<<(QDebug& d, const Include &i) {
    d << '[' << i.fileName
      << ',' << (i.type == Client::IncludeGlobal ? "Global" : (i.type == Client::IncludeLocal ? "Local" : "Unknown"))
      << ',' << i.line
      << ']';
    return d;
}

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class MockClient: public Client
{
public:
    struct Block {
        Block(): start(0), end(0) {}
        Block(unsigned start): start(start), end(0) {}

        unsigned start;
        unsigned end;
    };

public:
    MockClient(Environment *env, QByteArray *output)
        : m_env(env)
        , m_output(output)
        , m_pp(this, env)
        , m_includeDepth(0)
    {}

    virtual ~MockClient() {}

107
108
109
110
111
    virtual void macroAdded(const Macro & macro)
    {
        m_definedMacros.append(macro.name());
        m_definedMacrosLine.append(macro.line());
    }
112
113

    virtual void passedMacroDefinitionCheck(unsigned /*offset*/, const Macro &/*macro*/) {}
114
    virtual void failedMacroDefinitionCheck(unsigned /*offset*/, const ByteArrayRef &/*name*/) {}
115

116
    virtual void startExpandingMacro(unsigned offset,
117
                                     const Macro &/*macro*/,
118
                                     const ByteArrayRef &originalText,
119
                                     const QVector<MacroArgumentReference> &/*actuals*/
120
                                              = QVector<MacroArgumentReference>())
121
    {
122
        m_expandedMacros.append(QByteArray(originalText.start(), originalText.length()));
123
124
        m_expandedMacrosOffset.append(offset);
    }
125
126
127
128
129
130
131
132
133
134
135

    virtual void stopExpandingMacro(unsigned /*offset*/,
                                    const Macro &/*macro*/) {}

    virtual void startSkippingBlocks(unsigned offset)
    { m_skippedBlocks.append(Block(offset)); }

    virtual void stopSkippingBlocks(unsigned offset)
    { m_skippedBlocks.last().end = offset; }

    virtual void sourceNeeded(QString &includedFileName, IncludeType mode,
Erik Verbruggen's avatar
Erik Verbruggen committed
136
                              unsigned line)
137
    {
Erik Verbruggen's avatar
Erik Verbruggen committed
138
139
140
141
142
#if 1
        m_recordedIncludes.append(Include(includedFileName, mode, line));
#else
        Q_UNUSED(line);

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
        QString resolvedFileName;
        if (mode == IncludeLocal)
            resolvedFileName = resolveLocally(m_env->currentFile, includedFileName);
        else
            resolvedFileName = resolveGlobally(includedFileName);

    //    qDebug("resolved [[%s]] to [[%s]] from [[%s]] (%s)\n",
    //           includedFileName.toUtf8().constData(),
    //           resolvedFileName.toUtf8().constData(),
    //           currentFileName.toUtf8().constData(),
    //           (mode == IncludeLocal) ? "locally" : "globally");

        if (resolvedFileName.isEmpty())
            return;

        ++m_includeDepth;
        //    qDebug("%5d %s %s", m_includeDepth, QByteArray(m_includeDepth, '+').constData(), resolvedFileName.toUtf8().constData());
        sourceNeeded(resolvedFileName);
        --m_includeDepth;
Erik Verbruggen's avatar
Erik Verbruggen committed
162
#endif
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
    }

    QString resolveLocally(const QString &currentFileName,
                                         const QString &includedFileName) const
    {
        QDir dir;
        if (currentFileName.isEmpty())
            dir = QDir::current();
        else
            dir = QFileInfo(currentFileName).dir();
        const QFileInfo inc(dir, includedFileName);
        if (inc.exists()) {
            const QString resolved = inc.filePath();
            return resolved.toUtf8().constData();
        } else {
    //        std::cerr<<"Cannot find " << inc.fileName().toUtf8().constData()<<std::endl;
            return QString();
        }
    }

    QString resolveGlobally(const QString &currentFileName) const
    {
        foreach (const QDir &dir, m_includePaths) {
            QFileInfo f(dir, currentFileName);
            if (f.exists())
                return f.filePath();
        }

        return QString();
    }

    void setIncludePaths(const QStringList &includePaths)
    {
        foreach (const QString &path, includePaths) {
            QDir dir(path);
            if (dir.exists())
                m_includePaths.append(dir);
        }
    }

203
    void sourceNeeded(const QString &fileName, bool nolines)
204
205
206
    {
        QByteArray src = loadSource(fileName);
        QVERIFY(!src.isEmpty());
207
        *m_output = m_pp(fileName, src, nolines, true);
208
209
210
211
212
    }

    QList<Block> skippedBlocks() const
    { return m_skippedBlocks; }

Erik Verbruggen's avatar
Erik Verbruggen committed
213
214
215
    QList<Include> recordedIncludes() const
    { return m_recordedIncludes; }

216
217
218
    QList<QByteArray> expandedMacros() const
    { return m_expandedMacros; }

219
220
221
    QList<unsigned> expandedMacrosOffset() const
    { return m_expandedMacrosOffset; }

222
223
224
225
226
227
    QList<QByteArray> definedMacros() const
    { return m_definedMacros; }

    QList<unsigned> definedMacrosLine() const
    { return m_definedMacrosLine; }

228
229
230
231
232
233
234
private:
    Environment *m_env;
    QByteArray *m_output;
    Preprocessor m_pp;
    QList<QDir> m_includePaths;
    unsigned m_includeDepth;
    QList<Block> m_skippedBlocks;
Erik Verbruggen's avatar
Erik Verbruggen committed
235
    QList<Include> m_recordedIncludes;
236
    QList<QByteArray> m_expandedMacros;
237
    QList<unsigned> m_expandedMacrosOffset;
238
239
    QList<QByteArray> m_definedMacros;
    QList<unsigned> m_definedMacrosLine;
240
241
};

hjk's avatar
hjk committed
242
QT_BEGIN_NAMESPACE
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
namespace QTest {
    template<> char *toString(const QList<unsigned> &list)
    {
        QByteArray ba = "QList<unsigned>(";
        foreach (const unsigned& item, list) {
            ba += QTest::toString(item);
            ba += ',';
        }
        if (!list.isEmpty())
            ba[ba.size() - 1] = ')';
        return qstrdup(ba.data());
    }
    template<> char *toString(const QList<QByteArray> &list)
    {
        QByteArray ba = "QList<QByteArray>(";
        foreach (const QByteArray& item, list) {
            ba += QTest::toString(item);
            ba += ',';
        }
        if (!list.isEmpty())
            ba[ba.size() - 1] = ')';
        return qstrdup(ba.data());
    }
}
hjk's avatar
hjk committed
267
QT_END_NAMESPACE
268

269
270
QDebug &operator<<(QDebug& d, const MockClient::Block &b) { d << '[' << b.start << ',' << b.end << ']'; return d; }

271
272
class tst_Preprocessor: public QObject
{
273
274
275
    Q_OBJECT

protected:
276
    QByteArray preprocess(const QString &fileName, QByteArray * /*errors*/, bool nolines) {
277
278
279
280
        //### TODO: hook up errors
        QByteArray output;
        Environment env;
        MockClient client(&env, &output);
281
        client.sourceNeeded("data/" + fileName, nolines);
282
283
        return output;
    }
284
    static QString simplified(QByteArray buf);
285

286
287
private /* not corrected yet */:
    void macro_definition_lineno();
288
289
    void param_expanding_as_multiple_params();
    void macro_argument_expansion();
290
291

private slots:
292
293
294
    void defined();
    void defined_data();

295
296
    void va_args();
    void named_va_args();
297
    void first_empty_macro_arg();
298
    void invalid_param_count();
299
    void objmacro_expanding_as_fnmacro_notification();
300
    void macro_uses();
301
    void macro_arguments_notificatin();
302
    void unfinished_function_like_macro_call();
Erik Verbruggen's avatar
Erik Verbruggen committed
303
    void nasty_macro_expansion();
304
    void tstst();
305
306
307
    void test_file_builtin();

    void blockSkipping();
Erik Verbruggen's avatar
Erik Verbruggen committed
308
    void includes_1();
309
310
311

    void comparisons_data();
    void comparisons();
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
// Remove all #... lines, and 'simplify' string, to allow easily comparing the result
// Also, remove all unneeded spaces: keep only to ensure identifiers are separated.
// NOTE: may not correctly handle underscore in identifiers
QString tst_Preprocessor::simplified(QByteArray buf)
{
    QString out;
    QList<QByteArray> lines = buf.split('\n');
    foreach (QByteArray line, lines)
        if (!line.startsWith('#')) {
            out.append(' ');
            out.append(line);
        }

    out = out.simplified();
    for (int i=1; i<out.length()-1; ) {
        if (out.at(i).isSpace() && !(out.at(i-1).isLetterOrNumber() && out.at(i+1).isLetterOrNumber()))
            out.remove(i,1);
        else
            i++;
    }

    return out;
}

338
339
340
341
342
343
344
void tst_Preprocessor::va_args()
{
    Client *client = 0; // no client.
    Environment env;

    Preprocessor preprocess(client, &env);
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"),
345
                                         QByteArray("#define foo(...) int f(__VA_ARGS__);\n"
346
347
                                                    "\nfoo(  )\n"
                                                    "\nfoo(int a)\n"
348
349
350
                                                    "\nfoo(int a,int b)\n"),
                                         true,
                                         false);
351

352
353
    preprocessed = preprocessed.simplified();
//    DUMP_OUTPUT(preprocessed);
354
    QCOMPARE(simplified(preprocessed), QString("int f();int f(int a);int f(int a,int b);"));
355
356
357
358
359
360
361
362
363
364
365
366
}

void tst_Preprocessor::named_va_args()
{
    Client *client = 0; // no client.
    Environment env;

    Preprocessor preprocess(client, &env);
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"),
                                         QByteArray("\n#define foo(ARGS...) int f(ARGS);"
                                                    "\nfoo(  )\n"
                                                    "\nfoo(int a)\n"
367
368
                                                    "\nfoo(int a,int b)\n"),
                                         true, false);
369

370
    preprocessed = preprocessed.simplified();
371
    QCOMPARE(simplified(preprocessed), QString("int f();int f(int a);int f(int a,int b);"));
372
373
}

374
375
376
377
378
379
380
381
382
383
void tst_Preprocessor::first_empty_macro_arg()
{
    Client *client = 0; // no client.
    Environment env;

    Preprocessor preprocess(client, &env);
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"),
                                         QByteArray("\n#define foo(a,b) a int b;"
                                                    "\nfoo(const,cVal)\n"
                                                    "\nfoo(,Val)\n"
384
385
                                                    "\nfoo( ,Val2)\n"),
                                         true, false);
386

387
388
    preprocessed = preprocessed.simplified();
//    DUMP_OUTPUT(preprocessed);
389
    QCOMPARE(simplified(preprocessed), QString("const int cVal;int Val;int Val2;"));
390
391
}

392
void tst_Preprocessor::invalid_param_count()
393
394
395
396
397
{
    Client *client = 0; // no client.
    Environment env;

    Preprocessor preprocess(client, &env);
398
399
    // The following is illegal, but shouldn't crash the preprocessor.
    // GCC says: 3:14: error: macro "foo" requires 2 arguments, but only 1 given
400
401
402
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"),
                                         QByteArray("\n#define foo(a,b) int f(a,b);"
                                                    "\n#define ARGS(t)  t a,t b"
403
404
405
                                                    "\nfoo(ARGS(int))"),
                                         true, false);
    // do not verify the output: it's illegal, so anything might be outputted.
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
void tst_Preprocessor::param_expanding_as_multiple_params()
{
    Client *client = 0; // no client.
    Environment env;

    Preprocessor preprocess(client, &env);
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"),
                                         QByteArray("\n#define foo(a,b) int f(a,b);"
                                                    "\n#define ARGS(t)  t a,t b"
                                                    "\nfoo(ARGS(int))"));
    QCOMPARE(simplified(preprocessed), QString("int f(int a,int b);"));
}

void tst_Preprocessor::macro_argument_expansion() //QTCREATORBUG-7225
{
    Client *client = 0; // no client.
    Environment env;

    Preprocessor preprocess(client, &env);
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"),
                                         QByteArray("\n#define BAR1        2,3,4"
                                                    "\n#define FOO1(a,b,c) a+b+c"
                                                    "\nvoid test2(){"
                                                    "\nint x=FOO1(BAR1);"
                                                    "\n}"));
    QCOMPARE(simplified(preprocessed), QString("void test2(){int x=2+3+4;}"));

}

void tst_Preprocessor::macro_uses()
{
    QByteArray buffer = QByteArray("\n#define FOO 8"
                                   "\n#define BAR 9"
                                   "\nvoid test(){"
                                   "\n\tint x=FOO;"
                                   "\n\tint y=BAR;"
                                   "\n}");

    QByteArray output;
    Environment env;
    MockClient client(&env, &output);

    Preprocessor preprocess(&client, &env);
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"), buffer);
    QCOMPARE(simplified(preprocessed), QString("void test(){int x=8;int y=9;}"));
    QCOMPARE(client.expandedMacros(), QList<QByteArray>() << QByteArray("FOO") << QByteArray("BAR"));
    QCOMPARE(client.expandedMacrosOffset(), QList<unsigned>() << buffer.indexOf("FOO;") << buffer.indexOf("BAR;"));
455
456
    QCOMPARE(client.definedMacros(), QList<QByteArray>() << QByteArray("FOO") << QByteArray("BAR"));
    QCOMPARE(client.definedMacrosLine(), QList<unsigned>() << 2 << 3);
457
458
}

459
460
461
462
463
464
465
466
void tst_Preprocessor::macro_definition_lineno()
{
    Client *client = 0; // no client.
    Environment env;
    Preprocessor preprocess(client, &env);
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"),
                                         QByteArray("#define foo(ARGS) int f(ARGS)\n"
                                                    "foo(int a);\n"));
467
    QVERIFY(preprocessed.contains("#gen true\n# 2 \"<stdin>\"\nint f"));
468
469
470
471
472

    preprocessed = preprocess(QLatin1String("<stdin>"),
                              QByteArray("#define foo(ARGS) int f(ARGS)\n"
                                         "foo(int a)\n"
                                         ";\n"));
473
    QVERIFY(preprocessed.contains("#gen true\n# 2 \"<stdin>\"\nint f"));
474
475
476
477
478

    preprocessed = preprocess(QLatin1String("<stdin>"),
                              QByteArray("#define foo(ARGS) int f(ARGS)\n"
                                         "foo(int  \n"
                                         "    a);\n"));
479
    QVERIFY(preprocessed.contains("#gen true\n# 2 \"<stdin>\"\nint f"));
480
481
482
483

    preprocessed = preprocess(QLatin1String("<stdin>"),
                              QByteArray("#define foo int f\n"
                                         "foo;\n"));
484
    QVERIFY(preprocessed.contains("#gen true\n# 2 \"<stdin>\"\nint f"));
485
486
487
488
489

    preprocessed = preprocess(QLatin1String("<stdin>"),
                              QByteArray("#define foo int f\n"
                                         "foo\n"
                                         ";\n"));
490
    QVERIFY(preprocessed.contains("#gen true\n# 2 \"<stdin>\"\nint f"));
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
void tst_Preprocessor::objmacro_expanding_as_fnmacro_notification()
{
    QByteArray output;
    Environment env;
    MockClient client(&env, &output);

    Preprocessor preprocess(&client, &env);
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"),
                                         QByteArray("\n#define bar(a,b) a + b"
                                                    "\n#define foo bar"
                                                    "\nfoo(1, 2)\n"));

    QVERIFY(client.expandedMacros() == (QList<QByteArray>() << QByteArray("foo")));
}

void tst_Preprocessor::macro_arguments_notificatin()
{
    QByteArray output;
    Environment env;
    MockClient client(&env, &output);

    Preprocessor preprocess(&client, &env);
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"),
                                         QByteArray("\n#define foo(a,b) a + b"
                                                    "\n#define arg(a) a"
                                                    "\n#define value  2"
                                                    "\nfoo(arg(1), value)\n"));

    QVERIFY(client.expandedMacros() == (QList<QByteArray>() << QByteArray("foo")
                                                            << QByteArray("arg")
                                                            << QByteArray("value")));
}

526
527
528
529
530
531
void tst_Preprocessor::unfinished_function_like_macro_call()
{
    Client *client = 0; // no client.
    Environment env;

    Preprocessor preprocess(client, &env);
Roberto Raggi's avatar
Roberto Raggi committed
532
533
534
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"),
                                         QByteArray("\n#define foo(a,b) a + b"
                                         "\nfoo(1, 2\n"));
535
536
    QByteArray expected__("# 1 \"<stdin>\"\n\n\n    1\n#gen true\n# 2 \"<stdin>\"\n+\n#gen false\n# 3 \"<stdin>\"\n       2\n");
//    DUMP_OUTPUT(preprocessed);
537
    QCOMPARE(preprocessed, expected__);
538
539
}

Erik Verbruggen's avatar
Erik Verbruggen committed
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
void tst_Preprocessor::nasty_macro_expansion()
{
    QByteArray input("\n"
                     "#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))\n"
                     "#define is_power_of_two(x)      ( !((x) & ((x)-1)) )\n"
                     "#define low_bit_mask(x)         ( ((x)-1) & ~(x) )\n"
                     "#define is_valid_mask(x)        is_power_of_two(1LU + (x) + low_bit_mask(x))\n"
                     "#define compile_ffs2(__x) \\\n"
                     "        __builtin_choose_expr(((__x) & 0x1), 0, 1)\n"
                     "#define compile_ffs4(__x) \\\n"
                     "        __builtin_choose_expr(((__x) & 0x3), \\\n"
                     "                              (compile_ffs2((__x))), \\\n"
                     "                              (compile_ffs2((__x) >> 2) + 2))\n"
                     "#define compile_ffs8(__x) \\\n"
                     "        __builtin_choose_expr(((__x) & 0xf), \\\n"
                     "                              (compile_ffs4((__x))), \\\n"
                     "                              (compile_ffs4((__x) >> 4) + 4))\n"
                     "#define compile_ffs16(__x) \\\n"
                     "        __builtin_choose_expr(((__x) & 0xff), \\\n"
                     "                              (compile_ffs8((__x))), \\\n"
                     "                              (compile_ffs8((__x) >> 8) + 8))\n"
                     "#define compile_ffs32(__x) \\\n"
                     "        __builtin_choose_expr(((__x) & 0xffff), \\\n"
                     "                              (compile_ffs16((__x))), \\\n"
                     "                              (compile_ffs16((__x) >> 16) + 16))\n"
                     "#define FIELD_CHECK(__mask, __type)                     \\\n"
                     "        BUILD_BUG_ON(!(__mask) ||                       \\\n"
                     "                     !is_valid_mask(__mask) ||          \\\n"
                     "                     (__mask) != (__type)(__mask))      \\\n"
                     "\n"
                     "#define FIELD32(__mask)                         \\\n"
                     "({                                              \\\n"
                     "        FIELD_CHECK(__mask, u32);               \\\n"
                     "        (struct rt2x00_field32) {               \\\n"
                     "                compile_ffs32(__mask), (__mask) \\\n"
                     "        };                                      \\\n"
                     "})\n"
                     "#define BBPCSR                          0x00f0\n"
                     "#define BBPCSR_BUSY                     FIELD32(0x00008000)\n"
                     "#define WAIT_FOR_BBP(__dev, __reg)  \\\n"
                     "        rt2x00pci_regbusy_read((__dev), BBPCSR, BBPCSR_BUSY, (__reg))\n"
                     "if (WAIT_FOR_BBP(rt2x00dev, &reg)) {}\n"
                     );

    Client *client = 0; // no client.
    Environment env;

    Preprocessor preprocess(client, &env);
    QByteArray preprocessed = preprocess(QLatin1String("<stdin>"), input);

    QVERIFY(!preprocessed.contains("FIELD32"));
}

593
594
595
596
597
598
599
600
601
602
603
604
605
void tst_Preprocessor::tstst()
{
    Client *client = 0; // no client.
    Environment env;

    Preprocessor preprocess(client, &env);
    QByteArray preprocessed = preprocess(
                QLatin1String("<stdin>"),
                QByteArray("\n"
                           "# define _GLIBCXX_VISIBILITY(V) __attribute__ ((__visibility__ (#V)))\n"
                           "namespace std _GLIBCXX_VISIBILITY(default) {\n"
                           "}\n"
                           ));
606
    const QByteArray result____ ="# 1 \"<stdin>\"\n\n\n"
607
            "namespace std\n"
Erik Verbruggen's avatar
Erik Verbruggen committed
608
            "#gen true\n"
609
610
611
612
613
            "# 2 \"<stdin>\"\n"
            "__attribute__ ((__visibility__ (\n"
            "\"default\"\n"
            "# 2 \"<stdin>\"\n"
            ")))\n"
Erik Verbruggen's avatar
Erik Verbruggen committed
614
615
616
            "#gen false\n"
            "# 3 \"<stdin>\"\n"
            "                                           {\n"
617
618
            "}\n";

619
//    DUMP_OUTPUT(preprocessed);
620
621
622
623
624
625
626
627
628
629
630
631
632
633
    QCOMPARE(preprocessed, result____);
}

void tst_Preprocessor::test_file_builtin()
{
    Client *client = 0; // no client.
    Environment env;

    Preprocessor preprocess(client, &env);
    QByteArray preprocessed = preprocess(
                QLatin1String("some-file.c"),
                QByteArray("const char *f = __FILE__\n"
                           ));
    const QByteArray result____ =
634
            "# 1 \"some-file.c\"\n"
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
            "const char *f =\n"
            "#gen true\n"
            "# 1 \"some-file.c\"\n"
            "\"some-file.c\"\n"
            "#gen false\n"
            "# 2 \"some-file.c\"\n"
            ;
    QCOMPARE(preprocessed, result____);
}

void tst_Preprocessor::comparisons_data()
{
    QTest::addColumn<QString>("infile");
    QTest::addColumn<QString>("outfile");
    QTest::addColumn<QString>("errorfile");

    QTest::newRow("do nothing") << "noPP.1.cpp" << "noPP.1.cpp" << "";
    QTest::newRow("identifier-expansion 1") << "identifier-expansion.1.cpp" << "identifier-expansion.1.out.cpp" << "";
    QTest::newRow("identifier-expansion 2") << "identifier-expansion.2.cpp" << "identifier-expansion.2.out.cpp" << "";
    QTest::newRow("identifier-expansion 3") << "identifier-expansion.3.cpp" << "identifier-expansion.3.out.cpp" << "";
    QTest::newRow("identifier-expansion 4") << "identifier-expansion.4.cpp" << "identifier-expansion.4.out.cpp" << "";
    QTest::newRow("identifier-expansion 5") << "identifier-expansion.5.cpp" << "identifier-expansion.5.out.cpp" << "";
    QTest::newRow("reserved 1") << "reserved.1.cpp" << "reserved.1.out.cpp" << "";
    QTest::newRow("recursive 1") << "recursive.1.cpp" << "recursive.1.out.cpp" << "";
    QTest::newRow("macro_pounder_fn") << "macro_pounder_fn.c" << "" << "";
    QTest::newRow("macro_expand") << "macro_expand.c" << "macro_expand.out.c" << "";
    QTest::newRow("macro-test") << "macro-test.cpp" << "macro-test.out.cpp" << "";
    QTest::newRow("empty-macro") << "empty-macro.cpp" << "empty-macro.out.cpp" << "";
    QTest::newRow("empty-macro 2") << "empty-macro.2.cpp" << "empty-macro.2.out.cpp" << "";
}

void tst_Preprocessor::comparisons()
{
    QFETCH(QString, infile);
    QFETCH(QString, outfile);
    QFETCH(QString, errorfile);

    QByteArray errors;
673
    QByteArray preprocessed = preprocess(infile, &errors, infile == outfile);
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710

//    DUMP_OUTPUT(preprocessed);

    if (!outfile.isEmpty()) {
        QByteArray output____ = loadSource("data/"+outfile); // these weird underscores are here to make the name as long as "preprocessed", so the QCOMPARE error messages are nicely aligned.
        //    QCOMPARE(preprocessed, output____);
        QCOMPARE(QString::fromUtf8(preprocessed.constData()), QString::fromUtf8(output____.constData()));
    }

    if (!errorfile.isEmpty()) {
        QByteArray errorFileContents = loadSource("data/"+errorfile);
        QCOMPARE(QString::fromUtf8(errors.constData()), QString::fromUtf8(errorFileContents.constData()));
    }
}

void tst_Preprocessor::blockSkipping()
{
    QByteArray output;
    Environment env;
    MockClient client(&env, &output);
    Preprocessor pp(&client, &env);
    /*QByteArray preprocessed =*/ pp(
                QLatin1String("<stdin>"),
                QByteArray("#if 0\n"
                           "\n"
                           "int yes;\n"
                           "\n"
                           "#elif 0\n"
                           "\n"
                           "int no;\n"
                           "\n"
                           "#else // foobar\n"
                           "\n"
                           "void also_not;\n"
                           "\n"
                           "#endif\n"
                           ));
Erik Verbruggen's avatar
Erik Verbruggen committed
711

712
713
714
715
716
    QList<MockClient::Block> blocks = client.skippedBlocks();
    QCOMPARE(blocks.size(), 1);
    MockClient::Block b = blocks.at(0);
    QCOMPARE(b.start, 6U);
    QCOMPARE(b.end, 34U);
717
718
}

Erik Verbruggen's avatar
Erik Verbruggen committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
void tst_Preprocessor::includes_1()
{
    QByteArray output;
    Environment env;
    MockClient client(&env, &output);
    Preprocessor pp(&client, &env);
    /*QByteArray preprocessed =*/ pp(
                QLatin1String("<stdin>"),
                QByteArray("#define FOO <foo.h>\n"
                           "#define BAR \"bar.h\"\n"
                           "\n"
                           "#include FOO\n"
                           "#include BAR\n"
                           "\n"
                           "#include <zoo.h>\n"
                           "#include \"mooze.h\"\n"
                           ));

    QList<Include> incs = client.recordedIncludes();
//    qDebug()<<incs;
    QCOMPARE(incs.size(), 4);
    QCOMPARE(incs.at(0).fileName, QLatin1String("foo.h"));
    QCOMPARE(incs.at(0).type, Client::IncludeGlobal);
    QCOMPARE(incs.at(0).line, 4U);
    QCOMPARE(incs.at(1).fileName, QLatin1String("bar.h"));
    QCOMPARE(incs.at(1).type, Client::IncludeLocal);
    QCOMPARE(incs.at(1).line, 5U);
    QCOMPARE(incs.at(2).fileName, QLatin1String("zoo.h"));
    QCOMPARE(incs.at(2).type, Client::IncludeGlobal);
    QCOMPARE(incs.at(2).line, 7U);
    QCOMPARE(incs.at(3).fileName, QLatin1String("mooze.h"));
    QCOMPARE(incs.at(3).type, Client::IncludeLocal);
    QCOMPARE(incs.at(3).line, 8U);
}

754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
void tst_Preprocessor::defined()
{
    QFETCH(bool, xdefined);
    QFETCH(bool, ydefined);
    QFETCH(QString, input);
    QByteArray output;
    Environment env;
    MockClient client(&env, &output);
    Preprocessor pp(&client, &env);
    pp.run(QLatin1String("<stdin>"), input.toLatin1(), false, true);
    QList<QByteArray> expected;
    if (xdefined)
        expected.append("X");
    if (ydefined)
        expected.append("Y");
    if (client.definedMacros() != expected)
        qWarning() << "\nSource: " << input.replace('\n', "    ");
    QCOMPARE(client.definedMacros(), expected);
}

void tst_Preprocessor::defined_data()
{
    QTest::addColumn<bool>("xdefined");
    QTest::addColumn<bool>("ydefined");
    QTest::addColumn<QString>("input");

    QTest::newRow("1a") << true << true <<
        "#define X\n#if defined(X)\n#define Y\n#endif";
    QTest::newRow("1b") << true << true <<
        "#define X\n#if defined X \n#define Y\n#endif";
    QTest::newRow("1c") << true << true <<
        "#define X\n#ifdef X \n#define Y\n#endif";

    QTest::newRow("2a") << false << false <<
        "#if defined(X)\n#define Y\n#endif";
    QTest::newRow("2b") << false << false <<
        "#if defined X \n#define Y\n#endif";
    QTest::newRow("2c") << false << false <<
        "#ifdef X \n#define Y\n#endif";

    QTest::newRow("3a") << true << false <<
        "#define X\n#if !defined(X)\n#define Y\n#endif";
    QTest::newRow("3b") << true << false <<
        "#define X\n#if !defined X \n#define Y\n#endif";
    QTest::newRow("3c") << true << false <<
        "#define X\n#ifndef X \n#define Y\n#endif";

    QTest::newRow("4a") << false << true <<
        "#if !defined(X)\n#define Y\n#endif";
    QTest::newRow("4b") << false << true <<
        "#if !defined X \n#define Y\n#endif";
    QTest::newRow("4c") << false << true <<
        "#ifndef X \n#define Y\n#endif";
}

809
QTEST_APPLESS_MAIN(tst_Preprocessor)
810

811
#include "tst_preprocessor.moc"