Mercurial > projects > qtd
annotate generator/parser/rpp/preprocessor.cpp @ 52:09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
author | eldar |
---|---|
date | Mon, 18 May 2009 15:23:28 +0000 |
parents | e78566595089 |
children |
rev | line source |
---|---|
1 | 1 /**************************************************************************** |
2 ** | |
52
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
3 ** Copyright (C) 1992-2009 Nokia. All rights reserved. |
1 | 4 ** Copyright 2005 Harald Fernengel <harry@kdevelop.org> |
5 ** | |
6 ** This file is part of Qt Jambi. | |
7 ** | |
52
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
8 ** Commercial Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
9 Licensees holding valid Qt Commercial licenses may use this file in |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
10 accordance with the Qt Commercial License Agreement provided with the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
11 Software or, alternatively, in accordance with the terms contained in |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
12 a written agreement between you and Nokia. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
13 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
14 GNU Lesser General Public License Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
15 Alternatively, this file may be used under the terms of the GNU Lesser |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
16 General Public License version 2.1 as published by the Free Software |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
17 Foundation and appearing in the file LICENSE.LGPL included in the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
18 packaging of this file. Please review the following information to |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
19 ensure the GNU Lesser General Public License version 2.1 requirements |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
20 will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
21 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
22 In addition, as a special exception, Nokia gives you certain |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
23 additional rights. These rights are described in the Nokia Qt LGPL |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
24 Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
25 package. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
26 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
27 GNU General Public License Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
28 Alternatively, this file may be used under the terms of the GNU |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
29 General Public License version 3.0 as published by the Free Software |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
30 Foundation and appearing in the file LICENSE.GPL included in the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
31 packaging of this file. Please review the following information to |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
32 ensure the GNU General Public License version 3.0 requirements will be |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
33 met: http://www.gnu.org/copyleft/gpl.html. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
34 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
35 If you are unsure which license is appropriate for your use, please |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
36 contact the sales department at qt-sales@nokia.com. |
1 | 37 |
38 ** | |
39 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | |
40 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
41 ** | |
42 ****************************************************************************/ | |
43 | |
44 #include "preprocessor.h" | |
45 | |
46 #include <string> | |
47 | |
48 // register callback for include hooks | |
49 static void includeFileHook(const std::string &, const std::string &, FILE *); | |
50 | |
51 #define PP_HOOK_ON_FILE_INCLUDED(A, B, C) includeFileHook(A, B, C) | |
52 #include "pp.h" | |
53 | |
54 using namespace rpp; | |
55 | |
56 #include <QtCore/QtCore> | |
57 | |
58 class PreprocessorPrivate | |
59 { | |
60 public: | |
61 QByteArray result; | |
62 pp_environment env; | |
63 QStringList includePaths; | |
64 | |
65 void initPP(pp &proc) | |
66 { | |
67 foreach(QString path, includePaths) | |
68 proc.push_include_path(path.toStdString()); | |
69 } | |
70 }; | |
71 | |
72 QHash<QString, QStringList> includedFiles; | |
73 | |
74 void includeFileHook(const std::string &fileName, const std::string &filePath, FILE *) | |
75 { | |
76 includedFiles[QString::fromStdString(fileName)].append(QString::fromStdString(filePath)); | |
77 } | |
78 | |
79 Preprocessor::Preprocessor() | |
80 { | |
81 d = new PreprocessorPrivate; | |
82 includedFiles.clear(); | |
83 } | |
84 | |
85 Preprocessor::~Preprocessor() | |
86 { | |
87 delete d; | |
88 } | |
89 | |
90 void Preprocessor::processFile(const QString &fileName) | |
91 { | |
92 pp proc(d->env); | |
93 d->initPP(proc); | |
94 | |
95 d->result.reserve(d->result.size() + 20 * 1024); | |
96 | |
97 d->result += "# 1 \"" + fileName.toLatin1() + "\"\n"; // ### REMOVE ME | |
98 proc.file(fileName.toLocal8Bit().constData(), std::back_inserter(d->result)); | |
99 } | |
100 | |
101 void Preprocessor::processString(const QByteArray &str) | |
102 { | |
103 pp proc(d->env); | |
104 d->initPP(proc); | |
105 | |
106 proc(str.begin(), str.end(), std::back_inserter(d->result)); | |
107 } | |
108 | |
109 QByteArray Preprocessor::result() const | |
110 { | |
111 return d->result; | |
112 } | |
113 | |
114 void Preprocessor::addIncludePaths(const QStringList &includePaths) | |
115 { | |
116 d->includePaths += includePaths; | |
117 } | |
118 | |
119 QStringList Preprocessor::macroNames() const | |
120 { | |
121 QStringList macros; | |
122 | |
123 pp_environment::const_iterator it = d->env.first_macro(); | |
124 while (it != d->env.last_macro()) { | |
125 const pp_macro *m = *it; | |
126 macros += QString::fromLatin1(m->name->begin(), m->name->size()); | |
127 ++it; | |
128 } | |
129 | |
130 return macros; | |
131 } | |
132 | |
133 QList<Preprocessor::MacroItem> Preprocessor::macros() const | |
134 { | |
135 QList<MacroItem> items; | |
136 | |
137 pp_environment::const_iterator it = d->env.first_macro(); | |
138 while (it != d->env.last_macro()) { | |
139 const pp_macro *m = *it; | |
140 MacroItem item; | |
141 item.name = QString::fromLatin1(m->name->begin(), m->name->size()); | |
142 item.definition = QString::fromLatin1(m->definition->begin(), | |
143 m->definition->size()); | |
144 for (size_t i = 0; i < m->formals.size(); ++i) { | |
145 item.parameters += QString::fromLatin1(m->formals[i]->begin(), | |
146 m->formals[i]->size()); | |
147 } | |
148 item.isFunctionLike = m->function_like; | |
149 | |
150 #ifdef PP_WITH_MACRO_POSITION | |
151 item.fileName = QString::fromLatin1(m->file->begin(), m->file->size()); | |
152 #endif | |
153 items += item; | |
154 | |
155 ++it; | |
156 } | |
157 | |
158 return items; | |
159 } | |
160 | |
161 /* | |
162 int main() | |
163 { | |
164 Preprocessor pp; | |
165 | |
166 QStringList paths; | |
167 paths << "/usr/include"; | |
168 pp.addIncludePaths(paths); | |
169 | |
170 pp.processFile("pp-configuration"); | |
171 pp.processFile("/usr/include/stdio.h"); | |
172 | |
173 qDebug() << pp.result(); | |
174 | |
175 return 0; | |
176 } | |
177 */ | |
178 |