Skip to content

Commit

Permalink
pw_polyfill: Remove code related to C++11 support
Browse files Browse the repository at this point in the history
C++14 is now required for all Pigweed modules.

- Add a section to the style guide about the C++ standard.
- Remove the tokenizer and polyfill C++11 tests.
- Remove the cpp11 GN config.
- Remove unnecessary features and workarounds for C++11.
- Update C++14 tests to use EXPECT_EQ, EXPECT_NE, etc. since these are
  supported by C++14, but not C++11.

Change-Id: I6082ff01b4410db8a322cf8cefee60d3787443ff
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/65240
Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
Reviewed-by: Keir Mierle <[email protected]>
Reviewed-by: Ted Pudlik <[email protected]>
Commit-Queue: Wyatt Hepler <[email protected]>
  • Loading branch information
255 authored and CQ Bot Account committed Oct 12, 2021
1 parent fff3c7c commit 0132da5
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 107 deletions.
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Here is a selection of interesting modules:

- :ref:`module-pw_polyfill` - Similar to JavaScript “polyfill” libraries, this
module provides selected C++17 standard library components that are
compatible with C++11 and C++14.
compatible with C++14.

- :ref:`module-pw_tokenizer` - Replace string literals from log statements
with 32-bit tokens, to reduce flash use, reduce logging bandwidth, and save
Expand Down
15 changes: 14 additions & 1 deletion docs/style_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Style Guide and Conventions
---------
C++ style
---------

The Pigweed C++ style guide is closely based on Google's external C++ Style
Guide, which is found on the web at
https://google.github.io/styleguide/cppguide.html. The Google C++ Style Guide
Expand All @@ -28,6 +27,20 @@ Recommendations in the :doc:`embedded_cpp_guide` are considered part of the
Pigweed style guide, but are separated out since it covers more general
embedded development beyond just C++ style.

C++ standard
============
Pigweed primarily uses the C++17 standard. A few modules maintain support for
C++14, however (e.g. :ref:`module-pw_kvs` and its dependencies).

All Pigweed C++ code must compile with ``-std=C++17`` in Clang and GCC. C++20
features may be used as long as the code still compiles unmodified with C++17.
See ``pw_polyfill/language_feature_macros.h`` for macros that provide C++20
features when supported.

Compiler extensions should not be used unless wrapped in a macro or properly
guarded in the preprocessor. See ``pw_processor/compiler.h`` for macros that
wrap compiler-specific features.

Automatic formatting
====================
Pigweed uses `clang-format <https://clang.llvm.org/docs/ClangFormat.html>`_ to
Expand Down
2 changes: 1 addition & 1 deletion docs/targets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ build argument defined in Pigweed. Some notable arguments include:
* ``default_public_deps``: List of GN targets which are added as a dependency
to all ``pw_*`` GN targets. This is used to add global module dependencies;
for example, in upstream, ``pw_polyfill`` is added here to provide C++17
features in C++11/C++14 code.
features in C++14 code.
* Facade backends: Pigweed defines facades to provide a common interface for
core system features such as logging without assuming an implementation.
When building a Pigweed target, the implementations for each of these must be
Expand Down
4 changes: 0 additions & 4 deletions pw_build/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ config("extra_strict_warnings") {
cflags_c = [ "-Wstrict-prototypes" ]
}

config("cpp11") {
cflags_cc = [ "-std=c++11" ]
}

