An empty line at the of every source file in C++



  • Hi all,

    We recently started being a bit more multi-platform at our company and all of a sudden we now need to write code that compiles under GCC as well as MSVC. This has led to a large page of full of "Things To Remember" containing various things that MSVC will be quite happy with but which GCC gets upset about. One of these is that apparently there's a standard somewhere that states that all .cpp and .h files need to end with an empty line and GCC will start moaning if you forget it. Now, we're all still using MSVC as our main IDE and Compiler and I'm pretty sure this is one rule that will be forgotten repeatedly by alot of people. I was just wondering if anyone knew of a way to convince MSVC (Microsoft Visual C++ 2005 version 8 to be precise) to automatically make sure there's a new line at the end of all the C++ source files that are edited in it?

    Any other hints and tricks that people know of to help people make the leap from writing MSVC only code to truly portable C++ code would also be appreciated,

    Thanks in advance 🙂



  • Not an empty line. A newline at the end of the last line.

    This is correct:

    0000000   i   n   t  sp   x   ;  nl   i   n   t  sp   y   ;  nl

    This is nuts:

    0000000   i   n   t  sp   x   ;  nl   i   n   t  sp   y   ;

    If we were to try concatenating another line onto the end of that file:

    $ echo 'int z;' >> bar.c
    $ cat bar.c
    int x;
    int y;int z;

    Which is obviously silly.

    GCC will quite happily compile files without a newline on their last line, but it throws a warning to let you know that you screwed up. There is no portability issue here.

    Most editors are capable of warning you about this, or even automatically fixing up the newlines for you. 



  • Sounds like an ideal task for a version control system script. You are using one, aren't you?



  • @asuffield said:

    This is nuts:

    0000000   i   n   t  sp   x   ;  nl   i   n   t  sp   y   ;

    If we were to try concatenating another line onto the end of that file:

    $ echo 'int z;' >> bar.c
    $ cat bar.c
    int x;
    int y;int z;

    Which is obviously silly.

     

    Silly, but not wrong. A more dangerous example would be

    0000000  #  d  e  f  i  n  e  sp  x  sp  y



  • Ah okay, that clears things up. I suppose there's some advantage to be had by not simply letting the pre-processor do it automatically? I'd argue that anybody who thought it was a good idea to write code by building pre-processor commands from combined include files needs to be stopped by any and all tools that have the oppurtunity 🙂

     Anyways, I'll go digging through the IDE options tomorrow, maybe there's an option buried in there that'll sort it out for me. 



  • @ulzha said:

    Sounds like an ideal task for a version control system script. You are using one, aren't you?
     

     

    That's a good idea, we're using SVN at the moment. I've never had to do anything more complex with it than using branches though. I guess it might be possible to integrate some sort of Batch file into it somewhere that could catch .h andd .cpp files as they came in and made sure there was a newline on the end. Or am I on the wrong track here?



  • @Devi said:

    Ah okay, that clears things up. I suppose there's some advantage to be had by not simply letting the pre-processor do it automatically?

     

    No, people just never expect text files with missing newlines to show up, so a lot of software doesn't account for the possibility. It's not like you would ever want such a thing.



  • just ad a code style checker to your makefile so that it spits out warnings when they compile. 



  • @tster said:

    just ad a code style checker to your makefile so that it spits out warnings when they compile. 

     

    Well, GCC is already quite capable of generating the warnings for us. I'm hoping for a more automated solution since imo this kind of code style guideline is something that should be handled automatically somewhere rather than needing human intervention to fix...



  • I'm not sure what you mean by "handled automatically."  When visual studio generates new files it puts newlines at the bottom of every file.  Then you go and remove it, and you want visual studio to add it back?  I've never been a big fan of things that keep adding something that I keep taking away.  Not there are code formatters and stuff that you can use to run over your source code to fix all the newlines if you want.



  • For example, if you were using xemacs then the solution could be:

    (custom-set-variables '(require-final-newline (quote ask)))

    Or at least that's what I use. Then if I ever do something silly like trying to save a file without a trailing newline, it asks whether I really wanted to do that, and one extra keystroke adds the missing newline.

    Either your editor has a similar option or you need to get yourself a better editor. 



  • @tster said:

    I'm not sure what you mean by "handled
    automatically."  When visual studio generates new files it puts
    newlines at the bottom of every file.  Then you go and remove it, and
    you want visual studio to add it back?  I've never been a big fan of
    things that keep adding something that I keep taking away.  Not there
    are code formatters and stuff that you can use to run over your source
    code to fix all the newlines if you want.

     

    I just mean that I want to have an automated system somewhere which silently ensures all files with a .h or .cpp extension end in a new line before they get commited into source control. I can see your point about wanting control over whether the new line is there, but I can't imagine any reason why anyone in my office would deliberately omit it. If nothing else, requiring everyone to remember the presence of an invisible symbol at the end of every file doesn't seem very user friendly...

    Are there any code sweepers you'd recommend? We all tend to bit a bit carried away with macros around here, so most C++ analysis tools tend to throw a fit around half way through each file... There's nothing like Visual Assist suddenly deciding to try and auto-complete every word you type in a comment block to brighten your day 🙂


    @asuffield said:

    For example, if you were using xemacs then the solution could be:

    (custom-set-variables '(require-final-newline (quote ask)))

    Or
    at least that's what I use. Then if I ever do something silly like
    trying to save a file without a trailing newline, it asks whether I
    really wanted to do that, and one extra keystroke adds the missing
    newline.

    Either your editor has a similar option or you need to get yourself a better editor. 

     

    Well, I couldn't find anything in MSVC, but a colleague said he'd found a way of modifying SVN to do it so I guess all's well that ends well.

     

    Thanks for the help guys! 

     



  • MSVC still allows you to use VB macros in the IDE, doesn't it?  Just add one to trap the save event, check for a newline and add one if it's not there.  Make it mandatory for all developers, and you should be in business. 


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.