[cfe-commits] [LLVMdev] clang errors on void main()

Chandler Carruth chandlerc at google.com
Sat Feb 11 19:39:58 PST 2012


On Sat, Feb 11, 2012 at 5:31 AM, Vasiliy Korchagin <korchagin at ispras.ru>wrote:

> I agree, without setting implicit return zero bit changes in codegen are
> not necessary. New version of patch is attached.


The warning you're adding, as David suggested, should be under a separate
flag.

It should also be an extwarn as this is technically a language extension,
and it should be enabled by default (you may already have that, just want
it clarified).

--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -5795,8 +5795,13 @@ void Sema::CheckMain(FunctionDecl* FD, const
DeclSpec& DS) {
   const FunctionType* FT = T->getAs<FunctionType>();

   if (!Context.hasSameUnqualifiedType(FT->getResultType(), Context.IntTy))
{
-    Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint);
-    FD->setInvalidDecl(true);
+    if (getLangOptions().C99) {
+      // In C we allow main() to have non-integer return type.
+      Diag(FD->getTypeSpecStartLoc(), diag::warn_main_returns_nonint);

If you want this to be enabled in all C modes, you should accept more than
just C99: "!getLangOptions().CPlusPlus" is a good candidate here.

@@ -7204,7 +7209,8 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt
*Body,
     if (FD->isMain()) {
       // C and C++ allow for main to automagically return 0.
       // Implements C++ [basic.start.main]p5 and C99 5.1.2.2.3.
-      FD->setHasImplicitReturnZero(true);
+      if (!getLangOptions().C99)
+        FD->setHasImplicitReturnZero(true);

This isn't correct. You need to be checking for a non-int return type here,
not for C99. If the function has an int return type we want the implicit
return zero.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120211/a4507bb3/attachment.html>


More information about the cfe-commits mailing list