Coverage Report

Created: 2024-01-17 10:31

/src/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
0
                  FormatToken *Parent = nullptr) {
34
0
  bool First = true;
35
0
  for (const auto &N : Line.Tokens) {
36
0
    Call(N.Tok, Parent, First);
37
0
    First = false;
38
0
    for (const auto &Child : N.Children)
39
0
      forEachToken(Child, Call, N.Tok);
40
0
  }
41
0
}
42
43
MacroCallReconstructor::MacroCallReconstructor(
44
    unsigned Level,
45
    const llvm::DenseMap<FormatToken *, std::unique_ptr<UnwrappedLine>>
46
        &ActiveExpansions)
47
0
    : Level(Level), IdToReconstructed(ActiveExpansions) {
48
0
  Result.Tokens.push_back(std::make_unique<LineNode>());
49
0
  ActiveReconstructedLines.push_back(&Result);
50
0
}
51
52
0
void MacroCallReconstructor::addLine(const UnwrappedLine &Line) {
53
0
  assert(State != Finalized);
54
0
  LLVM_DEBUG(llvm::dbgs() << "MCR: new line...\n");
55
0
  forEachToken(Line, [&](FormatToken *Token, FormatToken *Parent, bool First) {
56
0
    add(Token, Parent, First);
57
0
  });
58
0
  assert(InProgress || finished());
59
0
}
60
61
0
UnwrappedLine MacroCallReconstructor::takeResult() && {
62
0
  finalize();
63
0
  assert(Result.Tokens.size() == 1 &&
64
0
         Result.Tokens.front()->Children.size() == 1);
65
0
  UnwrappedLine Final =
66
0
      createUnwrappedLine(*Result.Tokens.front()->Children.front(), Level);
67
0
  assert(!Final.Tokens.empty());
68
0
  return Final;
69
0
}
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
0
                                 FormatToken *ExpandedParent, bool First) {
76
0
  LLVM_DEBUG(
77
0
      llvm::dbgs() << "MCR: Token: " << Token->TokenText << ", Parent: "
78
0
                   << (ExpandedParent ? ExpandedParent->TokenText : "<null>")
79
0
                   << ", 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
0
  if (!ActiveExpansions.empty() && Token->MacroCtx &&
99
0
      (Token->MacroCtx->Role != MR_Hidden ||
100
0
       ActiveExpansions.size() != Token->MacroCtx->ExpandedFrom.size())) {
101
0
    if (/*PassedMacroComma = */ reconstructActiveCallUntil(Token))
102
0
      First = true;
103
0
  }
104
105
0
  prepareParent(ExpandedParent, First);
106
107
0
  if (Token->MacroCtx) {
108
    // If this token was generated by a macro call, add the reconstructed
109
    // equivalent of the token.
110
0
    reconstruct(Token);
111
0
  } else {
112
    // Otherwise, we add it to the current line.
113
0
    appendToken(Token);
114
0
  }
115
0
}
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
0
                                           bool NewLine) {
133
0
  LLVM_DEBUG({
134
0
    llvm::dbgs() << "ParentMap:\n";
135
0
    debugParentMap();
136
0
  });
137
  // We want to find the parent in the new unwrapped line, where the expanded
138
  // parent might have been replaced during reconstruction.
139
0
  FormatToken *Parent = getParentInResult(ExpandedParent);
140
0
  LLVM_DEBUG(llvm::dbgs() << "MCR: New parent: "
141
0
                          << (Parent ? Parent->TokenText : "<null>") << "\n");
142
143
0
  FormatToken *OpenMacroParent = nullptr;
144
0
  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
0
    OpenMacroParent =
153
0
        getParentInResult(MacroCallStructure.back().MacroCallLParen);
154
0
    LLVM_DEBUG(llvm::dbgs()
155
0
               << "MacroCallLParen: "
156
0
               << MacroCallStructure.back().MacroCallLParen->TokenText
157
0
               << ", OpenMacroParent: "
158
0
               << (OpenMacroParent ? OpenMacroParent->TokenText : "<null>")
159
0
               << "\n");
160
0
  }
