[clang] [Sema] Allow -Wno-main to suppress the arg wrong error (PR #85494)

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 16 14:24:57 PDT 2024


https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/85494

>From 0fd3675447f41b033472f9269648e8e735bc543a Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Fri, 15 Mar 2024 20:26:52 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.5-bogner
---
 clang/include/clang/Basic/DiagnosticSemaKinds.td      |  4 ++--
 clang/lib/Sema/SemaDecl.cpp                           |  2 +-
 .../CXX/basic/basic.start/basic.start.main/p2.cpp     | 11 +++++++++++
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 8e97902564af08..6cf4e288af5f1f 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -950,9 +950,9 @@ def err_main_surplus_args : Error<"too many parameters (%0) for 'main': "
     "must be 0, 2, or 3">;
 def warn_main_one_arg : Warning<"only one parameter on 'main' declaration">,
     InGroup<Main>;
-def err_main_arg_wrong : Error<"%select{first|second|third|fourth}0 "
+def warn_main_arg_wrong : Warning<"%select{first|second|third|fourth}0 "
     "parameter of 'main' (%select{argument count|argument array|environment|"
-    "platform-specific data}0) must be of type %1">;
+    "platform-specific data}0) must be of type %1">, DefaultError, InGroup<Main>;
 def warn_main_returns_bool_literal : Warning<"bool literal returned from "
     "'main'">, InGroup<Main>;
 def err_main_global_variable :
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5850cd0ab6b9aa..2718934609842b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12488,7 +12488,7 @@ void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) {
     }
 
     if (mismatch) {
-      Diag(FD->getLocation(), diag::err_main_arg_wrong) << i << Expected[i];
+      Diag(FD->getLocation(), diag::warn_main_arg_wrong) << i << Expected[i];
       // TODO: suggest replacing given type with expected type
       FD->setInvalidDecl(true);
     }
diff --git a/clang/test/CXX/basic/basic.start/basic.start.main/p2.cpp b/clang/test/CXX/basic/basic.start/basic.start.main/p2.cpp
index 42e87e5431f2a5..135c828e7366a0 100644
--- a/clang/test/CXX/basic/basic.start/basic.start.main/p2.cpp
+++ b/clang/test/CXX/basic/basic.start/basic.start.main/p2.cpp
@@ -4,6 +4,7 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST4
 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST5
 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST6
+// RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST6A -Wno-main
 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST7
 // RUN: %clang_cc1 -fsyntax-only -verify %s -DTEST8
 
@@ -64,6 +65,16 @@ main( // expected-error {{first parameter of 'main' (argument count) must be of
 
 const int main(); // expected-error {{'main' must return 'int'}}
 
+#elif TEST6A
+
+void  // expected-error {{'main' must return 'int'}}
+main(
+     float a
+) {
+}
+
+const int main(); // expected-error {{'main' must return 'int'}}
+
 #elif TEST7
 
 // expected-no-diagnostics



More information about the cfe-commits mailing list