config("cpp14") {
cflags_cc = [ "-std=c++14" ]
}
Expand Down
8 changes: 0 additions & 8 deletions pw_polyfill/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ pw_source_set("standard_library") {
pw_test_group("tests") {
tests = [
":default_cpp_test",
":cpp11_test",
":cpp14_test",
]
group_deps = [ "$dir_pw_span:tests" ]
Expand All @@ -99,13 +98,6 @@ pw_test("default_cpp_test") {
sources = [ "test.cc" ]
}

pw_test("cpp11_test") {
remove_configs = [ "$dir_pw_build:cpp17" ]
configs = [ "$dir_pw_build:cpp11" ]
sources = [ "test.cc" ]
deps = [ ":pw_polyfill" ]
}

pw_test("cpp14_test") {
remove_configs = [ "$dir_pw_build:cpp17" ]
configs = [ "$dir_pw_build:cpp14" ]
Expand Down
2 changes: 1 addition & 1 deletion pw_polyfill/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# pw\_polyfill: Backports C++17 features to C++11 and C++14
# pw\_polyfill: Backports C++17 features to C++14
31 changes: 14 additions & 17 deletions pw_polyfill/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
===========
pw_polyfill
===========
The ``pw_polyfill`` module backports new C++ features to older C++ standards.
When possible, features are adapted to work with in standards as old as C++11.
Pigweed does not support C++ standards older than C++11.
The ``pw_polyfill`` module backports new C++ features to C++14.

------------------------------------------------
Backport new C++ features to older C++ standards
Expand All @@ -22,7 +20,7 @@ C headers. The wrapper headers include the original header using
`#include_next <https://gcc.gnu.org/onlinedocs/cpp/Wrapper-Headers.html>`_, then
add missing features. The backported features are only defined if they aren't
provided by the standard header, so ``pw_polyfill`` is safe to use when
compiling with any standard C++11 or newer.
compiling with any standard C++14 or newer.

Language features are backported or stubbed via the
``pw_polyfill/language_features.h`` header. This header is included in files
Expand All @@ -39,17 +37,17 @@ for the ``language_features.h`` header.

Backported features
===================
================== ================================ ============================================ ========================================
Header Feature Level of support Feature test macro
================== ================================ ============================================ ========================================
<bit> std::endian full __cpp_lib_endian
<cstdlib> std::byte full; some operators not constexpr in C++11 __cpp_lib_byte
<iterator> std::data, std::size full __cpp_lib_nonmember_container_access
<type_traits> \*_t trait aliases partial (can expand as needed) __cpp_lib_transformation_trait_aliases
<type_traits> std::is_null_pointer full __cpp_lib_is_null_pointer
<utilty> std::integer_sequence & helpers full __cpp_lib_integer_sequence
(language feature) static_assert with no message full __cpp_static_assert
================== ================================ ============================================ ========================================
================== ================================ =============================== ========================================
Header Feature Level of support Feature test macro
================== ================================ =============================== ========================================
<bit> std::endian full __cpp_lib_endian
<cstdlib> std::byte full __cpp_lib_byte
<iterator> std::data, std::size full __cpp_lib_nonmember_container_access
<type_traits> \*_t trait aliases partial (can expand as needed) __cpp_lib_transformation_trait_aliases
<type_traits> std::is_null_pointer full __cpp_lib_is_null_pointer
<utilty> std::integer_sequence & helpers full __cpp_lib_integer_sequence
(language feature) static_assert with no message full __cpp_static_assert
================== ================================ =============================== ========================================

----------------------------------------------------
Adapt code to compile with different versions of C++
Expand All @@ -67,7 +65,6 @@ Language feature macros
Macro Feature Description Feature test macro
====================== ================================ ======================================== ==========================
PW_INLINE_VARIABLE inline variables inline if supported by the compiler __cpp_inline_variables
PW_CONSTEXPR_FUNCTION relaxed constexpr function rules constexpr if relaxed rules are supported __cpp_constexpr >= 201304L
PW_CONSTEXPR_CPP20 constexpr in C++20 constexpr if compiling for C++20 __cplusplus >= 202002L
PW_CONSTEVAL consteval consteval if supported by the compiler __cpp_consteval
PW_CONSTINIT constinit constinit in clang and GCC 10+ __cpp_constinit
Expand All @@ -81,4 +78,4 @@ systems, add ``pw_polyfill/standard_library_public`` and
-------------
Compatibility
-------------
C++11
C++14
7 changes: 0 additions & 7 deletions pw_polyfill/public/pw_polyfill/language_feature_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
#define PW_INLINE_VARIABLE
#endif // __cpp_inline_variables

// Mark functions as constexpr if the relaxed constexpr rules are supported.
#if __cpp_constexpr >= 201304L
#define PW_CONSTEXPR_FUNCTION constexpr
#else
#define PW_CONSTEXPR_FUNCTION
#endif // __cpp_constexpr >= 201304L

// Mark functions as constexpr if C++20 or newer
#if __cplusplus >= 202002L
#define PW_CONSTEXPR_CPP20 constexpr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,10 @@ constexpr byte operator>>(byte b, I shift) noexcept {
return byte(static_cast<unsigned int>(b) >> shift);
}

#if __cpp_constexpr >= 201304L

constexpr byte& operator|=(byte& l, byte r) noexcept { return l = l | r; }
constexpr byte& operator&=(byte& l, byte r) noexcept { return l = l & r; }
constexpr byte& operator^=(byte& l, byte r) noexcept { return l = l ^ r; }

#else // For C++11 compatiblity, these operators cannot be constexpr.

inline byte& operator|=(byte& l, byte r) noexcept { return l = l | r; }
inline byte& operator&=(byte& l, byte r) noexcept { return l = l & r; }
inline byte& operator^=(byte& l, byte r) noexcept { return l = l ^ r; }

#endif // __cpp_constexpr >= 201304L

template <typename I>
inline byte& operator<<=(byte& b, I shift) noexcept {
return b = b << shift;
Expand Down
41 changes: 18 additions & 23 deletions pw_polyfill/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ static_assert(foo == 42, "Error!");

static_assert(PW_CXX_STANDARD_IS_SUPPORTED(98), "C++98 must be supported");
static_assert(PW_CXX_STANDARD_IS_SUPPORTED(11), "C++11 must be supported");

#if __cplusplus >= 201402L
static_assert(PW_CXX_STANDARD_IS_SUPPORTED(14), "C++14 must be not supported");
#else
static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(14), "C++14 must be supported");
#endif // __cplusplus >= 201402L
static_assert(PW_CXX_STANDARD_IS_SUPPORTED(14), "C++14 must be supported");

#if __cplusplus >= 201703L
static_assert(PW_CXX_STANDARD_IS_SUPPORTED(17), "C++17 must be not supported");
Expand All @@ -48,31 +43,31 @@ static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(17), "C++17 must be supported");

TEST(Bit, Endian) {
if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) {
EXPECT_TRUE(std::endian::native == std::endian::big);
EXPECT_EQ(std::endian::native, std::endian::big);
} else if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) {
EXPECT_TRUE(std::endian::native == std::endian::little);
EXPECT_EQ(std::endian::native, std::endian::little);
} else {
FAIL();
}
}

