summaryrefslogtreecommitdiffstats
path: root/clang/lib/Sema/SemaDecl.cpp
diff options
context:
space:
mode:
authorJohn McCall <rjmccall@apple.com>2012-02-14 19:50:52 +0000
committerJohn McCall <rjmccall@apple.com>2012-02-14 19:50:52 +0000
commit5ed3caf2e3640cf5e16e901094f7c3c5cee20ea0 (patch)
tree6e5fa93368f4c709c1136a96ca3f99db9d57a479 /clang/lib/Sema/SemaDecl.cpp
parentd2a9075de0a21548b9f01d24ed309bc5cb26f99f (diff)
downloadbcm5719-llvm-5ed3caf2e3640cf5e16e901094f7c3c5cee20ea0.tar.gz
bcm5719-llvm-5ed3caf2e3640cf5e16e901094f7c3c5cee20ea0.zip
Warn about non-int main() results in GNU C mode instead of erroring.
Based on a patch by Vasiliy Korchagin! llvm-svn: 150500
Diffstat (limited to 'clang/lib/Sema/SemaDecl.cpp')
-rw-r--r--clang/lib/Sema/SemaDecl.cpp33
1 files changed, 21 insertions, 12 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index de9feccaf33..304304b776c 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -5794,9 +5794,23 @@ void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) {
QualType T = FD->getType();
assert(T->isFunctionType() && "function decl is not of function type");
- const FunctionType* FT = T->getAs<FunctionType>();
-
- if (!Context.hasSameUnqualifiedType(FT->getResultType(), Context.IntTy)) {
+ const FunctionType* FT = T->castAs<FunctionType>();
+
+ // All the standards say that main() should should return 'int'.
+ if (Context.hasSameUnqualifiedType(FT->getResultType(), Context.IntTy)) {
+ // In C and C++, main magically returns 0 if you fall off the end;
+ // set the flag which tells us that.
+ // This is C++ [basic.start.main]p5 and C99 5.1.2.2.3.
+ FD->setHasImplicitReturnZero(true);
+
+ // In C with GNU extensions we allow main() to have non-integer return
+ // type, but we should warn about the extension, and we disable the
+ // implicit-return-zero rule.
+ } else if (getLangOptions().GNUMode && !getLangOptions().CPlusPlus) {
+ Diag(FD->getTypeSpecStartLoc(), diag::ext_main_returns_nonint);
+
+ // Otherwise, this is just a flat-out error.
+ } else {
Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint);
FD->setInvalidDecl(true);
}
@@ -7259,16 +7273,11 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
if (FD) {
FD->setBody(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);
- WP.disableCheckFallThrough();
- } else if (FD->hasAttr<NakedAttr>()) {
- // If the function is marked 'naked', don't complain about missing return
- // statements.
+
+ // If the function implicitly returns zero (like 'main') or is naked,
+ // don't complain about missing return statements.
+ if (FD->hasImplicitReturnZero() || FD->hasAttr<NakedAttr>())
WP.disableCheckFallThrough();
- }
// MSVC permits the use of pure specifier (=0) on function definition,
// defined at class scope, warn about this non standard construct.
OpenPOWER on IntegriCloud