Bug 1562787 - Improve style struct size assertion messages. r=boris
authorEmilio Cobos Álvarez <emilio@crisal.io>
Tue, 02 Jul 2019 18:14:19 +0000
changeset 480994 e1cc6ea96d6cb75b33520f81e06bfedc2f0c99bd
parent 480993 c1b80824ae8885c5caef8401813da38d500f853b
child 480995 6525ac41c35455512bc1b681df331a5b74cb75cc
push id36230
push useraciure@mozilla.com
push dateWed, 03 Jul 2019 04:09:04 +0000
treeherdermozilla-central@42a9ef2a777f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersboris
bugs1562787
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1562787 - Improve style struct size assertion messages. r=boris This will display something like: ``` error: static_assert failed due to requirement '528UL <= 504UL' "Style struct became larger than the size limit" static_assert(Actual <= Limit, "Style struct became larger than the size limit"); ^ ~~~~~~~~~~~~~~~ note: in instantiation of template class 'AssertSizeIsLessThan<nsStylePosition, 528, 504>' requested here STYLE_STRUCT_RESET(Position) ^ note: expanded from macro 'STYLE_STRUCT_RESET' ^ note: expanded from macro 'STYLE_STRUCT' static_assert(AssertSizeIsLessThan<nsStyle##name_, sizeof(nsStyle##name_), kStyleStructSizeLimit>::instantiate, ""); ``` Which includes both the size, the limit, and the struct name, as opposed to the current: ``` error: static_assert failed due to requirement 'sizeof(nsStylePosition) <= kStyleStructSizeLimit' "nsStylePosition became larger than the size limit" STYLE_STRUCT_RESET(Position) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ note: expanded from macro 'STYLE_STRUCT_RESET' ^~~~~~~~~~~~~~~~~~ note: expanded from macro 'STYLE_STRUCT' static_assert(sizeof(nsStyle##name_) <= kStyleStructSizeLimit, \ ``` Which only includes the name and thus isn't very useful. Differential Revision: https://phabricator.services.mozilla.com/D36546
layout/style/nsStyleStruct.cpp
--- a/layout/style/nsStyleStruct.cpp
+++ b/layout/style/nsStyleStruct.cpp
@@ -47,19 +47,29 @@ using namespace mozilla;
 using namespace mozilla::dom;
 
 static const nscoord kMediumBorderWidth = nsPresContext::CSSPixelsToAppUnits(3);
 
 // We set the size limit of style structs to 504 bytes so that when they
 // are allocated by Servo side with Arc, the total size doesn't exceed
 // 512 bytes, which minimizes allocator slop.
 static constexpr size_t kStyleStructSizeLimit = 504;
-#define STYLE_STRUCT(name_)                                      \
-  static_assert(sizeof(nsStyle##name_) <= kStyleStructSizeLimit, \
-                "nsStyle" #name_ " became larger than the size limit");
+
+template <typename Struct, size_t Actual, size_t Limit>
+struct AssertSizeIsLessThan {
+  static_assert(Actual == sizeof(Struct), "Bogus invocation");
+  static_assert(Actual <= Limit,
+                "Style struct became larger than the size limit");
+  static constexpr bool instantiate = true;
+};
+
+#define STYLE_STRUCT(name_)                                                  \
+  static_assert(AssertSizeIsLessThan<nsStyle##name_, sizeof(nsStyle##name_), \
+                                     kStyleStructSizeLimit>::instantiate,    \
+                "");
 #include "nsStyleStructList.h"
 #undef STYLE_STRUCT
 
 static bool DefinitelyEqualImages(const nsStyleImageRequest* aRequest1,
                                   const nsStyleImageRequest* aRequest2) {
   if (aRequest1 == aRequest2) {
     return true;
   }