freetype-commit
[Top][All Lists]
Advanced

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

[freetype2] wl/cplusplus-casts 3798732: Provide C++ versions for public


From: Werner Lemberg
Subject: [freetype2] wl/cplusplus-casts 3798732: Provide C++ versions for public macros with casts.
Date: Sun, 14 Nov 2021 04:26:16 -0500 (EST)

branch: wl/cplusplus-casts
commit 37987329d7bab195ae7aa0f59452742fdc098455
Author: Werner Lemberg <wl@gnu.org>
Commit: Werner Lemberg <wl@gnu.org>

    Provide C++ versions for public macros with casts.
    
    Many FreeType clients use C++.  However `g++ -Wold-style-cast` warns for
    macros with C-style casts even for system header files; this also affects
    directories included with `-isystem`.  While this could be seen as a problem
    with g++, the problem is more a philosophical one: Over the time, C and C++
    diverged more and more, and some features of C are no longer the 'right'
    solution in C++.
    
    * include/freetype/config/public-macros.h (FT_STATIC_CAST,
    FT_REINTERPRET_CAST): New macros.
    
    * include/freetype/freetype.h (FT_ENC_TAG, FT_LOAD_TARGET_,
    FT_LOAD_TARGET_MODE): Use `FT_STATIC_CAST`.
    * include/freetype/ftimage.h (FT_IMAGE_TAG): Ditto.
    * include/freetype/fttypes.h (FT_MAKE_TAG, FT_BOOL): Ditto.
    * include/freetype/ftmodapi.h (FT_FACE_DRIVER_NAME): Use
    `FT_REINTERPRET_CAST`.
    
    * src/smooth/ftgrays.c (FT_STATIC_CAST)[STANDALONE_]: New macro.
    [!STANDALONE]: Include `FT_CONFIG_CONFIG_H`.
---
 include/freetype/config/public-macros.h | 13 +++++++++++++
 include/freetype/freetype.h             | 16 +++++++++-------
 include/freetype/ftimage.h              | 17 +++++++----------
 include/freetype/ftmodapi.h             |  5 +++--
 include/freetype/fttypes.h              | 14 +++++++-------
 src/smooth/ftgrays.c                    |  4 ++++
 6 files changed, 43 insertions(+), 26 deletions(-)

diff --git a/include/freetype/config/public-macros.h 
b/include/freetype/config/public-macros.h
index 51fbc9c..1b03640 100644
--- a/include/freetype/config/public-macros.h
+++ b/include/freetype/config/public-macros.h
@@ -103,6 +103,7 @@ FT_BEGIN_HEADER
    */
 #define FT_EXPORT( x )  FT_PUBLIC_FUNCTION_ATTRIBUTE extern x
 
+
   /*
    * `FT_UNUSED` indicates that a given parameter is not used -- this is
    * only used to get rid of unpleasant compiler warnings.
@@ -115,6 +116,18 @@ FT_BEGIN_HEADER
 #endif
 
 
+  /*
+   * Support for casts in both C and C++.
+   */
+#ifdef __cplusplus
+#define FT_STATIC_CAST( type )       static_cast<type>
+#define FT_REINTERPRET_CAST( type )  reinterpret_cast<type>
+#else
+#define FT_STATIC_CAST( type )       (type)
+#define FT_REINTERPRET_CAST( type )  (type)
+#endif
+
+
 FT_END_HEADER
 
 #endif  /* FREETYPE_CONFIG_PUBLIC_MACROS_H_ */
diff --git a/include/freetype/freetype.h b/include/freetype/freetype.h
index b457183..e45c702 100644
--- a/include/freetype/freetype.h
+++ b/include/freetype/freetype.h
@@ -616,11 +616,12 @@ FT_BEGIN_HEADER
    */
 
 #ifndef FT_ENC_TAG
-#define FT_ENC_TAG( value, a, b, c, d )         \
-          value = ( ( (FT_UInt32)(a) << 24 ) |  \
-                    ( (FT_UInt32)(b) << 16 ) |  \
-                    ( (FT_UInt32)(c) <<  8 ) |  \
-                      (FT_UInt32)(d)         )
+
+#define FT_ENC_TAG( value, a, b, c, d )                        \
+          value = ( ( FT_STATIC_CAST( FT_UInt32 )(a) << 24 ) | \
+                    ( FT_STATIC_CAST( FT_UInt32 )(b) << 16 ) | \
+                    ( FT_STATIC_CAST( FT_UInt32 )(c) <<  8 ) | \
+                      FT_STATIC_CAST( FT_UInt32 )(d)         )
 
 #endif /* FT_ENC_TAG */
 
@@ -3181,7 +3182,7 @@ FT_BEGIN_HEADER
    *   necessary to empty the cache after a mode switch to avoid false hits.
    *
    */
-#define FT_LOAD_TARGET_( x )   ( (FT_Int32)( (x) & 15 ) << 16 )
+#define FT_LOAD_TARGET_( x )   ( FT_STATIC_CAST( FT_Int32 )( (x) & 15 ) << 16 )
 
 #define FT_LOAD_TARGET_NORMAL  FT_LOAD_TARGET_( FT_RENDER_MODE_NORMAL )
 #define FT_LOAD_TARGET_LIGHT   FT_LOAD_TARGET_( FT_RENDER_MODE_LIGHT  )
