1*0a6a1f1dSLionel Sambuc //===--- RewriteMacros.cpp - Rewrite macros into their expansions ---------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // This code rewrites macro invocations into their expansions. This gives you
11*0a6a1f1dSLionel Sambuc // a macro expanded file that retains comments and #includes.
12*0a6a1f1dSLionel Sambuc //
13*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
14*0a6a1f1dSLionel Sambuc
15*0a6a1f1dSLionel Sambuc #include "clang/Rewrite/Frontend/Rewriters.h"
16*0a6a1f1dSLionel Sambuc #include "clang/Basic/SourceManager.h"
17*0a6a1f1dSLionel Sambuc #include "clang/Lex/Preprocessor.h"
18*0a6a1f1dSLionel Sambuc #include "clang/Rewrite/Core/Rewriter.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/Support/Path.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/Support/raw_ostream.h"
21*0a6a1f1dSLionel Sambuc #include <cstdio>
22*0a6a1f1dSLionel Sambuc #include <memory>
23*0a6a1f1dSLionel Sambuc
24*0a6a1f1dSLionel Sambuc using namespace clang;
25*0a6a1f1dSLionel Sambuc
26*0a6a1f1dSLionel Sambuc /// isSameToken - Return true if the two specified tokens start have the same
27*0a6a1f1dSLionel Sambuc /// content.
isSameToken(Token & RawTok,Token & PPTok)28*0a6a1f1dSLionel Sambuc static bool isSameToken(Token &RawTok, Token &PPTok) {
29*0a6a1f1dSLionel Sambuc // If two tokens have the same kind and the same identifier info, they are
30*0a6a1f1dSLionel Sambuc // obviously the same.
31*0a6a1f1dSLionel Sambuc if (PPTok.getKind() == RawTok.getKind() &&
32*0a6a1f1dSLionel Sambuc PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
33*0a6a1f1dSLionel Sambuc return true;
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc // Otherwise, if they are different but have the same identifier info, they
36*0a6a1f1dSLionel Sambuc // are also considered to be the same. This allows keywords and raw lexed
37*0a6a1f1dSLionel Sambuc // identifiers with the same name to be treated the same.
38*0a6a1f1dSLionel Sambuc if (PPTok.getIdentifierInfo() &&
39*0a6a1f1dSLionel Sambuc PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
40*0a6a1f1dSLionel Sambuc return true;
41*0a6a1f1dSLionel Sambuc
42*0a6a1f1dSLionel Sambuc return false;
43*0a6a1f1dSLionel Sambuc }
44*0a6a1f1dSLionel Sambuc
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuc /// GetNextRawTok - Return the next raw token in the stream, skipping over
47*0a6a1f1dSLionel Sambuc /// comments if ReturnComment is false.
GetNextRawTok(const std::vector<Token> & RawTokens,unsigned & CurTok,bool ReturnComment)48*0a6a1f1dSLionel Sambuc static const Token &GetNextRawTok(const std::vector<Token> &RawTokens,
49*0a6a1f1dSLionel Sambuc unsigned &CurTok, bool ReturnComment) {
50*0a6a1f1dSLionel Sambuc assert(CurTok < RawTokens.size() && "Overran eof!");
51*0a6a1f1dSLionel Sambuc
52*0a6a1f1dSLionel Sambuc // If the client doesn't want comments and we have one, skip it.
53*0a6a1f1dSLionel Sambuc if (!ReturnComment && RawTokens[CurTok].is(tok::comment))
54*0a6a1f1dSLionel Sambuc ++CurTok;
55*0a6a1f1dSLionel Sambuc
56*0a6a1f1dSLionel Sambuc return RawTokens[CurTok++];
57*0a6a1f1dSLionel Sambuc }
58*0a6a1f1dSLionel Sambuc
59*0a6a1f1dSLionel Sambuc
60*0a6a1f1dSLionel Sambuc /// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into
61*0a6a1f1dSLionel Sambuc /// the specified vector.
LexRawTokensFromMainFile(Preprocessor & PP,std::vector<Token> & RawTokens)62*0a6a1f1dSLionel Sambuc static void LexRawTokensFromMainFile(Preprocessor &PP,
63*0a6a1f1dSLionel Sambuc std::vector<Token> &RawTokens) {
64*0a6a1f1dSLionel Sambuc SourceManager &SM = PP.getSourceManager();
65*0a6a1f1dSLionel Sambuc
66*0a6a1f1dSLionel Sambuc // Create a lexer to lex all the tokens of the main file in raw mode. Even
67*0a6a1f1dSLionel Sambuc // though it is in raw mode, it will not return comments.
68*0a6a1f1dSLionel Sambuc const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
69*0a6a1f1dSLionel Sambuc Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
70*0a6a1f1dSLionel Sambuc
71*0a6a1f1dSLionel Sambuc // Switch on comment lexing because we really do want them.
72*0a6a1f1dSLionel Sambuc RawLex.SetCommentRetentionState(true);
73*0a6a1f1dSLionel Sambuc
74*0a6a1f1dSLionel Sambuc Token RawTok;
75*0a6a1f1dSLionel Sambuc do {
76*0a6a1f1dSLionel Sambuc RawLex.LexFromRawLexer(RawTok);
77*0a6a1f1dSLionel Sambuc
78*0a6a1f1dSLionel Sambuc // If we have an identifier with no identifier info for our raw token, look
79*0a6a1f1dSLionel Sambuc // up the indentifier info. This is important for equality comparison of
80*0a6a1f1dSLionel Sambuc // identifier tokens.
81*0a6a1f1dSLionel Sambuc if (RawTok.is(tok::raw_identifier))
82*0a6a1f1dSLionel Sambuc PP.LookUpIdentifierInfo(RawTok);
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc RawTokens.push_back(RawTok);
85*0a6a1f1dSLionel Sambuc } while (RawTok.isNot(tok::eof));
86*0a6a1f1dSLionel Sambuc }
87*0a6a1f1dSLionel Sambuc
88*0a6a1f1dSLionel Sambuc
89*0a6a1f1dSLionel Sambuc /// RewriteMacrosInInput - Implement -rewrite-macros mode.
RewriteMacrosInInput(Preprocessor & PP,raw_ostream * OS)90*0a6a1f1dSLionel Sambuc void clang::RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS) {
91*0a6a1f1dSLionel Sambuc SourceManager &SM = PP.getSourceManager();
92*0a6a1f1dSLionel Sambuc
93*0a6a1f1dSLionel Sambuc Rewriter Rewrite;
94*0a6a1f1dSLionel Sambuc Rewrite.setSourceMgr(SM, PP.getLangOpts());
95*0a6a1f1dSLionel Sambuc RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID());
96*0a6a1f1dSLionel Sambuc
97*0a6a1f1dSLionel Sambuc std::vector<Token> RawTokens;
98*0a6a1f1dSLionel Sambuc LexRawTokensFromMainFile(PP, RawTokens);
99*0a6a1f1dSLionel Sambuc unsigned CurRawTok = 0;
100*0a6a1f1dSLionel Sambuc Token RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
101*0a6a1f1dSLionel Sambuc
102*0a6a1f1dSLionel Sambuc
103*0a6a1f1dSLionel Sambuc // Get the first preprocessing token.
104*0a6a1f1dSLionel Sambuc PP.EnterMainSourceFile();
105*0a6a1f1dSLionel Sambuc Token PPTok;
106*0a6a1f1dSLionel Sambuc PP.Lex(PPTok);
107*0a6a1f1dSLionel Sambuc
108*0a6a1f1dSLionel Sambuc // Preprocess the input file in parallel with raw lexing the main file. Ignore
109*0a6a1f1dSLionel Sambuc // all tokens that are preprocessed from a file other than the main file (e.g.
110*0a6a1f1dSLionel Sambuc // a header). If we see tokens that are in the preprocessed file but not the
111*0a6a1f1dSLionel Sambuc // lexed file, we have a macro expansion. If we see tokens in the lexed file
112*0a6a1f1dSLionel Sambuc // that aren't in the preprocessed view, we have macros that expand to no
113*0a6a1f1dSLionel Sambuc // tokens, or macro arguments etc.
114*0a6a1f1dSLionel Sambuc while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) {
115*0a6a1f1dSLionel Sambuc SourceLocation PPLoc = SM.getExpansionLoc(PPTok.getLocation());
116*0a6a1f1dSLionel Sambuc
117*0a6a1f1dSLionel Sambuc // If PPTok is from a different source file, ignore it.
118*0a6a1f1dSLionel Sambuc if (!SM.isWrittenInMainFile(PPLoc)) {
119*0a6a1f1dSLionel Sambuc PP.Lex(PPTok);
120*0a6a1f1dSLionel Sambuc continue;
121*0a6a1f1dSLionel Sambuc }
122*0a6a1f1dSLionel Sambuc
123*0a6a1f1dSLionel Sambuc // If the raw file hits a preprocessor directive, they will be extra tokens
124*0a6a1f1dSLionel Sambuc // in the raw file that don't exist in the preprocsesed file. However, we
125*0a6a1f1dSLionel Sambuc // choose to preserve them in the output file and otherwise handle them
126*0a6a1f1dSLionel Sambuc // specially.
127*0a6a1f1dSLionel Sambuc if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) {
128*0a6a1f1dSLionel Sambuc // If this is a #warning directive or #pragma mark (GNU extensions),
129*0a6a1f1dSLionel Sambuc // comment the line out.
130*0a6a1f1dSLionel Sambuc if (RawTokens[CurRawTok].is(tok::identifier)) {
131*0a6a1f1dSLionel Sambuc const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo();
132*0a6a1f1dSLionel Sambuc if (II->getName() == "warning") {
133*0a6a1f1dSLionel Sambuc // Comment out #warning.
134*0a6a1f1dSLionel Sambuc RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
135*0a6a1f1dSLionel Sambuc } else if (II->getName() == "pragma" &&
136*0a6a1f1dSLionel Sambuc RawTokens[CurRawTok+1].is(tok::identifier) &&
137*0a6a1f1dSLionel Sambuc (RawTokens[CurRawTok+1].getIdentifierInfo()->getName() ==
138*0a6a1f1dSLionel Sambuc "mark")) {
139*0a6a1f1dSLionel Sambuc // Comment out #pragma mark.
140*0a6a1f1dSLionel Sambuc RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
141*0a6a1f1dSLionel Sambuc }
142*0a6a1f1dSLionel Sambuc }
143*0a6a1f1dSLionel Sambuc
144*0a6a1f1dSLionel Sambuc // Otherwise, if this is a #include or some other directive, just leave it
145*0a6a1f1dSLionel Sambuc // in the file by skipping over the line.
146*0a6a1f1dSLionel Sambuc RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
147*0a6a1f1dSLionel Sambuc while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof))
148*0a6a1f1dSLionel Sambuc RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
149*0a6a1f1dSLionel Sambuc continue;
150*0a6a1f1dSLionel Sambuc }
151*0a6a1f1dSLionel Sambuc
152*0a6a1f1dSLionel Sambuc // Okay, both tokens are from the same file. Get their offsets from the
153*0a6a1f1dSLionel Sambuc // start of the file.
154*0a6a1f1dSLionel Sambuc unsigned PPOffs = SM.getFileOffset(PPLoc);
155*0a6a1f1dSLionel Sambuc unsigned RawOffs = SM.getFileOffset(RawTok.getLocation());
156*0a6a1f1dSLionel Sambuc
157*0a6a1f1dSLionel Sambuc // If the offsets are the same and the token kind is the same, ignore them.
158*0a6a1f1dSLionel Sambuc if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) {
159*0a6a1f1dSLionel Sambuc RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
160*0a6a1f1dSLionel Sambuc PP.Lex(PPTok);
161*0a6a1f1dSLionel Sambuc continue;
162*0a6a1f1dSLionel Sambuc }
163*0a6a1f1dSLionel Sambuc
164*0a6a1f1dSLionel Sambuc // If the PP token is farther along than the raw token, something was
165*0a6a1f1dSLionel Sambuc // deleted. Comment out the raw token.
166*0a6a1f1dSLionel Sambuc if (RawOffs <= PPOffs) {
167*0a6a1f1dSLionel Sambuc // Comment out a whole run of tokens instead of bracketing each one with
168*0a6a1f1dSLionel Sambuc // comments. Add a leading space if RawTok didn't have one.
169*0a6a1f1dSLionel Sambuc bool HasSpace = RawTok.hasLeadingSpace();
170*0a6a1f1dSLionel Sambuc RB.InsertTextAfter(RawOffs, &" /*"[HasSpace]);
171*0a6a1f1dSLionel Sambuc unsigned EndPos;
172*0a6a1f1dSLionel Sambuc
173*0a6a1f1dSLionel Sambuc do {
174*0a6a1f1dSLionel Sambuc EndPos = RawOffs+RawTok.getLength();
175*0a6a1f1dSLionel Sambuc
176*0a6a1f1dSLionel Sambuc RawTok = GetNextRawTok(RawTokens, CurRawTok, true);
177*0a6a1f1dSLionel Sambuc RawOffs = SM.getFileOffset(RawTok.getLocation());
178*0a6a1f1dSLionel Sambuc
179*0a6a1f1dSLionel Sambuc if (RawTok.is(tok::comment)) {
180*0a6a1f1dSLionel Sambuc // Skip past the comment.
181*0a6a1f1dSLionel Sambuc RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
182*0a6a1f1dSLionel Sambuc break;
183*0a6a1f1dSLionel Sambuc }
184*0a6a1f1dSLionel Sambuc
185*0a6a1f1dSLionel Sambuc } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() &&
186*0a6a1f1dSLionel Sambuc (PPOffs != RawOffs || !isSameToken(RawTok, PPTok)));
187*0a6a1f1dSLionel Sambuc
188*0a6a1f1dSLionel Sambuc RB.InsertTextBefore(EndPos, "*/");
189*0a6a1f1dSLionel Sambuc continue;
190*0a6a1f1dSLionel Sambuc }
191*0a6a1f1dSLionel Sambuc
192*0a6a1f1dSLionel Sambuc // Otherwise, there was a replacement an expansion. Insert the new token
193*0a6a1f1dSLionel Sambuc // in the output buffer. Insert the whole run of new tokens at once to get
194*0a6a1f1dSLionel Sambuc // them in the right order.
195*0a6a1f1dSLionel Sambuc unsigned InsertPos = PPOffs;
196*0a6a1f1dSLionel Sambuc std::string Expansion;
197*0a6a1f1dSLionel Sambuc while (PPOffs < RawOffs) {
198*0a6a1f1dSLionel Sambuc Expansion += ' ' + PP.getSpelling(PPTok);
199*0a6a1f1dSLionel Sambuc PP.Lex(PPTok);
200*0a6a1f1dSLionel Sambuc PPLoc = SM.getExpansionLoc(PPTok.getLocation());
201*0a6a1f1dSLionel Sambuc PPOffs = SM.getFileOffset(PPLoc);
202*0a6a1f1dSLionel Sambuc }
203*0a6a1f1dSLionel Sambuc Expansion += ' ';
204*0a6a1f1dSLionel Sambuc RB.InsertTextBefore(InsertPos, Expansion);
205*0a6a1f1dSLionel Sambuc }
206*0a6a1f1dSLionel Sambuc
207*0a6a1f1dSLionel Sambuc // Get the buffer corresponding to MainFileID. If we haven't changed it, then
208*0a6a1f1dSLionel Sambuc // we are done.
209*0a6a1f1dSLionel Sambuc if (const RewriteBuffer *RewriteBuf =
210*0a6a1f1dSLionel Sambuc Rewrite.getRewriteBufferFor(SM.getMainFileID())) {
211*0a6a1f1dSLionel Sambuc //printf("Changed:\n");
212*0a6a1f1dSLionel Sambuc *OS << std::string(RewriteBuf->begin(), RewriteBuf->end());
213*0a6a1f1dSLionel Sambuc } else {
214*0a6a1f1dSLionel Sambuc fprintf(stderr, "No changes\n");
215*0a6a1f1dSLionel Sambuc }
216*0a6a1f1dSLionel Sambuc OS->flush();
217*0a6a1f1dSLionel Sambuc }
218