Extending and Unifying Attribute Macros



I was looking at the "Miscellaneous Macros" page [1] and thought that
the macros could be a little more extensive and cross-compiler.  I am
mainly focusing on the optimization/function attribute macros.

Currently all the attributes are defined as G_GNUC_* which is nice to
have but I think that it would be nicer to have compiler independent
macros where it is clearly specified that they very well may expand to
nothing.  While this is not very relevant at the moment because no
popular compiler supports an alternative syntax it is a little confusing
when your GNUC macros are being expanded on clang and in the future
supporting other compilers will be hard (because you would need a macro
in your code for each compiler).

What I propose is a set of macros that tell the compiler information
about the function iff it is supported and otherwise they have no
effect.  This obviously means that the macros must not change how the
code functions.

Compilers that I would like to support:
- GCC: already done, just need to alias the current macros and possibly
add more.
- clang: Supports many of the GCC attributes using the same syntax, but
in the future clang will likely have different macros than GCC.
- ICC: Intel's compiler supports some of GCC's attributes using the same
syntax and I would like to ensure they are used when available (I don't
know the current state of this).

I didn't mention MSVC because AFAIK they have no similar feature.  So
when using this (and other unsupported compilers) the macros wouldn't do
anything.

Listed below I have a number of attributes that GCC supports that I
think would be beneficial to have as part of glib.  (See [2] for the
details rather than my over-simplified summaries)

Some other attributes I think would be nice to have are:
- nothrow: Function will not throw an exception.
- leaf: Function will not call code from the calling compilation unit.
- nonnull: Specify what parameters should not be NULL.
- returns_nonnull: This function never returns NULL.
- hot: This function gets called a lot.
- cold: This function is rarely called.
- sentinel: Ensure the last (or offset from the last) parameter is an
explicit NULL (useful for variadics).

Others that might be useful are:
- noinline: Prevent function from being inlined.
- noclone: Don't create multiple versions of the (code for the) function.

I just was wondering what you guys think.  Would you be interested in a
patch that added these, provided it would gracefully degrade?

Would it be beneficial to have these macros, and is it worth the "bloat"?

What should they be named?
My current best idea is G_ATTR_*.

Or do you think we should just stick with the G_GNUC_* ones? (clang
pretends to be GCC so these work there too)

[1] https://developer.gnome.org/glib/2.38/glib-Miscellaneous-Macros.html
[2] http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html



Attachment: signature.asc
Description: OpenPGP digital signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]