From 6bb49105c62a27eced91b2fba7b546be5b94d9b0 Mon Sep 17 00:00:00 2001 From: Ting-Yu Lin Date: Mon, 18 Dec 2023 18:18:28 +0000 Subject: [PATCH] Bug 1870572 - Use constexpr variables instead of macro to define layout constants. r=layout-reviewers,emilio Here are some benefits to define those constants in constexpr variables: 1. Better assertion messages. For example, without this patch, the assertion in bug 1870103 looks like: ``` Assertion failure: cbSize.BSize(wm) == nscoord((1 << 30) - 1) ``` With this patch, it looks like: ``` Assertion failure: cbSize.BSize(wm) == NS_UNCONSTRAINEDSIZE ``` 2. We can use those constants in a debugger. This patch doesn't change behavior. Differential Revision: https://phabricator.services.mozilla.com/D196708 --- gfx/src/nsCoord.h | 6 +++--- gfx/src/nsSize.h | 2 +- layout/base/LayoutConstants.h | 9 ++++----- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/gfx/src/nsCoord.h b/gfx/src/nsCoord.h index 9d2241f15a98..6c2cd62be16a 100644 --- a/gfx/src/nsCoord.h +++ b/gfx/src/nsCoord.h @@ -25,9 +25,9 @@ * 96dpi as possible. */ -typedef int32_t nscoord; -#define nscoord_MAX nscoord((1 << 30) - 1) -#define nscoord_MIN (-nscoord_MAX) +using nscoord = int32_t; +inline constexpr nscoord nscoord_MAX = (1 << 30) - 1; +inline constexpr nscoord nscoord_MIN = -nscoord_MAX; namespace mozilla { struct AppUnit {}; diff --git a/gfx/src/nsSize.h b/gfx/src/nsSize.h index b80d1bde74a1..df1fa5570e46 100644 --- a/gfx/src/nsSize.h +++ b/gfx/src/nsSize.h @@ -12,7 +12,7 @@ #include "mozilla/gfx/Point.h" // Maximum allowable size -#define NS_MAXSIZE nscoord_MAX +inline constexpr nscoord NS_MAXSIZE = nscoord_MAX; typedef mozilla::gfx::IntSize nsIntSize; diff --git a/layout/base/LayoutConstants.h b/layout/base/LayoutConstants.h index d976afe99b4d..e8c7bb4221b9 100644 --- a/layout/base/LayoutConstants.h +++ b/layout/base/LayoutConstants.h @@ -10,7 +10,6 @@ #define LayoutConstants_h___ #include "mozilla/EnumSet.h" -#include "nsSize.h" // for NS_MAXSIZE #include "Units.h" /** @@ -20,15 +19,15 @@ * values, so user should not depend on the underlying numeric values. If * new specific use cases arise, define a new constant here. */ -#define NS_UNCONSTRAINEDSIZE NS_MAXSIZE +inline constexpr nscoord NS_UNCONSTRAINEDSIZE = nscoord_MAX; // NS_AUTOOFFSET is assumed to have the same value as NS_UNCONSTRAINEDSIZE. -#define NS_AUTOOFFSET NS_UNCONSTRAINEDSIZE +inline constexpr nscoord NS_AUTOOFFSET = NS_UNCONSTRAINEDSIZE; // +1 is to avoid clamped huge margin values being processed as auto margins -#define NS_AUTOMARGIN (NS_UNCONSTRAINEDSIZE + 1) +inline constexpr nscoord NS_AUTOMARGIN = NS_UNCONSTRAINEDSIZE + 1; -#define NS_INTRINSIC_ISIZE_UNKNOWN nscoord_MIN +inline constexpr nscoord NS_INTRINSIC_ISIZE_UNKNOWN = nscoord_MIN; namespace mozilla {