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