summaryrefslogtreecommitdiff
path: root/tests/manual/plain-cplusplus/main.cpp
diff options
context:
space:
mode:
authorRoberto Raggi <roberto.raggi@nokia.com>2009-12-15 16:49:59 +0100
committerRoberto Raggi <roberto.raggi@nokia.com>2009-12-15 16:49:59 +0100
commit986559d297f04542af550d56e09bf1bea640d6c4 (patch)
tree9f340f3a26dbc3290509f2b4776f3fd855a8ba02 /tests/manual/plain-cplusplus/main.cpp
parentd0abb5daf049e1dc766f5a4ca2500051cbe24953 (diff)
downloadqt-creator-986559d297f04542af550d56e09bf1bea640d6c4.tar.gz
Test the new preprocessor.
Diffstat (limited to 'tests/manual/plain-cplusplus/main.cpp')
-rw-r--r--tests/manual/plain-cplusplus/main.cpp63
1 files changed, 56 insertions, 7 deletions
diff --git a/tests/manual/plain-cplusplus/main.cpp b/tests/manual/plain-cplusplus/main.cpp
index dae95ba734..8bbd63becb 100644
--- a/tests/manual/plain-cplusplus/main.cpp
+++ b/tests/manual/plain-cplusplus/main.cpp
@@ -27,6 +27,7 @@
**
**************************************************************************/
+#include "Preprocessor.h"
#include <AST.h>
#include <ASTVisitor.h>
#include <Control.h>
@@ -44,8 +45,22 @@
using namespace CPlusPlus;
+enum { BLOCK_SIZE = 4 * 1024};
+
+void parse(const char *fileName, const char *source, unsigned size);
+int runWithSystemPreprocessor(int argc, char *argv[]);
+int runWithNewPreprocessor(int argc, char *argv[]);
+
int main(int argc, char *argv[])
{
+ if (getenv("CPLUSPLUS_WITH_NEW_PREPROCESSOR"))
+ return runWithNewPreprocessor(argc, argv);
+
+ return runWithSystemPreprocessor(argc, argv);
+}
+
+int runWithSystemPreprocessor(int argc, char *argv[])
+{
std::string cmdline;
cmdline += "gcc -E -xc++ -U__BLOCKS__";
@@ -54,14 +69,13 @@ int main(int argc, char *argv[])
cmdline += argv[i];
}
- enum { BLOCK_SIZE = 4 * 1024};
char block[BLOCK_SIZE];
- std::string source;
+ std::string preprocessedCode;
if (FILE *fp = popen(cmdline.c_str(), "r")) {
while (size_t sz = fread(block, 1, BLOCK_SIZE, fp))
- source.append(block, sz);
+ preprocessedCode.append(block, sz);
pclose(fp);
@@ -70,9 +84,46 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}
+ parse("<stdin>", preprocessedCode.c_str(), preprocessedCode.size());
+ return EXIT_SUCCESS;
+}
+
+int runWithNewPreprocessor(int argc, char *argv[])
+{
+ if (argc == 1) {
+ fprintf(stderr, "c++: No such file or directory\n");
+ return EXIT_FAILURE;
+ }
+
+ char block[BLOCK_SIZE];
+
+ std::string source;
+
+ if (FILE *fp = fopen(argv[1], "r")) {
+ while (size_t sz = fread(block, 1, BLOCK_SIZE, fp))
+ source.append(block, sz);
+
+ fclose(fp);
+
+ } else {
+ fprintf(stderr, "c++: No such file or directory\n");
+ return EXIT_FAILURE;
+ }
+
+ std::ostringstream out;
+ Preprocessor pp(out);
+ pp(source.c_str(), source.size(), StringRef(argv[1]));
+
+ const std::string preprocessedCode = out.str();
+ parse(argv[1], preprocessedCode.c_str(), preprocessedCode.size());
+ return EXIT_SUCCESS;
+}
+
+void parse(const char *fileName, const char *source, unsigned size)
+{
Control control;
- TranslationUnit unit(&control, control.findOrInsertStringLiteral("<stdin>"));
- unit.setSource(source.c_str(), source.size());
+ TranslationUnit unit(&control, control.findOrInsertStringLiteral(fileName));
+ unit.setSource(source, size);
unit.parse();
if (TranslationUnitAST *ast = unit.ast()->asTranslationUnit()) {
@@ -82,6 +133,4 @@ int main(int argc, char *argv[])
sem.check(it->value, globalNamespace->members());
}
}
-
- return EXIT_SUCCESS;
}