blob: 4ffb2978db7a334203b192f0ebe2a9442312e244 [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
Eli Friedmanb09f6e12009-05-19 04:14:29 +000015#include "clang/Frontend/Utils.h"
Chris Lattnerb57e3d42008-05-08 06:52:13 +000016#include "clang/Rewrite/Rewriter.h"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Basic/SourceManager.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattnerb57e3d42008-05-08 06:52:13 +000020#include "llvm/System/Path.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000021#include "llvm/ADT/OwningPtr.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000022#include <cstdio>
23
Chris Lattnerb57e3d42008-05-08 06:52:13 +000024using namespace clang;
25
Chris Lattnerec921562008-05-10 00:02:33 +000026/// isSameToken - Return true if the two specified tokens start have the same
27/// content.
28static bool isSameToken(Token &RawTok, Token &PPTok) {
Chris Lattner8ea78e62008-05-31 22:01:01 +000029 // If two tokens have the same kind and the same identifier info, they are
30 // obviously the same.
31 if (PPTok.getKind() == RawTok.getKind() &&
32 PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
Chris Lattnerec921562008-05-10 00:02:33 +000033 return true;
Mike Stump1eb44332009-09-09 15:08:12 +000034
Chris Lattner8ea78e62008-05-31 22:01:01 +000035 // Otherwise, if they are different but have the same identifier info, they
36 // are also considered to be the same. This allows keywords and raw lexed
37 // identifiers with the same name to be treated the same.
Chris Lattnerec921562008-05-10 00:02:33 +000038 if (PPTok.getIdentifierInfo() &&
39 PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
40 return true;
Mike Stump1eb44332009-09-09 15:08:12 +000041
Chris Lattnerec921562008-05-10 00:02:33 +000042 return false;
43}
44
Chris Lattner0d76fcd2008-07-25 16:29:12 +000045
46/// GetNextRawTok - Return the next raw token in the stream, skipping over
47/// comments if ReturnComment is false.
48static const Token &GetNextRawTok(const std::vector<Token> &RawTokens,
49 unsigned &CurTok, bool ReturnComment) {
50 assert(CurTok < RawTokens.size() && "Overran eof!");
Mike Stump1eb44332009-09-09 15:08:12 +000051
Chris Lattner0d76fcd2008-07-25 16:29:12 +000052 // If the client doesn't want comments and we have one, skip it.
53 if (!ReturnComment && RawTokens[CurTok].is(tok::comment))
54 ++CurTok;
Mike Stump1eb44332009-09-09 15:08:12 +000055
Chris Lattner0d76fcd2008-07-25 16:29:12 +000056 return RawTokens[CurTok++];
Chris Lattnerec921562008-05-10 00:02:33 +000057}
58
Chris Lattner0d76fcd2008-07-25 16:29:12 +000059
60/// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into
61/// the specified vector.
62static void LexRawTokensFromMainFile(Preprocessor &PP,
63 std::vector<Token> &RawTokens) {
64 SourceManager &SM = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner0d76fcd2008-07-25 16:29:12 +000066 // Create a lexer to lex all the tokens of the main file in raw mode. Even
67 // though it is in raw mode, it will not return comments.
Chris Lattner6e290142009-11-30 04:18:44 +000068 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
69 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
Chris Lattner0d76fcd2008-07-25 16:29:12 +000070
71 // Switch on comment lexing because we really do want them.
72 RawLex.SetCommentRetentionState(true);
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner0d76fcd2008-07-25 16:29:12 +000074 Token RawTok;
75 do {
Chris Lattner590f0cc2008-10-12 01:15:46 +000076 RawLex.LexFromRawLexer(RawTok);
Mike Stump1eb44332009-09-09 15:08:12 +000077
Chris Lattner0d76fcd2008-07-25 16:29:12 +000078 // If we have an identifier with no identifier info for our raw token, look
79 // up the indentifier info. This is important for equality comparison of
80 // identifier tokens.
81 if (RawTok.is(tok::identifier) && !RawTok.getIdentifierInfo())
Kovarththanan Rajaratnam65cc1e82010-03-13 08:53:33 +000082 PP.LookUpIdentifierInfo(RawTok);
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattner0d76fcd2008-07-25 16:29:12 +000084 RawTokens.push_back(RawTok);
85 } while (RawTok.isNot(tok::eof));
86}
87
88
Chris Lattnerb57e3d42008-05-08 06:52:13 +000089/// RewriteMacrosInInput - Implement -rewrite-macros mode.
Eli Friedmanf54fce82009-05-19 01:02:07 +000090void clang::RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream *OS) {
Chris Lattnerb57e3d42008-05-08 06:52:13 +000091 SourceManager &SM = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattnerb57e3d42008-05-08 06:52:13 +000093 Rewriter Rewrite;
Chris Lattner2c78b872009-04-14 23:22:57 +000094 Rewrite.setSourceMgr(SM, PP.getLangOptions());
Chris Lattnerec921562008-05-10 00:02:33 +000095 RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID());
Chris Lattnerb57e3d42008-05-08 06:52:13 +000096
Chris Lattner0d76fcd2008-07-25 16:29:12 +000097 std::vector<Token> RawTokens;
98 LexRawTokensFromMainFile(PP, RawTokens);
99 unsigned CurRawTok = 0;
100 Token RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
101
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattnerec921562008-05-10 00:02:33 +0000103 // Get the first preprocessing token.
Douglas Gregordbf8ee62010-03-17 15:44:30 +0000104 if (PP.EnterMainSourceFile())
105 return;
Chris Lattnerec921562008-05-10 00:02:33 +0000106 Token PPTok;
107 PP.Lex(PPTok);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattnerec921562008-05-10 00:02:33 +0000109 // Preprocess the input file in parallel with raw lexing the main file. Ignore
110 // all tokens that are preprocessed from a file other than the main file (e.g.
Ted Kremeneka9bc2d92008-08-14 03:45:07 +0000111 // a header). If we see tokens that are in the preprocessed file but not the
Chris Lattnerec921562008-05-10 00:02:33 +0000112 // lexed file, we have a macro expansion. If we see tokens in the lexed file
113 // that aren't in the preprocessed view, we have macros that expand to no
114 // tokens, or macro arguments etc.
115 while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000116 SourceLocation PPLoc = SM.getInstantiationLoc(PPTok.getLocation());
Chris Lattnerec921562008-05-10 00:02:33 +0000117
118 // If PPTok is from a different source file, ignore it.
119 if (!SM.isFromMainFile(PPLoc)) {
120 PP.Lex(PPTok);
121 continue;
122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Chris Lattnerec921562008-05-10 00:02:33 +0000124 // If the raw file hits a preprocessor directive, they will be extra tokens
Chris Lattnera59e0502008-07-25 16:37:06 +0000125 // in the raw file that don't exist in the preprocsesed file. However, we
126 // choose to preserve them in the output file and otherwise handle them
127 // specially.
Chris Lattnerec921562008-05-10 00:02:33 +0000128 if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) {
Chris Lattnera59e0502008-07-25 16:37:06 +0000129 // If this is a #warning directive or #pragma mark (GNU extensions),
130 // comment the line out.
131 if (RawTokens[CurRawTok].is(tok::identifier)) {
132 const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo();
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000133 if (II->getName() == "warning") {
Chris Lattnera59e0502008-07-25 16:37:06 +0000134 // Comment out #warning.
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000135 RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000136 } else if (II->getName() == "pragma" &&
Chris Lattnera59e0502008-07-25 16:37:06 +0000137 RawTokens[CurRawTok+1].is(tok::identifier) &&
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000138 (RawTokens[CurRawTok+1].getIdentifierInfo()->getName() ==
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +0000139 "mark")) {
Chris Lattnera59e0502008-07-25 16:37:06 +0000140 // Comment out #pragma mark.
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000141 RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
Chris Lattnera59e0502008-07-25 16:37:06 +0000142 }
143 }
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Chris Lattnera59e0502008-07-25 16:37:06 +0000145 // Otherwise, if this is a #include or some other directive, just leave it
146 // in the file by skipping over the line.
Chris Lattner0d76fcd2008-07-25 16:29:12 +0000147 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattnerec921562008-05-10 00:02:33 +0000148 while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof))
Chris Lattner0d76fcd2008-07-25 16:29:12 +0000149 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattnerec921562008-05-10 00:02:33 +0000150 continue;
151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Chris Lattnerec921562008-05-10 00:02:33 +0000153 // Okay, both tokens are from the same file. Get their offsets from the
154 // start of the file.
Chris Lattner52c29082009-01-27 06:27:13 +0000155 unsigned PPOffs = SM.getFileOffset(PPLoc);
156 unsigned RawOffs = SM.getFileOffset(RawTok.getLocation());
Chris Lattnerec921562008-05-10 00:02:33 +0000157
158 // If the offsets are the same and the token kind is the same, ignore them.
159 if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) {
Chris Lattner0d76fcd2008-07-25 16:29:12 +0000160 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattnerec921562008-05-10 00:02:33 +0000161 PP.Lex(PPTok);
162 continue;
163 }
164
165 // If the PP token is farther along than the raw token, something was
166 // deleted. Comment out the raw token.
167 if (RawOffs <= PPOffs) {
168 // Comment out a whole run of tokens instead of bracketing each one with
Chris Lattner1a787352008-06-03 06:10:17 +0000169 // comments. Add a leading space if RawTok didn't have one.
170 bool HasSpace = RawTok.hasLeadingSpace();
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000171 RB.InsertTextAfter(RawOffs, " /*"+HasSpace);
Chris Lattnerec921562008-05-10 00:02:33 +0000172 unsigned EndPos;
173
Chris Lattnerec921562008-05-10 00:02:33 +0000174 do {
175 EndPos = RawOffs+RawTok.getLength();
176
Chris Lattner0d76fcd2008-07-25 16:29:12 +0000177 RawTok = GetNextRawTok(RawTokens, CurRawTok, true);
Chris Lattner52c29082009-01-27 06:27:13 +0000178 RawOffs = SM.getFileOffset(RawTok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattnerec921562008-05-10 00:02:33 +0000180 if (RawTok.is(tok::comment)) {
Chris Lattnerec921562008-05-10 00:02:33 +0000181 // Skip past the comment.
Chris Lattner0d76fcd2008-07-25 16:29:12 +0000182 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattnerec921562008-05-10 00:02:33 +0000183 break;
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Chris Lattnerec921562008-05-10 00:02:33 +0000186 } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() &&
187 (PPOffs != RawOffs || !isSameToken(RawTok, PPTok)));
Chris Lattnerec921562008-05-10 00:02:33 +0000188
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000189 RB.InsertTextBefore(EndPos, "*/");
Chris Lattnerec921562008-05-10 00:02:33 +0000190 continue;
191 }
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattnerec921562008-05-10 00:02:33 +0000193 // Otherwise, there was a replacement an expansion. Insert the new token
194 // in the output buffer. Insert the whole run of new tokens at once to get
195 // them in the right order.
196 unsigned InsertPos = PPOffs;
197 std::string Expansion;
198 while (PPOffs < RawOffs) {
199 Expansion += ' ' + PP.getSpelling(PPTok);
200 PP.Lex(PPTok);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000201 PPLoc = SM.getInstantiationLoc(PPTok.getLocation());
Chris Lattner52c29082009-01-27 06:27:13 +0000202 PPOffs = SM.getFileOffset(PPLoc);
Chris Lattnerec921562008-05-10 00:02:33 +0000203 }
204 Expansion += ' ';
Daniel Dunbard7407dc2009-08-19 19:10:30 +0000205 RB.InsertTextBefore(InsertPos, Expansion);
Chris Lattnerec921562008-05-10 00:02:33 +0000206 }
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000207
208 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
209 // we are done.
Mike Stump1eb44332009-09-09 15:08:12 +0000210 if (const RewriteBuffer *RewriteBuf =
Chris Lattnerec921562008-05-10 00:02:33 +0000211 Rewrite.getRewriteBufferFor(SM.getMainFileID())) {
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000212 //printf("Changed:\n");
Eli Friedmanf54fce82009-05-19 01:02:07 +0000213 *OS << std::string(RewriteBuf->begin(), RewriteBuf->end());
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000214 } else {
215 fprintf(stderr, "No changes\n");
216 }
Eli Friedmanf54fce82009-05-19 01:02:07 +0000217 OS->flush();
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000218}