TEST(Cstddef, Byte_Operators) {
std::byte value = std::byte(0);
EXPECT_TRUE((value | std::byte(0x12)) == std::byte(0x12));
EXPECT_TRUE((value & std::byte(0x12)) == std::byte(0));
EXPECT_TRUE((value ^ std::byte(0x12)) == std::byte(0x12));
EXPECT_TRUE(~std::byte(0) == std::byte(-1));
EXPECT_TRUE((std::byte(1) << 3) == std::byte(0x8));
EXPECT_TRUE((std::byte(0x8) >> 3) == std::byte(1));
EXPECT_EQ((value | std::byte(0x12)), std::byte(0x12));
EXPECT_EQ((value & std::byte(0x12)), std::byte(0));
EXPECT_EQ((value ^ std::byte(0x12)), std::byte(0x12));
EXPECT_EQ(~std::byte(0), std::byte(-1));
EXPECT_EQ((std::byte(1) << 3), std::byte(0x8));
EXPECT_EQ((std::byte(0x8) >> 3), std::byte(1));
}

TEST(Cstddef, Byte_AssignmentOperators) {
std::byte value = std::byte(0);
EXPECT_TRUE((value |= std::byte(0x12)) == std::byte(0x12));
EXPECT_TRUE((value &= std::byte(0x0F)) == std::byte(0x02));
EXPECT_TRUE((value ^= std::byte(0xFF)) == std::byte(0xFD));
EXPECT_TRUE((value <<= 4) == std::byte(0xD0));
EXPECT_TRUE((value >>= 5) == std::byte(0x6));
EXPECT_EQ((value |= std::byte(0x12)), std::byte(0x12));
EXPECT_EQ((value &= std::byte(0x0F)), std::byte(0x02));
EXPECT_EQ((value ^= std::byte(0xFF)), std::byte(0xFD));
EXPECT_EQ((value <<= 4), std::byte(0xD0));
EXPECT_EQ((value >>= 5), std::byte(0x6));
}

