[PATCH] D157244: [clang-format] Correctly count annoated lines in a namespace body

2023-08-10 Thread Owen Pan via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG2b8542ce8e8c: [clang-format] Correctly count annoated lines 
of a namespace body (authored by owenpan).

Changed prior to commit:
  https://reviews.llvm.org/D157244?vs=547612=549190#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157244/new/

https://reviews.llvm.org/D157244

Files:
  clang/lib/Format/NamespaceEndCommentsFixer.cpp
  clang/lib/Format/TokenAnnotator.h
  clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp


Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
===
--- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -1376,6 +1376,22 @@
 "int k;\n"
 "}\n",
 Style));
+
+  // The namespace body has 5 unwrapped/annotated lines.
+  const std::string NestedLambdas{"namespace foo {\n"
+  "auto bar = [] {\n" // line 1
+  "  int i;\n"// line 2
+  "  return [] {\n"   // line 3
+  "  int j;"  // line 4
+  "  return 0;\n" // line 5
+  "  };\n"// part of line 3
+  "};\n"  // part of line 1
+  "}"};
+  Style.ShortNamespaceLines = 4;
+  EXPECT_EQ(NestedLambdas + " // namespace foo",
+fixNamespaceEndComments(NestedLambdas, Style));
+  ++Style.ShortNamespaceLines;
+  EXPECT_EQ(NestedLambdas, fixNamespaceEndComments(NestedLambdas, Style));
 }
 
 TEST_F(ShortNamespaceLinesTest, NamespaceAlias) {
Index: clang/lib/Format/TokenAnnotator.h
===
--- clang/lib/Format/TokenAnnotator.h
+++ clang/lib/Format/TokenAnnotator.h
@@ -91,6 +91,13 @@
 }
   }
 
+  size_t size() const {
+size_t Size = 1;
+for (const auto *Child : Children)
+  Size += Child->size();
+return Size;
+  }
+
   ~AnnotatedLine() {
 for (AnnotatedLine *Child : Children)
   delete Child;
Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp
===
--- clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -359,8 +359,10 @@
 computeEndCommentText(NamespaceName, AddNewline, NamespaceTok,
   Style.SpacesInLineCommentPrefix.Minimum);
 if (!hasEndComment(EndCommentPrevTok)) {
-  bool isShort = I - StartLineIndex <= Style.ShortNamespaceLines + 1;
-  if (!isShort) {
+  unsigned LineCount = 0;
+  for (auto J = StartLineIndex + 1; J < I; ++J)
+LineCount += AnnotatedLines[J]->size();
+  if (LineCount > Style.ShortNamespaceLines) {
 addEndComment(EndCommentPrevTok,
   std::string(Style.SpacesBeforeTrailingComments, ' ') +
   EndCommentText,


Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
===
--- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -1376,6 +1376,22 @@
 "int k;\n"
 "}\n",
 Style));
+
+  // The namespace body has 5 unwrapped/annotated lines.
+  const std::string NestedLambdas{"namespace foo {\n"
+  "auto bar = [] {\n" // line 1
+  "  int i;\n"// line 2
+  "  return [] {\n"   // line 3
+  "  int j;"  // line 4
+  "  return 0;\n" // line 5
+  "  };\n"// part of line 3
+  "};\n"  // part of line 1
+  "}"};
+  Style.ShortNamespaceLines = 4;
+  EXPECT_EQ(NestedLambdas + " // namespace foo",
+fixNamespaceEndComments(NestedLambdas, Style));
+  ++Style.ShortNamespaceLines;
+  EXPECT_EQ(NestedLambdas, fixNamespaceEndComments(NestedLambdas, Style));
 }
 
 TEST_F(ShortNamespaceLinesTest, NamespaceAlias) {
Index: clang/lib/Format/TokenAnnotator.h
===
--- clang/lib/Format/TokenAnnotator.h
+++ clang/lib/Format/TokenAnnotator.h
@@ -91,6 +91,13 @@
 }
   }
 
+  size_t size() const {
+size_t Size = 1;
+for (const auto *Child : Children)
+  Size += Child->size();
+return Size;
+  }
+
   ~AnnotatedLine() {
 for 

[PATCH] D157244: [clang-format] Correctly count annoated lines in a namespace body

2023-08-06 Thread Owen Pan via Phabricator via cfe-commits
owenpan added inline comments.



Comment at: clang/lib/Format/TokenAnnotator.h:95
+  size_t size() const {
+int Size = 1;
+for (const auto *Child : Children)

Will fix it before landing.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157244/new/

https://reviews.llvm.org/D157244

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157244: [clang-format] Correctly count annoated lines in a namespace body

2023-08-06 Thread Owen Pan via Phabricator via cfe-commits
owenpan created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, MyDeveloperDay.
owenpan requested review of this revision.

Fixes https://github.com/llvm/llvm-project/issues/63882.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157244

Files:
  clang/lib/Format/NamespaceEndCommentsFixer.cpp
  clang/lib/Format/TokenAnnotator.h
  clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp


Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
===
--- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -1376,6 +1376,22 @@
 "int k;\n"
 "}\n",
 Style));
+
+  // The namespace body has 5 unwrapped/annotated lines.
+  const std::string NestedLambdas{"namespace foo {\n"
+  "auto bar = [] {\n" // line 1
+  "  int i;\n"// line 2
+  "  return [] {\n"   // line 3
+  "  int j;"  // line 4
+  "  return 0;\n" // line 5
+  "  };\n"// part of line 3
+  "};\n"  // part of line 1
+  "}"};
+  Style.ShortNamespaceLines = 4;
+  EXPECT_EQ(NestedLambdas + " // namespace foo",
+fixNamespaceEndComments(NestedLambdas, Style));
+  ++Style.ShortNamespaceLines;
+  EXPECT_EQ(NestedLambdas, fixNamespaceEndComments(NestedLambdas, Style));
 }
 
 TEST_F(ShortNamespaceLinesTest, NamespaceAlias) {
Index: clang/lib/Format/TokenAnnotator.h
===
--- clang/lib/Format/TokenAnnotator.h
+++ clang/lib/Format/TokenAnnotator.h
@@ -91,6 +91,13 @@
 }
   }
 
