Commit 1575e683 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Show how to use the preprocessor to generate deps.

parent caad98ec
......@@ -3,25 +3,96 @@
#include <PreprocessorClient.h>
#include <pp.h>
#include <QCoreApplication>
#include <QFile>
#include <QStringList>
#include <QDir>
#include <QtDebug>
#include <iostream>
using namespace CPlusPlus;
int main()
class MakeDepend: public Client
{
Client *client = 0;
Environment env;
Preprocessor preprocess(client, &env);
Environment *env;
QList<QDir> systemDirs;
public:
MakeDepend(Environment *env)
: env(env)
{ }
void addSystemDir(const QDir &dir)
{ systemDirs.append(dir); }
void addSystemDir(const QString &path)
{ systemDirs.append(QDir(path)); }
virtual void macroAdded(const Macro &)
{ }
virtual void sourceNeeded(QString &fileName, IncludeType mode, unsigned)
{
if (mode == IncludeLocal) {
// ### cache
const QFileInfo currentFile(QFile::decodeName(env->currentFile));
const QDir dir = currentFile.dir();
QFileInfo fileInfo(dir, fileName);
if (fileInfo.exists()) {
fileName = fileInfo.absoluteFilePath();
std::cout << ' ' << qPrintable(fileName);
return;
}
}
QFile in;
if (! in.open(stdin, QFile::ReadOnly))
return 0;
foreach (const QDir &dir, systemDirs) {
QFileInfo fileInfo(dir, fileName);
if (fileInfo.exists() && fileInfo.isFile()) {
fileName = fileInfo.absoluteFilePath();
std::cout << ' ' << qPrintable(fileName);
return;
}
}
std::cerr << "file '" << qPrintable(fileName) << "' not found" << std::endl;
}
virtual void startExpandingMacro(unsigned, const Macro &,
const QByteArray &,
const QVector<MacroArgumentReference> &)
{ }
virtual void stopExpandingMacro(unsigned, const Macro &)
{ }
virtual void startSkippingBlocks(unsigned)
{ }
virtual void stopSkippingBlocks(unsigned)
{ }
};
int main(int argc, char *argv[])
{
Environment env;
MakeDepend client(&env);
const QByteArray source = in.readAll();
const QByteArray preprocessedCode = preprocess("<stdin>", source);
client.addSystemDir(QLatin1String("/usr/include"));
Preprocessor preproc(&client, &env);
std::cout << preprocessedCode.constData();
for (int i = 1; i < argc; ++i) {
const QByteArray fileName = argv[i];
std::cout << fileName.constData() << ':';
QFile file(QFile::decodeName(fileName));
if (file.open(QFile::ReadOnly)) {
// ### we should QTextStream here.
const QByteArray code = file.readAll();
preproc.preprocess(fileName, code, /*result = */ 0);
}
std::cout << std::endl;
}
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment