Bug 392055 - NS_ENSURE_SUCCESS should print out the error code. r=benjamin, sr=bzbarsky, a=bzbarsky
authorsdwilsh@shawnwilsher.com
Sun, 23 Sep 2007 19:04:47 -0700
changeset 6246 d39b39e78f886be098b7a95688349bc8bca60408
parent 6245 3c9f133941238a51b3326e3141b7d9e09fa8b907
child 6247 81b50b2f2eceef0ab4855d917208fd26d914ea02
push id1
push userbsmedberg@mozilla.com
push dateThu, 20 Mar 2008 16:49:24 +0000
treeherdermozilla-central@61007906a1f8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbenjamin, bzbarsky, bzbarsky
bugs392055
milestone1.9a9pre
Bug 392055 - NS_ENSURE_SUCCESS should print out the error code. r=benjamin, sr=bzbarsky, a=bzbarsky
xpcom/glue/nsDebug.h
--- a/xpcom/glue/nsDebug.h
+++ b/xpcom/glue/nsDebug.h
@@ -45,16 +45,17 @@
 #ifndef nsError_h__
 #include "nsError.h"
 #endif 
 
 #include "nsXPCOM.h"
 
 #ifdef DEBUG
 #define NS_DEBUG
+#include "prprf.h"
 #endif
 
 #ifdef DEBUG
 
 /**
  * Abort the execution of the program if the expression evaluates to
  * false.
  *
@@ -198,18 +199,39 @@
 
 #define NS_ENSURE_FALSE(x, ret)                               \
   NS_ENSURE_TRUE(!(x), ret)
 
 /******************************************************************************
 ** Macros for checking results
 ******************************************************************************/
 
+#ifdef DEBUG
+/* So, if you are wondering why on Earth would anyone need to store res in __rv,
+ * please consider the case where res is a function call.  We clearly only want
+ * to call this function once, so we store its result in __rv.
+ */
+#define NS_ENSURE_SUCCESS(res, ret)                                       \
+  PR_BEGIN_MACRO                                                          \
+  nsresult __rv = res;                                                    \
+  if (NS_FAILED(__rv)) {                                                  \
+    static const char format[] = "NS_ENSURE_SUCCESS(" #res ", " #ret ") " \
+      "failed with result 0x%X";                                          \
+    char *msg = PR_smprintf(format, __rv);                                \
+    NS_WARNING(msg);                                                      \
+    PR_smprintf_free(msg);                                                \
+    return ret;                                                           \
+  }                                                                       \
+  PR_END_MACRO
+#else
 #define NS_ENSURE_SUCCESS(res, ret) \
-  NS_ENSURE_TRUE(NS_SUCCEEDED(res), ret)
+  PR_BEGIN_MACRO                    \
+  if (NS_FAILED(res)) return ret;   \
+  PR_END_MACRO
+#endif
 
 /******************************************************************************
 ** Macros for checking state and arguments upon entering interface boundaries
 ******************************************************************************/
 
 #define NS_ENSURE_ARG(arg)                                    \
   NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_ARG)