1 /****************************************************************************
3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
4 ** Copyright 2005 Harald Fernengel <harry@kdevelop.org>
6 ** This file is part of $PRODUCT$.
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"
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)
27 #include <QtCore/QtCore>
29 class PreprocessorPrivate
34 QStringList includePaths
;
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()
61 void Preprocessor::processFile(const QString
&fileName
)
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
)
77 proc(str
.begin(), str
.end(), std::back_inserter(d
->result
));
80 QByteArray
Preprocessor::result() const
85 void Preprocessor::addIncludePaths(const QStringList
&includePaths
)
87 d
->includePaths
+= includePaths
;
90 QStringList
Preprocessor::macroNames() const
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());
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
;
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());
138 paths << "/usr/include";
139 pp.addIncludePaths(paths);
141 pp.processFile("pp-configuration");
142 pp.processFile("/usr/include/stdio.h");
144 qDebug() << pp.result();