Bug 836438 - Always implement nsresult using an unsigned 32-bit integer type. r=ehsan

--HG--
extra : rebase_source : f9d9d7772decee36a27ef09eb018b9912bf7bf6b
This commit is contained in:
Jeff Walden 2013-01-30 15:32:44 -08:00
parent f28fd07803
commit 8f98ecc84c
6 changed files with 60 additions and 24 deletions

View File

@ -10,6 +10,8 @@
#include "mozilla/Attributes.h"
#if defined(__cplusplus)
#if defined(__clang__)
/*
* Per Clang documentation, "Note that marketing version numbers should not
@ -211,4 +213,6 @@
inline int& operator>>=(int&, const Name::Enum&) MOZ_DELETE;
#endif
#endif /* __cplusplus */
#endif /* mozilla_TypedEnum_h_ */

View File

@ -54,6 +54,10 @@ ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
CPPSRCS += nsMacUtilsImpl.cpp
endif
CSRCS = \
nsErrorAssertsC.c \
$(NULL)
EXPORTS = \
nsAgg.h \
nsAutoRef.h \

View File

@ -7,12 +7,9 @@
#define nsError_h__
#include "mozilla/Likely.h"
#include "mozilla/StandardInteger.h"
#include "mozilla/TypedEnum.h"
#ifndef nscore_h___
#include "nscore.h" /* needed for nsresult */
#endif
/*
* To add error code to your module, you need to do the following:
*
@ -120,16 +117,8 @@
* either can be converted to the other, so it's ambiguous. So we have to fall
* back to a regular enum.
*/
#if defined(__cplusplus)
#if defined(MOZ_HAVE_CXX11_STRONG_ENUMS)
typedef enum class tag_nsresult : uint32_t
#elif defined(MOZ_HAVE_CXX11_ENUM_TYPE)
/* Need underlying type for workaround of Microsoft compiler (Bug 794734) */
typedef enum tag_nsresult : uint32_t
#else
/* no strong enums */
typedef enum tag_nsresult
#endif
{
#undef ERROR
#define ERROR(key, val) key = val
@ -137,18 +126,44 @@
#undef ERROR
} nsresult;
#if defined(MOZ_HAVE_CXX11_STRONG_ENUMS)
/* We're using enum classes, so we need #define's to put the constants in
* global scope for compatibility with old code. */
/*
* enum classes don't place their initializers in the global scope, so we need
* #define's for compatibility with old code.
*/
#include "ErrorListCxxDefines.h"
#endif
#else /* defined(__cplusplus) */
/* C enum can't have a value outside the range of 'int'.
* C const can't initialize with an expression involving other variables
* even if it is const. So we have to fall back to bad old #defines. */
#elif defined(MOZ_HAVE_CXX11_ENUM_TYPE)
typedef enum tag_nsresult : uint32_t
{
#undef ERROR
#define ERROR(key, val) key = val
#include "ErrorList.h"
#undef ERROR
} nsresult;
#elif defined(__cplusplus)
/*
* We're C++ in an old compiler lacking enum classes *and* typed enums (likely
* gcc < 4.5.1 as clang/MSVC have long supported one or both), or compiler
* support is unknown. Yet nsresult must have unsigned 32-bit representation.
* So just make it a typedef, and implement the constants with global consts.
*/
typedef uint32_t nsresult;
const nsresult
#undef ERROR
#define ERROR(key, val) key = val
#include "ErrorList.h"
#undef ERROR
;
#else
/*
* C doesn't have any way to fix the type underlying an enum, and enum
* initializers can't have values outside the range of 'int'. So typedef
* nsresult to the correct unsigned type, and fall back to using #defines for
* all error constants.
*/
typedef uint32_t nsresult;
#include "ErrorListCDefines.h"
#endif /* defined(__cplusplus) */
#endif
#undef SUCCESS_OR_FAILURE
#undef SUCCESS

View File

@ -1,7 +1,10 @@
#include "nsError.h"
#include "mozilla/Assertions.h"
#include "nsError.h"
// No reason to pull in Assertions.h for every single file that includes
// nsError.h, so let's put this in its own .cpp file instead of in the .h.
MOZ_STATIC_ASSERT(((nsresult)0) < ((nsresult)-1),
"nsresult must be an unsigned type");
MOZ_STATIC_ASSERT(sizeof(nsresult) == sizeof(uint32_t),
"nsresult must be 32 bits");

View File

@ -0,0 +1,12 @@
#include "mozilla/Assertions.h"
#include "nsError.h"
/*
* No reason to pull in Assertions.h for every single file that includes
* nsError.h, so let's put this in its own .c file instead of in the .h.
*/
MOZ_STATIC_ASSERT(((nsresult)0) < ((nsresult)-1),
"nsresult must be an unsigned type");
MOZ_STATIC_ASSERT(sizeof(nsresult) == sizeof(uint32_t),
"nsresult must be 32 bits");

View File

@ -325,8 +325,6 @@ typedef unsigned long nsrefcnt;
typedef uint32_t nsrefcnt;
#endif
#include "nsError.h"
/* ------------------------------------------------------------------------ */
/* Casting macros for hiding C++ features from older compilers */