Add a new -rewrite-test option, which is basically a
playground to experiment with some new rewriter approaches. For now
it is probably the most complex version of 'cat' ever invented.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57406 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp
new file mode 100644
index 0000000..446ae1a
--- /dev/null
+++ b/Driver/RewriteTest.cpp
@@ -0,0 +1,41 @@
+//===--- RewriteTest.cpp - Rewriter playground ----------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This is a testbed.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Rewrite/TokenRewriter.h"
+#include "clang.h"
+#include "clang/Lex/Preprocessor.h"
+#include <iostream>
+
+void clang::DoRewriteTest(Preprocessor &PP, const std::string &InFileName,
+ const std::string &OutFileName) {
+ SourceManager &SM = PP.getSourceManager();
+ const LangOptions &LangOpts = PP.getLangOptions();
+
+ std::pair<const char*,const char*> File =SM.getBufferData(SM.getMainFileID());
+
+ // Create a lexer to lex all the tokens of the main file in raw mode. Even
+ // though it is in raw mode, it will not return comments.
+ Lexer RawLex(SourceLocation::getFileLoc(SM.getMainFileID(), 0),
+ LangOpts, File.first, File.second);
+
+ RawLex.SetKeepWhitespaceMode(true);
+
+ Token RawTok;
+ RawLex.LexFromRawLexer(RawTok);
+ while (RawTok.isNot(tok::eof)) {
+ std::cout << PP.getSpelling(RawTok);
+ RawLex.LexFromRawLexer(RawTok);
+ }
+
+
+}
\ No newline at end of file
diff --git a/Driver/clang.cpp b/Driver/clang.cpp
index 91d5212..ae7ec87 100644
--- a/Driver/clang.cpp
+++ b/Driver/clang.cpp
@@ -66,6 +66,7 @@
RewriteObjC, // ObjC->C Rewriter.
RewriteBlocks, // ObjC->C Rewriter for Blocks.
RewriteMacros, // Expand macros but not #includes.
+ RewriteTest, // Rewriter playground
HTMLTest, // HTML displayer testing stuff.
EmitLLVM, // Emit a .ll file.
EmitBC, // Emit a .bc file.
@@ -119,6 +120,8 @@
"Build ASTs then convert to LLVM, emit .bc file"),
clEnumValN(SerializeAST, "serialize",
"Build ASTs and emit .ast file"),
+ clEnumValN(RewriteTest, "rewrite-test",
+ "Rewriter playground"),
clEnumValN(RewriteObjC, "rewrite-objc",
"Rewrite ObjC into C (code rewriter example)"),
clEnumValN(RewriteMacros, "rewrite-macros",
@@ -1173,6 +1176,11 @@
RewriteMacrosInInput(PP, InFile, OutputFile);
ClearSourceMgr = true;
break;
+
+ case RewriteTest:
+ DoRewriteTest(PP, InFile, OutputFile);
+ ClearSourceMgr = true;
+ break;
}
if (Consumer) {
diff --git a/Driver/clang.h b/Driver/clang.h
index ddb5dcb..8294675 100644
--- a/Driver/clang.h
+++ b/Driver/clang.h
@@ -32,6 +32,10 @@
/// RewriteMacrosInInput - Implement -rewrite-macros mode.
void RewriteMacrosInInput(Preprocessor &PP, const std::string &InFileName,
const std::string& OutFile);
+
+void DoRewriteTest(Preprocessor &PP, const std::string &InFileName,
+ const std::string &OutFileName);
+
/// CreatePrintParserActionsAction - Return the actions implementation that
/// implements the -parse-print-callbacks option.