[clang] 2b8542c - [clang-format] Correctly count annoated lines of a namespace body

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 10 15:55:10 PDT 2023


Author: Owen Pan
Date: 2023-08-10T15:55:01-07:00
New Revision: 2b8542ce8e8c24dfcdd04333333542cf733556b8

URL: https://github.com/llvm/llvm-project/commit/2b8542ce8e8c24dfcdd04333333542cf733556b8
DIFF: https://github.com/llvm/llvm-project/commit/2b8542ce8e8c24dfcdd04333333542cf733556b8.diff

LOG: [clang-format] Correctly count annoated lines of a namespace body

Fixes #63882.

Differential Revision: https://reviews.llvm.org/D157244

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
index 95eb058d09e198..32c2592834555a 100644
--- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -359,8 +359,10 @@ std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(
         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,

diff  --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h
index 611e95ba11b017..f3e5b397aa78b3 100644
--- a/clang/lib/Format/TokenAnnotator.h
+++ b/clang/lib/Format/TokenAnnotator.h
@@ -91,6 +91,13 @@ class AnnotatedLine {
     }
   }
 
+  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;

diff  --git a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
index b47435787591dd..65876a3c668650 100644
--- a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -1376,6 +1376,22 @@ TEST_F(ShortNamespaceLinesTest, MultipleUnwrappedLine) {
                                     "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) {


        


More information about the cfe-commits mailing list