6

I just read this post about why new-line warnings exist, but to be honest my team has people working on several different platforms and with several different editors (everyone uses what bests suites them), so the warning has become ubiquitous, and since its not really a warning worth taking care of it's become noise and makes finding serious warnings a hassle.

Many times important warnings have gone unnoticed because, people got used to having a gazillion useless warnings pass by, so they obviously just stop looking at them carefully, and with reason IMHO. One could say in our case GCC is crying wolf too much for anyone to take it seriously anymore, which is a bad attitude but its just human nature.

Right now we compile with -Wall, because we want warnings, but is there a counter flag to avoid the new-line warnings?

Note: I Looked through the manual a bit but didn't find the answer in any place obvious so I gave up.

Note: In response to Robert Gamble's totally reasonable solution, our code is cross-platform and we have people and builds on Linux, Solaris and Windows, so the new-line... is not under consensus. And Somebody's compiler is always going to cry-wolf. Because there are over 40 developers, and other non programmer staff as well.

Community
  • 1
  • 1
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
  • As long as each file ends with some sort of newline (linefeed, CR+LF) gcc won't complain despite the platform it is running on. – Robert Gamble Nov 12 '08 at 03:11
  • This is why the `nano` editor shines: it automatically adds the newline at the end. Great for editing configuration files (a bit poor for writing code however). – Alexandre C. Aug 05 '11 at 12:52
  • Sounds like the real problem lies here: "people got used to having a gazillion useless warnings pass by". I compile with `-Werror` so that this doesn't happen. I'd rather spend a little extra time fixing code that's just fine already if it means that I can turn on a warning that might catch a real problem. Clean compiles are important so that all warnings are taken seriously. – David Stone Jun 23 '12 at 05:46

6 Answers6

6

Assuming you use some kind of source control system, you could add a pre-commit hook that ensures that text files end with a proper newline. Furthermore, depending on which source control system you use, you could add a pre-commit hook that actually fixes the line ending if it's not present.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • +1. Actually this is a good solution, and will probably end up doing this. Although its kind of a roundabout way to doing this :) – Robert Gould Nov 12 '08 at 03:29
  • Damn, I was just going to write this :-) We do something similar to remove tabs. – Enno Nov 13 '08 at 21:19
5

There isn't one as far as i know, i've used GCC for years.

Update: There should not be any warnings raised with C++11 standard. Related Q

Community
  • 1
  • 1
John T
  • 23,735
  • 11
  • 56
  • 82
4
-Wno-eof-newline

This was added with the fix for gcc bug 14331


Oddly enough I can't actually get gcc to output a warning for missing newlines. I guess newer versions have dispensed with this warning altogether.

I can get gcc to accept -Wno-eof-newline but it complains of unrecognized flags when I try -Weof-newline. C++11 removed the requirement for newlines at the end of files but for writing portable code in the old standards it really should be possible to enable such pedantic warnings.


Fortunately clang does still correctly support diagnostics about missing newlines: This warning can be enabled with -Wnewline-eof in all modes, or in C++11 and higher modes it can also be enabled with -Wc++98-compat-pedantic.

These warnings are off by default, but if you're taking advantage of clang's -Weverything flag to enable a 'subtractive' strategy for controlling warnings, then in C++11 and higher modes you need both -Wno-newline-eof and -Wno-c++98-compat-pedantic to disable the warning.

bames53
  • 86,085
  • 15
  • 179
  • 244
4

Why don't you just make sure your files have a terminating newline like they are supposed to? This should be a simple configuration change in the offending editors and seems like a pretty easy way to "silence" the warning.

Robert Gamble
  • 106,424
  • 25
  • 145
  • 137
  • Well one reason is the code is cross-platform and we have people and builds on Linux, Solaris, Windows and Macs, so the new-line... is not under consensus. And Somebody's compiler is always going to cry-wolf. – Robert Gould Nov 12 '08 at 03:03
  • gcc won't complain if the files end with just a linefeed when run on Windows or a carriage return + linefeed when run on Unix so this should work as long as each file ends with some kind of newline. – Robert Gamble Nov 12 '08 at 03:08
  • +1 for a good answer. But we use 3 compilers... not just GCC. The other two are nice and shut up about the warning, and 90% of the team uses those compilers, just 10% of us use GCC, so the error keeps coming back for us. Ah, it'd be really nice to have such a flag... – Robert Gould Nov 12 '08 at 03:17
  • Do the other developers understand that they are generating code that isn't Standards compliant and the potential issues that surround not having an ending newline? – Robert Gamble Nov 12 '08 at 03:36
  • You always could just grep -v 'warning: no newline at end of file' if you really wanted to. – Robert Gamble Nov 12 '08 at 03:40
  • Yes we grep our automated builds, but not our local compiles, guess that could be done. As for the fact that our colleagues code is non-standard, they know it, but since in reality every compiler on earth gracefully manages the problem (with warnings perhaps) the truth is there is no consequence – Robert Gould Nov 12 '08 at 03:49
  • Because I use third party libraries. – DougW Feb 22 '12 at 19:23
0

I'm 90% sure there is no arguemnt to turn this off.

The reason for the warning is that files without an endline give undefined behavior when compiled:

See standard: http://c0x.coding-guidelines.com/5.1.1.2.html

Here's a blog post with some python code (that I have not tried) which says it will fixup source files with this issue.

http://www.johndcook.com/blog/tag/gcc/

Chris Hamons
  • 1,510
  • 11
  • 22
  • In other compilers it can be turned off, I mean you can turn off all warnings if you wanted to, which is "unreasonable" of course, but possible. So there is plenty of argument for switching a useless and in real-life well defined behavior (compiles perfectly fine) warning. – Robert Gould Nov 12 '08 at 03:09
  • 1
    Ok, to be honest you are right that there is **no reason to** turn it off in the ideal programmer world, but our project suffers from some logistic problems that make a good case for turning it off. – Robert Gould Nov 12 '08 at 03:27
0

Add a hook to your source control that won't allow successful code check-in until the newline is added?

HUAGHAGUAH
  • 1,063
  • 6
  • 3
  • 2
    The idea to add a hook that fixes the files is better, than the idea to refute the commit. I for one, maybe rare in our profession, think human needs go before the needs of the machines and compilers. – Robert Gould Nov 12 '08 at 04:05