Skip to content

Commit f6d70e9

Browse files
core: Add source location fallback (maplibre#4318)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5602ad8 commit f6d70e9

7 files changed

Lines changed: 60 additions & 43 deletions

File tree

bazel/core.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ MLN_CORE_SOURCE = [
586586
"src/mbgl/util/quaternion.hpp",
587587
"src/mbgl/util/rapidjson.cpp",
588588
"src/mbgl/util/rapidjson.hpp",
589+
"src/mbgl/util/source_location.hpp",
589590
"src/mbgl/util/std.hpp",
590591
"src/mbgl/util/stopwatch.cpp",
591592
"src/mbgl/util/stopwatch.hpp",

src/mbgl/layout/symbol_instance.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ std::optional<size_t> SymbolInstance::getDefaultHorizontalPlacedTextIndex() cons
235235
}
236236

237237
#if MLN_SYMBOL_GUARDS
238-
bool SymbolInstance::check(const std::source_location& source) const {
238+
bool SymbolInstance::check(const source_location& source) const {
239239
return !isFailed && check(check01, 1, source) && check(check02, 2, source) && check(check03, 3, source) &&
240240
check(check04, 4, source) && check(check05, 5, source) && check(check06, 6, source) &&
241241
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 {
251251
bool SymbolInstance::checkIndexes(std::size_t textCount,
252252
std::size_t iconSize,
253253
std::size_t sdfSize,
254-
const std::source_location& source) const {
254+
const source_location& source) const {
255255
return !isFailed && checkIndex(placedRightTextIndex, textCount, source) &&
256256
checkIndex(placedCenterTextIndex, textCount, source) && checkIndex(placedLeftTextIndex, textCount, source) &&
257257
checkIndex(placedVerticalTextIndex, textCount, source) &&
@@ -260,12 +260,12 @@ bool SymbolInstance::checkIndexes(std::size_t textCount,
260260
}
261261

262262
namespace {
263-
inline std::string locationSuffix(const std::source_location& source) {
263+
inline std::string locationSuffix(const source_location& source) {
264264
return std::string(" from ") + source.function_name() + " (" + source.file_name() + ":" +
265265
util::toString(source.line()) + ")";
266266
}
267267
} // namespace
268-
bool SymbolInstance::check(std::uint64_t v, int n, const std::source_location& source) const {
268+
bool SymbolInstance::check(std::uint64_t v, int n, const source_location& source) const {
269269
if (!isFailed && v != checkVal) {
270270
isFailed = true;
271271
Log::Error(Event::Crash,
@@ -275,7 +275,7 @@ bool SymbolInstance::check(std::uint64_t v, int n, const std::source_location& s
275275
return !isFailed;
276276
}
277277

278-
bool SymbolInstance::checkKey(const std::source_location& source) const {
278+
bool SymbolInstance::checkKey(const source_location& source) const {
279279
if (!isFailed && key.size() > 10000) { // largest observed value=62
280280
isFailed = true;
281281
Log::Error(Event::Crash,
@@ -286,7 +286,7 @@ bool SymbolInstance::checkKey(const std::source_location& source) const {
286286

287287
bool SymbolInstance::checkIndex(const std::optional<std::size_t>& index,
288288
std::size_t size,
289-
const std::source_location& source) const {
289+
const source_location& source) const {
290290
if (index.has_value() && *index >= size) {
291291
isFailed = true;
292292
Log::Error(Event::Crash,

src/mbgl/layout/symbol_instance.hpp

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
#include <mbgl/text/collision_feature.hpp>
55
#include <mbgl/style/layers/symbol_layer_properties.hpp>
66
#include <mbgl/util/bitmask_operations.hpp>
7+
#include <mbgl/util/source_location.hpp>
78

8-
#include <source_location>
9+
#include <cstdint>
910

1011
#ifndef MLN_SYMBOL_GUARDS
1112
#define MLN_SYMBOL_GUARDS 1
@@ -17,32 +18,8 @@
1718
#define SYM_GUARD_VALUE(N)
1819
#endif
1920

20-
// A temporary shim for partial C++20 support
2121
#if MLN_SYMBOL_GUARDS
22-
#ifdef __clang__
23-
#if __cplusplus <= 201703L || !__has_builtin(__builtin_source_location)
24-
namespace std {
25-
struct source_location {
26-
const char* fileName_;
27-
const char* functionName_;
28-
unsigned line_;
29-
30-
constexpr uint_least32_t line() const noexcept { return line_; }
31-
constexpr uint_least32_t column() const noexcept { return 0; }
32-
constexpr const char* file_name() const noexcept { return fileName_; }
33-
constexpr const char* function_name() const noexcept { return functionName_; }
34-
};
35-
} // namespace std
36-
#define SYM_GUARD_LOC \
37-
std::source_location { \
38-
__FILE__, __FUNCTION__, __LINE__ \
39-
}
40-
#else
41-
#define SYM_GUARD_LOC std::source_location::current()
42-
#endif
43-
#else
44-
#define SYM_GUARD_LOC std::source_location::current()
45-
#endif
22+
#define SYM_GUARD_LOC MLN_CURRENT_SOURCE_LOCATION
4623
#else
4724
#define SYM_GUARD_LOC \
4825
{ \
@@ -139,14 +116,11 @@ class SymbolInstance {
139116

140117
#if MLN_SYMBOL_GUARDS
141118
/// Check all guard blocks
142-
bool check(const std::source_location&) const;
119+
bool check(const source_location&) const;
143120
/// Check that an index is in the valid range
144-
bool checkIndex(const std::optional<std::size_t>& index, std::size_t size, const std::source_location&) const;
121+
bool checkIndex(const std::optional<std::size_t>& index, std::size_t size, const source_location&) const;
145122
/// Check all indexes
146-
bool checkIndexes(std::size_t textCount,
147-
std::size_t iconSize,
148-
std::size_t sdfSize,
149-
const std::source_location&) const;
123+
bool checkIndexes(std::size_t textCount, std::size_t iconSize, std::size_t sdfSize, const source_location&) const;
150124
/// Mark this item as failed (due to some external check) so that it cannot be used later
151125
void forceFail() const;
152126
#else
@@ -206,8 +180,8 @@ class SymbolInstance {
206180

207181
protected:
208182
#if MLN_SYMBOL_GUARDS
209-
bool check(std::uint64_t v, int n, const std::source_location&) const;
210-
bool checkKey(const std::source_location&) const;
183+
bool check(std::uint64_t v, int n, const source_location&) const;
184+
bool checkKey(const source_location&) const;
211185
void forceFailInternal(); // this is just to avoid warnings about the values never being set
212186
#else
213187
bool checkKey(std::string_view) const { return true; }

src/mbgl/renderer/bucket.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Bucket {
7171
const util::SimpleIdentity& getID() const { return bucketID; }
7272

7373
#if MLN_SYMBOL_GUARDS
74-
virtual bool check(std::source_location) { return true; }
74+
virtual bool check(source_location) { return true; }
7575
#else
7676
bool check(std::string_view = {}) { return true; }
7777
#endif

src/mbgl/renderer/buckets/symbol_bucket.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ SymbolInstanceReferences SymbolBucket::getSymbols(const std::optional<SortKeyRan
235235
}
236236

237237
#if MLN_SYMBOL_GUARDS
238-
bool SymbolBucket::check(std::source_location source) {
238+
bool SymbolBucket::check(source_location source) {
239239
if (text.vertices().elements() != text.dynamicVertices().elements() ||
240240
text.vertices().elements() != text.opacityVertices().elements() ||
241241
icon.vertices().elements() != icon.dynamicVertices().elements() ||

src/mbgl/renderer/buckets/symbol_bucket.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class SymbolBucket final : public Bucket {
251251
SymbolInstanceReferences getSymbols(const std::optional<SortKeyRange>& sortKeyRange = std::nullopt) const;
252252

253253
#if MLN_SYMBOL_GUARDS
254-
bool check(std::source_location) override;
254+
bool check(source_location) override;
255255
#endif
256256

257257
static SymbolLayoutVertex layoutVertex(Point<float> labelAnchor,

src/mbgl/util/source_location.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
#if defined(__has_include)
6+
#if __has_include(<source_location>)
7+
#include <source_location>
8+
#endif
9+
#endif
10+
11+
namespace mbgl {
12+
13+
#if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
14+
using source_location = std::source_location;
15+
#define MLN_CURRENT_SOURCE_LOCATION ::mbgl::source_location::current()
16+
#else
17+
class source_location final {
18+
public:
19+
constexpr source_location(const char* fileName_ = "",
20+
const char* functionName_ = "",
21+
std::uint_least32_t line_ = 0) noexcept
22+
: fileName(fileName_),
23+
functionName(functionName_),
24+
lineNumber(line_) {}
25+
26+
constexpr std::uint_least32_t line() const noexcept { return lineNumber; }
27+
constexpr std::uint_least32_t column() const noexcept { return 0; }
28+
constexpr const char* file_name() const noexcept { return fileName; }
29+
constexpr const char* function_name() const noexcept { return functionName; }
30+
31+
private:
32+
const char* fileName;
33+
const char* functionName;
34+
std::uint_least32_t lineNumber;
35+
};
36+
#define MLN_CURRENT_SOURCE_LOCATION \
37+
::mbgl::source_location { \
38+
__FILE__, __func__, __LINE__ \
39+
}
40+
#endif
41+
42+
} // namespace mbgl

0 commit comments

Comments
 (0)