Skip to content

Commit db1bc95

Browse files
wilhuffCorrob
authored andcommitted
Clean up warnings in the Firestore project (#2622)
* Add nanopb::CheckedSize Fix implicit narrowing warnings and reduce pb_size_t copypasta. * Rename local::DocumentReference to DocumentKeyReference This cleans up a build-time warning about a duplicate file in the Copy Headers phase and cleans up a massive duplicate id warning during pod install.
1 parent 0e0edaf commit db1bc95

12 files changed

+122
-77
lines changed

Firestore/core/src/firebase/firestore/local/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ endif()
5454
cc_library(
5555
firebase_firestore_local
5656
SOURCES
57-
document_reference.h
58-
document_reference.cc
57+
document_key_reference.h
58+
document_key_reference.cc
5959
index_manager.h
6060
listen_sequence.h
6161
local_documents_view.h

Firestore/core/src/firebase/firestore/local/document_reference.cc renamed to Firestore/core/src/firebase/firestore/local/document_key_reference.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include "Firestore/core/src/firebase/firestore/local/document_key_reference.h"
18+
1719
#include <string>
1820
#include <utility>
1921

20-
#include "Firestore/core/src/firebase/firestore/local/document_reference.h"
2122
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2223
#include "Firestore/core/src/firebase/firestore/util/comparison.h"
2324
#include "Firestore/core/src/firebase/firestore/util/hashing.h"
@@ -29,22 +30,23 @@ namespace local {
2930
using model::DocumentKey;
3031
using util::ComparisonResult;
3132

32-
bool operator==(const DocumentReference& lhs, const DocumentReference& rhs) {
33+
bool operator==(const DocumentKeyReference& lhs,
34+
const DocumentKeyReference& rhs) {
3335
return lhs.key_ == rhs.key_ && lhs.ref_id_ == rhs.ref_id_;
3436
}
3537

36-
size_t DocumentReference::Hash() const {
38+
size_t DocumentKeyReference::Hash() const {
3739
return util::Hash(key_.ToString(), ref_id_);
3840
}
3941

40-
std::string DocumentReference::ToString() const {
41-
return util::StringFormat("<DocumentReference: key=%s, id=%s>",
42+
std::string DocumentKeyReference::ToString() const {
43+
return util::StringFormat("<DocumentKeyReference: key=%s, id=%s>",
4244
key_.ToString(), ref_id_);
4345
}
4446

4547
/** Sorts document references by key then ID. */
46-
bool DocumentReference::ByKey::operator()(const DocumentReference& lhs,
47-
const DocumentReference& rhs) const {
48+
bool DocumentKeyReference::ByKey::operator()(
49+
const DocumentKeyReference& lhs, const DocumentKeyReference& rhs) const {
4850
util::Comparator<model::DocumentKey> key_less;
4951
if (key_less(lhs.key_, rhs.key_)) return true;
5052
if (key_less(rhs.key_, lhs.key_)) return false;
@@ -54,8 +56,8 @@ bool DocumentReference::ByKey::operator()(const DocumentReference& lhs,
5456
}
5557

5658
/** Sorts document references by ID then key. */
57-
bool DocumentReference::ById::operator()(const DocumentReference& lhs,
58-
const DocumentReference& rhs) const {
59+
bool DocumentKeyReference::ById::operator()(
60+
const DocumentKeyReference& lhs, const DocumentKeyReference& rhs) const {
5961
util::Comparator<int32_t> id_less;
6062
if (id_less(lhs.ref_id_, rhs.ref_id_)) return true;
6163
if (id_less(rhs.ref_id_, lhs.ref_id_)) return false;

Firestore/core/src/firebase/firestore/local/document_reference.h renamed to Firestore/core/src/firebase/firestore/local/document_key_reference.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_REFERENCE_H_
18-
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_REFERENCE_H_
17+
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_KEY_REFERENCE_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_KEY_REFERENCE_H_
1919

2020
#include <string>
2121
#include <utility>
@@ -39,13 +39,13 @@ namespace local {
3939
*
4040
* Not to be confused with FIRDocumentReference.
4141
*/
42-
class DocumentReference {
42+
class DocumentKeyReference {
4343
public:
44-
DocumentReference() {
44+
DocumentKeyReference() {
4545
}
4646

4747
/** Initializes the document reference with the given key and Id. */
48-
DocumentReference(model::DocumentKey key, int32_t ref_id)
48+
DocumentKeyReference(model::DocumentKey key, int32_t ref_id)
4949
: key_{std::move(key)}, ref_id_{ref_id} {
5050
}
5151

@@ -63,23 +63,23 @@ class DocumentReference {
6363
return ref_id_;
6464
}
6565

66-
friend bool operator==(const DocumentReference& lhs,
67-
const DocumentReference& rhs);
66+
friend bool operator==(const DocumentKeyReference& lhs,
67+
const DocumentKeyReference& rhs);
6868

6969
size_t Hash() const;
7070

7171
std::string ToString() const;
7272

7373
/** Sorts document references by key then Id. */
74-
struct ByKey : public util::Comparator<DocumentReference> {
75-
bool operator()(const DocumentReference& lhs,
76-
const DocumentReference& rhs) const;
74+
struct ByKey : public util::Comparator<DocumentKeyReference> {
75+
bool operator()(const DocumentKeyReference& lhs,
76+
const DocumentKeyReference& rhs) const;
7777
};
7878

7979
/** Sorts document references by Id then key. */
80-
struct ById : public util::Comparator<DocumentReference> {
81-
bool operator()(const DocumentReference& lhs,
82-
const DocumentReference& rhs) const;
80+
struct ById : public util::Comparator<DocumentKeyReference> {
81+
bool operator()(const DocumentKeyReference& lhs,
82+
const DocumentKeyReference& rhs) const;
8383
};
8484

8585
private:
@@ -94,4 +94,4 @@ class DocumentReference {
9494
} // namespace firestore
9595
} // namespace firebase
9696

97-
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_REFERENCE_H_
97+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_DOCUMENT_KEY_REFERENCE_H_

Firestore/core/src/firebase/firestore/local/local_serializer.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "Firestore/core/src/firebase/firestore/model/no_document.h"
3030
#include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
3131
#include "Firestore/core/src/firebase/firestore/model/unknown_document.h"
32+
#include "Firestore/core/src/firebase/firestore/nanopb/nanopb_util.h"
3233
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
3334
#include "Firestore/core/src/firebase/firestore/util/string_format.h"
3435

@@ -44,6 +45,7 @@ using model::MutationBatch;
4445
using model::NoDocument;
4546
using model::SnapshotVersion;
4647
using model::UnknownDocument;
48+
using nanopb::CheckedSize;
4749
using nanopb::Reader;
4850
using nanopb::Writer;
4951
using remote::MakeArray;
@@ -123,10 +125,8 @@ google_firestore_v1_Document LocalSerializer::EncodeDocument(
123125
rpc_serializer_.EncodeString(rpc_serializer_.EncodeKey(doc.key()));
124126

125127
// Encode Document.fields (unless it's empty)
126-
size_t count = doc.data().GetInternalValue().size();
127-
HARD_ASSERT(count <= std::numeric_limits<pb_size_t>::max(),
128-
"Unable to encode specified document. Too many fields.");
129-
result.fields_count = static_cast<pb_size_t>(count);
128+
pb_size_t count = CheckedSize(doc.data().GetInternalValue().size());
129+
result.fields_count = count;
130130
result.fields = MakeArray<google_firestore_v1_Document_FieldsEntry>(count);
131131
int i = 0;
132132
for (const auto& kv : doc.data().GetInternalValue()) {
@@ -257,10 +257,8 @@ firestore_client_WriteBatch LocalSerializer::EncodeMutationBatch(
257257
firestore_client_WriteBatch result{};
258258

259259
result.batch_id = mutation_batch.batch_id();
260-
size_t count = mutation_batch.mutations().size();
261-
HARD_ASSERT(count <= std::numeric_limits<pb_size_t>::max(),
262-
"Unable to encode specified mutation batch. Too many mutations.");
263-
result.writes_count = static_cast<pb_size_t>(count);
260+
pb_size_t count = CheckedSize(mutation_batch.mutations().size());
261+
result.writes_count = count;
264262
result.writes = MakeArray<google_firestore_v1_Write>(count);
265263
int i = 0;
266264
for (const std::unique_ptr<Mutation>& mutation : mutation_batch.mutations()) {

Firestore/core/src/firebase/firestore/local/memory_mutation_queue.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#import "Firestore/Source/Public/FIRTimestamp.h"
3030

3131
#include "Firestore/core/src/firebase/firestore/immutable/sorted_set.h"
32-
#include "Firestore/core/src/firebase/firestore/local/document_reference.h"
32+
#include "Firestore/core/src/firebase/firestore/local/document_key_reference.h"
3333
#include "Firestore/core/src/firebase/firestore/local/mutation_queue.h"
3434
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
3535
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
@@ -94,8 +94,8 @@ class MemoryMutationQueue : public MutationQueue {
9494
void SetLastStreamToken(NSData* _Nullable token) override;
9595

9696
private:
97-
using DocumentReferenceSet =
98-
immutable::SortedSet<DocumentReference, DocumentReference::ByKey>;
97+
using DocumentKeyReferenceSet =
98+
immutable::SortedSet<DocumentKeyReference, DocumentKeyReference::ByKey>;
9999

100100
std::vector<FSTMutationBatch*> AllMutationBatchesWithIds(
101101
const std::set<model::BatchId>& batch_ids);
@@ -147,7 +147,7 @@ class MemoryMutationQueue : public MutationQueue {
147147
NSData* _Nullable last_stream_token_;
148148

149149
/** An ordered mapping between documents and the mutation batch IDs. */
150-
DocumentReferenceSet batches_by_document_key_;
150+
DocumentKeyReferenceSet batches_by_document_key_;
151151
};
152152

153153
} // namespace local

Firestore/core/src/firebase/firestore/local/memory_mutation_queue.mm

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#import "Firestore/Source/Model/FSTMutation.h"
2626
#import "Firestore/Source/Model/FSTMutationBatch.h"
2727

28-
#include "Firestore/core/src/firebase/firestore/local/document_reference.h"
28+
#include "Firestore/core/src/firebase/firestore/local/document_key_reference.h"
2929
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
3030
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
3131

@@ -98,7 +98,7 @@
9898
// Track references by document key and index collection parents.
9999
for (FSTMutation* mutation : [batch mutations]) {
100100
batches_by_document_key_ = batches_by_document_key_.insert(
101-
DocumentReference{mutation.key, batch_id});
101+
DocumentKeyReference{mutation.key, batch_id});
102102

103103
persistence_.indexManager->AddToCollectionParentIndex(
104104
mutation.key.path().PopLast());
@@ -121,7 +121,7 @@
121121
const DocumentKey& key = mutation.key;
122122
[persistence_.referenceDelegate removeMutationReference:key];
123123

124-
DocumentReference reference{key, batch.batchID};
124+
DocumentKeyReference reference{key, batch.batchID};
125125
batches_by_document_key_ = batches_by_document_key_.erase(reference);
126126
}
127127
}
@@ -132,7 +132,7 @@
132132
// First find the set of affected batch IDs.
133133
std::set<BatchId> batch_ids;
134134
for (const DocumentKey& key : document_keys) {
135-
DocumentReference start{key, 0};
135+
DocumentKeyReference start{key, 0};
136136

137137
for (const auto& reference : batches_by_document_key_.values_from(start)) {
138138
if (key != reference.key()) break;
@@ -149,7 +149,7 @@
149149
const DocumentKey& key) {
150150
std::vector<FSTMutationBatch*> result;
151151

152-
DocumentReference start{key, 0};
152+
DocumentKeyReference start{key, 0};
153153
for (const auto& reference : batches_by_document_key_.values_from(start)) {
154154
if (key != reference.key()) break;
155155

@@ -178,7 +178,7 @@
178178
if (!DocumentKey::IsDocumentKey(start_path)) {
179179
start_path = start_path.Append("");
180180
}
181-
DocumentReference start{DocumentKey{start_path}, 0};
181+
DocumentKeyReference start{DocumentKey{start_path}, 0};
182182

183183
// Find unique batchIDs referenced by all documents potentially matching the
184184
// query.
@@ -242,7 +242,7 @@
242242
bool MemoryMutationQueue::ContainsKey(const model::DocumentKey& key) {
243243
// Create a reference with a zero ID as the start position to find any
244244
// document reference with this key.
245-
DocumentReference reference{key, 0};
245+
DocumentKeyReference reference{key, 0};
246246
auto range = batches_by_document_key_.values_from(reference);
247247
auto begin = range.begin();
248248
return begin != range.end() && begin->key() == key;

Firestore/core/src/firebase/firestore/local/reference_set.cc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
*/
1616

1717
#include "Firestore/core/src/firebase/firestore/local/reference_set.h"
18+
1819
#include "Firestore/core/src/firebase/firestore/immutable/sorted_set.h"
19-
#include "Firestore/core/src/firebase/firestore/local/document_reference.h"
20+
#include "Firestore/core/src/firebase/firestore/local/document_key_reference.h"
2021
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2122

2223
namespace firebase {
@@ -27,7 +28,7 @@ using model::DocumentKey;
2728
using model::DocumentKeySet;
2829

2930
void ReferenceSet::AddReference(const DocumentKey& key, int id) {
30-
DocumentReference reference{key, id};
31+
DocumentKeyReference reference{key, id};
3132
by_key_ = by_key_.insert(reference);
3233
by_id_ = by_id_.insert(reference);
3334
}
@@ -39,7 +40,7 @@ void ReferenceSet::AddReferences(const DocumentKeySet& keys, int id) {
3940
}
4041

4142
void ReferenceSet::RemoveReference(const DocumentKey& key, int id) {
42-
RemoveReference(DocumentReference{key, id});
43+
RemoveReference(DocumentKeyReference{key, id});
4344
}
4445

4546
void ReferenceSet::RemoveReferences(
@@ -50,8 +51,8 @@ void ReferenceSet::RemoveReferences(
5051
}
5152

5253
DocumentKeySet ReferenceSet::RemoveReferences(int id) {
53-
DocumentReference start{DocumentKey::Empty(), id};
54-
DocumentReference end{DocumentKey::Empty(), id + 1};
54+
DocumentKeyReference start{DocumentKey::Empty(), id};
55+
DocumentKeyReference end{DocumentKey::Empty(), id + 1};
5556

5657
DocumentKeySet removed{};
5758

@@ -65,19 +66,19 @@ DocumentKeySet ReferenceSet::RemoveReferences(int id) {
6566

6667
void ReferenceSet::RemoveAllReferences() {
6768
auto initial = by_key_;
68-
for (const DocumentReference& reference : initial) {
69+
for (const DocumentKeyReference& reference : initial) {
6970
RemoveReference(reference);
7071
}
7172
}
7273

73-
void ReferenceSet::RemoveReference(const DocumentReference& reference) {
74+
void ReferenceSet::RemoveReference(const DocumentKeyReference& reference) {
7475
by_key_ = by_key_.erase(reference);
7576
by_id_ = by_id_.erase(reference);
7677
}
7778

7879
DocumentKeySet ReferenceSet::ReferencedKeys(int id) {
79-
DocumentReference start{DocumentKey::Empty(), id};
80-
DocumentReference end{DocumentKey::Empty(), id + 1};
80+
DocumentKeyReference start{DocumentKey::Empty(), id};
81+
DocumentKeyReference end{DocumentKey::Empty(), id + 1};
8182

8283
DocumentKeySet keys;
8384
for (const auto& reference : by_id_.values_in(start, end)) {
@@ -89,7 +90,7 @@ DocumentKeySet ReferenceSet::ReferencedKeys(int id) {
8990
bool ReferenceSet::ContainsKey(const DocumentKey& key) {
9091
// Create a reference with a zero ID as the start position to find any
9192
// document reference with this key.
92-
DocumentReference start{key, 0};
93+
DocumentKeyReference start{key, 0};
9394

9495
auto range = by_key_.values_from(start);
9596
auto begin = range.begin();

Firestore/core/src/firebase/firestore/local/reference_set.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_REFERENCE_SET_H_
1919

2020
#include "Firestore/core/src/firebase/firestore/immutable/sorted_set.h"
21-
#include "Firestore/core/src/firebase/firestore/local/document_reference.h"
21+
#include "Firestore/core/src/firebase/firestore/local/document_key_reference.h"
2222
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2323
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
2424

@@ -31,7 +31,7 @@ namespace local {
3131
* (either a TargetId or BatchId). As references are added to or removed from
3232
* the set corresponding events are emitted to a registered garbage collector.
3333
*
34-
* Each reference is represented by a DocumentReference object. Each of them
34+
* Each reference is represented by a DocumentKeyReference object. Each of them
3535
* contains enough information to uniquely identify the reference. They are all
3636
* stored primarily in a set sorted by key. A document is considered garbage if
3737
* there's no references in that set (this can be efficiently checked thanks to
@@ -81,10 +81,11 @@ class ReferenceSet {
8181
bool ContainsKey(const model::DocumentKey& key);
8282

8383
private:
84-
void RemoveReference(const DocumentReference& reference);
84+
void RemoveReference(const DocumentKeyReference& reference);
8585

86-
immutable::SortedSet<DocumentReference, DocumentReference::ByKey> by_key_;
87-
immutable::SortedSet<DocumentReference, DocumentReference::ById> by_id_;
86+
immutable::SortedSet<DocumentKeyReference, DocumentKeyReference::ByKey>
87+
by_key_;
88+
immutable::SortedSet<DocumentKeyReference, DocumentKeyReference::ById> by_id_;
8889
};
8990

9091
} // namespace local

Firestore/core/src/firebase/firestore/nanopb/nanopb_string.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <cstdlib>
2020
#include <utility>
2121

22+
#include "Firestore/core/src/firebase/firestore/nanopb/nanopb_util.h"
23+
2224
namespace firebase {
2325
namespace firestore {
2426
namespace nanopb {
@@ -28,7 +30,7 @@ String::~String() {
2830
}
2931

3032
/* static */ pb_bytes_array_t* String::MakeBytesArray(absl::string_view value) {
31-
auto size = static_cast<pb_size_t>(value.size());
33+
pb_size_t size = CheckedSize(value.size());
3234

3335
// Allocate one extra byte for the null terminator that's not necessarily
3436
// there in a string_view. As long as we're making a copy, might as well

0 commit comments

Comments
 (0)