Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang: backslash and newline separated by space in single line comment

Tags:

c++

clang

Why this (live):

int main()
{
    // my pretty comment /\   
    //
    // 


}

gives the warning on clang:

warning: backslash and newline separated by space [-Wbackslash-newline-escape]

?

Looks like that code is perfectly valid without any pitfalls.

like image 228
grisha Avatar asked Oct 15 '25 14:10

grisha


1 Answers

From the C++14 draft [4296]:

2.2 Phases of translation [lex.phases]

  1. Each instance of a backslash character () immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. Except for splices reverted in a raw string literal, if a splice results in a character sequence that matches the syntax of a universal-character-name, the behavior is undefined. A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file.

Exactly the first sentence of it gives your warning.

In your example code:

int main()
{
    // my pretty comment /\
    int x;
    int y = x;    
}

the int x; line will be joined into a comment in the line above and will be removed in next phases of translation. So you got a error:

error: use of undeclared identifier 'x'

However

int main()
{
    // my pretty comment /\   
    //
}

is valid and will not cause a error. You may note that g++ (without -Wall) will not give a warning here. It looks like useless warning but I think compiler may treat it as possible fail of the new line character escaping. So, it is just better to give you warning here.

Do we need this warning there ?

Actually, no, we don't need it here. But it is just a diagnostic warning and it is better to have such warning to be able to detect possible problems than not to have it at all.

I think, here is also a reason such as:

  1. People don't usually escape spaces. Why? :)

  2. For multi-line comments here are /* and */ sequences.

like image 131
Victor Polevoy Avatar answered Oct 19 '25 13:10

Victor Polevoy