@@ -3200,7 +3201,8 @@ FT_BEGIN_HEADER
    *   @FT_LOAD_TARGET_XXX value.
    *
    */
-#define FT_LOAD_TARGET_MODE( x )  ( (FT_Render_Mode)( ( (x) >> 16 ) & 15 ) )
+#define FT_LOAD_TARGET_MODE( x )                                 \
+          FT_STATIC_CAST( FT_Render_Mode )( ( (x) >> 16 ) & 15 )
 
 
   /**************************************************************************
diff --git a/include/freetype/ftimage.h b/include/freetype/ftimage.h
index 66a8b89..04b5db5 100644
--- a/include/freetype/ftimage.h
+++ b/include/freetype/ftimage.h
@@ -28,11 +28,6 @@
 #define FTIMAGE_H_
 
 
-  /* STANDALONE_ is from ftgrays.c */
-#ifndef STANDALONE_
-#endif
-
-
 FT_BEGIN_HEADER
 
 
@@ -700,11 +695,13 @@ FT_BEGIN_HEADER
    *   to get a simple enumeration without assigning special numbers.
    */
 #ifndef FT_IMAGE_TAG
-#define FT_IMAGE_TAG( value, _x1, _x2, _x3, _x4 )  \
-          value = ( ( (unsigned long)_x1 << 24 ) | \
-                    ( (unsigned long)_x2 << 16 ) | \
-                    ( (unsigned long)_x3 << 8  ) | \
-                      (unsigned long)_x4         )
+
+#define FT_IMAGE_TAG( value, _x1, _x2, _x3, _x4 )                      \
+          value = ( ( FT_STATIC_CAST( unsigned long )( _x1 ) << 24 ) | \
+                    ( FT_STATIC_CAST( unsigned long )( _x2 ) << 16 ) | \
+                    ( FT_STATIC_CAST( unsigned long )( _x3 ) << 8  ) | \
+                      FT_STATIC_CAST( unsigned long )( _x4 )         )
+
 #endif /* FT_IMAGE_TAG */
 
 
diff --git a/include/freetype/ftmodapi.h b/include/freetype/ftmodapi.h
index cb15423..9d6c1b3 100644
--- a/include/freetype/ftmodapi.h
+++ b/include/freetype/ftmodapi.h
@@ -347,8 +347,9 @@ FT_BEGIN_HEADER
    *   2.11
    *
    */
-#define FT_FACE_DRIVER_NAME( face ) \
-          ( ( *(FT_Module_Class**)( ( face )->driver ) )->module_name )
+#define FT_FACE_DRIVER_NAME( face )                     \
+          ( ( *FT_REINTERPRET_CAST( FT_Module_Class** ) \
+                 ( ( face )->driver ) )->module_name )
 
 
   /**************************************************************************
diff --git a/include/freetype/fttypes.h b/include/freetype/fttypes.h
index 37f7353..dfeaf74 100644
--- a/include/freetype/fttypes.h
+++ b/include/freetype/fttypes.h
@@ -485,12 +485,12 @@ FT_BEGIN_HEADER
    *   The produced values **must** be 32-bit integers.  Don't redefine this
    *   macro.
    */
-#define FT_MAKE_TAG( _x1, _x2, _x3, _x4 ) \
-          (FT_Tag)                        \
-          ( ( (FT_ULong)_x1 << 24 ) |     \
-            ( (FT_ULong)_x2 << 16 ) |     \
-            ( (FT_ULong)_x3 <<  8 ) |     \
-              (FT_ULong)_x4         )
+#define FT_MAKE_TAG( _x1, _x2, _x3, _x4 )                 \
+          FT_STATIC_CAST( FT_Tag )                        \
+          ( ( FT_STATIC_CAST( FT_ULong )( _x1 ) << 24 ) | \
+            ( FT_STATIC_CAST( FT_ULong )( _x2 ) << 16 ) | \
+            ( FT_STATIC_CAST( FT_ULong )( _x3 ) <<  8 ) | \
+              FT_STATIC_CAST( FT_ULong )( _x4 )         )
 
 
   /*************************************************************************/
@@ -588,7 +588,7 @@ FT_BEGIN_HEADER
 
 
 #define FT_IS_EMPTY( list )  ( (list).head == 0 )
-#define FT_BOOL( x )  ( (FT_Bool)( (x) != 0 ) )
+#define FT_BOOL( x )         FT_STATIC_CAST( FT_Bool )( (x) != 0 )
 
   /* concatenate C tokens */
 #define FT_ERR_XCAT( x, y )  x ## y
diff --git a/src/smooth/ftgrays.c b/src/smooth/ftgrays.c
index 5978343..ef589ec 100644
--- a/src/smooth/ftgrays.c
+++ b/src/smooth/ftgrays.c
@@ -152,6 +152,8 @@
 #define ADD_INT( a, b )                                  \
           (int)( (unsigned int)(a) + (unsigned int)(b) )
 
+#define FT_STATIC_CAST( type )  (type)
+
 
 #define ft_memset   memset
 
@@ -273,6 +275,8 @@ typedef ptrdiff_t  FT_PtrDist;
 #else /* !STANDALONE_ */
 
 
+#include <ft2build.h>
+#include FT_CONFIG_CONFIG_H
 #include "ftgrays.h"
 #include <freetype/internal/ftobjs.h>
 #include <freetype/internal/ftdebug.h>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]