Skip to content

Commit 6d1ea68

Browse files
sstricklcommit-bot@chromium.org
authored andcommitted
[build] Move from the C++11 to the C++17 standard.
Also includes some example changes using C++14/C++17 features. TEST=vm/cc/BitField_Assert Cq-Include-Trybots: luci.dart.try:vm-kernel-precomp-win-release-x64-try,vm-kernel-precomp-mac-release-simarm64-try,vm-kernel-precomp-linux-release-x64-try,dart-sdk-win-try,vm-kernel-win-release-x64-try,vm-kernel-win-release-ia32-try,dart-sdk-mac-try,vm-kernel-mac-release-x64-try Change-Id: Icf5c5267431daf9cea2e61b67131021557675f3c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192183 Reviewed-by: Martin Kustermann <[email protected]> Reviewed-by: Vyacheslav Egorov <[email protected]> Commit-Queue: Tess Strickland <[email protected]>
1 parent caa31f5 commit 6d1ea68

File tree

7 files changed

+48
-39
lines changed

7 files changed

+48
-39
lines changed

build/config/compiler/BUILD.gn

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,12 @@ config("compiler") {
271271
cflags += [ "-fcolor-diagnostics" ]
272272
}
273273

274-
# C++11 compiler flags setup.
274+
# C++ standard compiler flags setup.
275275
# ---------------------------
276276
if (is_win) {
277-
# Up-to-date toolchain MSVC doesn't support c++11 flag any longer.
278-
cc_std = [ "/std:c++14" ]
279-
} else if (is_fuchsia) {
280-
cc_std = [ "-std=c++17" ]
277+
cc_std = [ "/std:c++17" ]
281278
} else {
282-
cc_std = [ "-std=c++11" ]
279+
cc_std = [ "-std=c++17" ]
283280
}
284281
cflags_cc += cc_std
285282
cflags_objcc += cc_std

runtime/platform/globals.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
// from the way the Dart project expects it: DEBUG indicating a debug build.
2121
#if !defined(NDEBUG) && !defined(DEBUG)
2222
#define DEBUG
23-
#endif // !NDEBUG && !DEBUG
23+
#endif // !NDEBUG && !DEBUG \
24+
#else
25+
// Since <cassert> uses NDEBUG to signify that assert() macros should be turned
26+
// off, we'll define it when DEBUG is _not_ set.
27+
#if !defined(DEBUG)
28+
#define NDEBUG
29+
#endif
2430
#endif // GOOGLE3
2531

2632
// __STDC_FORMAT_MACROS has to be defined before including <inttypes.h> to
@@ -85,6 +91,8 @@
8591
#include <string.h>
8692
#include <sys/types.h>
8793

94+
#include <cassert> // For assert() in constant expressions.
95+
8896
#if defined(_WIN32)
8997
#include "platform/floating_point_win.h"
9098
#endif // defined(_WIN32)
@@ -136,12 +144,6 @@
136144
#define DEBUG_ONLY(code)
137145
#endif // defined(DEBUG)
138146

