[Bf-blender-cvs] [ba20da7214e] blender-v2.90-release: Cleanup: avoid debug-only includes for BLI_assert.h

Campbell Barton noreply at git.blender.org
Thu Aug 6 14:50:21 CEST 2020


Commit: ba20da7214e0a71e843b0a6a8b11e0173599a0f0
Author: Campbell Barton
Date:   Thu Aug 6 20:17:44 2020 +1000
Branches: blender-v2.90-release
https://developer.blender.org/rBba20da7214e0a71e843b0a6a8b11e0173599a0f0

Cleanup: avoid debug-only includes for BLI_assert.h

Having includes in debug builds makes it possible to accidentally
break release builds.

Avoid this by moving calls to other modules out of BLI_assert.h
into BLI_assert.c

===================================================================

M	source/blender/blenlib/BLI_alloca.h
M	source/blender/blenlib/BLI_assert.h
M	source/blender/blenlib/CMakeLists.txt
A	source/blender/blenlib/intern/BLI_assert.c
M	source/blender/gpencil_modifiers/intern/MOD_gpenciltime.c
M	source/blender/makesdna/intern/CMakeLists.txt
M	source/blender/makesdna/intern/makesdna.c
M	source/blender/makesrna/intern/makesrna.c

===================================================================

diff --git a/source/blender/blenlib/BLI_alloca.h b/source/blender/blenlib/BLI_alloca.h
index 5297296b7ef..258e788ac12 100644
--- a/source/blender/blenlib/BLI_alloca.h
+++ b/source/blender/blenlib/BLI_alloca.h
@@ -25,6 +25,8 @@
 
 /* BLI_array_alloca / alloca */
 
+#include <stdlib.h>
+
 #if defined(__GNUC__) || defined(__clang__)
 #  if defined(__cplusplus) && (__cplusplus > 199711L)
 #    define BLI_array_alloca(arr, realsize) (decltype(arr)) alloca(sizeof(*arr) * (realsize))
