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.92) (envelope-from ) id 1jF64T-0004sY-Uz for pgsql-hackers@arkaria.postgresql.org; Fri, 20 Mar 2020 01:03:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jF64Q-0007ru-AI for pgsql-hackers@arkaria.postgresql.org; Fri, 20 Mar 2020 01:03:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jF64P-0007rn-U5 for pgsql-hackers@lists.postgresql.org; Fri, 20 Mar 2020 01:03:30 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jF64I-0002J8-MM for pgsql-hackers@lists.postgresql.org; Fri, 20 Mar 2020 01:03:28 +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 02K13Hs7018568; Thu, 19 Mar 2020 21:03:17 -0400 From: Tom Lane To: Andres Freund cc: Amit Kapila , Michael Paquier , Julien Rouhaud , Masahiko Sawada , PostgreSQL Hackers Subject: Re: Missing errcode() in ereport In-reply-to: <5995.1584660775@sss.pgh.pa.us> References: <20200317095839.GG2206@paquier.xyz> <32174.1584454158@sss.pgh.pa.us> <20200319175902.vjib2tslosw65iyr@alap3.anarazel.de> <5063.1584641224@sss.pgh.pa.us> <20200319230446.lxjybufdcgwaqkhj@alap3.anarazel.de> <5995.1584660775@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 19 Mar 2020 19:32:55 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <18537.1584666150.0@sss.pgh.pa.us> Date: Thu, 19 Mar 2020 21:03:17 -0400 Message-ID: <18567.1584666197@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <18537.1584666150.1@sss.pgh.pa.us> I wrote: > I think that at least some compilers will complain about side-effect-free > subexpressions of a comma expression. Could we restructure things so > that the errcode/errmsg/etc calls form a standalone comma expression > rather than appearing to be arguments of a varargs function? Yeah, the attached quick-hack patch seems to work really well for this. I find that this now works: ereport(ERROR, errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("foo")); where before it gave the weird error you quoted. Also, these both draw warnings about side-effect-free expressions: ereport(ERROR, ERRCODE_DIVISION_BY_ZERO, errmsg("foo")); ereport(ERROR, "%d", 42); With gcc you just need -Wall to get such warnings, and clang seems to give them by default. As a nice side benefit, the backend gets a couple KB smaller from removal of errfinish's useless dummy argument. I think that we could now also change all the helper functions (errmsg et al) to return void instead of a dummy value, but that would make the patch a lot longer and probably not move the goalposts much in terms of error detection. It also looks like we could use the same trick to get plain elog() to have the behavior of not evaluating its arguments when it's not going to print anything. I've not poked at that yet either. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="better-ereport-wip.patch"; charset="us-ascii" Content-ID: <18537.1584666150.2@sss.pgh.pa.us> Content-Description: better-ereport-wip.patch diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index 62eef7b..a29ccd9 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -438,7 +438,7 @@ matches_backtrace_functions(const char *funcname) * See elog.h for the error level definitions. */ void -errfinish(int dummy,...) +errfinish(void) { ErrorData *edata = &errordata[errordata_stack_depth]; int elevel; @@ -1463,7 +1463,7 @@ elog_finish(int elevel, const char *fmt,...) /* * And let errfinish() finish up. */ - errfinish(0); + errfinish(); } @@ -1749,7 +1749,7 @@ ThrowErrorData(ErrorData *edata) recursion_depth--; /* Process the error. */ - errfinish(0); + errfinish(); } /* @@ -1863,7 +1863,7 @@ pg_re_throw(void) */ error_context_stack = NULL; - errfinish(0); + errfinish(); } /* Doesn't return ... */ diff --git a/src/include/utils/elog.h b/src/include/utils/elog.h index 0a4ef02..e6fa85f 100644 --- a/src/include/utils/elog.h +++ b/src/include/utils/elog.h @@ -118,34 +118,34 @@ *---------- */ #ifdef HAVE__BUILTIN_CONSTANT_P -#define ereport_domain(elevel, domain, rest) \ +#define ereport_domain(elevel, domain, ...) \ do { \ pg_prevent_errno_in_scope(); \ if (errstart(elevel, __FILE__, __LINE__, PG_FUNCNAME_MACRO, domain)) \ - errfinish rest; \ + __VA_ARGS__, errfinish(); \ if (__builtin_constant_p(elevel) && (elevel) >= ERROR) \ pg_unreachable(); \ } while(0) #else /* !HAVE__BUILTIN_CONSTANT_P */ -#define ereport_domain(elevel, domain, rest) \ +#define ereport_domain(elevel, domain, ...) \ do { \ const int elevel_ = (elevel); \ pg_prevent_errno_in_scope(); \ if (errstart(elevel_, __FILE__, __LINE__, PG_FUNCNAME_MACRO, domain)) \ - errfinish rest; \ + __VA_ARGS__, errfinish(); \ if (elevel_ >= ERROR) \ pg_unreachable(); \ } while(0) #endif /* HAVE__BUILTIN_CONSTANT_P */ -#define ereport(elevel, rest) \ - ereport_domain(elevel, TEXTDOMAIN, rest) +#define ereport(elevel, ...) \ + ereport_domain(elevel, TEXTDOMAIN, __VA_ARGS__) #define TEXTDOMAIN NULL extern bool errstart(int elevel, const char *filename, int lineno, const char *funcname, const char *domain); -extern void errfinish(int dummy,...); +extern void errfinish(void); extern int errcode(int sqlerrcode); ------- =_aaaaaaaaaa0--