| Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 1 | /* |
| 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" |
| 19 | #include "options.h" |
| 20 | #include "type_java.h" |
| 21 | |
| 22 | #include <map> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace aidl { |
| 28 | |
| 29 | using std::map; |
| 30 | using std::set; |
| 31 | using std::string; |
| 32 | using std::vector; |
| 33 | |
| 34 | static bool have_compatible_annotations(const AidlAnnotatable& older, |
| 35 | const AidlAnnotatable& newer) { |
| Jeongik Cha | 3271ffa | 2018-12-04 15:19:20 +0900 | [diff] [blame] | 36 | set<AidlAnnotation> olderAnnotations(older.GetAnnotations().begin(), |
| 37 | older.GetAnnotations().end()); |
| 38 | set<AidlAnnotation> newerAnnotations(newer.GetAnnotations().begin(), |
| 39 | newer.GetAnnotations().end()); |
| 40 | if (olderAnnotations != newerAnnotations) { |
| Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 41 | const string from = older.ToString().empty() ? "(empty)" : older.ToString(); |
| 42 | const string to = newer.ToString().empty() ? "(empty)" : newer.ToString(); |
| 43 | AIDL_ERROR(newer) << "Changed annotations: " << from << " to " << to; |
| 44 | return false; |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | static bool are_compatible_types(const AidlTypeSpecifier& older, const AidlTypeSpecifier& newer) { |
| 50 | bool compatible = true; |
| 51 | if (older.ToString() != newer.ToString()) { |
| 52 | AIDL_ERROR(newer) << "Type changed: " << older.ToString() << " to " << newer.ToString() << "."; |
| 53 | compatible = false; |
| 54 | } |
| 55 | compatible &= have_compatible_annotations(older, newer); |
| 56 | return compatible; |
| 57 | } |
| 58 | |
| 59 | static bool are_compatible_interfaces(const AidlInterface& older, const AidlInterface& newer) { |
| 60 | bool compatible = true; |
| 61 | compatible &= have_compatible_annotations(older, newer); |
| 62 | |
| 63 | map<string, AidlMethod*> new_methods; |
| 64 | for (const auto& m : newer.AsInterface()->GetMethods()) { |
| 65 | new_methods.emplace(m->Signature(), m.get()); |
| 66 | } |
| 67 | |
| 68 | for (const auto& old_m : older.AsInterface()->GetMethods()) { |
| 69 | const auto found = new_methods.find(old_m->Signature()); |
| 70 | if (found == new_methods.end()) { |
| 71 | AIDL_ERROR(old_m) << "Removed method: " << older.GetCanonicalName() << "." |
| 72 | << old_m->Signature(); |
| 73 | compatible = false; |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | // Compare IDs to detect method reordering. IDs are assigned by their |
| 78 | // textual order, so if there is an ID mismatch, that means reordering |
| 79 | // has happened. |
| 80 | const auto new_m = found->second; |
| 81 | if (old_m->GetId() != new_m->GetId()) { |
| 82 | AIDL_ERROR(new_m) << "Transaction ID changed: " << older.GetCanonicalName() << "." |
| 83 | << old_m->Signature() << " is changed from " << old_m->GetId() << " to " |
| 84 | << new_m->GetId() << "."; |
| 85 | compatible = false; |
| 86 | } |
| 87 | |
| 88 | compatible &= are_compatible_types(old_m->GetType(), new_m->GetType()); |
| 89 | |
| 90 | const auto& old_args = old_m->GetArguments(); |
| 91 | const auto& new_args = new_m->GetArguments(); |
| 92 | // this is guaranteed because arguments are part of AidlMethod::Signature() |
| 93 | CHECK(old_args.size() == new_args.size()); |
| 94 | for (size_t i = 0; i < old_args.size(); i++) { |
| 95 | const AidlArgument& old_a = *(old_args.at(i)); |
| 96 | const AidlArgument& new_a = *(new_args.at(i)); |
| 97 | compatible &= are_compatible_types(old_a.GetType(), new_a.GetType()); |
| 98 | |
| 99 | if (old_a.GetDirection() != new_a.GetDirection()) { |
| 100 | AIDL_ERROR(new_m) << "Direction changed: " << old_a.GetDirectionSpecifier() << " to " |
| 101 | << new_a.GetDirectionSpecifier() << "."; |
| 102 | compatible = false; |
| 103 | } |
| 104 | } |
| 105 | } |
| Jiyong Park | a428d21 | 2018-08-29 22:26:30 +0900 | [diff] [blame] | 106 | |
| 107 | map<string, AidlConstantDeclaration*> new_constdecls; |
| 108 | for (const auto& c : newer.AsInterface()->GetConstantDeclarations()) { |
| 109 | new_constdecls.emplace(c->GetName(), c.get()); |
| 110 | } |
| 111 | |
| 112 | for (const auto& old_c : older.AsInterface()->GetConstantDeclarations()) { |
| 113 | const auto found = new_constdecls.find(old_c->GetName()); |
| 114 | if (found == new_constdecls.end()) { |
| 115 | AIDL_ERROR(old_c) << "Removed constant declaration: " << older.GetCanonicalName() << "." |
| 116 | << old_c->GetName(); |
| 117 | compatible = false; |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | const auto new_c = found->second; |
| 122 | compatible &= are_compatible_types(old_c->GetType(), new_c->GetType()); |
| 123 | |
| 124 | const string old_value = old_c->ValueString(AidlConstantValueDecorator); |
| 125 | const string new_value = new_c->ValueString(AidlConstantValueDecorator); |
| 126 | if (old_value != new_value) { |
| 127 | AIDL_ERROR(newer) << "Changed constant value: " << older.GetCanonicalName() << "." |
| 128 | << old_c->GetName() << " from " << old_value << " to " << new_value << "."; |
| 129 | compatible = false; |
| 130 | } |
| 131 | } |
| Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 132 | return compatible; |
| 133 | } |
| 134 | |
| 135 | static bool are_compatible_parcelables(const AidlStructuredParcelable& older, |
| 136 | const AidlStructuredParcelable& newer) { |
| 137 | const auto& old_fields = older.GetFields(); |
| 138 | const auto& new_fields = newer.GetFields(); |
| 139 | if (old_fields.size() > new_fields.size()) { |
| 140 | // you can add new fields only at the end |
| 141 | AIDL_ERROR(newer) << "Number of fields in " << older.GetCanonicalName() << " is reduced from " |
| 142 | << old_fields.size() << " to " << new_fields.size() << "."; |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | bool compatible = true; |
| 147 | for (size_t i = 0; i < old_fields.size(); i++) { |
| Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 148 | const auto& old_field = old_fields.at(i); |
| 149 | const auto& new_field = new_fields.at(i); |
| 150 | compatible &= are_compatible_types(old_field->GetType(), new_field->GetType()); |
| Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 151 | |
| 152 | // Note: unlike method argument names, field name change is an incompatible |
| 153 | // change, otherwise, we can't detect |
| 154 | // parcelable Point {int x; int y;} -> parcelable Point {int y; int x;} |
| Jiyong Park | a468e2a | 2018-08-29 21:25:18 +0900 | [diff] [blame] | 155 | if (old_field->GetName() != new_field->GetName()) { |
| 156 | AIDL_ERROR(newer) << "Renamed field: " << old_field->GetName() << " to " |
| 157 | << new_field->GetName() << "."; |
| 158 | compatible = false; |
| 159 | } |
| 160 | |
| 161 | const string old_value = old_field->ValueString(AidlConstantValueDecorator); |
| 162 | const string new_value = new_field->ValueString(AidlConstantValueDecorator); |
| 163 | if (old_value != new_value) { |
| 164 | AIDL_ERROR(newer) << "Changed default value: " << old_value << " to " << new_value << "."; |
| Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 165 | compatible = false; |
| 166 | } |
| 167 | } |
| 168 | return compatible; |
| 169 | } |
| 170 | |
| 171 | bool check_api(const Options& options, const IoDelegate& io_delegate) { |
| 172 | CHECK(options.IsStructured()); |
| 173 | CHECK(options.InputFiles().size() == 2) << "--checkapi requires two inputs " |
| 174 | << "but got " << options.InputFiles().size(); |
| 175 | |
| 176 | java::JavaTypeNamespace old_ns; |
| 177 | old_ns.Init(); |
| Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 178 | const string old_dir = options.InputFiles().at(0); |
| Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 179 | vector<AidlDefinedType*> old_types; |
| Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 180 | vector<string> old_files = io_delegate.ListFiles(old_dir); |
| 181 | if (old_files.size() == 0) { |
| 182 | AIDL_ERROR(old_dir) << "No API file exist"; |
| Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 183 | return false; |
| 184 | } |
| Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 185 | for (const auto& file : old_files) { |
| 186 | vector<AidlDefinedType*> types; |
| 187 | if (internals::load_and_validate_aidl(file, options, io_delegate, &old_ns, &types, |
| 188 | nullptr /* imported_files */) != AidlError::OK) { |
| 189 | AIDL_ERROR(file) << "Failed to read."; |
| 190 | return false; |
| 191 | } |
| 192 | old_types.insert(old_types.end(), types.begin(), types.end()); |
| 193 | } |
| Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 194 | |
| 195 | java::JavaTypeNamespace new_ns; |
| 196 | new_ns.Init(); |
| Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 197 | const string new_dir = options.InputFiles().at(1); |
| Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 198 | vector<AidlDefinedType*> new_types; |
| Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 199 | vector<string> new_files = io_delegate.ListFiles(new_dir); |
| 200 | if (new_files.size() == 0) { |
| 201 | AIDL_ERROR(new_dir) << "No API file exist"; |
| Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 202 | return false; |
| 203 | } |
| Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 204 | for (const auto& file : new_files) { |
| 205 | vector<AidlDefinedType*> types; |
| 206 | if (internals::load_and_validate_aidl(file, options, io_delegate, &new_ns, &types, |
| 207 | nullptr /* imported_files */) != AidlError::OK) { |
| 208 | AIDL_ERROR(file) << "Failed to read."; |
| 209 | return false; |
| 210 | } |
| 211 | new_types.insert(new_types.end(), types.begin(), types.end()); |
| 212 | } |
| Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 213 | |
| 214 | map<string, AidlDefinedType*> new_map; |
| 215 | for (const auto t : new_types) { |
| 216 | new_map.emplace(t->GetCanonicalName(), t); |
| 217 | } |
| 218 | |
| 219 | bool compatible = true; |
| 220 | for (const auto old_type : old_types) { |
| 221 | const auto found = new_map.find(old_type->GetCanonicalName()); |
| 222 | if (found == new_map.end()) { |
| 223 | AIDL_ERROR(old_type) << "Removed type: " << old_type->GetCanonicalName(); |
| 224 | compatible = false; |
| 225 | continue; |
| 226 | } |
| 227 | const auto new_type = found->second; |
| 228 | |
| 229 | const bool old_is_iface = old_type->AsInterface() != nullptr; |
| 230 | const bool new_is_iface = new_type->AsInterface() != nullptr; |
| 231 | if (old_is_iface != new_is_iface) { |
| 232 | AIDL_ERROR(new_type) << "Type mismatch: " << old_type->GetCanonicalName() |
| 233 | << " is changed from " << old_type->GetPreprocessDeclarationName() |
| 234 | << " to " << new_type->GetPreprocessDeclarationName(); |
| 235 | compatible = false; |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | if (old_is_iface) { |
| 240 | compatible &= |
| 241 | are_compatible_interfaces(*(old_type->AsInterface()), *(new_type->AsInterface())); |
| 242 | } else { |
| 243 | CHECK(old_type->AsStructuredParcelable() != nullptr) |
| 244 | << "Parcelable" << old_type->GetCanonicalName() << " is not structured. "; |
| 245 | CHECK(new_type->AsStructuredParcelable() != nullptr) |
| 246 | << "Parcelable" << new_type->GetCanonicalName() << " is not structured. "; |
| 247 | compatible &= are_compatible_parcelables(*(old_type->AsStructuredParcelable()), |
| 248 | *(new_type->AsStructuredParcelable())); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return compatible; |
| 253 | } |
| 254 | |
| 255 | } // namespace aidl |
| 256 | } // namespace android |