Lines Matching +full:- +full:- +full:token

1 //===--- MacroCallReconstructor.cpp - Format C++ code -----------*- C++ -*-===//
8 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
24 #define DEBUG_TYPE "format-reconstruct"
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.
55 forEachToken(Line, [&](FormatToken *Token, FormatToken *Parent, bool First, in addLine()
56 unsigned Level) { add(Token, Parent, First, Level); }); in addLine()
63 Result.Tokens.front()->Children.size() == 1); in takeResult()
65 *Result.Tokens.front()->Children.front(), Result.Level); in takeResult()
70 // Reconstruct the position of the next \p Token, given its parent \p
72 // is the first token in a given unwrapped line.
73 void MacroCallReconstructor::add(FormatToken *Token, in add() argument
77 llvm::dbgs() << "MCR: Token: " << Token->TokenText << ", Parent: " in add()
78 << (ExpandedParent ? ExpandedParent->TokenText : "<null>") in add()
80 // In order to be able to find the correct parent in the reconstructed token in add()
82 // given token if it is part of the reconstructed token stream. in add()
91 // The outer macro call will be C(a, {b}), and the hidden token '}' can be in add()
92 // found in the reconstructed token stream of that expansion level. in add()
93 // In the expanded token stream in add()
97 // so we later set the spelled token 'b' as a child of the ','. in add()
98 if (!ActiveExpansions.empty() && Token->MacroCtx && in add()
99 (Token->MacroCtx->Role != MR_Hidden || in add()
100 ActiveExpansions.size() != Token->MacroCtx->ExpandedFrom.size())) { in add()
101 if (/*PassedMacroComma = */ reconstructActiveCallUntil(Token)) in add()
107 if (Token->MacroCtx) { in add()
108 // If this token was generated by a macro call, add the reconstructed in add()
109 // equivalent of the token. in add()
110 reconstruct(Token); in add()
113 appendToken(Token); in add()
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
127 // reconstructed replacement token as a parent (when possible) - that is, the
128 // last token in \c ActiveReconstructedLines[ActiveReconstructedLines.size()-2]
141 << (Parent ? Parent->TokenText : "<null>") << "\n"); in prepareParent()
146 // parent - either because it is already popped, for example because it was in prepareParent()
156 << MacroCallStructure.back().MacroCallLParen->TokenText in prepareParent()
158 << (OpenMacroParent ? OpenMacroParent->TokenText : "<null>") in prepareParent()
162 (!ActiveReconstructedLines.back()->Tokens.empty() && in prepareParent()
163 Parent == ActiveReconstructedLines.back()->Tokens.back()->Tok)) { in prepareParent()
164 // If we are at the first token in a new line, we want to also in prepareParent()
166 while (ActiveReconstructedLines.back()->Tokens.empty() || in prepareParent()
167 (Parent != ActiveReconstructedLines.back()->Tokens.back()->Tok && in prepareParent()
168 ActiveReconstructedLines.back()->Tokens.back()->Tok != in prepareParent()
174 ActiveReconstructedLines.back()->Tokens.back()->Children.push_back( in prepareParent()
177 &*ActiveReconstructedLines.back()->Tokens.back()->Children.back()); in prepareParent()
178 } else if (parentLine().Tokens.back()->Tok != Parent) { in prepareParent()
179 // If we're not the first token in a new line, pop lines until we find in prepareParent()
181 while (Parent != parentLine().Tokens.back()->Tok && in prepareParent()
182 parentLine().Tokens.back()->Tok && in prepareParent()
183 parentLine().Tokens.back()->Tok != OpenMacroParent) { in prepareParent()
191 // For a given \p Parent in the incoming expanded token stream, find the
199 // If we use a different token than the parent in the expanded token stream in getParentInResult()
202 Parent->MacroParent = true; in getParentInResult()
206 // Reconstruct a \p Token that was expanded from a macro call.
207 void MacroCallReconstructor::reconstruct(FormatToken *Token) { in reconstruct() argument
208 assert(Token->MacroCtx); in reconstruct()
209 // A single token can be the only result of a macro call: in reconstruct()
213 if (Token->MacroCtx->StartOfExpansion) { in reconstruct()
214 startReconstruction(Token); in reconstruct()
215 // If the order of tokens in the expanded token stream is not the in reconstruct()
218 if (Token->MacroCtx->Role != MR_Hidden) in reconstruct()
219 reconstructActiveCallUntil(Token); in reconstruct()
223 assert(ActiveExpansions.size() == Token->MacroCtx->ExpandedFrom.size()); in reconstruct()
224 if (Token->MacroCtx->Role != MR_Hidden) { in reconstruct()
225 // The current token in the reconstructed token stream must be the token in reconstruct()
226 // we're looking for - we either arrive here after startReconstruction, in reconstruct()
227 // which initiates the stream to the first token, or after in reconstruct()
228 // continueReconstructionUntil skipped until the expected token in the in reconstruct()
230 assert(ActiveExpansions.back().SpelledI->Tok == Token); in reconstruct()
232 } else if (!currentLine()->Tokens.empty()) { in reconstruct()
233 // Map all hidden tokens to the last visible token in the output. in reconstruct()
234 // If the hidden token is a parent, we'll use the last visible in reconstruct()
235 // token as the parent of the hidden token's children. in reconstruct()
236 SpelledParentToReconstructedParent[Token] = in reconstruct()
237 currentLine()->Tokens.back()->Tok; in reconstruct()
242 if (!(*I)->Tokens.empty()) { in reconstruct()
243 SpelledParentToReconstructedParent[Token] = (*I)->Tokens.back()->Tok; in reconstruct()
249 if (Token->MacroCtx->EndOfExpansion) in reconstruct()
250 endReconstruction(Token); in reconstruct()
253 // Given a \p Token that starts an expansion, reconstruct the beginning of the
258 void MacroCallReconstructor::startReconstruction(FormatToken *Token) { in startReconstruction() argument
259 assert(Token->MacroCtx); in startReconstruction()
260 assert(!Token->MacroCtx->ExpandedFrom.empty()); in startReconstruction()
261 assert(ActiveExpansions.size() <= Token->MacroCtx->ExpandedFrom.size()); in startReconstruction()
263 // Check that the token's reconstruction stack matches our current in startReconstruction()
267 Token->MacroCtx in startReconstruction()
268 ->ExpandedFrom[Token->MacroCtx->ExpandedFrom.size() - 1 - I]); in startReconstruction()
271 // Start reconstruction for all calls for which this token is the first token in startReconstruction()
273 // Note that the token's expanded from stack is inside-to-outside, and the in startReconstruction()
274 // expansions for which this token is not the first are the outermost ones. in startReconstruction()
276 ArrayRef(Token->MacroCtx->ExpandedFrom) in startReconstruction()
278 assert(StartedMacros.size() == Token->MacroCtx->StartOfExpansion); in startReconstruction()
279 // We reconstruct macro calls outside-to-inside. in startReconstruction()
286 // Put the reconstructed macro call's token into our reconstruction stack. in startReconstruction()
290 {ID, IU->second->Tokens.begin(), IU->second->Tokens.end()}); in startReconstruction()
295 if (ActiveExpansions.back().SpelledI->Tok->is(tok::l_paren)) { in startReconstruction()
303 // given \p Token.
304 bool MacroCallReconstructor::reconstructActiveCallUntil(FormatToken *Token) { in reconstructActiveCallUntil() argument
307 // FIXME: If Token was already expanded earlier, due to in reconstructActiveCallUntil()
311 ActiveExpansions.back().SpelledI->Tok != Token) { in reconstructActiveCallUntil()
317 // End all reconstructions for which \p Token is the final token.
318 void MacroCallReconstructor::endReconstruction(FormatToken *Token) { in endReconstruction() argument
319 assert(Token->MacroCtx && in endReconstruction()
320 (ActiveExpansions.size() >= Token->MacroCtx->EndOfExpansion)); in endReconstruction()
321 for (size_t I = 0; I < Token->MacroCtx->EndOfExpansion; ++I) { in endReconstruction()
328 FormatToken *Token = T->Tok; in endReconstruction() local
330 std::next(T)->Tok->isTrailingComment()) && in endReconstruction()
331 !Token->MacroCtx && Token->is(tok::r_paren); in endReconstruction()
332 bool TrailingComment = Token->isTrailingComment(); in endReconstruction()
334 Token->MacroCtx && in endReconstruction()
335 (ActiveExpansions.size() < Token->MacroCtx->ExpandedFrom.size()); in endReconstruction()
337 llvm::dbgs() << "At token: " << Token->TokenText << "\n"; in endReconstruction()
348 // - expand the closing parenthesis, if it exists, including an optional in endReconstruction()
350 // - handle tokens that were already reconstructed at an inner expansion in endReconstruction()
352 // - handle tokens when a macro call had more than the expected number of in endReconstruction()
357 // FIXME: See the above debug-check for what we will need to do to be in endReconstruction()
375 llvm::dbgs() << (P.first ? P.first->TokenText : "<null>"); in debugParentMap()
378 I != E; I = SpelledParentToReconstructedParent.find(I->second)) { in debugParentMap()
379 llvm::dbgs() << " -> " << (I->second ? I->second->TokenText : "<null>"); in debugParentMap()
385 // If visible, add the next token of the reconstructed token sequence to the
389 FormatToken *Token = ActiveExpansions.back().SpelledI->Tok; in processNextReconstructed() local
391 if (Token->MacroCtx) { in processNextReconstructed()
393 if (Token->MacroCtx->Role == MR_Hidden) in processNextReconstructed()
399 // ID(f) -> {f} in processNextReconstructed()
400 // ID({f}) -> {{f}} in processNextReconstructed()
403 if (ActiveExpansions.size() < Token->MacroCtx->ExpandedFrom.size()) in processNextReconstructed()
408 if (!Token->MacroCtx) { in processNextReconstructed()
413 if (Token->is(tok::l_paren)) { in processNextReconstructed()
415 currentLine(), parentLine().Tokens.back()->Tok, Token)); in processNextReconstructed()
416 // All tokens that are children of the previous line's last token in the in processNextReconstructed()
417 // reconstructed token stream will now be children of the l_paren token. in processNextReconstructed()
420 // We will build up a map <null> -> ( -> ( with the first and second in processNextReconstructed()
422 // <null> parent will then become children of the l_paren token of the in processNextReconstructed()
425 .ParentLastToken] = Token; in processNextReconstructed()
426 appendToken(Token); in processNextReconstructed()
427 prepareParent(Token, /*NewLine=*/true, in processNextReconstructed()
428 MacroCallStructure.back().Line->Level); in processNextReconstructed()
429 Token->MacroParent = true; in processNextReconstructed()
433 if (Token->is(tok::comma)) { in processNextReconstructed()
434 // Make new lines inside the next argument children of the comma token. in processNextReconstructed()
436 [MacroCallStructure.back().Line->Tokens.back()->Tok] = Token; in processNextReconstructed()
437 Token->MacroParent = true; in processNextReconstructed()
438 appendToken(Token, MacroCallStructure.back().Line); in processNextReconstructed()
439 prepareParent(Token, /*NewLine=*/true, in processNextReconstructed()
440 MacroCallStructure.back().Line->Level); in processNextReconstructed()
443 if (Token->is(tok::r_paren)) { in processNextReconstructed()
444 appendToken(Token, MacroCallStructure.back().Line); in processNextReconstructed()
456 // 'b' will be part of the reconstructed token stream, but tagged MR_None. in processNextReconstructed()
458 // pushing the (unformatted) token. in processNextReconstructed()
459 // FIXME: This can lead to unfortunate formatting decisions - give the user in processNextReconstructed()
461 appendToken(Token); in processNextReconstructed()
472 // the the toplevel null token. in finalize()
473 assert(Result.Tokens.size() == 1 && !Result.Tokens.front()->Children.empty()); in finalize()
482 // Every subsequent line will become a child of the last token in the previous in finalize()
483 // line, which is the token prior to the first token in the line. in finalize()
484 LineNode *Last = (*I)->Tokens.back().get(); in finalize()
487 assert(Last->Children.empty()); in finalize()
488 Last->Children.push_back(std::move(*I)); in finalize()
490 // Mark the previous line's last token as generated by a macro expansion in finalize()
492 Last->Tok->MacroParent = true; in finalize()
494 Last = Last->Children.back()->Tokens.back().get(); in finalize()
499 void MacroCallReconstructor::appendToken(FormatToken *Token, in appendToken() argument
502 LLVM_DEBUG(llvm::dbgs() << "-> " << Token->TokenText << "\n"); in appendToken()
503 L->Tokens.push_back(std::make_unique<LineNode>(Token)); in appendToken()
512 Result.Tokens.push_back(N->Tok); in createUnwrappedLine()
515 std::count_if(N->Children.begin(), N->Children.end(), in createUnwrappedLine()
516 [](const auto &Child) { return !Child->Tokens.empty(); }); in createUnwrappedLine()
517 if (NumChildren == 1 && Current.Tok->isOneOf(tok::l_paren, tok::comma)) { in createUnwrappedLine()
522 N->Children.begin(), N->Children.end(), in createUnwrappedLine()
523 [](const auto &Child) { return !Child->Tokens.empty(); }); in createUnwrappedLine()
532 std::min_element(N->Children.begin(), N->Children.end(), in createUnwrappedLine()
534 return E1->Level < E2->Level; in createUnwrappedLine()
536 ->get() in createUnwrappedLine()
537 ->Level; in createUnwrappedLine()
538 for (const auto &Child : N->Children) { in createUnwrappedLine()
539 if (Child->Tokens.empty()) in createUnwrappedLine()
542 *Child, Level + 1 + (Child->Level - MinChildLevel))); in createUnwrappedLine()
555 if (N->Tok) in debug()
556 llvm::dbgs() << N->Tok->TokenText << " "; in debug()
557 for (const auto &Child : N->Children) { in debug()
584 << (ParentLastToken ? ParentLastToken->TokenText : "<null>") in MacroCallState()
587 assert(MacroCallLParen->is(tok::l_paren)); in MacroCallState()