summaryrefslogtreecommitdiffstats
path: root/clang/Driver/RewriteMacros.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang/Driver/RewriteMacros.cpp')
-rw-r--r--clang/Driver/RewriteMacros.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/clang/Driver/RewriteMacros.cpp b/clang/Driver/RewriteMacros.cpp
new file mode 100644
index 00000000000..4f75e8b267e
--- /dev/null
+++ b/clang/Driver/RewriteMacros.cpp
@@ -0,0 +1,76 @@
+//===--- RewriteMacros.cpp - Rewrite macros into their expansions ---------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This code rewrites macro invocations into their expansions. This gives you
+// a macro expanded file that retains comments and #includes.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang.h"
+#include "clang/Rewrite/Rewriter.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Basic/SourceManager.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Streams.h"
+#include "llvm/System/Path.h"
+#include <fstream>
+using namespace clang;
+
+/// RewriteMacrosInInput - Implement -rewrite-macros mode.
+void clang::RewriteMacrosInInput(Preprocessor &PP,
+ const std::string &OutFileName) {
+ SourceManager &SM = PP.getSourceManager();
+
+ Rewriter Rewrite;
+ Rewrite.setSourceMgr(SM);
+
+#if 0
+
+ // Get the ID and start/end of the main file.
+ unsigned MainFileID = SM.getMainFileID();
+ const llvm::MemoryBuffer *MainBuf = SM.getBuffer(MainFileID);
+ const char *MainFileStart = MainBuf->getBufferStart();
+ const char *MainFileEnd = MainBuf->getBufferEnd();
+
+
+ // Create the output file.
+
+ std::ostream *OutFile;
+ if (OutFileName == "-") {
+ OutFile = llvm::cout.stream();
+ } else if (!OutFileName.empty()) {
+ OutFile = new std::ofstream(OutFileName.c_str(),
+ std::ios_base::binary|std::ios_base::out);
+ } else if (InFileName == "-") {
+ OutFile = llvm::cout.stream();
+ } else {
+ llvm::sys::Path Path(InFileName);
+ Path.eraseSuffix();
+ Path.appendSuffix("cpp");
+ OutFile = new std::ofstream(Path.toString().c_str(),
+ std::ios_base::binary|std::ios_base::out);
+ }
+
+ // Get the buffer corresponding to MainFileID. If we haven't changed it, then
+ // we are done.
+ if (const RewriteBuffer *RewriteBuf =
+ Rewrite.getRewriteBufferFor(MainFileID)) {
+ //printf("Changed:\n");
+ *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
+ } else {
+ fprintf(stderr, "No changes\n");
+ }
+ // Emit metadata.
+ *OutFile << ResultStr;
+#endif
+
+}
+
+
+
OpenPOWER on IntegriCloud