Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hRe9k-0003xq-UX for pgsql-hackers@arkaria.postgresql.org; Fri, 17 May 2019 14:48:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hRe9h-0005me-NM for pgsql-hackers@arkaria.postgresql.org; Fri, 17 May 2019 14:48:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hRe9h-0005mW-EV for pgsql-hackers@lists.postgresql.org; Fri, 17 May 2019 14:48:17 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hRe9f-0003DH-BG for pgsql-hackers@postgresql.org; Fri, 17 May 2019 14:48:17 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x4HEm3Ss016887; Fri, 17 May 2019 10:48:03 -0400 From: Tom Lane To: Thomas Munro cc: Pg Hackers , Michael Paquier , Piotr Stefaniak Subject: Re: Emacs vs pg_indent's weird indentation for function declarations In-reply-to: <17265.1557955842@sss.pgh.pa.us> References: <7659.1548653310@sss.pgh.pa.us> <20190128070842.GF15401@paquier.xyz> <14259.1557895390@sss.pgh.pa.us> <9170.1557955021@sss.pgh.pa.us> <17265.1557955842@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Wed, 15 May 2019 17:30:42 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <16885.1558104483.1@sss.pgh.pa.us> Date: Fri, 17 May 2019 10:48:03 -0400 Message-ID: <16886.1558104483@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I wrote: >>> A small problem with the "rejiggering" is that it now makes the wrong >>> choice for K&R-style function definitions, causing them to be weirdly >>> indented. For our purposes, that's a non-problem so I'm not excited >>> about trying to make it smart enough to recognize those. We do have >>> a couple of amazingly old and crufty K&R-style functions in src/port/, >>> though, so probably we'd wish to fix those. To be concrete about that: the existing behavior when trying to decide whether "foo(" starts a function declaration or a function definition is to scan to the matching right paren[1] and then, if the immediately next character is ';' or ',', it's a declaration; else it's a definition. This fails on decls with attributes, such as extern void pg_re_throw(void) __attribute__((noreturn)); My patch changes the rule to be "scan until we see a ';' ',' or '{' that's not within parens; then it's a definition if that character is '{', otherwise a declaration". So it gets the right answer for the above, but the wrong answer for int isinf(x) double x; { It doesn't really seem practical to me to make the lookahead function smart enough to tell the difference between attributes and K&R-style parameter declarations. What I'm thinking of doing to have an upstreamable patch is to invent a new switch, perhaps '-kr'/'-nkr', to indicate whether the user is more worried about K&R function declarations than she is about function attributes. The default for this switch could be debated perhaps, but I'd just stick an explicit -nkr into pgindent's invocation, so we wouldn't care too much. (This would also ensure that pgindent would fail if you try to use a too-old copy of pg_bsd_indent...) regards, tom lane [1] Um, well, actually the existing behavior is to scan to the *first* right paren. But upgrading it to count parens correctly seems like an unobjectionable improvement in any case.