Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

end of line (GNU documentation)

Tags:

c

gcc

I'm reading documentation about gcc preprocessing, I read the following sentence (here):

If the last line of any input file lacks an end-of-line marker, the end of the file is considered to implicitly supply one. The C standard says that this condition provokes undefined behavior, so GCC will emit a warning message.

I try to produce the warning by doing :

> echo -n "int main(void) {return 0;}" > test.c
> gcc -Wall -Wextra -Werror test.c

But no prob, it compiles. I understand end-of-line marker as new-line char, but it seems to be anything else.

How could I produce the warning ?

like image 598
Gaut Avatar asked Dec 19 '15 17:12

Gaut


2 Answers

It seems that it got removed from gcc.

See this: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14331#c19

2007-05-31

    PR preprocessor/14331
    * lex.c (_cpp_get_fresh_line):  Don't warn if no newline at EOF.
like image 98
nnn Avatar answered Oct 02 '22 06:10

nnn


C11(N1570), §5.1.1.2, states:

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.

So not having a newline at the of the file violates the above "shall" constraint.

clang does warn about it:

$ printf "int main(void) {return 0;}" > test.c
$ clang -Weverything test.c
test.c:1:27: warning: no newline at end of file [-Wnewline-eof]
int main(void) {return 0;}
                          ^
1 warning generated.

But a constraint violation is undefined behaviour.

C11(N1570), §4, states:

If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint or runtime constraint is violated, the behavior is undefined. Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ or by the omission of any explicit definition of behavior. There is no difference in emphasis among these three; they all describe ‘‘behavior that is undefined’’.

So gcc took the liberty to take out the warnings (as noted by @nnn) as gcc is not required to issue any diagnostic for undefined behaviour.

like image 26
P.P Avatar answered Oct 02 '22 05:10

P.P