// Check that consteval is at least equivalent to constexpr.
Expand All @@ -83,13 +78,13 @@ int c_array[5423] = {};
std::array<int, 32> array;

TEST(Iterator, Size) {
EXPECT_TRUE(std::size(c_array) == sizeof(c_array) / sizeof(*c_array));
EXPECT_TRUE(std::size(array) == array.size());
EXPECT_EQ(std::size(c_array), sizeof(c_array) / sizeof(*c_array));
EXPECT_EQ(std::size(array), array.size());
}

TEST(Iterator, Data) {
EXPECT_TRUE(std::data(c_array) == c_array);
EXPECT_TRUE(std::data(array) == array.data());
EXPECT_EQ(std::data(c_array), c_array);
EXPECT_EQ(std::data(array), array.data());
}

PW_CONSTINIT bool mutable_value = true;
Expand Down
2 changes: 1 addition & 1 deletion pw_result/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ similar type.

Compatibility
=============
Works with C++11, but some features require C++17.
Works with C++14, but some features require C++17.

Size report
===========
Expand Down
2 changes: 1 addition & 1 deletion pw_ring_buffer/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This documentation is incomplete :)

Compatibility
=============
* C++11
* C++14

Iterator
========
Expand Down
2 changes: 1 addition & 1 deletion pw_span/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ Pointer and size arguments can be replaced with a ``std::span``:
Compatibility
=============
Works with C++11, but some features require C++17.
Works with C++14, but some features require C++17.
2 changes: 1 addition & 1 deletion pw_span/public/pw_span/internal/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class span : public pw_span_internal::ExtentStorage<Extent> {
constexpr span(const span<U, OtherExtent>& other)
: span(other.data(), other.size()) {}

PW_CONSTEXPR_FUNCTION span& operator=(const span& other) noexcept = default;
constexpr span& operator=(const span& other) noexcept = default;
~span() noexcept = default;

// [span.sub], span subviews
Expand Down
2 changes: 1 addition & 1 deletion pw_status/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,4 @@ the size from the StatusWithSize on ok.
Compatibility
=============
C++11
C++14
11 changes: 1 addition & 10 deletions pw_tokenizer/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ pw_test_group("tests") {
":detokenize_test",
":global_handlers_test",
":hash_test",
":simple_tokenize_test_cpp11",
":simple_tokenize_test_cpp14",
":simple_tokenize_test_cpp17",
":token_database_fuzzer",
Expand Down Expand Up @@ -270,8 +269,7 @@ pw_test("hash_test") {
deps = [ ":pw_tokenizer" ]
}

# Fully test C++11 and C++14 compatibility by compiling all sources as C++11 or
# C++14.
# Fully test C++14 compatibility by compiling all sources as C++14.
_simple_tokenize_test_sources = [
"$dir_pw_containers/public/pw_containers/to_array.h",
"$dir_pw_varint/public/pw_varint/varint.h",
Expand Down Expand Up @@ -301,13 +299,6 @@ _simple_tokenize_test_configs = [
"$dir_pw_containers:public_include_path",
]

pw_test("simple_tokenize_test_cpp11") {
remove_configs = [ "$dir_pw_build:cpp17" ]
configs = [ "$dir_pw_build:cpp11" ] + _simple_tokenize_test_configs
sources = _simple_tokenize_test_sources
deps = [ dir_pw_preprocessor ]
}

pw_test("simple_tokenize_test_cpp14") {
remove_configs = [ "$dir_pw_build:cpp17" ]
configs = [ "$dir_pw_build:cpp14" ] + _simple_tokenize_test_configs
Expand Down
2 changes: 1 addition & 1 deletion pw_tokenizer/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ The Python tooling continues to support the legacy tokenized string ELF format.
Compatibility
=============
* C11
* C++11
* C++14
* Python 3

Dependencies
Expand Down
2 changes: 1 addition & 1 deletion pw_tokenizer/public/pw_tokenizer/internal/argument_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ constexpr pw_tokenizer_ArgTypes VarargsType() {
}
}

#else // C++11 or C++14 version
#else // C++14 version

template <typename T,
bool kIsDouble = std::is_floating_point<T>(),
Expand Down
Loading

0 comments on commit 0132da5

Please sign in to comment.