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