Go Down

Topic: Compilers goes crazy when you have "\\" inside a comment (Read 2084 times) previous topic - next topic

manuelbenyacar

There's a problem when I put two \\ (not comment slashes, the opposite ones...) at the end of a comment.

It seems that compiler "reads" a multi line comment or something like that, and report errors that disappear if you delete them.

I think it's a compiler bug.

el_supremo

I think inside a comment the backslash doesn't have its special meaning as the escape character except the last one on a line. The first backslash is therefore ignored - i.e. it isn't read as an escape for the following backslash. The second backslash then gets read as the line continuation. The compiler issues a warning:
Code: [Select]
sketch_sep16a.cpp:8:1: warning: multi-line comment

Pete

eried

#2
Sep 17, 2012, 06:14 am Last Edit: Sep 17, 2012, 08:48 am by eried Reason: 1

There's a problem when I put two \\ (not comment slashes, the opposite ones...) at the end of a comment.

It seems that compiler "reads" a multi line comment or something like that, and report errors that disappear if you delete them.

I think it's a compiler bug.


http://arduino.cc/forum/index.php/topic,118440.0.html can you test the updated compiler? I don't get any error at all.
My website: http://ried.cl

westfw

Quote
The second backslash then gets read as the line continuation.

This sounds right.  And if there is "important stuff" on the next line, it will become part of the comment instead of being compiled properly, possibly leading to a cascade of errors.  This would be especially bad:
Code: [Select]
//  bad comment \\
#include <Ethernet.h>
#include <other.h>

Chagrin

Like westfw said:

Code: [Select]
root@zygot:/tmp# cat helloworld.c
#include <stdio.h>

// Comment \\
int main() {
  printf("Hello World!\n");
  return(0);
}
root@zygot:/tmp# gcc -Wall helloworld.c
helloworld.c:3:1: warning: multi-line comment
helloworld.c:5: error: expected declaration specifiers or '...' before string constant
helloworld.c:5: warning: data definition has no type or storage class
helloworld.c:5: warning: type defaults to 'int' in declaration of 'printf'
helloworld.c:5: error: conflicting types for 'printf'
helloworld.c:5: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
helloworld.c:6: error: expected identifier or '(' before 'return'
helloworld.c:7: error: expected identifier or '(' before '}' token

Tom Carpenter

It isn't particularly the "\\", but rather just the single \ at the end of the comment. It isn't a bug with the compiler at all, in fact it is part of the c language specification to allow you to wrap comments onto the next line.

It works with #define statements and comments:

Code: [Select]
#define hello\
this is still\
part of the\
define above
This line is not

//This is a multi\
line comment\
which can also\
be written as:

/*
This is a multi
line comment
*/
~Tom~

westfw

Quote
to allow you to wrap comments onto the next line.

Wrapping comments is useless;  you might as well have used /* */ or another comment line.
the backslash continuation feature was for code (where it's still not very useful, since newlines are largely irrelevant in C) and strings (where it's ugly and error prone, and string catenation should be replacing it.)

manuelbenyacar

Ooooppss...
You're write..!

Excuse me... I have 20 years of software development... but in Turbo Pascal and in the last years in Visual Basic!
I didn't know the C++ comment continuation...

Thanks everyone..|

MichaelMeissner

As others have noted, this is part of the C and C++ language standards.  In the C99 standard, it is covered in section 5.1.1.2 (translation phases):

Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. If, as a result, a character sequence that matches the syntax of a universal character name is produced, the behavior is undefined. Only the last backslash on any physical source line shall be eligible for being part of such a splice. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.


Go Up