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

via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 15 20:27:34 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Fangrui Song (MaskRay)

<details>
<summary>Changes</summary>

The diagnostic is a warning in GCC. We make it a DefaultError warning
under -Wmain.

There is a use case to pass customized arguments to main without using -ffreestanding.

Close #<!-- -->85491


---
Full diff: https://github.com/llvm/llvm-project/pull/85494.diff


3 Files Affected:

- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2-2) 
- (modified) clang/lib/Sema/SemaDecl.cpp (+1-1) 
- (modified) clang/test/CXX/basic/basic.start/basic.start.main/p2.cpp (+11) 


``````````diff
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

``````````

</details>


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


More information about the cfe-commits mailing list