/* 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)
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
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;
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 *);
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)
{
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;
}
}
{
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;
}
}
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);
}
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))
if (ok)
{
fmt_fix_output (out);
- ok = fmt_check_width_compat (out, 0);
+ ok = fmt_check_width_compat (out, NULL, 0);
}
msg_enable ();
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