From deb1922abcea42e03c55bd84144974913edce74b Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 29 Nov 2016 01:35:17 +0000 Subject: [PATCH] Add a warning for 'main' returning 'true' or 'false'. Patch by Joshua Hurwitz! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@288097 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/Sema/SemaStmt.cpp | 4 ++++ test/Sema/warn-main-returns-bool-literal.cpp | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 test/Sema/warn-main-returns-bool-literal.cpp diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 9b81a553dc..beba4a3eca 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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
; def err_main_global_variable : Error<"main cannot be declared as global variable">; def warn_main_redefined : Warning<"variable named 'main' with external linkage " diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index eba192d58a..79ad049d4b 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -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(RetValExp)) + Diag(ReturnLoc, diag::warn_main_returns_bool_literal) + << RetValExp->getSourceRange(); } else if (ObjCMethodDecl *MD = getCurMethodDecl()) { FnRetType = MD->getReturnType(); isObjCMethod = true; diff --git a/test/Sema/warn-main-returns-bool-literal.cpp b/test/Sema/warn-main-returns-bool-literal.cpp new file mode 100644 index 0000000000..188f161dd3 --- /dev/null +++ b/test/Sema/warn-main-returns-bool-literal.cpp @@ -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'}} +} -- 2.40.0