[PATCH v3] telemetry: use portable syntax to initialize array
Bruce Richardson
bruce.richardson at intel.com
Tue Apr 4 18:08:47 CEST 2023
On Tue, Apr 04, 2023 at 08:54:30AM -0700, Tyler Retzlaff wrote:
> On Tue, Apr 04, 2023 at 09:51:04AM +0100, Bruce Richardson wrote:
> > On Mon, Apr 03, 2023 at 11:59:25AM -0700, Tyler Retzlaff wrote:
> > > Use of ranges in designated initialization are a non-standard gcc
> > > extension. Use loops to initialize permitted characters on first use.
> > >
> > > Signed-off-by: Tyler Retzlaff <roretzla at linux.microsoft.com>
> >
> > Acked-by: Bruce Richardson <bruce.richardson at intel.com>
> >
> > > ---
> > > lib/telemetry/telemetry_data.c | 22 +++++++++++++++-------
> > > 1 file changed, 15 insertions(+), 7 deletions(-)
> > >
> > > diff --git a/lib/telemetry/telemetry_data.c b/lib/telemetry/telemetry_data.c
> > > index 2bac2de..562b387 100644
> > > --- a/lib/telemetry/telemetry_data.c
> > > +++ b/lib/telemetry/telemetry_data.c
> > > @@ -152,13 +152,21 @@
> > > static bool
> > > valid_name(const char *name)
> > > {
> > > - char allowed[128] = {
> > > - ['0' ... '9'] = 1,
> > > - ['A' ... 'Z'] = 1,
> > > - ['a' ... 'z'] = 1,
> > > - ['_'] = 1,
> > > - ['/'] = 1,
> > > - };
> > > + int index;
> >
> > My preference would be to limit the scope of index to the if block, but ok
> > to keep as here.
>
> yes, mine too. but i forgot we aren't C99 (yet) so i had to move it for
> this patch.
>
We do allow variable declarations within blocks, so no need to move it up
here. However, I believe our coding standards currently require them at the
*top* of each block, not in the middle - but also not just at the top of
each function. See [1] for the details. [For some reason this is in the
sub-section under "indentation" in the guide!]
[1] https://doc.dpdk.org/guides-22.11/contributing/coding_style.html#local-variables
> >
> > [In fact, when we switch to C11, I'd love to see the coding standards
> > relaxed to allow loop variable definition inside the for statement itself]
>
> i'm for it, in fact for any variable i prefer declaration at or near
> first use because it let's me const more. but i know that can be an
> unpopular opinion so no need to hit 'r' to tell me off, i will follow
> any and all documented convention the community has.
>
For const, our coding standards already call this out as an exception,
where it's allowed to define a variable in the middle of a block.
Generally though, I too prefer vars to be defined at site of first use,
rather than all together at the top of the block. Makes it easier when
commenting out code for debugging, not to get unused var warnings.
/Bruce
More information about the dev
mailing list