support for creating template instance classes
[lqt/mk.git] / cpptoxml / parser / rpp / preprocessor.cpp
blobb46607c325b3c9e5afc4ac152dfede24183aefd4
1 /****************************************************************************
2 **
3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
4 ** Copyright 2005 Harald Fernengel <harry@kdevelop.org>
5 **
6 ** This file is part of $PRODUCT$.
7 **
8 ** $CPP_LICENSE$
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 ****************************************************************************/
15 #include "preprocessor.h"
17 #include <string>
19 // register callback for include hooks
20 static void includeFileHook(const std::string &, const std::string &, FILE *);
22 #define PP_HOOK_ON_FILE_INCLUDED(A, B, C) includeFileHook(A, B, C)
23 #include "pp.h"
25 using namespace rpp;
27 #include <QtCore/QtCore>
29 class PreprocessorPrivate
31 public:
32 QByteArray result;
33 pp_environment env;
34 QStringList includePaths;
36 void initPP(pp &proc)
38 foreach(const QString& path, includePaths)
39 proc.push_include_path(path.toStdString());
43 QHash<QString, QStringList> includedFiles;
45 void includeFileHook(const std::string &fileName, const std::string &filePath, FILE *)
47 includedFiles[QString::fromStdString(fileName)].append(QString::fromStdString(filePath));
50 Preprocessor::Preprocessor()
52 d = new PreprocessorPrivate;
53 includedFiles.clear();
56 Preprocessor::~Preprocessor()
58 delete d;
61 void Preprocessor::processFile(const QString &fileName)
63 pp proc(d->env);
64 d->initPP(proc);
66 d->result.reserve(d->result.size() + 20 * 1024);
68 d->result += "# 1 \"" + fileName.toLatin1() + "\"\n"; // ### REMOVE ME
69 proc.file(fileName.toLocal8Bit().constData(), std::back_inserter(d->result));
72 void Preprocessor::processString(const QByteArray &str)
74 pp proc(d->env);
75 d->initPP(proc);
77 proc(str.begin(), str.end(), std::back_inserter(d->result));
80 QByteArray Preprocessor::result() const
82 return d->result;
85 void Preprocessor::addIncludePaths(const QStringList &includePaths)
87 d->includePaths += includePaths;
90 QStringList Preprocessor::macroNames() const
92 QStringList macros;
94 pp_environment::const_iterator it = d->env.first_macro();
95 while (it != d->env.last_macro()) {
96 const pp_macro *m = *it;
97 macros += QString::fromLatin1(m->name->begin(), m->name->size());
98 ++it;
101 return macros;
104 QList<Preprocessor::MacroItem> Preprocessor::macros() const
106 QList<MacroItem> items;
108 pp_environment::const_iterator it = d->env.first_macro();
109 while (it != d->env.last_macro()) {
110 const pp_macro *m = *it;
111 MacroItem item;
112 item.name = QString::fromLatin1(m->name->begin(), m->name->size());
113 item.definition = QString::fromLatin1(m->definition->begin(),
114 m->definition->size());
115 for (size_t i = 0; i < m->formals.size(); ++i) {
116 item.parameters += QString::fromLatin1(m->formals[i]->begin(),
117 m->formals[i]->size());
119 item.isFunctionLike = m->f.function_like;
121 #ifdef PP_WITH_MACRO_POSITION
122 item.fileName = QString::fromLatin1(m->file->begin(), m->file->size());
123 #endif
124 items += item;
126 ++it;
129 return items;
133 int main()
135 Preprocessor pp;
137 QStringList paths;
138 paths << "/usr/include";
139 pp.addIncludePaths(paths);
141 pp.processFile("pp-configuration");
142 pp.processFile("/usr/include/stdio.h");
144 qDebug() << pp.result();
146 return 0;