public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* lambda coding style
@ 2024-01-10 19:58 Jason Merrill
  2024-01-10 20:47 ` I am causing valgrind errors Robert Dubner
                   ` (4 more replies)
  0 siblings, 5 replies; 14+ messages in thread
From: Jason Merrill @ 2024-01-10 19:58 UTC (permalink / raw)
  To: GCC Mailing List

What formatting style do we want for non-trivial lambdas in GCC sources? 
  I'm thinking the most consistent choice would be

auto l = [&] (parms) // space between ] (
   {                  // brace on new line, indented two spaces
     return stuff;
   };

By default, recent emacs lines up the { with the previous line, like an 
in-class function definition; I talked it into the above indentation with

(defun lambda-offset (elem)
   (if (assq 'inline-open c-syntactic-context) '+ 0))
(add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))

I think we probably want the same formatting for lambdas in function 
argument lists, e.g.

algorithm ([] (parms)
   {
     return foo;
   });

Any other opinions?

Jason


^ permalink raw reply	[flat|nested] 14+ messages in thread

* I am causing valgrind errors...
  2024-01-10 19:58 lambda coding style Jason Merrill
@ 2024-01-10 20:47 ` Robert Dubner
  2024-01-10 20:51   ` Jakub Jelinek
  2024-01-10 20:59 ` lambda coding style Marek Polacek
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 14+ messages in thread
From: Robert Dubner @ 2024-01-10 20:47 UTC (permalink / raw)
  To: 'GCC Mailing List'; +Cc: Robert Dubner

This message is a bit of a Hail Mary play.  I am not asking anybody to spend 
any real effort on this question; I am, instead, hoping that somebody will 
say, "Oh, sure; you can fix that by doing so-and-so..." or "That happens 
when the GENERIC tree is ..."

Jim Lowden and I are working on a COBOL front end for GCC.  We are, at this 
point, pretty far along.  He's been focusing on the parsing; I've been doing 
the code generation.

Our front end is based on GCC-13; I just merged in "40e16fda0f4 - 
(gnu-gcc/releases/gcc-13) Daily bump." a few hours ago.

The minimalist background is this:  I start the process of building a 
function by calling
build_varargs_function_type_array().

I feed the returned type_decl to build_fn_decl(), which returns a 
function_decl.

That function_decl becomes the root of a tree that gets everything else 
tacked on.

When I am done creating the function, I pass that function_decl to

    cgraph_node::finalize_function (function_decl, true);

This whole process works; we've been producing executables this way for a 
couple of years.  But I freely admit that I don't know if I am performing 
all necessary magical incantations properly.

The COBOL program I am compiling here does absolutely nothing; it just 
returns.  But our COBOL executables have significant overhead; it’s the 
nature of COBOL.  There are a bunch of variables that get created, whether 
they are used or not, and there is boilerplate code on both entry and exit 
to COBOL functions.

Here's the thing:  when I run valgrind on the compilation -- not on the 
executable, but on the compiler with the COBOL front end -- I am getting a 
bunch of errors that look like variations of this:

==1232157== Command: /home/bob/repos/gcc-cobol/build/gcc/cobol1 
playpen.cbl -quiet -dumpbase 
playpen.cbl -mtune=generic -march=x86-64 -ggdb -O0 -o playpen.s -cmain
==1232157==
==1232157== Conditional jump or move depends on uninitialised value(s)
==1232157==    at 0xABA0CB: sparseset_bit_p (sparseset.h:146)
==1232157==    by 0xABA0CB: mark_pseudo_regno_live(int) (ira-lives.cc:326)
==1232157==    by 0xABBDC0: process_bb_node_lives(ira_loop_tree_node*) 
(ira-lives.cc:1434)
==1232157==    by 0xA9E8D5: ira_traverse_loop_tree(bool, 
ira_loop_tree_node*, void (*)(ira_loop_tree_node*), void 
(*)(ira_loop_tree_node*)) (ira-build.cc:1807)
==1232157==    by 0xABC6F3: ira_create_allocno_live_ranges() 
(ira-lives.cc:1734)
==1232157==    by 0xAA038C: ira_build() (ira-build.cc:3483)
==1232157==    by 0xA971BA: ira (ira.cc:5783)
==1232157==    by 0xA971BA: (anonymous 
namespace)::pass_ira::execute(function*) (ira.cc:6106)
==1232157==    by 0xB95A71: execute_one_pass(opt_pass*) (passes.cc:2651)
==1232157==    by 0xB9632F: execute_pass_list_1(opt_pass*) (passes.cc:2760)
==1232157==    by 0xB96341: execute_pass_list_1(opt_pass*) (passes.cc:2761)
==1232157==    by 0xB9636C: execute_pass_list(function*, opt_pass*) 
(passes.cc:2771)
==1232157==    by 0x846BA7: expand (cgraphunit.cc:1841)
==1232157==    by 0x846BA7: cgraph_node::expand() (cgraphunit.cc:1794)
==1232157==    by 0x847E80: output_in_order (cgraphunit.cc:2191)
==1232157==    by 0x847E80: symbol_table::compile() [clone .part.0] 
(cgraphunit.cc:2395)

Please note that the compiler is generating a good executable from the 
function_decl I am passing to cgraph_ node::finalize_function().  But I 
don't like leaving unexplained errors behind me.  They tend to sneak up 
behind, and they often have teeth.

So, here's my question to the giant brains with eidetic memories who 
populate this list:  Can anybody give me a hint as to what I might be doing 
wrong, either with the tree of GENERIC tags or with the way I am asking the 
GCC back end to compile it, to give valgrind that particular bit of 
indigestion?

Thank you so much for even reading this, and thanks for any hint you might 
have.

Bob Dubner


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: I am causing valgrind errors...
  2024-01-10 20:47 ` I am causing valgrind errors Robert Dubner
@ 2024-01-10 20:51   ` Jakub Jelinek
  2024-01-10 21:17     ` Robert Dubner
  0 siblings, 1 reply; 14+ messages in thread
From: Jakub Jelinek @ 2024-01-10 20:51 UTC (permalink / raw)
  To: Robert Dubner; +Cc: 'GCC Mailing List'

On Wed, Jan 10, 2024 at 02:47:08PM -0600, Robert Dubner wrote:
> Here's the thing:  when I run valgrind on the compilation -- not on the 
> executable, but on the compiler with the COBOL front end -- I am getting a 
> bunch of errors that look like variations of this:
> 
> ==1232157== Command: /home/bob/repos/gcc-cobol/build/gcc/cobol1 
> playpen.cbl -quiet -dumpbase 
> playpen.cbl -mtune=generic -march=x86-64 -ggdb -O0 -o playpen.s -cmain
> ==1232157==
> ==1232157== Conditional jump or move depends on uninitialised value(s)
> ==1232157==    at 0xABA0CB: sparseset_bit_p (sparseset.h:146)
> ==1232157==    by 0xABA0CB: mark_pseudo_regno_live(int) (ira-lives.cc:326)
> ==1232157==    by 0xABBDC0: process_bb_node_lives(ira_loop_tree_node*) 

That is normal and not a bug.
If you want to avoid that, you need to configure the compiler with
--enable-valgrind-annotations
(and have valgrind development installed before you configure/build).

	Jakub


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: lambda coding style
  2024-01-10 19:58 lambda coding style Jason Merrill
  2024-01-10 20:47 ` I am causing valgrind errors Robert Dubner
@ 2024-01-10 20:59 ` Marek Polacek
  2024-01-10 21:24   ` Jason Merrill
  2024-01-11 17:45 ` Tom Tromey
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 14+ messages in thread
From: Marek Polacek @ 2024-01-10 20:59 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Mailing List

On Wed, Jan 10, 2024 at 02:58:03PM -0500, Jason Merrill via Gcc wrote:
> What formatting style do we want for non-trivial lambdas in GCC sources?
> I'm thinking the most consistent choice would be
> 
> auto l = [&] (parms) // space between ] (
>   {                  // brace on new line, indented two spaces
>     return stuff;
>   };

Sure, why not.  Consistency is what matters.  Thus far we seem
to have been very inconsistent.  ;)
 
