-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomparison.h
178 lines (146 loc) · 4.69 KB
/
comparison.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/descriptor.h>
#include <iostream>
#include <sstream>
#include <memory>
#include <list>
#include <unordered_map>
using std::string;
using std::list;
using std::shared_ptr;
using std::unordered_map;
using google::protobuf::Descriptor;
using google::protobuf::FieldDescriptor;
using google::protobuf::EnumDescriptor;
class ErrorCollector : public google::protobuf::compiler::MultiFileErrorCollector
{
public:
ErrorCollector() {}
void AddError(const std::string & filename, int line, int column, const std::string & message) override
{
using namespace std;
cerr << "Error: " << filename << "@" << line << "," << column << ": " << message << endl;
}
void AddWarning(const std::string & filename, int line, int column, const std::string & message) override
{
using namespace std;
cerr << "Warning: " << filename << "@" << line << "," << column << ": " << message << endl;
}
};
class Source
{
using DiskSourceTree = google::protobuf::compiler::DiskSourceTree;
using Importer = google::protobuf::compiler::Importer;
using DescriptorPool = google::protobuf::DescriptorPool;
using FileDescriptor = google::protobuf::FileDescriptor;
public:
Source() {}
Source(const string & file_path, const string & root_dir)
{
source_tree.MapPath("", root_dir);
ErrorCollector error_collector;
importer = std::make_shared<Importer>(&source_tree, &error_collector);
d_file_descriptor = importer->Import(file_path);
if (!d_file_descriptor)
{
throw std::runtime_error("Failed to load source.");
}
}
const FileDescriptor * file_descriptor() const { return d_file_descriptor; }
const DescriptorPool * pool() const { return importer->pool(); }
private:
DiskSourceTree source_tree;
ErrorCollector error_collector;
shared_ptr<Importer> importer;
const FileDescriptor * d_file_descriptor = nullptr;
};
class Comparison
{
public:
enum ItemType
{
Enum_Value_Name_Changed,
Enum_Value_Id_Changed,
Enum_Value_Added,
Enum_Value_Removed,
Message_Field_Name_Changed,
Message_Field_Id_Changed,
Message_Field_Label_Changed,
Message_Field_Type_Changed,
Message_Field_Default_Value_Changed,
Message_Field_Added,
Message_Field_Removed,
File_Message_Added,
File_Message_Removed,
File_Enum_Added,
File_Enum_Removed,
Name_Missing
};
struct Item
{
Item(ItemType t, const string & a, const string & b): type(t), a(a), b(b) {}
ItemType type;
string a;
string b;
string message() const;
};
enum SectionType
{
Root_Section,
Message_Comparison,
Message_Field_Comparison,
Enum_Comparison,
Enum_Value_Comparison
};
struct Section
{
Section(SectionType t, const string & a, const string & b):
type(t), a(a), b(b) {}
SectionType type;
string a;
string b;
list<string> notes;
list<Section> subsections;
list<Item> items;
Section & add_subsection(SectionType t, const string & a, const string & b)
{
subsections.emplace_back(t, a, b);
return subsections.back();
}
void add_item(ItemType t, const string & a, const string & b)
{
items.emplace_back(t, a, b);
}
bool is_empty() const { return subsections.empty() and items.empty(); }
void trim()
{
auto s = subsections.begin();
while (s != subsections.end())
{
s->trim();
if (s->is_empty())
s = subsections.erase(s);
else
++s;
}
}
string message() const;
void print(int level = 0);
};
struct Options
{
Options() {}
bool binary = false;
};
Comparison(const Options & options = Options{});
void compare(Source & source1, Source & source2);
void compare(Source & source1, const string & name1, Source & source2, const string &name2);
Section * compare(const EnumDescriptor * enum1, const EnumDescriptor * enum2);
Section * compare(const Descriptor * desc1, const Descriptor * desc2);
Section compare(const FieldDescriptor * field1, const FieldDescriptor * field2);
bool compare_default_value(const FieldDescriptor * field1, const FieldDescriptor * field2);
Section root { Root_Section, "", "" };
unordered_map<string, Section*> compared;
private:
Options options;
};