161
0
  if (NewLine ||
162
0
      (!ActiveReconstructedLines.back()->Tokens.empty() &&
163
0
       Parent == ActiveReconstructedLines.back()->Tokens.back()->Tok)) {
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
0
    while (ActiveReconstructedLines.back()->Tokens.empty() ||
167
0
           (Parent != ActiveReconstructedLines.back()->Tokens.back()->Tok &&
168
0
            ActiveReconstructedLines.back()->Tokens.back()->Tok !=
169
0
                OpenMacroParent)) {
170
0
      ActiveReconstructedLines.pop_back();
171
0
      assert(!ActiveReconstructedLines.empty());
172
0
    }
173
0
    assert(!ActiveReconstructedLines.empty());
174
0
    ActiveReconstructedLines.back()->Tokens.back()->Children.push_back(
175
0
        std::make_unique<ReconstructedLine>());
176
0
    ActiveReconstructedLines.push_back(
177
0
        &*ActiveReconstructedLines.back()->Tokens.back()->Children.back());
178
0
  } 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
0
    while (Parent != parentLine().Tokens.back()->Tok &&
182
0
           parentLine().Tokens.back()->Tok &&
183
0
           parentLine().Tokens.back()->Tok != OpenMacroParent) {
184
0
      ActiveReconstructedLines.pop_back();
185
0
      assert(!ActiveReconstructedLines.empty());
186
0
    }
187
0
  }
188
0
  assert(!ActiveReconstructedLines.empty());
189
0
}
190
191
// For a given \p Parent in the incoming expanded token stream, find the
192
// corresponding parent in the output.
193
0
FormatToken *MacroCallReconstructor::getParentInResult(FormatToken *Parent) {
194
0
  FormatToken *Mapped = SpelledParentToReconstructedParent.lookup(Parent);
195
0
  if (!Mapped)
196
0
    return Parent;
197
0
  for (; Mapped; Mapped = SpelledParentToReconstructedParent.lookup(Parent))
198
0
    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
0
  Parent->MacroParent = true;
203
0
  return Parent;
204
0
}
205
206
// Reconstruct a \p Token that was expanded from a macro call.
207
0
void MacroCallReconstructor::reconstruct(FormatToken *Token) {
208
0
  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
0
  if (Token->MacroCtx->StartOfExpansion) {
214
0
    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
0
    if (Token->MacroCtx->Role != MR_Hidden)
219
0
      reconstructActiveCallUntil(Token);
220
0
  }
221
0
  assert(!ActiveExpansions.empty());
222
0
  if (ActiveExpansions.back().SpelledI != ActiveExpansions.back().SpelledE) {
223
0
    assert(ActiveExpansions.size() == Token->MacroCtx->ExpandedFrom.size());
224
0
    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
0
      assert(ActiveExpansions.back().SpelledI->Tok == Token);
231
0
      processNextReconstructed();
232
0
    } 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
235
      // token as the parent of the hidden token's children.
236
0
      SpelledParentToReconstructedParent[Token] =
237
0
          currentLine()->Tokens.back()->Tok;
238
0
    } else {
239
0
      for (auto I = ActiveReconstructedLines.rbegin(),
240
0
                E = ActiveReconstructedLines.rend();
241
0
           I != E; ++I) {
242
0
        if (!(*I)->Tokens.empty()) {
243
0
          SpelledParentToReconstructedParent[Token] = (*I)->Tokens.back()->Tok;
244
0
          break;
245
0
        }
246
0
      }
247
0
    }
248
0
  }
249
0
  if (Token->MacroCtx->EndOfExpansion)
250
0
    endReconstruction(Token);
