Lines Matching +full:last +full:- +full:level

1 //===--- MacroCallReconstructor.cpp - Format C++ code -----------*- C++ -*-===//
8 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
24 #define DEBUG_TYPE "format-reconstruct"
36 Call(N.Tok, Parent, First, Line.Level);
44 unsigned Level,
47 : Result(Level), IdToReconstructed(ActiveExpansions) {
56 unsigned Level) { add(Token, Parent, First, Level); });
63 Result.Tokens.front()->Children.size() == 1);
65 *Result.Tokens.front()->Children.front(), Result.Level);
75 unsigned Level) {
77 llvm::dbgs() << "MCR: Token: " << Token->TokenText << ", Parent: "
78 << (ExpandedParent ? ExpandedParent->TokenText : "<null>")
81 // stream, we need to continue the last open reconstruction until we find the
92 // found in the reconstructed token stream of that expansion level.
98 if (!ActiveExpansions.empty() && Token->MacroCtx &&
99 (Token->MacroCtx->Role != MR_Hidden ||
100 ActiveExpansions.size() != Token->MacroCtx->ExpandedFrom.size())) {
105 prepareParent(ExpandedParent, First, Level);
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
127 // reconstructed replacement token as a parent (when possible) - that is, the
128 // last token in \c ActiveReconstructedLines[ActiveReconstructedLines.size()-2]
132 bool NewLine, unsigned Level) {
141 << (Parent ? Parent->TokenText : "<null>") << "\n");
146 // parent - either because it is already popped, for example because it was
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(
175 std::make_unique<ReconstructedLine>(Level));
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)
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()) {
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
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.
290 {ID, IU->second->Tokens.begin(), IU->second->Tokens.end()});
295 if (ActiveExpansions.back().SpelledI->Tok->is(tok::l_paren)) {
311 ActiveExpansions.back().SpelledI->Tok != Token) {
319 assert(Token->MacroCtx &&
320 (ActiveExpansions.size() >= Token->MacroCtx->EndOfExpansion));
321 for (size_t I = 0; I < Token->MacroCtx->EndOfExpansion; ++I) {
325 // expansion level.
328 FormatToken *Token = T->Tok;
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";
348 // - expand the closing parenthesis, if it exists, including an optional
350 // - handle tokens that were already reconstructed at an inner expansion
351 // level
352 // - handle tokens when a macro call had more than the expected number of
357 // FIXME: See the above debug-check for what we will need to do to be
375 llvm::dbgs() << (P.first ? P.first->TokenText : "<null>");
378 I != E; I = SpelledParentToReconstructedParent.find(I->second)) {
379 llvm::dbgs() << " -> " << (I->second ? I->second->TokenText : "<null>");
389 FormatToken *Token = ActiveExpansions.back().SpelledI->Tok;
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));
416 // All tokens that are children of the previous line's last token in the
420 // We will build up a map <null> -> ( -> ( with the first and second
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;
440 MacroCallStructure.back().Line->Level);
443 if (Token->is(tok::r_paren)) {
459 // FIXME: This can lead to unfortunate formatting decisions - give the user
473 assert(Result.Tokens.size() == 1 && !Result.Tokens.front()->Children.empty());
479 // The first line becomes the top level line in the resulting unwrapped line.
482 // Every subsequent line will become a child of the last token in the previous
484 LineNode *Last = (*I)->Tokens.back().get();
487 assert(Last->Children.empty());
488 Last->Children.push_back(std::move(*I));
490 // Mark the previous line's last token as generated by a macro expansion
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));
508 int Level) {
510 Result.Level = Level;
512 Result.Tokens.push_back(N->Tok);
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(); });
524 auto Line = createUnwrappedLine(**Child, Level);
529 // 1. One level below the current line's level.
530 // 2. At the correct level relative to each other.
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)));
549 void MacroCallReconstructor::debug(const ReconstructedLine &Line, int Level) {
550 for (int i = 0; i < Level; ++i)
555 if (N->Tok)
556 llvm::dbgs() << N->Tok->TokenText << " ";
557 for (const auto &Child : N->Children) {
559 debug(*Child, Level + 1);
560 for (int i = 0; i < Level; ++i)
584 << (ParentLastToken ? ParentLastToken->TokenText : "<null>")
587 assert(MacroCallLParen->is(tok::l_paren));