pw_tokenizer: Make C++11 compatible
- Add pw_polyfill adapters for static_assert and std::is_null_pointer.
- In tokenizer, use alternative API where necessary to adapt to C++11.
- Add tests that compile all of tokenize.cc and its dependencies with
older C++ standards.
Change-Id: I3ef81ee4d0e95fc725843c4f20906825baf77cf7
diff --git a/pw_tokenizer/BUILD b/pw_tokenizer/BUILD
index 49c2b5c..e03fdee 100644
--- a/pw_tokenizer/BUILD
+++ b/pw_tokenizer/BUILD
@@ -169,6 +169,16 @@
)
pw_cc_test(
+ name = "simple_tokenize_test",
+ srcs = [
+ "simple_tokenize_test.cc",
+ ],
+ deps = [
+ ":pw_tokenizer",
+ ],
+)
+
+pw_cc_test(
name = "token_database_test",
srcs = [
"token_database_test.cc",
diff --git a/pw_tokenizer/BUILD.gn b/pw_tokenizer/BUILD.gn
index 2bf43fc..f628380 100644
--- a/pw_tokenizer/BUILD.gn
+++ b/pw_tokenizer/BUILD.gn
@@ -123,6 +123,9 @@
":decode_test",
":detokenize_test",
":hash_test",
+ ":simple_tokenize_test_cpp11",
+ ":simple_tokenize_test_cpp14",
+ ":simple_tokenize_test_cpp17",
":token_database_test",
":tokenize_test",
]
@@ -184,6 +187,53 @@
]
}
+# Fully test C++11 and C++14 compatibility by compiling all sources as C++11 or
+# C++14.
+_simple_tokenize_test_sources = [
+ "public/pw_tokenizer/pw_tokenizer_65599_fixed_length_hash.h",
+ "public/pw_tokenizer/tokenize.h",
+ "public/pw_tokenizer/config.h",
+ "public/pw_tokenizer/internal/argument_types.h",
+ "public/pw_tokenizer/internal/argument_types_macro_4_byte.h",
+ "public/pw_tokenizer/internal/argument_types_macro_8_byte.h",
+ "public/pw_tokenizer/internal/pw_tokenizer_65599_fixed_length_128_hash_macro.h",
+ "public/pw_tokenizer/internal/pw_tokenizer_65599_fixed_length_80_hash_macro.h",
+ "public/pw_tokenizer/internal/pw_tokenizer_65599_fixed_length_96_hash_macro.h",
+ "public/pw_tokenizer/internal/tokenize_string.h",
+ "tokenize.cc",
+ "simple_tokenize_test.cc",
+ "$dir_pw_varint/public/pw_varint/varint.h",
+ "$dir_pw_varint/varint.cc",
+]
+_simple_tokenize_test_configs = [
+ ":default_config",
+ "$dir_pw_varint:default_config",
+]
+
+pw_test("simple_tokenize_test_cpp11") {
+ configs = [ "$dir_pw_build:cpp11" ] + _simple_tokenize_test_configs
+ sources = _simple_tokenize_test_sources
+ deps = [
+ dir_pw_preprocessor,
+ ]
+}
+
+pw_test("simple_tokenize_test_cpp14") {
+ configs = [ "$dir_pw_build:cpp14" ] + _simple_tokenize_test_configs
+ sources = _simple_tokenize_test_sources
+ deps = [
+ dir_pw_preprocessor,
+ ]
+}
+
+pw_test("simple_tokenize_test_cpp17") {
+ configs = [ "$dir_pw_build:cpp17" ] + _simple_tokenize_test_configs
+ sources = _simple_tokenize_test_sources
+ deps = [
+ dir_pw_preprocessor,
+ ]
+}
+
pw_test("token_database_test") {
sources = [
"token_database_test.cc",
diff --git a/pw_tokenizer/docs.rst b/pw_tokenizer/docs.rst
index baf5f3f..eff7280 100644
--- a/pw_tokenizer/docs.rst
+++ b/pw_tokenizer/docs.rst
@@ -489,7 +489,7 @@
Compatibility
=============
* C11
- * C++17
+ * C++11
* Python 3
Dependencies
diff --git a/pw_tokenizer/public/pw_tokenizer/internal/argument_types.h b/pw_tokenizer/public/pw_tokenizer/internal/argument_types.h
index 4ceea40..0f376ec 100644
--- a/pw_tokenizer/public/pw_tokenizer/internal/argument_types.h
+++ b/pw_tokenizer/public/pw_tokenizer/internal/argument_types.h
@@ -82,7 +82,10 @@
#define _PW_VARARGS_TYPE(arg) ::pw::tokenizer::VarargsType<decltype(arg)>()
-namespace pw::tokenizer {
+namespace pw {
+namespace tokenizer {
+
+#if __cpp_if_constexpr // C++17 version
// This function selects the matching type enum for supported argument types.
template <typename T>
@@ -102,7 +105,44 @@
}
}
-} // namespace pw::tokenizer
+#else // C++11 or C++14 version
+
+template <typename T,
+ bool kIsDouble = std::is_floating_point<T>(),
+ bool kIsString = !std::is_null_pointer<T>() &&
+ std::is_convertible<T, const char*>(),
+ bool kIsInt64 = sizeof(T) == sizeof(int64_t)>
+struct SelectVarargsType;
+
+template <typename T, bool kDontCare1, bool kDontCare2>
+struct SelectVarargsType<T, true, kDontCare1, kDontCare2> {
+ static constexpr pw_TokenizerArgTypes kValue = PW_TOKENIZER_ARG_TYPE_DOUBLE;
+};
+
+template <typename T, bool kDontCare>
+struct SelectVarargsType<T, false, true, kDontCare> {
+ static constexpr pw_TokenizerArgTypes kValue = PW_TOKENIZER_ARG_TYPE_STRING;
+};
+
+template <typename T>
+struct SelectVarargsType<T, false, false, true> {
+ static constexpr pw_TokenizerArgTypes kValue = PW_TOKENIZER_ARG_TYPE_INT64;
+};
+
+template <typename T>
+struct SelectVarargsType<T, false, false, false> {
+ static constexpr pw_TokenizerArgTypes kValue = PW_TOKENIZER_ARG_TYPE_INT;
+};
+
+template <typename T>
+constexpr pw_TokenizerArgTypes VarargsType() {
+ return SelectVarargsType<typename std::decay<T>::type>::kValue;
+}
+
+#endif // __cpp_if_constexpr
+
+} // namespace tokenizer
+} // namespace pw
#else // C version
diff --git a/pw_tokenizer/public/pw_tokenizer/internal/tokenize_string.h b/pw_tokenizer/public/pw_tokenizer/internal/tokenize_string.h
index 40e5150..b30223a 100644
--- a/pw_tokenizer/public/pw_tokenizer/internal/tokenize_string.h
+++ b/pw_tokenizer/public/pw_tokenizer/internal/tokenize_string.h
@@ -27,7 +27,8 @@
#include <stdint.h>
-#if __cplusplus // In C++, use a constexpr function to calculate the hash.
+// In C++17, use a constexpr function to calculate the hash.
+#if __cpp_constexpr >= 201304L && defined(__cpp_inline_variables)
#include "pw_tokenizer/pw_tokenizer_65599_fixed_length_hash.h"
@@ -36,7 +37,7 @@
std::string_view((format), sizeof(format "") - 1), \
PW_TOKENIZER_CFG_HASH_LENGTH)
-#else // In C code, use the hashing macro
+#else // In C or older C++ code, use the hashing macro.
#if PW_TOKENIZER_CFG_HASH_LENGTH == 80
@@ -63,7 +64,7 @@
#endif // PW_TOKENIZER_CFG_HASH_LENGTH
-#endif // __cplusplus
+#endif // __cpp_constexpr >= 201304L && defined(__cpp_inline_variables)
// The type of the token used in place of a format string.
typedef uint32_t pw_TokenizerStringToken;
diff --git a/pw_tokenizer/simple_tokenize_test.cc b/pw_tokenizer/simple_tokenize_test.cc
new file mode 100644
index 0000000..ce84db8
--- /dev/null
+++ b/pw_tokenizer/simple_tokenize_test.cc
@@ -0,0 +1,204 @@
+// Copyright 2020 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include <algorithm>
+#include <array>
+
+#include "gtest/gtest.h"
+#include "pw_tokenizer/tokenize.h"
+
+namespace pw {
+namespace tokenizer {
+namespace {
+
+template <size_t kSize>
+uint32_t TestHash(const char (&str)[kSize]) {
+ static_assert(kSize > 0u, "Must have at least a null terminator");
+
+ static constexpr uint32_t k65599HashConstant = 65599u;
+
+ // The length is hashed as if it were the first character.
+ uint32_t hash = kSize - 1;
+ uint32_t coefficient = k65599HashConstant;
+
+ size_t length =
+ std::min(static_cast<size_t>(PW_TOKENIZER_CFG_HASH_LENGTH), kSize - 1);
+
+ // Hash all of the characters in the string as unsigned ints.
+ for (size_t i = 0; i < length; ++i) {
+ hash += coefficient * str[i];
+ coefficient *= k65599HashConstant;
+ }
+
+ return hash;
+}
+
+TEST(TokenizeStringLiteral, EmptyString_IsZero) {
+ constexpr pw_TokenizerStringToken token = PW_TOKENIZE_STRING("");
+ EXPECT_TRUE(0u == token);
+}
+
+TEST(TokenizeStringLiteral, String_MatchesHash) {
+ constexpr uint32_t token = PW_TOKENIZE_STRING("[:-)");
+ EXPECT_TRUE(TestHash("[:-)") == token);
+}
+
+constexpr uint32_t kGlobalToken = PW_TOKENIZE_STRING(">:-[]");
+
+TEST(TokenizeStringLiteral, GlobalVariable_MatchesHash) {
+ EXPECT_TRUE(TestHash(">:-[]") == kGlobalToken);
+}
+
+class TokenizeToBuffer : public ::testing::Test {
+ public:
+ TokenizeToBuffer() : buffer_{} {}
+
+ protected:
+ uint8_t buffer_[64];
+};
+
+// Test fixture for callback and global handler. Both of these need a global
+// message buffer. To keep the message buffers separate, template this on the
+// derived class type.
+template <typename Impl>
+class GlobalMessage : public ::testing::Test {
+ public:
+ static void SetMessage(const uint8_t* message, size_t size) {
+ ASSERT_TRUE(size <= sizeof(message_));
+ std::memcpy(message_, message, size);
+ message_size_bytes_ = size;
+ }
+
+ protected:
+ GlobalMessage() {
+ std::memset(message_, 0, sizeof(message_));
+ message_size_bytes_ = 0;
+ }
+
+ static uint8_t message_[256];
+ static size_t message_size_bytes_;
+};
+
+template <typename Impl>
+uint8_t GlobalMessage<Impl>::message_[256] = {};
+template <typename Impl>
+size_t GlobalMessage<Impl>::message_size_bytes_ = 0;
+
+class TokenizeToCallback : public GlobalMessage<TokenizeToCallback> {};
+
+template <uint8_t... kData, size_t kSize>
+std::array<uint8_t, sizeof(uint32_t) + sizeof...(kData)> ExpectedData(
+ const char (&format)[kSize]) {
+ const uint32_t value = TestHash(format);
+ return std::array<uint8_t, sizeof(uint32_t) + sizeof...(kData)>{
+ static_cast<uint8_t>(value & 0xff),
+ static_cast<uint8_t>(value >> 8 & 0xff),
+ static_cast<uint8_t>(value >> 16 & 0xff),
+ static_cast<uint8_t>(value >> 24 & 0xff),
+ kData...};
+}
+
+TEST_F(TokenizeToCallback, Variety) {
+ PW_TOKENIZE_TO_CALLBACK(
+ SetMessage, "%s there are %x (%.2f) of them%c", "Now", 2u, 2.0f, '.');
+ const auto expected = // clang-format off
+ ExpectedData<3, 'N', 'o', 'w', // string "Now"
+ 0x04, // unsigned 2 (zig-zag encoded)
+ 0x00, 0x00, 0x00, 0x40, // float 2.0
+ 0x5C // char '.' (0x2E, zig-zag encoded)
+ >("%s there are %x (%.2f) of them%c");
+ // clang-format on
+ ASSERT_TRUE(expected.size() == message_size_bytes_);
+ EXPECT_TRUE(std::memcmp(expected.data(), message_, expected.size()) == 0);
+}
+
+#if PW_TOKENIZER_CFG_ENABLE_TOKENIZE_TO_GLOBAL_HANDLER
+
+class TokenizeToGlobalHandler : public GlobalMessage<TokenizeToGlobalHandler> {
+};
+
+TEST_F(TokenizeToGlobalHandler, Variety) {
+ PW_TOKENIZE_TO_GLOBAL_HANDLER("%x%lld%1.2f%s", 0, 0ll, -0.0, "");
+ const auto expected =
+ ExpectedData<0, 0, 0x00, 0x00, 0x00, 0x80, 0>("%x%lld%1.2f%s");
+ ASSERT_TRUE(expected.size() == message_size_bytes_);
+ EXPECT_TRUE(std::memcmp(expected.data(), message_, expected.size()) == 0);
+}
+
+extern "C" void pw_TokenizerHandleEncodedMessage(const uint8_t* encoded_message,
+ size_t size_bytes) {
+ TokenizeToGlobalHandler::SetMessage(encoded_message, size_bytes);
+}
+
+#endif // PW_TOKENIZER_CFG_ENABLE_TOKENIZE_TO_GLOBAL_HANDLER
+
+#if PW_TOKENIZER_CFG_ENABLE_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD
+
+class TokenizeToGlobalHandlerWithPayload
+ : public GlobalMessage<TokenizeToGlobalHandlerWithPayload> {
+ public:
+ static void SetPayload(pw_TokenizerPayload payload) {
+ payload_ = reinterpret_cast<intptr_t>(payload);
+ }
+
+ protected:
+ TokenizeToGlobalHandlerWithPayload() { payload_ = {}; }
+
+ static intptr_t payload_;
+};
+
+intptr_t TokenizeToGlobalHandlerWithPayload::payload_;
+
+TEST_F(TokenizeToGlobalHandlerWithPayload, Variety) {
+ ASSERT_TRUE(payload_ != 123);
+
+ const auto expected =
+ ExpectedData<0, 0, 0x00, 0x00, 0x00, 0x80, 0>("%x%lld%1.2f%s");
+
+ PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD(
+ reinterpret_cast<pw_TokenizerPayload>(123),
+ "%x%lld%1.2f%s",
+ 0,
+ 0ll,
+ -0.0,
+ "");
+ ASSERT_TRUE(expected.size() == message_size_bytes_);
+ EXPECT_TRUE(std::memcmp(expected.data(), message_, expected.size()) == 0);
+ EXPECT_TRUE(payload_ == 123);
+
+ PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD(
+ reinterpret_cast<pw_TokenizerPayload>(-543),
+ "%x%lld%1.2f%s",
+ 0,
+ 0ll,
+ -0.0,
+ "");
+ ASSERT_TRUE(expected.size() == message_size_bytes_);
+ EXPECT_TRUE(std::memcmp(expected.data(), message_, expected.size()) == 0);
+ EXPECT_TRUE(payload_ == -543);
+}
+
+extern "C" void pw_TokenizerHandleEncodedMessageWithPayload(
+ pw_TokenizerPayload payload,
+ const uint8_t* encoded_message,
+ size_t size_bytes) {
+ TokenizeToGlobalHandlerWithPayload::SetMessage(encoded_message, size_bytes);
+ TokenizeToGlobalHandlerWithPayload::SetPayload(payload);
+}
+
+#endif // PW_TOKENIZER_CFG_ENABLE_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD
+
+} // namespace
+} // namespace tokenizer
+} // namespace pw
diff --git a/pw_tokenizer/tokenize.cc b/pw_tokenizer/tokenize.cc
index 18bf88d..b7ab505 100644
--- a/pw_tokenizer/tokenize.cc
+++ b/pw_tokenizer/tokenize.cc
@@ -24,9 +24,11 @@
#include <cstddef>
#include <cstring>
+#include "pw_polyfill/language_features.h" // static_assert
#include "pw_varint/varint.h"
-namespace pw::tokenizer {
+namespace pw {
+namespace tokenizer {
namespace {
// Store metadata about this compilation's string tokenization in the ELF.
@@ -191,8 +193,8 @@
const size_t encoded_bytes =
EncodeArgs(types,
args,
- span(static_cast<uint8_t*>(buffer) + sizeof(token),
- *buffer_size_bytes - sizeof(token)));
+ span<uint8_t>(static_cast<uint8_t*>(buffer) + sizeof(token),
+ *buffer_size_bytes - sizeof(token)));
va_end(args);
*buffer_size_bytes = sizeof(token) + encoded_bytes;
@@ -258,4 +260,5 @@
} // extern "C"
-} // namespace pw::tokenizer
+} // namespace tokenizer
+} // namespace pw