This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
A small bug with operator% related error messages.
- To: egcs-patches at cygnus dot com
- Subject: A small bug with operator% related error messages.
- From: Theodore Papadopoulo <Theodore dot Papadopoulo at sophia dot inria dot fr>
- Date: Thu, 03 Sep 1998 14:25:39 +0200
- cc: Theodore dot Papadopoulo at sophia dot inria dot fr
In the following program, the error message generated is buggy.
> cat Bug.C
class A {
public:
A() { }
};
int
main()
{
const A a;
return a%a;
}
> g++ Bug.C
Bug.C: In function `int main()':
Bug.C:10: no match for `const A & ¬onst A &'
The reason is that the string in opname_tab (in gcc/cp/lex.c)
used to generate are directly copied into the buf variable of function
cp_thing (in gcc/cp/errfn.c). This variable is then used as a format to
actually output the message. Consequently, in the case above the buffer
contains the string "no match for `const A & % const A &'" which is
misprinted by printf like function, which considers the `%' character to
be part of a `%c' directive (I do not understand why it is so as the printf
manuals I have access to do not describe the ` ' as being a valid modifier
for the %(something) directives).
I can see 3 possible fixes:
1) In all opname_tab[] (and presumably in all assignop_tab[]),
change `%' into "%%".
2) Create a `formatize_string' function which purpose is to take a string
and create the format that will produce the string and call it in
cp_thing at the proper place.
3) Change cp_thing to print buf not as a format but as a string.
i.e replace something like printf(buffer) into printf("%s",buffer).
This requires also some small change in the handling of "%%" in
cp_thing.
To me 1) seems risky as I'm not totally sure that opname_tab are used
only to generate the names of the functions and in any case, I do not
consider this as a very robust fix, 2) sounds very clumsy IMHO...
The small patch below implements the third strategy which I believe
to be the best.
Any comments? Am I missing something ?
Theo.
1998-09-03 Theodore Papadopoulo <Theodore.Papadopoulo@sophia.inria.fr>
* errfn.c (cp_thing): Print buf as a string not as a printf format
to avoid problems with the operator%. Consequently, `%%' sequences
in format are copied as `%' in buf.
Index: cp/errfn.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/errfn.c,v
retrieving revision 1.14
diff -c -3 -p -r1.14 errfn.c
*** errfn.c 1998/08/26 08:11:46 1.14
--- errfn.c 1998/09/03 12:18:21
*************** cp_thing (errfn, atarg1, format, ap)
*** 149,166 ****
}
else if (*f == '%')
{
! /* A `%%' has occurred in the input string. Since the
! string we produce here will be passed to vprintf we must
! preserve both `%' characters. */
! len += 2;
! if (len > buflen)
{
buflen = len;
buf = xrealloc (buf, len);
}
! strcpy (buf + offset, "%%");
! offset += 2;
}
else
{
--- 149,163 ----
}
else if (*f == '%')
{
! /* A `%%' has occurred in the input string. Replace it with
! a `%' in the formatted message buf. */
! if (++len > buflen)
{
buflen = len;
buf = xrealloc (buf, len);
}
! buf[offset++] = '%';
}
else
{
*************** cp_thing (errfn, atarg1, format, ap)
*** 190,199 ****
{
char *file = cp_file_of (atarg);
int line = cp_line_of (atarg);
! (*errfn) (file, line, buf);
}
else
! (*errfn) (buf);
}
--- 187,196 ----
{
char *file = cp_file_of (atarg);
int line = cp_line_of (atarg);
! (*errfn) (file, line, "%s", buf);
}
else
! (*errfn) ("%s", buf);
}
--------------------------------------------------------------------
Theodore Papadopoulo
Email: Theodore.Papadopoulo@sophia.inria.fr Tel: (33) 04 92 38 76 01
--------------------------------------------------------------------