[clang] Add new flag -Wreturn-mismatch (PR #82872)

Bhuminjay Soni via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 5 11:22:34 PST 2024


https://github.com/11happy updated https://github.com/llvm/llvm-project/pull/82872

>From 557ee75d60f0fdb4dd2b353c819b4f22f71b46d7 Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Sat, 24 Feb 2024 13:50:30 +0530
Subject: [PATCH 1/3] add new flag -Wreturn-mismatch

Signed-off-by: 11happy <soni5happy at gmail.com>
---
 clang/include/clang/Basic/DiagnosticGroups.td    | 1 +
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 6 +++---
 clang/test/Analysis/null-deref-ps.c              | 4 ++--
 clang/test/CodeGen/statements.c                  | 2 +-
 clang/test/CodeGen/volatile-1.c                  | 2 +-
 clang/test/Sema/return-silent.c                  | 2 +-
 clang/test/Sema/return.c                         | 2 +-
 7 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 6765721ae7002c..7a786a24083583 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -607,6 +607,7 @@ def RedundantMove : DiagGroup<"redundant-move">;
 def Register : DiagGroup<"register", [DeprecatedRegister]>;
 def ReturnTypeCLinkage : DiagGroup<"return-type-c-linkage">;
 def ReturnType : DiagGroup<"return-type", [ReturnTypeCLinkage]>;
+def ReturnMismatch : DiagGroup<"return-mismatch">;
 def BindToTemporaryCopy : DiagGroup<"bind-to-temporary-copy",
                                     [CXX98CompatBindToTemporaryCopy]>;
 def SelfAssignmentField : DiagGroup<"self-assign-field">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 24d32cb87c89e2..7057b4b2123671 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10212,14 +10212,14 @@ def warn_second_parameter_to_va_arg_never_compatible : Warning<
 
 def warn_return_missing_expr : Warning<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
-  InGroup<ReturnType>;
+  InGroup<ReturnMismatch>;
 def ext_return_missing_expr : ExtWarn<
   "non-void %select{function|method}1 %0 should return a value">, DefaultError,
-  InGroup<ReturnType>;
+  InGroup<ReturnMismatch>;
 def ext_return_has_expr : ExtWarn<
   "%select{void function|void method|constructor|destructor}1 %0 "
   "should not return a value">,
-  DefaultError, InGroup<ReturnType>;
+  DefaultError, InGroup<ReturnMismatch>;
 def ext_return_has_void_expr : Extension<
   "void %select{function|method|block}1 %0 should not return void expression">;
 def err_return_init_list : Error<
diff --git a/clang/test/Analysis/null-deref-ps.c b/clang/test/Analysis/null-deref-ps.c
index d80de15c05a3fe..2682ad02ee37f9 100644
--- a/clang/test/Analysis/null-deref-ps.c
+++ b/clang/test/Analysis/null-deref-ps.c
@@ -1,5 +1,5 @@
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion -Wno-strict-prototypes -Wno-tautological-constant-compare -Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion -Wno-strict-prototypes -Wno-tautological-constant-compare -Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -verify %s -Wno-error=return-type
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion -Wno-strict-prototypes -Wno-tautological-constant-compare -Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type -Wno-error=return-mismatch
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion -Wno-strict-prototypes -Wno-tautological-constant-compare -Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -verify %s -Wno-error=return-type -Wno-error=return-mismatch
 
 typedef unsigned uintptr_t;
 
diff --git a/clang/test/CodeGen/statements.c b/clang/test/CodeGen/statements.c
index 07ae075d6d807e..76f4f254dfd1dc 100644
--- a/clang/test/CodeGen/statements.c
+++ b/clang/test/CodeGen/statements.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -Wno-error=return-type -Wno-error=int-conversion %s -emit-llvm-only
+// RUN: %clang_cc1 -Wno-error=return-type -Wno-error=return-mismatch -Wno-error=int-conversion %s -emit-llvm-only
 // REQUIRES: LP64
 
 // Mismatched type between return and function result.
diff --git a/clang/test/CodeGen/volatile-1.c b/clang/test/CodeGen/volatile-1.c
index cd919c78989fe2..80c40d21cf593e 100644
--- a/clang/test/CodeGen/volatile-1.c
+++ b/clang/test/CodeGen/volatile-1.c
@@ -1,5 +1,5 @@
 // XFAIL: target=aarch64-pc-windows-msvc
-// RUN: %clang_cc1 -Wno-return-type -Wno-unused-value -emit-llvm %s -w -o - | FileCheck %s
+// RUN: %clang_cc1 -Wno-return-type -Wno-return-mismatch -Wno-unused-value -emit-llvm %s -w -o - | FileCheck %s
 
 // CHECK: @i = {{(dso_local )?}}global [[INT:i[0-9]+]] 0
 volatile int i, j, k;
diff --git a/clang/test/Sema/return-silent.c b/clang/test/Sema/return-silent.c
index 720128d7ea60b2..e510c9c678c2bd 100644
--- a/clang/test/Sema/return-silent.c
+++ b/clang/test/Sema/return-silent.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -Wno-return-type -fsyntax-only -verify
+// RUN: %clang_cc1 %s -Wno-return-type -Wno-return-mismatch -fsyntax-only -verify
 // expected-no-diagnostics
 
 int t14(void) {
diff --git a/clang/test/Sema/return.c b/clang/test/Sema/return.c
index 14d5300e819492..b4f3f132dce7a1 100644
--- a/clang/test/Sema/return.c
+++ b/clang/test/Sema/return.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple x86_64-apple-darwin9 %s -fsyntax-only -Wignored-qualifiers -Wno-error=return-type -Wno-error=implicit-int -verify -fblocks -Wno-unreachable-code -Wno-unused-value -Wno-strict-prototypes
+// RUN: %clang_cc1 -triple x86_64-apple-darwin9 %s -fsyntax-only -Wignored-qualifiers -Wno-error=return-type -Wno-error=return-mismatch -Wno-error=implicit-int -verify -fblocks -Wno-unreachable-code -Wno-unused-value -Wno-strict-prototypes
 
 // clang emits the following warning by default.
 // With GCC, -pedantic, -Wreturn-type or -Wall are required to produce the

>From fd543c86cbcd1082d8d55151494014c735020dea Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Tue, 5 Mar 2024 17:32:23 +0530
Subject: [PATCH 2/3] add test and release notes

Signed-off-by: 11happy <soni5happy at gmail.com>
---
 clang/docs/ReleaseNotes.rst                   |  1 +
 clang/include/clang/Basic/DiagnosticGroups.td |  3 +-
 clang/test/Analysis/null-deref-ps.c           |  4 +--
 clang/test/CodeGen/statements.c               |  2 +-
 clang/test/CodeGen/volatile-1.c               |  2 +-
 clang/test/Misc/warning-wall.c                |  1 +
 clang/test/Sema/return-mismatch.c             | 29 +++++++++++++++++++
 clang/test/Sema/return-silent.c               |  2 +-
 clang/test/Sema/return-type.c                 | 29 +++++++++++++++++++
 clang/test/Sema/return.c                      |  2 +-
 10 files changed, 68 insertions(+), 7 deletions(-)
 create mode 100644 clang/test/Sema/return-mismatch.c
 create mode 100644 clang/test/Sema/return-type.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 254e0a9cb72979..c6f4cd6dcafea3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -94,6 +94,7 @@ Deprecated Compiler Flags
 
 Modified Compiler Flags
 -----------------------
+- The `-Wreturn-type` flag has been modified to split some of its functionality into `-Wreturn-mismatch` flag.
 
 Removed Compiler Flags
 -------------------------
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 7a786a24083583..ac39e9990d16de 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -606,8 +606,9 @@ def GNURedeclaredEnum : DiagGroup<"gnu-redeclared-enum">;
 def RedundantMove : DiagGroup<"redundant-move">;
 def Register : DiagGroup<"register", [DeprecatedRegister]>;
 def ReturnTypeCLinkage : DiagGroup<"return-type-c-linkage">;
-def ReturnType : DiagGroup<"return-type", [ReturnTypeCLinkage]>;
 def ReturnMismatch : DiagGroup<"return-mismatch">;
+def ReturnType : DiagGroup<"return-type", [ReturnTypeCLinkage, ReturnMismatch]>;
+
 def BindToTemporaryCopy : DiagGroup<"bind-to-temporary-copy",
                                     [CXX98CompatBindToTemporaryCopy]>;
 def SelfAssignmentField : DiagGroup<"self-assign-field">;
diff --git a/clang/test/Analysis/null-deref-ps.c b/clang/test/Analysis/null-deref-ps.c
index 2682ad02ee37f9..d80de15c05a3fe 100644
--- a/clang/test/Analysis/null-deref-ps.c
+++ b/clang/test/Analysis/null-deref-ps.c
@@ -1,5 +1,5 @@
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion -Wno-strict-prototypes -Wno-tautological-constant-compare -Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type -Wno-error=return-mismatch
-// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion -Wno-strict-prototypes -Wno-tautological-constant-compare -Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -verify %s -Wno-error=return-type -Wno-error=return-mismatch
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion -Wno-strict-prototypes -Wno-tautological-constant-compare -Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -analyzer-purge=none -verify %s -Wno-error=return-type
+// RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -Wno-int-conversion -Wno-strict-prototypes -Wno-tautological-constant-compare -Wtautological-unsigned-zero-compare -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -verify %s -Wno-error=return-type
 
 typedef unsigned uintptr_t;
 
diff --git a/clang/test/CodeGen/statements.c b/clang/test/CodeGen/statements.c
index 76f4f254dfd1dc..07ae075d6d807e 100644
--- a/clang/test/CodeGen/statements.c
+++ b/clang/test/CodeGen/statements.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -Wno-error=return-type -Wno-error=return-mismatch -Wno-error=int-conversion %s -emit-llvm-only
+// RUN: %clang_cc1 -Wno-error=return-type -Wno-error=int-conversion %s -emit-llvm-only
 // REQUIRES: LP64
 
 // Mismatched type between return and function result.
diff --git a/clang/test/CodeGen/volatile-1.c b/clang/test/CodeGen/volatile-1.c
index 80c40d21cf593e..cd919c78989fe2 100644
--- a/clang/test/CodeGen/volatile-1.c
+++ b/clang/test/CodeGen/volatile-1.c
@@ -1,5 +1,5 @@
 // XFAIL: target=aarch64-pc-windows-msvc
-// RUN: %clang_cc1 -Wno-return-type -Wno-return-mismatch -Wno-unused-value -emit-llvm %s -w -o - | FileCheck %s
+// RUN: %clang_cc1 -Wno-return-type -Wno-unused-value -emit-llvm %s -w -o - | FileCheck %s
 
 // CHECK: @i = {{(dso_local )?}}global [[INT:i[0-9]+]] 0
 volatile int i, j, k;
diff --git a/clang/test/Misc/warning-wall.c b/clang/test/Misc/warning-wall.c
index 05a82770e26de6..4909ab034ef30a 100644
--- a/clang/test/Misc/warning-wall.c
+++ b/clang/test/Misc/warning-wall.c
@@ -44,6 +44,7 @@ CHECK-NEXT:      -Wreorder-ctor
 CHECK-NEXT:      -Wreorder-init-list
 CHECK-NEXT:    -Wreturn-type
 CHECK-NEXT:      -Wreturn-type-c-linkage
+CHECK-NEXT:      -Wreturn-mismatch
 CHECK-NEXT:    -Wself-assign
 CHECK-NEXT:      -Wself-assign-overloaded
 CHECK-NEXT:      -Wself-assign-field
diff --git a/clang/test/Sema/return-mismatch.c b/clang/test/Sema/return-mismatch.c
new file mode 100644
index 00000000000000..d2a7185d9c6ca9
--- /dev/null
+++ b/clang/test/Sema/return-mismatch.c
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -Wno-return-type -Wreturn-mismatch -fsyntax-only -verify %s
+
+// Test that -Wreturn-mismatch is enabled and -Wreturn-type is disabled.
+
+int foo(void) __attribute__((noreturn));
+int bar(void);
+
+void test1() {
+  return 1; // expected-warning{{void function 'test1' should not return a value}}
+}
+
+int test2() { 
+    return; // expected-warning{{non-void function 'test2' should return a value}}
+} 
+
+int test3() { 
+    // no-warning
+} 
+
+int test4() {
+    (void)(bar() || foo()); // no-warning
+} 
+
+void test5() {
+} // no-warning
+
+int test6() {
+  return 0; // no-warning
+}
\ No newline at end of file
diff --git a/clang/test/Sema/return-silent.c b/clang/test/Sema/return-silent.c
index e510c9c678c2bd..720128d7ea60b2 100644
--- a/clang/test/Sema/return-silent.c
+++ b/clang/test/Sema/return-silent.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -Wno-return-type -Wno-return-mismatch -fsyntax-only -verify
+// RUN: %clang_cc1 %s -Wno-return-type -fsyntax-only -verify
 // expected-no-diagnostics
 
 int t14(void) {
diff --git a/clang/test/Sema/return-type.c b/clang/test/Sema/return-type.c
new file mode 100644
index 00000000000000..0a5f5a85368f68
--- /dev/null
+++ b/clang/test/Sema/return-type.c
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -Wreturn-type -Wno-return-mismatch -fsyntax-only -verify %s
+
+// Test that -Wreturn-type is enabled and -Wreturn-mismatch is disabled.
+
+int foo(void) __attribute__((noreturn));
+int bar(void);
+
+void test1() {
+  return 1; // no-warning
+}
+
+int test2() { 
+    return; // no-warning
+} 
+
+int test3() { 
+    // expected-warning at +1 {{non-void function does not return a value}}
+} 
+
+int test4() {
+    (void)(bar() || foo()); // expected-warning at +1 {{non-void function does not return a value in all control paths}}
+} 
+
+void test5() {
+} // no-warning
+
+int test6() {
+  return 0; // no-warning
+}
\ No newline at end of file
diff --git a/clang/test/Sema/return.c b/clang/test/Sema/return.c
index b4f3f132dce7a1..14d5300e819492 100644
--- a/clang/test/Sema/return.c
+++ b/clang/test/Sema/return.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple x86_64-apple-darwin9 %s -fsyntax-only -Wignored-qualifiers -Wno-error=return-type -Wno-error=return-mismatch -Wno-error=implicit-int -verify -fblocks -Wno-unreachable-code -Wno-unused-value -Wno-strict-prototypes
+// RUN: %clang_cc1 -triple x86_64-apple-darwin9 %s -fsyntax-only -Wignored-qualifiers -Wno-error=return-type -Wno-error=implicit-int -verify -fblocks -Wno-unreachable-code -Wno-unused-value -Wno-strict-prototypes
 
 // clang emits the following warning by default.
 // With GCC, -pedantic, -Wreturn-type or -Wall are required to produce the

>From 2059e2a6fd8ee559de0b142b6169fbb48922cd7a Mon Sep 17 00:00:00 2001
From: 11happy <soni5happy at gmail.com>
Date: Wed, 6 Mar 2024 00:52:19 +0530
Subject: [PATCH 3/3] reword release notes and add more tests

Signed-off-by: 11happy <soni5happy at gmail.com>
---
 clang/docs/ReleaseNotes.rst            |  4 ++-
 clang/test/Sema/return-mismatch.c      | 29 ---------------------
 clang/test/Sema/return-type-mismatch.c | 36 ++++++++++++++++++++++++++
 clang/test/Sema/return-type.c          | 29 ---------------------
 4 files changed, 39 insertions(+), 59 deletions(-)
 delete mode 100644 clang/test/Sema/return-mismatch.c
 create mode 100644 clang/test/Sema/return-type-mismatch.c
 delete mode 100644 clang/test/Sema/return-type.c

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c6f4cd6dcafea3..b5e3c6c252578a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -94,7 +94,9 @@ Deprecated Compiler Flags
 
 Modified Compiler Flags
 -----------------------
-- The `-Wreturn-type` flag has been modified to split some of its functionality into `-Wreturn-mismatch` flag.
+- Added a new diagnostic flag ``-Wreturn-mismatch`` which is grouped under
+  ``-Wreturn-type``, and moved some of the diagnostics previously controlled by
+  ``-Wreturn-type`` under this new flag. Fixes #GH72116.
 
 Removed Compiler Flags
 -------------------------
diff --git a/clang/test/Sema/return-mismatch.c b/clang/test/Sema/return-mismatch.c
deleted file mode 100644
index d2a7185d9c6ca9..00000000000000
--- a/clang/test/Sema/return-mismatch.c
+++ /dev/null
@@ -1,29 +0,0 @@
-// RUN: %clang_cc1 -Wno-return-type -Wreturn-mismatch -fsyntax-only -verify %s
-
-// Test that -Wreturn-mismatch is enabled and -Wreturn-type is disabled.
-
-int foo(void) __attribute__((noreturn));
-int bar(void);
-
-void test1() {
-  return 1; // expected-warning{{void function 'test1' should not return a value}}
-}
-
-int test2() { 
-    return; // expected-warning{{non-void function 'test2' should return a value}}
-} 
-
-int test3() { 
-    // no-warning
-} 
-
-int test4() {
-    (void)(bar() || foo()); // no-warning
-} 
-
-void test5() {
-} // no-warning
-
-int test6() {
-  return 0; // no-warning
-}
\ No newline at end of file
diff --git a/clang/test/Sema/return-type-mismatch.c b/clang/test/Sema/return-type-mismatch.c
new file mode 100644
index 00000000000000..79a625d7df1f54
--- /dev/null
+++ b/clang/test/Sema/return-type-mismatch.c
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -Wreturn-type -Wno-return-mismatch -fsyntax-only -verify=return-type %s
+// RUN: %clang_cc1 -Wno-return-type -Wreturn-mismatch -fsyntax-only -verify=return-mismatch %s
+
+int foo(void) __attribute__((noreturn));
+int bar(void);
+
+void test1(void) {
+  return 1; // return-mismatch-warning{{void function 'test1' should not return a value}}
+}
+
+int test2(void) { 
+    return; // return-mismatch-warning{{non-void function 'test2' should return a value}}
+} 
+
+int test3(void) { 
+    // return-type-warning at +1 {{non-void function does not return a value}}
+} 
+
+int test4(void) {
+    (void)(bar() || foo()); // return-type-warning at +1 {{non-void function does not return a value in all control paths}}
+} 
+
+void test5(void) {
+} // no-warning
+
+int test6(void) {
+  return 0; // no-warning
+}
+
+int test7(void) {
+  foo(); // no warning
+}
+
+int test8(void) {
+  bar(); // return-type-warning at +1 {{non-void function does not return a value}}
+}
diff --git a/clang/test/Sema/return-type.c b/clang/test/Sema/return-type.c
deleted file mode 100644
index 0a5f5a85368f68..00000000000000
--- a/clang/test/Sema/return-type.c
+++ /dev/null
@@ -1,29 +0,0 @@
-// RUN: %clang_cc1 -Wreturn-type -Wno-return-mismatch -fsyntax-only -verify %s
-
-// Test that -Wreturn-type is enabled and -Wreturn-mismatch is disabled.
-
-int foo(void) __attribute__((noreturn));
-int bar(void);
-
-void test1() {
-  return 1; // no-warning
-}
-
-int test2() { 
-    return; // no-warning
-} 
-
-int test3() { 
-    // expected-warning at +1 {{non-void function does not return a value}}
-} 
-
-int test4() {
-    (void)(bar() || foo()); // expected-warning at +1 {{non-void function does not return a value in all control paths}}
-} 
-
-void test5() {
-} // no-warning
-
-int test6() {
-  return 0; // no-warning
-}
\ No newline at end of file



More information about the cfe-commits mailing list