blob: 5db2170d63e86c504d4011ac7c7a4fc0fd9ae685 [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
Eli Friedman7e1d6372009-05-19 04:14:29 +000015#include "clang/Frontend/Utils.h"
Chris Lattner1665a9f2008-05-08 06:52:13 +000016#include "clang/Rewrite/Rewriter.h"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Basic/SourceManager.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattner1665a9f2008-05-08 06:52:13 +000020#include "llvm/System/Path.h"
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000021#include "llvm/ADT/OwningPtr.h"
Chris Lattner1665a9f2008-05-08 06:52:13 +000022using namespace clang;
23
Chris Lattner3a8c36d2008-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 Lattner3c11e952008-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 Lattner3a8c36d2008-05-10 00:02:33 +000031 return true;
32
Chris Lattner3c11e952008-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 Lattner3a8c36d2008-05-10 00:02:33 +000036 if (PPTok.getIdentifierInfo() &&
37 PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
38 return true;
39
40 return false;
41}
42
Chris Lattnerb9326c52008-07-25 16:29:12 +000043
44/// GetNextRawTok - Return the next raw token in the stream, skipping over
45/// comments if ReturnComment is false.
46static const Token &GetNextRawTok(const std::vector<Token> &RawTokens,
47 unsigned &CurTok, bool ReturnComment) {
48 assert(CurTok < RawTokens.size() && "Overran eof!");
Chris Lattner3a8c36d2008-05-10 00:02:33 +000049
Chris Lattnerb9326c52008-07-25 16:29:12 +000050 // If the client doesn't want comments and we have one, skip it.
51 if (!ReturnComment && RawTokens[CurTok].is(tok::comment))
52 ++CurTok;
53
54 return RawTokens[CurTok++];
Chris Lattner3a8c36d2008-05-10 00:02:33 +000055}
56
Chris Lattnerb9326c52008-07-25 16:29:12 +000057
58/// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into
59/// the specified vector.
60static void LexRawTokensFromMainFile(Preprocessor &PP,
61 std::vector<Token> &RawTokens) {
62 SourceManager &SM = PP.getSourceManager();
Chris Lattnerb9326c52008-07-25 16:29:12 +000063
64 // Create a lexer to lex all the tokens of the main file in raw mode. Even
65 // though it is in raw mode, it will not return comments.
Chris Lattner3a61c052009-01-17 07:41:36 +000066 Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions());
Chris Lattnerb9326c52008-07-25 16:29:12 +000067
68 // Switch on comment lexing because we really do want them.
69 RawLex.SetCommentRetentionState(true);
70
71 Token RawTok;
72 do {
Chris Lattner0b5892e2008-10-12 01:15:46 +000073 RawLex.LexFromRawLexer(RawTok);
Chris Lattnerb9326c52008-07-25 16:29:12 +000074
75 // If we have an identifier with no identifier info for our raw token, look
76 // up the indentifier info. This is important for equality comparison of
77 // identifier tokens.
78 if (RawTok.is(tok::identifier) && !RawTok.getIdentifierInfo())
79 RawTok.setIdentifierInfo(PP.LookUpIdentifierInfo(RawTok));
80
81 RawTokens.push_back(RawTok);
82 } while (RawTok.isNot(tok::eof));
83}
84
85
Chris Lattner1665a9f2008-05-08 06:52:13 +000086/// RewriteMacrosInInput - Implement -rewrite-macros mode.
Eli Friedman4ae15b92009-05-19 01:02:07 +000087void clang::RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream *OS) {
Chris Lattner1665a9f2008-05-08 06:52:13 +000088 SourceManager &SM = PP.getSourceManager();
89
90 Rewriter Rewrite;
Chris Lattnere1be6022009-04-14 23:22:57 +000091 Rewrite.setSourceMgr(SM, PP.getLangOptions());
Chris Lattner3a8c36d2008-05-10 00:02:33 +000092 RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID());
Chris Lattner1665a9f2008-05-08 06:52:13 +000093
Chris Lattnerb9326c52008-07-25 16:29:12 +000094 std::vector<Token> RawTokens;
95 LexRawTokensFromMainFile(PP, RawTokens);
96 unsigned CurRawTok = 0;
97 Token RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
98
Chris Lattner3a8c36d2008-05-10 00:02:33 +000099
100 // Get the first preprocessing token.
101 PP.EnterMainSourceFile();
102 Token PPTok;
103 PP.Lex(PPTok);
104
105 // Preprocess the input file in parallel with raw lexing the main file. Ignore
106 // all tokens that are preprocessed from a file other than the main file (e.g.
Ted Kremenek8fb620a2008-08-14 03:45:07 +0000107 // a header). If we see tokens that are in the preprocessed file but not the
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000108 // lexed file, we have a macro expansion. If we see tokens in the lexed file
109 // that aren't in the preprocessed view, we have macros that expand to no
110 // tokens, or macro arguments etc.
111 while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) {
Chris Lattner18c8dc02009-01-16 07:36:28 +0000112 SourceLocation PPLoc = SM.getInstantiationLoc(PPTok.getLocation());
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000113
114 // If PPTok is from a different source file, ignore it.
115 if (!SM.isFromMainFile(PPLoc)) {
116 PP.Lex(PPTok);
117 continue;
118 }
119
120 // If the raw file hits a preprocessor directive, they will be extra tokens
Chris Lattner038d3222008-07-25 16:37:06 +0000121 // in the raw file that don't exist in the preprocsesed file. However, we
122 // choose to preserve them in the output file and otherwise handle them
123 // specially.
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000124 if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) {
Chris Lattner038d3222008-07-25 16:37:06 +0000125 // If this is a #warning directive or #pragma mark (GNU extensions),
126 // comment the line out.
127 if (RawTokens[CurRawTok].is(tok::identifier)) {
128 const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo();
129 if (!strcmp(II->getName(), "warning")) {
130 // Comment out #warning.
Daniel Dunbar60987c92009-08-19 19:10:30 +0000131 RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
Chris Lattner038d3222008-07-25 16:37:06 +0000132 } else if (!strcmp(II->getName(), "pragma") &&
133 RawTokens[CurRawTok+1].is(tok::identifier) &&
134 !strcmp(RawTokens[CurRawTok+1].getIdentifierInfo()->getName(),
135 "mark")){
136 // Comment out #pragma mark.
Daniel Dunbar60987c92009-08-19 19:10:30 +0000137 RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
Chris Lattner038d3222008-07-25 16:37:06 +0000138 }
139 }
140
141 // Otherwise, if this is a #include or some other directive, just leave it
142 // in the file by skipping over the line.
Chris Lattnerb9326c52008-07-25 16:29:12 +0000143 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000144 while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof))
Chris Lattnerb9326c52008-07-25 16:29:12 +0000145 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000146 continue;
147 }
148
149 // Okay, both tokens are from the same file. Get their offsets from the
150 // start of the file.
Chris Lattnercbb2bd42009-01-27 06:27:13 +0000151 unsigned PPOffs = SM.getFileOffset(PPLoc);
152 unsigned RawOffs = SM.getFileOffset(RawTok.getLocation());
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000153
154 // If the offsets are the same and the token kind is the same, ignore them.
155 if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) {
Chris Lattnerb9326c52008-07-25 16:29:12 +0000156 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000157 PP.Lex(PPTok);
158 continue;
159 }
160
161 // If the PP token is farther along than the raw token, something was
162 // deleted. Comment out the raw token.
163 if (RawOffs <= PPOffs) {
164 // Comment out a whole run of tokens instead of bracketing each one with
Chris Lattner460d2372008-06-03 06:10:17 +0000165 // comments. Add a leading space if RawTok didn't have one.
166 bool HasSpace = RawTok.hasLeadingSpace();
Daniel Dunbar60987c92009-08-19 19:10:30 +0000167 RB.InsertTextAfter(RawOffs, " /*"+HasSpace);
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000168 unsigned EndPos;
169
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000170 do {
171 EndPos = RawOffs+RawTok.getLength();
172
Chris Lattnerb9326c52008-07-25 16:29:12 +0000173 RawTok = GetNextRawTok(RawTokens, CurRawTok, true);
Chris Lattnercbb2bd42009-01-27 06:27:13 +0000174 RawOffs = SM.getFileOffset(RawTok.getLocation());
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000175
176 if (RawTok.is(tok::comment)) {
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000177 // Skip past the comment.
Chris Lattnerb9326c52008-07-25 16:29:12 +0000178 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000179 break;
180 }
181
182 } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() &&
183 (PPOffs != RawOffs || !isSameToken(RawTok, PPTok)));
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000184
Daniel Dunbar60987c92009-08-19 19:10:30 +0000185 RB.InsertTextBefore(EndPos, "*/");
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000186 continue;
187 }
188
189 // Otherwise, there was a replacement an expansion. Insert the new token
190 // in the output buffer. Insert the whole run of new tokens at once to get
191 // them in the right order.
192 unsigned InsertPos = PPOffs;
193 std::string Expansion;
194 while (PPOffs < RawOffs) {
195 Expansion += ' ' + PP.getSpelling(PPTok);
196 PP.Lex(PPTok);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000197 PPLoc = SM.getInstantiationLoc(PPTok.getLocation());
Chris Lattnercbb2bd42009-01-27 06:27:13 +0000198 PPOffs = SM.getFileOffset(PPLoc);
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000199 }
200 Expansion += ' ';
Daniel Dunbar60987c92009-08-19 19:10:30 +0000201 RB.InsertTextBefore(InsertPos, Expansion);
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000202 }
Chris Lattner1665a9f2008-05-08 06:52:13 +0000203
204 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
205 // we are done.
206 if (const RewriteBuffer *RewriteBuf =
Chris Lattner3a8c36d2008-05-10 00:02:33 +0000207 Rewrite.getRewriteBufferFor(SM.getMainFileID())) {
Chris Lattner1665a9f2008-05-08 06:52:13 +0000208 //printf("Changed:\n");
Eli Friedman4ae15b92009-05-19 01:02:07 +0000209 *OS << std::string(RewriteBuf->begin(), RewriteBuf->end());
Chris Lattner1665a9f2008-05-08 06:52:13 +0000210 } else {
211 fprintf(stderr, "No changes\n");
212 }
Eli Friedman4ae15b92009-05-19 01:02:07 +0000213 OS->flush();
Chris Lattner1665a9f2008-05-08 06:52:13 +0000214}