Skip to content

Commit

Permalink
Fix gcc-10 compilation issues mostly with spaceship operator
Browse files Browse the repository at this point in the history
Gcc 10 has issues with speceship operator:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96299

Therefore remove it in problematic places.

Fix other minor compilation issues with 10 gcc.

Change-Id: I4977ca2462af6beadece622eb4f7e5f56d3ed2da
Reviewed-on: https://codereview.qt-project.org/c/qt/qtwebengine-chromium/+/562031
Reviewed-by: Allan Sandfeld Jensen <[email protected]>
  • Loading branch information
Michal Klocek authored and Allan Sandfeld Jensen committed Aug 15, 2024
1 parent f32e26d commit 448be1e
Show file tree
Hide file tree
Showing 24 changed files with 364 additions and 126 deletions.
22 changes: 19 additions & 3 deletions chromium/base/bit_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

#include <type_traits>

#include "base/compiler_specific.h"
#if !HAS_BUILTIN(__builtin_bit_cast)
#include <string.h>
#endif

namespace base {

// This is an equivalent to C++20's std::bit_cast<>(), but with additional
Expand All @@ -18,8 +23,14 @@ namespace base {
// reinterpret_cast<>(), and then look at https://eel.is/c++draft/basic.lval#11
// as that's probably UB also.

template <class Dest, class Source>
constexpr Dest bit_cast(const Source& source) {
template <typename Dest, typename Source>
#if !HAS_BUILTIN(__builtin_bit_cast)
constexpr
#else
inline
#endif
Dest
bit_cast(const Source& source) {
static_assert(!std::is_pointer_v<Source>,
"bit_cast must not be used on pointer types");
static_assert(!std::is_pointer_v<Dest>,
Expand All @@ -36,8 +47,13 @@ constexpr Dest bit_cast(const Source& source) {
static_assert(
std::is_trivially_copyable_v<Dest>,
"bit_cast requires the destination type to be trivially copyable");

#if HAS_BUILTIN(__builtin_bit_cast)
return __builtin_bit_cast(Dest, source);
#else
Dest dest;
memcpy(&dest, &source, sizeof(dest));
return dest;
#endif
}

} // namespace base
Expand Down
42 changes: 36 additions & 6 deletions chromium/base/time/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,24 @@ class BASE_EXPORT TimeDelta {
}

// Comparison operators.
friend constexpr bool operator==(TimeDelta, TimeDelta) = default;
friend constexpr std::strong_ordering operator<=>(TimeDelta,
TimeDelta) = default;
constexpr bool operator==(TimeDelta other) const {
return delta_ == other.delta_;
}
constexpr bool operator!=(TimeDelta other) const {
return delta_ != other.delta_;
}
constexpr bool operator<(TimeDelta other) const {
return delta_ < other.delta_;
}
constexpr bool operator<=(TimeDelta other) const {
return delta_ <= other.delta_;
}
constexpr bool operator>(TimeDelta other) const {
return delta_ > other.delta_;
}
constexpr bool operator>=(TimeDelta other) const {
return delta_ >= other.delta_;
}

// Returns this delta, ceiled/floored/rounded-away-from-zero to the nearest
// multiple of |interval|.
Expand Down Expand Up @@ -471,9 +486,24 @@ class TimeBase {
}

// Comparison operators
friend constexpr bool operator==(const TimeBase&, const TimeBase&) = default;
friend constexpr std::strong_ordering operator<=>(const TimeBase&,
const TimeBase&) = default;
constexpr bool operator==(const TimeBase<TimeClass>& other) const {
return us_ == other.us_;
}
constexpr bool operator!=(const TimeBase<TimeClass>& other) const {
return us_ != other.us_;
}
constexpr bool operator<(const TimeBase<TimeClass>& other) const {
return us_ < other.us_;
}
constexpr bool operator<=(const TimeBase<TimeClass>& other) const {
return us_ <= other.us_;
}
constexpr bool operator>(const TimeBase<TimeClass>& other) const {
return us_ > other.us_;
}
constexpr bool operator>=(const TimeBase<TimeClass>& other) const {
return us_ >= other.us_;
}

protected:
constexpr explicit TimeBase(int64_t us) : us_(us) {}
Expand Down
17 changes: 12 additions & 5 deletions chromium/base/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

#include <stdint.h>

#include <compare>
#include <optional>
#include <string>
#include <tuple>

#include "base/base_export.h"
#include "base/containers/span.h"
Expand Down Expand Up @@ -56,11 +56,18 @@ class BASE_EXPORT Token {
return as_bytes(make_span(words_));
}

friend constexpr auto operator<=>(const Token& lhs,
const Token& rhs) = default;
friend constexpr bool operator==(const Token& lhs,
const Token& rhs) = default;
constexpr bool operator==(const Token& other) const {
return words_[0] == other.words_[0] && words_[1] == other.words_[1];
}

constexpr bool operator!=(const Token& other) const {
return !(*this == other);
}

constexpr bool operator<(const Token& other) const {
return std::tie(words_[0], words_[1]) <
std::tie(other.words_[0], other.words_[1]);
}
// Generates a string representation of this Token useful for e.g. logging.
std::string ToString() const;

Expand Down
29 changes: 18 additions & 11 deletions chromium/base/types/strong_alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,24 @@ class StrongAlias {

constexpr explicit operator const UnderlyingType&() const& { return value_; }

// Comparison operators that default to the behavior of `UnderlyingType`.
// Note that if you wish to compare `StrongAlias<UnderlyingType>`, e.g.,
// by using `operator<` in a `std::set`, then `UnderlyingType` must
// implement `operator<=>`. If you cannot modify `UnderlyingType` (e.g.,
// because it is from an external library), then a work-around is to create a
// thin wrapper `W` around it, define `operator<=>` for the wrapper and create
// a `StrongAlias<W>`.
friend auto operator<=>(const StrongAlias& lhs,
const StrongAlias& rhs) = default;
friend bool operator==(const StrongAlias& lhs,
const StrongAlias& rhs) = default;
constexpr bool operator==(const StrongAlias& other) const {
return value_ == other.value_;
}
constexpr bool operator!=(const StrongAlias& other) const {
return value_ != other.value_;
}
constexpr bool operator<(const StrongAlias& other) const {
return value_ < other.value_;
}
constexpr bool operator<=(const StrongAlias& other) const {
return value_ <= other.value_;
}
constexpr bool operator>(const StrongAlias& other) const {
return value_ > other.value_;
}
constexpr bool operator>=(const StrongAlias& other) const {
return value_ >= other.value_;
}

// Hasher to use in std::unordered_map, std::unordered_set, etc.
//
Expand Down
8 changes: 0 additions & 8 deletions chromium/base/types/token_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#ifndef BASE_TYPES_TOKEN_TYPE_H_
#define BASE_TYPES_TOKEN_TYPE_H_

#include <compare>
#include <type_traits>

#include "base/check.h"
Expand Down Expand Up @@ -42,13 +41,6 @@ class TokenType : public StrongAlias<TypeMarker, UnguessableToken> {
// StrongAlias doesn't define <=> because not all underlying types will
// implement it. TokenType can define it using UnguessableToken's
// implementation, though.
friend constexpr auto operator<=>(const TokenType& lhs,
const TokenType& rhs) {
return lhs.value() <=> rhs.value();
}
friend constexpr bool operator==(const TokenType& lhs, const TokenType& rhs) {
return lhs.value() == rhs.value();
}

// Hash functor for use in unordered containers.
struct Hasher {
Expand Down
9 changes: 5 additions & 4 deletions chromium/base/unguessable_token.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ std::optional<UnguessableToken> UnguessableToken::DeserializeFromString(
return UnguessableToken(token.value());
}

bool operator==(const UnguessableToken& lhs, const UnguessableToken& rhs) {
bool UnguessableToken::operator==(const UnguessableToken& other) const {
#if BUILDFLAG(IS_NACL)
// BoringSSL is unavailable for NaCl builds so it remains timing dependent.
return lhs.token_ == rhs.token_;
return token_ == other.token_;
#else
auto bytes = lhs.token_.AsBytes();
auto other_bytes = rhs.token_.AsBytes();
auto bytes = token_.AsBytes();
auto other_bytes = other.token_.AsBytes();

return CRYPTO_memcmp(bytes.data(), other_bytes.data(), bytes.size()) == 0;
#endif
}
Expand Down
16 changes: 8 additions & 8 deletions chromium/base/unguessable_token.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,15 @@ class BASE_EXPORT UnguessableToken {

span<const uint8_t, 16> AsBytes() const { return token_.AsBytes(); }

friend constexpr auto operator<=>(const UnguessableToken& lhs,
const UnguessableToken& rhs) = default;
constexpr bool operator<(const UnguessableToken& other) const {
return token_ < other.token_;
}

bool operator==(const UnguessableToken& other) const;

// operator== uses constant-time comparison for security where available.
friend BASE_EXPORT bool operator==(const UnguessableToken& lhs,
const UnguessableToken& rhs);
bool operator!=(const UnguessableToken& other) const {
return !(*this == other);
}

#if defined(UNIT_TEST)
static UnguessableToken CreateForTesting(uint64_t high, uint64_t low) {
Expand All @@ -128,9 +131,6 @@ class BASE_EXPORT UnguessableToken {
base::Token token_;
};

BASE_EXPORT bool operator==(const UnguessableToken& lhs,
const UnguessableToken& rhs);

BASE_EXPORT std::ostream& operator<<(std::ostream& out,
const UnguessableToken& token);

Expand Down
50 changes: 50 additions & 0 deletions chromium/components/autofill/core/common/form_field_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ bool DeserializeSection11(base::PickleIterator* iter,

} // namespace

bool operator==(const SelectOption& lhs, const SelectOption& rhs) {
return std::tie(lhs.value, lhs.content) == std::tie(rhs.value, rhs.content);
}

bool operator!=(const SelectOption& lhs, const SelectOption& rhs) {
return !(lhs == rhs);
}

Section Section::FromAutocomplete(Section::Autocomplete autocomplete) {
Section section;
if (autocomplete.section.empty() && autocomplete.mode == HtmlFieldMode::kNone)
Expand Down Expand Up @@ -290,6 +298,48 @@ Section::Section(const Section& section) = default;

Section::~Section() = default;

bool operator==(const Section::Autocomplete& a,
const Section::Autocomplete& b) {
return std::tie(a.section, a.mode) == std::tie(b.section, b.mode);
}

bool operator!=(const Section::Autocomplete& a,
const Section::Autocomplete& b) {
return !(a == b);
}

bool operator<(const Section::Autocomplete& a, const Section::Autocomplete& b) {
return std::tie(a.section, a.mode) < std::tie(b.section, b.mode);
}

bool operator==(const Section::FieldIdentifier& a,
const Section::FieldIdentifier& b) {
return std::tie(a.field_name, a.local_frame_id, a.field_renderer_id) ==
std::tie(b.field_name, b.local_frame_id, b.field_renderer_id);
}

bool operator!=(const Section::FieldIdentifier& a,
const Section::FieldIdentifier& b) {
return !(a == b);
}

bool operator<(const Section::FieldIdentifier& a,
const Section::FieldIdentifier& b) {
return std::tie(a.field_name, a.local_frame_id, a.field_renderer_id) <
std::tie(b.field_name, b.local_frame_id, b.field_renderer_id);
}

bool operator==(const Section& a, const Section& b) {
return a.value_ == b.value_;
}

bool operator!=(const Section& a, const Section& b) {
return !(a == b);
}
bool operator<(const Section& a, const Section& b) {
return a.value_ < b.value_;
}

Section::operator bool() const {
return !is_default();
}
Expand Down
27 changes: 7 additions & 20 deletions chromium/components/autofill/core/common/form_field_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <stddef.h>

#include <compare>
#include <limits>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -74,9 +73,6 @@ using FieldPropertiesMask = std::underlying_type_t<FieldPropertiesFlags>;
// For the HTML snippet |<option value="US">United States</option>|, the
// value is "US" and the contents is "United States".
struct SelectOption {
friend bool operator==(const SelectOption& lhs,
const SelectOption& rhs) = default;

std::u16string value;
std::u16string content;
};
Expand All @@ -85,11 +81,6 @@ struct SelectOption {
class Section {
public:
struct Autocomplete {
friend auto operator<=>(const Autocomplete& lhs,
const Autocomplete& rhs) = default;
friend bool operator==(const Autocomplete& lhs,
const Autocomplete& rhs) = default;

std::string section;
HtmlFieldMode mode = HtmlFieldMode::kNone;
};
Expand All @@ -105,10 +96,9 @@ class Section {
local_frame_id(local_frame_id),
field_renderer_id(field_renderer_id) {}

friend auto operator<=>(const FieldIdentifier& lhs,
const FieldIdentifier& rhs) = default;
friend bool operator==(const FieldIdentifier& lhs,
const FieldIdentifier& rhs) = default;
friend bool operator==(const FieldIdentifier& a, const FieldIdentifier& b);
friend bool operator!=(const FieldIdentifier& a, const FieldIdentifier& b);
friend bool operator<(const FieldIdentifier& a, const FieldIdentifier& b);

std::string field_name;
size_t local_frame_id;
Expand All @@ -124,14 +114,11 @@ class Section {
Section(const Section& section);
~Section();

// `absl::variant` does not implement `operator<=>` - therefore the ordering
// needs to be specified manually. Once `absl::variant` is `std::variant`,
// this return type can become `auto`.
friend std::strong_ordering operator<=>(const Section& lhs,
const Section& rhs) = default;
friend bool operator==(const Section& lhs, const Section& rhs) = default;
explicit operator bool() const;
friend bool operator==(const Section& a, const Section& b);
friend bool operator!=(const Section& a, const Section& b);
friend bool operator<(const Section& a, const Section& b);

explicit operator bool() const;
bool is_from_autocomplete() const;
bool is_from_fieldidentifier() const;
bool is_default() const;
Expand Down
Loading

0 comments on commit 448be1e

Please sign in to comment.