Skip to content

Commit 704ba90

Browse files
benjaminpcopybara-github
authored andcommitted
Fix various unsigned to signed comparison warnings. (#17212)
GCC aggressively emit warnings when comparing unsigned and signed integer types, which causes failures under protobuf's -Werror default. We can fix these often by switching to iterators, but sometimes it's easiest to add a cast or switch a variable type. Closes #17212 COPYBARA_INTEGRATE_REVIEW=#17212 from benjaminp:unsigned-size-comparison-warnings 4b3c9c2 FUTURE_COPYBARA_INTEGRATE_REVIEW=#17212 from benjaminp:unsigned-size-comparison-warnings 4b3c9c2 PiperOrigin-RevId: 680638328
1 parent cd6df36 commit 704ba90

File tree

6 files changed

+52
-52
lines changed

6 files changed

+52
-52
lines changed

src/google/protobuf/compiler/java/full/enum.cc

+16-17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "absl/container/flat_hash_map.h"
1919
#include "absl/strings/str_cat.h"
20+
#include "absl/strings/string_view.h"
2021
#include "google/protobuf/compiler/java/context.h"
2122
#include "google/protobuf/compiler/java/doc_comment.h"
2223
#include "google/protobuf/compiler/java/helpers.h"
@@ -82,22 +83,21 @@ void EnumNonLiteGenerator::Generate(io::Printer* printer) {
8283
}
8384
}
8485