139-
#if defined(DEBUG)
140-
#define UNLESS_DEBUG(code)
141-
#else // defined(DEBUG)
142-
#define UNLESS_DEBUG(code) code
143-
#endif // defined(DEBUG)
144-
145147
namespace dart {
146148

147149
struct simd128_value_t {

runtime/platform/utils.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Utils {
5757
}
5858

5959
template <typename T>
60-
static inline bool IsPowerOfTwo(T x) {
60+
static constexpr bool IsPowerOfTwo(T x) {
6161
return ((x & (x - 1)) == 0) && (x != 0);
6262
}
6363

@@ -73,13 +73,13 @@ class Utils {
7373
}
7474

7575
template <typename T>
76-
static inline bool IsAligned(T x, intptr_t n) {
77-
ASSERT(IsPowerOfTwo(n));
76+
static constexpr bool IsAligned(T x, intptr_t n) {
77+
assert(IsPowerOfTwo(n));
7878
return (x & (n - 1)) == 0;
7979
}
8080

8181
template <typename T>
82-
static inline bool IsAligned(T* x, intptr_t n) {
82+
static constexpr bool IsAligned(T* x, intptr_t n) {
8383
return IsAligned(reinterpret_cast<uword>(x), n);
8484
}
8585

runtime/vm/bitfield.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <type_traits>
99

10-
#include "platform/assert.h"
1110
#include "platform/globals.h"
1211

1312
namespace dart {
@@ -52,8 +51,8 @@ class BitField {
5251
static constexpr int bitsize() { return size; }
5352

5453
// Returns an S with the bit field value encoded.
55-
static UNLESS_DEBUG(constexpr) S encode(T value) {
56-
DEBUG_ASSERT(is_valid(value));
54+
static constexpr S encode(T value) {
55+
assert(is_valid(value));
5756
return encode_unchecked(value);
5857
}
5958

@@ -62,29 +61,28 @@ class BitField {
6261
// Ensure we slide down the sign bit if the value in the bit field is signed
6362
// and negative. We use 64-bit ints inside the expression since we can have
6463
// both cases: sizeof(S) > sizeof(T) or sizeof(S) < sizeof(T).
65-
return static_cast<T>(
66-
(sign_extend
67-
? (static_cast<int64_t>(static_cast<uint64_t>(value)
68-
<< (64 - (size + position))) >>
69-
(64 - size))
70-
: ((static_cast<typename std::make_unsigned<S>::type>(value) >>
71-
position) &
72-
mask())));
64+
if constexpr (sign_extend) {
65+
auto const u = static_cast<uint64_t>(value);
66+
return static_cast<T>((static_cast<int64_t>(u << (64 - kNextBit))) >>
67+
(64 - size));
68+
} else {
69+
auto const u = static_cast<typename std::make_unsigned<S>::type>(value);
70+
return static_cast<T>((u >> position) & mask());
71+
}
7372
}
7473

7574
// Returns an S with the bit field value encoded based on the
7675
// original value. Only the bits corresponding to this bit field
7776
// will be changed.
78-
static UNLESS_DEBUG(constexpr) S update(T value, S original) {
79-
DEBUG_ASSERT(is_valid(value));
80-
return encode_unchecked(value) | (~mask_in_place() & original);
77+
static constexpr S update(T value, S original) {
78+
return encode(value) | (~mask_in_place() & original);
8179
}
8280

8381
private:
8482
// Returns an S with the bit field value encoded.
8583
static constexpr S encode_unchecked(T value) {
86-
return (static_cast<typename std::make_unsigned<S>::type>(value) & mask())
87-
<< position;
84+
auto const u = static_cast<typename std::make_unsigned<S>::type>(value);
85+
return (u & mask()) << position;
8886
}
8987
};
9088

runtime/vm/bitfield_test.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,16 @@ VM_UNIT_TEST_CASE(BitFields_SignedField) {
7070
TestSignExtendedBitField<int32_t>();
7171
}
7272

73+
#if defined(DEBUG)
74+
#define DEBUG_CRASH "Crash"
75+
#else
76+
#define DEBUG_CRASH "Pass"
77+
#endif
78+
79+
VM_UNIT_TEST_CASE_WITH_EXPECTATION(BitFields_Assert, DEBUG_CRASH) {
80+
class F : public BitField<uint32_t, uint32_t, 0, 8, /*sign_extend=*/false> {};
81+
const uint32_t value = F::encode(kMaxUint32);
82+
EXPECT_EQ(kMaxUint8, value);
83+
}
84+
7385
} // namespace dart

runtime/vm/image_snapshot.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ void ImageWriter::WriteROData(NonStreamingWriteStream* stream, bool vm) {
557557
}
558558
}
559559

560-
static UNLESS_DEBUG(constexpr) const uword kReadOnlyGCBits =
560+
static constexpr uword kReadOnlyGCBits =
561561
UntaggedObject::OldBit::encode(true) |
562562
UntaggedObject::OldAndNotMarkedBit::encode(false) |
563563
UntaggedObject::OldAndNotRememberedBit::encode(true) |

runtime/vm/raw_object.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,19 @@ class UntaggedObject {
153153
static constexpr intptr_t kMaxSizeTag =
154154
kMaxSizeTagInUnitsOfAlignment * kObjectAlignment;
155155

156-
static UNLESS_DEBUG(constexpr) uword encode(intptr_t size) {
156+
static constexpr uword encode(intptr_t size) {
157157
return SizeBits::encode(SizeToTagValue(size));
158158
}
159159

160160
static constexpr uword decode(uword tag) {
161161
return TagValueToSize(SizeBits::decode(tag));
162162
}
163163

164-
static UNLESS_DEBUG(constexpr) uword update(intptr_t size, uword tag) {
164+
static constexpr uword update(intptr_t size, uword tag) {
165165
return SizeBits::update(SizeToTagValue(size), tag);
166166
}
167167

168-
static UNLESS_DEBUG(constexpr) bool SizeFits(intptr_t size) {
168+
static constexpr bool SizeFits(intptr_t size) {
169169
DEBUG_ASSERT(Utils::IsAligned(size, kObjectAlignment));
170170
return (size <= kMaxSizeTag);
171171
}
@@ -175,8 +175,8 @@ class UntaggedObject {
175175
class SizeBits
176176
: public BitField<uword, intptr_t, kSizeTagPos, kSizeTagSize> {};
177177

178-
static UNLESS_DEBUG(constexpr) intptr_t SizeToTagValue(intptr_t size) {
179-
DEBUG_ASSERT(Utils::IsAligned(size, kObjectAlignment));
178+
static constexpr intptr_t SizeToTagValue(intptr_t size) {
179+
assert(Utils::IsAligned(size, kObjectAlignment));
180180
return !SizeFits(size) ? 0 : (size >> kObjectAlignmentLog2);
181181
}
182182
static constexpr intptr_t TagValueToSize(intptr_t value) {

0 commit comments

Comments
 (0)