diff --git a/source/blender/blenlib/BLI_assert.h b/source/blender/blenlib/BLI_assert.h
index 603be115b35..7978f04dd9d 100644
--- a/source/blender/blenlib/BLI_assert.h
+++ b/source/blender/blenlib/BLI_assert.h
@@ -30,58 +30,33 @@
 extern "C" {
 #endif
 
-#ifndef NDEBUG /* for BLI_assert */
-#  include <stdio.h>
-#endif
+/* Utility functions. */
+void _BLI_assert_print_pos(const char *file, const int line, const char *function, const char *id);
+void _BLI_assert_print_backtrace(void);
+void _BLI_assert_abort(void);
 
 #ifdef _MSC_VER
 #  include <crtdbg.h> /* for _STATIC_ASSERT */
 #endif
 
-/* BLI_assert(), default only to print
- * for aborting need to define WITH_ASSERT_ABORT
- */
-/* For 'abort' only. */
-#include <stdlib.h>
-
 #ifndef NDEBUG
-#  include "BLI_system.h"
 /* _BLI_ASSERT_PRINT_POS */
 #  if defined(__GNUC__)
-#    define _BLI_ASSERT_PRINT_POS(a) \
-      fprintf(stderr, \
-              "BLI_assert failed: %s:%d, %s(), at \'%s\'\n", \
-              __FILE__, \
-              __LINE__, \
-              __func__, \
-              #a)
+#    define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, __func__, #    a)
 #  elif defined(_MSC_VER)
-#    define _BLI_ASSERT_PRINT_POS(a) \
-      fprintf(stderr, \
-              "BLI_assert failed: %s:%d, %s(), at \'%s\'\n", \
-              __FILE__, \
-              __LINE__, \
-              __FUNCTION__, \
-              #a)
+#    define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, __func__, #    a)
 #  else
-#    define _BLI_ASSERT_PRINT_POS(a) \
-      fprintf(stderr, "BLI_assert failed: %s:%d, at \'%s\'\n", __FILE__, __LINE__, #a)
+#    define _BLI_ASSERT_PRINT_POS(a) _BLI_assert_print_pos(__FILE__, __LINE__, "<?>", #    a)
 #  endif
 /* _BLI_ASSERT_ABORT */
 #  ifdef WITH_ASSERT_ABORT
-#    ifdef __GNUC__
-/* Cast to remove 'noreturn' attribute since this suppresses missing return statements,
- * allowing changes to debug builds to accidentally to break release builds. */
-#      define _BLI_ASSERT_ABORT ((void (*)(void))(*(((void **)abort))))
-#    else
-#      define _BLI_ASSERT_ABORT abort
-#    endif
+#    define _BLI_ASSERT_ABORT _BLI_assert_abort
 #  else
 #    define _BLI_ASSERT_ABORT() (void)0
 #  endif
 /* BLI_assert */
 #  define BLI_assert(a) \
-    (void)((!(a)) ? ((BLI_system_backtrace(stderr), \
+    (void)((!(a)) ? ((_BLI_assert_print_backtrace(), \
                       _BLI_ASSERT_PRINT_POS(a), \
                       _BLI_ASSERT_ABORT(), \
                       NULL)) : \
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index d7b279c9bbf..9703c78e19c 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -37,6 +37,7 @@ set(INC_SYS
 set(SRC
   intern/BLI_args.c
   intern/BLI_array.c
+  intern/BLI_assert.c
   intern/BLI_dial_2d.c
   intern/BLI_dynstr.c
   intern/BLI_filelist.c
diff --git a/source/blender/blenlib/intern/BLI_assert.c b/source/blender/blenlib/intern/BLI_assert.c
new file mode 100644
index 00000000000..dc22d035459
--- /dev/null
+++ b/source/blender/blenlib/intern/BLI_assert.c
@@ -0,0 +1,51 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup bli
+ *
+ * Helper functions for BLI_assert.h header.
+ */
+
+#include "BLI_assert.h" /* Own include. */
+#include "BLI_system.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void _BLI_assert_print_pos(const char *file, int line, const char *function, const char *id)
+{
+  fprintf(stderr, "BLI_assert failed: %s:%d, %s(), at \'%s\'\n", file, line, function, id);
+}
+
+void _BLI_assert_print_backtrace(void)
+{
+#ifndef NDEBUG
+  BLI_system_backtrace(stderr);
+#endif
+}
+
+/**
+ * Wrap to remove 'noreturn' attribute since this suppresses missing return statements,
+ * allowing changes to debug builds to accidentally to break release builds.
+ *
+ * For example `BLI_assert(0);` at the end of a function that returns a value,
+ * will hide that it's missing a return.
+ */
+void _BLI_assert_abort(void)
+{
+  abort();
+}
diff --git a/source/blender/gpencil_modifiers/intern/MOD_gpenciltime.c b/source/blender/gpencil_modifiers/intern/MOD_gpenciltime.c
index 49396f56d26..315f1b9e19b 100644
--- a/source/blender/gpencil_modifiers/intern/MOD_gpenciltime.c
+++ b/source/blender/gpencil_modifiers/intern/MOD_gpenciltime.c
@@ -22,6 +22,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "BLI_utildefines.h"
diff --git a/source/blender/makesdna/intern/CMakeLists.txt b/source/blender/makesdna/intern/CMakeLists.txt
index 737ea9a7e12..9808740e030 100644
--- a/source/blender/makesdna/intern/CMakeLists.txt
+++ b/source/blender/makesdna/intern/CMakeLists.txt
@@ -35,6 +35,7 @@ blender_include_dirs(
 set(SRC
   dna_utils.c
   makesdna.c
+  ../../blenlib/intern/BLI_assert.c
   ../../blenlib/intern/BLI_ghash.c
   ../../blenlib/intern/BLI_ghash_utils.c
   ../../blenlib/intern/BLI_memarena.c
@@ -126,6 +127,7 @@ set(INC_SYS
 )
 
 set(SRC
+  ../../blenlib/intern/BLI_assert.c
   ../../blenlib/intern/BLI_ghash.c
   ../../blenlib/intern/BLI_ghash_utils.c
   ../../blenlib/intern/BLI_linklist.c
diff --git a/source/blender/makesdna/intern/makesdna.c b/source/blender/makesdna/intern/makesdna.c
index 9d2fecb123b..9875d997916 100644
--- a/source/blender/makesdna/intern/makesdna.c
+++ b/source/blender/makesdna/intern/makesdna.c
@@ -54,6 +54,7 @@
 #include "BLI_ghash.h"
 #include "BLI_memarena.h"
 #include "BLI_sys_types.h" /* for intptr_t support */
+#include "BLI_system.h"    /* for 'BLI_system_backtrace' stub. */
 #include "BLI_utildefines.h"
 
 #include "dna_utils.h"
diff --git a/source/blender/makesrna/intern/makesrna.c b/source/blender/makesrna/intern/makesrna.c
index 045d098bf6a..b7f0fb87536 100644
--- a/source/blender/makesrna/intern/makesrna.c
+++ b/source/blender/makesrna/intern/makesrna.c
@@ -29,6 +29,7 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "BLI_system.h" /* for 'BLI_system_backtrace' stub. */
 #include "BLI_utildefines.h"
 
 #include "RNA_define.h"



More information about the Bf-blender-cvs mailing list