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 <cstdio> |
20 | |
21 | using namespace clang; |
22 | using llvm::RewriteBuffer; |
23 | |
24 | /// isSameToken - Return true if the two specified tokens start have the same |
25 | /// content. |
26 | static bool isSameToken(Token &RawTok, Token &PPTok) { |
27 | // 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()) |
31 | return true; |
32 | |
33 | // 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. |
36 | if (PPTok.getIdentifierInfo() && |
37 | PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo()) |
38 | return true; |
39 | |
40 | return false; |
41 | } |
42 | |
43 | |
44 | /// GetNextRawTok - Return the next raw token in the stream, skipping over |
45 | /// comments if ReturnComment is false. |
46 | static const Token &(const std::vector<Token> &RawTokens, |
47 | unsigned &CurTok, bool ) { |
48 | assert(CurTok < RawTokens.size() && "Overran eof!" ); |
49 | |
50 | // If the client doesn't want comments and we have one, skip it. |
51 | if (!ReturnComment && RawTokens[CurTok].is(K: tok::comment)) |
52 | ++CurTok; |
53 | |
54 | return RawTokens[CurTok++]; |
55 | } |
56 | |
57 | |
58 | /// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into |
59 | /// the specified vector. |
60 | static void LexRawTokensFromMainFile(Preprocessor &PP, |
61 | std::vector<Token> &RawTokens) { |
62 | SourceManager &SM = PP.getSourceManager(); |
63 | |
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. |
66 | llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(FID: SM.getMainFileID()); |
67 | Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts()); |
68 | |
69 | // Switch on comment lexing because we really do want them. |
70 | RawLex.SetCommentRetentionState(true); |
71 | |
72 | Token RawTok; |
73 | do { |
74 | RawLex.LexFromRawLexer(Result&: RawTok); |
75 | |
76 | // If we have an identifier with no identifier info for our raw token, look |
77 | // up the identifier info. This is important for equality comparison of |
78 | // identifier tokens. |
79 | if (RawTok.is(K: tok::raw_identifier)) |
80 | PP.LookUpIdentifierInfo(Identifier&: RawTok); |
81 | |
82 | RawTokens.push_back(x: RawTok); |
83 | } while (RawTok.isNot(K: tok::eof)); |
84 | } |
85 | |
86 | |
87 | /// RewriteMacrosInInput - Implement -rewrite-macros mode. |
88 | void clang::RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS) { |
89 | SourceManager &SM = PP.getSourceManager(); |
90 | |
91 | Rewriter Rewrite; |
92 | Rewrite.setSourceMgr(SM, LO: PP.getLangOpts()); |
93 | RewriteBuffer &RB = Rewrite.getEditBuffer(FID: SM.getMainFileID()); |
94 | |
95 | std::vector<Token> RawTokens; |
96 | LexRawTokensFromMainFile(PP, RawTokens); |
97 | unsigned CurRawTok = 0; |
98 | Token RawTok = GetNextRawTok(RawTokens, CurTok&: CurRawTok, ReturnComment: false); |
99 | |
100 | |
101 | // Get the first preprocessing token. |
102 | PP.EnterMainSourceFile(); |
103 | Token PPTok; |
104 | PP.Lex(Result&: 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. |
108 | // a header). If we see tokens that are in the preprocessed file but not the |
109 | // 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(K: tok::eof) || PPTok.isNot(K: tok::eof)) { |
113 | SourceLocation PPLoc = SM.getExpansionLoc(Loc: PPTok.getLocation()); |
114 | |
115 | // If PPTok is from a different source file, ignore it. |
116 | if (!SM.isWrittenInMainFile(Loc: PPLoc)) { |
117 | PP.Lex(Result&: PPTok); |
118 | continue; |
119 | } |
120 | |
121 | // If the raw file hits a preprocessor directive, they will be extra tokens |
122 | // 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. |
125 | if (RawTok.is(K: tok::hash) && RawTok.isAtStartOfLine()) { |
126 | // If this is a #warning directive or #pragma mark (GNU extensions), |
127 | // comment the line out. |
128 | if (RawTokens[CurRawTok].is(K: tok::identifier)) { |
129 | const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo(); |
130 | if (II->getName() == "warning" ) { |
131 | // Comment out #warning. |
132 | RB.InsertTextAfter(OrigOffset: SM.getFileOffset(SpellingLoc: RawTok.getLocation()), Str: "//" ); |
133 | } else if (II->getName() == "pragma" && |
134 | RawTokens[CurRawTok+1].is(K: tok::identifier) && |
135 | (RawTokens[CurRawTok+1].getIdentifierInfo()->getName() == |
136 | "mark" )) { |
137 | // Comment out #pragma mark. |
138 | RB.InsertTextAfter(OrigOffset: SM.getFileOffset(SpellingLoc: RawTok.getLocation()), Str: "//" ); |
139 | } |
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. |
144 | RawTok = GetNextRawTok(RawTokens, CurTok&: CurRawTok, ReturnComment: false); |
145 | while (!RawTok.isAtStartOfLine() && RawTok.isNot(K: tok::eof)) |
146 | RawTok = GetNextRawTok(RawTokens, CurTok&: CurRawTok, ReturnComment: false); |
147 | continue; |
148 | } |
149 | |
150 | // Okay, both tokens are from the same file. Get their offsets from the |
151 | // start of the file. |
152 | unsigned PPOffs = SM.getFileOffset(SpellingLoc: PPLoc); |
153 | unsigned RawOffs = SM.getFileOffset(SpellingLoc: RawTok.getLocation()); |
154 | |
155 | // If the offsets are the same and the token kind is the same, ignore them. |
156 | if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) { |
157 | RawTok = GetNextRawTok(RawTokens, CurTok&: CurRawTok, ReturnComment: false); |
158 | PP.Lex(Result&: 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 |
166 | // comments. Add a leading space if RawTok didn't have one. |
167 | bool HasSpace = RawTok.hasLeadingSpace(); |
168 | RB.InsertTextAfter(OrigOffset: RawOffs, Str: &" /*" [HasSpace]); |
169 | unsigned EndPos; |
170 | |
171 | do { |
172 | EndPos = RawOffs+RawTok.getLength(); |
173 | |
174 | RawTok = GetNextRawTok(RawTokens, CurTok&: CurRawTok, ReturnComment: true); |
175 | RawOffs = SM.getFileOffset(SpellingLoc: RawTok.getLocation()); |
176 | |
177 | if (RawTok.is(K: tok::comment)) { |
178 | // Skip past the comment. |
179 | RawTok = GetNextRawTok(RawTokens, CurTok&: CurRawTok, ReturnComment: false); |
180 | break; |
181 | } |
182 | |
183 | } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() && |
184 | (PPOffs != RawOffs || !isSameToken(RawTok, PPTok))); |
185 | |
186 | RB.InsertTextBefore(OrigOffset: EndPos, Str: "*/" ); |
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(Tok: PPTok); |
197 | PP.Lex(Result&: PPTok); |
198 | PPLoc = SM.getExpansionLoc(Loc: PPTok.getLocation()); |
199 | PPOffs = SM.getFileOffset(SpellingLoc: PPLoc); |
200 | } |
201 | Expansion += ' '; |
202 | RB.InsertTextBefore(OrigOffset: InsertPos, Str: Expansion); |
203 | } |
204 | |
205 | // Get the buffer corresponding to MainFileID. If we haven't changed it, then |
206 | // we are done. |
207 | if (const RewriteBuffer *RewriteBuf = |
208 | Rewrite.getRewriteBufferFor(FID: SM.getMainFileID())) { |
209 | //printf("Changed:\n"); |
210 | *OS << std::string(RewriteBuf->begin(), RewriteBuf->end()); |
211 | } else { |
212 | fprintf(stderr, format: "No changes\n" ); |
213 | } |
214 | OS->flush(); |
215 | } |
216 | |