[RFC] codingconventions: add lambda guidelines

Message ID 20240111213310.1566285-1-jason@redhat.com
State New
Headers
Series [RFC] codingconventions: add lambda guidelines |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply

Commit Message

Jason Merrill Jan. 11, 2024, 9:33 p.m. UTC
  Now in patch form!

Any further comments?

---
 htdocs/codingconventions.html | 60 +++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)


base-commit: 62250c79a7483076f1b2b11496e4a1123c9df6a0
  

Comments

Marek Polacek Jan. 11, 2024, 9:56 p.m. UTC | #1
On Thu, Jan 11, 2024 at 04:33:10PM -0500, Jason Merrill wrote:
> Now in patch form!
> 
> Any further comments?

It looks good to me, but it doesn't say if we want to put a space
after } and before () if we're invoking the lambda (I suppose we
want that space).  As in

  ... = [&] (tree arg) { ... } ();
 

Thanks,

> ---
>  htdocs/codingconventions.html | 60 +++++++++++++++++++++++++++++++++++
>  1 file changed, 60 insertions(+)
> 
> diff --git a/htdocs/codingconventions.html b/htdocs/codingconventions.html
> index f5a356a8..2bbf6670 100644
> --- a/htdocs/codingconventions.html
> +++ b/htdocs/codingconventions.html
> @@ -76,6 +76,7 @@ the conventions separately from any other changes to the code.</p>
>          <li><a href="#Template_Form">Templates</a></li>
>          <li><a href="#ExternC">Extern "C"</a></li>
>          <li><a href="#Namespace_Form">Namespaces</a></li>
> +        <li><a href="#Lambda_Form">Lambdas</a></li>
>          </ul>
>      </li>
>      </ul>
> @@ -1488,6 +1489,65 @@ with a right brace, optional closing comment, and a new line.
>  Definitions within the body of a namespace are not indented.
>  </p>
>  
> +<h4 id="Lambda_Form">Lambdas</h4>
> +
> +<p>There should be a space between the lambda-introducer and the parameter
> +  list, if any.</p>
> +<p>Lambdas that do not outlive their enclosing function should
> +  typically use <code>[&]</code> implicit capture.</p>
> +
> +<blockquote><pre><code>auto l = [&] (tree arg) { ... };
> +</code></pre></blockquote>
> +
> +<p>If a lambda does not fit on one line, the left brace should be indented like
> +the body of a for-statement.</p>
> +
> +<blockquote><pre><code>auto l = [&] (tree arg)
> +  {
> +    ...
> +  };
> +</code></pre></blockquote>
> +
> +<p>This also applies if the lambda is the last argument, and only lambda
> +argument, to a function.</p>
> +
> +<blockquote><pre><code>std::for_each (start, end, [&] (tree arg)
> +  {
> +    ...
> +  });
> +</code></pre></blockquote>
> +
> +To get the above behavior from
> +<a href="https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html">
> +  GNU Emacs CC Mode</a>, you can add this to your <code>.emacs</code>:
> +
> +<blockquote><pre><code>(defun lambda-offset (elem)
> +  "If the opening brace of a lambda is on a new line, indent it one step."
> +  (if (assq 'inline-open c-syntactic-context) '+ 0))
> +(add-hook 'c++-mode-hook
> +	  '(lambda () (c-set-offset 'inlambda 'lambda-offset)))
> +</code></pre></blockquote>
> +
> +<p>If the multi-line lambda is not the last argument, or there are multiple
> +lambda arguments, you are encouraged to make them local variables, as
> +the <code>l</code> examples above.  If you do pass them directly, they should
> +be indented like other parameters.
> +
> +<blockquote><pre><code>my_algo (start, end,
> +	 [&] (tree arg)
> +           {
> +             thing one...
> +           },
> +	 [&] (tree arg)
> +           {
> +             thing two...
> +           });
> +</code></pre></blockquote>
> +
> +<p>See also the
> +  <a href="https://sourceware.org/gdb/wiki/Internals%20GDB-C-Coding-Standards#Indentation_of_lambdas_as_parameters">
> +    GDB coding standards</a>.</p>
> +
>  <h2 id="python">Python Language Conventions</h2>
>  
>  <p>Python scripts should follow <a href="https://peps.python.org/pep-0008/">PEP 8 – Style Guide for Python Code</a>
> 
> base-commit: 62250c79a7483076f1b2b11496e4a1123c9df6a0
> -- 
> 2.39.3
> 

Marek
  
Gerald Pfeifer Jan. 12, 2024, 12:20 a.m. UTC | #2
On Thu, 11 Jan 2024, Jason Merrill wrote:
> Now in patch form!

It appears quite clear to me.

(At first I thought we need to escape the '&' in "[&] (tree arg)" as
"&amp;", but checking with validator.w3.org apparently not so in this 
specific context.)

Gerald
  

Patch

diff --git a/htdocs/codingconventions.html b/htdocs/codingconventions.html
index f5a356a8..2bbf6670 100644
--- a/htdocs/codingconventions.html
+++ b/htdocs/codingconventions.html
@@ -76,6 +76,7 @@  the conventions separately from any other changes to the code.</p>
         <li><a href="#Template_Form">Templates</a></li>
         <li><a href="#ExternC">Extern "C"</a></li>
         <li><a href="#Namespace_Form">Namespaces</a></li>
+        <li><a href="#Lambda_Form">Lambdas</a></li>
         </ul>
     </li>
     </ul>
@@ -1488,6 +1489,65 @@  with a right brace, optional closing comment, and a new line.
 Definitions within the body of a namespace are not indented.
 </p>
 
+<h4 id="Lambda_Form">Lambdas</h4>
+
+<p>There should be a space between the lambda-introducer and the parameter
+  list, if any.</p>
+<p>Lambdas that do not outlive their enclosing function should
+  typically use <code>[&]</code> implicit capture.</p>
+
+<blockquote><pre><code>auto l = [&] (tree arg) { ... };
+</code></pre></blockquote>
+
+<p>If a lambda does not fit on one line, the left brace should be indented like
+the body of a for-statement.</p>
+
+<blockquote><pre><code>auto l = [&] (tree arg)
+  {
+    ...
+  };
+</code></pre></blockquote>
+
+<p>This also applies if the lambda is the last argument, and only lambda
+argument, to a function.</p>
+
+<blockquote><pre><code>std::for_each (start, end, [&] (tree arg)
+  {
+    ...
+  });
+</code></pre></blockquote>
+
+To get the above behavior from
+<a href="https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html">
+  GNU Emacs CC Mode</a>, you can add this to your <code>.emacs</code>:
+
+<blockquote><pre><code>(defun lambda-offset (elem)
+  "If the opening brace of a lambda is on a new line, indent it one step."
+  (if (assq 'inline-open c-syntactic-context) '+ 0))
+(add-hook 'c++-mode-hook
+	  '(lambda () (c-set-offset 'inlambda 'lambda-offset)))
+</code></pre></blockquote>
+
+<p>If the multi-line lambda is not the last argument, or there are multiple
+lambda arguments, you are encouraged to make them local variables, as
+the <code>l</code> examples above.  If you do pass them directly, they should
+be indented like other parameters.
+
+<blockquote><pre><code>my_algo (start, end,
+	 [&] (tree arg)
+           {
+             thing one...
+           },
+	 [&] (tree arg)
+           {
+             thing two...
+           });
+</code></pre></blockquote>
+
+<p>See also the
+  <a href="https://sourceware.org/gdb/wiki/Internals%20GDB-C-Coding-Standards#Indentation_of_lambdas_as_parameters">
+    GDB coding standards</a>.</p>
+
 <h2 id="python">Python Language Conventions</h2>
 
 <p>Python scripts should follow <a href="https://peps.python.org/pep-0008/">PEP 8 – Style Guide for Python Code</a>