[PATCH wayland] private: Remove unnecessary forward declarations

Yong Bakos junk at humanoriented.com
Thu May 12 14:19:36 UTC 2016


On May 12, 2016, at 3:43 AM, Pekka Paalanen <ppaalanen at gmail.com> wrote:
> 
> On Mon, 09 May 2016 13:24:29 -0500
> Derek Foreman <derekf at osg.samsung.com> wrote:
> 
>> On 07/05/16 09:11 AM, Yong Bakos wrote:
>>> From: Yong Bakos <ybakos at humanoriented.com>
>>> 
>>> Declarations for wl_connection and wl_closure are not needed here.
>>> wl_closure already has a complete definition.
>>> Removing these forward declarations results in a clean, warning-free compile.
>>> 
>>> Signed-off-by: Yong Bakos <ybakos at humanoriented.com>
>>> ---
>>> src/wayland-private.h | 4 ----
>>> 1 file changed, 4 deletions(-)
>>> 
>>> diff --git a/src/wayland-private.h b/src/wayland-private.h
>>> index 994bc45..0d8b630 100644
>>> --- a/src/wayland-private.h
>>> +++ b/src/wayland-private.h
>>> @@ -99,10 +99,6 @@ wl_map_lookup_flags(struct wl_map *map, uint32_t i);
>>> void
>>> wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data);
>>> 
>>> -struct wl_connection;
>>> -struct wl_closure;
>>> -struct wl_proxy;  
>> 
>> Trying to think of a place in C where a forward struct declaration is
>> actually required.
>> 
>> This file isn't it. ;)
>> 
>> Reviewed-by: Derek Foreman <derekf at osg.samsung.com>
> 
> Why are they not required, btw? Yes, I don't get a compiler warning or
> anything here and now, but I do recall sometimes seeing warning like
> "declared inside parameter list" or such. So declaring in a return type
> is fine?

The fact that you and Derek were a little surprised about this, as was I,
makes me feel a little better. Because I asked on SO [1] and some dude was
like, "You need to go study your C, kid."

Because these are pointer declarations, we don't need a complete
type definition. This we know.

Furthermore, the use of a struct pointer will generate an incomplete type
for that type's identifier. For example, a pointer to a previously
undeclared struct used as a struct member or a function return type. This
removes the need for the forward declaration, since the creation of the
incomplete type is implicit.

But declaring a pointer to a previously undeclared type in a parameter
list scopes the incomplete type definition to the function. So the error
is caused by the header having its own incomplete type definition of
wl_connection, which conflicts with the concrete type definition of the
same name in connection.c.

My explanation is just a summary as I understand it. I did start to dig
in the specification to find some explicit evidence of this, but I
stopped because, well, life is short, y'know?

yong

[1] http://stackoverflow.com/questions/37088931/why-does-the-existence-of-a-return-type-prevent-the-need-for-a-forward-declarati

> 
> wl_closure indeed is defined before its use, but wl_connection and
> wl_proxy are not even declared.
> 
> If I move the declaration of wl_connection_destroy before
> wl_connection_create, I get this:
> 
> In file included from src/connection.c:44:0:
> src/wayland-private.h:107:30: warning: ‘struct wl_connection’ declared inside parameter list [enabled by default]
> wl_connection_destroy(struct wl_connection *connection);
>                              ^
> src/wayland-private.h:107:30: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]
> src/connection.c:195:1: error: conflicting types for ‘wl_connection_destroy’
> wl_connection_destroy(struct wl_connection *connection)
> ^
> In file included from src/connection.c:44:0:
> src/wayland-private.h:107:1: note: previous declaration of ‘wl_connection_destroy’ was here
> wl_connection_destroy(struct wl_connection *connection);
> ^
> 
> Thanks,
> pq
> _______________________________________________
> wayland-devel mailing list
> wayland-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/wayland-devel



More information about the wayland-devel mailing list