]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Modernize pg_bsd_indent's error/warning reporting code.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 15 Jun 2026 16:22:55 +0000 (12:22 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 15 Jun 2026 16:22:55 +0000 (12:22 -0400)
Late-model clang complains that these functions should be labeled
with "format(printf, 2, 3)", and it's right.  But let's go a bit
further and also make use of varargs, to remove duplication and
allow these functions to be used with non-integer input values.

Since no good deed goes unpunished, I had to also adjust a couple
of call sites.  They weren't wrong as-is, since the size_t-sized
arguments were coerced to int on the way into diag3().  But
without that, we have to adjust the format strings.

The point of this is to suppress compiler warnings, so back-patch
into branches containing pg_bsd_indent, even though there's no
functional change.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/1645041.1781283554@sss.pgh.pa.us
Backpatch-through: 16

src/tools/pg_bsd_indent/indent.c
src/tools/pg_bsd_indent/indent.h
src/tools/pg_bsd_indent/io.c

index 1a29409173b235dacad7cc331be35fbef02f657d..138a13063a7e7f0616db331f99ba46a0d094780b 100644 (file)
@@ -536,7 +536,7 @@ check_type:
        case lparen:            /* got a '(' or '[' */
            /* count parens to make Healy happy */
            if (++ps.p_l_follow == nitems(ps.paren_indents)) {
-               diag3(0, "Reached internal limit of %d unclosed parens",
+               diag3(0, "Reached internal limit of %zu unclosed parens",
                    nitems(ps.paren_indents));
                ps.p_l_follow--;
            }
@@ -809,7 +809,7 @@ check_type:
                                                 * declaration or an init */
                di_stack[ps.dec_nest] = dec_ind;
                if (++ps.dec_nest == nitems(di_stack)) {
-                   diag3(0, "Reached internal limit of %d struct levels",
+                   diag3(0, "Reached internal limit of %zu struct levels",
                        nitems(di_stack));
                    ps.dec_nest--;
                }
index e9e71d667d8c460f302fa737c5cfa8948d95a524..974ffe1ac272011531cd0344f1b3768fe7d50a23 100644 (file)
@@ -39,9 +39,7 @@ int   compute_label_target(void);
 int    count_spaces(int, char *);
 int    count_spaces_until(int, char *, char *);
 int    lexi(struct parser_state *);
-void   diag2(int, const char *);
-void   diag3(int, const char *, int);
-void   diag4(int, const char *, int, int);
+void   diag(int level, const char *msg, ...) pg_attribute_printf(2, 3);
 void   dump_line(void);
 int    lookahead(void);
 void   lookahead_reset(void);
@@ -51,3 +49,8 @@ void  pr_comment(void);
 void   set_defaults(void);
 void   set_option(char *);
 void   set_profile(const char *);
+
+/* backwards-compatibility macros */
+#define diag2(level, msg) diag(level, msg)
+#define diag3(level, msg, a) diag(level, msg, a)
+#define diag4(level, msg, a, b) diag(level, msg, a, b)
index 62d600bbb1167669644d3e88e4832ae8c9675bec..787b3ce6177aa31760d76d300fd9c8fad9e258de 100644 (file)
@@ -553,53 +553,22 @@ count_spaces(int cur, char *buffer)
 }
 
 void
-diag4(int level, const char *msg, int a, int b)
+diag(int level, const char *msg, ...)
 {
-    if (level)
-       found_err = 1;
-    if (output == stdout) {
-       fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
-       fprintf(stdout, msg, a, b);
-       fprintf(stdout, " */\n");
-    }
-    else {
-       fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
-       fprintf(stderr, msg, a, b);
-       fprintf(stderr, "\n");
-    }
-}
-
-void
-diag3(int level, const char *msg, int a)
-{
-    if (level)
-       found_err = 1;
-    if (output == stdout) {
-       fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
-       fprintf(stdout, msg, a);
-       fprintf(stdout, " */\n");
-    }
-    else {
-       fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
-       fprintf(stderr, msg, a);
-       fprintf(stderr, "\n");
-    }
-}
+    va_list ap;
 
-void
-diag2(int level, const char *msg)
-{
+    va_start(ap, msg);
     if (level)
        found_err = 1;
     if (output == stdout) {
        fprintf(stdout, "/**INDENT** %s@%d: ", level == 0 ? "Warning" : "Error", line_no);
-       fprintf(stdout, "%s", msg);
+       vfprintf(stdout, msg, ap);
        fprintf(stdout, " */\n");
     }
     else {
        fprintf(stderr, "%s@%d: ", level == 0 ? "Warning" : "Error", line_no);
-       fprintf(stderr, "%s", msg);
+       vfprintf(stderr, msg, ap);
        fprintf(stderr, "\n");
     }
+    va_end(ap);
 }
-