From 5e6008329f458cd659e89f6821b9263a594bd840 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Sat, 12 Feb 2022 13:25:43 +0100 Subject: [PATCH] refactor: deprecate TOML_API, add more specific defines --- .clang-format | 4 ++ CHANGELOG.md | 14 +++++ README.md | 5 +- cpp.hint | 8 ++- include/toml++/impl/array.h | 71 +++++++++------------- include/toml++/impl/array.inl | 16 +++++ include/toml++/impl/at_path.h | 8 +-- include/toml++/impl/at_path.inl | 3 +- include/toml++/impl/formatter.h | 36 +++++------ include/toml++/impl/json_formatter.h | 8 +-- include/toml++/impl/node.h | 25 ++++---- include/toml++/impl/node.inl | 9 ++- include/toml++/impl/node_view.h | 4 -- include/toml++/impl/node_view.inl | 21 ------- include/toml++/impl/node_view_extern.inl | 71 ---------------------- include/toml++/impl/parser.h | 24 ++++---- include/toml++/impl/preprocessor.h | 64 ++++++++++++-------- include/toml++/impl/print_to_stream.h | 60 +++++++++---------- include/toml++/impl/std_string.h | 6 +- include/toml++/impl/table.h | 64 ++++++++------------ include/toml++/impl/table.inl | 16 +++++ include/toml++/impl/toml_formatter.h | 14 ++--- include/toml++/impl/value.h | 6 -- include/toml++/impl/value.inl | 21 ------- include/toml++/impl/value_extern.inl | 76 ------------------------ include/toml++/impl/yaml_formatter.h | 10 ++-- include/toml++/toml.h | 4 -- tests/tests.h | 3 - toml++.vcxproj | 4 -- tools/generate_single_header.py | 4 +- 30 files changed, 258 insertions(+), 421 deletions(-) delete mode 100644 include/toml++/impl/node_view.inl delete mode 100644 include/toml++/impl/node_view_extern.inl delete mode 100644 include/toml++/impl/value.inl delete mode 100644 include/toml++/impl/value_extern.inl diff --git a/.clang-format b/.clang-format index a607365..162b9a6 100644 --- a/.clang-format +++ b/.clang-format @@ -165,6 +165,10 @@ StatementMacros: - TOML_ATTR - TOML_CONST_GETTER - TOML_CONST_INLINE_GETTER + - TOML_EXPORTED_CLASS + - TOML_EXPORTED_MEMBER_FUNCTION + - TOML_EXPORTED_FREE_FUNCTION + - TOML_EXPORTED_STATIC_FUNCTION - TOML_EXTERN - TOML_EXTERNAL_LINKAGE - TOML_INTERNAL_LINKAGE diff --git a/CHANGELOG.md b/CHANGELOG.md index f0eac2b..d80073e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,20 @@ template: --> +## Unreleased + +#### Fixes: +- Fixed potential segfault when calling `at_path()` with an empty string + +#### Additions: +- Added config options `TOML_EXPORTED_CLASS`, `TOML_EXPORTED_MEMBER_FUNCTION`, `TOML_EXPORTED_STATIC_FUNCTION` & `TOML_EXPORTED_FREE_FUNCTION` + +#### Removals/Deprecations: +- Deprecated old `TOML_API` option in favour new `TOML_EXPORTED_X` options + (it will continue to work as it did before if none of the new function export options are defined) + + + ## [v3.0.1](https://github.com/marzer/tomlplusplus/releases/tag/v3.0.1) - 2022-01-13 This is a single-bugfix release to fix an ODR issue for people using header-only mode in multiple diff --git a/README.md b/README.md index 688dc1c..7f042c7 100644 --- a/README.md +++ b/README.md @@ -145,7 +145,6 @@ won't need to mess with these at all, but if you do, set them before including t | Option | Type | Description | Default | |-----------------------------------|:--------------:|----------------------------------------------------------------------------------------------------------|------------------------| -| `TOML_API` | define | API annotation to add to public symbols (e.g. `__declspec(dllexport)` on Windows). | undefined | | `TOML_ASSERT(expr)` | function macro | Sets the assert function used by the library. | `assert()` | | `TOML_CONFIG_HEADER` | string literal | Includes the given header file before the rest of the library. | undefined | | `TOML_ENABLE_FORMATTERS` | boolean | Enables the formatters. Set to `0` if you don't need them to improve compile times and binary size. | `1` | @@ -153,6 +152,10 @@ won't need to mess with these at all, but if you do, set them before including t | `TOML_ENABLE_UNRELEASED_FEATURES` | boolean | Enables support for [unreleased TOML language features]. | `0` | | `TOML_ENABLE_WINDOWS_COMPAT` | boolean | Enables support for transparent conversion between wide and narrow strings. | `1` on Windows | | `TOML_EXCEPTIONS` | boolean | Sets whether the library uses exceptions. | per compiler settings | +| `TOML_EXPORTED_CLASS` | define | API export annotation to add to classes. | undefined | +| `TOML_EXPORTED_MEMBER_FUNCTION` | define | API export annotation to add to non-static class member functions. | undefined | +| `TOML_EXPORTED_FREE_FUNCTION` | define | API export annotation to add to free functions. | undefined | +| `TOML_EXPORTED_STATIC_FUNCTION` | define | API export annotation to add to static functions. | undefined | | `TOML_HEADER_ONLY` | boolean | Disable this to explicitly control where toml++'s implementation is compiled (e.g. as part of a library).| `1` | | `TOML_IMPLEMENTATION` | define | Define this to enable compilation of the library's implementation when `TOML_HEADER_ONLY` == `0`. | undefined | | `TOML_OPTIONAL_TYPE` | type name | Overrides the `optional` type used by the library if you need [something better than std::optional]. | undefined | diff --git a/cpp.hint b/cpp.hint index 2799258..bd358c1 100644 --- a/cpp.hint +++ b/cpp.hint @@ -12,8 +12,10 @@ #define TOML_CONST_INLINE_GETTER inline #define TOML_CONSTRAINED_TEMPLATE(cond, ...) template <__VA_ARGS__> #define TOML_EMPTY_BASES -#define TOML_EXTERN -#define TOML_EXTERN_NOEXCEPT(...) +#define TOML_EXPORTED_CLASS +#define TOML_EXPORTED_FREE_FUNCTION +#define TOML_EXPORTED_MEMBER_FUNCTION +#define TOML_EXPORTED_STATIC_FUNCTION #define TOML_EXTERNAL_LINKAGE #define TOML_FLAGS_ENUM #define TOML_HIDDEN_CONSTRAINT(cond, ...) template <__VA_ARGS__> @@ -21,6 +23,7 @@ #define TOML_IMPL_NAMESPACE_START namespace toml::impl #define TOML_INTERNAL_LINKAGE static #define TOML_LIKELY(...) (__VA_ARGS__) +#define TOML_LIKELY_CASE #define TOML_NAMESPACE_END static_assert(true) #define TOML_NAMESPACE_START namespace toml #define TOML_NEVER_INLINE @@ -33,5 +36,4 @@ #define TOML_RETURNS_BY_THROWING #define TOML_TRIVIAL_ABI #define TOML_UNLIKELY(...) (__VA_ARGS__) -#define TOML_LIKELY_CASE #define TOML_UNLIKELY_CASE diff --git a/include/toml++/impl/array.h b/include/toml++/impl/array.h index f657373..7372933 100644 --- a/include/toml++/impl/array.h +++ b/include/toml++/impl/array.h @@ -261,7 +261,7 @@ TOML_NAMESPACE_START /// [ 3, 4, 5, 'six', 7, 8.0, 'nine' ] /// [ 3, 4, 5, 'six', 7, 8.0, 'nine', 'ten', [ 11, 12.0 ] ] /// \eout - class array : public node + class TOML_EXPORTED_CLASS array : public node { private: /// \cond @@ -272,7 +272,7 @@ TOML_NAMESPACE_START vector_type elems_; TOML_NODISCARD_CTOR - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION array(const impl::array_init_elem*, const impl::array_init_elem*); TOML_NODISCARD_CTOR @@ -280,13 +280,13 @@ TOML_NAMESPACE_START : array{ elems.begin(), elems.end() } {} - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void preinsertion_resize(size_t idx, size_t count); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void insert_at_back(impl::node_ptr&&); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION vector_iterator insert_at(const_vector_iterator, impl::node_ptr&&); template @@ -301,10 +301,10 @@ TOML_NAMESPACE_START } TOML_NODISCARD - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION size_t total_leaf_count() const noexcept; - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void flatten_child(array&& child, size_t& dest_index) noexcept; /// \endcond @@ -316,35 +316,22 @@ TOML_NAMESPACE_START using reference = node&; using const_reference = const node&; -#if TOML_LIFETIME_HOOKS - - TOML_NODISCARD_CTOR - array() noexcept - { - TOML_ARRAY_CREATED; - } - - ~array() noexcept - { - TOML_ARRAY_DESTROYED; - } - -#else - /// \brief Default constructor. TOML_NODISCARD_CTOR - array() noexcept = default; + TOML_EXPORTED_MEMBER_FUNCTION + array() noexcept; -#endif + TOML_EXPORTED_MEMBER_FUNCTION + ~array() noexcept; /// \brief Copy constructor. TOML_NODISCARD_CTOR - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION array(const array&); /// \brief Move constructor. TOML_NODISCARD_CTOR - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION array(array&& other) noexcept; /// \brief Constructs an array with one or more initial elements. @@ -390,11 +377,11 @@ TOML_NAMESPACE_START {} /// \brief Copy-assignment operator. - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION array& operator=(const array&); /// \brief Move-assignment operator. - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION array& operator=(array&& rhs) noexcept; /// \name Type checks @@ -408,15 +395,15 @@ TOML_NAMESPACE_START } TOML_PURE_GETTER - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION bool is_homogeneous(node_type ntype) const noexcept final; TOML_PURE_GETTER - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept final; TOML_PURE_GETTER - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept final; /// \cond @@ -746,7 +733,7 @@ TOML_NAMESPACE_START /// \brief Gets a reference to the element at a specific index, throwing `std::out_of_range` if none existed. TOML_NODISCARD - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION node& at(size_t index); /// \brief Gets a reference to the element at a specific index, throwing `std::out_of_range` if none existed. @@ -871,11 +858,11 @@ TOML_NAMESPACE_START } /// \brief Reserves internal storage capacity up to a pre-determined number of elements. - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void reserve(size_t new_capacity); /// \brief Requests the removal of any unused internal storage capacity. - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void shrink_to_fit(); /// \brief Shrinks the array to the given size. @@ -900,7 +887,7 @@ TOML_NAMESPACE_START /// \eout /// /// \remarks Does nothing if the requested size is larger than or equal to the current size. - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void truncate(size_t new_size); /// \brief Resizes the array. @@ -969,7 +956,7 @@ TOML_NAMESPACE_START /// \param pos Iterator to the element being erased. /// /// \returns Iterator to the first element immediately following the removed element. - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION iterator erase(const_iterator pos) noexcept; /// \brief Removes the elements in the range [first, last) from the array. @@ -991,7 +978,7 @@ TOML_NAMESPACE_START /// \param last Iterator to the one-past-the-last element being erased. /// /// \returns Iterator to the first element immediately following the last removed element. - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION iterator erase(const_iterator first, const_iterator last) noexcept; /// \brief Flattens this array, recursively hoisting the contents of child arrays up into itself. @@ -1013,7 +1000,7 @@ TOML_NAMESPACE_START /// \remarks Arrays inside child tables are not flattened. /// /// \returns A reference to the array. - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION array& flatten() &; /// \brief Flattens this array, recursively hoisting the contents of child arrays up into itself (rvalue overload). @@ -1043,7 +1030,7 @@ TOML_NAMESPACE_START /// \param recursive Should child arrays and tables themselves be pruned? /// /// \returns A reference to the array. - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION array& prune(bool recursive = true) & noexcept; /// \brief Removes empty child arrays and tables (rvalue overload). @@ -1057,11 +1044,11 @@ TOML_NAMESPACE_START } /// \brief Removes the last element from the array. - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void pop_back() noexcept; /// \brief Removes all elements from the array. - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void clear() noexcept; /// @} @@ -1406,7 +1393,7 @@ TOML_NAMESPACE_START /// \cond TOML_NODISCARD - TOML_API + TOML_EXPORTED_STATIC_FUNCTION static bool equal(const array&, const array&) noexcept; template diff --git a/include/toml++/impl/array.inl b/include/toml++/impl/array.inl index 7b691ae..32629e6 100644 --- a/include/toml++/impl/array.inl +++ b/include/toml++/impl/array.inl @@ -16,6 +16,22 @@ TOML_NAMESPACE_START { + TOML_EXTERNAL_LINKAGE + array::array() noexcept + { +#if TOML_LIFETIME_HOOKS + TOML_ARRAY_CREATED; +#endif + } + + TOML_EXTERNAL_LINKAGE + array::~array() noexcept + { +#if TOML_LIFETIME_HOOKS + TOML_ARRAY_DESTROYED; +#endif + } + TOML_EXTERNAL_LINKAGE array::array(const impl::array_init_elem* b, const impl::array_init_elem* e) { diff --git a/include/toml++/impl/at_path.h b/include/toml++/impl/at_path.h index 4026c56..0449cac 100644 --- a/include/toml++/impl/at_path.h +++ b/include/toml++/impl/at_path.h @@ -47,14 +47,14 @@ TOML_NAMESPACE_START /// \param root The root node from which the path will be traversed. /// \param path The "TOML path" to traverse. TOML_NODISCARD - TOML_API + TOML_EXPORTED_FREE_FUNCTION node_view at_path(node & root, std::string_view path) noexcept; /// \brief Returns a const view of the node matching a fully-qualified "TOML path". /// /// \see #toml::at_path(node&, std::string_view) TOML_NODISCARD - TOML_API + TOML_EXPORTED_FREE_FUNCTION node_view at_path(const node& root, std::string_view path) noexcept; #if TOML_ENABLE_WINDOWS_COMPAT @@ -65,7 +65,7 @@ TOML_NAMESPACE_START /// /// \see #toml::at_path(node&, std::string_view) TOML_NODISCARD - TOML_API + TOML_EXPORTED_FREE_FUNCTION node_view at_path(node & root, std::wstring_view path); /// \brief Returns a const view of the node matching a fully-qualified "TOML path". @@ -74,7 +74,7 @@ TOML_NAMESPACE_START /// /// \see #toml::at_path(node&, std::string_view) TOML_NODISCARD - TOML_API + TOML_EXPORTED_FREE_FUNCTION node_view at_path(const node& root, std::wstring_view path); #endif diff --git a/include/toml++/impl/at_path.inl b/include/toml++/impl/at_path.inl index a2aa8f9..253c3a5 100644 --- a/include/toml++/impl/at_path.inl +++ b/include/toml++/impl/at_path.inl @@ -37,7 +37,7 @@ TOML_ANON_NAMESPACE_START bool prev_was_array_indexer = false; bool prev_was_dot = root.is_table(); // implicit '.' at the start for tables - do + while (pos < end && current) { // start of an array indexer if (path[pos] == '[') @@ -153,7 +153,6 @@ TOML_ANON_NAMESPACE_START prev_was_array_indexer = false; } } - while (pos < end && current); // a dot at the end is as if we'd asked for an empty child at the end, e.g. // diff --git a/include/toml++/impl/formatter.h b/include/toml++/impl/formatter.h index 6b85b85..47143eb 100644 --- a/include/toml++/impl/formatter.h +++ b/include/toml++/impl/formatter.h @@ -33,7 +33,7 @@ TOML_IMPL_NAMESPACE_START std::string_view indent; }; - class formatter + class TOML_EXPORTED_CLASS formatter { private: const node* source_; @@ -124,57 +124,57 @@ TOML_IMPL_NAMESPACE_START return !!(config_.flags & format_flags::allow_unicode_strings); } - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void attach(std::ostream& stream) noexcept; - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void detach() noexcept; - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void print_newline(bool force = false); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void print_indent(); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void print_unformatted(char); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void print_unformatted(std::string_view); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void print_string(std::string_view str, bool allow_multi_line = true, bool allow_bare = false); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void print(const value&); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void print(const value&); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void print(const value&); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void print(const value&); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void print(const value&); - TOML_API + TOML_EXPORTED_MEMBER_FUNCTION void print(const value