Add a warning for 'main' returning 'true' or 'false'.

Patch by Joshua Hurwitz!

llvm-svn: 288097
This commit is contained in:
Richard Smith 2016-11-29 01:35:17 +00:00
parent 78565839c6
commit 9bb192ed99
3 changed files with 26 additions and 0 deletions

View File

@ -646,6 +646,8 @@ def warn_main_one_arg : Warning<"only one parameter on 'main' declaration">,
def err_main_arg_wrong : Error<"%select{first|second|third|fourth}0 "
"parameter of 'main' (%select{argument count|argument array|environment|"
"platform-specific data}0) must be of type %1">;
def warn_main_returns_bool_literal : Warning<"bool literal returned from "
"'main'">, InGroup<Main>;
def err_main_global_variable :
Error<"main cannot be declared as global variable">;
def warn_main_redefined : Warning<"variable named 'main' with external linkage "

View File

@ -3193,6 +3193,10 @@ StmtResult Sema::BuildReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
if (FD->isNoReturn())
Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr)
<< FD->getDeclName();
if (FD->isMain() && RetValExp)
if (isa<CXXBoolLiteralExpr>(RetValExp))
Diag(ReturnLoc, diag::warn_main_returns_bool_literal)
<< RetValExp->getSourceRange();
} else if (ObjCMethodDecl *MD = getCurMethodDecl()) {
FnRetType = MD->getReturnType();
isObjCMethod = true;

View File

@ -0,0 +1,20 @@
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wmain -verify %s
// expected-note@+1 {{previous definition is here}}
int main() {
return 0;
} // no-warning
// expected-error@+1 {{redefinition of 'main'}}
int main() {
return 1.0;
} // no-warning
int main() {
bool b = true;
return b; // no-warning
}
int main() {
return true; // expected-warning {{bool literal returned from 'main'}}
}