See attached.

Returning a bool from main is a special case of return type mismatch. The
common convention when returning a bool is that 'true' (== 1) indicates
success and 'false' (== 0) failure. But since main expects a return value
of 0 on success, returning a bool is usually unintended.
From 4b88e06e060fc70d6f82d17d92155377f939a96a Mon Sep 17 00:00:00 2001
From: Joshua Hurwitz <hurwi...@google.com>
Date: Fri, 14 Oct 2016 13:04:26 -0400
Subject: [PATCH] Warning for main returning a bool.

Returning a bool from main is a special case of return type mismatch.
The common convention when returning a bool is that 'true' (== 1)
indicates success and 'false' (== 0) failure. But since main expects a
return value of 0 on success, returning a bool is usually unintended.
---
 include/clang/Basic/DiagnosticSemaKinds.td |  2 ++
 lib/Sema/SemaStmt.cpp                      |  5 +++++
 test/Sema/warn-main-returns-bool.cpp       | 17 +++++++++++++++++
 3 files changed, 24 insertions(+)
 create mode 100644 test/Sema/warn-main-returns-bool.cpp

diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 872311f..9b9115d 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -634,6 +634,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 : Warning<"type of expression returned from 'main' "
+    "(%0) should be 'int'">, 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 "
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index eba192d..dca81a2 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/lib/Sema/SemaStmt.cpp
@@ -3193,6 +3193,11 @@ 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 (Context.hasSameUnqualifiedType(RetValExp->getType(), Context.BoolTy))
+        Diag(ReturnLoc, diag::warn_main_returns_bool)
+          << RetValExp->getType().getUnqualifiedType()
+          << RetValExp->getSourceRange();
   } else if (ObjCMethodDecl *MD = getCurMethodDecl()) {
     FnRetType = MD->getReturnType();
     isObjCMethod = true;
diff --git a/test/Sema/warn-main-returns-bool.cpp b/test/Sema/warn-main-returns-bool.cpp
new file mode 100644
index 0000000..8763335
--- /dev/null
+++ b/test/Sema/warn-main-returns-bool.cpp
@@ -0,0 +1,17 @@
+// 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() {
+  unsigned int u = 0;
+  return u;
+}  // no-warning
+
+int main() {
+  const bool b = true;
+  return b;  // expected-warning {{type of expression returned from 'main' ('bool') should be 'int'}}
+}
-- 
2.8.0.rc3.226.g39d4020

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to