+  size_t size() const {
+int Size = 1;
+for (const auto *Child : Children)
+  Size += Child->size();
+return Size;
+  }
+
   ~AnnotatedLine() {
 for (AnnotatedLine *Child : Children)
   delete Child;
Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp
===
--- clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -359,8 +359,10 @@
 computeEndCommentText(NamespaceName, AddNewline, NamespaceTok,
   Style.SpacesInLineCommentPrefix.Minimum);
 if (!hasEndComment(EndCommentPrevTok)) {
-  bool isShort = I - StartLineIndex <= Style.ShortNamespaceLines + 1;
-  if (!isShort) {
+  unsigned LineCount = 0;
+  for (auto J = StartLineIndex + 1; J < I; ++J)
+LineCount += AnnotatedLines[J]->size();
+  if (LineCount > Style.ShortNamespaceLines) {
 addEndComment(EndCommentPrevTok,
   std::string(Style.SpacesBeforeTrailingComments, ' ') +
   EndCommentText,


Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
===
--- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -1376,6 +1376,22 @@
 "int k;\n"
 "}\n",
 Style));
+
+  // The namespace body has 5 unwrapped/annotated lines.
+  const std::string NestedLambdas{"namespace foo {\n"
+  "auto bar = [] {\n" // line 1
+  "  int i;\n"// line 2
+  "  return [] {\n"   // line 3
+  "  int j;"  // line 4
+  "  return 0;\n" // line 5
+  "  };\n"// part of line 3
+  "};\n"  // part of line 1
+  "}"};
+  Style.ShortNamespaceLines = 4;
+  EXPECT_EQ(NestedLambdas + " // namespace foo",
+fixNamespaceEndComments(NestedLambdas, Style));
+  ++Style.ShortNamespaceLines;
+  EXPECT_EQ(NestedLambdas, fixNamespaceEndComments(NestedLambdas, Style));
 }
 
 TEST_F(ShortNamespaceLinesTest, NamespaceAlias) {
Index: clang/lib/Format/TokenAnnotator.h
===
--- clang/lib/Format/TokenAnnotator.h
+++ clang/lib/Format/TokenAnnotator.h
@@ -91,6 +91,13 @@
 }
   }
 
+  size_t size() const {
+int Size = 1;
+for (const auto *Child : Children)
+  Size += Child->size();
+return Size;
+  }
+
   ~AnnotatedLine() {
 for (AnnotatedLine *Child : Children)
   delete Child;
Index: