OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / tests / auto / cplusplus / preprocessor / tst_preprocessor.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include <QtTest>
34 #include <pp.h>
35
36 //TESTED_COMPONENT=src/libs/cplusplus
37 using namespace CPlusPlus;
38
39 class tst_Preprocessor: public QObject
40 {
41 Q_OBJECT
42
43 private Q_SLOTS:
44     void unfinished_function_like_macro_call();
45     void nasty_macro_expansion();
46 };
47
48 void tst_Preprocessor::unfinished_function_like_macro_call()
49 {
50     Client *client = 0; // no client.
51     Environment env;
52
53     Preprocessor preprocess(client, &env);
54     QByteArray preprocessed = preprocess(QLatin1String("<stdin>"),
55                                          QByteArray("\n#define foo(a,b) a + b"
56                                          "\nfoo(1, 2\n"));
57
58     QCOMPARE(preprocessed.trimmed(), QByteArray("foo"));
59 }
60
61 void tst_Preprocessor::nasty_macro_expansion()
62 {
63     QByteArray input("\n"
64                      "#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))\n"
65                      "#define is_power_of_two(x)      ( !((x) & ((x)-1)) )\n"
66                      "#define low_bit_mask(x)         ( ((x)-1) & ~(x) )\n"
67                      "#define is_valid_mask(x)        is_power_of_two(1LU + (x) + low_bit_mask(x))\n"
68                      "#define compile_ffs2(__x) \\\n"
69                      "        __builtin_choose_expr(((__x) & 0x1), 0, 1)\n"
70                      "#define compile_ffs4(__x) \\\n"
71                      "        __builtin_choose_expr(((__x) & 0x3), \\\n"
72                      "                              (compile_ffs2((__x))), \\\n"
73                      "                              (compile_ffs2((__x) >> 2) + 2))\n"
74                      "#define compile_ffs8(__x) \\\n"
75                      "        __builtin_choose_expr(((__x) & 0xf), \\\n"
76                      "                              (compile_ffs4((__x))), \\\n"
77                      "                              (compile_ffs4((__x) >> 4) + 4))\n"
78                      "#define compile_ffs16(__x) \\\n"
79                      "        __builtin_choose_expr(((__x) & 0xff), \\\n"
80                      "                              (compile_ffs8((__x))), \\\n"
81                      "                              (compile_ffs8((__x) >> 8) + 8))\n"
82                      "#define compile_ffs32(__x) \\\n"
83                      "        __builtin_choose_expr(((__x) & 0xffff), \\\n"
84                      "                              (compile_ffs16((__x))), \\\n"
85                      "                              (compile_ffs16((__x) >> 16) + 16))\n"
86                      "#define FIELD_CHECK(__mask, __type)                     \\\n"
87                      "        BUILD_BUG_ON(!(__mask) ||                       \\\n"
88                      "                     !is_valid_mask(__mask) ||          \\\n"
89                      "                     (__mask) != (__type)(__mask))      \\\n"
90                      "\n"
91                      "#define FIELD32(__mask)                         \\\n"
92                      "({                                              \\\n"
93                      "        FIELD_CHECK(__mask, u32);               \\\n"
94                      "        (struct rt2x00_field32) {               \\\n"
95                      "                compile_ffs32(__mask), (__mask) \\\n"
96                      "        };                                      \\\n"
97                      "})\n"
98                      "#define BBPCSR                          0x00f0\n"
99                      "#define BBPCSR_BUSY                     FIELD32(0x00008000)\n"
100                      "#define WAIT_FOR_BBP(__dev, __reg)  \\\n"
101                      "        rt2x00pci_regbusy_read((__dev), BBPCSR, BBPCSR_BUSY, (__reg))\n"
102                      "if (WAIT_FOR_BBP(rt2x00dev, &reg)) {}\n"
103                      );
104
105     Client *client = 0; // no client.
106     Environment env;
107
108     Preprocessor preprocess(client, &env);
109     QByteArray preprocessed = preprocess(QLatin1String("<stdin>"), input);
110
111     QVERIFY(!preprocessed.contains("FIELD32"));
112 }
113
114 QTEST_APPLESS_MAIN(tst_Preprocessor)
115 #include "tst_preprocessor.moc"