From 48f54e5969db351ed3593519f3cab83435ff9793 Mon Sep 17 00:00:00 2001 From: Christopher Bazley Date: Sat, 26 Sep 2026 12:42:40 +0100 Subject: [PATCH] tccgen: Remove volatile qualifier from function return types Modify post_type() such that the volatile qualifier is removed from function return types, as required by ISO C. Previously, only the const qualifier was removed. Added tests for all three standard qualifiers, although the new test for restrict only passes because TCC ignores that keyword. --- tccgen.c | 6 ++--- tests/tests2/150_return_qualifiers.c | 30 +++++++++++++++++++++++ tests/tests2/150_return_qualifiers.expect | 0 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 tests/tests2/150_return_qualifiers.c create mode 100644 tests/tests2/150_return_qualifiers.expect diff --git a/tccgen.c b/tccgen.c index ab04f30a..4364ede4 100644 --- a/tccgen.c +++ b/tccgen.c @@ -5119,9 +5119,9 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td) /* if no parameters, then old type prototype */ l = FUNC_OLD; skip(')'); - /* NOTE: const is ignored in returned type as it has a special - meaning in gcc / C++ */ - type->t &= ~VT_CONSTANT; + /* A function return value has the unqualified version of its + declared type. */ + type->t &= ~(VT_CONSTANT | VT_VOLATILE); /* some ancient pre-K&R C allows a function to return an array and the array brackets to be put after the arguments, such that "int c()[]" means something like "int[] c()" */ diff --git a/tests/tests2/150_return_qualifiers.c b/tests/tests2/150_return_qualifiers.c new file mode 100644 index 00000000..b8a06f5b --- /dev/null +++ b/tests/tests2/150_return_qualifiers.c @@ -0,0 +1,30 @@ +const int const_return(void); +volatile int volatile_return(void); +int *restrict restrict_return(void); + +_Static_assert(__builtin_types_compatible_p(__typeof__(const_return()) *, int *), + "const is removed from a return type"); +_Static_assert(__builtin_types_compatible_p(__typeof__(volatile_return()) *, int *), + "volatile is removed from a return type"); +_Static_assert(__builtin_types_compatible_p(__typeof__(restrict_return()) *, int **), + "restrict is removed from a return type"); + +int const_return(void) +{ + return 0; +} + +int volatile_return(void) +{ + return 0; +} + +int *restrict_return(void) +{ + return 0; +} + +int main(void) +{ + return 0; +} diff --git a/tests/tests2/150_return_qualifiers.expect b/tests/tests2/150_return_qualifiers.expect new file mode 100644 index 00000000..e69de29b