/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/MacroCallReconstructor.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- MacroCallReconstructor.cpp - Format C++ code -----------*- C++ -*-===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the implementation of MacroCallReconstructor, which fits |
12 | | /// an reconstructed macro call to a parsed set of UnwrappedLines. |
13 | | /// |
14 | | //===----------------------------------------------------------------------===// |
15 | | |
16 | | #include "Macros.h" |
17 | | |
18 | | #include "UnwrappedLineParser.h" |
19 | | #include "clang/Basic/TokenKinds.h" |
20 | | #include "llvm/ADT/DenseSet.h" |
21 | | #include "llvm/Support/Debug.h" |
22 | | #include <cassert> |
23 | | |
24 | | #define DEBUG_TYPE "format-reconstruct" |
25 | | |
26 | | namespace clang { |
27 | | namespace format { |
28 | | |
29 | | // Call \p Call for each token in the unwrapped line given, passing |
30 | | // the token, its parent and whether it is the first token in the line. |
31 | | template <typename T> |
32 | | void forEachToken(const UnwrappedLine &Line, const T &Call, |
33 | 513 | FormatToken *Parent = nullptr) { |
34 | 513 | bool First = true; |
35 | 2.39k | for (const auto &N : Line.Tokens) { |
36 | 2.39k | Call(N.Tok, Parent, First); |
37 | 2.39k | First = false; |
38 | 2.39k | for (const auto &Child : N.Children) |
39 | 101 | forEachToken(Child, Call, N.Tok); |
40 | 2.39k | } |
41 | 513 | } |
42 | | |
43 | | MacroCallReconstructor::MacroCallReconstructor( |
44 | | unsigned Level, |
45 | | const llvm::DenseMap<FormatToken *, std::unique_ptr<UnwrappedLine>> |
46 | | &ActiveExpansions) |
47 | 254 | : Level(Level), IdToReconstructed(ActiveExpansions) { |
48 | 254 | Result.Tokens.push_back(std::make_unique<LineNode>()); |
49 | 254 | ActiveReconstructedLines.push_back(&Result); |
50 | 254 | } |
51 | | |
52 | 412 | void MacroCallReconstructor::addLine(const UnwrappedLine &Line) { |
53 | 412 | assert(State != Finalized); |
54 | 412 | LLVM_DEBUG(llvm::dbgs() << "MCR: new line...\n"); |
55 | 2.39k | forEachToken(Line, [&](FormatToken *Token, FormatToken *Parent, bool First) { |
56 | 2.39k | add(Token, Parent, First); |
57 | 2.39k | }); |
58 | 412 | assert(InProgress || finished()); |
59 | 412 | } |
60 | | |
61 | 254 | UnwrappedLine MacroCallReconstructor::takeResult() && { |
62 | 254 | finalize(); |
63 | 254 | assert(Result.Tokens.size() == 1 && |
64 | 254 | Result.Tokens.front()->Children.size() == 1); |
65 | 254 | UnwrappedLine Final = |
66 | 254 | createUnwrappedLine(*Result.Tokens.front()->Children.front(), Level); |
67 | 254 | assert(!Final.Tokens.empty()); |
68 | 254 | return Final; |
69 | 254 | } |
70 | | |
71 | | // Reconstruct the position of the next \p Token, given its parent \p |
72 | | // ExpandedParent in the incoming unwrapped line. \p First specifies whether it |
73 | | // is the first token in a given unwrapped line. |
74 | | void MacroCallReconstructor::add(FormatToken *Token, |
75 | 2.39k | FormatToken *ExpandedParent, bool First) { |
76 | 2.39k | LLVM_DEBUG( |
77 | 2.39k | llvm::dbgs() << "MCR: Token: " << Token->TokenText << ", Parent: " |
78 | 2.39k | << (ExpandedParent ? ExpandedParent->TokenText : "<null>") |
79 | 2.39k | << ", First: " << First << "\n"); |
80 | | // In order to be able to find the correct parent in the reconstructed token |
81 | | // stream, we need to continue the last open reconstruction until we find the |
82 | | // given token if it is part of the reconstructed token stream. |
83 | | // |
84 | | // Note that hidden tokens can be part of the reconstructed stream in nested |
85 | | // macro calls. |
86 | | // For example, given |
87 | | // #define C(x, y) x y |
88 | | // #define B(x) {x} |
89 | | // And the call: |
90 | | // C(a, B(b)) |
91 | | // The outer macro call will be C(a, {b}), and the hidden token '}' can be |
92 | | // found in the reconstructed token stream of that expansion level. |
93 | | // In the expanded token stream |
94 | | // a {b} |
95 | | // 'b' is a child of '{'. We need to continue the open expansion of the ',' |
96 | | // in the call of 'C' in order to correctly set the ',' as the parent of '{', |
97 | | // so we later set the spelled token 'b' as a child of the ','. |
98 | 2.39k | if (!ActiveExpansions.empty() && Token->MacroCtx1.83k && |
99 | 2.39k | (1.83k Token->MacroCtx->Role != MR_Hidden1.83k || |
100 | 1.83k | ActiveExpansions.size() != Token->MacroCtx->ExpandedFrom.size()680 )) { |
101 | 1.18k | if (/*PassedMacroComma = */ reconstructActiveCallUntil(Token)) |
102 | 99 | First = true; |
103 | 1.18k | } |
104 | | |
105 | 2.39k | prepareParent(ExpandedParent, First); |
106 | | |
107 | 2.39k | if (Token->MacroCtx) { |
108 | | // If this token was generated by a macro call, add the reconstructed |
109 | | // equivalent of the token. |
110 | 2.11k | reconstruct(Token); |
111 | 2.11k | } else { |
112 | | // Otherwise, we add it to the current line. |
113 | 272 | appendToken(Token); |
114 | 272 | } |
115 | 2.39k | } |
116 | | |
117 | | // Adjusts the stack of active reconstructed lines so we're ready to push |
118 | | // tokens. The tokens to be pushed are children of ExpandedParent in the |
119 | | // expanded code. |
120 | | // |
121 | | // This may entail: |
122 | | // - creating a new line, if the parent is on the active line |
123 | | // - popping active lines, if the parent is further up the stack |
124 | | // |
125 | | // Postcondition: |
126 | | // ActiveReconstructedLines.back() is the line that has \p ExpandedParent or its |
127 | | // reconstructed replacement token as a parent (when possible) - that is, the |
128 | | // last token in \c ActiveReconstructedLines[ActiveReconstructedLines.size()-2] |
129 | | // is the parent of ActiveReconstructedLines.back() in the reconstructed |
130 | | // unwrapped line. |
131 | | void MacroCallReconstructor::prepareParent(FormatToken *ExpandedParent, |
132 | 2.71k | bool NewLine) { |
133 | 2.71k | LLVM_DEBUG({ |
134 | 2.71k | llvm::dbgs() << "ParentMap:\n"; |
135 | 2.71k | debugParentMap(); |
136 | 2.71k | }); |
137 | | // We want to find the parent in the new unwrapped line, where the expanded |
138 | | // parent might have been replaced during reconstruction. |
139 | 2.71k | FormatToken *Parent = getParentInResult(ExpandedParent); |
140 | 2.71k | LLVM_DEBUG(llvm::dbgs() << "MCR: New parent: " |
141 | 2.71k | << (Parent ? Parent->TokenText : "<null>") << "\n"); |
142 | | |
143 | 2.71k | FormatToken *OpenMacroParent = nullptr; |
144 | 2.71k | if (!MacroCallStructure.empty()) { |
145 | | // Inside a macro expansion, it is possible to lose track of the correct |
146 | | // parent - either because it is already popped, for example because it was |
147 | | // in a different macro argument (e.g. M({, })), or when we work on invalid |
148 | | // code. |
149 | | // Thus, we use the innermost macro call's parent as the parent at which |
150 | | // we stop; this allows us to stay within the macro expansion and keeps |
151 | | // any problems confined to the extent of the macro call. |
152 | 1.93k | OpenMacroParent = |
153 | 1.93k | getParentInResult(MacroCallStructure.back().MacroCallLParen); |
154 | 1.93k | LLVM_DEBUG(llvm::dbgs() |
155 | 1.93k | << "MacroCallLParen: " |
156 | 1.93k | << MacroCallStructure.back().MacroCallLParen->TokenText |
157 | 1.93k | << ", OpenMacroParent: " |
158 | 1.93k | << (OpenMacroParent ? OpenMacroParent->TokenText : "<null>") |
159 | 1.93k | << "\n"); |
160 | 1.93k | } |
161 | 2.71k | if (NewLine || |
162 | 2.71k | (1.79k !ActiveReconstructedLines.back()->Tokens.empty()1.79k && |
163 | 1.79k | Parent == ActiveReconstructedLines.back()->Tokens.back()->Tok1.59k )) { |
164 | | // If we are at the first token in a new line, we want to also |
165 | | // create a new line in the resulting reconstructed unwrapped line. |
166 | 1.35k | while (ActiveReconstructedLines.back()->Tokens.empty() || |
167 | 1.35k | (1.18k Parent != ActiveReconstructedLines.back()->Tokens.back()->Tok1.18k && |
168 | 1.18k | ActiveReconstructedLines.back()->Tokens.back()->Tok != |
169 | 431 | OpenMacroParent)) { |
170 | 431 | ActiveReconstructedLines.pop_back(); |
171 | 431 | assert(!ActiveReconstructedLines.empty()); |
172 | 431 | } |
173 | 922 | assert(!ActiveReconstructedLines.empty()); |
174 | 922 | ActiveReconstructedLines.back()->Tokens.back()->Children.push_back( |
175 | 922 | std::make_unique<ReconstructedLine>()); |
176 | 922 | ActiveReconstructedLines.push_back( |
177 | 922 | &*ActiveReconstructedLines.back()->Tokens.back()->Children.back()); |
178 | 1.79k | } else if (parentLine().Tokens.back()->Tok != Parent) { |
179 | | // If we're not the first token in a new line, pop lines until we find |
180 | | // the child of \c Parent in the stack. |
181 | 343 | while (Parent != parentLine().Tokens.back()->Tok && |
182 | 343 | parentLine().Tokens.back()->Tok182 && |
183 | 343 | parentLine().Tokens.back()->Tok != OpenMacroParent177 ) { |
184 | 175 | ActiveReconstructedLines.pop_back(); |
185 | 175 | assert(!ActiveReconstructedLines.empty()); |
186 | 175 | } |
187 | 168 | } |
188 | 2.71k | assert(!ActiveReconstructedLines.empty()); |
189 | 2.71k | } |
190 | | |
191 | | // For a given \p Parent in the incoming expanded token stream, find the |
192 | | // corresponding parent in the output. |
193 | 4.64k | FormatToken *MacroCallReconstructor::getParentInResult(FormatToken *Parent) { |
194 | 4.64k | FormatToken *Mapped = SpelledParentToReconstructedParent.lookup(Parent); |
195 | 4.64k | if (!Mapped) |
196 | 2.34k | return Parent; |
197 | 6.10k | for (; 2.30k Mapped; Mapped = SpelledParentToReconstructedParent.lookup(Parent)3.80k ) |
198 | 3.80k | Parent = Mapped; |
199 | | // If we use a different token than the parent in the expanded token stream |
200 | | // as parent, mark it as a special parent, so the formatting code knows it |
201 | | // needs to have its children formatted. |
202 | 2.30k | Parent->MacroParent = true; |
203 | 2.30k | return Parent; |
204 | 4.64k | } |
205 | | |
206 | | // Reconstruct a \p Token that was expanded from a macro call. |
207 | 2.11k | void MacroCallReconstructor::reconstruct(FormatToken *Token) { |
208 | 2.11k | assert(Token->MacroCtx); |
209 | | // A single token can be the only result of a macro call: |
210 | | // Given: #define ID(x, y) ; |
211 | | // And the call: ID(<some>, <tokens>) |
212 | | // ';' in the expanded stream will reconstruct all of ID(<some>, <tokens>). |
213 | 2.11k | if (Token->MacroCtx->StartOfExpansion) { |
214 | 327 | startReconstruction(Token); |
215 | | // If the order of tokens in the expanded token stream is not the |
216 | | // same as the order of tokens in the reconstructed stream, we need |
217 | | // to reconstruct tokens that arrive later in the stream. |
218 | 327 | if (Token->MacroCtx->Role != MR_Hidden) |
219 | 177 | reconstructActiveCallUntil(Token); |
220 | 327 | } |
221 | 2.11k | assert(!ActiveExpansions.empty()); |
222 | 2.11k | if (ActiveExpansions.back().SpelledI != ActiveExpansions.back().SpelledE) { |
223 | 1.74k | assert(ActiveExpansions.size() == Token->MacroCtx->ExpandedFrom.size()); |
224 | 1.74k | if (Token->MacroCtx->Role != MR_Hidden) { |
225 | | // The current token in the reconstructed token stream must be the token |
226 | | // we're looking for - we either arrive here after startReconstruction, |
227 | | // which initiates the stream to the first token, or after |
228 | | // continueReconstructionUntil skipped until the expected token in the |
229 | | // reconstructed stream at the start of add(...). |
230 | 1.31k | assert(ActiveExpansions.back().SpelledI->Tok == Token); |
231 | 1.31k | processNextReconstructed(); |
232 | 1.31k | } else if (433 !currentLine()->Tokens.empty()433 ) { |
233 | | // Map all hidden tokens to the last visible token in the output. |
234 | | // If the hidden token is a parent, we'll use the last visible |
235 | | // token as the parent of the hidden token's children. |
236 | 186 | SpelledParentToReconstructedParent[Token] = |
237 | 186 | currentLine()->Tokens.back()->Tok; |
238 | 247 | } else { |
239 | 247 | for (auto I = ActiveReconstructedLines.rbegin(), |
240 | 247 | E = ActiveReconstructedLines.rend(); |
241 | 494 | I != E; ++I247 ) { |
242 | 494 | if (!(*I)->Tokens.empty()) { |
243 | 247 | SpelledParentToReconstructedParent[Token] = (*I)->Tokens.back()->Tok; |
244 | 247 | break; |
245 | 247 | } |
246 | 494 | } |
247 | 247 | } |
248 | 1.74k | } |
249 | 2.11k | if (Token->MacroCtx->EndOfExpansion) |
250 | 323 | endReconstruction(Token); |
251 | 2.11k | } |
252 | | |
253 | | // Given a \p Token that starts an expansion, reconstruct the beginning of the |
254 | | // macro call. |
255 | | // For example, given: #define ID(x) x |
256 | | // And the call: ID(int a) |
257 | | // Reconstructs: ID( |
258 | 327 | void MacroCallReconstructor::startReconstruction(FormatToken *Token) { |
259 | 327 | assert(Token->MacroCtx); |
260 | 327 | assert(!Token->MacroCtx->ExpandedFrom.empty()); |
261 | 327 | assert(ActiveExpansions.size() <= Token->MacroCtx->ExpandedFrom.size()); |
262 | 327 | #ifndef NDEBUG |
263 | | // Check that the token's reconstruction stack matches our current |
264 | | // reconstruction stack. |
265 | 384 | for (size_t I = 0; 327 I < ActiveExpansions.size(); ++I57 ) { |
266 | 57 | assert(ActiveExpansions[I].ID == |
267 | 57 | Token->MacroCtx |
268 | 57 | ->ExpandedFrom[Token->MacroCtx->ExpandedFrom.size() - 1 - I]); |
269 | 57 | } |
270 | 327 | #endif |
271 | | // Start reconstruction for all calls for which this token is the first token |
272 | | // generated by the call. |
273 | | // Note that the token's expanded from stack is inside-to-outside, and the |
274 | | // expansions for which this token is not the first are the outermost ones. |
275 | 327 | ArrayRef<FormatToken *> StartedMacros = |
276 | 327 | ArrayRef(Token->MacroCtx->ExpandedFrom) |
277 | 327 | .drop_back(ActiveExpansions.size()); |
278 | 327 | assert(StartedMacros.size() == Token->MacroCtx->StartOfExpansion); |
279 | | // We reconstruct macro calls outside-to-inside. |
280 | 339 | for (FormatToken *ID : llvm::reverse(StartedMacros))327 { |
281 | | // We found a macro call to be reconstructed; the next time our |
282 | | // reconstruction stack is empty we know we finished an reconstruction. |
283 | 339 | #ifndef NDEBUG |
284 | 339 | State = InProgress; |
285 | 339 | #endif |
286 | | // Put the reconstructed macro call's token into our reconstruction stack. |
287 | 339 | auto IU = IdToReconstructed.find(ID); |
288 | 339 | assert(IU != IdToReconstructed.end()); |
289 | 339 | ActiveExpansions.push_back( |
290 | 339 | {ID, IU->second->Tokens.begin(), IU->second->Tokens.end()}); |
291 | | // Process the macro call's identifier. |
292 | 339 | processNextReconstructed(); |
293 | 339 | if (ActiveExpansions.back().SpelledI == ActiveExpansions.back().SpelledE) |
294 | 118 | continue; |
295 | 221 | if (ActiveExpansions.back().SpelledI->Tok->is(tok::l_paren)) { |
296 | | // Process the optional opening parenthesis. |
297 | 221 | processNextReconstructed(); |
298 | 221 | } |
299 | 221 | } |
300 | 327 | } |
301 | | |
302 | | // Add all tokens in the reconstruction stream to the output until we find the |
303 | | // given \p Token. |
304 | 1.35k | bool MacroCallReconstructor::reconstructActiveCallUntil(FormatToken *Token) { |
305 | 1.35k | assert(!ActiveExpansions.empty()); |
306 | 1.35k | bool PassedMacroComma = false; |
307 | | // FIXME: If Token was already expanded earlier, due to |
308 | | // a change in order, we will not find it, but need to |
309 | | // skip it. |
310 | 1.56k | while (ActiveExpansions.back().SpelledI != ActiveExpansions.back().SpelledE && |
311 | 1.56k | ActiveExpansions.back().SpelledI->Tok != Token1.56k ) { |
312 | 205 | PassedMacroComma = processNextReconstructed() || PassedMacroComma105 ; |
313 | 205 | } |
314 | 1.35k | return PassedMacroComma; |
315 | 1.35k | } |
316 | | |
317 | | // End all reconstructions for which \p Token is the final token. |
318 | 323 | void MacroCallReconstructor::endReconstruction(FormatToken *Token) { |
319 | 323 | assert(Token->MacroCtx && |
320 | 323 | (ActiveExpansions.size() >= Token->MacroCtx->EndOfExpansion)); |
321 | 662 | for (size_t I = 0; 323 I < Token->MacroCtx->EndOfExpansion; ++I339 ) { |
322 | 339 | #ifndef NDEBUG |
323 | | // Check all remaining tokens but the final closing parenthesis and optional |
324 | | // trailing comment were already reconstructed at an inner expansion level. |
325 | 339 | for (auto T = ActiveExpansions.back().SpelledI; |
326 | 950 | T != ActiveExpansions.back().SpelledE; ++T611 ) { |
327 | 611 | FormatToken *Token = T->Tok; |
328 | 611 | bool ClosingParen = (std::next(T) == ActiveExpansions.back().SpelledE || |
329 | 611 | std::next(T)->Tok->isTrailingComment()391 ) && |
330 | 611 | !Token->MacroCtx223 && Token->is(tok::r_paren)223 ; |
331 | 611 | bool TrailingComment = Token->isTrailingComment(); |
332 | 611 | bool PreviousLevel = |
333 | 611 | Token->MacroCtx && |
334 | 611 | (ActiveExpansions.size() < Token->MacroCtx->ExpandedFrom.size())387 ; |
335 | 611 | if (!ClosingParen && !TrailingComment391 && !PreviousLevel388 ) |
336 | 1 | llvm::dbgs() << "At token: " << Token->TokenText << "\n"; |
337 | | // In addition to the following cases, we can also run into this |
338 | | // when a macro call had more arguments than expected; in that case, |
339 | | // the comma and the remaining tokens in the macro call will potentially |
340 | | // end up in the line when we finish the expansion. |
341 | | // FIXME: Add the information which arguments are unused, and assert |
342 | | // one of the cases below plus reconstructed macro argument tokens. |
343 | | // assert(ClosingParen || TrailingComment || PreviousLevel); |
344 | 611 | } |
345 | 339 | #endif |
346 | | // Handle the remaining open tokens: |
347 | | // - expand the closing parenthesis, if it exists, including an optional |
348 | | // trailing comment |
349 | | // - handle tokens that were already reconstructed at an inner expansion |
350 | | // level |
351 | | // - handle tokens when a macro call had more than the expected number of |
352 | | // arguments, i.e. when #define M(x) is called as M(a, b, c) we'll end |
353 | | // up with the sequence ", b, c)" being open at the end of the |
354 | | // reconstruction; we want to gracefully handle that case |
355 | | // |
356 | | // FIXME: See the above debug-check for what we will need to do to be |
357 | | // able to assert this. |
358 | 339 | for (auto T = ActiveExpansions.back().SpelledI; |
359 | 950 | T != ActiveExpansions.back().SpelledE; ++T611 ) { |
360 | 611 | processNextReconstructed(); |
361 | 611 | } |
362 | 339 | ActiveExpansions.pop_back(); |
363 | 339 | } |
364 | 323 | } |
365 | | |
366 | 0 | void MacroCallReconstructor::debugParentMap() const { |
367 | 0 | llvm::DenseSet<FormatToken *> Values; |
368 | 0 | for (const auto &P : SpelledParentToReconstructedParent) |
369 | 0 | Values.insert(P.second); |
370 | |
|
371 | 0 | for (const auto &P : SpelledParentToReconstructedParent) { |
372 | 0 | if (Values.contains(P.first)) |
373 | 0 | continue; |
374 | 0 | llvm::dbgs() << (P.first ? P.first->TokenText : "<null>"); |
375 | 0 | for (auto I = SpelledParentToReconstructedParent.find(P.first), |
376 | 0 | E = SpelledParentToReconstructedParent.end(); |
377 | 0 | I != E; I = SpelledParentToReconstructedParent.find(I->second)) { |
378 | 0 | llvm::dbgs() << " -> " << (I->second ? I->second->TokenText : "<null>"); |
379 | 0 | } |
380 | 0 | llvm::dbgs() << "\n"; |
381 | 0 | } |
382 | 0 | } |
383 | | |
384 | | // If visible, add the next token of the reconstructed token sequence to the |
385 | | // output. Returns whether reconstruction passed a comma that is part of a |
386 | | // macro call. |
387 | 2.69k | bool MacroCallReconstructor::processNextReconstructed() { |
388 | 2.69k | FormatToken *Token = ActiveExpansions.back().SpelledI->Tok; |
389 | 2.69k | ++ActiveExpansions.back().SpelledI; |
390 | 2.69k | if (Token->MacroCtx) { |
391 | | // Skip tokens that are not part of the macro call. |
392 | 1.80k | if (Token->MacroCtx->Role == MR_Hidden) |
393 | 320 | return false; |
394 | | // Skip tokens we already expanded during an inner reconstruction. |
395 | | // For example, given: #define ID(x) {x} |
396 | | // And the call: ID(ID(f)) |
397 | | // We get two reconstructions: |
398 | | // ID(f) -> {f} |
399 | | // ID({f}) -> {{f}} |
400 | | // We reconstruct f during the first reconstruction, and skip it during the |
401 | | // second reconstruction. |
402 | 1.48k | if (ActiveExpansions.size() < Token->MacroCtx->ExpandedFrom.size()) |
403 | 170 | return false; |
404 | 1.48k | } |
405 | | // Tokens that do not have a macro context are tokens in that are part of the |
406 | | // macro call that have not taken part in expansion. |
407 | 2.20k | if (!Token->MacroCtx) { |
408 | | // Put the parentheses and commas of a macro call into the same line; |
409 | | // if the arguments produce new unwrapped lines, they will become children |
410 | | // of the corresponding opening parenthesis or comma tokens in the |
411 | | // reconstructed call. |
412 | 885 | if (Token->is(tok::l_paren)) { |
413 | 221 | MacroCallStructure.push_back(MacroCallState( |
414 | 221 | currentLine(), parentLine().Tokens.back()->Tok, Token)); |
415 | | // All tokens that are children of the previous line's last token in the |
416 | | // reconstructed token stream will now be children of the l_paren token. |
417 | | // For example, for the line containing the macro calls: |
418 | | // auto x = ID({ID(2)}); |
419 | | // We will build up a map <null> -> ( -> ( with the first and second |
420 | | // l_paren of the macro call respectively. New lines that come in with a |
421 | | // <null> parent will then become children of the l_paren token of the |
422 | | // currently innermost macro call. |
423 | 221 | SpelledParentToReconstructedParent[MacroCallStructure.back() |
424 | 221 | .ParentLastToken] = Token; |
425 | 221 | appendToken(Token); |
426 | 221 | prepareParent(Token, /*NewLine=*/true); |
427 | 221 | Token->MacroParent = true; |
428 | 221 | return false; |
429 | 221 | } |
430 | 664 | if (!MacroCallStructure.empty()) { |
431 | 378 | if (Token->is(tok::comma)) { |
432 | | // Make new lines inside the next argument children of the comma token. |
433 | 101 | SpelledParentToReconstructedParent |
434 | 101 | [MacroCallStructure.back().Line->Tokens.back()->Tok] = Token; |
435 | 101 | Token->MacroParent = true; |
436 | 101 | appendToken(Token, MacroCallStructure.back().Line); |
437 | 101 | prepareParent(Token, /*NewLine=*/true); |
438 | 101 | return true; |
439 | 101 | } |
440 | 277 | if (Token->is(tok::r_paren)) { |
441 | 221 | appendToken(Token, MacroCallStructure.back().Line); |
442 | 221 | SpelledParentToReconstructedParent.erase( |
443 | 221 | MacroCallStructure.back().ParentLastToken); |
444 | 221 | MacroCallStructure.pop_back(); |
445 | 221 | return false; |
446 | 221 | } |
447 | 277 | } |
448 | 664 | } |
449 | | // Note that any tokens that are tagged with MR_None have been passed as |
450 | | // arguments to the macro that have not been expanded, for example: |
451 | | // Given: #define ID(X) x |
452 | | // When calling: ID(a, b) |
453 | | // 'b' will be part of the reconstructed token stream, but tagged MR_None. |
454 | | // Given that erroring out in this case would be disruptive, we continue |
455 | | // pushing the (unformatted) token. |
456 | | // FIXME: This can lead to unfortunate formatting decisions - give the user |
457 | | // a hint that their macro definition is broken. |
458 | 1.65k | appendToken(Token); |
459 | 1.65k | return false; |
460 | 2.20k | } |
461 | | |
462 | 254 | void MacroCallReconstructor::finalize() { |
463 | 254 | #ifndef NDEBUG |
464 | 254 | assert(State != Finalized && finished()); |
465 | 254 | State = Finalized; |
466 | 254 | #endif |
467 | | |
468 | | // We created corresponding unwrapped lines for each incoming line as children |
469 | | // the the toplevel null token. |
470 | 254 | assert(Result.Tokens.size() == 1 && !Result.Tokens.front()->Children.empty()); |
471 | 254 | LLVM_DEBUG({ |
472 | 254 | llvm::dbgs() << "Finalizing reconstructed lines:\n"; |
473 | 254 | debug(Result, 0); |
474 | 254 | }); |
475 | | |
476 | | // The first line becomes the top level line in the resulting unwrapped line. |
477 | 254 | LineNode &Top = *Result.Tokens.front(); |
478 | 254 | auto *I = Top.Children.begin(); |
479 | | // Every subsequent line will become a child of the last token in the previous |
480 | | // line, which is the token prior to the first token in the line. |
481 | 254 | LineNode *Last = (*I)->Tokens.back().get(); |
482 | 254 | ++I; |
483 | 267 | for (auto *E = Top.Children.end(); I != E; ++I13 ) { |
484 | 13 | assert(Last->Children.empty()); |
485 | 13 | Last->Children.push_back(std::move(*I)); |
486 | | |
487 | | // Mark the previous line's last token as generated by a macro expansion |
488 | | // so the formatting algorithm can take that into account. |
489 | 13 | Last->Tok->MacroParent = true; |
490 | | |
491 | 13 | Last = Last->Children.back()->Tokens.back().get(); |
492 | 13 | } |
493 | 254 | Top.Children.resize(1); |
494 | 254 | } |
495 | | |
496 | | void MacroCallReconstructor::appendToken(FormatToken *Token, |
497 | 2.47k | ReconstructedLine *L) { |
498 | 2.47k | L = L ? L322 : currentLine()2.15k ; |
499 | 2.47k | LLVM_DEBUG(llvm::dbgs() << "-> " << Token->TokenText << "\n"); |
500 | 2.47k | L->Tokens.push_back(std::make_unique<LineNode>(Token)); |
501 | 2.47k | } |
502 | | |
503 | | UnwrappedLine |
504 | | MacroCallReconstructor::createUnwrappedLine(const ReconstructedLine &Line, |
505 | 754 | int Level) { |
506 | 754 | UnwrappedLine Result; |
507 | 754 | Result.Level = Level; |
508 | 2.47k | for (const auto &N : Line.Tokens) { |
509 | 2.47k | Result.Tokens.push_back(N->Tok); |
510 | 2.47k | UnwrappedLineNode &Current = Result.Tokens.back(); |
511 | 2.47k | for (const auto &Child : N->Children) { |
512 | 668 | if (Child->Tokens.empty()) |
513 | 168 | continue; |
514 | 500 | Current.Children.push_back(createUnwrappedLine(*Child, Level + 1)); |
515 | 500 | } |
516 | 2.47k | if (Current.Children.size() == 1 && |
517 | 2.47k | Current.Tok->isOneOf(tok::l_paren, tok::comma)267 ) { |
518 | 241 | Result.Tokens.splice(Result.Tokens.end(), |
519 | 241 | Current.Children.front().Tokens); |
520 | 241 | Current.Children.clear(); |
521 | 241 | } |
522 | 2.47k | } |
523 | 754 | return Result; |
524 | 754 | } |
525 | | |
526 | 0 | void MacroCallReconstructor::debug(const ReconstructedLine &Line, int Level) { |
527 | 0 | for (int i = 0; i < Level; ++i) |
528 | 0 | llvm::dbgs() << " "; |
529 | 0 | for (const auto &N : Line.Tokens) { |
530 | 0 | if (!N) |
531 | 0 | continue; |
532 | 0 | if (N->Tok) |
533 | 0 | llvm::dbgs() << N->Tok->TokenText << " "; |
534 | 0 | for (const auto &Child : N->Children) { |
535 | 0 | llvm::dbgs() << "\n"; |
536 | 0 | debug(*Child, Level + 1); |
537 | 0 | for (int i = 0; i < Level; ++i) |
538 | 0 | llvm::dbgs() << " "; |
539 | 0 | } |
540 | 0 | } |
541 | 0 | llvm::dbgs() << "\n"; |
542 | 0 | } |
543 | | |
544 | | MacroCallReconstructor::ReconstructedLine & |
545 | 2.71k | MacroCallReconstructor::parentLine() { |
546 | 2.71k | return **std::prev(std::prev(ActiveReconstructedLines.end())); |
547 | 2.71k | } |
548 | | |
549 | | MacroCallReconstructor::ReconstructedLine * |
550 | 2.99k | MacroCallReconstructor::currentLine() { |
551 | 2.99k | return ActiveReconstructedLines.back(); |
552 | 2.99k | } |
553 | | |
554 | | MacroCallReconstructor::MacroCallState::MacroCallState( |
555 | | MacroCallReconstructor::ReconstructedLine *Line, |
556 | | FormatToken *ParentLastToken, FormatToken *MacroCallLParen) |
557 | | : Line(Line), ParentLastToken(ParentLastToken), |
558 | 221 | MacroCallLParen(MacroCallLParen) { |
559 | 221 | LLVM_DEBUG( |
560 | 221 | llvm::dbgs() << "ParentLastToken: " |
561 | 221 | << (ParentLastToken ? ParentLastToken->TokenText : "<null>") |
562 | 221 | << "\n"); |
563 | | |
564 | 221 | assert(MacroCallLParen->is(tok::l_paren)); |
565 | 221 | } |
566 | | |
567 | | } // namespace format |
568 | | } // namespace clang |