On 11.02.2012 02:31, John McCall wrote:
On Feb 10, 2012, at 1:27 PM, Vasiliy Korchagin wrote:
On 11.02.2012 00:16, David Blaikie wrote:
2012/2/10 Vasiliy Korchagin<[email protected]>:
On 10.02.2012 19:45, David Blaikie wrote:
2012/2/9 Vasiliy Korchagin<[email protected]>:
On 08.02.2012 23:47, David Blaikie wrote:

On Wed, Feb 8, 2012 at 11:25 AM, Chris Lattner<[email protected]>
  wrote:

On Feb 8, 2012, at 5:31 AM, Vasiliy Korchagin wrote:

07.02.2012 07:27, Eli Friedman пишет:

On Mon, Feb 6, 2012 at 6:51 PM, Xin Tong<[email protected]>
  wrote:

Is there any way to stop this ?

/home/socrates/llvm/llvm-3.0.src/benchmarks/powerstone/crc/crc.c:67:1:
error: 'main' must return 'int'
void main()
^
1 error generated.

You mean besides fixing the source of your benchmark so it's valid C?
Not at the moment... patches welcome.

-Eli

We suggest patch for allowing main() function to have non-integer return
type. This feature can be enabled with "-allow-non-int-main" option. In
this
case warning about incorrect main() return type will be printed instead
of
error.

In patch also included test case for this feature.

Vasiliy Korchagin,

Hi Vasiliy,

Please send clang patches to cfe-dev.

or even cfe-commits (further instructions are here:
http://clang.llvm.org/get_involved.html)

[I've dropped llvm-dev and added cfe-commits to this email]

My first thought based on your description alone would be that we
should support this, if at all, probably in the way that GCC already
does - surfacing non-int returning main as a warning in C (under
-Wmain) and error in C++ (as it is already) if that's practical.

&     looking at the patch itself: Your change is even more permissive
than GCC (when you use the flag you've added) allowing C++ to have
void returning main. I don't see any need to be so accepting.

(&     the change you've made in Sema::ActOnFinishFunctionBody scares me a
bit - what does that do when you have int returning main but you turn
this flag on? not allow implicit return 0? that seems problematic)

- David

David, thanks for your reply.

Seems like my response missed mailing list, so I'm sending it again.
I changed the patch and now "-allow-non-int-main" option allows to print
a warning in C and error in C++ in case of non-integer main. I also
fixed implicit returning 0.
Sorry, I just didn't get around to replying - I still think this
should be under the -Wmain flag, probably - like it is in GCC. But I
could be wrong - hopefully someone else pipes up with an opinion too.

(it's possible it should be stronger - perhaps a separate warning that
defaults to error - but I don't think it should be an entirely
new/separate flag)

- David
I reduced patch and now it provides GCC-like behavior. In C clang prints a
warning and in C++ prints error. The warning is under control of -Wmain
flag.
This looks better - though do you need the change in codegen? what
happens if you remove that (leave it as it was before)?&   some test
cases would be good.

I'm still a bit concerned about relaxing the default behavior too much
- hopefully other Clang developers will chime in with a more informed
opinion on this, but perhaps we'll want to group this particular
warning under its own flag (main-return-type ?)&   make that diagnostic
an error by default (this is a weird device to use&   it turns up in a
few other places in Clang, but it might be an appropriate fit here).
According to C99 5.1.2.2.3 Clang allows main() to implicitly return 0. 
ReturnValue variable in CodeGenFunction should not be null to hold the return 
value. If changes in codegen are removed Clang crashes trying to create 
implicit return.
The appropriate fix here is for Sema to not set the hasImplicitReturnZero() bit.

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

Vasiliy Korchagin,
The Institute for System Programming of the Russian Academy of Sciences
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 0caf192..3f9d388 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -348,6 +348,8 @@ def err_constexpr_main : Error<
   "'main' is not allowed to be declared constexpr">;
 def err_main_template_decl : Error<"'main' cannot be a template">;
 def err_main_returns_nonint : Error<"'main' must return 'int'">;
+def warn_main_returns_nonint : Warning<"return type of 'main' is not 'int'">,
+    InGroup<Main>;
 def err_main_surplus_args : Error<"too many parameters (%0) for 'main': "
     "must be 0, 2, or 3">;
 def warn_main_one_arg : Warning<"only one parameter on 'main' declaration">,
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 6ba3228..925536a 100644
--- 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);
+    } else {
+      Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint);
+      FD->setInvalidDecl(true);
+    }
   }
 
   // Treat protoless main() as nullary.
@@ -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);
       WP.disableCheckFallThrough();
     } else if (FD->hasAttr<NakedAttr>()) {
       // If the function is marked 'naked', don't complain about missing return
diff --git a/test/Sema/warn-main-returns-nonint.c b/test/Sema/warn-main-returns-nonint.c
new file mode 100644
index 0000000..e1dc114
--- /dev/null
+++ b/test/Sema/warn-main-returns-nonint.c
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+void main() {} // expected-warning {{return type of 'main' is not 'int'}}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to