Re: pgindent && weirdness

From: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Michael Paquier <michael(at)paquier(dot)xyz>, Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Piotr Stefaniak <postgres(at)piotr-stefaniak(dot)me>
Subject: Re: pgindent && weirdness
Date: 2020-02-17 22:46:48
Message-ID: CA+hUKGKFQiLEj2dZ=SPvdUfyT7SJ48nVb3UErbe9aMxZDsLF+A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Feb 18, 2020 at 4:35 AM Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> wrote:
> Hmm ... this suggests to me that if you remove these alleged special
> cases for offsetof and sizeof, the new code handles them correctly
> anyway. Do you think it's worth giving that a try? Not because
> removing the special cases would have any value, but rather to see if
> anything interesting pops up.

Good thought, since keywords also have last_token == ident so it's
redundant to check the keyword. But while testing that I realised
that either way we get this wrong:

- return (int) *s1 - (int) *s2;
+ return (int) * s1 - (int) *s2;

So I think the right formulation is one that allows offsetof and
sizeof to receive not-a-cast treatment, but not any other known
keyword:

diff --git a/indent.c b/indent.c
index 9faf57a..ed6dce2 100644
--- a/indent.c
+++ b/indent.c
@@ -570,8 +570,15 @@ check_type:
ps.in_or_st = false; /* turn off flag for structure decl or
* initialization */
}
- /* parenthesized type following sizeof or offsetof is not a cast */
- if (ps.keyword == 1 || ps.keyword == 2)
+ /*
+ * parenthesized type following sizeof or offsetof is not a
+ * cast; we also assume the same about similar macros,
+ * so if there is any other non-keyword identifier immediately
+ * preceding a type name in parens we won't consider it to be
+ * a cast
+ */
+ if (ps.last_token == ident &&
+ (ps.keyword == 0 || ps.keyword == 1 || ps.keyword == 2))
ps.not_cast_mask |= 1 << ps.p_l_follow;
break;

Another problem is that there is one thing in our tree that looks like
a non-cast under the new rule, but it actually expands to a type name,
so now we get that wrong! (I mean, unpatched indent doesn't really
understand it either, it thinks it's a cast, but at least it knows the
following * is not a binary operator):

- STACK_OF(X509_NAME) *root_cert_list = NULL;
+ STACK_OF(X509_NAME) * root_cert_list = NULL;

That's a macro from an OpenSSL header. Not sure what to do about that.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2020-02-17 22:53:33 more ALTER .. DEPENDS ON EXTENSION fixes
Previous Message Dave Cramer 2020-02-17 22:12:04 Re: Error on failed COMMIT