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