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