blob: b61a13c9bb5224ca63e4113ee73c966802be31c6 [file] [log] [blame]
Chris Lattnerb57e3d42008-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"
Chris Lattnerb57e3d42008-05-08 06:52:13 +000019#include "llvm/Support/Streams.h"
20#include "llvm/System/Path.h"
21#include <fstream>
22using namespace clang;
23
Chris Lattnerec921562008-05-10 00:02:33 +000024/// isSameToken - Return true if the two specified tokens start have the same
25/// content.
26static bool isSameToken(Token &RawTok, Token &PPTok) {
27 if (PPTok.getKind() == RawTok.getKind())
28 return true;
29
30 if (PPTok.getIdentifierInfo() &&
31 PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
32 return true;
33
34 return false;
35}
36
37static void GetNextRawTok(Lexer &RawLex, Token &RawTok, Preprocessor &PP) {
38 RawLex.LexRawToken(RawTok);
39
40 // If we have an identifier with no identifier info for our raw token, look
41 // up the indentifier info.
42 if (RawTok.is(tok::identifier) && !RawTok.getIdentifierInfo())
43 RawTok.setIdentifierInfo(PP.LookUpIdentifierInfo(RawTok));
44}
45
Chris Lattnerb57e3d42008-05-08 06:52:13 +000046/// RewriteMacrosInInput - Implement -rewrite-macros mode.
Chris Lattner09510522008-05-09 22:43:24 +000047void clang::RewriteMacrosInInput(Preprocessor &PP,const std::string &InFileName,
Chris Lattnerb57e3d42008-05-08 06:52:13 +000048 const std::string &OutFileName) {
49 SourceManager &SM = PP.getSourceManager();
50
51 Rewriter Rewrite;
52 Rewrite.setSourceMgr(SM);
Chris Lattnerec921562008-05-10 00:02:33 +000053 RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID());
Chris Lattnerb57e3d42008-05-08 06:52:13 +000054
Chris Lattnerec921562008-05-10 00:02:33 +000055 const SourceManager &SourceMgr = PP.getSourceManager();
56 std::pair<const char*, const char*> File =
57 SourceMgr.getBufferData(SM.getMainFileID());
58
59 // Create a lexer to lex all the tokens of the main file in raw mode. Even
60 // though it is in raw mode, it will not return comments.
61 Lexer RawLex(SourceLocation::getFileLoc(SM.getMainFileID(), 0),
62 PP.getLangOptions(), File.first, File.second);
63 Token RawTok;
64 GetNextRawTok(RawLex, RawTok, PP);
65
66 // Get the first preprocessing token.
67 PP.EnterMainSourceFile();
68 Token PPTok;
69 PP.Lex(PPTok);
70
71 // Preprocess the input file in parallel with raw lexing the main file. Ignore
72 // all tokens that are preprocessed from a file other than the main file (e.g.
73 // a header). If we see tokens that are in the preprocessed file bug not the
74 // lexed file, we have a macro expansion. If we see tokens in the lexed file
75 // that aren't in the preprocessed view, we have macros that expand to no
76 // tokens, or macro arguments etc.
77 while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) {
78 SourceLocation PPLoc = SM.getLogicalLoc(PPTok.getLocation());
79
80 // If PPTok is from a different source file, ignore it.
81 if (!SM.isFromMainFile(PPLoc)) {
82 PP.Lex(PPTok);
83 continue;
84 }
85
86 // If the raw file hits a preprocessor directive, they will be extra tokens
87 // in the input file, but we don't want to treat them as such... just ignore
88 // them.
89 if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) {
90 GetNextRawTok(RawLex, RawTok, PP);
91 while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof))
92 GetNextRawTok(RawLex, RawTok, PP);
93 continue;
94 }
95
96 // Okay, both tokens are from the same file. Get their offsets from the
97 // start of the file.
98 unsigned PPOffs = SM.getFullFilePos(PPLoc);
99 unsigned RawOffs = SM.getFullFilePos(RawTok.getLocation());
100
101 // If the offsets are the same and the token kind is the same, ignore them.
102 if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) {
103 GetNextRawTok(RawLex, RawTok, PP);
104 PP.Lex(PPTok);
105 continue;
106 }
107
108 // If the PP token is farther along than the raw token, something was
109 // deleted. Comment out the raw token.
110 if (RawOffs <= PPOffs) {
111 // Comment out a whole run of tokens instead of bracketing each one with
112 // comments.
113 RB.InsertTextAfter(RawOffs, "/*", 2);
114 unsigned EndPos;
115
116 // Switch on comment lexing. If we get a comment, we don't want to
117 // include it as part of our run of tokens, because we don't want to
118 // nest /* */ comments.
119 RawLex.SetCommentRetentionState(true);
120
121 do {
122 EndPos = RawOffs+RawTok.getLength();
123
124 GetNextRawTok(RawLex, RawTok, PP);
125 RawOffs = SM.getFullFilePos(RawTok.getLocation());
126
127 if (RawTok.is(tok::comment)) {
128 RawLex.SetCommentRetentionState(false);
129 // Skip past the comment.
130 GetNextRawTok(RawLex, RawTok, PP);
131 break;
132 }
133
134 } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() &&
135 (PPOffs != RawOffs || !isSameToken(RawTok, PPTok)));
136
137 RawLex.SetCommentRetentionState(false);
138
139 RB.InsertTextBefore(EndPos, "*/", 2);
140 continue;
141 }
142
143 // Otherwise, there was a replacement an expansion. Insert the new token
144 // in the output buffer. Insert the whole run of new tokens at once to get
145 // them in the right order.
146 unsigned InsertPos = PPOffs;
147 std::string Expansion;
148 while (PPOffs < RawOffs) {
149 Expansion += ' ' + PP.getSpelling(PPTok);
150 PP.Lex(PPTok);
151 PPLoc = SM.getLogicalLoc(PPTok.getLocation());
152 PPOffs = SM.getFullFilePos(PPLoc);
153 }
154 Expansion += ' ';
155 RB.InsertTextBefore(InsertPos, &Expansion[0], Expansion.size());
156 }
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000157
158 // Create the output file.
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000159 std::ostream *OutFile;
160 if (OutFileName == "-") {
161 OutFile = llvm::cout.stream();
162 } else if (!OutFileName.empty()) {
163 OutFile = new std::ofstream(OutFileName.c_str(),
164 std::ios_base::binary|std::ios_base::out);
165 } else if (InFileName == "-") {
166 OutFile = llvm::cout.stream();
167 } else {
168 llvm::sys::Path Path(InFileName);
169 Path.eraseSuffix();
170 Path.appendSuffix("cpp");
171 OutFile = new std::ofstream(Path.toString().c_str(),
172 std::ios_base::binary|std::ios_base::out);
173 }
174
175 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
176 // we are done.
177 if (const RewriteBuffer *RewriteBuf =
Chris Lattnerec921562008-05-10 00:02:33 +0000178 Rewrite.getRewriteBufferFor(SM.getMainFileID())) {
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000179 //printf("Changed:\n");
180 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
181 } else {
182 fprintf(stderr, "No changes\n");
183 }
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000184}