Lines Matching +full:back +full:- +full:end

1 //===--- MacroCallReconstructor.cpp - Format C++ code -----------*- C++ -*-===//
8 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
24 #define DEBUG_TYPE "format-reconstruct"
63 Result.Tokens.front()->Children.size() == 1);
65 *Result.Tokens.front()->Children.front(), Result.Level);
77 llvm::dbgs() << "MCR: Token: " << Token->TokenText << ", Parent: "
78 << (ExpandedParent ? ExpandedParent->TokenText : "<null>")
98 if (!ActiveExpansions.empty() && Token->MacroCtx &&
99 (Token->MacroCtx->Role != MR_Hidden ||
100 ActiveExpansions.size() != Token->MacroCtx->ExpandedFrom.size())) {
107 if (Token->MacroCtx) {
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
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
141 << (Parent ? Parent->TokenText : "<null>") << "\n");
146 // parent - either because it is already popped, for example because it was
153 getParentInResult(MacroCallStructure.back().MacroCallLParen);
156 << MacroCallStructure.back().MacroCallLParen->TokenText
158 << (OpenMacroParent ? OpenMacroParent->TokenText : "<null>")
162 (!ActiveReconstructedLines.back()->Tokens.empty() &&
163 Parent == ActiveReconstructedLines.back()->Tokens.back()->Tok)) {
166 while (ActiveReconstructedLines.back()->Tokens.empty() ||
167 (Parent != ActiveReconstructedLines.back()->Tokens.back()->Tok &&
168 ActiveReconstructedLines.back()->Tokens.back()->Tok !=
174 ActiveReconstructedLines.back()->Tokens.back()->Children.push_back(
177 &*ActiveReconstructedLines.back()->Tokens.back()->Children.back());
178 } else if (parentLine().Tokens.back()->Tok != Parent) {
181 while (Parent != parentLine().Tokens.back()->Tok &&
182 parentLine().Tokens.back()->Tok &&
183 parentLine().Tokens.back()->Tok != OpenMacroParent) {
202 Parent->MacroParent = true;
208 assert(Token->MacroCtx);
213 if (Token->MacroCtx->StartOfExpansion) {
218 if (Token->MacroCtx->Role != MR_Hidden)
222 if (ActiveExpansions.back().SpelledI != ActiveExpansions.back().SpelledE) {
223 assert(ActiveExpansions.size() == Token->MacroCtx->ExpandedFrom.size());
224 if (Token->MacroCtx->Role != MR_Hidden) {
226 // we're looking for - we either arrive here after startReconstruction,
230 assert(ActiveExpansions.back().SpelledI->Tok == Token);
232 } else if (!currentLine()->Tokens.empty()) {
237 currentLine()->Tokens.back()->Tok;
242 if (!(*I)->Tokens.empty()) {
243 SpelledParentToReconstructedParent[Token] = (*I)->Tokens.back()->Tok;
249 if (Token->MacroCtx->EndOfExpansion)
259 assert(Token->MacroCtx);
260 assert(!Token->MacroCtx->ExpandedFrom.empty());
261 assert(ActiveExpansions.size() <= Token->MacroCtx->ExpandedFrom.size());
267 Token->MacroCtx
268 ->ExpandedFrom[Token->MacroCtx->ExpandedFrom.size() - 1 - I]);
273 // Note that the token's expanded from stack is inside-to-outside, and the
276 ArrayRef(Token->MacroCtx->ExpandedFrom)
278 assert(StartedMacros.size() == Token->MacroCtx->StartOfExpansion);
279 // We reconstruct macro calls outside-to-inside.
288 assert(IU != IdToReconstructed.end());
290 {ID, IU->second->Tokens.begin(), IU->second->Tokens.end()});
293 if (ActiveExpansions.back().SpelledI == ActiveExpansions.back().SpelledE)
295 if (ActiveExpansions.back().SpelledI->Tok->is(tok::l_paren)) {
310 while (ActiveExpansions.back().SpelledI != ActiveExpansions.back().SpelledE &&
311 ActiveExpansions.back().SpelledI->Tok != Token) {
317 // End all reconstructions for which \p Token is the final token.
319 assert(Token->MacroCtx &&
320 (ActiveExpansions.size() >= Token->MacroCtx->EndOfExpansion));
321 for (size_t I = 0; I < Token->MacroCtx->EndOfExpansion; ++I) {
326 for (auto T = ActiveExpansions.back().SpelledI;
327 T != ActiveExpansions.back().SpelledE; ++T) {
328 FormatToken *Token = T->Tok;
329 bool ClosingParen = (std::next(T) == ActiveExpansions.back().SpelledE ||
330 std::next(T)->Tok->isTrailingComment()) &&
331 !Token->MacroCtx && Token->is(tok::r_paren);
332 bool TrailingComment = Token->isTrailingComment();
334 Token->MacroCtx &&
335 (ActiveExpansions.size() < Token->MacroCtx->ExpandedFrom.size());
337 llvm::dbgs() << "At token: " << Token->TokenText << "\n";
341 // potentially end up in the line when we finish the expansion.
348 // - expand the closing parenthesis, if it exists, including an optional
350 // - handle tokens that were already reconstructed at an inner expansion
352 // - handle tokens when a macro call had more than the expected number of
353 // arguments, i.e. when #define M(x) is called as M(a, b, c) we'll end
354 // up with the sequence ", b, c)" being open at the end of the
357 // FIXME: See the above debug-check for what we will need to do to be
359 for (auto T = ActiveExpansions.back().SpelledI;
360 T != ActiveExpansions.back().SpelledE; ++T) {
375 llvm::dbgs() << (P.first ? P.first->TokenText : "<null>");
377 E = SpelledParentToReconstructedParent.end();
378 I != E; I = SpelledParentToReconstructedParent.find(I->second)) {
379 llvm::dbgs() << " -> " << (I->second ? I->second->TokenText : "<null>");
389 FormatToken *Token = ActiveExpansions.back().SpelledI->Tok;
390 ++ActiveExpansions.back().SpelledI;
391 if (Token->MacroCtx) {
393 if (Token->MacroCtx->Role == MR_Hidden)
399 // ID(f) -> {f}
400 // ID({f}) -> {{f}}
403 if (ActiveExpansions.size() < Token->MacroCtx->ExpandedFrom.size())
408 if (!Token->MacroCtx) {
413 if (Token->is(tok::l_paren)) {
415 currentLine(), parentLine().Tokens.back()->Tok, Token));
420 // We will build up a map <null> -> ( -> ( with the first and second
424 SpelledParentToReconstructedParent[MacroCallStructure.back()
428 MacroCallStructure.back().Line->Level);
429 Token->MacroParent = true;
433 if (Token->is(tok::comma)) {
436 [MacroCallStructure.back().Line->Tokens.back()->Tok] = Token;
437 Token->MacroParent = true;
438 appendToken(Token, MacroCallStructure.back().Line);
440 MacroCallStructure.back().Line->Level);
443 if (Token->is(tok::r_paren)) {
444 appendToken(Token, MacroCallStructure.back().Line);
446 MacroCallStructure.back().ParentLastToken);
459 // FIXME: This can lead to unfortunate formatting decisions - give the user
473 assert(Result.Tokens.size() == 1 && !Result.Tokens.front()->Children.empty());
484 LineNode *Last = (*I)->Tokens.back().get();
486 for (auto *E = Top.Children.end(); I != E; ++I) {
487 assert(Last->Children.empty());
488 Last->Children.push_back(std::move(*I));
492 Last->Tok->MacroParent = true;
494 Last = Last->Children.back()->Tokens.back().get();
502 LLVM_DEBUG(llvm::dbgs() << "-> " << Token->TokenText << "\n");
503 L->Tokens.push_back(std::make_unique<LineNode>(Token));
512 Result.Tokens.push_back(N->Tok);
513 UnwrappedLineNode &Current = Result.Tokens.back();
515 std::count_if(N->Children.begin(), N->Children.end(),
516 [](const auto &Child) { return !Child->Tokens.empty(); });
517 if (NumChildren == 1 && Current.Tok->isOneOf(tok::l_paren, tok::comma)) {
522 N->Children.begin(), N->Children.end(),
523 [](const auto &Child) { return !Child->Tokens.empty(); });
525 Result.Tokens.splice(Result.Tokens.end(), Line.Tokens);
532 std::min_element(N->Children.begin(), N->Children.end(),
534 return E1->Level < E2->Level;
536 ->get()
537 ->Level;
538 for (const auto &Child : N->Children) {
539 if (Child->Tokens.empty())
542 *Child, Level + 1 + (Child->Level - MinChildLevel)));
555 if (N->Tok)
556 llvm::dbgs() << N->Tok->TokenText << " ";
557 for (const auto &Child : N->Children) {
569 return **std::prev(std::prev(ActiveReconstructedLines.end()));
574 return ActiveReconstructedLines.back();
584 << (ParentLastToken ? ParentLastToken->TokenText : "<null>")
587 assert(MacroCallLParen->is(tok::l_paren));