return void expressions in C (Was: [PATCH 3/6] Rework pretty_print_commit ...)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Sep 10, 2007 at 05:48:26PM +0000, Junio C Hamano wrote:
> Junio C Hamano <gitster@xxxxxxxxx> writes:
> 
> > Pierre Habouzit <madcoder@xxxxxxxxxx> writes:
> >
> >>   Actually this is, but it's so tasteless that I want to hide now.
> >>
> >>   if foo is a void foo(`whatever') it's correct to call:
> >>
> >>   return foo(...);
> >>
> >>   it's completely equivalent to:
> >>
> >>   foo(...);
> >>   return;
> >
> > News to me.
> 
> What I meant was that gcc disagrees with you and the resulting
> source does not build for me (I build with -Werror -Wall -ansi
> among other things).

  Okay, I stand corrected, but as I'm curious, I've now the Truth about
this, for those who still care at that point (as the patch has been
fixed in the next round anyways ;p)


  The standard (C99 here, but I would be surprised it changed a lot
since C89):

] 6.8.6.4 The return statement
]
] * Constraints
]
]   1 A return statement with an expression shall not appear in a function
]     whose return type is void. A return statement without an expression shall
]     only appear in a function whose return type is void.
]
] * Semantics
]
]   2 A return statement terminates execution of the current function
]     and returns control to its caller. A function may have any number
]     of return statements.
]
]   3 If a return statement with an expression is executed, the value of
]     the expression is returned to the caller as the value of the
]     function call expression. If the expression has a type different
]     from the return type of the function in which it appears, the
]     value is converted as if by assignment to an object having the
]     return type of the function.)


  So it seems that GCC does not care about (1) and only applies (2) and
(3). So for my own culture, I wrote the following C code, and the sole
way to make gcc unhappy about it is to add -pedantic to the command line
(which I don't use because it is way too verbose about non issues, or
things that are caught by other warnings). Even adding -std=c89 does not
helps !

  $ cat a.c
  #include <stdio.h>

  void foo(void) { puts("foo");  }
  void bar(void) { return foo(); }

  int main(void) {
      bar();
      return 0;
  }
  $ gcc -Werror -Wall -Wextra -ansi -o /dev/null a.c && echo $?
  0
  $ gcc -Werror -Wall -Wextra -pedantic -ansi -o /dev/null a.c
  cc1: warnings being treated as errors
  a.c: In function ‘bar’:
  a.c:8: warning: ‘return’ with a value, in function returning void

  I've searched in the GNU C manual, and it does not lists the return
[expr] in a void function to be an extension.  Though, I know why I was
confused, C++ does allows returns with an expression:

] 6.6.3 The return statement
]
] […]
]
] 3 A return statement with an expression of type “void” can be used
]   only in functions with a return type of “void”; the expression is
]   evaluated just before the function returns to its caller.


  So it seems gcc allows it for some kind of compability with C++, and
that I mixed up this because of the few C++ I did when I was young. Oh
boy, I knew this would made my soul unclean /o\

-- 
·O·  Pierre Habouzit
··O                                                madcoder@xxxxxxxxxx
OOO                                                http://www.madism.org

Attachment: pgpYttXwI0H4O.pgp
Description: PGP signature


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux