Commit Graph

87 Commits

Author SHA1 Message Date
Nirbheek Chauhan
92e3189613 gmacros.h: offsetof is also available on MSVC
All versions since Visual C++ 2005 have this available, so we can just
use it for G_STRUCT_OFFSET.

See: https://msdn.microsoft.com/en-us/library/dz4y9b9a.aspx
2016-07-13 11:44:34 +08:00
Chun-wei Fan
2ca496a2e7 glib/gmacros.h: Fix build on C++ mode in Visual Studio
Later Visual Studio versions does not allow one to define known keywords,
even if they are actually not known to the compiler.  Avoid this issue by
checking more conditions before we define inline as __inline:

-We are not building under C++ mode.
-We are on Visual Studio 2013 or earlier.

Where both of these conditions need to hold true.

https://bugzilla.gnome.org/show_bug.cgi?id=765990
2016-05-05 00:44:48 +08:00
Emmanuele Bassi
1964a8726b macros: Fix Clang attribute detection
For attributes we have to use __has_attribute().

https://bugzilla.gnome.org/show_bug.cgi?id=761843
2016-02-10 22:37:41 +00:00
Stephan Bergmann
b44fba25fb G_LIKELY/_UNLIKELY macros need more parentheses
...for cases like

  #include "glib.h"
  #define COMMA ,
  void f(void) { if (G_LIKELY(0 COMMA 1)); }

https://bugzilla.gnome.org/show_bug.cgi?id=760215
2016-01-06 10:33:04 -05:00
Christian Hergert
3272267b99 macros: add G_GNUC_CHECK_VERSION() for compiler checks.
https://bugzilla.gnome.org/show_bug.cgi?id=728099
2015-12-16 07:47:53 -05:00
Allison Ryan Lortie
7bc6f021d7 gmacros: fix unguarded use of __STDC_VERSION__
According to the C spec, any undefined identifier used in a #if
expression is taken to have a numerical value of zero.

Commit db2367e878 introduced an #i
statement which depended on this behaviour.

gcc has a -Wundef option which warns about depending on this behaviour,
and unfortunately there are projects that are using -Werror=undef in
builds that include our headers.

Adding a check for defined(__STDC_VERSION__) before using the macro is
enough to silence gcc.
2015-11-17 13:43:49 -05:00
Allison Ryan Lortie
db2367e878 GLib: clean up the "inline" mess once and for all
It's been a long time since we've been unconditionally saying "static
inline" in GLib headers without complaints so it's safe to assume that
all compilers that we care about support this.

One thing that is not yet totally supported is the unadorned use of the
word "inline".  Depending on the flags (-std=c89, for example), even GCC
will complain about this.  Detect missing C99 support and define
"inline" to "__inline" in that case.  Some research shows "__inline"
appears to be the most widely-supported keyword here, but we may need to
tweak this if we get some reports of breakage.

Clean up all of the configure checks around this and define G_CAN_INLINE
unconditionally.  Unfortunately, we must assume that some people are
still using G_IMPLEMENT_INLINES, we must continue to implement that
(including undefining G_CAN_INLINE and redefining G_INLINE_FUNC) if
requested.

It is not our intent to break existing users of the old-style
G_INLINE_FUNC approach and if that has happened, we may need to make
some further adjustments.

https://bugzilla.gnome.org/show_bug.cgi?id=757374
2015-11-16 13:14:19 -05:00
Allison Ryan Lortie
89bda59170 macros: add dummy __has_builtin()
Add a dummy definition for Clang's __has_builtin() macro.  This will
allow us to use __has_builtin() unconditionally, in the same way as we
already do for __has_feature().

https://bugzilla.gnome.org/show_bug.cgi?id=503096
2015-10-30 11:58:49 +01:00
Ting-Wei Lan
d7a1d890c1 gmacros: Only set G_ANALYZER_ANALYZING to 1 when clang static analyzer is in use
We set G_ANALYZER_ANALYZING to 1 when clang supporting static analyzing before,
but this will cause compilation error when -Werror=return-type is used and the
static analyzer is not in use because g_error static function only has
__attribute__((analyzer_noreturn)), which is useless for normal compilation.

https://bugzilla.gnome.org/show_bug.cgi?id=741901
2015-06-29 04:11:39 +08:00
Behdad Esfahbod
46779a3122 Remove UTF-8 quotation marks
https://bugzilla.gnome.org/show_bug.cgi?id=747772
2015-04-13 12:30:16 -07:00
Matthias Clasen
b0e330b68c Fix the build
The world would be a better place if gcc had __has_feature(), too.
2015-03-22 15:05:13 -04:00
Matthias Clasen
42870201f8 Account for clangs lack of __alloc_size__
clang's emulation of gcc 4.3 is not perfect, despite its
pretending that it is.

https://bugzilla.gnome.org/show_bug.cgi?id=745821
2015-03-22 14:20:00 -04:00
Colin Walters
d0105f1c08 Add g_autofree
The g_autoptr() being associated with the type name works out really
well for things like GHashTable.  However, it's a bit more awkward to
associate with "gchar".  Also because one can't use "char".
Similarly, there are a lot of other "bare primitive array" types that
one might reasonably use.

This patch does not remove the autoptr for "gchar", even though I
think it's rather awkward and strange.

Also while we're here, add a test case for the cleanup bits.

https://bugzilla.gnome.org/show_bug.cgi?id=744747
2015-02-22 22:18:07 -05:00
Chun-wei Fan
696db75615 gmacros.h: Add Private Macro _GLIB_DEFINE_AUTOPTR_CHAINUP
This is necessary as we are using _GLIB_AUTOPTR_TYPENAME and
_GLIB_AUTOPTR_FUNC_NAME in gtype.h for G_DECLARE_DERIVABLE_TYPE and
G_DECLARE_FINAL_TYPE, but _GLIB_AUTOPTR_TYPENAME and
_GLIB_AUTOPTR_FUNC_NAME expand to nothing on non-GCC, causing builds on
non-GCC to break, due to bad typedef and function definitions.

This patch defines a new private macro which does what is needed on GCC
builds and does nothing on non-GCC builds, thus fixing the build.

https://bugzilla.gnome.org/show_bug.cgi?id=743640
2015-02-03 18:11:30 +08:00
Ryan Lortie
57a49f6891 fix G_DEFINE_AUTO_CLEANUP_FREE_FUNC on non-GCC
Add the missing 'none' argument to this macro in the non-GCC case.  The
none parameter was added after the others and I forgot to update the
non-GCC case.

https://bugzilla.gnome.org/show_bug.cgi?id=743640
2015-02-02 09:17:44 +01:00
Ryan Lortie
2596919c58 macros: add support for GNUC cleanup __attribute__
Add g_auto() and g_autoptr() as helpers for declaring variables with
automatic cleanup.

Add some macros to help types define cleanup functions for themselves.

Going forward it will be an expectation that people use this macro when
creating a new type, even if they do not intend to use the auto-cleanup
functionality for themselves.

These new macros only work on GCC and clang, which is why we resisted
adding them for so long.  There exist many people who are only
interested in writing programs for these compilers, however, and a
similar API in libgsystem has proven to be extremely popular, so let's
expose this functionality to an even wider audience.

We ignore deprecation warnings when emitting the free functions, which
seems suspicious.  The reason that we do this is not because we want to
call deprecated functions, but just the opposite: sometimes the free
function will be an _unref() function that is only AVAILABLE_IN newer
versions, and these warnings are also implemented as deprecation
warnings.

https://bugzilla.gnome.org/show_bug.cgi?id=743640
2015-01-30 16:49:53 +01:00
Paolo Borelli
7a8ef00aae Avoid warning when using G_STMT_END macro with MSVC
Workaround found on
http://cnicholson.net/2009/03/stupid-c-tricks-dowhile0-and-c4127/

https://bugzilla.gnome.org/show_bug.cgi?id=742851
2015-01-14 16:21:00 +01:00
Thomas Haller
35eaf037bd gmacros.h: add G_GNUC_*_IGNORE_DEPRECATIONS macros for clang
https://bugzilla.gnome.org/show_bug.cgi?id=734126

Signed-off-by: Thomas Haller <thaller@redhat.com>
2014-08-22 20:58:11 +02:00
Matthias Clasen
c575d24dfb Docs: Don't use the note tag
More markup avoidance.
2014-01-31 18:20:06 -05:00
Daniel Mustieles
078dbda148 Updated FSF's address 2014-01-31 14:31:55 +01:00
Dan Winship
dc172f0616 gmacros.h: fix C99 check
G_STRFUNC was checking __STDC_VERSION__ against the wrong value
(though it didn't actually matter, since __STDC_VERSION__ wasn't
defined in C90, so the check still only matched C99 and above anyway).
2013-11-02 12:23:30 -04:00
Stef Walter
409a6db349 Mark up warnings/critical functions for clang analyzer
The clang code analyzer needs to know that functions like g_error
g_critical an g_return_if_fail should be seen by the analyzer in the
same way as g_assert(). That is the analyzer should think they are
fatal.

https://bugzilla.gnome.org/show_bug.cgi?id=700268
2013-08-17 13:40:06 -04:00
Chun-wei Fan
d0b4f59e8c Add MSVC implementations of G_GNUC_*_IGNORE_DEPRECATIONS
As Visual Studio 2008 and later have support for the __pragma keyword,
where the compiler pragmas can be used in a macro, we can support
G_GNUC_BEGIN_IGNORE_DEPRECATIONS and G_GNUC_END_IGNORE_DEPRECATIONS
for Visual Studio 2008 and later, so many deprecation (C4996) warnings
can be suppressed when using these compilers when we use these macros
in the code.

https://bugzilla.gnome.org/show_bug.cgi?id=704543
2013-07-29 10:26:01 +08:00
Dan Winship
b7b3838401 Make G_STRFUNC consistent across platforms
Recent versions of clang have changed __PRETTY_FUNCTION__ to always
include the function signature (rather than including the function
signature in C++ but not in C like gcc does). This causes G_STRFUNC to
give different results under clang and gcc, causing some tests with
g_test_expect_messages() to fail.

Fix this by only using __PRETTY_FUNCTION__ in C++, and using
__FUNCTION__ in C. (Under gcc this change has no effect.)

https://bugzilla.gnome.org/show_bug.cgi?id=702147
2013-06-29 23:23:36 -04:00
Colin Walters
d1bbab5689 gmacros: Mark G_UNAVAILABLE() functions as deprecated (gcc <= 4.4)
RHEL6 ships with GCC 4.4 by default, which doesn't understand the
nicer deprecated attribute that takes a message.  However, we can at
least fall back to the old G_DEPRECATED, rather than silently doing
nothing.

This gives me warning messages when building OSTree on RHEL6 when I
accidentally added a usage of g_unix_fd_source_new().

https://bugzilla.gnome.org/show_bug.cgi?id=697160
2013-04-03 00:33:45 -04:00
Cosimo Cecchi
0ea7abaed5 gmacros: add G_GNUC_UNUSED to G_STATIC_ASSERT
Silence some warnings seen with GCC 4.8.
2013-01-30 17:59:09 +01:00
Ryan Lortie
b91c476827 Add a new _GLIB_EXTERN macro for "extern"
This macro simply evaluates the "extern" unless it has been explicitly
defined to something else.

All of the version macros (including the unversioned deprecation markers
and GLIB_AVAILABLE_IN_ALL) now include _GLIB_EXTERN as part of their
definition.

G_INLINE has also been modified to use _GLIB_EXTERN where appropriate.

This macro should never be used outside of the gmacros.h/gversonmacros.h
headers.

The effect of this patch is that "extern" has now been added to all
functions declared in installed headers.  Strictly speaking, this is
something we should have had all along...

GLIB_VAR and GOBJECT_VAR have also been modified to use _GLIB_EXTERN on
non-Windows, instead of "extern" which they were using before.  The
eventual goal is to use the normal version/deprecation macros on
exported variables and drop GLIB_VAR but we need to see how this will
work on Windows before we go ahead with that.

https://bugzilla.gnome.org/show_bug.cgi?id=688681
2013-01-13 13:13:36 -05:00
Matthias Clasen
e1b99b2ddc Move single-include guards inside include guards
gcc has optimizations for include guards that only work
if they are outermost in the the header.
https://bugzilla.gnome.org/show_bug.cgi?id=689810
2012-12-27 23:43:14 -05:00
Shota Akiyama
5f280eb6a8 glib/gmacros.h: Fix up attribute to avoid namespace collisions
https://bugzilla.gnome.org/show_bug.cgi?id=670751
2012-03-03 13:29:29 -05:00
Matthias Clasen
96c98ce62e Improve the wording of the message for G_UNAVAILABLE
The version number we have here is the one the function appeared
in, not the max-allowed version. Therefore 'unavailable before
VERSION' makes more sense than 'unavailable for VERSION'.
2012-02-27 06:54:16 -05:00
Emmanuele Bassi
34aeeb7d64 Add flexible API version boundaries
There are cases when it should be possible to define at compile time
what range of functions and types should be used, in order to get,
or restrict, the compiler warnings for deprecated or newly added
types or functions.

For instance, if GLib introduces a deprecation warning on a type in
version 2.32, application code can decide to specify the minimum and
maximum boundary of the used API to be 2.30; when compiling against
a new version of GLib, this would produce the following results:

  - all deprecations introduced prior to 2.32 would emit compiler
    warnings when used by the application code;
  - all deprecations introduced in 2.32 would not emit compiler
    warnings when used by the application code;
  - all new symbols introduced in 2.32 would emit a compiler warning.

Using this scheme it should be possible to have fairly complex
situations, like the following one:

  assuming that an application is compiled with:
    GLIB_VERSION_MIN_REQUIRED = GLIB_VERSION_2_30
    GLIB_VERSION_MAX_ALLOWED  = GLIB_VERSION_2_32

  and a GLib header containing:

    void function_A (void) GLIB_DEPRECATED_IN_2_26;
    void function_B (void) GLIB_DEPRECATED_IN_2_28;
    void function_C (void) GLIB_DEPRECATED_IN_2_30;
    void function_D (void) GLIB_AVAILABLE_IN_2_32;
    void function_E (void) GLIB_AVAILABLE_IN_2_34;

  any application code using the above functions will get the following
  compiler warnings:

    function_A: deprecated symbol warning
    function_B: deprecated symbol warning
    function_C: no warning
    function_D: no warning
    function_E: undefined symbol warning

This means that it should be possible to gradually port code towards
non-deprecated API gradually, on a per-release basis.

https://bugzilla.gnome.org/show_bug.cgi?id=670542
2012-02-26 23:58:41 -05:00
Filippo Della Betta
462f7f5a4d Added better support of G_STRFUNC for Visual Studio platform
https://bugzilla.gnome.org/show_bug.cgi?id=670128
2012-02-24 16:21:45 -05:00
Dan Winship
ca05902a58 Add G_GNUC_BEGIN/END_IGNORE_DEPRECATIONS
Add new macros to disable -Wdeprecated-declarations around a piece of
code, using the C99 (and GNU89) _Pragma() operator. Replace the
existing use of #pragma for this in gio, and suppress the warnings in
gvaluearray.c as well.

https://bugzilla.gnome.org/show_bug.cgi?id=669671
2012-02-15 09:54:38 -05:00
Alexander Larsson
968f4e8d79 Move constructor macros to an internal header and into generated code
With this we're not longer exporting the constructor headers, which means
we're not tying ourselves to a macro that might need special tweaking on
a compiler-by-compiler basis.
2012-01-30 16:59:27 +01:00
Kjartan Maraas
3fe710c0b5 Fix broken build with -Wundefined 2012-01-17 17:10:19 +01:00
Alexander Larsson
9ef4554a0b Add macros to handle constructor functions on the compilers that support it
This is only supported on some compilers, so we define G_HAS_CONSTRUCTORS
when it is supported. However, when it is supported we guarantee that
both constructors and destructors work, in executables as well as shared
libraries (including runtime unloading of shared libraries).

Usage is a bit unorthodox, as some compilers need to use #pragma to
implement constructors, and #pragma can't be used in macros.

The canonical way to use this:

  #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
  #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
  #endif
  G_DEFINE_CONSTRUCTOR(my_constructor)

  static void my_constructor (void)
  {
    ...

  #ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
  #pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(my_destructor)
  #endif
  G_DEFINE_DESTRUCTOR(my_destructor)

  static void my_destructor (void)
  {
    ...
2012-01-13 16:02:47 +01:00
Matthias Clasen
7455dd370e Make single includes mandatory
This has been the official line since 2.17, which seems plenty
long enough for a transition phase.
2011-10-12 00:25:38 -04:00
Chun-wei Fan
36ccdab760 Bug 661438: Fix up gmacros.h a bit
The macro here should be G_DEPRECATED, not G_GNUC_DEPRECATED.
2011-10-12 10:38:47 +08:00
Matthias Clasen
a1acf35653 Add our own deprecation macros
This will allow to suppress deprecation warnings on a per-module
basis, if needed. They are on by default now, though.
2011-10-11 13:42:58 -04:00
Matthias Clasen
0da0411a4a Add better deprecation macros
Unlike G_GNUC_... macros, the new G_DEPRECATED[_FOR] are
meant as abstractions that work with different compilers.
Using a new name also lets us restrict it to 'must be placed
before the declaration', which works with more compilers.

https://bugzilla.gnome.org/show_bug.cgi?id=661438
2011-10-11 13:13:27 -04:00
Matthias Clasen
97b0b36ae3 USE __COUNTER__ instead of __LINE__ when available
I've seen builds fail with

nm-connection.c:119:691: error: declaration of '_GStaticAssertCompileTimeAssertion_119' shadows a previous local [-Werror=shadow]

because several compile-time assertions ended up on the same
line. __COUNTER__ is meant specifically for the purpose of
constructing identifiers, so use it when available.
2011-10-07 01:19:15 -04:00
Colin Walters
c3133affcd gmacros: Hide G_STATIC_ASSERT from g-ir-scanner
The C syntax here is twisted and confuses the scanner.  We don't
need to see it, so let's just skip it.

https://bugzilla.gnome.org/show_bug.cgi?id=660413
2011-10-05 13:39:30 -04:00
Ryan Lortie
e517fb6cb0 Make G_ASSERT_STATIC work with clang
A simplified variant of an approach proposed by Behdad.

https://bugzilla.gnome.org/show_bug.cgi?id=660413
2011-10-04 11:52:47 -04:00
Ryan Lortie
fd772fbe95 Formally deprecate G_CONST_RETURN 2011-06-13 09:27:41 -04:00
Colin Walters
2300be5be3 Consistently use G_GNUC_EXTENSION instead of __extension__
g-ir-scanner doesn't like __extension__, and while I can add it, since
we have this macro, we should be using it consistently.
2011-05-31 10:35:38 -04:00
Ryan Lortie
c00ef0a17a Add G_STATIC_ASSERT_EXPR macro
https://bugzilla.gnome.org/show_bug.cgi?id=626549
2011-05-27 22:36:16 -04:00
Emmanuele Bassi
0c1acc7e74 Revert "Deprecate G_CONST_RETURN"
This reverts commit a7fc7909da.

The deprecations was not discussed except on Bugzilla:

https://bugzilla.gnome.org/show_bug.cgi?id=644611
2011-03-15 09:02:08 +00:00
Ryan Lortie
a7fc7909da Deprecate G_CONST_RETURN 2011-03-15 01:32:22 -04:00
Emmanuele Bassi
38e2273207 Add G_GNUC_DEPRECATED_FOR macro
It would be good, error reporting-wise, to be able to signal which
function should be used instead of a deprecated one. GCC 4.5 added an
optional "message" payload to the deprecated attribute, so that:

  void f1 (void) __attribute__((deprecated("Use f2 instead")));

Will expand to:

  warning: f1 is deprecated: Use f2 instead

Instead of just printing:

  warning: f1 is deprecated

Since we already have a G_GNUC_DEPRECATED macro we should provide a
G_GNUC_DEPRECATED_FOR macro defined as:

  G_GNUC_DEPRECATED_FOR(bar)

Which would expand the deprecation message to "Use bar instead"
automatically. The deprecation message should probably be similar
to what we use in gtk-doc to match up with the documentation.

https://bugzilla.gnome.org/show_bug.cgi?id=614965
2010-04-23 15:26:49 +01:00
Matthias Clasen
ed6c7f0e32 Use offsetof instead of __builtin_offsetof, for icc's sake. Proposed by
* glib/gmacros.h (G_STRUCT_OFFSETOF): Use offsetof instead of
        __builtin_offsetof, for icc's sake. Proposed by Hrvoje Niksic.



svn path=/trunk/; revision=7811
2009-01-18 01:03:00 +00:00