14.02.2012 05:20, John McCall пишет:
On Feb 13, 2012, at 4:38 PM, John McCall wrote:
On Feb 11, 2012, at 7:31 AM, Vasiliy Korchagin wrote:
I agree, without setting implicit return zero bit changes in codegen are not
necessary. New version of patch is attached.
This check has nothing to do with C99. You should be checking for "has GNU extensions"
and "is not C++", i.e.
getLangOptions().GNU&& !getLangOptions().CPlusPlus
I see that I missed the later follow-ups in this thread, and you're now
checking CPlusPlus correctly. Please do also check for GNU
extensions being enabled and implement this:
Also, please have CheckMain just set hasImplicitReturnZero() as appropriate and
then make actOnFinishFunctionBody disable fall-through checking when that bit
is set.
With that, this should be fine.
John.
I fixed the patch. New patch is attached.
Vasiliy Korchagin,
The Institute for System Programming (ISP) of the Russian Academy of
Sciences (RAS)
diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td
index 5d07e5c..d295683 100644
--- a/include/clang/Basic/DiagnosticGroups.td
+++ b/include/clang/Basic/DiagnosticGroups.td
@@ -104,6 +104,7 @@ def LocalTypeTemplateArgs : DiagGroup<"local-type-template-args",
[CXX98CompatLocalTypeTemplateArgs]>;
def MalformedWarningCheck : DiagGroup<"malformed-warning-check">;
def Main : DiagGroup<"main">;
+def MainReturnType : DiagGroup<"main-return-type">;
def MissingBraces : DiagGroup<"missing-braces">;
def MissingDeclarations: DiagGroup<"missing-declarations">;
def : DiagGroup<"missing-format-attribute">;
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index f990212..f16ba61 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -346,6 +346,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 ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
+ InGroup<MainReturnType>;
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 de9fecc..e5e59fb 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -5797,9 +5797,16 @@ 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().GNUMode && !getLangOptions().CPlusPlus) {
+ // In C with GNU extensions we allow main() to have non-integer return
+ // type, but we should warn about it.
+ Diag(FD->getTypeSpecStartLoc(), diag::ext_main_returns_nonint);
+ } else {
+ Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint);
+ FD->setInvalidDecl(true);
+ }
+ } else
+ FD->setHasImplicitReturnZero(true);
// Treat protoless main() as nullary.
if (isa<FunctionNoProtoType>(FT)) return;
@@ -7259,16 +7266,13 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
if (FD) {
FD->setBody(Body);
- if (FD->isMain()) {
+ if ((FD->isMain() && FD->hasImplicitReturnZero()) ||
+ FD->hasAttr<NakedAttr>())
// 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);
+ // Also if the function is marked 'naked', don't complain about missing
+ // return statements.
WP.disableCheckFallThrough();
- } else if (FD->hasAttr<NakedAttr>()) {
- // If the function is marked 'naked', don't complain about missing return
- // statements.
- WP.disableCheckFallThrough();
- }
// MSVC permits the use of pure specifier (=0) on function definition,
// defined at class scope, warn about this non standard construct.
diff --git a/test/Sema/main.c b/test/Sema/main.c
new file mode 100644
index 0000000..e1dc114
--- /dev/null
+++ b/test/Sema/main.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