Skip to content

Commit c5cf0fe

Browse files
authored
Merge pull request #325 from firebase/g3merge11
Firestore: Migrate hard assertions to common infrastructure too
2 parents 678ea76 + 3c6ff8b commit c5cf0fe

15 files changed

+122
-86
lines changed

firestore/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ set(common_SRCS
3434
src/common/firestore.cc
3535
src/common/futures.cc
3636
src/common/futures.h
37+
src/common/hard_assert_common.cc
38+
src/common/hard_assert_common.h
3739
src/common/listener_registration.cc
3840
src/common/macros.h
3941
src/common/main_for_testing_build.cc
@@ -185,7 +187,6 @@ set(main_SRCS
185187
src/ios/field_value_ios.h
186188
src/ios/firestore_ios.cc
187189
src/ios/firestore_ios.h
188-
src/ios/hard_assert_ios.h
189190
src/ios/listener_ios.h
190191
src/ios/listener_registration_ios.cc
191192
src/ios/listener_registration_ios.h
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "firestore/src/common/hard_assert_common.h"
2+
3+
#include "firestore/src/common/exception_common.h"
4+
5+
namespace firebase {
6+
namespace firestore {
7+
namespace util {
8+
namespace internal {
9+
10+
#if defined(__ANDROID__)
11+
12+
void FailAssertion(const char* file, const char* func, const int line,
13+
const std::string& message) {
14+
Throw(ExceptionType::AssertionFailure, file, func, line, message);
15+
}
16+
17+
void FailAssertion(const char* file, const char* func, const int line,
18+
const std::string& message, const char* condition) {
19+
std::string failure;
20+
if (message.empty()) {
21+
failure = condition;
22+
} else {
23+
failure = message + " (expected " + condition + ")";
24+
}
25+
Throw(ExceptionType::AssertionFailure, file, func, line, failure);
26+
}
27+
28+
#endif // defined(__ANDROID__)
29+
30+
} // namespace internal
31+
} // namespace util
32+
} // namespace firestore
33+
} // namespace firebase
Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Google
2+
* Copyright 2019 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,24 +14,23 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_IOS_HARD_ASSERT_IOS_H_
18-
#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_IOS_HARD_ASSERT_IOS_H_
17+
#ifndef FIREBASE_FIRESTORE_CLIENT_CPP_SRC_COMMON_HARD_ASSERT_COMMON_H_
18+
#define FIREBASE_FIRESTORE_CLIENT_CPP_SRC_COMMON_HARD_ASSERT_COMMON_H_
19+
20+
// TODO(b/163140650): Remove this/unify with the iOS implementation.
21+
// On Android we still support customers building with STLPort, which precludes
22+
// use of Abseil here.
1923

2024
#include <string>
2125
#include <utility>
2226

23-
#include "absl/base/optimization.h"
24-
#include "Firestore/core/src/util/exception.h"
27+
#include "firestore/src/common/macros.h"
2528

26-
// TODO(b/147444199): delete this file and use the one that comes from the
27-
// GitHub repo. This file provides simplified versions of `HARD_ASSERT`,
28-
// `HARD_FAIL`, and `ThrowInvalidArgument` that don't support string formatting.
29+
#if !defined(__ANDROID__)
30+
#include "Firestore/core/src/util/hard_assert.h"
31+
#endif // !defined(__ANDROID__)
2932

30-
#if defined(_MSC_VER)
31-
#define FIRESTORE_FUNCTION_NAME __FUNCSIG__
32-
#else
33-
#define FIRESTORE_FUNCTION_NAME __PRETTY_FUNCTION__
34-
#endif
33+
#if defined(__ANDROID__)
3534

3635
/**
3736
* Invokes the internal Fail function below with all the required contextual
@@ -42,22 +41,24 @@
4241
*/
4342
#define INVOKE_INTERNAL_FAIL(...) \
4443
firebase::firestore::util::internal::FailAssertion( \
45-
__FILE__, FIRESTORE_FUNCTION_NAME, __LINE__, __VA_ARGS__)
44+
__FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
45+
46+
#endif // !defined(__ANDROID__)
4647

4748
/**
4849
* Fails the current function if the given condition is false.
4950
*
5051
* Unlike assert(3) or NSAssert, this macro is never compiled out.
5152
*
53+
* Note: this version of `HARD_ASSERT` is deliberately simplified to avoid
54+
* using `util::StringFormat`.
55+
*
5256
* @param condition The condition to test.
5357
* @param message (optional) A message to print.
5458
*/
55-
56-
// Note: this version of `HARD_ASSERT` is deliberately dumbed down to avoid
57-
// using `util::StringFormat`.
58-
#define HARD_ASSERT_IOS(condition, ...) \
59+
#define SIMPLE_HARD_ASSERT(condition, ...) \
5960
do { \
60-
if (!ABSL_PREDICT_TRUE(condition)) { \
61+
if (!FIRESTORE_PREDICT_TRUE(condition)) { \
6162
std::string _message{__VA_ARGS__}; \
6263
INVOKE_INTERNAL_FAIL(_message, #condition); \
6364
} \
@@ -70,18 +71,13 @@
7071
*
7172
* @param message (optional) A message to print.
7273
*/
73-
#define HARD_FAIL_IOS(...) \
74+
#define SIMPLE_HARD_FAIL(...) \
7475
do { \
7576
std::string _failure{__VA_ARGS__}; \
7677
INVOKE_INTERNAL_FAIL(_failure); \
7778
} while (0)
7879

79-
/**
80-
* Indicates an area of the code that cannot be reached (except possibly due to
81-
* undefined behaviour or other similar badness). The only reasonable thing to
82-
* do in these cases is to immediately abort.
83-
*/
84-
#define UNREACHABLE() abort()
80+
#if defined(__ANDROID__)
8581

8682
/**
8783
* Returns the given `ptr` if it is non-null; otherwise, results in a failed
@@ -96,7 +92,7 @@
9692
* @param ptr The pointer to check and return. Can be a smart pointer.
9793
*/
9894
#define NOT_NULL(ptr) \
99-
(static_cast<void>(ABSL_PREDICT_FALSE((ptr) == nullptr) \
95+
(static_cast<void>(FIRESTORE_PREDICT_FALSE((ptr) == nullptr) \
10096
? INVOKE_INTERNAL_FAIL("Expected non-null " #ptr) \
10197
: static_cast<void>(0)), \
10298
(ptr)) // NOLINT(whitespace/indent)
@@ -107,17 +103,22 @@ namespace util {
107103
namespace internal {
108104

109105
// A no-return helper function. To raise an assertion, use Macro instead.
110-
ABSL_ATTRIBUTE_NORETURN void FailAssertion(const char* file, const char* func,
111-
int line,
112-
const std::string& message);
106+
// These symbols are in the util::internal namespace to match their iOS
107+
// equivalents.
108+
FIRESTORE_ATTRIBUTE_NORETURN void FailAssertion(const char* file,
109+
const char* func, int line,
110+
const std::string& message);
113111

114-
ABSL_ATTRIBUTE_NORETURN void FailAssertion(const char* file, const char* func,
115-
int line, const std::string& message,
116-
const char* condition);
112+
FIRESTORE_ATTRIBUTE_NORETURN void FailAssertion(const char* file,
113+
const char* func, int line,
114+
const std::string& message,
115+
const char* condition);
117116

118117
} // namespace internal
119118
} // namespace util
120119
} // namespace firestore
121120
} // namespace firebase
122121

123-
#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_IOS_HARD_ASSERT_IOS_H_
122+
#endif // defined(__ANDROID__)
123+
124+
#endif // FIREBASE_FIRESTORE_CLIENT_CPP_SRC_COMMON_HARD_ASSERT_COMMON_H_

firestore/src/ios/credentials_provider_desktop.cc

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "app/src/function_registry.h"
77
#include "app/src/reference_counted_future_impl.h"
88
#include "firestore/src/common/futures.h"
9-
#include "firestore/src/ios/hard_assert_ios.h"
9+
#include "firestore/src/common/hard_assert_common.h"
1010
#include "firebase/firestore/firestore_errors.h"
1111
#include "Firestore/core/src/util/status.h"
1212

@@ -79,8 +79,9 @@ StatusOr<Token> ConvertToken(const Future<std::string>& future, App& app) {
7979
void OnToken(const Future<std::string>& future_token, App& app,
8080
int token_generation, const TokenListener& listener,
8181
int expected_generation) {
82-
HARD_ASSERT_IOS(future_token.status() == FutureStatus::kFutureStatusComplete,
83-
"Expected to receive a completed future");
82+
SIMPLE_HARD_ASSERT(
83+
future_token.status() == FutureStatus::kFutureStatusComplete,
84+
"Expected to receive a completed future");
8485

8586
if (expected_generation != token_generation) {
8687
// Cancel the request since the user may have changed while the request was
@@ -109,14 +110,14 @@ void FirebaseCppCredentialsProvider::SetCredentialChangeListener(
109110
std::lock_guard<std::recursive_mutex> lock(contents_->mutex);
110111

111112
if (!listener) {
112-
HARD_ASSERT_IOS(change_listener_,
113-
"Change listener removed without being set!");
113+
SIMPLE_HARD_ASSERT(change_listener_,
114+
"Change listener removed without being set!");
114115
change_listener_ = {};
115116
RemoveAuthStateListener();
116117
return;
117118
}
118119

119-
HARD_ASSERT_IOS(!change_listener_, "Set change listener twice!");
120+
SIMPLE_HARD_ASSERT(!change_listener_, "Set change listener twice!");
120121
change_listener_ = std::move(listener);
121122
change_listener_(GetCurrentUser(contents_->app));
122123
}
@@ -177,8 +178,8 @@ void FirebaseCppCredentialsProvider::OnAuthStateChanged(void* context) {
177178
// Private member functions
178179

179180
void FirebaseCppCredentialsProvider::RequestToken(TokenListener listener) {
180-
HARD_ASSERT_IOS(IsSignedIn(),
181-
"Cannot get token when there is no signed-in user");
181+
SIMPLE_HARD_ASSERT(IsSignedIn(),
182+
"Cannot get token when there is no signed-in user");
182183

183184
// Take note of the current value of `token_generation` so that this request
184185
// can fail if there is a token change while the request is outstanding.

firestore/src/ios/document_change_ios.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
#include <utility>
44

5+
#include "firestore/src/common/hard_assert_common.h"
56
#include "firestore/src/common/macros.h"
67
#include "firestore/src/ios/converter_ios.h"
78
#include "firestore/src/ios/document_snapshot_ios.h"
8-
#include "firestore/src/ios/hard_assert_ios.h"
99
#include "firestore/src/ios/util_ios.h"
1010

1111
namespace firebase {

firestore/src/ios/document_snapshot_ios.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ MapFieldValue DocumentSnapshotInternal::GetData(
6060
const Map& map =
6161
maybe_object ? maybe_object.value().GetInternalValue() : Map{};
6262
FieldValue result = ConvertObject(map, stb);
63-
HARD_ASSERT_IOS(result.type() == FieldValue::Type::kMap,
64-
"Expected snapshot data to parse to a map");
63+
SIMPLE_HARD_ASSERT(result.type() == FieldValue::Type::kMap,
64+
"Expected snapshot data to parse to a map");
6565
return result.map_value();
6666
}
6767

@@ -143,14 +143,14 @@ FieldValue DocumentSnapshotInternal::ConvertScalar(
143143
// HARD_FAIL("Unexpected kind of FieldValue: '%s'", scalar.type());
144144
auto message = std::string("Unexpected kind of FieldValue: '") +
145145
std::to_string(static_cast<int>(scalar.type())) + "'";
146-
HARD_FAIL_IOS(message.c_str());
146+
SIMPLE_HARD_FAIL(message);
147147
}
148148
}
149149
}
150150

151151
FieldValue DocumentSnapshotInternal::ConvertReference(
152152
const model::FieldValue::Reference& reference) const {
153-
HARD_ASSERT_IOS(
153+
SIMPLE_HARD_ASSERT(
154154
reference.database_id() == firestore_internal()->database_id(),
155155
"Converted reference is from another database");
156156

firestore/src/ios/field_value_ios.cc

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
#include <utility>
44

5+
#include "firestore/src/common/hard_assert_common.h"
56
#include "firestore/src/common/macros.h"
67
#include "firestore/src/include/firebase/firestore/map_field_value.h"
78
#include "firestore/src/ios/converter_ios.h"
8-
#include "firestore/src/ios/hard_assert_ios.h"
99
#include "Firestore/core/src/nanopb/byte_string.h"
1010

1111
namespace firebase {
@@ -57,72 +57,72 @@ FieldValueInternal::FieldValueInternal(MapFieldValue value)
5757
// Accessors
5858

5959
bool FieldValueInternal::boolean_value() const {
60-
HARD_ASSERT_IOS(type_ == Type::kBoolean);
60+
SIMPLE_HARD_ASSERT(type_ == Type::kBoolean);
6161
return absl::get<model::FieldValue>(value_).boolean_value();
6262
}
6363

6464
int64_t FieldValueInternal::integer_value() const {
65-
HARD_ASSERT_IOS(type_ == Type::kInteger);
65+
SIMPLE_HARD_ASSERT(type_ == Type::kInteger);
6666
return absl::get<model::FieldValue>(value_).integer_value();
6767
}
6868

6969
double FieldValueInternal::double_value() const {
70-
HARD_ASSERT_IOS(type_ == Type::kDouble);
70+
SIMPLE_HARD_ASSERT(type_ == Type::kDouble);
7171
return absl::get<model::FieldValue>(value_).double_value();
7272
}
7373

7474
Timestamp FieldValueInternal::timestamp_value() const {
75-
HARD_ASSERT_IOS(type_ == Type::kTimestamp);
75+
SIMPLE_HARD_ASSERT(type_ == Type::kTimestamp);
7676
return absl::get<model::FieldValue>(value_).timestamp_value();
7777
}
7878

7979
std::string FieldValueInternal::string_value() const {
80-
HARD_ASSERT_IOS(type_ == Type::kString);
80+
SIMPLE_HARD_ASSERT(type_ == Type::kString);
8181
return absl::get<model::FieldValue>(value_).string_value();
8282
}
8383

8484
const uint8_t* FieldValueInternal::blob_value() const {
85-
HARD_ASSERT_IOS(type_ == Type::kBlob);
85+
SIMPLE_HARD_ASSERT(type_ == Type::kBlob);
8686
return absl::get<model::FieldValue>(value_).blob_value().data();
8787
}
8888

8989
size_t FieldValueInternal::blob_size() const {
90-
HARD_ASSERT_IOS(type_ == Type::kBlob);
90+
SIMPLE_HARD_ASSERT(type_ == Type::kBlob);
9191
return absl::get<model::FieldValue>(value_).blob_value().size();
9292
}
9393

9494
DocumentReference FieldValueInternal::reference_value() const {
95-
HARD_ASSERT_IOS(type_ == Type::kReference);
95+
SIMPLE_HARD_ASSERT(type_ == Type::kReference);
9696
return absl::get<DocumentReference>(value_);
9797
}
9898

9999
GeoPoint FieldValueInternal::geo_point_value() const {
100-
HARD_ASSERT_IOS(type_ == Type::kGeoPoint);
100+
SIMPLE_HARD_ASSERT(type_ == Type::kGeoPoint);
101101
return absl::get<model::FieldValue>(value_).geo_point_value();
102102
}
103103

104104
std::vector<FieldValue> FieldValueInternal::array_value() const {
105-
HARD_ASSERT_IOS(type_ == Type::kArray);
105+
SIMPLE_HARD_ASSERT(type_ == Type::kArray);
106106
return absl::get<ArrayT>(value_);
107107
}
108108

109109
MapFieldValue FieldValueInternal::map_value() const {
110-
HARD_ASSERT_IOS(type_ == Type::kMap);
110+
SIMPLE_HARD_ASSERT(type_ == Type::kMap);
111111
return absl::get<MapT>(value_);
112112
}
113113

114114
std::vector<FieldValue> FieldValueInternal::array_transform_value() const {
115-
HARD_ASSERT_IOS(type_ == Type::kArrayUnion || type_ == Type::kArrayRemove);
115+
SIMPLE_HARD_ASSERT(type_ == Type::kArrayUnion || type_ == Type::kArrayRemove);
116116
return absl::get<ArrayT>(value_);
117117
}
118118

119119
std::int64_t FieldValueInternal::integer_increment_value() const {
120-
HARD_ASSERT_IOS(type_ == Type::kIncrementInteger);
120+
SIMPLE_HARD_ASSERT(type_ == Type::kIncrementInteger);
121121
return absl::get<model::FieldValue>(value_).integer_value();
122122
}
123123

124124
double FieldValueInternal::double_increment_value() const {
125-
HARD_ASSERT_IOS(type_ == Type::kIncrementDouble);
125+
SIMPLE_HARD_ASSERT(type_ == Type::kIncrementDouble);
126126
return absl::get<model::FieldValue>(value_).double_value();
127127
}
128128

@@ -244,7 +244,7 @@ std::string Describe(Type type) {
244244
// HARD_FAIL("Unexpected type '%s'", type);
245245
auto message = std::string("Unexpected type '") +
246246
std::to_string(static_cast<int>(type)) + "'";
247-
HARD_FAIL_IOS(message.c_str());
247+
SIMPLE_HARD_FAIL(message.c_str());
248248
}
249249
}
250250
}

firestore/src/ios/firestore_ios.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "app/src/include/firebase/future.h"
66
#include "app/src/reference_counted_future_impl.h"
7+
#include "firestore/src/common/hard_assert_common.h"
78
#include "firestore/src/common/macros.h"
89
#include "firestore/src/common/util.h"
910
#include "firestore/src/include/firebase/firestore.h"
@@ -12,7 +13,6 @@
1213
#include "firestore/src/ios/create_firebase_metadata_provider.h"
1314
#include "firestore/src/ios/document_reference_ios.h"
1415
#include "firestore/src/ios/document_snapshot_ios.h"
15-
#include "firestore/src/ios/hard_assert_ios.h"
1616
#include "firestore/src/ios/listener_ios.h"
1717
#include "absl/memory/memory.h"
1818
#include "absl/types/any.h"

0 commit comments

Comments
 (0)