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

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 28 17:35:18 PST 2016


Author: rsmith
Date: Mon Nov 28 19:35:17 2016
New Revision: 288097

URL: http://llvm.org/viewvc/llvm-project?rev=288097&view=rev
Log:
Add a warning for 'main' returning 'true' or 'false'.

Patch by Joshua Hurwitz!

Added:
    cfe/trunk/test/Sema/warn-main-returns-bool-literal.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaStmt.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=288097&r1=288096&r2=288097&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Nov 28 19:35:17 2016
@@ -646,6 +646,8 @@ def warn_main_one_arg : Warning<"only on
 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 "

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=288097&r1=288096&r2=288097&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Mon Nov 28 19:35:17 2016
@@ -3193,6 +3193,10 @@ StmtResult Sema::BuildReturnStmt(SourceL
     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;

Added: cfe/trunk/test/Sema/warn-main-returns-bool-literal.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-main-returns-bool-literal.cpp?rev=288097&view=auto
==============================================================================
--- cfe/trunk/test/Sema/warn-main-returns-bool-literal.cpp (added)
+++ cfe/trunk/test/Sema/warn-main-returns-bool-literal.cpp Mon Nov 28 19:35:17 2016
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wmain -verify %s
+
+// expected-note at +1 {{previous definition is here}}
+int main() {
+  return 0;
+}  // no-warning
+
+// expected-error at +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'}}
+}




More information about the cfe-commits mailing list