From 1575e6836d73ec32b9fbfd4517f6740e0f8235e5 Mon Sep 17 00:00:00 2001
From: Roberto Raggi <roberto.raggi@nokia.com>
Date: Wed, 13 May 2009 15:29:45 +0200
Subject: [PATCH] Show how to use the preprocessor to generate deps.

---
 tests/manual/preprocessor/main.cpp | 91 ++++++++++++++++++++++++++----
 1 file changed, 81 insertions(+), 10 deletions(-)

diff --git a/tests/manual/preprocessor/main.cpp b/tests/manual/preprocessor/main.cpp
index e38f0aecf7a..b8395334f58 100644
--- a/tests/manual/preprocessor/main.cpp
+++ b/tests/manual/preprocessor/main.cpp
@@ -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;
 }
-- 
GitLab