blob: 5ef4892e5bc148e2b396d0d162c9add18b10e491 [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"
Chris Lattnerb57e3d42008-05-08 06:52:13 +000019#include "llvm/Support/Streams.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattnerb57e3d42008-05-08 06:52:13 +000021#include "llvm/System/Path.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000022#include "llvm/ADT/OwningPtr.h"
Chris Lattnerb57e3d42008-05-08 06:52:13 +000023using namespace clang;
24
Chris Lattnerec921562008-05-10 00:02:33 +000025/// isSameToken - Return true if the two specified tokens start have the same
26/// content.
27static bool isSameToken(Token &RawTok, Token &PPTok) {
Chris Lattner8ea78e62008-05-31 22:01:01 +000028 // If two tokens have the same kind and the same identifier info, they are
29 // obviously the same.
30 if (PPTok.getKind() == RawTok.getKind() &&
31 PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
Chris Lattnerec921562008-05-10 00:02:33 +000032 return true;
33
Chris Lattner8ea78e62008-05-31 22:01:01 +000034 // Otherwise, if they are different but have the same identifier info, they
35 // are also considered to be the same. This allows keywords and raw lexed
36 // identifiers with the same name to be treated the same.
Chris Lattnerec921562008-05-10 00:02:33 +000037 if (PPTok.getIdentifierInfo() &&
38 PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
39 return true;
40
41 return false;
42}
43
Chris Lattner0d76fcd2008-07-25 16:29:12 +000044
45/// GetNextRawTok - Return the next raw token in the stream, skipping over
46/// comments if ReturnComment is false.
47static const Token &GetNextRawTok(const std::vector<Token> &RawTokens,
48 unsigned &CurTok, bool ReturnComment) {
49 assert(CurTok < RawTokens.size() && "Overran eof!");
Chris Lattnerec921562008-05-10 00:02:33 +000050
Chris Lattner0d76fcd2008-07-25 16:29:12 +000051 // If the client doesn't want comments and we have one, skip it.
52 if (!ReturnComment && RawTokens[CurTok].is(tok::comment))
53 ++CurTok;
54
55 return RawTokens[CurTok++];
Chris Lattnerec921562008-05-10 00:02:33 +000056}
57
Chris Lattner0d76fcd2008-07-25 16:29:12 +000058
59/// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into
60/// the specified vector.
61static void LexRawTokensFromMainFile(Preprocessor &PP,
62 std::vector<Token> &RawTokens) {
63 SourceManager &SM = PP.getSourceManager();
Chris Lattner0d76fcd2008-07-25 16:29:12 +000064
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.
Chris Lattner4448a012009-01-17 07:41:36 +000067 Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions());
Chris Lattner0d76fcd2008-07-25 16:29:12 +000068
69 // Switch on comment lexing because we really do want them.
70 RawLex.SetCommentRetentionState(true);
71
72 Token RawTok;
73 do {
Chris Lattner590f0cc2008-10-12 01:15:46 +000074 RawLex.LexFromRawLexer(RawTok);
Chris Lattner0d76fcd2008-07-25 16:29:12 +000075
76 // If we have an identifier with no identifier info for our raw token, look
77 // up the indentifier info. This is important for equality comparison of
78 // identifier tokens.
79 if (RawTok.is(tok::identifier) && !RawTok.getIdentifierInfo())
80 RawTok.setIdentifierInfo(PP.LookUpIdentifierInfo(RawTok));
81
82 RawTokens.push_back(RawTok);
83 } while (RawTok.isNot(tok::eof));
84}
85
86
Chris Lattnerb57e3d42008-05-08 06:52:13 +000087/// RewriteMacrosInInput - Implement -rewrite-macros mode.
Eli Friedmanf54fce82009-05-19 01:02:07 +000088void clang::RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream *OS) {
Chris Lattnerb57e3d42008-05-08 06:52:13 +000089 SourceManager &SM = PP.getSourceManager();
90
91 Rewriter Rewrite;
Chris Lattner2c78b872009-04-14 23:22:57 +000092 Rewrite.setSourceMgr(SM, PP.getLangOptions());
Chris Lattnerec921562008-05-10 00:02:33 +000093 RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID());
Chris Lattnerb57e3d42008-05-08 06:52:13 +000094
Chris Lattner0d76fcd2008-07-25 16:29:12 +000095 std::vector<Token> RawTokens;
96 LexRawTokensFromMainFile(PP, RawTokens);
97 unsigned CurRawTok = 0;
98 Token RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
99
Chris Lattnerec921562008-05-10 00:02:33 +0000100
101 // Get the first preprocessing token.
102 PP.EnterMainSourceFile();
103 Token PPTok;
104 PP.Lex(PPTok);
105
106 // Preprocess the input file in parallel with raw lexing the main file. Ignore
107 // all tokens that are preprocessed from a file other than the main file (e.g.
Ted Kremeneka9bc2d92008-08-14 03:45:07 +0000108 // a header). If we see tokens that are in the preprocessed file but not the
Chris Lattnerec921562008-05-10 00:02:33 +0000109 // lexed file, we have a macro expansion. If we see tokens in the lexed file
110 // that aren't in the preprocessed view, we have macros that expand to no
111 // tokens, or macro arguments etc.
112 while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000113 SourceLocation PPLoc = SM.getInstantiationLoc(PPTok.getLocation());
Chris Lattnerec921562008-05-10 00:02:33 +0000114
115 // If PPTok is from a different source file, ignore it.
116 if (!SM.isFromMainFile(PPLoc)) {
117 PP.Lex(PPTok);
118 continue;
119 }
120
121 // If the raw file hits a preprocessor directive, they will be extra tokens
Chris Lattnera59e0502008-07-25 16:37:06 +0000122 // in the raw file that don't exist in the preprocsesed file. However, we
123 // choose to preserve them in the output file and otherwise handle them
124 // specially.
Chris Lattnerec921562008-05-10 00:02:33 +0000125 if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) {
Chris Lattnera59e0502008-07-25 16:37:06 +0000126 // If this is a #warning directive or #pragma mark (GNU extensions),
127 // comment the line out.
128 if (RawTokens[CurRawTok].is(tok::identifier)) {
129 const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo();
130 if (!strcmp(II->getName(), "warning")) {
131 // Comment out #warning.
Chris Lattner52c29082009-01-27 06:27:13 +0000132 RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//", 2);
Chris Lattnera59e0502008-07-25 16:37:06 +0000133 } else if (!strcmp(II->getName(), "pragma") &&
134 RawTokens[CurRawTok+1].is(tok::identifier) &&
135 !strcmp(RawTokens[CurRawTok+1].getIdentifierInfo()->getName(),
136 "mark")){
137 // Comment out #pragma mark.
Chris Lattner52c29082009-01-27 06:27:13 +0000138 RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//", 2);
Chris Lattnera59e0502008-07-25 16:37:06 +0000139 }
140 }
141
142 // Otherwise, if this is a #include or some other directive, just leave it
143 // in the file by skipping over the line.
Chris Lattner0d76fcd2008-07-25 16:29:12 +0000144 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattnerec921562008-05-10 00:02:33 +0000145 while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof))
Chris Lattner0d76fcd2008-07-25 16:29:12 +0000146 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattnerec921562008-05-10 00:02:33 +0000147 continue;
148 }
149
150 // Okay, both tokens are from the same file. Get their offsets from the
151 // start of the file.
Chris Lattner52c29082009-01-27 06:27:13 +0000152 unsigned PPOffs = SM.getFileOffset(PPLoc);
153 unsigned RawOffs = SM.getFileOffset(RawTok.getLocation());
Chris Lattnerec921562008-05-10 00:02:33 +0000154
155 // If the offsets are the same and the token kind is the same, ignore them.
156 if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) {
Chris Lattner0d76fcd2008-07-25 16:29:12 +0000157 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattnerec921562008-05-10 00:02:33 +0000158 PP.Lex(PPTok);
159 continue;
160 }
161
162 // If the PP token is farther along than the raw token, something was
163 // deleted. Comment out the raw token.
164 if (RawOffs <= PPOffs) {
165 // Comment out a whole run of tokens instead of bracketing each one with
Chris Lattner1a787352008-06-03 06:10:17 +0000166 // comments. Add a leading space if RawTok didn't have one.
167 bool HasSpace = RawTok.hasLeadingSpace();
168 RB.InsertTextAfter(RawOffs, " /*"+HasSpace, 2+!HasSpace);
Chris Lattnerec921562008-05-10 00:02:33 +0000169 unsigned EndPos;
170
Chris Lattnerec921562008-05-10 00:02:33 +0000171 do {
172 EndPos = RawOffs+RawTok.getLength();
173
Chris Lattner0d76fcd2008-07-25 16:29:12 +0000174 RawTok = GetNextRawTok(RawTokens, CurRawTok, true);
Chris Lattner52c29082009-01-27 06:27:13 +0000175 RawOffs = SM.getFileOffset(RawTok.getLocation());
Chris Lattnerec921562008-05-10 00:02:33 +0000176
177 if (RawTok.is(tok::comment)) {
Chris Lattnerec921562008-05-10 00:02:33 +0000178 // Skip past the comment.
Chris Lattner0d76fcd2008-07-25 16:29:12 +0000179 RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
Chris Lattnerec921562008-05-10 00:02:33 +0000180 break;
181 }
182
183 } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() &&
184 (PPOffs != RawOffs || !isSameToken(RawTok, PPTok)));
Chris Lattnerec921562008-05-10 00:02:33 +0000185
186 RB.InsertTextBefore(EndPos, "*/", 2);
187 continue;
188 }
189
190 // Otherwise, there was a replacement an expansion. Insert the new token
191 // in the output buffer. Insert the whole run of new tokens at once to get
192 // them in the right order.
193 unsigned InsertPos = PPOffs;
194 std::string Expansion;
195 while (PPOffs < RawOffs) {
196 Expansion += ' ' + PP.getSpelling(PPTok);
197 PP.Lex(PPTok);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000198 PPLoc = SM.getInstantiationLoc(PPTok.getLocation());
Chris Lattner52c29082009-01-27 06:27:13 +0000199 PPOffs = SM.getFileOffset(PPLoc);
Chris Lattnerec921562008-05-10 00:02:33 +0000200 }
201 Expansion += ' ';
202 RB.InsertTextBefore(InsertPos, &Expansion[0], Expansion.size());
203 }
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000204
205 // Get the buffer corresponding to MainFileID. If we haven't changed it, then
206 // we are done.
207 if (const RewriteBuffer *RewriteBuf =
Chris Lattnerec921562008-05-10 00:02:33 +0000208 Rewrite.getRewriteBufferFor(SM.getMainFileID())) {
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000209 //printf("Changed:\n");
Eli Friedmanf54fce82009-05-19 01:02:07 +0000210 *OS << std::string(RewriteBuf->begin(), RewriteBuf->end());
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000211 } else {
212 fprintf(stderr, "No changes\n");
213 }
Eli Friedmanf54fce82009-05-19 01:02:07 +0000214 OS->flush();
Chris Lattnerb57e3d42008-05-08 06:52:13 +0000215}