blob: b4825b60cf001c37480af3e914c177b60f671d87 [file] [log] [blame]
Jiyong Park3656c3c2018-08-01 20:02:01 +09001/*
2 * Copyright (C) 2018, The Android Open Source Project *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://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,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "aidl.h"
17#include "aidl_language.h"
18#include "import_resolver.h"
Jeongik Cha047c5ee2019-08-07 23:16:49 +090019#include "logging.h"
Jiyong Park3656c3c2018-08-01 20:02:01 +090020#include "options.h"
Jiyong Park3656c3c2018-08-01 20:02:01 +090021
22#include <map>
23#include <string>
24#include <vector>
25
Jiyong Parkf7534672019-08-12 22:06:22 +090026#include <android-base/strings.h>
27
Jiyong Park3656c3c2018-08-01 20:02:01 +090028namespace android {
29namespace aidl {
30
31using std::map;
32using std::set;
33using std::string;
34using std::vector;
35
36static bool have_compatible_annotations(const AidlAnnotatable& older,
37 const AidlAnnotatable& newer) {
Jeongik Cha3271ffa2018-12-04 15:19:20 +090038 set<AidlAnnotation> olderAnnotations(older.GetAnnotations().begin(),
39 older.GetAnnotations().end());
40 set<AidlAnnotation> newerAnnotations(newer.GetAnnotations().begin(),
41 newer.GetAnnotations().end());
42 if (olderAnnotations != newerAnnotations) {
Jiyong Park3656c3c2018-08-01 20:02:01 +090043 const string from = older.ToString().empty() ? "(empty)" : older.ToString();
44 const string to = newer.ToString().empty() ? "(empty)" : newer.ToString();
45 AIDL_ERROR(newer) << "Changed annotations: " << from << " to " << to;
46 return false;
47 }
48 return true;
49}
50
51static bool are_compatible_types(const AidlTypeSpecifier& older, const AidlTypeSpecifier& newer) {
52 bool compatible = true;
53 if (older.ToString() != newer.ToString()) {
54 AIDL_ERROR(newer) << "Type changed: " << older.ToString() << " to " << newer.ToString() << ".";
55 compatible = false;
56 }
57 compatible &= have_compatible_annotations(older, newer);
58 return compatible;
59}
60
61static bool are_compatible_interfaces(const AidlInterface& older, const AidlInterface& newer) {
62 bool compatible = true;
63 compatible &= have_compatible_annotations(older, newer);
64
65 map<string, AidlMethod*> new_methods;
66 for (const auto& m : newer.AsInterface()->GetMethods()) {
67 new_methods.emplace(m->Signature(), m.get());
68 }
69
70 for (const auto& old_m : older.AsInterface()->GetMethods()) {
71 const auto found = new_methods.find(old_m->Signature());
72 if (found == new_methods.end()) {
Steven Moreland4ee68632018-12-14 15:52:46 -080073 AIDL_ERROR(old_m) << "Removed or changed method: " << older.GetCanonicalName() << "."
Jiyong Park3656c3c2018-08-01 20:02:01 +090074 << old_m->Signature();
75 compatible = false;
76 continue;
77 }
78
79 // Compare IDs to detect method reordering. IDs are assigned by their
80 // textual order, so if there is an ID mismatch, that means reordering
81 // has happened.
82 const auto new_m = found->second;
Steven Moreland4ee68632018-12-14 15:52:46 -080083
84 if (old_m->IsOneway() != new_m->IsOneway()) {
85 AIDL_ERROR(new_m) << "Oneway attribute " << (old_m->IsOneway() ? "removed" : "added") << ": "
86 << older.GetCanonicalName() << "." << old_m->Signature();
87 compatible = false;
88 }
89
Jiyong Park3656c3c2018-08-01 20:02:01 +090090 if (old_m->GetId() != new_m->GetId()) {
91 AIDL_ERROR(new_m) << "Transaction ID changed: " << older.GetCanonicalName() << "."
92 << old_m->Signature() << " is changed from " << old_m->GetId() << " to "
93 << new_m->GetId() << ".";
94 compatible = false;
95 }
96
97 compatible &= are_compatible_types(old_m->GetType(), new_m->GetType());
98
99 const auto& old_args = old_m->GetArguments();
100 const auto& new_args = new_m->GetArguments();
101 // this is guaranteed because arguments are part of AidlMethod::Signature()
102 CHECK(old_args.size() == new_args.size());
103 for (size_t i = 0; i < old_args.size(); i++) {
104 const AidlArgument& old_a = *(old_args.at(i));
105 const AidlArgument& new_a = *(new_args.at(i));
106 compatible &= are_compatible_types(old_a.GetType(), new_a.GetType());
107
108 if (old_a.GetDirection() != new_a.GetDirection()) {
109 AIDL_ERROR(new_m) << "Direction changed: " << old_a.GetDirectionSpecifier() << " to "
110 << new_a.GetDirectionSpecifier() << ".";
111 compatible = false;
112 }
113 }
114 }
Jiyong Parka428d212018-08-29 22:26:30 +0900115
116 map<string, AidlConstantDeclaration*> new_constdecls;
117 for (const auto& c : newer.AsInterface()->GetConstantDeclarations()) {
118 new_constdecls.emplace(c->GetName(), c.get());
119 }
120
121 for (const auto& old_c : older.AsInterface()->GetConstantDeclarations()) {
122 const auto found = new_constdecls.find(old_c->GetName());
123 if (found == new_constdecls.end()) {
124 AIDL_ERROR(old_c) << "Removed constant declaration: " << older.GetCanonicalName() << "."
125 << old_c->GetName();
126 compatible = false;
127 continue;
128 }
129
130 const auto new_c = found->second;
131 compatible &= are_compatible_types(old_c->GetType(), new_c->GetType());
132
133 const string old_value = old_c->ValueString(AidlConstantValueDecorator);
134 const string new_value = new_c->ValueString(AidlConstantValueDecorator);
135 if (old_value != new_value) {
136 AIDL_ERROR(newer) << "Changed constant value: " << older.GetCanonicalName() << "."
137 << old_c->GetName() << " from " << old_value << " to " << new_value << ".";
138 compatible = false;
139 }
140 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900141 return compatible;
142}
143
144static bool are_compatible_parcelables(const AidlStructuredParcelable& older,
145 const AidlStructuredParcelable& newer) {
146 const auto& old_fields = older.GetFields();
147 const auto& new_fields = newer.GetFields();
148 if (old_fields.size() > new_fields.size()) {
149 // you can add new fields only at the end
150 AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from "
151 << old_fields.size() << " to " << new_fields.size() << ".";
152 return false;
153 }
154
155 bool compatible = true;
156 for (size_t i = 0; i < old_fields.size(); i++) {
Jiyong Parka468e2a2018-08-29 21:25:18 +0900157 const auto& old_field = old_fields.at(i);
158 const auto& new_field = new_fields.at(i);
159 compatible &= are_compatible_types(old_field->GetType(), new_field->GetType());
Jiyong Park3656c3c2018-08-01 20:02:01 +0900160
161 // Note: unlike method argument names, field name change is an incompatible
162 // change, otherwise, we can't detect
163 // parcelable Point {int x; int y;} -> parcelable Point {int y; int x;}
Jiyong Parka468e2a2018-08-29 21:25:18 +0900164 if (old_field->GetName() != new_field->GetName()) {
165 AIDL_ERROR(newer) << "Renamed field: " << old_field->GetName() << " to "
166 << new_field->GetName() << ".";
167 compatible = false;
168 }
169
170 const string old_value = old_field->ValueString(AidlConstantValueDecorator);
171 const string new_value = new_field->ValueString(AidlConstantValueDecorator);
172 if (old_value != new_value) {
173 AIDL_ERROR(newer) << "Changed default value: " << old_value << " to " << new_value << ".";
Jiyong Park3656c3c2018-08-01 20:02:01 +0900174 compatible = false;
175 }
176 }
177 return compatible;
178}
179
Daniel Norman85aed542019-08-21 12:01:14 -0700180static bool are_compatible_enums(const AidlEnumDeclaration& older,
181 const AidlEnumDeclaration& newer) {
182 if (!are_compatible_types(older.GetBackingType(), newer.GetBackingType())) {
183 AIDL_ERROR(newer) << "Changed backing types.";
184 return false;
185 }
186
187 std::map<std::string, const AidlConstantValue*> old_enum_map;
188 for (const auto& enumerator : older.GetEnumerators()) {
189 old_enum_map[enumerator->GetName()] = enumerator->GetValue();
190 }
191 std::map<std::string, const AidlConstantValue*> new_enum_map;
192 for (const auto& enumerator : newer.GetEnumerators()) {
193 new_enum_map[enumerator->GetName()] = enumerator->GetValue();
194 }
195
196 bool compatible = true;
197 for (const auto& [name, value] : old_enum_map) {
198 if (new_enum_map.find(name) == new_enum_map.end()) {
199 AIDL_ERROR(newer) << "Removed enumerator from " << older.GetCanonicalName() << ": " << name;
200 compatible = false;
201 continue;
202 }
203 const string old_value =
204 old_enum_map[name]->As(older.GetBackingType(), AidlConstantValueDecorator);
205 const string new_value =
206 new_enum_map[name]->As(older.GetBackingType(), AidlConstantValueDecorator);
207 if (old_value != new_value) {
208 AIDL_ERROR(newer) << "Changed enumerator value: " << older.GetCanonicalName() << "::" << name
209 << " from " << old_value << " to " << new_value << ".";
210 compatible = false;
211 }
212 }
213 return compatible;
214}
215
Jiyong Park3656c3c2018-08-01 20:02:01 +0900216bool check_api(const Options& options, const IoDelegate& io_delegate) {
217 CHECK(options.IsStructured());
218 CHECK(options.InputFiles().size() == 2) << "--checkapi requires two inputs "
219 << "but got " << options.InputFiles().size();
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900220 AidlTypenames old_tns;
Jiyong Parke59c3682018-09-11 23:10:25 +0900221 const string old_dir = options.InputFiles().at(0);
Jiyong Park3656c3c2018-08-01 20:02:01 +0900222 vector<AidlDefinedType*> old_types;
Jiyong Parke59c3682018-09-11 23:10:25 +0900223 vector<string> old_files = io_delegate.ListFiles(old_dir);
224 if (old_files.size() == 0) {
225 AIDL_ERROR(old_dir) << "No API file exist";
Jiyong Park3656c3c2018-08-01 20:02:01 +0900226 return false;
227 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900228 for (const auto& file : old_files) {
Jiyong Parkf7534672019-08-12 22:06:22 +0900229 if (!android::base::EndsWith(file, ".aidl")) continue;
230
Jiyong Parke59c3682018-09-11 23:10:25 +0900231 vector<AidlDefinedType*> types;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900232 if (internals::load_and_validate_aidl(file, options, io_delegate, &old_tns, &types,
Jiyong Parke59c3682018-09-11 23:10:25 +0900233 nullptr /* imported_files */) != AidlError::OK) {
234 AIDL_ERROR(file) << "Failed to read.";
235 return false;
236 }
237 old_types.insert(old_types.end(), types.begin(), types.end());
238 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900239
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900240 AidlTypenames new_tns;
Jiyong Parke59c3682018-09-11 23:10:25 +0900241 const string new_dir = options.InputFiles().at(1);
Jiyong Park3656c3c2018-08-01 20:02:01 +0900242 vector<AidlDefinedType*> new_types;
Jiyong Parke59c3682018-09-11 23:10:25 +0900243 vector<string> new_files = io_delegate.ListFiles(new_dir);
244 if (new_files.size() == 0) {
245 AIDL_ERROR(new_dir) << "No API file exist";
Jiyong Park3656c3c2018-08-01 20:02:01 +0900246 return false;
247 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900248 for (const auto& file : new_files) {
Jiyong Parkf7534672019-08-12 22:06:22 +0900249 if (!android::base::EndsWith(file, ".aidl")) continue;
250
Jiyong Parke59c3682018-09-11 23:10:25 +0900251 vector<AidlDefinedType*> types;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900252 if (internals::load_and_validate_aidl(file, options, io_delegate, &new_tns, &types,
Jiyong Parke59c3682018-09-11 23:10:25 +0900253 nullptr /* imported_files */) != AidlError::OK) {
254 AIDL_ERROR(file) << "Failed to read.";
255 return false;
256 }
257 new_types.insert(new_types.end(), types.begin(), types.end());
258 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900259
260 map<string, AidlDefinedType*> new_map;
261 for (const auto t : new_types) {
262 new_map.emplace(t->GetCanonicalName(), t);
263 }
264
265 bool compatible = true;
266 for (const auto old_type : old_types) {
267 const auto found = new_map.find(old_type->GetCanonicalName());
268 if (found == new_map.end()) {
269 AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName();
270 compatible = false;
271 continue;
272 }
273 const auto new_type = found->second;
274
Daniel Norman85aed542019-08-21 12:01:14 -0700275 if (old_type->AsInterface() != nullptr) {
276 if (new_type->AsInterface() == nullptr) {
277 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
278 << " is changed from " << old_type->GetPreprocessDeclarationName()
279 << " to " << new_type->GetPreprocessDeclarationName();
280 compatible = false;
281 continue;
282 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900283 compatible &=
284 are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface()));
Daniel Norman85aed542019-08-21 12:01:14 -0700285 } else if (old_type->AsStructuredParcelable() != nullptr) {
286 if (new_type->AsStructuredParcelable() == nullptr) {
287 AIDL_ERROR(new_type) << "Parcelable" << new_type->GetCanonicalName()
288 << " is not structured. ";
289 compatible = false;
290 continue;
291 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900292 compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()),
293 *(new_type->AsStructuredParcelable()));
Daniel Norman85aed542019-08-21 12:01:14 -0700294 } else if (old_type->AsEnumDeclaration() != nullptr) {
295 if (new_type->AsEnumDeclaration() == nullptr) {
296 AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName()
297 << " is changed from " << old_type->GetPreprocessDeclarationName()
298 << " to " << new_type->GetPreprocessDeclarationName();
299 compatible = false;
300 continue;
301 }
302 compatible &=
303 are_compatible_enums(*(old_type->AsEnumDeclaration()), *(new_type->AsEnumDeclaration()));
304 } else {
305 AIDL_ERROR(old_type) << "Unsupported type " << old_type->GetPreprocessDeclarationName()
306 << " for " << old_type->GetCanonicalName();
307 compatible = false;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900308 }
309 }
310
311 return compatible;
312}
313
314} // namespace aidl
315} // namespace android