[clang] [clang-format] Fix a bug in annotating angles containing FatArrow (PR #108671)
@@ -243,14 +244,16 @@ class AnnotatingParser { // operator that was misinterpreted because we are parsing template // parameters. // FIXME: This is getting out of hand, write a decent parser. - if (InExpr && !Line.startsWith(tok::kw_template) && + if (InExpr && !SeenFatArrow && !Line.startsWith(tok::kw_template) && Prev.is(TT_BinaryOperator)) { const auto Precedence = Prev.getPrecedence(); if (Precedence > prec::Conditional && Precedence < prec::Relational) return false; } if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto()) SeenTernaryOperator = true; + else if (Prev.is(TT_FatArrow)) owenca wrote: > hence I am not expecting a "fix" for the issue here, but I guess a narrowed > down version of [other > patch](https://github.com/llvm/llvm-project/commit/73c961a3345c697f40e2148318f34f5f347701c1), > where it actually preserves behavior with old clang-format in uncertain > cases. This patch IMO *is* a narrowed down version of the other patch #100980, which fixed a real regression #100300 caused by yet another patch #96801, which in turn was an attempt at fixing a real bug #81385. In your [reply](https://github.com/llvm/llvm-project/pull/108671#discussion_r1762725421) above, it doesn't seem that your C++ example `return FixedInt(foo);` is from an existing codebase. The fact that it happened to get formatted that way before doesn't necessarily mean that we should always format another similar pattern e.g. `return Foo < A || B > (C ^ D);` the same way, especially if the latter is much more likely. https://github.com/llvm/llvm-project/pull/108671 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -616,6 +626,62 @@ class LineJoiner { return 1; } + unsigned tryMergeNamespace(SmallVectorImpl::const_iterator I, + SmallVectorImpl::const_iterator E, + unsigned Limit) { +if (Limit == 0) + return 0; +if (I[1]->InPPDirective != (*I)->InPPDirective || +(I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) { + return 0; +} +if (I + 2 == E || I[2]->Type == LT_Invalid) + return 0; + +Limit = limitConsideringMacros(I + 1, E, Limit); + +if (!nextTwoLinesFitInto(I, Limit)) + return 0; + +// Check if it's a namespace inside a namespace, and call recursively if so +// '3' is the sizes of the whitespace and closing brace for " _inner_ }". +if (I[1]->First->is(tok::kw_namespace)) { + if (I[1]->Last->is(TT_LineComment)) owenca wrote: ```suggestion if (I[1]->Last->is(tok::comment)) ``` Does it make sense to merge if there's a trailing block comment? https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -27981,6 +27981,132 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, ShortNamespacesOption) { + auto BaseStyle = getLLVMStyle(); + BaseStyle.AllowShortNamespacesOnASingleLine = true; + BaseStyle.FixNamespaceComments = false; + + auto Style = BaseStyle; + + // Basic functionality. + verifyFormat("namespace foo { class bar; }", Style); + verifyFormat("namespace foo::bar { class baz; }", Style); + verifyFormat("namespace { class bar; }", Style); + verifyFormat("namespace foo {\n" + "class bar;\n" + "class baz;\n" + "}", + Style); + + // Trailing comments prevent merging. + verifyFormat("namespace foo {\n" + "namespace baz { class qux; } // comment\n" + "}", + Style); + + // Make sure code doesn't walk too far on unbalanced code. + verifyFormat("namespace foo {", Style); + verifyFormat("namespace foo {\n" + "class baz;", + Style); + verifyFormat("namespace foo {\n" + "namespace bar { class baz; }", + Style); + + // Nested namespaces. + verifyFormat("namespace foo { namespace bar { class baz; } }", Style); + verifyFormat("namespace foo {\n" + "namespace bar { class baz; }\n" + "namespace qux { class quux; }\n" + "}", + Style); + + // Varying inner content. + verifyFormat("namespace foo {\n" + "int f() { return 5; }\n" + "}", + Style); + verifyFormat("namespace foo { template struct bar; }", Style); + verifyFormat("namespace foo { constexpr int num = 42; }", Style); + + // Validate wrapping scenarios around the ColumnLimit. + Style.ColumnLimit = 64; + + // Validate just under the ColumnLimit. + verifyFormat( + "namespace foo { namespace bar { namespace baz { class qux; } } }", + Style); + + // Validate just over the ColumnLimit. + verifyFormat("namespace foo {\n" + "namespace bar { namespace baz { class quux; } }\n" + "}", + Style); + + verifyFormat("namespace foo {\n" + "namespace bar {\n" + "namespace baz { namespace qux { class quux; } }\n" + "}\n" + "}", + Style); + + // Validate that the ColumnLimit logic accounts for trailing content as well. + verifyFormat("namespace foo {\n" + "namespace bar { namespace baz { class qux; } }\n" + "} // extra", + Style); + + // No ColumnLimit, allows long nested one-liners, but also leaves multi-line + // instances alone. + Style.ColumnLimit = 0; + verifyFormat( + "namespace foo { namespace bar { namespace baz { class qux; } } }", + Style); + + verifyNoChange("namespace foo {\n" + "namespace bar { namespace baz { class qux; } }\n" + "}", + Style); owenca wrote: ```suggestion // FIXME #if 0 verifyNoChange("namespace foo {\n" "namespace bar { namespace baz { class qux; } }\n" "}", Style); #endif ``` Shouldn't this be merged as well? See this potential [bug](https://discourse.llvm.org/t/clang-format-compactnamespaces-in-combination-with-columnlimit-0/78808). https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -616,6 +626,62 @@ class LineJoiner { return 1; } + unsigned tryMergeNamespace(SmallVectorImpl::const_iterator I, + SmallVectorImpl::const_iterator E, + unsigned Limit) { +if (Limit == 0) + return 0; +if (I[1]->InPPDirective != (*I)->InPPDirective || +(I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) { + return 0; +} +if (I + 2 == E || I[2]->Type == LT_Invalid) + return 0; + +Limit = limitConsideringMacros(I + 1, E, Limit); + +if (!nextTwoLinesFitInto(I, Limit)) + return 0; + +// Check if it's a namespace inside a namespace, and call recursively if so +// '3' is the sizes of the whitespace and closing brace for " _inner_ }". +if (I[1]->First->is(tok::kw_namespace)) { + if (I[1]->Last->is(TT_LineComment)) +return 0; + + const unsigned InnerLimit = Limit - I[1]->Last->TotalLength - 3; + const unsigned MergedLines = tryMergeNamespace(I + 1, E, InnerLimit); + if (!MergedLines) owenca wrote: ```suggestion const int InnerLimit = Limit - I[1]->Last->TotalLength - 3; const auto MergedLines = tryMergeNamespace(I + 1, E, InnerLimit); if (MergedLines == 0) ``` Use `int` to guard against underflow. https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -616,6 +626,62 @@ class LineJoiner { return 1; } + unsigned tryMergeNamespace(SmallVectorImpl::const_iterator I, + SmallVectorImpl::const_iterator E, + unsigned Limit) { +if (Limit == 0) + return 0; +if (I[1]->InPPDirective != (*I)->InPPDirective || +(I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) { + return 0; +} +if (I + 2 == E || I[2]->Type == LT_Invalid) + return 0; + +Limit = limitConsideringMacros(I + 1, E, Limit); + +if (!nextTwoLinesFitInto(I, Limit)) + return 0; + +// Check if it's a namespace inside a namespace, and call recursively if so +// '3' is the sizes of the whitespace and closing brace for " _inner_ }". +if (I[1]->First->is(tok::kw_namespace)) { + if (I[1]->Last->is(TT_LineComment)) +return 0; + + const unsigned InnerLimit = Limit - I[1]->Last->TotalLength - 3; + const unsigned MergedLines = tryMergeNamespace(I + 1, E, InnerLimit); + if (!MergedLines) +return 0; + // Check if there is even a line after the inner result. + if (std::distance(I, E) <= MergedLines + 2) +return 0; + // Check that the line after the inner result starts with a closing brace + // which we are permitted to merge into one line. + if (I[2 + MergedLines]->First->is(tok::r_brace) && + !I[2 + MergedLines]->First->MustBreakBefore && + !I[1 + MergedLines]->Last->is(TT_LineComment) && + nextNLinesFitInto(I, I + 2 + MergedLines + 1, Limit)) { +return 2 + MergedLines; owenca wrote: ```suggestion if (I[N]->First->is(tok::r_brace) && !I[N]->First->MustBreakBefore && I[MergedLines + 1]->Last->isNot(tok::comment) && nextNLinesFitInto(I, I + N + 1, Limit)) { return N; ``` https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -616,6 +626,62 @@ class LineJoiner { return 1; } + unsigned tryMergeNamespace(SmallVectorImpl::const_iterator I, + SmallVectorImpl::const_iterator E, + unsigned Limit) { +if (Limit == 0) + return 0; +if (I[1]->InPPDirective != (*I)->InPPDirective || +(I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) { + return 0; +} +if (I + 2 == E || I[2]->Type == LT_Invalid) + return 0; + +Limit = limitConsideringMacros(I + 1, E, Limit); + +if (!nextTwoLinesFitInto(I, Limit)) + return 0; + +// Check if it's a namespace inside a namespace, and call recursively if so +// '3' is the sizes of the whitespace and closing brace for " _inner_ }". +if (I[1]->First->is(tok::kw_namespace)) { + if (I[1]->Last->is(TT_LineComment)) +return 0; + + const unsigned InnerLimit = Limit - I[1]->Last->TotalLength - 3; + const unsigned MergedLines = tryMergeNamespace(I + 1, E, InnerLimit); + if (!MergedLines) +return 0; owenca wrote: ```suggestion return 0; const auto N = MergedLines + 2; ``` https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -616,6 +626,62 @@ class LineJoiner { return 1; } + unsigned tryMergeNamespace(SmallVectorImpl::const_iterator I, + SmallVectorImpl::const_iterator E, + unsigned Limit) { +if (Limit == 0) + return 0; +if (I[1]->InPPDirective != (*I)->InPPDirective || +(I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) { + return 0; +} +if (I + 2 == E || I[2]->Type == LT_Invalid) + return 0; + +Limit = limitConsideringMacros(I + 1, E, Limit); + +if (!nextTwoLinesFitInto(I, Limit)) + return 0; + +// Check if it's a namespace inside a namespace, and call recursively if so owenca wrote: ```suggestion // Check if it's a namespace inside a namespace, and call recursively if so. ``` https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -616,6 +626,62 @@ class LineJoiner { return 1; } + unsigned tryMergeNamespace(SmallVectorImpl::const_iterator I, + SmallVectorImpl::const_iterator E, + unsigned Limit) { +if (Limit == 0) owenca wrote: ```suggestion int Limit) { if (Limit <= 0) ``` See below. https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -361,9 +361,18 @@ class LineJoiner { const auto *FirstNonComment = TheLine->getFirstNonComment(); if (!FirstNonComment) return 0; + // FIXME: There are probably cases where we should use FirstNonComment // instead of TheLine->First. +if (TheLine->Last->is(tok::l_brace)) { + if (Style.AllowShortNamespacesOnASingleLine && + TheLine->First->is(tok::kw_namespace)) { +if (unsigned result = tryMergeNamespace(I, E, Limit)) + return result; owenca wrote: ```suggestion unsigned result = tryMergeNamespace(I, E, Limit); if (result > 0); return result; ``` https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -616,6 +626,62 @@ class LineJoiner { return 1; } + unsigned tryMergeNamespace(SmallVectorImpl::const_iterator I, + SmallVectorImpl::const_iterator E, + unsigned Limit) { +if (Limit == 0) + return 0; +if (I[1]->InPPDirective != (*I)->InPPDirective || +(I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) { + return 0; +} +if (I + 2 == E || I[2]->Type == LT_Invalid) + return 0; + +Limit = limitConsideringMacros(I + 1, E, Limit); + +if (!nextTwoLinesFitInto(I, Limit)) + return 0; + +// Check if it's a namespace inside a namespace, and call recursively if so +// '3' is the sizes of the whitespace and closing brace for " _inner_ }". +if (I[1]->First->is(tok::kw_namespace)) { + if (I[1]->Last->is(TT_LineComment)) +return 0; + + const unsigned InnerLimit = Limit - I[1]->Last->TotalLength - 3; + const unsigned MergedLines = tryMergeNamespace(I + 1, E, InnerLimit); + if (!MergedLines) +return 0; + // Check if there is even a line after the inner result. + if (std::distance(I, E) <= MergedLines + 2) owenca wrote: ```suggestion if (std::distance(I, E) <= N) ``` https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -27981,6 +27981,132 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, ShortNamespacesOption) { + auto BaseStyle = getLLVMStyle(); + BaseStyle.AllowShortNamespacesOnASingleLine = true; + BaseStyle.FixNamespaceComments = false; + + auto Style = BaseStyle; + + // Basic functionality. + verifyFormat("namespace foo { class bar; }", Style); + verifyFormat("namespace foo::bar { class baz; }", Style); + verifyFormat("namespace { class bar; }", Style); + verifyFormat("namespace foo {\n" + "class bar;\n" + "class baz;\n" + "}", + Style); + + // Trailing comments prevent merging. + verifyFormat("namespace foo {\n" + "namespace baz { class qux; } // comment\n" + "}", + Style); + + // Make sure code doesn't walk too far on unbalanced code. + verifyFormat("namespace foo {", Style); + verifyFormat("namespace foo {\n" + "class baz;", + Style); + verifyFormat("namespace foo {\n" + "namespace bar { class baz; }", + Style); + + // Nested namespaces. + verifyFormat("namespace foo { namespace bar { class baz; } }", Style); + verifyFormat("namespace foo {\n" + "namespace bar { class baz; }\n" + "namespace qux { class quux; }\n" + "}", + Style); + + // Varying inner content. + verifyFormat("namespace foo {\n" + "int f() { return 5; }\n" + "}", + Style); + verifyFormat("namespace foo { template struct bar; }", Style); + verifyFormat("namespace foo { constexpr int num = 42; }", Style); + + // Validate wrapping scenarios around the ColumnLimit. + Style.ColumnLimit = 64; + + // Validate just under the ColumnLimit. + verifyFormat( + "namespace foo { namespace bar { namespace baz { class qux; } } }", + Style); + + // Validate just over the ColumnLimit. + verifyFormat("namespace foo {\n" + "namespace bar { namespace baz { class quux; } }\n" + "}", + Style); + + verifyFormat("namespace foo {\n" + "namespace bar {\n" + "namespace baz { namespace qux { class quux; } }\n" + "}\n" + "}", + Style); + + // Validate that the ColumnLimit logic accounts for trailing content as well. + verifyFormat("namespace foo {\n" + "namespace bar { namespace baz { class qux; } }\n" + "} // extra", + Style); + + // No ColumnLimit, allows long nested one-liners, but also leaves multi-line + // instances alone. + Style.ColumnLimit = 0; + verifyFormat( + "namespace foo { namespace bar { namespace baz { class qux; } } }", + Style); + + verifyNoChange("namespace foo {\n" + "namespace bar { namespace baz { class qux; } }\n" + "}", + Style); + + Style = BaseStyle; + Style.CompactNamespaces = true; + verifyFormat("namespace foo { namespace bar { class baz; } }", Style); + + // If we can't merge an outer nested namespaces, but can merge an inner + // nested namespace, then CompactNamespaces will merge the outer namespace + // first, preventing the merging of the inner namespace owenca wrote: ```suggestion // first, preventing the merging of the inner namespace. ``` https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -27981,6 +27981,132 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, ShortNamespacesOption) { + auto BaseStyle = getLLVMStyle(); + BaseStyle.AllowShortNamespacesOnASingleLine = true; + BaseStyle.FixNamespaceComments = false; + + auto Style = BaseStyle; + + // Basic functionality. + verifyFormat("namespace foo { class bar; }", Style); + verifyFormat("namespace foo::bar { class baz; }", Style); + verifyFormat("namespace { class bar; }", Style); + verifyFormat("namespace foo {\n" + "class bar;\n" + "class baz;\n" + "}", + Style); + + // Trailing comments prevent merging. + verifyFormat("namespace foo {\n" + "namespace baz { class qux; } // comment\n" + "}", + Style); + + // Make sure code doesn't walk too far on unbalanced code. + verifyFormat("namespace foo {", Style); + verifyFormat("namespace foo {\n" + "class baz;", + Style); + verifyFormat("namespace foo {\n" + "namespace bar { class baz; }", + Style); + + // Nested namespaces. + verifyFormat("namespace foo { namespace bar { class baz; } }", Style); + verifyFormat("namespace foo {\n" + "namespace bar { class baz; }\n" + "namespace qux { class quux; }\n" + "}", + Style); + + // Varying inner content. + verifyFormat("namespace foo {\n" + "int f() { return 5; }\n" + "}", + Style); + verifyFormat("namespace foo { template struct bar; }", Style); + verifyFormat("namespace foo { constexpr int num = 42; }", Style); + + // Validate wrapping scenarios around the ColumnLimit. + Style.ColumnLimit = 64; + + // Validate just under the ColumnLimit. + verifyFormat( + "namespace foo { namespace bar { namespace baz { class qux; } } }", + Style); + + // Validate just over the ColumnLimit. + verifyFormat("namespace foo {\n" + "namespace bar { namespace baz { class quux; } }\n" + "}", + Style); + + verifyFormat("namespace foo {\n" + "namespace bar {\n" + "namespace baz { namespace qux { class quux; } }\n" + "}\n" + "}", + Style); + + // Validate that the ColumnLimit logic accounts for trailing content as well. + verifyFormat("namespace foo {\n" + "namespace bar { namespace baz { class qux; } }\n" + "} // extra", + Style); + + // No ColumnLimit, allows long nested one-liners, but also leaves multi-line + // instances alone. + Style.ColumnLimit = 0; + verifyFormat( + "namespace foo { namespace bar { namespace baz { class qux; } } }", + Style); + + verifyNoChange("namespace foo {\n" + "namespace bar { namespace baz { class qux; } }\n" + "}", + Style); + + Style = BaseStyle; + Style.CompactNamespaces = true; + verifyFormat("namespace foo { namespace bar { class baz; } }", Style); + + // If we can't merge an outer nested namespaces, but can merge an inner + // nested namespace, then CompactNamespaces will merge the outer namespace + // first, preventing the merging of the inner namespace + verifyFormat("namespace foo { namespace baz {\n" + "class qux;\n" + "} // comment\n" + "}", + Style); + + // This option doesn't really work with FixNamespaceComments and nested + // namespaces. Code should use the concatenated namespace syntax. e.g. + // 'namespace foo::bar'. + Style = BaseStyle; + Style.FixNamespaceComments = true; + + verifyFormat( + "namespace foo {\n" + "namespace bar { namespace baz { class qux; } } // namespace bar\n" + "} // namespace foo", + "namespace foo { namespace bar { namespace baz { class qux; } } }", + Style); + + // This option doesn't really make any sense with ShortNamespaceLines = 0. + Style.ShortNamespaceLines = 0; + + verifyFormat( + "namespace foo {\n" + "namespace bar {\n" + "namespace baz { class qux; } // namespace baz\n" + "} // namespace bar\n" + "} // namespace foo", + "namespace foo { namespace bar { namespace baz { class qux; } } }", + Style); +} owenca wrote: IMO it makes more sense that `AllowShortNamespacesOnASingleLine` disables `FixNamespaceComments` on the merged closing braces. https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -972,6 +972,11 @@ struct FormatStyle { /// \version 3.7 bool AllowShortLoopsOnASingleLine; + /// If ``true``, ``namespace a { class b; }`` can be put on a single a single + /// line. owenca wrote: ```suggestion /// If ``true``, ``namespace a { class b; }`` can be put on a single line. ``` https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add AlignFunctionDeclarations attribute to AlignConsecutiveDeclarations (PR #108241)
@@ -409,6 +409,21 @@ the configuration (without a prefix: ``Auto``). int *p; int (*f)(); + * ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations owenca wrote: Version badge is unsupported for sub-options. https://github.com/llvm/llvm-project/pull/108241 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add AlignFunctionDeclarations attribute to AlignConsecutiveDeclarations (PR #108241)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/108241 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add AlignFunctionDeclarations attribute to AlignConsecutiveDeclarations (PR #108241)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/108241 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add AlignFunctionDeclarations attribute to AlignConsecutiveDeclarations (PR #108241)
@@ -225,6 +225,21 @@ struct FormatStyle { /// bbb = 2; /// \endcode bool AlignCompound; +/// Only for ``AlignConsecutiveDeclarations``. Whether function declarations +/// are aligned. +/// \code +/// true: +/// unsigned int f1(void); +/// void f2(void); +/// size_t f3(void); +/// +/// false: +/// unsigned int f1(void); +/// void f2(void); +/// size_t f3(void); +/// \endcode +/// \version 20 owenca wrote: GitHub review comments is about the line above unless a range of lines are shown like in https://github.com/llvm/llvm-project/pull/106145/files#r1769761150. https://github.com/llvm/llvm-project/pull/108241 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix regression with BlockIndent of Braced Initializers (PR #108717)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/108717 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix regression with BlockIndent of Braced Initializers (PR #108717)
https://github.com/owenca approved this pull request. https://github.com/llvm/llvm-project/pull/108717 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add AlignFunctionDeclarations attribute to AlignConsecutiveDeclarations (PR #108241)
owenca wrote: > In regards to the "Test documentation build / Test documentation build > (pull_request) " failure, I'm honestly not sure how to fix the formatting in > this. I'm not seeing anything wrong with my addition in Format.h, but maybe > there's something I'm missing. I never touch ClangFormatStyleOptions.rst > directly. See https://github.com/llvm/llvm-project/pull/108241/files#r1770622313. https://github.com/llvm/llvm-project/pull/108241 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add AlignFunctionDeclarations attribute to AlignConsecutiveDeclarations (PR #108241)
@@ -225,6 +225,21 @@ struct FormatStyle { /// bbb = 2; /// \endcode bool AlignCompound; +/// Only for ``AlignConsecutiveDeclarations``. Whether function declarations +/// are aligned. +/// \code +/// true: +/// unsigned int f1(void); +/// void f2(void); +/// size_t f3(void); +/// +/// false: +/// unsigned int f1(void); +/// void f2(void); +/// size_t f3(void); +/// \endcode +/// \version 20 owenca wrote: Delete. https://github.com/llvm/llvm-project/pull/108241 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", owenca wrote: Ditto. https://github.com/llvm/llvm-project/pull/106145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" owenca wrote: ```suggestion // Nested namespaces. verifyFormat("namespace N1 {\n" ``` https://github.com/llvm/llvm-project/pull/106145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; owenca wrote: Delete as it's irrelevant. https://github.com/llvm/llvm-project/pull/106145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); owenca wrote: ```suggestion auto Style = getLLVMStyle(); ``` https://github.com/llvm/llvm-project/pull/106145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Empty namespace + verifyNoChange("namespace N1 { namespace N2 {\n" + "}};", + Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { owenca wrote: Please make similar changes where applicable. https://github.com/llvm/llvm-project/pull/106145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Empty namespace + verifyNoChange("namespace N1 { namespace N2 {\n" + "}};", + Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 0; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "\n" + "int f1(int a) { return 2 * a; }\n" + "\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "}}}", + Style); + + Style.MaxEmptyLinesToKeep = 2; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "\n" + "\n" + "void function()\n" + "\n" + "\n" + "};", + Style); + + // Nested namespace + verifyFormat("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}\n" + "}\n" + "}", + "namespace N1 {\n" + "namespace N2 {\n" + "\n" + "namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}\n" + "\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 0; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Leave; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single name
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; owenca wrote: Delete as it's already so. https://github.com/llvm/llvm-project/pull/106145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Empty namespace + verifyNoChange("namespace N1 { namespace N2 {\n" + "}};", + Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 0; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "\n" + "int f1(int a) { return 2 * a; }\n" + "\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "}}}", + Style); + + Style.MaxEmptyLinesToKeep = 2; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "\n" + "\n" + "void function()\n" + "\n" + "\n" + "};", + Style); + + // Nested namespace + verifyFormat("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}\n" + "}\n" + "}", + "namespace N1 {\n" + "namespace N2 {\n" + "\n" + "namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}\n" + "\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { + FormatStyle Style = getLLVMStyle(); owenca wrote: ```suggestion auto Style = getLLVMStyle(); ``` https://github.com/llvm/llvm-project/pull/106145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Empty namespace + verifyNoChange("namespace N1 { namespace N2 {\n" + "}};", + Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 0; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "\n" + "int f1(int a) { return 2 * a; }\n" + "\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "}}}", + Style); + + Style.MaxEmptyLinesToKeep = 2; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "\n" + "\n" + "void function()\n" + "\n" + "\n" + "};", + Style); + + // Nested namespace + verifyFormat("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}\n" + "}\n" + "}", + "namespace N1 {\n" + "namespace N2 {\n" + "\n" + "namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}\n" + "\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 0; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Leave; + Style.CompactNamespaces = false; owenca wrote: Delete. https://github.com/llvm/llvm-project/pull/10614
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Empty namespace + verifyNoChange("namespace N1 { namespace N2 {\n" + "}};", + Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 0; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "\n" + "int f1(int a) { return 2 * a; }\n" + "\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "}}}", + Style); + + Style.MaxEmptyLinesToKeep = 2; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "\n" + "\n" + "void function()\n" + "\n" + "\n" + "};", + Style); + + // Nested namespace + verifyFormat("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}\n" + "}\n" + "}", + "namespace N1 {\n" + "namespace N2 {\n" + "\n" + "namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}\n" + "\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 0; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Leave; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single name
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Empty namespace + verifyNoChange("namespace N1 { namespace N2 {\n" + "}};", + Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 0; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "\n" + "int f1(int a) { return 2 * a; }\n" + "\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "}}}", + Style); + + Style.MaxEmptyLinesToKeep = 2; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "\n" + "\n" + "void function()\n" + "\n" + "\n" + "};", + Style); + + // Nested namespace + verifyFormat("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}\n" + "}\n" + "}", + "namespace N1 {\n" + "namespace N2 {\n" + "\n" + "namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}\n" + "\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "\n" + "\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "\n" + "\n" + "}}}", + Style); +} + +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 0; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Leave; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single name
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Empty namespace + verifyNoChange("namespace N1 { namespace N2 {\n" + "}};", + Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}}}", + Style); +} owenca wrote: Please modify the above tests to test removing empty lines, e.g.: ``` verifyFormat("namespace N {\n" "int f1(int a) { return 2 * a; }\n" "}", "namespace N {\n" "\n" "int f1(int a) { return 2 * a; }\n" "\n" "}", Style); ``` https://github.com/llvm/llvm-project/pull/106145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
@@ -28104,6 +28104,253 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { + FormatStyle Style = getLLVMStyle(); + Style.FixNamespaceComments = false; + Style.ShortNamespaceLines = 0; + Style.MaxEmptyLinesToKeep = 2; + Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; + Style.CompactNamespaces = false; + + // Empty namespace + verifyNoChange("namespace N {};", Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 {\n" + "namespace N2 {\n" + "namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}\n" + "}\n" + "}", + Style); + + Style.CompactNamespaces = true; + + // Empty namespace + verifyNoChange("namespace N1 { namespace N2 {\n" + "}};", + Style); + + // Single namespace + verifyNoChange("namespace N {\n" + "int f1(int a) { return 2 * a; }\n" + "};", + Style); + + // Nested namespace + verifyNoChange("namespace N1 { namespace N2 { namespace N3 {\n" + "int f1() {\n" + " int a = 1;\n" + " return a;\n" + "}\n" + "}}}", owenca wrote: ```suggestion // Nested namespaces. verifyFormat("namespace N1 { namespace N2 {\n" "int a = 1;\n" "}}", ``` https://github.com/llvm/llvm-project/pull/106145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix regression with BlockIndent of Braced Initializers (PR #108717)
@@ -348,6 +348,13 @@ bool ContinuationIndenter::canBreak(const LineState &State) { } } + // Don't allow breaking before a closing right brace of a block-indented + // braced list initializer if there was not already a break. owenca wrote: ```suggestion // Don't allow breaking before a closing brace of a block-indented braced list // initializer if there isn't already a break. ``` https://github.com/llvm/llvm-project/pull/108717 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix regression with BlockIndent of Braced Initializers (PR #108717)
@@ -9336,6 +9336,9 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) { "ccc(a, //\n" "b));", Style); + verifyFormat("aaa const aa{\n" + "a(aaa, )};", + Style); owenca wrote: This test seems irrelevant and IMO ought to be deleted. https://github.com/llvm/llvm-project/pull/108717 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix regression with BlockIndent of Braced Initializers (PR #108717)
@@ -9395,6 +9398,9 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) { "fooo(new FOO::BA(\n" "XXXZ()));", Style); + verifyFormat("aaa const aa{\n" + "a(aaa, )};", + Style); owenca wrote: Ditto. https://github.com/llvm/llvm-project/pull/108717 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Annotate the l_paren of function pointer types (PR #109229)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/109229 >From 84c166dfabc3f314cd922baa3933b3d0ea11e08e Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Wed, 18 Sep 2024 21:03:24 -0700 Subject: [PATCH] [clang-format] Annotate the l_paren of function pointer types Fixes #109146. --- clang/lib/Format/TokenAnnotator.cpp | 14 -- clang/unittests/Format/TokenAnnotatorTest.cpp | 6 ++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 6f09835bad3a83..9e5f5588592199 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -486,16 +486,18 @@ class AnnotatingParser { } } - if (CurrentToken->Previous->is(TT_PointerOrReference) && - CurrentToken->Previous->Previous->isOneOf(tok::l_paren, -tok::coloncolon)) { + const auto &Prev = *CurrentToken->Previous; + if (Prev.is(TT_PointerOrReference) && + Prev.Previous->isOneOf(tok::l_paren, tok::coloncolon)) { ProbablyFunctionType = true; } if (CurrentToken->is(tok::comma)) MightBeFunctionType = false; - if (CurrentToken->Previous->is(TT_BinaryOperator)) + if (Prev.is(TT_BinaryOperator)) Contexts.back().IsExpression = true; if (CurrentToken->is(tok::r_paren)) { +if (Prev.is(TT_PointerOrReference) && Prev.Previous == &OpeningParen) + MightBeFunctionType = true; if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType && ProbablyFunctionType && CurrentToken->Next && (CurrentToken->Next->is(tok::l_paren) || @@ -568,8 +570,8 @@ class AnnotatingParser { bool ProbablyFunctionTypeLParen = (CurrentToken->is(tok::l_paren) && CurrentToken->Next && CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret)); - if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) || - CurrentToken->Previous->isTypeName(LangOpts)) && + if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) || + Prev.isTypeName(LangOpts)) && !(CurrentToken->is(tok::l_brace) || (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) { Contexts.back().IsExpression = false; diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 34c03d668a9a0a..b5f03d4f851e6e 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -789,6 +789,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) { ASSERT_EQ(Tokens.size(), 13u) << Tokens; EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_CastRParen); + Tokens = annotate("return (Foo (*)(void *, Bar, ...))&foo;"); + ASSERT_EQ(Tokens.size(), 19u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionTypeLParen); + EXPECT_TOKEN(Tokens[14], tok::r_paren, TT_CastRParen); + EXPECT_TOKEN(Tokens[15], tok::amp, TT_UnaryOperator); + auto Style = getLLVMStyle(); Style.TypeNames.push_back("Foo"); Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Correctly annotate */& in if condition with braced init (PR #109505)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/109505 Fixes #109371. >From 192deb4adc9f7e77167a02c060eef8c91932b912 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Fri, 20 Sep 2024 20:28:31 -0700 Subject: [PATCH] [clang-format] Correctly annotate */& in if condition with braced init Fixes #109371. --- clang/lib/Format/TokenAnnotator.cpp | 25 --- clang/unittests/Format/TokenAnnotatorTest.cpp | 5 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index f665ce2ad81eb0..f5acfaaee900a2 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1165,19 +1165,26 @@ class AnnotatingParser { ScopedContextCreator ContextCreator(*this, tok::l_brace, 1); Contexts.back().ColonIsDictLiteral = true; -if (OpeningBrace.is(BK_BracedInit)) + +const auto *Prev = OpeningBrace.getPreviousNonComment(); + +if (OpeningBrace.is(BK_BracedInit)) { Contexts.back().IsExpression = true; -if (Style.isJavaScript() && OpeningBrace.Previous && -OpeningBrace.Previous->is(TT_JsTypeColon)) { - Contexts.back().IsExpression = false; -} -if (Style.isVerilog() && -(!OpeningBrace.getPreviousNonComment() || - OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) { - Contexts.back().VerilogMayBeConcatenation = true; + if (Prev) { +for (auto *Tok = Prev->Previous; Tok && Tok->isPointerOrReference(); + Tok = Tok->Previous) { + Tok->setFinalizedType(TT_PointerOrReference); +} + } } + +if (Style.isJavaScript() && Prev && Prev->is(TT_JsTypeColon)) + Contexts.back().IsExpression = false; + if (Style.isTableGen()) Contexts.back().ColonIsDictLiteral = false; +else if (Style.isVerilog() && !(Prev && Prev->is(Keywords.kw_apostrophe))) + Contexts.back().VerilogMayBeConcatenation = true; unsigned CommaCount = 0; while (CurrentToken) { diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 1884d41a5f23f5..e2c9ba01794caf 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -308,6 +308,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) { EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference); EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference); + Tokens = annotate("if (Foo *&foo{a})"); + ASSERT_EQ(Tokens.size(), 11u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference); + EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference); + FormatStyle Style = getLLVMStyle(); Style.TypeNames.push_back("MYI"); Tokens = annotate("if (MYI *p{nullptr})", Style); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Correctly annotate pointer/reference in `if` statement (PR #109370)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/109370 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Correctly annotate pointer/reference in range-for loop (PR #109361)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/109361 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Correctly annotate pointer/reference in `if` statement (PR #109370)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/109370 Fixes #60146. >From bbe60a2126bd90835738485955fbe05f530afb6e Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Thu, 19 Sep 2024 21:12:06 -0700 Subject: [PATCH] [clang-format] Correctly annotate pointer/reference in `if` statement Fixes #60146. --- clang/lib/Format/TokenAnnotator.cpp | 34 +++ clang/unittests/Format/TokenAnnotatorTest.cpp | 14 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 6f09835bad3a83..83f5e624d41416 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -300,7 +300,7 @@ class AnnotatingParser { return false; } - bool parseParens(bool LookForDecls = false) { + bool parseParens(bool IsIf = false) { if (!CurrentToken) return false; assert(CurrentToken->Previous && "Unknown previous token"); @@ -468,24 +468,6 @@ class AnnotatingParser { OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for); FormatToken *PossibleObjCForInToken = nullptr; while (CurrentToken) { - // LookForDecls is set when "if (" has been seen. Check for - // 'identifier' '*' 'identifier' followed by not '=' -- this - // '*' has to be a binary operator but determineStarAmpUsage() will - // categorize it as an unary operator, so set the right type here. - if (LookForDecls && CurrentToken->Next) { -FormatToken *Prev = CurrentToken->getPreviousNonComment(); -if (Prev) { - FormatToken *PrevPrev = Prev->getPreviousNonComment(); - FormatToken *Next = CurrentToken->Next; - if (PrevPrev && PrevPrev->is(tok::identifier) && - PrevPrev->isNot(TT_TypeName) && Prev->isPointerOrReference() && - CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) { -Prev->setType(TT_BinaryOperator); -LookForDecls = false; - } -} - } - if (CurrentToken->Previous->is(TT_PointerOrReference) && CurrentToken->Previous->Previous->isOneOf(tok::l_paren, tok::coloncolon)) { @@ -581,6 +563,15 @@ class AnnotatingParser { PossibleObjCForInToken = nullptr; } } + if (IsIf && CurrentToken->is(tok::semi)) { +for (auto *Tok = OpeningParen.Next; + Tok != CurrentToken && + !Tok->isOneOf(tok::equal, tok::l_paren, tok::l_brace); + Tok = Tok->Next) { + if (Tok->isPointerOrReference()) +Tok->setFinalizedType(TT_PointerOrReference); +} + } if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) { PossibleObjCForInToken = CurrentToken; PossibleObjCForInToken->setType(TT_ObjCForIn); @@ -1307,7 +1298,7 @@ class AnnotatingParser { // Multi-line string itself is a single annotated token. if (Tok->is(TT_TableGenMultiLineString)) return true; -switch (Tok->Tok.getKind()) { +switch (bool IsIf = false; Tok->Tok.getKind()) { case tok::plus: case tok::minus: if (!Tok->Previous && Line.MustBeDeclaration) @@ -1461,11 +1452,12 @@ class AnnotatingParser { CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) { next(); } + IsIf = true; [[fallthrough]]; case tok::kw_while: if (CurrentToken && CurrentToken->is(tok::l_paren)) { next(); -if (!parseParens(/*LookForDecls=*/true)) +if (!parseParens(IsIf)) return false; } break; diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 34c03d668a9a0a..32df6dd5fa5bf5 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -294,6 +294,20 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) { EXPECT_TOKEN(Tokens[7], tok::ampamp, TT_BinaryOperator); EXPECT_TOKEN(Tokens[10], tok::greater, TT_BinaryOperator); + Tokens = annotate("if (Foo *foo; bar)"); + ASSERT_EQ(Tokens.size(), 9u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference); + + Tokens = annotate("if (Foo **foo(); bar)"); + ASSERT_EQ(Tokens.size(), 12u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference); + EXPECT_TOKEN(Tokens[4], tok::star, TT_PointerOrReference); + + Tokens = annotate("if (Foo *&foo{a}; bar)"); + ASSERT_EQ(Tokens.size(), 13u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference); + EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference); + FormatStyle Style = getLLVMStyle(); Style.TypeNames.push_back("MYI"); Tokens = annotate("if (MYI *p{nullptr})", Style); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://l
[clang] [clang-format] Correctly annotate pointer/reference in range-for loop (PR #109361)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/109361 Fixes #109358. >From d954c56f41a96ace6b507f6e6f4531c885db9f38 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Thu, 19 Sep 2024 18:56:38 -0700 Subject: [PATCH] [clang-format] Correctly annotate pointer/reference in range-for loop Fixes #109358. --- clang/lib/Format/TokenAnnotator.cpp | 6 ++ clang/unittests/Format/TokenAnnotatorTest.cpp | 6 ++ 2 files changed, 12 insertions(+) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 6f09835bad3a83..3f7ac6c7776f40 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1407,6 +1407,12 @@ class AnnotatingParser { } } else if (Contexts.back().ColonIsForRangeExpr) { Tok->setType(TT_RangeBasedForLoopColon); +for (auto *Prev = Tok->Previous; + Prev && !Prev->isOneOf(tok::semi, tok::l_paren); + Prev = Prev->Previous) { + if (Prev->isPointerOrReference()) +Prev->setFinalizedType(TT_PointerOrReference); +} } else if (Contexts.back().ContextType == Context::C11GenericSelection) { Tok->setType(TT_GenericSelectionColon); } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) { diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 34c03d668a9a0a..dfb6c060d32094 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -333,6 +333,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) { ASSERT_EQ(Tokens.size(), 17u) << Tokens; EXPECT_TOKEN(Tokens[11], tok::star, TT_BinaryOperator); + Tokens = annotate("for (int i; Foo *&foo : foos)"); + ASSERT_EQ(Tokens.size(), 13u) << Tokens; + EXPECT_TOKEN(Tokens[6], tok::star, TT_PointerOrReference); + EXPECT_TOKEN(Tokens[7], tok::amp, TT_PointerOrReference); + EXPECT_TOKEN(Tokens[9], tok::colon, TT_RangeBasedForLoopColon); + Tokens = annotate("#define FOO auto Foo = [] { f(a * b); };"); ASSERT_EQ(Tokens.size(), 19u) << Tokens; EXPECT_TOKEN(Tokens[12], tok::star, TT_BinaryOperator); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in annotating StartOfName (PR #99791)
owenca wrote: Wouldn't you have the same problem if we were to backport the fix, say, to 18.1.9? Anyway, LLVM 19.1.0 has just been released. https://github.com/llvm/llvm-project/pull/99791 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Annotate the l_paren of function pointer types (PR #109229)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/109229 Fixes #109146. >From 84c166dfabc3f314cd922baa3933b3d0ea11e08e Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Wed, 18 Sep 2024 21:03:24 -0700 Subject: [PATCH] [clang-format] Annotate the l_paren of function pointer types Fixes #109146. --- clang/lib/Format/TokenAnnotator.cpp | 14 -- clang/unittests/Format/TokenAnnotatorTest.cpp | 6 ++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 6f09835bad3a83..9e5f5588592199 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -486,16 +486,18 @@ class AnnotatingParser { } } - if (CurrentToken->Previous->is(TT_PointerOrReference) && - CurrentToken->Previous->Previous->isOneOf(tok::l_paren, -tok::coloncolon)) { + const auto &Prev = *CurrentToken->Previous; + if (Prev.is(TT_PointerOrReference) && + Prev.Previous->isOneOf(tok::l_paren, tok::coloncolon)) { ProbablyFunctionType = true; } if (CurrentToken->is(tok::comma)) MightBeFunctionType = false; - if (CurrentToken->Previous->is(TT_BinaryOperator)) + if (Prev.is(TT_BinaryOperator)) Contexts.back().IsExpression = true; if (CurrentToken->is(tok::r_paren)) { +if (Prev.is(TT_PointerOrReference) && Prev.Previous == &OpeningParen) + MightBeFunctionType = true; if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType && ProbablyFunctionType && CurrentToken->Next && (CurrentToken->Next->is(tok::l_paren) || @@ -568,8 +570,8 @@ class AnnotatingParser { bool ProbablyFunctionTypeLParen = (CurrentToken->is(tok::l_paren) && CurrentToken->Next && CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret)); - if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) || - CurrentToken->Previous->isTypeName(LangOpts)) && + if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) || + Prev.isTypeName(LangOpts)) && !(CurrentToken->is(tok::l_brace) || (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) { Contexts.back().IsExpression = false; diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 34c03d668a9a0a..b5f03d4f851e6e 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -789,6 +789,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) { ASSERT_EQ(Tokens.size(), 13u) << Tokens; EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_CastRParen); + Tokens = annotate("return (Foo (*)(void *, Bar, ...))&foo;"); + ASSERT_EQ(Tokens.size(), 19u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionTypeLParen); + EXPECT_TOKEN(Tokens[14], tok::r_paren, TT_CastRParen); + EXPECT_TOKEN(Tokens[15], tok::amp, TT_UnaryOperator); + auto Style = getLLVMStyle(); Style.TypeNames.push_back("Foo"); Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix regression in BAS_AlwaysBreak for-await (PR #108634)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/108634 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Handle C-style cast of qualified type (PR #108929)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/108929 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in SpacesInParens InConditionalStatements (PR #108797)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/108797 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Reimplement InsertNewlineAtEOF (PR #108513)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/108513 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format[NFC] Clean up FormatTestBase and Proto/TextProto tests (PR #108334)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/108334 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in annotating angles containing FatArrow (PR #108671)
@@ -243,14 +244,16 @@ class AnnotatingParser { // operator that was misinterpreted because we are parsing template // parameters. // FIXME: This is getting out of hand, write a decent parser. - if (InExpr && !Line.startsWith(tok::kw_template) && + if (InExpr && !SeenFatArrow && !Line.startsWith(tok::kw_template) && Prev.is(TT_BinaryOperator)) { const auto Precedence = Prev.getPrecedence(); if (Precedence > prec::Conditional && Precedence < prec::Relational) return false; } if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto()) SeenTernaryOperator = true; + else if (Prev.is(TT_FatArrow)) owenca wrote: How realistic is your C++ example? In general, I don't think we can really tell if the angle brackets are template opener/closer or comparison operators without checking more (and very specific) contexts, e.g. the `new` keyword before `Bar` and the empty parentheses after `>`. In absence of that, it seems more likely that the angles are comparison operators. See e.g. this [test case](https://github.com/llvm/llvm-project/blob/95a0b4f729310d95d89f01f4d92ab7d2bf09941c/clang/unittests/Format/TokenAnnotatorTest.cpp#L645). https://github.com/llvm/llvm-project/pull/108671 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Handle C-style cast of qualified type (PR #108929)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/108929 Fixes #102874. >From db3da97b684403fdbf38b0f029438c00cfa16a5f Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Mon, 16 Sep 2024 21:31:29 -0700 Subject: [PATCH] [clang-format] Handle C-style cast of qualified type Fixes #102874. --- clang/lib/Format/TokenAnnotator.cpp | 13 - clang/unittests/Format/TokenAnnotatorTest.cpp | 8 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index dfa703aed0d34d..eabb6e68d75b09 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2840,11 +2840,14 @@ class AnnotatingParser { if (AfterRParen->isOneOf(tok::identifier, tok::kw_this)) return true; -// Look for a cast `( x ) (`. -if (AfterRParen->is(tok::l_paren) && BeforeRParen->Previous) { - if (BeforeRParen->is(tok::identifier) && - BeforeRParen->Previous->is(tok::l_paren)) { -return true; +// Look for a cast `( x ) (`, where x may be a qualified identifier. +if (AfterRParen->is(tok::l_paren)) { + for (const auto *Prev = BeforeRParen; Prev->is(tok::identifier);) { +Prev = Prev->Previous; +if (Prev->is(tok::coloncolon)) + Prev = Prev->Previous; +if (Prev == LParen) + return true; } } diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 5c28e3a4ea5a1f..f97b47be772e62 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -781,6 +781,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) { EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_Unknown); EXPECT_TOKEN(Tokens[10], tok::minus, TT_BinaryOperator); + Tokens = annotate("return (::Type)(1 + 2);"); + ASSERT_EQ(Tokens.size(), 12u) << Tokens; + EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_CastRParen); + + Tokens = annotate("return (Namespace::Class)(1 + 2);"); + ASSERT_EQ(Tokens.size(), 13u) << Tokens; + EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_CastRParen); + auto Style = getLLVMStyle(); Style.TypeNames.push_back("Foo"); Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Format] Dont treat LBrace after extends/implements as initializer list (PR #108524)
https://github.com/owenca approved this pull request. https://github.com/llvm/llvm-project/pull/108524 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in SpacesInParens InConditionalStatements (PR #108797)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/108797 Fixes #64416. >From 85e130d6e1fc1213f7daccf492aecc422c026c73 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Mon, 16 Sep 2024 00:15:28 -0700 Subject: [PATCH] [clang-format] Fix a bug in SpacesInParens InConditionalStatements Fixes #64416. --- clang/lib/Format/TokenAnnotator.cpp | 36 +-- clang/unittests/Format/FormatTest.cpp | 6 + 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index dfa703aed0d34d..9e36570bc6ff41 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -4418,31 +4418,29 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, Right.MatchingParen == &Left && Line.Children.empty()) { return Style.SpaceInEmptyBlock; } - if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) || - (Left.is(tok::l_brace) && Left.isNot(BK_Block) && - Right.is(tok::r_brace) && Right.isNot(BK_Block))) { -return Style.SpacesInParensOptions.InEmptyParentheses; - } - if (Style.SpacesInParens == FormatStyle::SIPO_Custom && - Style.SpacesInParensOptions.ExceptDoubleParentheses && - Left.is(tok::r_paren) && Right.is(tok::r_paren)) { -auto *InnerLParen = Left.MatchingParen; -if (InnerLParen && InnerLParen->Previous == Right.MatchingParen) { - InnerLParen->SpacesRequiredBefore = 0; - return false; + if (Style.SpacesInParens == FormatStyle::SIPO_Custom) { +if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) || +(Left.is(tok::l_brace) && Left.isNot(BK_Block) && + Right.is(tok::r_brace) && Right.isNot(BK_Block))) { + return Style.SpacesInParensOptions.InEmptyParentheses; +} +if (Style.SpacesInParensOptions.ExceptDoubleParentheses && +Left.is(tok::r_paren) && Right.is(tok::r_paren)) { + auto *InnerLParen = Left.MatchingParen; + if (InnerLParen && InnerLParen->Previous == Right.MatchingParen) { +InnerLParen->SpacesRequiredBefore = 0; +return false; + } } - } - if (Style.SpacesInParensOptions.InConditionalStatements) { const FormatToken *LeftParen = nullptr; if (Left.is(tok::l_paren)) LeftParen = &Left; else if (Right.is(tok::r_paren) && Right.MatchingParen) LeftParen = Right.MatchingParen; -if (LeftParen) { - if (LeftParen->is(TT_ConditionLParen)) -return true; - if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous)) -return true; +if (LeftParen && (LeftParen->is(TT_ConditionLParen) || + (LeftParen->Previous && + isKeywordWithCondition(*LeftParen->Previous { + return Style.SpacesInParensOptions.InConditionalStatements; } } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 5ebf0d7068dd6c..f9c6a6eca1d724 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -17282,6 +17282,12 @@ TEST_F(FormatTest, ConfigurableSpacesInParens) { Spaces.SpacesInParens = FormatStyle::SIPO_Custom; Spaces.SpacesInParensOptions = {}; Spaces.SpacesInParensOptions.Other = true; + + EXPECT_FALSE(Spaces.SpacesInParensOptions.InConditionalStatements); + verifyFormat("if (a)\n" + " return;", + Spaces); + Spaces.SpacesInParensOptions.InConditionalStatements = true; verifyFormat("do_something( ::globalVar );", Spaces); verifyFormat("call( x, y, z );", Spaces); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in annotating StartOfName (PR #99791)
owenca wrote: > That is very unfortunate. It means one will either get different results for > the clang-format versions, one disables the formatting, or works around it by > sth. Like Q_EMIT(something()->mySignal()). Or wait for the next release (in this case 19.1.0, which is just around the corner). https://github.com/llvm/llvm-project/pull/99791 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [git-clang-format] Fix: make the tool backward compatible (PR #108721)
owenca wrote: Why do we need to make newer versions of git-clang-format "compatible" with older versions of clang-format? Shouldn't both be from the same LLVM release? https://github.com/llvm/llvm-project/pull/108721 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix regression in BAS_AlwaysBreak for-await (PR #108634)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/108634 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix regression in AlwaysBreak for-await (PR #108634)
https://github.com/owenca approved this pull request. https://github.com/llvm/llvm-project/pull/108634 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix regression in AlwaysBreak for-await (PR #108634)
@@ -809,7 +809,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, if (Tok.Previous->isIf()) return Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak; return !Tok.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while, - tok::kw_switch); + tok::kw_switch, Keywords.kw_await); owenca wrote: ```suggestion tok::kw_switch) && !(Style.isJavaScript() && Tok.Previous->is(Keywords.kw_await)); ``` as `await` is not a keyword in C++, Java, etc. https://github.com/llvm/llvm-project/pull/108634 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Format] Dont treat LBrace after extends/implements as initializer list (PR #108524)
https://github.com/owenca commented: Can you add a `TokenAnnotatorTest`? https://github.com/llvm/llvm-project/pull/108524 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in annotating StartOfName (PR #99791)
owenca wrote: Unfortunately no because clang-format is part of llvm, and as far as I know there will be no more llvm 18 point releases. https://github.com/llvm/llvm-project/pull/99791 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in annotating angles containing FatArrow (PR #108671)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/108671 Fixes #108536. >From 90a2015196595bb72fb64490d1579f01b7c18a2a Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Fri, 13 Sep 2024 19:36:48 -0700 Subject: [PATCH] [clang-format] Fix a bug in annotating angles containing FatArrow Fixes #108536. --- clang/lib/Format/TokenAnnotator.cpp | 10 ++ clang/unittests/Format/TokenAnnotatorTest.cpp | 9 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index dfa703aed0d34d..4313cc20dd61a3 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -189,7 +189,8 @@ class AnnotatingParser { next(); } -for (bool SeenTernaryOperator = false; CurrentToken;) { +for (bool SeenTernaryOperator = false, SeenFatArrow = false; + CurrentToken;) { const bool InExpr = Contexts[Contexts.size() - 2].IsExpression; if (CurrentToken->is(tok::greater)) { const auto *Next = CurrentToken->Next; @@ -243,7 +244,7 @@ class AnnotatingParser { // operator that was misinterpreted because we are parsing template // parameters. // FIXME: This is getting out of hand, write a decent parser. - if (InExpr && !Line.startsWith(tok::kw_template) && + if (InExpr && !SeenFatArrow && !Line.startsWith(tok::kw_template) && Prev.is(TT_BinaryOperator)) { const auto Precedence = Prev.getPrecedence(); if (Precedence > prec::Conditional && Precedence < prec::Relational) @@ -251,6 +252,8 @@ class AnnotatingParser { } if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto()) SeenTernaryOperator = true; + else if (Prev.is(TT_FatArrow)) +SeenFatArrow = true; updateParameterCount(Left, CurrentToken); if (Style.Language == FormatStyle::LK_Proto) { if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) { @@ -260,8 +263,7 @@ class AnnotatingParser { Previous->setType(TT_SelectorName); } } - } - if (Style.isTableGen()) { + } else if (Style.isTableGen()) { if (CurrentToken->isOneOf(tok::comma, tok::equal)) { // They appear as separators. Unless they are not in class definition. next(); diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 5c28e3a4ea5a1f..c3ac5df12d085c 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -616,6 +616,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsTernaryInTemplate) { EXPECT_TOKEN(Tokens[8], tok::greater, TT_TemplateCloser); } +TEST_F(TokenAnnotatorTest, FatArrowInAngleBrackets) { + auto Tokens = annotate("foo = new Bar<(id: int) => X | Y>();", + getLLVMStyle(FormatStyle::LK_JavaScript)); + ASSERT_EQ(Tokens.size(), 19u) << Tokens; + EXPECT_TOKEN(Tokens[4], tok::less, TT_TemplateOpener); + EXPECT_TOKEN(Tokens[10], tok::equal, TT_FatArrow); + EXPECT_TOKEN(Tokens[14], tok::greater, TT_TemplateCloser); +} + TEST_F(TokenAnnotatorTest, UnderstandsNonTemplateAngleBrackets) { auto Tokens = annotate("return a < b && c > d;"); ASSERT_EQ(Tokens.size(), 10u) << Tokens; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Reimplement InsertNewlineAtEOF (PR #108513)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/108513 Fixes #108333. >From c85d8cdd44ad577bbc2a29e45058b0c972c6fa9e Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Fri, 13 Sep 2024 01:21:29 -0700 Subject: [PATCH] [clang-format] Reimplement InsertNewlineAtEOF Fixes #108333. --- clang/lib/Format/FormatTokenLexer.cpp | 7 +++ clang/lib/Format/TokenAnnotator.cpp | 5 - clang/unittests/Format/FormatTest.cpp | 6 ++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index e21b5a882b7773..63949b2e26bdc1 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -100,6 +100,13 @@ ArrayRef FormatTokenLexer::lex() { if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline) FirstInLineIndex = Tokens.size() - 1; } while (Tokens.back()->isNot(tok::eof)); + if (Style.InsertNewlineAtEOF) { +auto &TokEOF = *Tokens.back(); +if (TokEOF.NewlinesBefore == 0) { + TokEOF.NewlinesBefore = 1; + TokEOF.OriginalColumn = 0; +} + } return Tokens; } diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index dfa703aed0d34d..aa0d310a355ff6 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3704,11 +3704,6 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) { auto *First = Line.First; First->SpacesRequiredBefore = 1; First->CanBreakBefore = First->MustBreakBefore; - - if (First->is(tok::eof) && First->NewlinesBefore == 0 && - Style.InsertNewlineAtEOF) { -First->NewlinesBefore = 1; - } } // This function heuristically determines whether 'Current' starts the name of a diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 5ebf0d7068dd6c..033daa3645db0d 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -27577,6 +27577,12 @@ TEST_F(FormatTest, InsertNewlineAtEOF) { verifyNoChange("int i;\n", Style); verifyFormat("int i;\n", "int i;", Style); + + constexpr StringRef Code{"namespace {\n" + "int i;\n" + "} // namespace"}; + verifyFormat(Code.str() + '\n', Code, Style, + {tooling::Range(19, 13)}); // line 3 } TEST_F(FormatTest, KeepEmptyLinesAtEOF) { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format[NFC] Clean up FormatTestBase and Proto/TextProto tests (PR #108334)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/108334 None >From 556db4b4c5b727cd44aec39d16178720edf77942 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Wed, 11 Sep 2024 23:07:37 -0700 Subject: [PATCH] [clang-format[NFC] Clean up FormatTestBase and Proto/TextProto tests --- clang/unittests/Format/FormatTestBase.h | 14 +-- clang/unittests/Format/FormatTestProto.cpp| 2 - .../unittests/Format/FormatTestTextProto.cpp | 90 --- 3 files changed, 45 insertions(+), 61 deletions(-) diff --git a/clang/unittests/Format/FormatTestBase.h b/clang/unittests/Format/FormatTestBase.h index 33110ca5d9edfd..9d9472964fd3b4 100644 --- a/clang/unittests/Format/FormatTestBase.h +++ b/clang/unittests/Format/FormatTestBase.h @@ -61,23 +61,23 @@ class FormatTestBase : public testing::Test { return *Result; } - FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { + FormatStyle getStyleWithColumns(FormatStyle Style, + unsigned ColumnLimit) const { Style.ColumnLimit = ColumnLimit; return Style; } - FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { + FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) const { return getStyleWithColumns(getLLVMStyle(), ColumnLimit); } - FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { + FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) const { return getStyleWithColumns(getGoogleStyle(), ColumnLimit); } - FormatStyle getTextProtoStyleWithColumns(unsigned ColumnLimit) { -FormatStyle Style = getGoogleStyle(FormatStyle::FormatStyle::LK_TextProto); -Style.ColumnLimit = ColumnLimit; -return Style; + FormatStyle getTextProtoStyleWithColumns(unsigned ColumnLimit) const { +return getStyleWithColumns(getGoogleStyle(FormatStyle::LK_TextProto), + ColumnLimit); } bool _verifyFormat(const char *File, int Line, StringRef Expected, diff --git a/clang/unittests/Format/FormatTestProto.cpp b/clang/unittests/Format/FormatTestProto.cpp index 5adb532ae4a412..30ce57c545ec76 100644 --- a/clang/unittests/Format/FormatTestProto.cpp +++ b/clang/unittests/Format/FormatTestProto.cpp @@ -516,8 +516,6 @@ TEST_F(FormatTestProto, AcceptsOperatorAsKeyInOptions) { } TEST_F(FormatTestProto, BreaksEntriesOfSubmessagesContainingSubmessages) { - FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto); - Style.ColumnLimit = 60; // The column limit allows for the keys submessage to be put on 1 line, but we // break it since it contains a submessage an another entry. verifyFormat("option (MyProto.options) = {\n" diff --git a/clang/unittests/Format/FormatTestTextProto.cpp b/clang/unittests/Format/FormatTestTextProto.cpp index 23f46202a34637..fd65c9a58db5d8 100644 --- a/clang/unittests/Format/FormatTestTextProto.cpp +++ b/clang/unittests/Format/FormatTestTextProto.cpp @@ -18,9 +18,7 @@ namespace { class FormatTestTextProto : public FormatTestBase { protected: virtual FormatStyle getDefaultStyle() const override { -FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto); -Style.ColumnLimit = 60; // To make writing tests easier. -return Style; +return getTextProtoStyleWithColumns(60); } }; @@ -126,7 +124,8 @@ TEST_F(FormatTestTextProto, ImplicitStringLiteralConcatenation) { " 'b'"); verifyFormat("field_a: \"a\"\n" " \"b\""); - FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto); + + auto Style = getDefaultStyle(); Style.AlwaysBreakBeforeMultilineStrings = true; verifyFormat("field_a:\n" "'a'\n" @@ -359,46 +358,40 @@ TEST_F(FormatTestTextProto, KeepsCommentsIndentedInList) { } TEST_F(FormatTestTextProto, UnderstandsHashComments) { - FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto); - Style.ColumnLimit = 60; // To make writing tests easier. - EXPECT_EQ("aaa: 100\n" -"## this is a double-hash comment.\n" -"bb: 100\n" -"## another double-hash comment.\n" -"### a triple-hash comment\n" -"cc: 200\n" -"### another triple-hash comment\n" -" a quadriple-hash comment\n" -"dd: 100\n" -" another quadriple-hash comment", -format("aaa: 100\n" - "##this is a double-hash comment.\n" - "bb: 100\n" - "## another double-hash comment.\n" - "###a triple-hash comment\n" - "cc: 200\n" - "### another triple-hash comment\n" - "a quadriple-hash comment\n" - "dd: 100\n" - " another quadriple-hash comment", - Style)); + auto Style = getDefaultStyle(); + + verifyFormat("aaa: 100\n" + "## this is a double-has
[clang] 9469836 - [clang-format][NFC] Minor clean of TokenAnnotatorTest
Author: Owen Pan Date: 2024-09-11T20:35:13-07:00 New Revision: 94698369e9cc211b4d1e666b82dc5848c40ab5ce URL: https://github.com/llvm/llvm-project/commit/94698369e9cc211b4d1e666b82dc5848c40ab5ce DIFF: https://github.com/llvm/llvm-project/commit/94698369e9cc211b4d1e666b82dc5848c40ab5ce.diff LOG: [clang-format][NFC] Minor clean of TokenAnnotatorTest Added: Modified: clang/unittests/Format/TokenAnnotatorTest.cpp Removed: diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 36a6db9283893e..5c28e3a4ea5a1f 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -2050,7 +2050,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) { EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionTypeLParen); Tokens = annotate("void instanceof();"); - ASSERT_EQ(Tokens.size(), 6u); + ASSERT_EQ(Tokens.size(), 6u) << Tokens; EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName); EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen); @@ -3365,55 +3365,55 @@ TEST_F(TokenAnnotatorTest, SwitchExpression) { TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) { auto Tokens = annotate("a = b and c;"); - ASSERT_EQ(Tokens.size(), 7u); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_BinaryOperator); Tokens = annotate("a = b and_eq c;"); - ASSERT_EQ(Tokens.size(), 7u); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::ampequal, TT_BinaryOperator); Tokens = annotate("a = b bitand c;"); - ASSERT_EQ(Tokens.size(), 7u); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::amp, TT_BinaryOperator); Tokens = annotate("a = b bitor c;"); - ASSERT_EQ(Tokens.size(), 7u); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::pipe, TT_BinaryOperator); Tokens = annotate("a = b compl c;"); - ASSERT_EQ(Tokens.size(), 7u); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::tilde, TT_UnaryOperator); Tokens = annotate("a = b not c;"); - ASSERT_EQ(Tokens.size(), 7u); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::exclaim, TT_UnaryOperator); Tokens = annotate("a = b not_eq c;"); - ASSERT_EQ(Tokens.size(), 7u); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::exclaimequal, TT_BinaryOperator); Tokens = annotate("a = b or c;"); - ASSERT_EQ(Tokens.size(), 7u); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::pipepipe, TT_BinaryOperator); Tokens = annotate("a = b or_eq c;"); - ASSERT_EQ(Tokens.size(), 7u); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::pipeequal, TT_BinaryOperator); Tokens = annotate("a = b xor c;"); - ASSERT_EQ(Tokens.size(), 7u); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::caret, TT_BinaryOperator); Tokens = annotate("a = b xor_eq c;"); - ASSERT_EQ(Tokens.size(), 7u); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::caretequal, TT_BinaryOperator); Tokens = annotate("xor = foo;"); - ASSERT_EQ(Tokens.size(), 5u); + ASSERT_EQ(Tokens.size(), 5u) << Tokens; EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown); Tokens = annotate("int xor = foo;"); - ASSERT_EQ(Tokens.size(), 6u); + ASSERT_EQ(Tokens.size(), 6u) << Tokens; EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName); } @@ -3423,7 +3423,7 @@ TEST_F(TokenAnnotatorTest, FunctionTryBlock) { ": foo{[] -> std::string { return {}; }(), x}, bar{y} {\n" "} catch (...) {\n" "}"); - ASSERT_EQ(Tokens.size(), 45u); + ASSERT_EQ(Tokens.size(), 45u) << Tokens; EXPECT_TOKEN(Tokens[2], tok::identifier, TT_CtorDtorDeclName); EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionDeclarationLParen); EXPECT_TOKEN(Tokens[11], tok::colon, TT_CtorInitializerColon); @@ -3439,7 +3439,7 @@ TEST_F(TokenAnnotatorTest, TypenameMacro) { Style.TypenameMacros.push_back("STRUCT"); auto Tokens = annotate("STRUCT(T, B) { int i; };", Style); - ASSERT_EQ(Tokens.size(), 13u); + ASSERT_EQ(Tokens.size(), 13u) << Tokens; EXPECT_TOKEN(Tokens[0], tok::identifier, TT_TypenameMacro); EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_TypeDeclarationParen); EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_TypeDeclarationParen); @@ -3451,7 +3451,7 @@ TEST_F(TokenAnnotatorTest, GNULanguageStandard) { EXPECT_EQ(Style.Standard, FormatStyle::LS_Latest); auto Tokens = annotate("return 1 <=> 2;", Style); - ASSERT_EQ(Tokens.size(), 6u); + ASSERT_EQ(Tokens.size(), 6u) << Tokens; EXPECT_TOKEN(Tokens[2], tok::spaceship, TT_BinaryOperator); } ___ cfe-commits mailing l
[clang] [clang-format] Fix regressions in BAS_AlwaysBreak (PR #107506)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/107506 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Format] Avoid repeated hash lookups (NFC) (PR #107962)
@@ -86,9 +86,7 @@ void ObjCPropertyAttributeOrderFixer::sortPropertyAttributes( Value = Tok->TokenText; } -auto It = SortOrderMap.find(Attribute); -if (It == SortOrderMap.end()) - It = SortOrderMap.insert({Attribute, SortOrderMap.size()}).first; +auto It = SortOrderMap.try_emplace(Attribute, SortOrderMap.size()).first; // Sort the indices based on the priority stored in `SortOrderMap`. const auto Ordinal = It->second; owenca wrote: My preference: ```suggestion // Sort the indices based on the priority stored in `SortOrderMap`. const auto Ordinal = SortOrderMap.try_emplace(Attribute, SortOrderMap.size()).first->second; ``` And delete lines 89-91. https://github.com/llvm/llvm-project/pull/107962 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Format] Avoid repeated hash lookups (NFC) (PR #107962)
https://github.com/owenca approved this pull request. https://github.com/llvm/llvm-project/pull/107962 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
https://github.com/owenca requested changes to this pull request. Running `FormatTests` failed: ``` [ RUN ] FormatTest.WrapNamespaceBodyWithEmptyLinesNever Assertion failed: (TheLine->MatchingClosingBlockLineIndex > 0), function tryFitMultipleLinesInOne, file UnwrappedLineFormatter.cpp, line 390. ``` https://github.com/llvm/llvm-project/pull/106145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix regressions in BAS_AlwaysBreak (PR #107506)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/107506 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix regressions in BAS_AlwaysBreak (PR #107506)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/107506 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a regression on BAS_AlwaysBreak (PR #107506)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/107506 >From bd7da6ec9afabd829010db4c33d088590ab68b5a Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Thu, 5 Sep 2024 19:44:46 -0700 Subject: [PATCH 1/3] [clang-format] Fix a regression on BAS_AlwaysBreak Fixes #107401. --- clang/lib/Format/ContinuationIndenter.cpp | 2 +- clang/unittests/Format/FormatTestJS.cpp | 8 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 5843571718b3a2..f65c1640a8765a 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -861,7 +861,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, // or // cal( // new SomethingElseee()); - !IsSimpleFunction(Current)) { + Current.isNot(tok::comment) && !IsSimpleFunction(Current)) { CurrentState.NoLineBreak = true; } diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 4b29ba720f6823..a0b663170c7b33 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -2850,5 +2850,13 @@ TEST_F(FormatTestJS, DontBreakFieldsAsGoToLabels) { "};"); } +TEST_F(FormatTestJS, BreakAfterOpenBracket) { + auto Style = getGoogleStyle(FormatStyle::LK_JavaScript); + EXPECT_EQ(Style.AlignAfterOpenBracket, FormatStyle::BAS_AlwaysBreak); + verifyFormat("ctrl.onCopy(/** @type {!WizEvent}*/ (\n" + "{event, targetElement: {el: () => selectedElement}}));", + Style); +} + } // namespace format } // end namespace clang >From 920e26dcd9c5d7f78a4f8b572451ecae73dd9201 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sat, 7 Sep 2024 16:40:04 -0700 Subject: [PATCH 2/3] Rename `InSimpleFunction` to `StartsSimpleOneArgList` and skip to the next token if the argument is a comment. --- clang/lib/Format/ContinuationIndenter.cpp | 9 ++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index f65c1640a8765a..58e6b78ce578e4 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -815,7 +815,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, return Tok.is(tok::l_paren) && Tok.ParameterCount > 0 && Tok.Previous && Tok.Previous->is(tok::identifier); }; - const auto IsInTemplateString = [this](const FormatToken &Tok) { + auto IsInTemplateString = [this](const FormatToken &Tok) { if (!Style.isJavaScript()) return false; for (const auto *Prev = &Tok; Prev; Prev = Prev->Previous) { @@ -827,7 +827,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, return false; }; // Identifies simple (no expression) one-argument function calls. - const auto IsSimpleFunction = [&](const FormatToken &Tok) { + auto StartsSimpleOneArgList = [&](const FormatToken &TokAfterLParen) { +assert(TokAfterLParen.isNot(tok::comment) || TokAfterLParen.Next); +const auto &Tok = +TokAfterLParen.is(tok::comment) ? *TokAfterLParen.Next : TokAfterLParen; if (!Tok.FakeLParens.empty() && Tok.FakeLParens.back() > prec::Unknown) return false; // Nested calls that involve `new` expressions also look like simple @@ -861,7 +864,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, // or // cal( // new SomethingElseee()); - Current.isNot(tok::comment) && !IsSimpleFunction(Current)) { + !StartsSimpleOneArgList(Current)) { CurrentState.NoLineBreak = true; } >From 4ff558c57fbd931cc65053c81def60501d3fe4af Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sun, 8 Sep 2024 00:54:41 -0700 Subject: [PATCH 3/3] Also fix #107574. --- clang/lib/Format/ContinuationIndenter.cpp | 5 + clang/unittests/Format/FormatTestJS.cpp | 9 + 2 files changed, 14 insertions(+) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 58e6b78ce578e4..f29f8796ea9290 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -839,6 +839,11 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, // - foo(::new Bar()) if (Tok.is(tok::kw_new) || Tok.startsSequence(tok::coloncolon, tok::kw_new)) return true; +if (Tok.is(TT_UnaryOperator) || +(Style.isJavaScript() && + Tok.isOneOf(tok::ellipsis, Keywords.kw_await))) { + return true; +} const auto *Previous = Tok.Previous; if (!Previous || (!Previous->isOneOf(TT_FunctionDec
[clang] [clang-format] Fix a bug in annotating CastRParen (PR #107675)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/107675 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a regression on BAS_AlwaysBreak (PR #107506)
owenca wrote: > i've also found 3 more cases that point back to same patch in #107574, in > case you want to address all of them with this change I'd rather make incremental changes. Maybe you or @gedare can give them a try. https://github.com/llvm/llvm-project/pull/107506 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a regression on BAS_AlwaysBreak (PR #107506)
@@ -861,7 +861,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, // or // cal( // new SomethingElseee()); - !IsSimpleFunction(Current)) { + Current.isNot(tok::comment) && !IsSimpleFunction(Current)) { owenca wrote: > would it make sense to introduce this into `IsSimpleFunction` ? similar to > handling of `new` keyword in there? I did that at first but thought it would be confusing to say that a comment is a simple function (or function call). https://github.com/llvm/llvm-project/pull/107506 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a regression on BAS_AlwaysBreak (PR #107506)
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/107506 >From bd7da6ec9afabd829010db4c33d088590ab68b5a Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Thu, 5 Sep 2024 19:44:46 -0700 Subject: [PATCH 1/2] [clang-format] Fix a regression on BAS_AlwaysBreak Fixes #107401. --- clang/lib/Format/ContinuationIndenter.cpp | 2 +- clang/unittests/Format/FormatTestJS.cpp | 8 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 5843571718b3a2..f65c1640a8765a 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -861,7 +861,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, // or // cal( // new SomethingElseee()); - !IsSimpleFunction(Current)) { + Current.isNot(tok::comment) && !IsSimpleFunction(Current)) { CurrentState.NoLineBreak = true; } diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 4b29ba720f6823..a0b663170c7b33 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -2850,5 +2850,13 @@ TEST_F(FormatTestJS, DontBreakFieldsAsGoToLabels) { "};"); } +TEST_F(FormatTestJS, BreakAfterOpenBracket) { + auto Style = getGoogleStyle(FormatStyle::LK_JavaScript); + EXPECT_EQ(Style.AlignAfterOpenBracket, FormatStyle::BAS_AlwaysBreak); + verifyFormat("ctrl.onCopy(/** @type {!WizEvent}*/ (\n" + "{event, targetElement: {el: () => selectedElement}}));", + Style); +} + } // namespace format } // end namespace clang >From 920e26dcd9c5d7f78a4f8b572451ecae73dd9201 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sat, 7 Sep 2024 16:40:04 -0700 Subject: [PATCH 2/2] Rename `InSimpleFunction` to `StartsSimpleOneArgList` and skip to the next token if the argument is a comment. --- clang/lib/Format/ContinuationIndenter.cpp | 9 ++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index f65c1640a8765a..58e6b78ce578e4 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -815,7 +815,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, return Tok.is(tok::l_paren) && Tok.ParameterCount > 0 && Tok.Previous && Tok.Previous->is(tok::identifier); }; - const auto IsInTemplateString = [this](const FormatToken &Tok) { + auto IsInTemplateString = [this](const FormatToken &Tok) { if (!Style.isJavaScript()) return false; for (const auto *Prev = &Tok; Prev; Prev = Prev->Previous) { @@ -827,7 +827,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, return false; }; // Identifies simple (no expression) one-argument function calls. - const auto IsSimpleFunction = [&](const FormatToken &Tok) { + auto StartsSimpleOneArgList = [&](const FormatToken &TokAfterLParen) { +assert(TokAfterLParen.isNot(tok::comment) || TokAfterLParen.Next); +const auto &Tok = +TokAfterLParen.is(tok::comment) ? *TokAfterLParen.Next : TokAfterLParen; if (!Tok.FakeLParens.empty() && Tok.FakeLParens.back() > prec::Unknown) return false; // Nested calls that involve `new` expressions also look like simple @@ -861,7 +864,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, // or // cal( // new SomethingElseee()); - Current.isNot(tok::comment) && !IsSimpleFunction(Current)) { + !StartsSimpleOneArgList(Current)) { CurrentState.NoLineBreak = true; } ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in annotating CastRParen (PR #107675)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/107675 Fixes #107568. >From 6deac871ceae8ba3918570a4bdbd8476e014a3a1 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sat, 7 Sep 2024 01:39:34 -0700 Subject: [PATCH] [clang-format] Fix a bug in annotating CastRParen Fixes #107568. --- clang/lib/Format/TokenAnnotator.cpp | 29 +++ clang/unittests/Format/TokenAnnotatorTest.cpp | 6 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 6a1cf61659fd97..dfa703aed0d34d 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2874,21 +2874,26 @@ class AnnotatingParser { if (Line.InPPDirective && AfterRParen->is(tok::minus)) return false; +const auto *Prev = BeforeRParen; + +// Look for a function pointer type, e.g. `(*)()`. +if (Prev->is(tok::r_paren)) { + if (Prev->is(TT_CastRParen)) +return false; + Prev = Prev->MatchingParen; + if (!Prev) +return false; + Prev = Prev->Previous; + if (!Prev || Prev->isNot(tok::r_paren)) +return false; + Prev = Prev->MatchingParen; + return Prev && Prev->is(TT_FunctionTypeLParen); +} + // Search for unexpected tokens. -for (auto *Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous) { - if (Prev->is(tok::r_paren)) { -if (Prev->is(TT_CastRParen)) - return false; -Prev = Prev->MatchingParen; -if (!Prev) - return false; -if (Prev->is(TT_FunctionTypeLParen)) - break; -continue; - } +for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous) if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon)) return false; -} return true; } diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 1bb796fd6f5ee9..36a6db9283893e 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -775,6 +775,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) { EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown); EXPECT_TOKEN(Tokens[9], tok::minus, TT_BinaryOperator); + Tokens = annotate("return (double)(foo(30)) - 15;"); + ASSERT_EQ(Tokens.size(), 14u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_CastRParen); + EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_Unknown); + EXPECT_TOKEN(Tokens[10], tok::minus, TT_BinaryOperator); + auto Style = getLLVMStyle(); Style.TypeNames.push_back("Foo"); Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -27981,6 +27981,118 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, ShortNamespacesOption) { + FormatStyle Style = getLLVMStyle(); + Style.AllowShortNamespacesOnASingleLine = true; + Style.FixNamespaceComments = false; + + // Basic functionality. + verifyFormat("namespace foo { class bar; }", Style); + verifyFormat("namespace foo::bar { class baz; }", Style); + verifyFormat("namespace { class bar; }", Style); + verifyFormat("namespace foo {\n" + "class bar;\n" + "class baz;\n" + "}", + Style); + + // Trailing comments prevent merging. + verifyFormat("namespace foo {\n" + "namespace baz { class qux; } // comment\n" + "}", + Style); + + // Make sure code doesn't walk to far on unbalanced code. + verifyFormat("namespace foo {", Style); + verifyFormat("namespace foo {\n" + "class baz;", + Style); + verifyFormat("namespace foo {\n" + "namespace bar { class baz; }", + Style); + + // Nested namespaces. + verifyFormat("namespace foo { namespace bar { class baz; } }", Style); + verifyFormat("namespace foo {\n" + "namespace bar { class baz; }\n" + "namespace quar { class quaz; }\n" + "}", + Style); + + // Varying inner content. + verifyFormat("namespace foo {\n" + "int f() { return 5; }\n" + "}", + Style); + verifyFormat("namespace foo { template struct bar; }", Style); + verifyFormat("namespace foo { constexpr int num = 42; }", Style); + + // Validate wrapping scenarios around the ColumnLimit. + Style.ColumnLimit = 64; + + // Validate just under the ColumnLimit. + verifyFormat( + "namespace foo { namespace bar { namespace baz { class qux; } } }", + Style); + + // Validate just over the ColumnLimit. + verifyFormat("namespace foo {\n" + "namespace bar { namespace baz { class quux; } }\n" + "}", + Style); + + verifyFormat("namespace foo {\n" + "namespace bar {\n" + "namespace baz { namespace qux { class quux; } }\n" + "}\n" + "}", + Style); + + // Validate that the ColumnLimit logic accounts for trailing content as well. + verifyFormat("namespace foo {\n" + "namespace bar { namespace baz { class qux; } }\n" + "} // extra", + Style); + + // No ColumnLimit, allows long nested one-liners, but also leaves multi-line + // instances alone. + Style.ColumnLimit = 0; + verifyFormat("namespace foo { namespace bar { namespace baz { namespace qux " + "{ class quux; } } } }", + Style); + + verifyNoChange("namespace foo {\n" + "namespace bar {\n" + "namespace baz { namespace qux { class quux; } }\n" owenca wrote: Drop `namespace qux` to match the above if desired. https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -27981,6 +27981,118 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, ShortNamespacesOption) { + FormatStyle Style = getLLVMStyle(); + Style.AllowShortNamespacesOnASingleLine = true; + Style.FixNamespaceComments = false; + + // Basic functionality. + verifyFormat("namespace foo { class bar; }", Style); + verifyFormat("namespace foo::bar { class baz; }", Style); + verifyFormat("namespace { class bar; }", Style); + verifyFormat("namespace foo {\n" + "class bar;\n" + "class baz;\n" + "}", + Style); + + // Trailing comments prevent merging. + verifyFormat("namespace foo {\n" + "namespace baz { class qux; } // comment\n" + "}", + Style); + + // Make sure code doesn't walk to far on unbalanced code. + verifyFormat("namespace foo {", Style); + verifyFormat("namespace foo {\n" + "class baz;", + Style); + verifyFormat("namespace foo {\n" + "namespace bar { class baz; }", + Style); + + // Nested namespaces. + verifyFormat("namespace foo { namespace bar { class baz; } }", Style); + verifyFormat("namespace foo {\n" + "namespace bar { class baz; }\n" + "namespace quar { class quaz; }\n" + "}", + Style); + + // Varying inner content. + verifyFormat("namespace foo {\n" + "int f() { return 5; }\n" + "}", + Style); + verifyFormat("namespace foo { template struct bar; }", Style); + verifyFormat("namespace foo { constexpr int num = 42; }", Style); + + // Validate wrapping scenarios around the ColumnLimit. + Style.ColumnLimit = 64; + + // Validate just under the ColumnLimit. + verifyFormat( + "namespace foo { namespace bar { namespace baz { class qux; } } }", + Style); + + // Validate just over the ColumnLimit. + verifyFormat("namespace foo {\n" + "namespace bar { namespace baz { class quux; } }\n" + "}", + Style); + + verifyFormat("namespace foo {\n" + "namespace bar {\n" + "namespace baz { namespace qux { class quux; } }\n" + "}\n" + "}", + Style); + + // Validate that the ColumnLimit logic accounts for trailing content as well. + verifyFormat("namespace foo {\n" + "namespace bar { namespace baz { class qux; } }\n" + "} // extra", + Style); + + // No ColumnLimit, allows long nested one-liners, but also leaves multi-line + // instances alone. + Style.ColumnLimit = 0; + verifyFormat("namespace foo { namespace bar { namespace baz { namespace qux " + "{ class quux; } } } }", owenca wrote: ```suggestion verifyFormat( "namespace foo { namespace bar { namespace baz { class quux; } } }", ``` https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -27981,6 +27981,118 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, ShortNamespacesOption) { + FormatStyle Style = getLLVMStyle(); + Style.AllowShortNamespacesOnASingleLine = true; + Style.FixNamespaceComments = false; + + // Basic functionality. + verifyFormat("namespace foo { class bar; }", Style); + verifyFormat("namespace foo::bar { class baz; }", Style); + verifyFormat("namespace { class bar; }", Style); + verifyFormat("namespace foo {\n" + "class bar;\n" + "class baz;\n" + "}", + Style); + + // Trailing comments prevent merging. + verifyFormat("namespace foo {\n" + "namespace baz { class qux; } // comment\n" + "}", + Style); + + // Make sure code doesn't walk to far on unbalanced code. owenca wrote: ```suggestion // Make sure code doesn't walk too far on unbalanced code. ``` https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
https://github.com/owenca commented: The following are missing: - Run `dump_format_style.py`. - Add a `ConfigParseTest` for the new option. How does the new option interact with `CompactNamespaces`? For example: - `AllowShortNamespacesOnASingleLine: true` and `CompactNamespaces: false` ``` namespace a { namespace b { class c; } } // namespace a ``` - Both `true` `namespace a { namespace b { class c; } }` https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] clang-format: Add "AllowShortNamespacesOnASingleLine" option (PR #105597)
@@ -27981,6 +27981,118 @@ TEST_F(FormatTest, BreakBinaryOperations) { Style); } +TEST_F(FormatTest, ShortNamespacesOption) { + FormatStyle Style = getLLVMStyle(); owenca wrote: ```suggestion auto Style = getLLVMStyle(); ``` https://github.com/llvm/llvm-project/pull/105597 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [llvm] [clang-format] Add support for BasedOnStyle referencing an arbitrary file (PR #107312)
owenca wrote: @jediry, can you open a github issue for this? We probably should continue the discussion on your [RFC](https://discourse.llvm.org/t/rfc-clang-format-option-to-include-another-configuration-file/74891) before getting into the details of the implementation. At the very least, I need to understand what exactly you are proposing to extend `BasedOnStyle` from the user's perspective. (See [here](https://clang.llvm.org/docs/ClangFormat.html#clang-format-ignore) for an example.) Some questions off the top of my head: - Should both absolute and relative file paths be supported? - How would the other configs in the .clang-format file work with the imported ones? - What happens if the imported config file also imports another config file, possibly the one that imports it? - Is the search path (or environment variable) mechanism necessary? https://github.com/llvm/llvm-project/pull/107312 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Correctly annotate braces in macro definition (PR #107352)
owenca wrote: Done in #107531. https://github.com/llvm/llvm-project/pull/107352 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Correctly annotate braces in macro definition (PR #107352)
owenca wrote: /cherry-pick 616a8ce6203d8c7569266bfaf163e74df1f440ad https://github.com/llvm/llvm-project/pull/107352 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Correctly annotate braces in macro definition (PR #107352)
https://github.com/owenca milestoned https://github.com/llvm/llvm-project/pull/107352 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a regression on BAS_AlwaysBreak (PR #107506)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/107506 Fixes #107401. >From bd7da6ec9afabd829010db4c33d088590ab68b5a Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Thu, 5 Sep 2024 19:44:46 -0700 Subject: [PATCH] [clang-format] Fix a regression on BAS_AlwaysBreak Fixes #107401. --- clang/lib/Format/ContinuationIndenter.cpp | 2 +- clang/unittests/Format/FormatTestJS.cpp | 8 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 5843571718b3a2..f65c1640a8765a 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -861,7 +861,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, // or // cal( // new SomethingElseee()); - !IsSimpleFunction(Current)) { + Current.isNot(tok::comment) && !IsSimpleFunction(Current)) { CurrentState.NoLineBreak = true; } diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 4b29ba720f6823..a0b663170c7b33 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -2850,5 +2850,13 @@ TEST_F(FormatTestJS, DontBreakFieldsAsGoToLabels) { "};"); } +TEST_F(FormatTestJS, BreakAfterOpenBracket) { + auto Style = getGoogleStyle(FormatStyle::LK_JavaScript); + EXPECT_EQ(Style.AlignAfterOpenBracket, FormatStyle::BAS_AlwaysBreak); + verifyFormat("ctrl.onCopy(/** @type {!WizEvent}*/ (\n" + "{event, targetElement: {el: () => selectedElement}}));", + Style); +} + } // namespace format } // end namespace clang ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Correctly annotate braces in macro definition (PR #107352)
https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/107352 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Correctly annotate braces in macro definition (PR #107352)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/107352 Also add a test case for #106418. Fixes #107096. >From 8b359c9ab7e2b119383235c042f9804136ee172e Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Wed, 4 Sep 2024 21:41:28 -0700 Subject: [PATCH] [clang-format] Correctly annotate braces in macro definition Also add a test case for #106418. Fixes #107096. --- clang/lib/Format/UnwrappedLineParser.cpp | 3 ++- clang/unittests/Format/TokenAnnotatorTest.cpp | 20 +++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 246b29d308bfaf..1727ed93822b1b 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -570,7 +570,8 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in, Keywords.kw_as)); ProbablyBracedList = - ProbablyBracedList || (IsCpp && NextTok->is(tok::l_paren)); + ProbablyBracedList || (IsCpp && (PrevTok->Tok.isLiteral() || + NextTok->is(tok::l_paren))); // If there is a comma, semicolon or right paren after the closing // brace, we assume this is a braced initializer list. diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index c0436d8a2e180a..1bb796fd6f5ee9 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -3278,6 +3278,26 @@ TEST_F(TokenAnnotatorTest, BraceKind) { EXPECT_BRACE_KIND(Tokens[10], BK_Block); EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_StructRBrace); EXPECT_BRACE_KIND(Tokens[11], BK_Block); + + Tokens = annotate("#define MACRO\\\n" +" struct hash {\\\n" +"void f() { return; } \\\n" +" };"); + ASSERT_EQ(Tokens.size(), 20u) << Tokens; + EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace); + EXPECT_BRACE_KIND(Tokens[8], BK_Block); + EXPECT_TOKEN(Tokens[10], tok::identifier, TT_FunctionDeclarationName); + EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen); + EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace); + EXPECT_BRACE_KIND(Tokens[13], BK_Block); + EXPECT_BRACE_KIND(Tokens[16], BK_Block); + EXPECT_TOKEN(Tokens[17], tok::r_brace, TT_StructRBrace); + EXPECT_BRACE_KIND(Tokens[17], BK_Block); + + Tokens = annotate("#define MEMBER(NAME) NAME{\"\"}"); + ASSERT_EQ(Tokens.size(), 11u) << Tokens; + EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit); + EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit); } TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] 3449ed8 - Revert "[clang-format] Correctly annotate braces in macro definition (#106662)"
Author: Owen Pan Date: 2024-09-03T22:54:50-07:00 New Revision: 3449ed8dece600f387357b71ff74ae4bc46828b6 URL: https://github.com/llvm/llvm-project/commit/3449ed8dece600f387357b71ff74ae4bc46828b6 DIFF: https://github.com/llvm/llvm-project/commit/3449ed8dece600f387357b71ff74ae4bc46828b6.diff LOG: Revert "[clang-format] Correctly annotate braces in macro definition (#106662)" This reverts commit 0fa78b6c7bd43c2498700a98c47a02cf4fd06388 due to regression. Fixes #107096. Added: Modified: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Removed: diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 0d42a6c2bfb5c6..246b29d308bfaf 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -609,8 +609,9 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { ProbablyBracedList = NextTok->isNot(tok::l_square); } - // Cpp macro definition body containing nonempty braced list or block: + // Cpp macro definition body that is a nonempty braced list or block: if (IsCpp && Line->InMacroBody && PrevTok != FormatTok && + !FormatTok->Previous && NextTok->is(tok::eof) && // A statement can end with only `;` (simple statement), a block // closing brace (compound statement), or `:` (label statement). // If PrevTok is a block opening brace, Tok ends an empty block. diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index a2986f589396b4..c0436d8a2e180a 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -3278,11 +3278,6 @@ TEST_F(TokenAnnotatorTest, BraceKind) { EXPECT_BRACE_KIND(Tokens[10], BK_Block); EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_StructRBrace); EXPECT_BRACE_KIND(Tokens[11], BK_Block); - - Tokens = annotate("#define MEMBER(NAME) NAME{\"\"}"); - ASSERT_EQ(Tokens.size(), 11u) << Tokens; - EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit); - EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit); } TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Handle pointer/reference in macro definitions (PR #107074)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/107074 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a regression in annotating ObjCBlockLParen (PR #107021)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/107021 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Handle spaces in file paths in git-clang-format.bat (PR #107041)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/107041 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Handle pointer/reference in macro definitions (PR #107074)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/107074 A macro definition needs its own scope stack in the annotator, so we add the MacroBodyScopes stack and use ScopeStack to refer to it when in the macro definition body. Also, we need to have a scope type for a child block because its parent line is parsed (and thus the scope type for the braces is popped off the scope stack) before the lines in the child block are. Fixes #99271. >From ff7a46ef601fb7f37dacf03e3a5a7416d40b7ff3 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Tue, 3 Sep 2024 02:38:35 -0700 Subject: [PATCH] [clang-format] Handle pointer/reference in macro definitions A macro definition needs its own scope stack in the annotator, so we add the MacroBodyScopes stack and use ScopeStack to refer to it when in the macro definition body. Also, we need to have a scope type for a child block because its parent line is parsed (and thus the scope type for the braces is popped off the scope stack) before the lines in the child block are. Fixes #99271. --- clang/lib/Format/TokenAnnotator.cpp | 24 +-- clang/lib/Format/TokenAnnotator.h | 8 +++ clang/unittests/Format/TokenAnnotatorTest.cpp | 15 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 64e936f627d438..8cd2edaf4fbcdc 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -137,9 +137,8 @@ class AnnotatingParser { private: ScopeType getScopeType(const FormatToken &Token) const { switch (Token.getType()) { -case TT_FunctionLBrace: case TT_LambdaLBrace: - return ST_Function; + return ST_ChildBlock; case TT_ClassLBrace: case TT_StructLBrace: case TT_UnionLBrace: @@ -400,7 +399,8 @@ class AnnotatingParser { OpeningParen.Previous->MatchingParen->isOneOf( TT_ObjCBlockLParen, TT_FunctionTypeLParen)) { Contexts.back().IsExpression = false; -} else if (!Line.MustBeDeclaration && !Line.InPPDirective) { +} else if (!Line.MustBeDeclaration && + (!Line.InPPDirective || (Line.InMacroBody && !Scopes.empty( { bool IsForOrCatch = OpeningParen.Previous && OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch); @@ -3650,11 +3650,21 @@ static bool isCtorOrDtorName(const FormatToken *Tok) { } void TokenAnnotator::annotate(AnnotatedLine &Line) { - AnnotatingParser Parser(Style, Line, Keywords, Scopes); + if (!Line.InMacroBody) +MacroBodyScopes.clear(); + + auto &ScopeStack = Line.InMacroBody ? MacroBodyScopes : Scopes; + AnnotatingParser Parser(Style, Line, Keywords, ScopeStack); Line.Type = Parser.parseLine(); - for (auto &Child : Line.Children) -annotate(*Child); + if (!Line.Children.empty()) { +ScopeStack.push_back(ST_ChildBlock); +for (auto &Child : Line.Children) + annotate(*Child); +// ScopeStack can become empty if Child has an unmatched `}`. +if (!ScopeStack.empty()) + ScopeStack.pop_back(); + } // With very deep nesting, ExpressionParser uses lots of stack and the // formatting algorithm is very slow. We're not going to do a good job here @@ -3672,7 +3682,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) { if (IsCpp) { FormatToken *OpeningParen = nullptr; auto *Tok = getFunctionName(Line, OpeningParen); -if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) || +if (Tok && ((!ScopeStack.empty() && ScopeStack.back() == ST_Class) || Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) { Tok->setFinalizedType(TT_CtorDtorDeclName); assert(OpeningParen); diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h index f4f2bba0eb217f..5a02030e5ba7f9 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -36,11 +36,11 @@ enum LineType { }; enum ScopeType { + // Contained in child block. + ST_ChildBlock, // Contained in class declaration/definition. ST_Class, - // Contained within function definition. - ST_Function, - // Contained within other scope block (loop, if/else, etc). + // Contained within other scope block (function, loop, if/else, etc). ST_Other, }; @@ -269,7 +269,7 @@ class TokenAnnotator { const AdditionalKeywords &Keywords; - SmallVector Scopes; + SmallVector Scopes, MacroBodyScopes; }; } // end namespace format diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 497b911f4efbba..93a64276fa021d 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -327,6 +327,21 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) { ASSERT_EQ(Tokens.size(), 26u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::a
[clang] [clang-format] Correctly annotate braces in macro definition (PR #106662)
owenca wrote: /cherry-pick 0fa78b6c7bd43c2498700a98c47a02cf4fd06388 https://github.com/llvm/llvm-project/pull/106662 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Handle spaces in file paths in git-clang-format.bat (PR #107041)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/107041 This patch is provided by @jeliebig. Fixes #107017. >From f44ecbe9880aba26ca02046dbe26582ea81abe73 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Mon, 2 Sep 2024 19:33:01 -0700 Subject: [PATCH] [clang-format] Handle spaces in file paths in git-clang-format.bat This patch is provided by @jeliebig. Fixes #107017. --- clang/tools/clang-format/git-clang-format.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/tools/clang-format/git-clang-format.bat b/clang/tools/clang-format/git-clang-format.bat index 9965cd4312fe39..19c82d8a04132b 100644 --- a/clang/tools/clang-format/git-clang-format.bat +++ b/clang/tools/clang-format/git-clang-format.bat @@ -1 +1 @@ -py -3 %~pn0 %* +py -3 "%~pn0" %* ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a regression in annotating ObjCBlockLParen (PR #107021)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/107021 Fixes #106994. >From 563ad14fb2b5b91adbc906fce34e74e71dc1f141 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Mon, 2 Sep 2024 14:02:54 -0700 Subject: [PATCH] [clang-format] Fix a regression in annotating ObjCBlockLParen Fixes #106994. --- clang/lib/Format/TokenAnnotator.cpp | 3 +-- clang/unittests/Format/TokenAnnotatorTest.cpp | 7 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 64e936f627d438..bf37062393719c 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3588,8 +3588,7 @@ static FormatToken *getFunctionName(const AnnotatedLine &Line, // Make sure the name is followed by a pair of parentheses. if (Name) { - if (Tok->is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) && - Tok->MatchingParen) { + if (Tok->is(tok::l_paren) && Tok->is(TT_Unknown) && Tok->MatchingParen) { OpeningParen = Tok; return Name; } diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 5d37a65250d0b1..b8a86245808e5a 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -1678,6 +1678,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsObjCBlock) { "}();"); ASSERT_EQ(Tokens.size(), 19u) << Tokens; EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_ObjCBlockLBrace); + + Tokens = annotate("id (^block)(Foo *a) = ^id _Nullable(Foo *_Nullable a) {\n" +" return a;\n" +"};"); + ASSERT_EQ(Tokens.size(), 27u) << Tokens; + EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown); // Not CtorDtorDeclName. + EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_ObjCBlockLParen); } TEST_F(TokenAnnotatorTest, UnderstandsObjCMethodExpr) { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Correctly annotate braces in macro definition (PR #106662)
owenca wrote: /cherry-pick 0fa78b6c7bd43c2498700a98c47a02cf4fd06388 https://github.com/llvm/llvm-project/pull/106662 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Correctly annotate braces in macro definition (PR #106662)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/106662 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits