[PATCH] D157244: [clang-format] Correctly count annoated lines in a namespace body
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 6 16:18:36 PDT 2023
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,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157244.547612.patch
Type: text/x-patch
Size: 2676 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230806/d2815b21/attachment.bin>
More information about the cfe-commits
mailing list