[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-21 Thread via cfe-commits

https://github.com/r4nt created https://github.com/llvm/llvm-project/pull/82523

When reconstructing lines from a macro expansion, make sure that lines at 
different levels in the expanded code get indented correctly as part of the 
macro argument.

>From d9b8493f993a4020f3eaf2911b9704e4b25ac6b7 Mon Sep 17 00:00:00 2001
From: Manuel Klimek 
Date: Wed, 21 Feb 2024 14:16:54 +
Subject: [PATCH] [ClangFormat] Fix indent in child lines within a macro
 argument.

When reconstructing lines from a macro expansion, make sure that
lines at different levels in the expanded code get indented
correctly as part of the macro argument.
---
 clang/lib/Format/MacroCallReconstructor.cpp   |  69 ++
 clang/lib/Format/Macros.h |  10 +-
 clang/lib/Format/UnwrappedLineParser.cpp  |   6 +
 clang/lib/Format/UnwrappedLineParser.h|   2 +
 .../Format/FormatTestMacroExpansion.cpp   |  23 +++-
 .../Format/MacroCallReconstructorTest.cpp | 129 --
 6 files changed, 166 insertions(+), 73 deletions(-)

diff --git a/clang/lib/Format/MacroCallReconstructor.cpp 
b/clang/lib/Format/MacroCallReconstructor.cpp
index cbdd1683c54d1a..5277b406c81b11 100644
--- a/clang/lib/Format/MacroCallReconstructor.cpp
+++ b/clang/lib/Format/MacroCallReconstructor.cpp
@@ -19,6 +19,7 @@
 #include "clang/Basic/TokenKinds.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/Support/Debug.h"
+#include 
 #include 
 
 #define DEBUG_TYPE "format-reconstruct"
@@ -33,7 +34,7 @@ void forEachToken(const UnwrappedLine &Line, const T &Call,
   FormatToken *Parent = nullptr) {
   bool First = true;
   for (const auto &N : Line.Tokens) {
-Call(N.Tok, Parent, First);
+Call(N.Tok, Parent, First, Line.Level);
 First = false;
 for (const auto &Child : N.Children)
   forEachToken(Child, Call, N.Tok);
@@ -44,7 +45,7 @@ MacroCallReconstructor::MacroCallReconstructor(
 unsigned Level,
 const llvm::DenseMap>
 &ActiveExpansions)
-: Level(Level), IdToReconstructed(ActiveExpansions) {
+: Result(Level), IdToReconstructed(ActiveExpansions) {
   Result.Tokens.push_back(std::make_unique());
   ActiveReconstructedLines.push_back(&Result);
 }
@@ -52,9 +53,8 @@ MacroCallReconstructor::MacroCallReconstructor(
 void MacroCallReconstructor::addLine(const UnwrappedLine &Line) {
   assert(State != Finalized);
   LLVM_DEBUG(llvm::dbgs() << "MCR: new line...\n");
-  forEachToken(Line, [&](FormatToken *Token, FormatToken *Parent, bool First) {
-add(Token, Parent, First);
-  });
+  forEachToken(Line, [&](FormatToken *Token, FormatToken *Parent, bool First,
+ unsigned Level) { add(Token, Parent, First, Level); 
});
   assert(InProgress || finished());
 }
 
@@ -62,8 +62,8 @@ UnwrappedLine MacroCallReconstructor::takeResult() && {
   finalize();
   assert(Result.Tokens.size() == 1 &&
  Result.Tokens.front()->Children.size() == 1);
-  UnwrappedLine Final =
-  createUnwrappedLine(*Result.Tokens.front()->Children.front(), Level);
+  UnwrappedLine Final = createUnwrappedLine(
+  *Result.Tokens.front()->Children.front(), Result.Level);
   assert(!Final.Tokens.empty());
   return Final;
 }
@@ -72,7 +72,8 @@ UnwrappedLine MacroCallReconstructor::takeResult() && {
 // ExpandedParent in the incoming unwrapped line. \p First specifies whether it
 // is the first token in a given unwrapped line.
 void MacroCallReconstructor::add(FormatToken *Token,
- FormatToken *ExpandedParent, bool First) {
+ FormatToken *ExpandedParent, bool First,
+ unsigned Level) {
   LLVM_DEBUG(
   llvm::dbgs() << "MCR: Token: " << Token->TokenText << ", Parent: "
<< (ExpandedParent ? ExpandedParent->TokenText : "")
@@ -102,7 +103,7 @@ void MacroCallReconstructor::add(FormatToken *Token,
   First = true;
   }
 
-  prepareParent(ExpandedParent, First);
+  prepareParent(ExpandedParent, First, Level);
 
   if (Token->MacroCtx) {
 // If this token was generated by a macro call, add the reconstructed
@@ -129,7 +130,7 @@ void MacroCallReconstructor::add(FormatToken *Token,
 // is the parent of ActiveReconstructedLines.back() in the reconstructed
 // unwrapped line.
 void MacroCallReconstructor::prepareParent(FormatToken *ExpandedParent,
-   bool NewLine) {
+   bool NewLine, unsigned Level) {
   LLVM_DEBUG({
 llvm::dbgs() << "ParentMap:\n";
 debugParentMap();
@@ -172,7 +173,7 @@ void MacroCallReconstructor::prepareParent(FormatToken 
*ExpandedParent,
 }
 assert(!ActiveReconstructedLines.empty());
 ActiveReconstructedLines.back()->Tokens.back()->Children.push_back(
-std::make_unique());
+std::make_unique(Level));
 ActiveReconstructedLines.push_back(
 &*ActiveReconstructedLines.back()->Tokens.back()->Children.back());
   } else if (paren

[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-21 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-format

Author: None (r4nt)


Changes

When reconstructing lines from a macro expansion, make sure that lines at 
different levels in the expanded code get indented correctly as part of the 
macro argument.

---
Full diff: https://github.com/llvm/llvm-project/pull/82523.diff


6 Files Affected:

- (modified) clang/lib/Format/MacroCallReconstructor.cpp (+46-23) 
- (modified) clang/lib/Format/Macros.h (+5-5) 
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+6) 
- (modified) clang/lib/Format/UnwrappedLineParser.h (+2) 
- (modified) clang/unittests/Format/FormatTestMacroExpansion.cpp (+19-4) 
- (modified) clang/unittests/Format/MacroCallReconstructorTest.cpp (+88-41) 


``diff
diff --git a/clang/lib/Format/MacroCallReconstructor.cpp 
b/clang/lib/Format/MacroCallReconstructor.cpp
index cbdd1683c54d1a..5277b406c81b11 100644
--- a/clang/lib/Format/MacroCallReconstructor.cpp
+++ b/clang/lib/Format/MacroCallReconstructor.cpp
@@ -19,6 +19,7 @@
 #include "clang/Basic/TokenKinds.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/Support/Debug.h"
+#include 
 #include 
 
 #define DEBUG_TYPE "format-reconstruct"
@@ -33,7 +34,7 @@ void forEachToken(const UnwrappedLine &Line, const T &Call,
   FormatToken *Parent = nullptr) {
   bool First = true;
   for (const auto &N : Line.Tokens) {
-Call(N.Tok, Parent, First);
+Call(N.Tok, Parent, First, Line.Level);
 First = false;
 for (const auto &Child : N.Children)
   forEachToken(Child, Call, N.Tok);
@@ -44,7 +45,7 @@ MacroCallReconstructor::MacroCallReconstructor(
 unsigned Level,
 const llvm::DenseMap>
 &ActiveExpansions)
-: Level(Level), IdToReconstructed(ActiveExpansions) {
+: Result(Level), IdToReconstructed(ActiveExpansions) {
   Result.Tokens.push_back(std::make_unique());
   ActiveReconstructedLines.push_back(&Result);
 }
@@ -52,9 +53,8 @@ MacroCallReconstructor::MacroCallReconstructor(
 void MacroCallReconstructor::addLine(const UnwrappedLine &Line) {
   assert(State != Finalized);
   LLVM_DEBUG(llvm::dbgs() << "MCR: new line...\n");
-  forEachToken(Line, [&](FormatToken *Token, FormatToken *Parent, bool First) {
-add(Token, Parent, First);
-  });
+  forEachToken(Line, [&](FormatToken *Token, FormatToken *Parent, bool First,
+ unsigned Level) { add(Token, Parent, First, Level); 
});
   assert(InProgress || finished());
 }
 
@@ -62,8 +62,8 @@ UnwrappedLine MacroCallReconstructor::takeResult() && {
   finalize();
   assert(Result.Tokens.size() == 1 &&
  Result.Tokens.front()->Children.size() == 1);
-  UnwrappedLine Final =
-  createUnwrappedLine(*Result.Tokens.front()->Children.front(), Level);
+  UnwrappedLine Final = createUnwrappedLine(
+  *Result.Tokens.front()->Children.front(), Result.Level);
   assert(!Final.Tokens.empty());
   return Final;
 }
@@ -72,7 +72,8 @@ UnwrappedLine MacroCallReconstructor::takeResult() && {
 // ExpandedParent in the incoming unwrapped line. \p First specifies whether it
 // is the first token in a given unwrapped line.
 void MacroCallReconstructor::add(FormatToken *Token,
- FormatToken *ExpandedParent, bool First) {
+ FormatToken *ExpandedParent, bool First,
+ unsigned Level) {
   LLVM_DEBUG(
   llvm::dbgs() << "MCR: Token: " << Token->TokenText << ", Parent: "
<< (ExpandedParent ? ExpandedParent->TokenText : "")
@@ -102,7 +103,7 @@ void MacroCallReconstructor::add(FormatToken *Token,
   First = true;
   }
 
-  prepareParent(ExpandedParent, First);
+  prepareParent(ExpandedParent, First, Level);
 
   if (Token->MacroCtx) {
 // If this token was generated by a macro call, add the reconstructed
@@ -129,7 +130,7 @@ void MacroCallReconstructor::add(FormatToken *Token,
 // is the parent of ActiveReconstructedLines.back() in the reconstructed
 // unwrapped line.
 void MacroCallReconstructor::prepareParent(FormatToken *ExpandedParent,
-   bool NewLine) {
+   bool NewLine, unsigned Level) {
   LLVM_DEBUG({
 llvm::dbgs() << "ParentMap:\n";
 debugParentMap();
@@ -172,7 +173,7 @@ void MacroCallReconstructor::prepareParent(FormatToken 
*ExpandedParent,
 }
 assert(!ActiveReconstructedLines.empty());
 ActiveReconstructedLines.back()->Tokens.back()->Children.push_back(
-std::make_unique());
+std::make_unique(Level));
 ActiveReconstructedLines.push_back(
 &*ActiveReconstructedLines.back()->Tokens.back()->Children.back());
   } else if (parentLine().Tokens.back()->Tok != Parent) {
@@ -424,7 +425,8 @@ bool MacroCallReconstructor::processNextReconstructed() {
   SpelledParentToReconstructedParent[MacroCallStructure.back()
  .ParentLastToken] = Token;
   appendToken(Token);
-  prepareParent

[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-23 Thread Owen Pan via cfe-commits


@@ -420,6 +420,8 @@ struct UnwrappedLineNode {
   SmallVector Children;
 };
 
+std::ostream &operator<<(std::ostream &Stream, const UnwrappedLine &Line);
+

owenca wrote:

Delete.

https://github.com/llvm/llvm-project/pull/82523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-23 Thread Owen Pan via cfe-commits


@@ -19,6 +19,7 @@
 #include "clang/Basic/TokenKinds.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/Support/Debug.h"
+#include 

owenca wrote:

Not needed explicitly.

https://github.com/llvm/llvm-project/pull/82523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-23 Thread Owen Pan via cfe-commits


@@ -287,6 +287,21 @@ TEST_F(FormatTestMacroExpansion,
Style);
 }
 
+TEST_F(FormatTestMacroExpansion, IndentChildrenWithinMacroCall) {
+  FormatStyle Style = getGoogleStyleWithColumns(22);
+  Style.Macros.push_back("MACRO(a, b)=a=(b)");
+  verifyFormat("void f() {\n"
+   "  MACRO(a b, call([] {\n"
+   "  if (expr) {\n"
+   "indent();\n"
+   "  }\n"
+   "}));\n"
+   "}"
+
+   ,

owenca wrote:

```suggestion
   "}",
```

https://github.com/llvm/llvm-project/pull/82523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-23 Thread Owen Pan via cfe-commits


@@ -90,6 +90,12 @@ class ScopedDeclarationState {
 
 } // end anonymous namespace
 
+std::ostream &operator<<(std::ostream &Stream, const UnwrappedLine &Line) {
+  llvm::raw_os_ostream OS(Stream);
+  printLine(OS, Line);
+  return Stream;
+}
+

owenca wrote:

Delete.

https://github.com/llvm/llvm-project/pull/82523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-23 Thread via cfe-commits


@@ -420,6 +420,8 @@ struct UnwrappedLineNode {
   SmallVector Children;
 };
 
+std::ostream &operator<<(std::ostream &Stream, const UnwrappedLine &Line);
+

r4nt wrote:

I intentionally put this there, as otherwise the errors we get from the tests 
are useless. With the operator, the gtest matchers nicely output the expected 
and actual lines, and it's easy to see what's different.

https://github.com/llvm/llvm-project/pull/82523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-23 Thread Owen Pan via cfe-commits


@@ -420,6 +420,8 @@ struct UnwrappedLineNode {
   SmallVector Children;
 };
 
+std::ostream &operator<<(std::ostream &Stream, const UnwrappedLine &Line);
+

owenca wrote:

Ah, thanks for the explanation.

https://github.com/llvm/llvm-project/pull/82523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-23 Thread via cfe-commits

https://github.com/r4nt updated https://github.com/llvm/llvm-project/pull/82523

>From d9b8493f993a4020f3eaf2911b9704e4b25ac6b7 Mon Sep 17 00:00:00 2001
From: Manuel Klimek 
Date: Wed, 21 Feb 2024 14:16:54 +
Subject: [PATCH 1/2] [ClangFormat] Fix indent in child lines within a macro
 argument.

When reconstructing lines from a macro expansion, make sure that
lines at different levels in the expanded code get indented
correctly as part of the macro argument.
---
 clang/lib/Format/MacroCallReconstructor.cpp   |  69 ++
 clang/lib/Format/Macros.h |  10 +-
 clang/lib/Format/UnwrappedLineParser.cpp  |   6 +
 clang/lib/Format/UnwrappedLineParser.h|   2 +
 .../Format/FormatTestMacroExpansion.cpp   |  23 +++-
 .../Format/MacroCallReconstructorTest.cpp | 129 --
 6 files changed, 166 insertions(+), 73 deletions(-)

diff --git a/clang/lib/Format/MacroCallReconstructor.cpp 
b/clang/lib/Format/MacroCallReconstructor.cpp
index cbdd1683c54d1a..5277b406c81b11 100644
--- a/clang/lib/Format/MacroCallReconstructor.cpp
+++ b/clang/lib/Format/MacroCallReconstructor.cpp
@@ -19,6 +19,7 @@
 #include "clang/Basic/TokenKinds.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/Support/Debug.h"
+#include 
 #include 
 
 #define DEBUG_TYPE "format-reconstruct"
@@ -33,7 +34,7 @@ void forEachToken(const UnwrappedLine &Line, const T &Call,
   FormatToken *Parent = nullptr) {
   bool First = true;
   for (const auto &N : Line.Tokens) {
-Call(N.Tok, Parent, First);
+Call(N.Tok, Parent, First, Line.Level);
 First = false;
 for (const auto &Child : N.Children)
   forEachToken(Child, Call, N.Tok);
@@ -44,7 +45,7 @@ MacroCallReconstructor::MacroCallReconstructor(
 unsigned Level,
 const llvm::DenseMap>
 &ActiveExpansions)
-: Level(Level), IdToReconstructed(ActiveExpansions) {
+: Result(Level), IdToReconstructed(ActiveExpansions) {
   Result.Tokens.push_back(std::make_unique());
   ActiveReconstructedLines.push_back(&Result);
 }
@@ -52,9 +53,8 @@ MacroCallReconstructor::MacroCallReconstructor(
 void MacroCallReconstructor::addLine(const UnwrappedLine &Line) {
   assert(State != Finalized);
   LLVM_DEBUG(llvm::dbgs() << "MCR: new line...\n");
-  forEachToken(Line, [&](FormatToken *Token, FormatToken *Parent, bool First) {
-add(Token, Parent, First);
-  });
+  forEachToken(Line, [&](FormatToken *Token, FormatToken *Parent, bool First,
+ unsigned Level) { add(Token, Parent, First, Level); 
});
   assert(InProgress || finished());
 }
 
@@ -62,8 +62,8 @@ UnwrappedLine MacroCallReconstructor::takeResult() && {
   finalize();
   assert(Result.Tokens.size() == 1 &&
  Result.Tokens.front()->Children.size() == 1);
-  UnwrappedLine Final =
-  createUnwrappedLine(*Result.Tokens.front()->Children.front(), Level);
+  UnwrappedLine Final = createUnwrappedLine(
+  *Result.Tokens.front()->Children.front(), Result.Level);
   assert(!Final.Tokens.empty());
   return Final;
 }
@@ -72,7 +72,8 @@ UnwrappedLine MacroCallReconstructor::takeResult() && {
 // ExpandedParent in the incoming unwrapped line. \p First specifies whether it
 // is the first token in a given unwrapped line.
 void MacroCallReconstructor::add(FormatToken *Token,
- FormatToken *ExpandedParent, bool First) {
+ FormatToken *ExpandedParent, bool First,
+ unsigned Level) {
   LLVM_DEBUG(
   llvm::dbgs() << "MCR: Token: " << Token->TokenText << ", Parent: "
<< (ExpandedParent ? ExpandedParent->TokenText : "")
@@ -102,7 +103,7 @@ void MacroCallReconstructor::add(FormatToken *Token,
   First = true;
   }
 
-  prepareParent(ExpandedParent, First);
+  prepareParent(ExpandedParent, First, Level);
 
   if (Token->MacroCtx) {
 // If this token was generated by a macro call, add the reconstructed
@@ -129,7 +130,7 @@ void MacroCallReconstructor::add(FormatToken *Token,
 // is the parent of ActiveReconstructedLines.back() in the reconstructed
 // unwrapped line.
 void MacroCallReconstructor::prepareParent(FormatToken *ExpandedParent,
-   bool NewLine) {
+   bool NewLine, unsigned Level) {
   LLVM_DEBUG({
 llvm::dbgs() << "ParentMap:\n";
 debugParentMap();
@@ -172,7 +173,7 @@ void MacroCallReconstructor::prepareParent(FormatToken 
*ExpandedParent,
 }
 assert(!ActiveReconstructedLines.empty());
 ActiveReconstructedLines.back()->Tokens.back()->Children.push_back(
-std::make_unique());
+std::make_unique(Level));
 ActiveReconstructedLines.push_back(
 &*ActiveReconstructedLines.back()->Tokens.back()->Children.back());
   } else if (parentLine().Tokens.back()->Tok != Parent) {
@@ -424,7 +425,8 @@ bool MacroCallReconstructor::processNextReconstructed() {
   SpelledParentToReconstructedParent[Macro

[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-23 Thread via cfe-commits


@@ -420,6 +420,8 @@ struct UnwrappedLineNode {
   SmallVector Children;
 };
 
+std::ostream &operator<<(std::ostream &Stream, const UnwrappedLine &Line);
+

r4nt wrote:

I can move the operator<< to the test header, but I'd then at least need some 
public method to do the printing.

https://github.com/llvm/llvm-project/pull/82523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-23 Thread via cfe-commits

https://github.com/r4nt updated https://github.com/llvm/llvm-project/pull/82523

>From d9b8493f993a4020f3eaf2911b9704e4b25ac6b7 Mon Sep 17 00:00:00 2001
From: Manuel Klimek 
Date: Wed, 21 Feb 2024 14:16:54 +
Subject: [PATCH 1/3] [ClangFormat] Fix indent in child lines within a macro
 argument.

When reconstructing lines from a macro expansion, make sure that
lines at different levels in the expanded code get indented
correctly as part of the macro argument.
---
 clang/lib/Format/MacroCallReconstructor.cpp   |  69 ++
 clang/lib/Format/Macros.h |  10 +-
 clang/lib/Format/UnwrappedLineParser.cpp  |   6 +
 clang/lib/Format/UnwrappedLineParser.h|   2 +
 .../Format/FormatTestMacroExpansion.cpp   |  23 +++-
 .../Format/MacroCallReconstructorTest.cpp | 129 --
 6 files changed, 166 insertions(+), 73 deletions(-)

diff --git a/clang/lib/Format/MacroCallReconstructor.cpp 
b/clang/lib/Format/MacroCallReconstructor.cpp
index cbdd1683c54d1a..5277b406c81b11 100644
--- a/clang/lib/Format/MacroCallReconstructor.cpp
+++ b/clang/lib/Format/MacroCallReconstructor.cpp
@@ -19,6 +19,7 @@
 #include "clang/Basic/TokenKinds.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/Support/Debug.h"
+#include 
 #include 
 
 #define DEBUG_TYPE "format-reconstruct"
@@ -33,7 +34,7 @@ void forEachToken(const UnwrappedLine &Line, const T &Call,
   FormatToken *Parent = nullptr) {
   bool First = true;
   for (const auto &N : Line.Tokens) {
-Call(N.Tok, Parent, First);
+Call(N.Tok, Parent, First, Line.Level);
 First = false;
 for (const auto &Child : N.Children)
   forEachToken(Child, Call, N.Tok);
@@ -44,7 +45,7 @@ MacroCallReconstructor::MacroCallReconstructor(
 unsigned Level,
 const llvm::DenseMap>
 &ActiveExpansions)
-: Level(Level), IdToReconstructed(ActiveExpansions) {
+: Result(Level), IdToReconstructed(ActiveExpansions) {
   Result.Tokens.push_back(std::make_unique());
   ActiveReconstructedLines.push_back(&Result);
 }
@@ -52,9 +53,8 @@ MacroCallReconstructor::MacroCallReconstructor(
 void MacroCallReconstructor::addLine(const UnwrappedLine &Line) {
   assert(State != Finalized);
   LLVM_DEBUG(llvm::dbgs() << "MCR: new line...\n");
-  forEachToken(Line, [&](FormatToken *Token, FormatToken *Parent, bool First) {
-add(Token, Parent, First);
-  });
+  forEachToken(Line, [&](FormatToken *Token, FormatToken *Parent, bool First,
+ unsigned Level) { add(Token, Parent, First, Level); 
});
   assert(InProgress || finished());
 }
 
@@ -62,8 +62,8 @@ UnwrappedLine MacroCallReconstructor::takeResult() && {
   finalize();
   assert(Result.Tokens.size() == 1 &&
  Result.Tokens.front()->Children.size() == 1);
-  UnwrappedLine Final =
-  createUnwrappedLine(*Result.Tokens.front()->Children.front(), Level);
+  UnwrappedLine Final = createUnwrappedLine(
+  *Result.Tokens.front()->Children.front(), Result.Level);
   assert(!Final.Tokens.empty());
   return Final;
 }
@@ -72,7 +72,8 @@ UnwrappedLine MacroCallReconstructor::takeResult() && {
 // ExpandedParent in the incoming unwrapped line. \p First specifies whether it
 // is the first token in a given unwrapped line.
 void MacroCallReconstructor::add(FormatToken *Token,
- FormatToken *ExpandedParent, bool First) {
+ FormatToken *ExpandedParent, bool First,
+ unsigned Level) {
   LLVM_DEBUG(
   llvm::dbgs() << "MCR: Token: " << Token->TokenText << ", Parent: "
<< (ExpandedParent ? ExpandedParent->TokenText : "")
@@ -102,7 +103,7 @@ void MacroCallReconstructor::add(FormatToken *Token,
   First = true;
   }
 
-  prepareParent(ExpandedParent, First);
+  prepareParent(ExpandedParent, First, Level);
 
   if (Token->MacroCtx) {
 // If this token was generated by a macro call, add the reconstructed
@@ -129,7 +130,7 @@ void MacroCallReconstructor::add(FormatToken *Token,
 // is the parent of ActiveReconstructedLines.back() in the reconstructed
 // unwrapped line.
 void MacroCallReconstructor::prepareParent(FormatToken *ExpandedParent,
-   bool NewLine) {
+   bool NewLine, unsigned Level) {
   LLVM_DEBUG({
 llvm::dbgs() << "ParentMap:\n";
 debugParentMap();
@@ -172,7 +173,7 @@ void MacroCallReconstructor::prepareParent(FormatToken 
*ExpandedParent,
 }
 assert(!ActiveReconstructedLines.empty());
 ActiveReconstructedLines.back()->Tokens.back()->Children.push_back(
-std::make_unique());
+std::make_unique(Level));
 ActiveReconstructedLines.push_back(
 &*ActiveReconstructedLines.back()->Tokens.back()->Children.back());
   } else if (parentLine().Tokens.back()->Tok != Parent) {
@@ -424,7 +425,8 @@ bool MacroCallReconstructor::processNextReconstructed() {
   SpelledParentToReconstructedParent[Macro

[clang] [ClangFormat] Fix indent in child lines within a macro argument. (PR #82523)

2024-02-23 Thread via cfe-commits

https://github.com/r4nt closed https://github.com/llvm/llvm-project/pull/82523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits