Skip to content

Commit

Permalink
Add new flag -Wreturn-mismatch (#82872)
Browse files Browse the repository at this point in the history
This pull request fixes #72116 where a new flag is introduced for
compatibility with GCC 14, the functionality of -Wreturn-type is
modified to split some of its behaviors into -Wreturn-mismatch

Fixes #72116
  • Loading branch information
11happy committed Mar 11, 2024
1 parent 866ac9a commit 8467457
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ Deprecated Compiler Flags

Modified Compiler Flags
-----------------------
- 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
-------------------------
Expand Down
4 changes: 3 additions & 1 deletion clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,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">;
Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -10248,14 +10248,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<
Expand Down
1 change: 1 addition & 0 deletions clang/test/Misc/warning-wall.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions clang/test/Sema/return-type-mismatch.c
Original file line number Diff line number Diff line change
@@ -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@+1 {{non-void function does not return a value}}
}

int test4(void) {
(void)(bar() || foo()); // return-type-warning@+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@+1 {{non-void function does not return a value}}
}

0 comments on commit 8467457

Please sign in to comment.