xref: /aosp_15_r20/external/perfetto/src/tools/proto_merger/proto_file.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_TOOLS_PROTO_MERGER_PROTO_FILE_H_
18 #define SRC_TOOLS_PROTO_MERGER_PROTO_FILE_H_
19 
20 #include <string>
21 #include <vector>
22 
23 // We include this intentionally instead of forward declaring to allow
24 // for an easy find/replace transformation when moving to Google3.
25 #include <google/protobuf/descriptor.h>
26 
27 namespace perfetto {
28 namespace proto_merger {
29 
30 // Simplified representation of the components of a .proto file.
31 struct ProtoFile {
32   struct Option {
33     std::string key;
34     std::string value;
35   };
36   struct Member {
37     std::vector<std::string> leading_comments;
38     std::vector<std::string> trailing_comments;
39   };
40   struct Enum : Member {
41     struct Value : Member {
42       std::string name;
43       int number;
44       std::vector<Option> options;
45     };
46     std::string name;
47     std::vector<Value> values;
48 
49     std::vector<Value> deleted_values;
50   };
51   struct Field : Member {
52     bool is_repeated;
53     std::string packageless_type;
54     std::string type;
55     std::string name;
56     int number;
57     std::vector<Option> options;
58   };
59   struct Oneof : Member {
60     std::string name;
61     std::vector<Field> fields;
62 
63     std::vector<Field> deleted_fields;
64   };
65   struct Message : Member {
66     std::string name;
67     std::vector<Enum> enums;
68     std::vector<Message> nested_messages;
69     std::vector<Oneof> oneofs;
70     std::vector<Field> fields;
71 
72     std::vector<Enum> deleted_enums;
73     std::vector<Message> deleted_nested_messages;
74     std::vector<Oneof> deleted_oneofs;
75     std::vector<Field> deleted_fields;
76   };
77 
78   std::string preamble;
79 
80   std::vector<Message> messages;
81   std::vector<Enum> enums;
82 
83   std::vector<Message> deleted_messages;
84   std::vector<Enum> deleted_enums;
85 };
86 
87 // Creates a ProtoFile struct from a libprotobuf-full descriptor clas.
88 ProtoFile ProtoFileFromDescriptor(std::string preamble,
89                                   const google::protobuf::FileDescriptor&);
90 
91 }  // namespace proto_merger
92 }  // namespace perfetto
93 
94 #endif  // SRC_TOOLS_PROTO_MERGER_PROTO_FILE_H_
95