xref: /aosp_15_r20/external/pigweed/pw_protobuf/size_report/decoder_incremental.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include <cstring>
16 
17 #include "pw_bloat/bloat_this_binary.h"
18 #include "pw_protobuf/decoder.h"
19 
20 namespace {
21 // clang-format off
22 constexpr uint8_t encoded_proto[] = {
23   // type=int32, k=1, v=42
24   0x08, 0x2a,
25   // type=sint32, k=2, v=-13
26   0x10, 0x19,
27 };
28 // clang-format on
29 }  // namespace
30 
31 int* volatile non_optimizable_pointer;
32 
main()33 int main() {
34   pw::bloat::BloatThisBinary();
35 
36   int32_t test_int32, test_sint32;
37   std::string_view str;
38   float f;
39   double d;
40   uint32_t uint;
41 
42   pw::protobuf::Decoder decoder(pw::as_bytes(pw::span(encoded_proto)));
43   while (decoder.Next().ok()) {
44     switch (decoder.FieldNumber()) {
45       case 1:
46         decoder.ReadInt32(&test_int32).IgnoreError();
47         break;
48       case 2:
49         decoder.ReadSint32(&test_sint32).IgnoreError();
50         break;
51       case 3:
52         decoder.ReadString(&str).IgnoreError();
53         break;
54       case 4:
55         decoder.ReadFloat(&f).IgnoreError();
56         break;
57       case 5:
58         decoder.ReadDouble(&d).IgnoreError();
59         break;
60 
61       // Extra fields over decoder_full.
62       case 21:
63         decoder.ReadInt32(&test_int32).IgnoreError();
64         break;
65       case 22:
66         decoder.ReadUint32(&uint).IgnoreError();
67         break;
68       case 23:
69         decoder.ReadSint32(&test_sint32).IgnoreError();
70         break;
71     }
72   }
73 
74   *non_optimizable_pointer = test_int32 + test_sint32;
75 
76   return 0;
77 }
78