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