Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1 | /* |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 2 | * Copyright (C) 2018, The Android Open Source Project |
| 3 | * |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 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 | #include "aidl.h" |
| 18 | #include "aidl_language.h" |
| 19 | #include "import_resolver.h" |
Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 20 | #include "logging.h" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 21 | #include "options.h" |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 22 | |
| 23 | #include <map> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 27 | #include <android-base/result.h> |
Jiyong Park | f753467 | 2019-08-12 22:06:22 +0900 | [diff] [blame] | 28 | #include <android-base/strings.h> |
| 29 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace aidl { |
| 32 | |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 33 | using android::base::Error; |
| 34 | using android::base::Result; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 35 | using std::map; |
| 36 | using std::set; |
| 37 | using std::string; |
| 38 | using std::vector; |
| 39 | |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 40 | static vector<string> get_strict_annotations(const AidlAnnotatable& node) { |
Steven Moreland | 7b6a7d9 | 2020-04-20 22:00:33 -0700 | [diff] [blame] | 41 | // This must be symmetrical (if you can add something, you must be able to |
| 42 | // remove it). The reason is that we have no way of knowing which interface a |
| 43 | // server serves and which interface a client serves (e.g. a callback |
| 44 | // interface). Note that this is being overly lenient. It makes sense for |
| 45 | // newer code to start accepting nullable things. However, here, we don't know |
| 46 | // if the client of an interface or the server of an interface is newer. |
| 47 | // |
| 48 | // Here are two examples to demonstrate this: |
| 49 | // - a new implementation might change so that it no longer returns null |
| 50 | // values (remove @nullable) |
| 51 | // - a new implementation might start accepting null values (add @nullable) |
| 52 | static const set<AidlAnnotation::Type> kIgnoreAnnotations{ |
| 53 | AidlAnnotation::Type::NULLABLE, |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 54 | // @JavaDerive doesn't affect read/write |
Jooyung Han | 9034500 | 2020-10-23 15:28:53 +0900 | [diff] [blame] | 55 | AidlAnnotation::Type::JAVA_DERIVE, |
Jeongik Cha | d0a1027 | 2020-08-06 16:33:36 +0900 | [diff] [blame] | 56 | AidlAnnotation::Type::JAVA_ONLY_IMMUTABLE, |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 57 | // @Backing for a enum type is checked by the enum checker |
| 58 | AidlAnnotation::Type::BACKING, |
| 59 | // @RustDerive doesn't affect read/write |
| 60 | AidlAnnotation::Type::RUST_DERIVE, |
Steven Moreland | 7b6a7d9 | 2020-04-20 22:00:33 -0700 | [diff] [blame] | 61 | }; |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 62 | vector<string> annotations; |
Steven Moreland | 7b6a7d9 | 2020-04-20 22:00:33 -0700 | [diff] [blame] | 63 | for (const AidlAnnotation& annotation : node.GetAnnotations()) { |
| 64 | if (kIgnoreAnnotations.find(annotation.GetType()) == kIgnoreAnnotations.end()) { |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame^] | 65 | annotations.push_back(annotation.ToString()); |
Steven Moreland | 7b6a7d9 | 2020-04-20 22:00:33 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | return annotations; |
| 69 | } |
| 70 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 71 | static bool have_compatible_annotations(const AidlAnnotatable& older, |
| 72 | const AidlAnnotatable& newer) { |
Jooyung Han | 69ea4ba | 2020-10-29 15:33:37 +0900 | [diff] [blame] | 73 | vector<string> olderAnnotations = get_strict_annotations(older); |
| 74 | vector<string> newerAnnotations = get_strict_annotations(newer); |
| 75 | sort(olderAnnotations.begin(), olderAnnotations.end()); |
| 76 | sort(newerAnnotations.begin(), newerAnnotations.end()); |
Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 77 | if (olderAnnotations != newerAnnotations) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 78 | const string from = older.ToString().empty() ? "(empty)" : older.ToString(); |
| 79 | const string to = newer.ToString().empty() ? "(empty)" : newer.ToString(); |
| 80 | AIDL_ERROR(newer) << "Changed annotations: " << from << " to " << to; |
| 81 | return false; |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | static bool are_compatible_types(const AidlTypeSpecifier& older, const AidlTypeSpecifier& newer) { |
| 87 | bool compatible = true; |
Jooyung Han | 965e31d | 2020-11-27 12:30:16 +0900 | [diff] [blame^] | 88 | if (older.Signature() != newer.Signature()) { |
| 89 | AIDL_ERROR(newer) << "Type changed: " << older.Signature() << " to " << newer.Signature() |
| 90 | << "."; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 91 | compatible = false; |
| 92 | } |
| 93 | compatible &= have_compatible_annotations(older, newer); |
| 94 | return compatible; |
| 95 | } |
| 96 | |
| 97 | static bool are_compatible_interfaces(const AidlInterface& older, const AidlInterface& newer) { |
| 98 | bool compatible = true; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 99 | |
| 100 | map<string, AidlMethod*> new_methods; |
| 101 | for (const auto& m : newer.AsInterface()->GetMethods()) { |
| 102 | new_methods.emplace(m->Signature(), m.get()); |
| 103 | } |
| 104 | |
| 105 | for (const auto& old_m : older.AsInterface()->GetMethods()) { |
| 106 | const auto found = new_methods.find(old_m->Signature()); |
| 107 | if (found == new_methods.end()) { |
Steven Moreland | 4ee6863 | 2018-12-14 15:52:46 -0800 | [diff] [blame] | 108 | AIDL_ERROR(old_m) << "Removed or changed method: " << older.GetCanonicalName() << "." |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 109 | << old_m->Signature(); |
| 110 | compatible = false; |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | // Compare IDs to detect method reordering. IDs are assigned by their |
| 115 | // textual order, so if there is an ID mismatch, that means reordering |
| 116 | // has happened. |
| 117 | const auto new_m = found->second; |
Steven Moreland | 4ee6863 | 2018-12-14 15:52:46 -0800 | [diff] [blame] | 118 | |
| 119 | if (old_m->IsOneway() != new_m->IsOneway()) { |
| 120 | AIDL_ERROR(new_m) << "Oneway attribute " << (old_m->IsOneway() ? "removed" : "added") << ": " |
| 121 | << older.GetCanonicalName() << "." << old_m->Signature(); |
| 122 | compatible = false; |
| 123 | } |
| 124 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 125 | if (old_m->GetId() != new_m->GetId()) { |
| 126 | AIDL_ERROR(new_m) << "Transaction ID changed: " << older.GetCanonicalName() << "." |
| 127 | << old_m->Signature() << " is changed from " << old_m->GetId() << " to " |
| 128 | << new_m->GetId() << "."; |
| 129 | compatible = false; |
| 130 | } |
| 131 | |
| 132 | compatible &= are_compatible_types(old_m->GetType(), new_m->GetType()); |
| 133 | |
| 134 | const auto& old_args = old_m->GetArguments(); |
| 135 | const auto& new_args = new_m->GetArguments(); |
| 136 | // this is guaranteed because arguments are part of AidlMethod::Signature() |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 137 | AIDL_FATAL_IF(old_args.size() != new_args.size(), old_m); |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 138 | for (size_t i = 0; i < old_args.size(); i++) { |
| 139 | const AidlArgument& old_a = *(old_args.at(i)); |
| 140 | const AidlArgument& new_a = *(new_args.at(i)); |
| 141 | compatible &= are_compatible_types(old_a.GetType(), new_a.GetType()); |
| 142 | |
| 143 | if (old_a.GetDirection() != new_a.GetDirection()) { |
| 144 | AIDL_ERROR(new_m) << "Direction changed: " << old_a.GetDirectionSpecifier() << " to " |
| 145 | << new_a.GetDirectionSpecifier() << "."; |
| 146 | compatible = false; |
| 147 | } |
| 148 | } |
| 149 | } |
Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 150 | |
| 151 | map<string, AidlConstantDeclaration*> new_constdecls; |
| 152 | for (const auto& c : newer.AsInterface()->GetConstantDeclarations()) { |
| 153 | new_constdecls.emplace(c->GetName(), c.get()); |
| 154 | } |
| 155 | |
| 156 | for (const auto& old_c : older.AsInterface()->GetConstantDeclarations()) { |
| 157 | const auto found = new_constdecls.find(old_c->GetName()); |
| 158 | if (found == new_constdecls.end()) { |
| 159 | AIDL_ERROR(old_c) << "Removed constant declaration: " << older.GetCanonicalName() << "." |
| 160 | << old_c->GetName(); |
| 161 | compatible = false; |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | const auto new_c = found->second; |
| 166 | compatible &= are_compatible_types(old_c->GetType(), new_c->GetType()); |
| 167 | |
| 168 | const string old_value = old_c->ValueString(AidlConstantValueDecorator); |
| 169 | const string new_value = new_c->ValueString(AidlConstantValueDecorator); |
| 170 | if (old_value != new_value) { |
| 171 | AIDL_ERROR(newer) << "Changed constant value: " << older.GetCanonicalName() << "." |
| 172 | << old_c->GetName() << " from " << old_value << " to " << new_value << "."; |
| 173 | compatible = false; |
| 174 | } |
| 175 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 176 | return compatible; |
| 177 | } |
| 178 | |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 179 | // returns whether the given type when defaulted will be accepted by |
| 180 | // unmarshalling code |
| 181 | static bool has_usable_nil_type(const AidlTypeSpecifier& specifier) { |
| 182 | // TODO(b/155238508): fix for primitives |
| 183 | |
| 184 | // This technically only applies in C++, but even if both the client and the |
| 185 | // server of an interface are in Java at a particular point in time, where |
| 186 | // null is currently always acceptable, we want to make sure that versions |
| 187 | // of this service can work in native and future backends without a problem. |
| 188 | // Also, in that case, adding nullable does not hurt. |
| 189 | return specifier.IsNullable(); |
| 190 | } |
| 191 | |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 192 | static bool HasZeroEnumerator(const AidlEnumDeclaration& enum_decl) { |
| 193 | return std::any_of(enum_decl.GetEnumerators().begin(), enum_decl.GetEnumerators().end(), |
| 194 | [&](const unique_ptr<AidlEnumerator>& enumerator) { |
| 195 | return enumerator->GetValue()->ValueString( |
| 196 | enum_decl.GetBackingType(), AidlConstantValueDecorator) == "0"; |
| 197 | }); |
| 198 | } |
| 199 | |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 200 | template <typename ParcelableType> |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 201 | static bool are_compatible_parcelables(const ParcelableType& older, const AidlTypenames&, |
| 202 | const ParcelableType& newer, |
| 203 | const AidlTypenames& new_types) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 204 | const auto& old_fields = older.GetFields(); |
| 205 | const auto& new_fields = newer.GetFields(); |
| 206 | if (old_fields.size() > new_fields.size()) { |
| 207 | // you can add new fields only at the end |
| 208 | AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from " |
| 209 | << old_fields.size() << " to " << new_fields.size() << "."; |
| 210 | return false; |
| 211 | } |
Devin Moore | c7e47a3 | 2020-08-07 10:55:25 -0700 | [diff] [blame] | 212 | if (newer.IsFixedSize() && old_fields.size() != new_fields.size()) { |
| 213 | AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is changed from " |
| 214 | << old_fields.size() << " to " << new_fields.size() |
| 215 | << ". This is an incompatible change for FixedSize types."; |
| 216 | return false; |
| 217 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 218 | |
| 219 | bool compatible = true; |
| 220 | for (size_t i = 0; i < old_fields.size(); i++) { |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 221 | const auto& old_field = old_fields.at(i); |
| 222 | const auto& new_field = new_fields.at(i); |
| 223 | compatible &= are_compatible_types(old_field->GetType(), new_field->GetType()); |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 224 | |
Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 225 | const string old_value = old_field->ValueString(AidlConstantValueDecorator); |
| 226 | const string new_value = new_field->ValueString(AidlConstantValueDecorator); |
| 227 | if (old_value != new_value) { |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 228 | AIDL_ERROR(new_field) << "Changed default value: " << old_value << " to " << new_value << "."; |
| 229 | compatible = false; |
| 230 | } |
| 231 | } |
| 232 | |
Jiyong Park | b07d993 | 2020-05-15 12:56:54 +0900 | [diff] [blame] | 233 | // Reordering of fields is an incompatible change. |
| 234 | for (size_t i = 0; i < new_fields.size(); i++) { |
| 235 | const auto& new_field = new_fields.at(i); |
| 236 | auto found = std::find_if(old_fields.begin(), old_fields.end(), [&new_field](const auto& f) { |
| 237 | return new_field->GetName() == f->GetName(); |
| 238 | }); |
| 239 | if (found != old_fields.end()) { |
| 240 | size_t old_index = std::distance(old_fields.begin(), found); |
| 241 | if (old_index != i) { |
| 242 | AIDL_ERROR(new_field) << "Reordered " << new_field->GetName() << " from " << old_index |
| 243 | << " to " << i << "."; |
| 244 | compatible = false; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Steven Moreland | 370ed34 | 2020-04-28 18:14:39 -0700 | [diff] [blame] | 249 | for (size_t i = old_fields.size(); i < new_fields.size(); i++) { |
| 250 | const auto& new_field = new_fields.at(i); |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 251 | if (new_field->GetDefaultValue()) { |
| 252 | continue; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 253 | } |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 254 | |
| 255 | // null is accepted as a valid default value |
| 256 | if (has_usable_nil_type(new_field->GetType())) { |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | // enum can't be nullable, but it's okay if it has 0 as a valid enumerator. |
| 261 | if (const auto& enum_decl = new_types.GetEnumDeclaration(new_field->GetType()); |
| 262 | enum_decl != nullptr) { |
| 263 | if (HasZeroEnumerator(*enum_decl)) { |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | // TODO(b/142893595): Rephrase the message: "provide a default value or make sure ..." |
| 268 | AIDL_ERROR(new_field) << "Field '" << new_field->GetName() << "' of enum '" |
| 269 | << enum_decl->GetName() |
| 270 | << "' can't be initialized as '0'. Please make sure '" |
| 271 | << enum_decl->GetName() << "' has '0' as a valid value."; |
| 272 | compatible = false; |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | // Old API versions may suffer from the issue presented here. There is |
| 277 | // only a finite number in Android, which we must allow indefinitely. |
| 278 | struct HistoricalException { |
| 279 | std::string canonical; |
| 280 | std::string field; |
| 281 | }; |
| 282 | static std::vector<HistoricalException> exceptions = { |
| 283 | {"android.net.DhcpResultsParcelable", "serverHostName"}, |
| 284 | {"android.net.ResolverParamsParcel", "resolverOptions"}, |
| 285 | }; |
| 286 | bool excepted = false; |
| 287 | for (const HistoricalException& exception : exceptions) { |
| 288 | if (older.GetCanonicalName() == exception.canonical && |
| 289 | new_field->GetName() == exception.field) { |
| 290 | excepted = true; |
| 291 | break; |
| 292 | } |
| 293 | } |
| 294 | if (excepted) continue; |
| 295 | |
| 296 | AIDL_ERROR(new_field) |
| 297 | << "Field '" << new_field->GetName() |
| 298 | << "' does not have a useful default in some backends. Please either provide a default " |
| 299 | "value for this field or mark the field as @nullable. This value or a null value will " |
| 300 | "be used automatically when an old version of this parcelable is sent to a process " |
| 301 | "which understands a new version of this parcelable. In order to make sure your code " |
| 302 | "continues to be backwards compatible, make sure the default or null value does not " |
| 303 | "cause a semantic change to this parcelable."; |
| 304 | compatible = false; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 305 | } |
| 306 | return compatible; |
| 307 | } |
| 308 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 309 | static bool are_compatible_enums(const AidlEnumDeclaration& older, |
| 310 | const AidlEnumDeclaration& newer) { |
| 311 | if (!are_compatible_types(older.GetBackingType(), newer.GetBackingType())) { |
| 312 | AIDL_ERROR(newer) << "Changed backing types."; |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | std::map<std::string, const AidlConstantValue*> old_enum_map; |
| 317 | for (const auto& enumerator : older.GetEnumerators()) { |
| 318 | old_enum_map[enumerator->GetName()] = enumerator->GetValue(); |
| 319 | } |
| 320 | std::map<std::string, const AidlConstantValue*> new_enum_map; |
| 321 | for (const auto& enumerator : newer.GetEnumerators()) { |
| 322 | new_enum_map[enumerator->GetName()] = enumerator->GetValue(); |
| 323 | } |
| 324 | |
| 325 | bool compatible = true; |
| 326 | for (const auto& [name, value] : old_enum_map) { |
| 327 | if (new_enum_map.find(name) == new_enum_map.end()) { |
| 328 | AIDL_ERROR(newer) << "Removed enumerator from " << older.GetCanonicalName() << ": " << name; |
| 329 | compatible = false; |
| 330 | continue; |
| 331 | } |
| 332 | const string old_value = |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 333 | old_enum_map[name]->ValueString(older.GetBackingType(), AidlConstantValueDecorator); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 334 | const string new_value = |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 335 | new_enum_map[name]->ValueString(newer.GetBackingType(), AidlConstantValueDecorator); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 336 | if (old_value != new_value) { |
| 337 | AIDL_ERROR(newer) << "Changed enumerator value: " << older.GetCanonicalName() << "::" << name |
| 338 | << " from " << old_value << " to " << new_value << "."; |
| 339 | compatible = false; |
| 340 | } |
| 341 | } |
| 342 | return compatible; |
| 343 | } |
| 344 | |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 345 | static Result<AidlTypenames> load_from_dir(const Options& options, const IoDelegate& io_delegate, |
| 346 | const std::string& dir) { |
| 347 | AidlTypenames typenames; |
| 348 | for (const auto& file : io_delegate.ListFiles(dir)) { |
| 349 | if (!android::base::EndsWith(file, ".aidl")) continue; |
| 350 | if (internals::load_and_validate_aidl(file, options, io_delegate, &typenames, |
| 351 | nullptr /* imported_files */) != AidlError::OK) { |
| 352 | AIDL_ERROR(file) << "Failed to read."; |
| 353 | return Error(); |
| 354 | } |
| 355 | } |
| 356 | return typenames; |
| 357 | } |
| 358 | |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 359 | bool check_api(const Options& options, const IoDelegate& io_delegate) { |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 360 | AIDL_FATAL_IF(!options.IsStructured(), AIDL_LOCATION_HERE); |
| 361 | AIDL_FATAL_IF(options.InputFiles().size() != 2, AIDL_LOCATION_HERE) |
| 362 | << "--checkapi requires two inputs " |
| 363 | << "but got " << options.InputFiles().size(); |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 364 | auto old_tns = load_from_dir(options, io_delegate, options.InputFiles().at(0)); |
| 365 | if (!old_tns.ok()) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 366 | return false; |
| 367 | } |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 368 | auto new_tns = load_from_dir(options, io_delegate, options.InputFiles().at(1)); |
| 369 | if (!new_tns.ok()) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 370 | return false; |
| 371 | } |
Jiyong Park | f753467 | 2019-08-12 22:06:22 +0900 | [diff] [blame] | 372 | |
Jiyong Park | 0cf03b1 | 2020-07-22 19:36:34 +0900 | [diff] [blame] | 373 | std::vector<AidlDefinedType*> old_types = old_tns->AllDefinedTypes(); |
| 374 | std::vector<AidlDefinedType*> new_types = new_tns->AllDefinedTypes(); |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 375 | |
| 376 | map<string, AidlDefinedType*> new_map; |
| 377 | for (const auto t : new_types) { |
| 378 | new_map.emplace(t->GetCanonicalName(), t); |
| 379 | } |
| 380 | |
| 381 | bool compatible = true; |
| 382 | for (const auto old_type : old_types) { |
| 383 | const auto found = new_map.find(old_type->GetCanonicalName()); |
| 384 | if (found == new_map.end()) { |
| 385 | AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName(); |
| 386 | compatible = false; |
| 387 | continue; |
| 388 | } |
| 389 | const auto new_type = found->second; |
| 390 | |
Devin Moore | db7ac51 | 2020-08-07 11:17:36 -0700 | [diff] [blame] | 391 | if (!have_compatible_annotations(*old_type, *new_type)) { |
| 392 | compatible = false; |
| 393 | } |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 394 | if (old_type->AsInterface() != nullptr) { |
| 395 | if (new_type->AsInterface() == nullptr) { |
| 396 | AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName() |
| 397 | << " is changed from " << old_type->GetPreprocessDeclarationName() |
| 398 | << " to " << new_type->GetPreprocessDeclarationName(); |
| 399 | compatible = false; |
| 400 | continue; |
| 401 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 402 | compatible &= |
| 403 | are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface())); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 404 | } else if (old_type->AsStructuredParcelable() != nullptr) { |
| 405 | if (new_type->AsStructuredParcelable() == nullptr) { |
| 406 | AIDL_ERROR(new_type) << "Parcelable" << new_type->GetCanonicalName() |
| 407 | << " is not structured. "; |
| 408 | compatible = false; |
| 409 | continue; |
| 410 | } |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 411 | compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()), *old_tns, |
| 412 | *(new_type->AsStructuredParcelable()), *new_tns); |
Jooyung Han | 2946afc | 2020-10-05 20:29:16 +0900 | [diff] [blame] | 413 | } else if (old_type->AsUnionDeclaration() != nullptr) { |
| 414 | if (new_type->AsUnionDeclaration() == nullptr) { |
| 415 | AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName() |
| 416 | << " is changed from " << old_type->GetPreprocessDeclarationName() |
| 417 | << " to " << new_type->GetPreprocessDeclarationName(); |
| 418 | compatible = false; |
| 419 | continue; |
| 420 | } |
Jooyung Han | 636fd2f | 2020-10-22 11:33:45 +0900 | [diff] [blame] | 421 | compatible &= are_compatible_parcelables(*(old_type->AsUnionDeclaration()), *old_tns, |
| 422 | *(new_type->AsUnionDeclaration()), *new_tns); |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 423 | } else if (old_type->AsEnumDeclaration() != nullptr) { |
| 424 | if (new_type->AsEnumDeclaration() == nullptr) { |
| 425 | AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName() |
| 426 | << " is changed from " << old_type->GetPreprocessDeclarationName() |
| 427 | << " to " << new_type->GetPreprocessDeclarationName(); |
| 428 | compatible = false; |
| 429 | continue; |
| 430 | } |
| 431 | compatible &= |
| 432 | are_compatible_enums(*(old_type->AsEnumDeclaration()), *(new_type->AsEnumDeclaration())); |
| 433 | } else { |
| 434 | AIDL_ERROR(old_type) << "Unsupported type " << old_type->GetPreprocessDeclarationName() |
| 435 | << " for " << old_type->GetCanonicalName(); |
| 436 | compatible = false; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | return compatible; |
| 441 | } |
| 442 | |
| 443 | } // namespace aidl |
| 444 | } // namespace android |