blob: 4f75e8b267ed7345f0569da515f1a7c78802f5b7 [file] [log] [blame]
Chris Lattner1665a9f2008-05-08 06:52:13 +00001//===--- RewriteMacros.cpp - Rewrite macros into their expansions ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This code rewrites macro invocations into their expansions. This gives you
11// a macro expanded file that retains comments and #includes.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang.h"
16#include "clang/Rewrite/Rewriter.h"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Basic/SourceManager.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/Support/Streams.h"
21#include "llvm/System/Path.h"
22#include <fstream>
23using namespace clang;
24
25/// RewriteMacrosInInput - Implement -rewrite-macros mode.
26void clang::RewriteMacrosInInput(Preprocessor &PP,
27 const std::string &OutFileName) {
28 SourceManager &SM = PP.getSourceManager();
29
30 Rewriter Rewrite;
31 Rewrite.setSourceMgr(SM);
32
33#if 0
34
35 // Get the ID and start/end of the main file.
36 unsigned MainFileID = SM.getMainFileID();
37 const llvm::MemoryBuffer *MainBuf = SM.getBuffer(MainFileID);
38 const char *MainFileStart = MainBuf->getBufferStart();
39 const char *MainFileEnd = MainBuf->getBufferEnd();
40
41
42 // Create the output file.
43
44 std::ostream *OutFile;
45 if (OutFileName == "-") {
46 OutFile = llvm::cout.stream();
47 } else if (!OutFileName.empty()) {
48 OutFile = new std::ofstream(OutFileName.c_str(),
49 std::ios_base::binary|std::ios_base::out);
50 } else if (InFileName == "-") {
51 OutFile = llvm::cout.stream();
52 } else {
53 llvm::sys::Path Path(InFileName);
54 Path.eraseSuffix();
55 Path.appendSuffix("cpp");
56 OutFile = new std::ofstream(Path.toString().c_str(),
57 std::ios_base::binary|std::ios_base::out);
58 }
59
60 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
61 // we are done.
62 if (const RewriteBuffer *RewriteBuf =
63 Rewrite.getRewriteBufferFor(MainFileID)) {
64 //printf("Changed:\n");
65 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
66 } else {
67 fprintf(stderr, "No changes\n");
68 }
69 // Emit metadata.
70 *OutFile << ResultStr;
71#endif
72
73}
74
75
76