GNOME Bugzilla – Bug 339794
comments syntax-highlighting problem with c-code
Last modified: 2007-08-20 12:16:46 UTC
That bug has been described on https://launchpad.net/distros/ubuntu/+source/gedit/+bug/41345 "following c-code will be highlighted wrong: if(key == 0xdf) key = 0x5c; //sz -> \\ if(key == '+') key = '~'; the second line is also a comment due the backslash at the end, but is not highlighted as such. because i assume gedit thinks the backslash has been escaped, but gcc does not care and assumes the next line to be a comment as well. for example vim handles this correctly."
This small code snippet can reproduce the bug : #include <stdio.h> int main (int argv, char *argc) { char *test; test = "foo\n"; // \\ test = "bar\n"; printf(test); } [sf:antea] tmp $ gcc test-bug.c [sf:antea] tmp $ ./a.out foo Anyway, it should be tested with other compilers than gcc before confirming, since it could as well be a gcc bug. (this is a corner case and it seems weird to me that the C standard envisages such a behaviour)
This is not a bug, it is a consequence of a GCC convenience add-on. //-style comments are part of C++, not ANSI C. GCC supports using them, however, and thus compiles the testcases. Adding -pedantic -ansi compiler flags to enforce the standard will cause this testcode to fail. Additionally, an escaping backslash is not supposed to have anything follow it except a newline. Just change test = "foo\n"; // \\ test = "bar\n"; to test = "foo\n"; // \ test = "bar\n"; to highlight it the way GCC is seeing it. If you want proof that escaping backslashes aren't supposed to have anything follow them, try to compile this: #define FOOBARBAZ "foo" \ /* this comment will error */ "bar" \ "baz" int main() { printf(FOOBARBAZ); return 0; } Remove the /* */ comment and try it again. Works all of a sudden, eh? Add some whitespace after one of the escaped slashes and see if it gives you a warning like, "warning: backslash and newline separated by space."
> //-style comments are part of C++, not ANSI C. Actually it is part of C now. Ever heard of C99 ?
fixed in the new highlighing engine