diff --git a/bazel/core.bzl b/bazel/core.bzl index 185d99dd0..54812eccd 100644 --- a/bazel/core.bzl +++ b/bazel/core.bzl @@ -586,6 +586,7 @@ MLN_CORE_SOURCE = [ "src/mbgl/util/quaternion.hpp", "src/mbgl/util/rapidjson.cpp", "src/mbgl/util/rapidjson.hpp", + "src/mbgl/util/source_location.hpp", "src/mbgl/util/std.hpp", "src/mbgl/util/stopwatch.cpp", "src/mbgl/util/stopwatch.hpp", diff --git a/src/mbgl/layout/symbol_instance.cpp b/src/mbgl/layout/symbol_instance.cpp index 6ca7c6f2f..98ae74def 100644 --- a/src/mbgl/layout/symbol_instance.cpp +++ b/src/mbgl/layout/symbol_instance.cpp @@ -235,7 +235,7 @@ std::optional SymbolInstance::getDefaultHorizontalPlacedTextIndex() cons } #if MLN_SYMBOL_GUARDS -bool SymbolInstance::check(const std::source_location& source) const { +bool SymbolInstance::check(const source_location& source) const { return !isFailed && check(check01, 1, source) && check(check02, 2, source) && check(check03, 3, source) && check(check04, 4, source) && check(check05, 5, source) && check(check06, 6, source) && check(check07, 7, source) && check(check08, 8, source) && check(check09, 9, source) && @@ -251,7 +251,7 @@ bool SymbolInstance::check(const std::source_location& source) const { bool SymbolInstance::checkIndexes(std::size_t textCount, std::size_t iconSize, std::size_t sdfSize, - const std::source_location& source) const { + const source_location& source) const { return !isFailed && checkIndex(placedRightTextIndex, textCount, source) && checkIndex(placedCenterTextIndex, textCount, source) && checkIndex(placedLeftTextIndex, textCount, source) && checkIndex(placedVerticalTextIndex, textCount, source) && @@ -260,12 +260,12 @@ bool SymbolInstance::checkIndexes(std::size_t textCount, } namespace { -inline std::string locationSuffix(const std::source_location& source) { +inline std::string locationSuffix(const source_location& source) { return std::string(" from ") + source.function_name() + " (" + source.file_name() + ":" + util::toString(source.line()) + ")"; } } // namespace -bool SymbolInstance::check(std::uint64_t v, int n, const std::source_location& source) const { +bool SymbolInstance::check(std::uint64_t v, int n, const source_location& source) const { if (!isFailed && v != checkVal) { isFailed = true; Log::Error(Event::Crash, @@ -275,7 +275,7 @@ bool SymbolInstance::check(std::uint64_t v, int n, const std::source_location& s return !isFailed; } -bool SymbolInstance::checkKey(const std::source_location& source) const { +bool SymbolInstance::checkKey(const source_location& source) const { if (!isFailed && key.size() > 10000) { // largest observed value=62 isFailed = true; Log::Error(Event::Crash, @@ -286,7 +286,7 @@ bool SymbolInstance::checkKey(const std::source_location& source) const { bool SymbolInstance::checkIndex(const std::optional& index, std::size_t size, - const std::source_location& source) const { + const source_location& source) const { if (index.has_value() && *index >= size) { isFailed = true; Log::Error(Event::Crash, diff --git a/src/mbgl/layout/symbol_instance.hpp b/src/mbgl/layout/symbol_instance.hpp index 479058d7e..eadf3b15e 100644 --- a/src/mbgl/layout/symbol_instance.hpp +++ b/src/mbgl/layout/symbol_instance.hpp @@ -4,8 +4,9 @@ #include #include #include +#include -#include +#include #ifndef MLN_SYMBOL_GUARDS #define MLN_SYMBOL_GUARDS 1 @@ -17,32 +18,8 @@ #define SYM_GUARD_VALUE(N) #endif -// A temporary shim for partial C++20 support #if MLN_SYMBOL_GUARDS -#ifdef __clang__ -#if __cplusplus <= 201703L || !__has_builtin(__builtin_source_location) -namespace std { -struct source_location { - const char* fileName_; - const char* functionName_; - unsigned line_; - - constexpr uint_least32_t line() const noexcept { return line_; } - constexpr uint_least32_t column() const noexcept { return 0; } - constexpr const char* file_name() const noexcept { return fileName_; } - constexpr const char* function_name() const noexcept { return functionName_; } -}; -} // namespace std -#define SYM_GUARD_LOC \ - std::source_location { \ - __FILE__, __FUNCTION__, __LINE__ \ - } -#else -#define SYM_GUARD_LOC std::source_location::current() -#endif -#else -#define SYM_GUARD_LOC std::source_location::current() -#endif +#define SYM_GUARD_LOC MLN_CURRENT_SOURCE_LOCATION #else #define SYM_GUARD_LOC \ { \ @@ -139,14 +116,11 @@ public: #if MLN_SYMBOL_GUARDS /// Check all guard blocks - bool check(const std::source_location&) const; + bool check(const source_location&) const; /// Check that an index is in the valid range - bool checkIndex(const std::optional& index, std::size_t size, const std::source_location&) const; + bool checkIndex(const std::optional& index, std::size_t size, const source_location&) const; /// Check all indexes - bool checkIndexes(std::size_t textCount, - std::size_t iconSize, - std::size_t sdfSize, - const std::source_location&) const; + bool checkIndexes(std::size_t textCount, std::size_t iconSize, std::size_t sdfSize, const source_location&) const; /// Mark this item as failed (due to some external check) so that it cannot be used later void forceFail() const; #else @@ -206,8 +180,8 @@ public: protected: #if MLN_SYMBOL_GUARDS - bool check(std::uint64_t v, int n, const std::source_location&) const; - bool checkKey(const std::source_location&) const; + bool check(std::uint64_t v, int n, const source_location&) const; + bool checkKey(const source_location&) const; void forceFailInternal(); // this is just to avoid warnings about the values never being set #else bool checkKey(std::string_view) const { return true; } diff --git a/src/mbgl/renderer/bucket.hpp b/src/mbgl/renderer/bucket.hpp index 319a0607b..5696a7232 100644 --- a/src/mbgl/renderer/bucket.hpp +++ b/src/mbgl/renderer/bucket.hpp @@ -71,7 +71,7 @@ public: const util::SimpleIdentity& getID() const { return bucketID; } #if MLN_SYMBOL_GUARDS - virtual bool check(std::source_location) { return true; } + virtual bool check(source_location) { return true; } #else bool check(std::string_view = {}) { return true; } #endif diff --git a/src/mbgl/renderer/buckets/symbol_bucket.cpp b/src/mbgl/renderer/buckets/symbol_bucket.cpp index 99cb6b998..8f2922b76 100644 --- a/src/mbgl/renderer/buckets/symbol_bucket.cpp +++ b/src/mbgl/renderer/buckets/symbol_bucket.cpp @@ -235,7 +235,7 @@ SymbolInstanceReferences SymbolBucket::getSymbols(const std::optional& sortKeyRange = std::nullopt) const; #if MLN_SYMBOL_GUARDS - bool check(std::source_location) override; + bool check(source_location) override; #endif static SymbolLayoutVertex layoutVertex(Point labelAnchor, diff --git a/src/mbgl/util/source_location.hpp b/src/mbgl/util/source_location.hpp new file mode 100644 index 000000000..a9d089eda --- /dev/null +++ b/src/mbgl/util/source_location.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include + +#if defined(__has_include) +#if __has_include() +#include +#endif +#endif + +namespace mbgl { + +#if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L +using source_location = std::source_location; +#define MLN_CURRENT_SOURCE_LOCATION ::mbgl::source_location::current() +#else +class source_location final { +public: + constexpr source_location(const char* fileName_ = "", + const char* functionName_ = "", + std::uint_least32_t line_ = 0) noexcept + : fileName(fileName_), + functionName(functionName_), + lineNumber(line_) {} + + constexpr std::uint_least32_t line() const noexcept { return lineNumber; } + constexpr std::uint_least32_t column() const noexcept { return 0; } + constexpr const char* file_name() const noexcept { return fileName; } + constexpr const char* function_name() const noexcept { return functionName; } + +private: + const char* fileName; + const char* functionName; + std::uint_least32_t lineNumber; +}; +#define MLN_CURRENT_SOURCE_LOCATION \ + ::mbgl::source_location { \ + __FILE__, __func__, __LINE__ \ + } +#endif + +} // namespace mbgl