Index: gcc/doc/cppopts.texi =================================================================== --- gcc/doc/cppopts.texi (revision 123637) +++ gcc/doc/cppopts.texi (working copy) @@ -163,6 +163,13 @@ This usually happens in code of the form The second and third @code{FOO} should be in comments, but often are not in older programs. This warning is on by default. +@item -Weof-newline +@opindex Weof-newline +Warn whenever an input file does not end with a newline. This is technically +not conformant C, but does not cause problems in practice unless you were +hoping to form a single preprocessor token from the final characters of the +first file and the first characters of the last file! + @item -Werror @opindex Werror Make all warnings into hard errors. Source code which triggers warnings Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 123637) +++ gcc/doc/invoke.texi (working copy) @@ -229,7 +229,7 @@ Objective-C and Objective-C++ Dialects}. -Wchar-subscripts -Wclobbered -Wcomment @gol -Wconversion -Wcoverage-mismatch -Wno-deprecated-declarations @gol -Wdisabled-optimization -Wno-div-by-zero @gol --Wempty-body -Wno-endif-labels @gol +-Wempty-body -Wno-endif-labels -Wno-eof-newline @gol -Werror -Werror=* @gol -Wfatal-errors -Wfloat-equal -Wformat -Wformat=2 @gol -Wno-format-extra-args -Wformat-nonliteral @gol @@ -3322,6 +3322,11 @@ Warn if an undefined identifier is evalu @opindex Wendif-labels Do not warn whenever an @samp{#else} or an @samp{#endif} are followed by text. +@item -Wno-eof-newline +@opindex Wno-eof-newline +@opindex Weof-newline +Do not warn when an an input file fails to end with a newline. + @item -Wshadow @opindex Wshadow Warn whenever a local variable shadows another local variable, parameter or Index: gcc/testsuite/gcc.dg/cpp/Wno-eof-newline.c =================================================================== --- gcc/testsuite/gcc.dg/cpp/Wno-eof-newline.c (revision 0) +++ gcc/testsuite/gcc.dg/cpp/Wno-eof-newline.c (revision 0) @@ -0,0 +1,10 @@ +/* Copyright (C) 2007 Free Software Foundation, Inc. */ + +/* { dg-do preprocess } */ +/* { dg-options "-Wno-eof-newline" } */ + +/* Tests new -W(no-)eof-newline option. */ + +#include "Wno-eof-newline.h" /* { dg-bogus "no newline at end of file" } */ + +/* { dg-bogus "no newline at end of file" } */ \ No newline at end of file Index: gcc/testsuite/gcc.dg/cpp/Wno-eof-newline.h =================================================================== --- gcc/testsuite/gcc.dg/cpp/Wno-eof-newline.h (revision 0) +++ gcc/testsuite/gcc.dg/cpp/Wno-eof-newline.h (revision 0) @@ -0,0 +1 @@ +/* Non-empty but not newline-terminated. */ /* { dg-bogus "no newline at end of file" } */ \ No newline at end of file Index: gcc/testsuite/gcc.dg/cpp/Wno-eof-newline-2.c =================================================================== --- gcc/testsuite/gcc.dg/cpp/Wno-eof-newline-2.c (revision 0) +++ gcc/testsuite/gcc.dg/cpp/Wno-eof-newline-2.c (revision 0) @@ -0,0 +1,10 @@ +/* Copyright (C) 2007 Free Software Foundation, Inc. */ + +/* { dg-do preprocess } */ +/* { dg-options "-pedantic -Wno-eof-newline" } */ + +/* Tests new -W(no-)eof-newline option. */ + +#include "Wno-eof-newline.h" /* { dg-bogus "no newline at end of file" } */ + +/* { dg-bogus "no newline at end of file" } */ \ No newline at end of file Index: gcc/testsuite/gcc.dg/cpp/Weof-newline-2.c =================================================================== --- gcc/testsuite/gcc.dg/cpp/Weof-newline-2.c (revision 0) +++ gcc/testsuite/gcc.dg/cpp/Weof-newline-2.c (revision 0) @@ -0,0 +1,10 @@ +/* Copyright (C) 2007 Free Software Foundation, Inc. */ + +/* { dg-do preprocess } */ +/* { dg-options "-Wno-eof-newline -pedantic" } */ + +/* Tests new -W(no-)eof-newline option. */ + +#include "Weof-newline.h" /* { dg-warning "" "In file included from" } { dg-warning 1 "no newline at end of file" } { dg-bogus 8 "no newline at end of file" } */ + +/* { dg-warning "no newline at end of file" } */ \ No newline at end of file Index: gcc/testsuite/gcc.dg/cpp/Weof-newline.c =================================================================== --- gcc/testsuite/gcc.dg/cpp/Weof-newline.c (revision 0) +++ gcc/testsuite/gcc.dg/cpp/Weof-newline.c (revision 0) @@ -0,0 +1,10 @@ +/* Copyright (C) 2007 Free Software Foundation, Inc. */ + +/* { dg-do preprocess } */ +/* { dg-options "-Weof-newline" } */ + +/* Tests new -W(no-)eof-newline option. */ + +#include "Weof-newline.h" /* { dg-warning "" "In file included from" } { dg-warning 1 "no newline at end of file" } { dg-bogus 8 "no newline at end of file" } */ + +/* { dg-warning "no newline at end of file" } */ \ No newline at end of file Index: gcc/testsuite/gcc.dg/cpp/Weof-newline.h =================================================================== --- gcc/testsuite/gcc.dg/cpp/Weof-newline.h (revision 0) +++ gcc/testsuite/gcc.dg/cpp/Weof-newline.h (revision 0) @@ -0,0 +1 @@ +/* Non-empty but not newline-terminated. */ /* { dg-warning "no newline at end of file" } */ \ No newline at end of file Index: gcc/c.opt =================================================================== --- gcc/c.opt (revision 123637) +++ gcc/c.opt (working copy) @@ -192,6 +192,10 @@ Wendif-labels C ObjC C++ ObjC++ Warning Warn about stray tokens after #elif and #endif +Weof-newline +C ObjC C++ ObjC++ Warning Init(1) +Warn if an input file does not end with a newline + Werror C ObjC C++ ObjC++ ; Documented in common.opt Index: gcc/c-opts.c =================================================================== --- gcc/c-opts.c (revision 123637) +++ gcc/c-opts.c (working copy) @@ -451,6 +451,10 @@ c_common_handle_option (size_t scode, co cpp_opts->warn_endif_labels = value; break; + case OPT_Weof_newline: + cpp_opts->warn_eof_newline = value; + break; + case OPT_Werror: cpp_opts->warnings_are_errors = value; global_dc->warning_as_error_requested = value; @@ -899,13 +903,14 @@ c_common_handle_option (size_t scode, co /* We need to handle the -pedantic switches here, rather than in c_common_post_options, so that a subsequent -Wno-endif-labels - is not overridden. */ + or -Wno-eof-newline is not overridden. */ case OPT_pedantic_errors: cpp_opts->pedantic_errors = 1; /* Fall through. */ case OPT_pedantic: cpp_opts->pedantic = 1; cpp_opts->warn_endif_labels = 1; + cpp_opts->warn_eof_newline = 1; if (warn_pointer_sign == -1) warn_pointer_sign = 1; if (warn_overlength_strings == -1) Index: libcpp/include/cpplib.h =================================================================== --- libcpp/include/cpplib.h (revision 123637) +++ libcpp/include/cpplib.h (working copy) @@ -333,6 +333,9 @@ struct cpp_options /* Nonzero means warn about text after an #endif (or #else). */ unsigned char warn_endif_labels; + /* Nonzero means warn if an input file does not end with a newline. */ + unsigned char warn_eof_newline; + /* Nonzero means warn about implicit sign changes owing to integer promotions. */ unsigned char warn_num_sign_change; Index: libcpp/init.c =================================================================== --- libcpp/init.c (revision 123637) +++ libcpp/init.c (working copy) @@ -156,6 +156,7 @@ cpp_create_reader (enum c_lang lang, has CPP_OPTION (pfile, operator_names) = 1; CPP_OPTION (pfile, warn_trigraphs) = 2; CPP_OPTION (pfile, warn_endif_labels) = 1; + CPP_OPTION (pfile, warn_eof_newline) = 1; CPP_OPTION (pfile, warn_deprecated) = 1; CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99); CPP_OPTION (pfile, dollars_in_ident) = 1; Index: libcpp/lex.c =================================================================== --- libcpp/lex.c (revision 123637) +++ libcpp/lex.c (working copy) @@ -850,7 +850,8 @@ _cpp_get_fresh_line (cpp_reader *pfile) /* End of buffer. Non-empty files should end in a newline. */ if (buffer->buf != buffer->rlimit && buffer->next_line > buffer->rlimit - && !buffer->from_stage3) + && !buffer->from_stage3 + && CPP_OPTION (pfile, warn_eof_newline)) { /* Only warn once. */ buffer->next_line = buffer->rlimit;