85-
for (int i = 0; i < canonical_values_.size(); i++) {
86+
for (const EnumValueDescriptor* value : canonical_values_) {
8687
absl::flat_hash_map<absl::string_view, std::string> vars;
87-
vars["name"] = canonical_values_[i]->name();
88-
vars["index"] = absl::StrCat(canonical_values_[i]->index());
89-
vars["number"] = absl::StrCat(canonical_values_[i]->number());
90-
WriteEnumValueDocComment(printer, canonical_values_[i],
91-
context_->options());
92-
if (canonical_values_[i]->options().deprecated()) {
88+
vars["name"] = value->name();
89+
vars["index"] = absl::StrCat(value->index());
90+
vars["number"] = absl::StrCat(value->number());
91+
WriteEnumValueDocComment(printer, value, context_->options());
92+
if (value->options().deprecated()) {
9393
printer->Print("@java.lang.Deprecated\n");
9494
}
9595
if (ordinal_is_index) {
9696
printer->Print(vars, "$name$($number$),\n");
9797
} else {
9898
printer->Print(vars, "$name$($index$, $number$),\n");
9999
}
100-
printer->Annotate("name", canonical_values_[i]);
100+
printer->Annotate("name", value);
101101
}
102102

103103
if (!descriptor_->is_closed()) {
@@ -122,15 +122,15 @@ void EnumNonLiteGenerator::Generate(io::Printer* printer) {
122122
printer->Outdent();
123123
printer->Print("}\n");
124124

125-
for (int i = 0; i < aliases_.size(); i++) {
125+
for (const Alias& alias : aliases_) {
126126
absl::flat_hash_map<absl::string_view, std::string> vars;
127127
vars["classname"] = descriptor_->name();
128-
vars["name"] = aliases_[i].value->name();
129-
vars["canonical_name"] = aliases_[i].canonical_value->name();
130-
WriteEnumValueDocComment(printer, aliases_[i].value, context_->options());
128+
vars["name"] = alias.value->name();
129+
vars["canonical_name"] = alias.canonical_value->name();
130+
WriteEnumValueDocComment(printer, alias.value, context_->options());
131131
printer->Print(
132132
vars, "public static final $classname$ $name$ = $canonical_name$;\n");
133-
printer->Annotate("name", aliases_[i].value);
133+
printer->Annotate("name", alias.value);
134134
}
135135

136136
for (int i = 0; i < descriptor_->value_count(); i++) {
@@ -206,10 +206,9 @@ void EnumNonLiteGenerator::Generate(io::Printer* printer) {
206206
printer->Indent();
207207
printer->Indent();
208208

209-
for (int i = 0; i < canonical_values_.size(); i++) {
210-
printer->Print("case $number$: return $name$;\n", "name",
211-
canonical_values_[i]->name(), "number",
212-
absl::StrCat(canonical_values_[i]->number()));
209+
for (const EnumValueDescriptor* value : canonical_values_) {
210+
printer->Print("case $number$: return $name$;\n", "name", value->name(),
211+
"number", absl::StrCat(value->number()));
213212
}
214213

215214
printer->Outdent();

src/google/protobuf/compiler/java/full/message.cc

+9-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <cstdint>
1616
#include <memory>
1717
#include <string>
18+
#include <utility>
1819
#include <vector>
1920

2021
#include "absl/container/flat_hash_map.h"
@@ -27,6 +28,7 @@
2728
#include "google/protobuf/compiler/java/doc_comment.h"
2829
#include "google/protobuf/compiler/java/field_common.h"
2930
#include "google/protobuf/compiler/java/generator_common.h"
31+
#include "google/protobuf/compiler/java/generator_factory.h"
3032
#include "google/protobuf/compiler/java/helpers.h"
3133
#include "google/protobuf/compiler/java/full/enum.h"
3234
#include "google/protobuf/compiler/java/full/extension.h"
@@ -48,9 +50,6 @@ namespace protobuf {
4850
namespace compiler {
4951
namespace java {
5052

51-
using internal::WireFormat;
52-
using internal::WireFormatLite;
53-
5453
namespace {
5554
std::string MapValueImmutableClassdName(const Descriptor* descriptor,
5655
ClassNameResolver* name_resolver) {
@@ -72,7 +71,7 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor)
7271
}
7372
}
7473

75-
MessageGenerator::~MessageGenerator() {}
74+
MessageGenerator::~MessageGenerator() = default;
7675

7776
// ===================================================================
7877
ImmutableMessageGenerator::ImmutableMessageGenerator(
@@ -86,7 +85,7 @@ ImmutableMessageGenerator::ImmutableMessageGenerator(
8685
"generate lite messages.";
8786
}
8887

89-
ImmutableMessageGenerator::~ImmutableMessageGenerator() {}
88+
ImmutableMessageGenerator::~ImmutableMessageGenerator() = default;
9089

9190
void ImmutableMessageGenerator::GenerateStaticVariables(
9291
io::Printer* printer, int* bytecode_estimate) {
@@ -273,7 +272,7 @@ void ImmutableMessageGenerator::GenerateInterface(io::Printer* printer) {
273272
field_generators_.get(descriptor_->field(i))
274273
.GenerateInterfaceMembers(printer);
275274
}
276-
for (auto& kv : oneofs_) {
275+
for (const std::pair<const int, const OneofDescriptor*> kv : oneofs_) {
277276
printer->Print(
278277
"\n"
279278
"$classname$.$oneof_capitalized_name$Case "
@@ -394,7 +393,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
394393

395394
// oneof
396395
absl::flat_hash_map<absl::string_view, std::string> vars;
397-
for (auto& kv : oneofs_) {
396+
for (const std::pair<const int, const OneofDescriptor*> kv : oneofs_) {
398397
const OneofDescriptor* oneof = kv.second;
399398
vars["oneof_name"] = context_->GetOneofGeneratorInfo(oneof)->name;
400399
vars["oneof_capitalized_name"] =
@@ -804,8 +803,7 @@ void ImmutableMessageGenerator::GenerateDescriptorMethods(
804803
" switch (number) {\n");
805804
printer->Indent();
806805
printer->Indent();
807-
for (int i = 0; i < map_fields.size(); ++i) {
808-
const FieldDescriptor* field = map_fields[i];
806+
for (const FieldDescriptor* field : map_fields) {
809807
const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
810808
printer->Print(
811809
"case $number$:\n"
@@ -994,7 +992,7 @@ void ImmutableMessageGenerator::GenerateEqualsAndHashCode(
994992
}
995993

996994
// Compare oneofs.
997-
for (auto& kv : oneofs_) {
995+
for (const std::pair<const int, const OneofDescriptor*> kv : oneofs_) {
998996
const OneofDescriptor* oneof = kv.second;
999997
printer->Print(
1000998
"if (!get$oneof_capitalized_name$Case().equals("
@@ -1074,7 +1072,7 @@ void ImmutableMessageGenerator::GenerateEqualsAndHashCode(
10741072
}
10751073

10761074
// hashCode oneofs.
1077-
for (auto& kv : oneofs_) {
1075+
for (const std::pair<const int, const OneofDescriptor*> kv : oneofs_) {
10781076
const OneofDescriptor* oneof = kv.second;
10791077
printer->Print("switch ($oneof_name$Case_) {\n", "oneof_name",
10801078
context_->GetOneofGeneratorInfo(oneof)->name);

src/google/protobuf/compiler/java/full/message_builder.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "google/protobuf/descriptor.pb.h"
3636
#include "google/protobuf/io/printer.h"
3737
#include "google/protobuf/wire_format.h"
38+
#include "google/protobuf/wire_format_lite.h"
3839

3940
// Must be last.
4041
#include "google/protobuf/port_def.inc"
@@ -92,7 +93,7 @@ MessageBuilderGenerator::MessageBuilderGenerator(const Descriptor* descriptor,
9293
}
9394
}
9495

95-
MessageBuilderGenerator::~MessageBuilderGenerator() {}
96+
MessageBuilderGenerator::~MessageBuilderGenerator() = default;
9697

9798
void MessageBuilderGenerator::Generate(io::Printer* printer) {
9899
WriteMessageDocComment(printer, descriptor_, context_->options());
@@ -211,8 +212,7 @@ void MessageBuilderGenerator::GenerateDescriptorMethods(io::Printer* printer) {
211212
" switch (number) {\n");
212213
printer->Indent();
213214
printer->Indent();
214-
for (int i = 0; i < map_fields.size(); ++i) {
215-
const FieldDescriptor* field = map_fields[i];
215+
for (const FieldDescriptor* field : map_fields) {
216216
const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
217217
printer->Print(
218218
"case $number$:\n"
@@ -237,8 +237,7 @@ void MessageBuilderGenerator::GenerateDescriptorMethods(io::Printer* printer) {
237237
" switch (number) {\n");
238238
printer->Indent();
239239
printer->Indent();
240-
for (int i = 0; i < map_fields.size(); ++i) {
241-
const FieldDescriptor* field = map_fields[i];
240+
for (const FieldDescriptor* field : map_fields) {
242241
const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
243242
printer->Print(
244243
"case $number$:\n"

src/google/protobuf/compiler/java/lite/enum.cc

+16-16
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
#include "absl/container/flat_hash_map.h"
1919
#include "absl/strings/str_cat.h"
20+
#include "absl/strings/string_view.h"
2021
#include "google/protobuf/compiler/java/context.h"
2122
#include "google/protobuf/compiler/java/doc_comment.h"
2223
#include "google/protobuf/compiler/java/helpers.h"
2324
#include "google/protobuf/compiler/java/internal_helpers.h"
2425
#include "google/protobuf/compiler/java/name_resolver.h"
26+
#include "google/protobuf/descriptor.h"
2527
#include "google/protobuf/descriptor.pb.h"
2628
#include "google/protobuf/io/printer.h"
2729

@@ -67,17 +69,16 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
6769
printer->Annotate("classname", descriptor_);
6870
printer->Indent();
6971

70-
for (int i = 0; i < canonical_values_.size(); i++) {
72+
for (const EnumValueDescriptor* value : canonical_values_) {
7173
absl::flat_hash_map<absl::string_view, std::string> vars;
72-
vars["name"] = canonical_values_[i]->name();
73-
vars["number"] = absl::StrCat(canonical_values_[i]->number());
74-
WriteEnumValueDocComment(printer, canonical_values_[i],
75-
context_->options());
76-
if (canonical_values_[i]->options().deprecated()) {
74+
vars["name"] = value->name();
75+
vars["number"] = absl::StrCat(value->number());
76+
WriteEnumValueDocComment(printer, value, context_->options());
77+
if (value->options().deprecated()) {
7778
printer->Print("@java.lang.Deprecated\n");
7879
}
7980
printer->Print(vars, "$name$($number$),\n");
80-
printer->Annotate("name", canonical_values_[i]);
81+
printer->Annotate("name", value);
8182
}
8283

8384
if (!descriptor_->is_closed()) {
@@ -91,15 +92,15 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
9192

9293
// -----------------------------------------------------------------
9394

94-
for (int i = 0; i < aliases_.size(); i++) {
95+
for (const Alias& alias : aliases_) {
9596
absl::flat_hash_map<absl::string_view, std::string> vars;
9697
vars["classname"] = descriptor_->name();
97-
vars["name"] = aliases_[i].value->name();
98-
vars["canonical_name"] = aliases_[i].canonical_value->name();
99-
WriteEnumValueDocComment(printer, aliases_[i].value, context_->options());
98+
vars["name"] = alias.value->name();
99+
vars["canonical_name"] = alias.canonical_value->name();
100+
WriteEnumValueDocComment(printer, alias.value, context_->options());
100101
printer->Print(
101102
vars, "public static final $classname$ $name$ = $canonical_name$;\n");
102-
printer->Annotate("name", aliases_[i].value);
103+
printer->Annotate("name", alias.value);
103104
}
104105

105106
for (int i = 0; i < descriptor_->value_count(); i++) {
@@ -162,10 +163,9 @@ void EnumLiteGenerator::Generate(io::Printer* printer) {
162163
printer->Indent();
163164
printer->Indent();
164165

165-
for (int i = 0; i < canonical_values_.size(); i++) {
166-
printer->Print("case $number$: return $name$;\n", "name",
167-
canonical_values_[i]->name(), "number",
168-
absl::StrCat(canonical_values_[i]->number()));
166+
for (const EnumValueDescriptor* value : canonical_values_) {
167+
printer->Print("case $number$: return $name$;\n", "name", value->name(),
168+
"number", absl::StrCat(value->number()));
169169
}
170170

171171
printer->Outdent();

src/google/protobuf/compiler/rust/relative_path.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
#include "google/protobuf/compiler/rust/relative_path.h"
99

10+
#include <cstddef>
1011
#include <string>
1112
#include <vector>
1213

1314
#include "absl/algorithm/container.h"
15+
#include "absl/log/absl_check.h"
1416
#include "absl/strings/match.h"
1517
#include "absl/strings/str_join.h"
1618
#include "absl/strings/str_split.h"
@@ -62,7 +64,7 @@ std::string RelativePath::Relative(const RelativePath& dest) const {
6264
result.push_back(segment);
6365
}
6466
// Push `..` from the common ancestor to the current path.
65-
for (int i = 0; i < current_segments.size(); ++i) {
67+
for (size_t i = 0; i < current_segments.size(); ++i) {
6668
result.push_back("..");
6769
}
6870
absl::c_reverse(result);

src/google/protobuf/io/printer.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
#include "absl/strings/str_format.h"
3333
#include "absl/strings/string_view.h"
3434
#include "absl/types/optional.h"
35+
#include "absl/types/span.h"
3536
#include "absl/types/variant.h"
3637
#include "google/protobuf/io/zero_copy_sink.h"
38+
#include "google/protobuf/io/zero_copy_stream.h"
3739

3840

3941
// Must be included last.
@@ -124,8 +126,8 @@ class AnnotationProtoCollector : public AnnotationCollector {
124126
const std::string& file_path, const std::vector<int>& path,
125127
absl::optional<Semantic> semantic) override {
126128
auto* annotation = annotation_proto_->add_annotation();
127-
for (int i = 0; i < path.size(); ++i) {
128-
annotation->add_path(path[i]);
129+
for (const int segment : path) {
130+
annotation->add_path(segment);
129131
}
130132
annotation->set_source_file(file_path);
131133
annotation->set_begin(begin_offset);

0 commit comments

Comments
 (0)