win: Fix undefined symbol with cef_sandbox and VS 17.9.2 (see #3708)

This avoids a bug in clang + MSVC STL when using the default three-way
comparison operator with base::TimeDelta. The compiler does not throw
away the function call to the `std::_Literal_zero_is_expected` symbol,
which is deliberately undefined.

See also https://github.com/microsoft/STL/issues/4359#issuecomment-2042911928
This commit is contained in:
Marshall Greenblatt 2024-06-03 13:49:31 -04:00
parent 39cafe3e6c
commit cb1830e16c
2 changed files with 60 additions and 0 deletions

View File

@ -814,5 +814,11 @@ patches = [
# RequestContextTest.PopupNavDestroyParentAfterCreationRCGlobal.
# https://issues.chromium.org/issues/323753235#comment11
'name': 'content_initiator_policy_323753235'
},
{
# win: Fix undefined std::_Literal_zero_is_expected() when building
# cef_sandbox with VS 17.9.2 version of MSVC STL.
# https://github.com/chromiumembedded/cef/issues/3708
'name': 'win_sandbox_op3way_3708'
}
]

View File

@ -0,0 +1,54 @@
diff --git base/time/time.h base/time/time.h
index 0efbea85491f4..17f9fe92ef78c 100644
--- base/time/time.h
+++ base/time/time.h
@@ -134,6 +134,13 @@ constexpr bool isnan(double d) {
}
+// Clang compiler is unable to eliminate a "dead" function call to an undefined
+// `std::_Literal_zero_is_expected()` function that MSVC uses to allow
+// comparisons with literal zero without warning.
+#define MSVC_OPERATOR_3WAY_BROKEN \
+ BUILDFLAG(IS_WIN) && (__cplusplus >= 202002L || _MSVC_LANG >= 202002L) && \
+ _MSVC_STL_VERSION >= 143 && _MSVC_STL_UPDATE >= 202303
+
// TimeDelta ------------------------------------------------------------------
class BASE_EXPORT TimeDelta {
@@ -320,8 +327,17 @@ class BASE_EXPORT TimeDelta {
// Comparison operators.
friend constexpr bool operator==(TimeDelta, TimeDelta) = default;
+#if MSVC_OPERATOR_3WAY_BROKEN
+ friend constexpr std::strong_ordering operator<=>(TimeDelta lhs,
+ TimeDelta rhs) {
+ if(lhs.delta_ == rhs.delta_) return std::strong_ordering::equal;
+ if(lhs.delta_ < rhs.delta_) return std::strong_ordering::less;
+ return std::strong_ordering::greater;
+ }
+#else
friend constexpr std::strong_ordering operator<=>(TimeDelta,
TimeDelta) = default;
+#endif
// Returns this delta, ceiled/floored/rounded-away-from-zero to the nearest
// multiple of |interval|.
@@ -472,8 +488,17 @@ class TimeBase {
// Comparison operators
friend constexpr bool operator==(const TimeBase&, const TimeBase&) = default;
+#if MSVC_OPERATOR_3WAY_BROKEN
+ friend constexpr std::strong_ordering operator<=>(TimeBase lhs,
+ TimeBase rhs) {
+ if(lhs.us_ == rhs.us_) return std::strong_ordering::equal;
+ if(lhs.us_ < rhs.us_) return std::strong_ordering::less;
+ return std::strong_ordering::greater;
+ }
+#else
friend constexpr std::strong_ordering operator<=>(const TimeBase&,
const TimeBase&) = default;
+#endif
protected:
constexpr explicit TimeBase(int64_t us) : us_(us) {}