After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 337128 - Macro expansion problems calling G_DEFINE_TYPE_EXTENDED
Macro expansion problems calling G_DEFINE_TYPE_EXTENDED
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: gobject
unspecified
Other Linux
: Normal normal
: ---
Assigned To: Tim Janik
gtkdev
Depends on:
Blocks: 337129
 
 
Reported: 2006-04-04 02:39 UTC by Behdad Esfahbod
Modified: 2006-05-02 14:53 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
test case (862 bytes, text/x-csrc)
2006-04-18 20:48 UTC, Behdad Esfahbod
  Details
patch (2.73 KB, patch)
2006-04-18 20:49 UTC, Behdad Esfahbod
none Details | Review

Description Behdad Esfahbod 2006-04-04 02:39:21 UTC
Currently the different G_DEFINE_TYPE macros call G_DEFINE_TYPE_EXTENDED:

#define G_DEFINE_TYPE(TN, t_n, T_P)                         G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, 0, {})
#define G_DEFINE_TYPE_WITH_CODE(TN, t_n, T_P, _C_)          G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, 0, _C_)
#define G_DEFINE_ABSTRACT_TYPE(TN, t_n, T_P)                G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, G_TYPE_FLAG_ABSTRACT, {})
#define G_DEFINE_ABSTRACT_TYPE_WITH_CODE(TN, t_n, T_P, _C_) G_DEFINE_TYPE_EXTENDED (TN, t_n, T_P, G_TYPE_FLAG_ABSTRACT, _C_)

This is problematic though, as _C_ is expanded before passing to G_DEFINE_TYPE_EXTENDED, causing compile errors if the code passed in contains any comma tokens!


This can be fixed easily, by breaking G_DEFINE_TYPE_EXTENDED into two parts, one before the code and one after, and make _WITH_CODE macros call them and expand _C_ themselves.
Comment 1 Behdad Esfahbod 2006-04-04 02:42:52 UTC
Ok, not any comma tokens, but commas not inside parantheses.  See bug 337129 for an example of code breaking because of this.
Comment 2 Matthias Clasen 2006-04-04 02:54:14 UTC
Isn't the usual trick to work around this to add extra () around the _C_ ?
Comment 3 Behdad Esfahbod 2006-04-04 03:03:19 UTC
That only works for expressions, which is not the case in G_IMPLEMENT_INTERFACE for example.
Comment 4 Tim Janik 2006-04-18 10:17:29 UTC
behdad, can you provide examples and a patch, so that:
a) the example gets broken by commas in the code,
b) the patch splits up DEFINE_TYPE as described
c) the example works with teh patch applied?
i'm asking since i have my doubts that (c) actually occours, because additional commas will already confuse the call to G_DEFINE_TYPE_WITH_CODE().

as for the general buggynedd of commas in code in preprocessor args... there's only so much you can do in C, GObject + preprocessor magic simply isn't a full replacement for native language OO support.
Comment 5 Behdad Esfahbod 2006-04-18 15:16:23 UTC
Tim,
a) the patch in bug 337129 breaks implementation of any interface using g_DEFINE_TYPE_WITH_CODE (..., G_IMPLEMENT_INTERFACE(...)).
b) I'll attach a patch,
c) it happens when you pass a macro as the code to G_DEFINE_TYPE_WITH_CODE.  For example, G_IMPLEMENT_INTERFACE is such a macro.  So it doesn't confuse G_DEFINE_TYPE_WITH_CODE.
Comment 6 Behdad Esfahbod 2006-04-18 20:48:00 UTC
Created attachment 63825 [details]
test case
Comment 7 Behdad Esfahbod 2006-04-18 20:49:49 UTC
Created attachment 63826 [details] [review]
patch
Comment 8 Tim Janik 2006-05-02 13:02:08 UTC
ok, thanks for the code Behdad, i see the problem now.
your "fix" really only works in the presence of one level of macro nesting, the general problem persists. however that's no reason to not apply your changes as they give one nesting macro level headroom, so i've done that now with some slight cleanups.
i've also added your test code, assuming the same license you submitted the patch under.
Comment 9 Behdad Esfahbod 2006-05-02 14:53:56 UTC
Thanks Tim.  Yeah, no license problem.