> By default, recent emacs lines up the { with the previous line, like an
> in-class function definition; I talked it into the above indentation with
> 
> (defun lambda-offset (elem)
>   (if (assq 'inline-open c-syntactic-context) '+ 0))
> (add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))
> 
> I think we probably want the same formatting for lambdas in function
> argument lists, e.g.
> 
> algorithm ([] (parms)
>   {
>     return foo;
>   });

And what about lambdas in conditions:

if (foo ()
    && [&] (params) mutable
       {
	 return 42;
       } ())

should the { go just below [?

Marek


^ permalink raw reply	[flat|nested] 14+ messages in thread

* RE: I am causing valgrind errors...
  2024-01-10 20:51   ` Jakub Jelinek
@ 2024-01-10 21:17     ` Robert Dubner
  0 siblings, 0 replies; 14+ messages in thread
From: Robert Dubner @ 2024-01-10 21:17 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: GCC Mailing List

In the words of Nick Danger: "You just saved me a lot of investigative
work."

Thank you so much.

Bob D.

-----Original Message-----
From: Jakub Jelinek <jakub@redhat.com> 
Sent: Wednesday, January 10, 2024 15:51
To: Robert Dubner <rdubner@symas.com>
Cc: 'GCC Mailing List' <gcc@gcc.gnu.org>
Subject: Re: I am causing valgrind errors...

On Wed, Jan 10, 2024 at 02:47:08PM -0600, Robert Dubner wrote:
> Here's the thing:  when I run valgrind on the compilation -- not on 
> the executable, but on the compiler with the COBOL front end -- I am 
> getting a bunch of errors that look like variations of this:
> 
> ==1232157== Command: /home/bob/repos/gcc-cobol/build/gcc/cobol1
> playpen.cbl -quiet -dumpbase
> playpen.cbl -mtune=generic -march=x86-64 -ggdb -O0 -o playpen.s -cmain 
> ==1232157== ==1232157== Conditional jump or move depends on 
> uninitialised value(s)
> ==1232157==    at 0xABA0CB: sparseset_bit_p (sparseset.h:146)
> ==1232157==    by 0xABA0CB: mark_pseudo_regno_live(int)
(ira-lives.cc:326)
> ==1232157==    by 0xABBDC0: process_bb_node_lives(ira_loop_tree_node*) 

That is normal and not a bug.
If you want to avoid that, you need to configure the compiler with
--enable-valgrind-annotations (and have valgrind development installed
before you configure/build).

	Jakub


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: lambda coding style
  2024-01-10 20:59 ` lambda coding style Marek Polacek
@ 2024-01-10 21:24   ` Jason Merrill
  2024-01-10 21:41     ` Marek Polacek
  2024-01-20  2:22     ` Hans-Peter Nilsson
  0 siblings, 2 replies; 14+ messages in thread
From: Jason Merrill @ 2024-01-10 21:24 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Mailing List

On 1/10/24 15:59, Marek Polacek wrote:
> On Wed, Jan 10, 2024 at 02:58:03PM -0500, Jason Merrill via Gcc wrote:
>> What formatting style do we want for non-trivial lambdas in GCC sources?
>> I'm thinking the most consistent choice would be
>>
>> auto l = [&] (parms) // space between ] (
>>    {                  // brace on new line, indented two spaces
>>      return stuff;
>>    };
> 
> Sure, why not.  Consistency is what matters.  Thus far we seem
> to have been very inconsistent.  ;)
>   
>> By default, recent emacs lines up the { with the previous line, like an
>> in-class function definition; I talked it into the above indentation with
>>
>> (defun lambda-offset (elem)
>>    (if (assq 'inline-open c-syntactic-context) '+ 0))
>> (add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))
>>
>> I think we probably want the same formatting for lambdas in function
>> argument lists, e.g.
>>
>> algorithm ([] (parms)
>>    {
>>      return foo;
>>    });
> 
> And what about lambdas in conditions:
> 
> if (foo ()
>      && [&] (params) mutable
>         {
> 	 return 42;
>         } ())
> 
> should the { go just below [?

I think we don't want the { to go below the [ in general; that was the 
old emacs default behavior, and it produced lambda bodies with excessive 
indentation.

With my adjustment above, emacs indents the { two spaces from the &&, 
which seems a bit arbitrary but reasonable.

Jason


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: lambda coding style
  2024-01-10 21:24   ` Jason Merrill
@ 2024-01-10 21:41     ` Marek Polacek
  2024-01-11  2:34       ` Jason Merrill
  2024-01-20  2:22     ` Hans-Peter Nilsson
  1 sibling, 1 reply; 14+ messages in thread
From: Marek Polacek @ 2024-01-10 21:41 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Mailing List

On Wed, Jan 10, 2024 at 04:24:42PM -0500, Jason Merrill wrote:
> On 1/10/24 15:59, Marek Polacek wrote:
> > On Wed, Jan 10, 2024 at 02:58:03PM -0500, Jason Merrill via Gcc wrote:
> > > What formatting style do we want for non-trivial lambdas in GCC sources?
> > > I'm thinking the most consistent choice would be
> > > 
> > > auto l = [&] (parms) // space between ] (
> > >    {                  // brace on new line, indented two spaces
> > >      return stuff;
> > >    };
> > 
> > Sure, why not.  Consistency is what matters.  Thus far we seem
> > to have been very inconsistent.  ;)
> > > By default, recent emacs lines up the { with the previous line, like an
> > > in-class function definition; I talked it into the above indentation with
> > > 
> > > (defun lambda-offset (elem)
> > >    (if (assq 'inline-open c-syntactic-context) '+ 0))
> > > (add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))
> > > 
> > > I think we probably want the same formatting for lambdas in function
> > > argument lists, e.g.
> > > 
> > > algorithm ([] (parms)
> > >    {
> > >      return foo;
> > >    });
> > 
> > And what about lambdas in conditions:
> > 
> > if (foo ()
> >      && [&] (params) mutable
> >         {
> > 	 return 42;
> >         } ())
> > 
> > should the { go just below [?
> 
> I think we don't want the { to go below the [ in general; that was the old
> emacs default behavior, and it produced lambda bodies with excessive
> indentation.
> 
> With my adjustment above, emacs indents the { two spaces from the &&, which
> seems a bit arbitrary but reasonable.

Fair enough, I think that's better.

I suppose we should add a note wrt lambdas to
https://gcc.gnu.org/codingconventions.html#Cxx_Conventions

Marek


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: lambda coding style
  2024-01-10 21:41     ` Marek Polacek
@ 2024-01-11  2:34       ` Jason Merrill
  2024-01-11  5:30         ` waffl3x
  0 siblings, 1 reply; 14+ messages in thread
From: Jason Merrill @ 2024-01-11  2:34 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Mailing List

On 1/10/24 16:41, Marek Polacek wrote:
> On Wed, Jan 10, 2024 at 04:24:42PM -0500, Jason Merrill wrote:
>> On 1/10/24 15:59, Marek Polacek wrote:
>>> On Wed, Jan 10, 2024 at 02:58:03PM -0500, Jason Merrill via Gcc wrote:
>>>> What formatting style do we want for non-trivial lambdas in GCC sources?
>>>> I'm thinking the most consistent choice would be
>>>>
>>>> auto l = [&] (parms) // space between ] (
>>>>     {                  // brace on new line, indented two spaces
>>>>       return stuff;
>>>>     };
>>>
>>> Sure, why not.  Consistency is what matters.  Thus far we seem
>>> to have been very inconsistent.  ;)
>>>> By default, recent emacs lines up the { with the previous line, like an
>>>> in-class function definition; I talked it into the above indentation with
>>>>
>>>> (defun lambda-offset (elem)
>>>>     (if (assq 'inline-open c-syntactic-context) '+ 0))
>>>> (add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))
>>>>
>>>> I think we probably want the same formatting for lambdas in function
>>>> argument lists, e.g.
>>>>
>>>> algorithm ([] (parms)
>>>>     {
>>>>       return foo;
>>>>     });
>>>
>>> And what about lambdas in conditions:
>>>
>>> if (foo ()
>>>       && [&] (params) mutable
>>>          {
>>> 	 return 42;
>>>          } ())
>>>
>>> should the { go just below [?
>>
>> I think we don't want the { to go below the [ in general; that was the old
>> emacs default behavior, and it produced lambda bodies with excessive
>> indentation.
>>
>> With my adjustment above, emacs indents the { two spaces from the &&, which
>> seems a bit arbitrary but reasonable.
> 
> Fair enough, I think that's better.
> 
> I suppose we should add a note wrt lambdas to
> https://gcc.gnu.org/codingconventions.html#Cxx_Conventions

Indeed, that's my goal.

Looking through the various lambdas already in the compiler, I see a 
good number already follow my suggested indentation, but a large 
majority omit the space between ] and (, so maybe we don't want it?

Jason


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: lambda coding style
  2024-01-11  2:34       ` Jason Merrill
@ 2024-01-11  5:30         ` waffl3x
  0 siblings, 0 replies; 14+ messages in thread
From: waffl3x @ 2024-01-11  5:30 UTC (permalink / raw)
  To: Jason Merrill; +Cc: Marek Polacek, GCC Mailing List






On Wednesday, January 10th, 2024 at 7:34 PM, Jason Merrill via Gcc <gcc@gcc.gnu.org> wrote:


> 
> 
> On 1/10/24 16:41, Marek Polacek wrote:
> 
> > On Wed, Jan 10, 2024 at 04:24:42PM -0500, Jason Merrill wrote:
> > 
> > > On 1/10/24 15:59, Marek Polacek wrote:
> > > 
> > > > On Wed, Jan 10, 2024 at 02:58:03PM -0500, Jason Merrill via Gcc wrote:
> > > > 
> > > > > What formatting style do we want for non-trivial lambdas in GCC sources?
> > > > > I'm thinking the most consistent choice would be
> > > > > 
> > > > > auto l = [&] (parms) // space between ] (
> > > > > { // brace on new line, indented two spaces
> > > > > return stuff;
> > > > > };
> > > > 
> > > > Sure, why not. Consistency is what matters. Thus far we seem
> > > > to have been very inconsistent. ;)
> > > > 
> > > > > By default, recent emacs lines up the { with the previous line, like an
> > > > > in-class function definition; I talked it into the above indentation with
> > > > > 
> > > > > (defun lambda-offset (elem)
> > > > > (if (assq 'inline-open c-syntactic-context) '+ 0))
> > > > > (add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))
> > > > > 
> > > > > I think we probably want the same formatting for lambdas in function
> > > > > argument lists, e.g.
> > > > > 
> > > > > algorithm ([] (parms)
> > > > > {
> > > > > return foo;
> > > > > });
> > > > 
> > > > And what about lambdas in conditions:
> > > > 
> > > > if (foo ()
> > > > && [&] (params) mutable
> > > > {
> > > > return 42;
> > > > } ())
> > > > 
> > > > should the { go just below [?
> > > 
> > > I think we don't want the { to go below the [ in general; that was the old
> > > emacs default behavior, and it produced lambda bodies with excessive
> > > indentation.
> > > 
> > > With my adjustment above, emacs indents the { two spaces from the &&, which
> > > seems a bit arbitrary but reasonable.
> > 
> > Fair enough, I think that's better.
> > 
> > I suppose we should add a note wrt lambdas to
> > https://gcc.gnu.org/codingconventions.html#Cxx_Conventions
> 
> 
> Indeed, that's my goal.
> 
> Looking through the various lambdas already in the compiler, I see a
> good number already follow my suggested indentation, but a large
> majority omit the space between ] and (, so maybe we don't want it?
> 
> Jason

I'm of the opinion that you should just be naming the lambdas once you
want to use them in complex logic. It gets really messy really quick,
if not immediately. It's not as bad when used in algorithms since they
generally are the last parameter, and it's an established practice to
do so, but it's still not super wonderful when they get long.

The other thing I want to raise is whether we want to allow different
formatting for IILE's, having the opening brace on a new line makes
those use cases look a lot messier imo. IILE's stand out quite a bit
already so the rationale for the current bracing is a little weaker for
lambdas imo, especially IILE's. But for a lambda with parameters I can
see the value of formatting it more strictly.

With that said I understand the value of consistency, so perhaps
consistency is the better way to go.

Alex

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: lambda coding style
  2024-01-10 19:58 lambda coding style Jason Merrill
  2024-01-10 20:47 ` I am causing valgrind errors Robert Dubner
  2024-01-10 20:59 ` lambda coding style Marek Polacek
@ 2024-01-11 17:45 ` Tom Tromey
  2024-01-11 17:48 ` Martin Jambor
       [not found] ` <65328.124011112484300622@us-mta-617.us.mimecast.lan>
  4 siblings, 0 replies; 14+ messages in thread
From: Tom Tromey @ 2024-01-11 17:45 UTC (permalink / raw)
  To: Jason Merrill via Gcc; +Cc: Jason Merrill

>>>>> "Jason" == Jason Merrill via Gcc <gcc@gcc.gnu.org> writes:

Jason> I think we probably want the same formatting for lambdas in function
Jason> argument lists, e.g.

Jason> algorithm ([] (parms)
Jason>   {
Jason>     return foo;
Jason>   });

Jason> Any other opinions?

FWIW gdb did pretty much this same thing.  Our rules are documented
here:

https://sourceware.org/gdb/wiki/Internals%20GDB-C-Coding-Standards#Indentation_of_lambdas_as_parameters

There's a special case in here where a call takes a single lambda as the
last argument -- gdb indents that in a more block-like way.

Tom

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: lambda coding style
  2024-01-10 19:58 lambda coding style Jason Merrill
                   ` (2 preceding siblings ...)
  2024-01-11 17:45 ` Tom Tromey
@ 2024-01-11 17:48 ` Martin Jambor
       [not found] ` <65328.124011112484300622@us-mta-617.us.mimecast.lan>
  4 siblings, 0 replies; 14+ messages in thread
From: Martin Jambor @ 2024-01-11 17:48 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Mailing List

Hi,

On Wed, Jan 10 2024, Jason Merrill via Gcc wrote:
> What formatting style do we want for non-trivial lambdas in GCC sources? 
>   I'm thinking the most consistent choice would be
>
> auto l = [&] (parms) // space between ] (
>    {                  // brace on new line, indented two spaces
>      return stuff;
>    };
>
> By default, recent emacs lines up the { with the previous line, like an 
> in-class function definition; I talked it into the above indentation with
>
> (defun lambda-offset (elem)
>    (if (assq 'inline-open c-syntactic-context) '+ 0))
> (add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))

Is this really add-to-hook and not add-hook?

Thanks!

Martin

>
> I think we probably want the same formatting for lambdas in function 
> argument lists, e.g.
>
> algorithm ([] (parms)
>    {
>      return foo;
>    });
>
> Any other opinions?
>
> Jason

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: lambda coding style
       [not found] ` <65328.124011112484300622@us-mta-617.us.mimecast.lan>
@ 2024-01-11 19:25   ` Jason Merrill
  2024-01-12 11:06     ` Martin Jambor
  0 siblings, 1 reply; 14+ messages in thread
From: Jason Merrill @ 2024-01-11 19:25 UTC (permalink / raw)
  To: Martin Jambor; +Cc: GCC Mailing List

On 1/11/24 12:48, Martin Jambor wrote:
> On Wed, Jan 10 2024, Jason Merrill via Gcc wrote:
>> What formatting style do we want for non-trivial lambdas in GCC sources?
>>    I'm thinking the most consistent choice would be
>>
>> auto l = [&] (parms) // space between ] (
>>     {                  // brace on new line, indented two spaces
>>       return stuff;
>>     };
>>
>> By default, recent emacs lines up the { with the previous line, like an
>> in-class function definition; I talked it into the above indentation with
>>
>> (defun lambda-offset (elem)
>>     (if (assq 'inline-open c-syntactic-context) '+ 0))
>> (add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))
> 
> Is this really add-to-hook and not add-hook?

Oops! add-to-hook is a custom variant I wrote back in like 1992; for 
add-hook you'll want

(add-hook 'c++-mode-hook
           '(lambda () (c-set-offset 'inlambda 'lambda-offset)))

Jason


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: lambda coding style
  2024-01-11 19:25   ` Jason Merrill
@ 2024-01-12 11:06     ` Martin Jambor
  0 siblings, 0 replies; 14+ messages in thread
From: Martin Jambor @ 2024-01-12 11:06 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Mailing List

Hi,

On Thu, Jan 11 2024, Jason Merrill wrote:
> On 1/11/24 12:48, Martin Jambor wrote:
>> On Wed, Jan 10 2024, Jason Merrill via Gcc wrote:
>>> What formatting style do we want for non-trivial lambdas in GCC sources?
>>>    I'm thinking the most consistent choice would be
>>>
>>> auto l = [&] (parms) // space between ] (
>>>     {                  // brace on new line, indented two spaces
>>>       return stuff;
>>>     };
>>>
>>> By default, recent emacs lines up the { with the previous line, like an
>>> in-class function definition; I talked it into the above indentation with
>>>
>>> (defun lambda-offset (elem)
>>>     (if (assq 'inline-open c-syntactic-context) '+ 0))
>>> (add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))
>> 
>> Is this really add-to-hook and not add-hook?
>
> Oops! add-to-hook is a custom variant I wrote back in like 1992; for 
> add-hook you'll want
>
> (add-hook 'c++-mode-hook
>            '(lambda () (c-set-offset 'inlambda 'lambda-offset)))
>

I see, thanks!

Martin


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: lambda coding style
  2024-01-10 21:24   ` Jason Merrill
  2024-01-10 21:41     ` Marek Polacek
@ 2024-01-20  2:22     ` Hans-Peter Nilsson
  1 sibling, 0 replies; 14+ messages in thread
From: Hans-Peter Nilsson @ 2024-01-20  2:22 UTC (permalink / raw)
  To: Jason Merrill; +Cc: Marek Polacek, GCC Mailing List

On Wed, 10 Jan 2024, Jason Merrill via Gcc wrote:
> On 1/10/24 15:59, Marek Polacek wrote:
> > On Wed, Jan 10, 2024 at 02:58:03PM -0500, Jason Merrill via Gcc wrote:
> > > What formatting style do we want for non-trivial lambdas in GCC sources?
> > > I'm thinking the most consistent choice would be
> > > 
> > > auto l = [&] (parms) // space between ] (
> > >    {                  // brace on new line, indented two spaces
> > >      return stuff;
> > >    };
> > 
> > Sure, why not.  Consistency is what matters.  Thus far we seem
> > to have been very inconsistent.  ;)
> >   
> > > By default, recent emacs lines up the { with the previous line, like an
> > > in-class function definition; I talked it into the above indentation with
> > > 
> > > (defun lambda-offset (elem)
> > >    (if (assq 'inline-open c-syntactic-context) '+ 0))
> > > (add-to-hook 'c++-mode-hook '(c-set-offset 'inlambda 'lambda-offset))
> > > 
> > > I think we probably want the same formatting for lambdas in function
> > > argument lists, e.g.
> > > 
> > > algorithm ([] (parms)
> > >    {
> > >      return foo;
> > >    });
> > 
> > And what about lambdas in conditions:
> > 
> > if (foo ()
> >      && [&] (params) mutable
> >         {
> > 	 return 42;
> >         } ())
> > 
> > should the { go just below [?

Also, what about trailing-type and mutable (above) when needing 
a line-break?

(FTR: in technical terms, trailing-type is known as the 
pointy-arrow-declaring-return-type thing :) the optional "-> 
type" between "(parms)" and "{ body }")

I suggest the obvious (to me): line up stuff after (params) with 
the opening brace for body, when needing a line-break before 
that item, and line-break *before* "->" .

brgds, H-P

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2024-01-20  2:22 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-10 19:58 lambda coding style Jason Merrill
2024-01-10 20:47 ` I am causing valgrind errors Robert Dubner
2024-01-10 20:51   ` Jakub Jelinek
2024-01-10 21:17     ` Robert Dubner
2024-01-10 20:59 ` lambda coding style Marek Polacek
2024-01-10 21:24   ` Jason Merrill
2024-01-10 21:41     ` Marek Polacek
2024-01-11  2:34       ` Jason Merrill
2024-01-11  5:30         ` waffl3x
2024-01-20  2:22     ` Hans-Peter Nilsson
2024-01-11 17:45 ` Tom Tromey
2024-01-11 17:48 ` Martin Jambor
     [not found] ` <65328.124011112484300622@us-mta-617.us.mimecast.lan>
2024-01-11 19:25   ` Jason Merrill
2024-01-12 11:06     ` Martin Jambor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).