251
0
}
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
0
void MacroCallReconstructor::startReconstruction(FormatToken *Token) {
259
0
  assert(Token->MacroCtx);
260
0
  assert(!Token->MacroCtx->ExpandedFrom.empty());
261
0
  assert(ActiveExpansions.size() <= Token->MacroCtx->ExpandedFrom.size());
262
0
#ifndef NDEBUG
263
  // Check that the token's reconstruction stack matches our current
264
  // reconstruction stack.
265
0
  for (size_t I = 0; I < ActiveExpansions.size(); ++I) {
266
0
    assert(ActiveExpansions[I].ID ==
267
0
           Token->MacroCtx
268
0
               ->ExpandedFrom[Token->MacroCtx->ExpandedFrom.size() - 1 - I]);
269
0
  }
270
0
#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
0
  ArrayRef<FormatToken *> StartedMacros =
276
0
      ArrayRef(Token->MacroCtx->ExpandedFrom)
277
0
          .drop_back(ActiveExpansions.size());
278
0
  assert(StartedMacros.size() == Token->MacroCtx->StartOfExpansion);
279
  // We reconstruct macro calls outside-to-inside.
280
0
  for (FormatToken *ID : llvm::reverse(StartedMacros)) {
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
0
#ifndef NDEBUG
284
0
    State = InProgress;
285
0
#endif
286
    // Put the reconstructed macro call's token into our reconstruction stack.
287
0
    auto IU = IdToReconstructed.find(ID);
288
0
    assert(IU != IdToReconstructed.end());
289
0
    ActiveExpansions.push_back(
290
0
        {ID, IU->second->Tokens.begin(), IU->second->Tokens.end()});
291
    // Process the macro call's identifier.
292
0
    processNextReconstructed();
293
0
    if (ActiveExpansions.back().SpelledI == ActiveExpansions.back().SpelledE)
294
0
      continue;
295
0
    if (ActiveExpansions.back().SpelledI->Tok->is(tok::l_paren)) {
296
      // Process the optional opening parenthesis.
297
0
      processNextReconstructed();
298
0
    }
299
0
  }
300
0
}
301
302
// Add all tokens in the reconstruction stream to the output until we find the
303
// given \p Token.
304
0
bool MacroCallReconstructor::reconstructActiveCallUntil(FormatToken *Token) {
305
0
  assert(!ActiveExpansions.empty());
306
0
  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
0
  while (ActiveExpansions.back().SpelledI != ActiveExpansions.back().SpelledE &&
311
0
         ActiveExpansions.back().SpelledI->Tok != Token) {
312
0
    PassedMacroComma = processNextReconstructed() || PassedMacroComma;
313
0
  }
314
0
  return PassedMacroComma;
315
0
}
316
317
// End all reconstructions for which \p Token is the final token.
318
0
void MacroCallReconstructor::endReconstruction(FormatToken *Token) {
319
0
  assert(Token->MacroCtx &&
320
0
         (ActiveExpansions.size() >= Token->MacroCtx->EndOfExpansion));
321
0
  for (size_t I = 0; I < Token->MacroCtx->EndOfExpansion; ++I) {
322
0
    LLVM_DEBUG([&] {
323
      // Check all remaining tokens but the final closing parenthesis and
324
      // optional trailing comment were already reconstructed at an inner
325
      // expansion level.
326
0
      for (auto T = ActiveExpansions.back().SpelledI;
327
0
           T != ActiveExpansions.back().SpelledE; ++T) {
328
0
        FormatToken *Token = T->Tok;
329
0
        bool ClosingParen = (std::next(T) == ActiveExpansions.back().SpelledE ||
330
0
                             std::next(T)->Tok->isTrailingComment()) &&
331
0
                            !Token->MacroCtx && Token->is(tok::r_paren);
332
0
        bool TrailingComment = Token->isTrailingComment();
333
0
        bool PreviousLevel =
334
0
            Token->MacroCtx &&
335
0
            (ActiveExpansions.size() < Token->MacroCtx->ExpandedFrom.size());
336
0
        if (!ClosingParen && !TrailingComment && !PreviousLevel)
337
0
          llvm::dbgs() << "At token: " << Token->TokenText << "\n";
338
        // In addition to the following cases, we can also run into this
339
        // when a macro call had more arguments than expected; in that case,
340
        // the comma and the remaining tokens in the macro call will
341
        // potentially end up in the line when we finish the expansion.
342
        // FIXME: Add the information which arguments are unused, and assert
343
        // one of the cases below plus reconstructed macro argument tokens.
344
        // assert(ClosingParen || TrailingComment || PreviousLevel);
345
0
      }
346
0
    }());
347
    // Handle the remaining open tokens:
348
    // - expand the closing parenthesis, if it exists, including an optional
349
    //   trailing comment
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
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
355
    //   reconstruction; we want to gracefully handle that case
356
    //
357
    // FIXME: See the above debug-check for what we will need to do to be
358
    // able to assert this.
359
0
    for (auto T = ActiveExpansions.back().SpelledI;
360
0
         T != ActiveExpansions.back().SpelledE; ++T) {
361
0
      processNextReconstructed();
362
0
    }
363
0
    ActiveExpansions.pop_back();
364
0
  }
365
0
}
366
367
0
void MacroCallReconstructor::debugParentMap() const {
368
0
  llvm::DenseSet<FormatToken *> Values;
369
0
  for (const auto &P : SpelledParentToReconstructedParent)
370
0
    Values.insert(P.second);
371
372
0
  for (const auto &P : SpelledParentToReconstructedParent) {
373
0
    if (Values.contains(P.first))
374
0
      continue;
375
0
    llvm::dbgs() << (P.first ? P.first->TokenText : "<null>");
376
0
    for (auto I = SpelledParentToReconstructedParent.find(P.first),
377
0
              E = SpelledParentToReconstructedParent.end();
378
0
         I != E; I = SpelledParentToReconstructedParent.find(I->second)) {
379
0
      llvm::dbgs() << " -> " << (I->second ? I->second->TokenText : "<null>");
380
0
    }
381
0
    llvm::dbgs() << "\n";
382
0
  }
383
0
}
384
385
// If visible, add the next token of the reconstructed token sequence to the
386
// output. Returns whether reconstruction passed a comma that is part of a
387
// macro call.
388
0
bool MacroCallReconstructor::processNextReconstructed() {
389
0
  FormatToken *Token = ActiveExpansions.back().SpelledI->Tok;
390
0
  ++ActiveExpansions.back().SpelledI;
391
0
  if (Token->MacroCtx) {
392
    // Skip tokens that are not part of the macro call.
393
0
    if (Token->MacroCtx->Role == MR_Hidden)
394
0
      return false;
395
    // Skip tokens we already expanded during an inner reconstruction.
396
    // For example, given: #define ID(x) {x}
397
    // And the call: ID(ID(f))
398
    // We get two reconstructions:
399
    // ID(f) -> {f}
400
    // ID({f}) -> {{f}}
401
    // We reconstruct f during the first reconstruction, and skip it during the
402
    // second reconstruction.
403
0
    if (ActiveExpansions.size() < Token->MacroCtx->ExpandedFrom.size())
404
0
      return false;
405
0
  }
406
  // Tokens that do not have a macro context are tokens in that are part of the
407
  // macro call that have not taken part in expansion.
408
0
  if (!Token->MacroCtx) {
409
    // Put the parentheses and commas of a macro call into the same line;
410
    // if the arguments produce new unwrapped lines, they will become children
411
    // of the corresponding opening parenthesis or comma tokens in the
412
    // reconstructed call.
413
0
    if (Token->is(tok::l_paren)) {
414
0
      MacroCallStructure.push_back(MacroCallState(
415
0
          currentLine(), parentLine().Tokens.back()->Tok, Token));
416
      // All tokens that are children of the previous line's last token in the
417
      // reconstructed token stream will now be children of the l_paren token.
418
      // For example, for the line containing the macro calls:
419
      //   auto x = ID({ID(2)});
420
      // We will build up a map <null> -> ( -> ( with the first and second
421
      // l_paren of the macro call respectively. New lines that come in with a
422
      // <null> parent will then become children of the l_paren token of the
423
      // currently innermost macro call.
424
0
      SpelledParentToReconstructedParent[MacroCallStructure.back()
425
0
                                             .ParentLastToken] = Token;
426
0
      appendToken(Token);
427
0
      prepareParent(Token, /*NewLine=*/true);
428
0
      Token->MacroParent = true;
429
0
      return false;
430
0
    }
431
0
    if (!MacroCallStructure.empty()) {
432
0
      if (Token->is(tok::comma)) {
433
        // Make new lines inside the next argument children of the comma token.
434
0
        SpelledParentToReconstructedParent
435
0
            [MacroCallStructure.back().Line->Tokens.back()->Tok] = Token;
436
0
        Token->MacroParent = true;
437
0
        appendToken(Token, MacroCallStructure.back().Line);
438
0
        prepareParent(Token, /*NewLine=*/true);
439
0
        return true;
440
0
      }
441
0
      if (Token->is(tok::r_paren)) {
442
0
        appendToken(Token, MacroCallStructure.back().Line);
443
0
        SpelledParentToReconstructedParent.erase(
444
0
            MacroCallStructure.back().ParentLastToken);
445
0
        MacroCallStructure.pop_back();
446
0
        return false;
447
0
      }
448
0
    }
449
0
  }
450
  // Note that any tokens that are tagged with MR_None have been passed as
451
  // arguments to the macro that have not been expanded, for example:
452
  // Given: #define ID(X) x
453
  // When calling: ID(a, b)
454
  // 'b' will be part of the reconstructed token stream, but tagged MR_None.
455
  // Given that erroring out in this case would be disruptive, we continue
456
  // pushing the (unformatted) token.
457
  // FIXME: This can lead to unfortunate formatting decisions - give the user
458
  // a hint that their macro definition is broken.
459
0
  appendToken(Token);
460
0
  return false;
461
0
}
462
463
0
void MacroCallReconstructor::finalize() {
464
0
#ifndef NDEBUG
465
0
  assert(State != Finalized && finished());
466
0
  State = Finalized;
467
0
#endif
468
469
  // We created corresponding unwrapped lines for each incoming line as children
470
  // the the toplevel null token.
471
0
  assert(Result.Tokens.size() == 1 && !Result.Tokens.front()->Children.empty());
472
0
  LLVM_DEBUG({
473
0
    llvm::dbgs() << "Finalizing reconstructed lines:\n";
474
0
    debug(Result, 0);
475
0
  });
476
477
  // The first line becomes the top level line in the resulting unwrapped line.
478
0
  LineNode &Top = *Result.Tokens.front();
479
0
  auto *I = Top.Children.begin();
480
  // Every subsequent line will become a child of the last token in the previous
481
  // line, which is the token prior to the first token in the line.
482
0
  LineNode *Last = (*I)->Tokens.back().get();
483
0
  ++I;
484
0
  for (auto *E = Top.Children.end(); I != E; ++I) {
485
0
    assert(Last->Children.empty());
486
0
    Last->Children.push_back(std::move(*I));
487
488
    // Mark the previous line's last token as generated by a macro expansion
489
    // so the formatting algorithm can take that into account.
490
0
    Last->Tok->MacroParent = true;
491
492
0
    Last = Last->Children.back()->Tokens.back().get();
493
0
  }
494
0
  Top.Children.resize(1);
495
0
}
496
497
void MacroCallReconstructor::appendToken(FormatToken *Token,
498
0
                                         ReconstructedLine *L) {
499
0
  L = L ? L : currentLine();
500
0
  LLVM_DEBUG(llvm::dbgs() << "-> " << Token->TokenText << "\n");
501
0
  L->Tokens.push_back(std::make_unique<LineNode>(Token));
502
0
}
503
504
UnwrappedLine
505
MacroCallReconstructor::createUnwrappedLine(const ReconstructedLine &Line,
506
0
                                            int Level) {
507
0
  UnwrappedLine Result;
508
0
  Result.Level = Level;
509
0
  for (const auto &N : Line.Tokens) {
510
0
    Result.Tokens.push_back(N->Tok);
511
0
    UnwrappedLineNode &Current = Result.Tokens.back();
512
0
    for (const auto &Child : N->Children) {
513
0
      if (Child->Tokens.empty())
514
0
        continue;
515
0
      Current.Children.push_back(createUnwrappedLine(*Child, Level + 1));
516
0
    }
517
0
    if (Current.Children.size() == 1 &&
518
0
        Current.Tok->isOneOf(tok::l_paren, tok::comma)) {
519
0
      Result.Tokens.splice(Result.Tokens.end(),
520
0
                           Current.Children.front().Tokens);
521
0
      Current.Children.clear();
522
0
    }
523
0
  }
524
0
  return Result;
525
0
}
526
527
0
void MacroCallReconstructor::debug(const ReconstructedLine &Line, int Level) {
528
0
  for (int i = 0; i < Level; ++i)
529
0
    llvm::dbgs() << " ";
530
0
  for (const auto &N : Line.Tokens) {
531
0
    if (!N)
532
0
      continue;
533
0
    if (N->Tok)
534
0
      llvm::dbgs() << N->Tok->TokenText << " ";
535
0
    for (const auto &Child : N->Children) {
536
0
      llvm::dbgs() << "\n";
537
0
      debug(*Child, Level + 1);
538
0
      for (int i = 0; i < Level; ++i)
539
0
        llvm::dbgs() << " ";
540
0
    }
541
0
  }
542
0
  llvm::dbgs() << "\n";
543
0
}
544
545
MacroCallReconstructor::ReconstructedLine &
546
0
MacroCallReconstructor::parentLine() {
547
0
  return **std::prev(std::prev(ActiveReconstructedLines.end()));
548
0
}
549
550
MacroCallReconstructor::ReconstructedLine *
551
0
MacroCallReconstructor::currentLine() {
552
0
  return ActiveReconstructedLines.back();
553
0
}
554
555
MacroCallReconstructor::MacroCallState::MacroCallState(
556
    MacroCallReconstructor::ReconstructedLine *Line,
557
    FormatToken *ParentLastToken, FormatToken *MacroCallLParen)
558
    : Line(Line), ParentLastToken(ParentLastToken),
559
0
      MacroCallLParen(MacroCallLParen) {
560
0
  LLVM_DEBUG(
561
0
      llvm::dbgs() << "ParentLastToken: "
562
0
                   << (ParentLastToken ? ParentLastToken->TokenText : "<null>")
563
0
                   << "\n");
564
565
0
  assert(MacroCallLParen->is(tok::l_paren));
566
0
}
567
568
} // namespace format
569
} // namespace clang