From: Ben Pfaff Date: Sat, 10 Sep 2022 18:12:33 +0000 (-0700) Subject: format: Cite variable names in messages reporting incompatible width. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dc26037479e9887a84375ad6aeb657e45a8a1933;p=pspp format: Cite variable names in messages reporting incompatible width. --- diff --git a/src/data/format.c b/src/data/format.c index 17afda6162..8c1c467398 100644 --- a/src/data/format.c +++ b/src/data/format.c @@ -518,9 +518,11 @@ fmt_check_type_compat (const struct fmt_spec *format, enum val_type var_type) /* Checks that FORMAT is appropriate for a variable of the given WIDTH and returns NULL if so. Otherwise returns a malloc()'d error message that the - calelr must eventually free(). */ + caller must eventually free(). VARNAME is optional and only used in the + error message. */ char * -fmt_check_width_compat__ (const struct fmt_spec *format, int width) +fmt_check_width_compat__ (const struct fmt_spec *format, const char *varname, + int width) { char *error = fmt_check_type_compat__ (format, val_type_from_width (width)); if (error) @@ -528,22 +530,31 @@ fmt_check_width_compat__ (const struct fmt_spec *format, int width) if (fmt_var_width (format) != width) { - char str[FMT_STRING_LEN_MAX + 1]; - return xasprintf (_("String variable with width %d is not compatible " - "with format %s."), - width, fmt_to_string (format, str)); + char format_str[FMT_STRING_LEN_MAX + 1]; + fmt_to_string (format, format_str); + + if (varname) + return xasprintf (_("String variable %s with width %d is not " + "compatible with format %s."), + varname, width, format_str); + else + return xasprintf (_("String variable with width %d is not compatible " + "with format %s."), + width, format_str); } return NULL; } -/* Checks that FORMAT is appropriate for a variable of the given - WIDTH and returns true if so. Otherwise returns false and - emits an error message. */ +/* Checks that FORMAT is appropriate for a variable of the given WIDTH and + returns true if so. Otherwise returns false and emits an error message. + VARNAME is optional and only used in the error message. */ bool -fmt_check_width_compat (const struct fmt_spec *format, int width) +fmt_check_width_compat (const struct fmt_spec *format, const char *varname, + int width) { - return fmt_emit_and_free_error (fmt_check_width_compat__ (format, width)); + return fmt_emit_and_free_error (fmt_check_width_compat__ (format, varname, + width)); } /* Returns the width corresponding to FORMAT. The return value @@ -1020,7 +1031,7 @@ fmt_from_u32 (uint32_t u32, int width, bool loose, struct fmt_spec *f) ok = fmt_check_output (f); } if (ok) - ok = fmt_check_width_compat (f, width); + ok = fmt_check_width_compat (f, NULL, width); msg_enable (); return ok; diff --git a/src/data/format.h b/src/data/format.h index 8c54ba3a17..5fe6c101f8 100644 --- a/src/data/format.h +++ b/src/data/format.h @@ -95,11 +95,13 @@ bool fmt_check (const struct fmt_spec *, enum fmt_use); bool fmt_check_input (const struct fmt_spec *); bool fmt_check_output (const struct fmt_spec *); bool fmt_check_type_compat (const struct fmt_spec *, enum val_type); -bool fmt_check_width_compat (const struct fmt_spec *, int var_width); +bool fmt_check_width_compat (const struct fmt_spec *, const char *varname, + int var_width); char *fmt_check__ (const struct fmt_spec *, enum fmt_use); char *fmt_check_type_compat__ (const struct fmt_spec *, enum val_type); -char *fmt_check_width_compat__ (const struct fmt_spec *, int var_width); +char *fmt_check_width_compat__ (const struct fmt_spec *, const char *varname, + int var_width); /* Working with formats. */ int fmt_var_width (const struct fmt_spec *); diff --git a/src/data/por-file-reader.c b/src/data/por-file-reader.c index 7f32d2cf8f..9ae6799ac4 100644 --- a/src/data/por-file-reader.c +++ b/src/data/por-file-reader.c @@ -640,7 +640,8 @@ convert_format (struct pfm_reader *r, const int portable_format[3], msg_disable (); ok = (fmt_check_output (&format) - && fmt_check_width_compat (&format, var_get_width (v))); + && fmt_check_width_compat (&format, var_get_name (v), + var_get_width (v))); msg_enable (); if (!ok) diff --git a/src/data/variable.c b/src/data/variable.c index 87e7d07823..3c6bea2717 100644 --- a/src/data/variable.c +++ b/src/data/variable.c @@ -634,7 +634,7 @@ var_set_print_format_quiet (struct variable *v, const struct fmt_spec *print) { if (!fmt_equal (&v->print, print)) { - assert (fmt_check_width_compat (print, v->width)); + assert (fmt_check_width_compat (print, v->name, v->width)); v->print = *print; } } @@ -667,7 +667,7 @@ var_set_write_format_quiet (struct variable *v, const struct fmt_spec *write) { if (!fmt_equal (&v->write, write)) { - assert (fmt_check_width_compat (write, v->width)); + assert (fmt_check_width_compat (write, v->name, v->width)); v->write = *write; } } diff --git a/src/language/data-io/print.c b/src/language/data-io/print.c index 3b5f901b53..906730dce3 100644 --- a/src/language/data-io/print.c +++ b/src/language/data-io/print.c @@ -398,7 +398,8 @@ parse_variable_argument (struct lexer *lexer, const struct dictionary *dict, struct prt_out_spec *spec; var = vars[var_idx++]; - if (!fmt_check_width_compat (f, var_get_width (var))) + if (!fmt_check_width_compat (f, var_get_name (var), + var_get_width (var))) return false; spec = pool_alloc (trns->pool, sizeof *spec); diff --git a/src/language/dictionary/formats.c b/src/language/dictionary/formats.c index 817cdbd6d9..b2cc3a5c39 100644 --- a/src/language/dictionary/formats.c +++ b/src/language/dictionary/formats.c @@ -89,7 +89,7 @@ internal_cmd_formats (struct lexer *lexer, struct dataset *ds, int which) } if (!parse_format_specifier (lexer, &f) || !fmt_check_output (&f) - || !fmt_check_width_compat (&f, width)) + || !fmt_check_width_compat (&f, var_get_name (v[0]), width)) goto fail; if (!lex_match (lexer, T_RPAREN)) diff --git a/src/output/spv/spv.c b/src/output/spv/spv.c index 4547e96794..04aa610645 100644 --- a/src/output/spv/spv.c +++ b/src/output/spv/spv.c @@ -888,7 +888,7 @@ spv_decode_fmt_spec (uint32_t u32, struct fmt_spec *out) if (ok) { fmt_fix_output (out); - ok = fmt_check_width_compat (out, 0); + ok = fmt_check_width_compat (out, NULL, 0); } msg_enable (); diff --git a/tests/language/dictionary/formats.at b/tests/language/dictionary/formats.at index 98019ae7e5..93e135b106 100644 --- a/tests/language/dictionary/formats.at +++ b/tests/language/dictionary/formats.at @@ -90,14 +90,14 @@ z,A3 3 | FORMATS a y (F4). | ^" -formats.sps:4: error: FORMATS: String variable with width 1 is not compatible with format A2. +formats.sps:4: error: FORMATS: String variable x with width 1 is not compatible with format A2. -formats.sps:5: error: FORMATS: String variable with width 2 is not compatible with format AHEX2. +formats.sps:5: error: FORMATS: String variable y with width 2 is not compatible with format AHEX2. "formats.sps:6.11: error: FORMATS: x and y are string variables with different widths. All variables in this variable list must have the same width. y will be omitted from the list. 6 | FORMATS x y (A2). | ^" -formats.sps:6: error: FORMATS: String variable with width 1 is not compatible with format A2. +formats.sps:6: error: FORMATS: String variable x with width 1 is not compatible with format A2. ]) AT_CLEANUP