blob: 13054bf14c783140a633e3b0bd9104aea45c3666 [file] [log] [blame]
Adam Lesinski34a16872018-02-23 16:18:10 -08001/*
2 * Copyright (C) 2018 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
Mårten Kongstad5c541f62018-06-20 08:46:41 +020017#include "androidfw/Locale.h"
18
Adam Lesinski34a16872018-02-23 16:18:10 -080019#include "link/NoDefaultResourceRemover.h"
20
21#include <algorithm>
22
23#include "ResourceTable.h"
24
Mårten Kongstad5c541f62018-06-20 08:46:41 +020025using android::ConfigDescription;
26
Adam Lesinski34a16872018-02-23 16:18:10 -080027namespace aapt {
28
29static bool IsDefaultConfigRequired(const ConfigDescription& config) {
30 // We don't want to be overzealous with resource removal, so have strict requirements.
31 // If a resource defines a value for a locale-only configuration, the default configuration is
32 // required.
33 if (ConfigDescription::DefaultConfig().diff(config) == ConfigDescription::CONFIG_LOCALE) {
34 return true;
35 }
36 return false;
37}
38
39static bool KeepResource(const std::unique_ptr<ResourceEntry>& entry) {
40 if (entry->visibility.level == Visibility::Level::kPublic) {
41 // Removing a public API without the developer knowing is bad, so just leave this here for now.
42 return true;
43 }
44
45 if (entry->HasDefaultValue()) {
46 // There is a default value, no removal needed.
47 return true;
48 }
49
50 // There is no default value defined, check if removal is required.
51 for (const auto& config_value : entry->values) {
52 if (IsDefaultConfigRequired(config_value->config)) {
53 return false;
54 }
55 }
56 return true;
57}
58
59bool NoDefaultResourceRemover::Consume(IAaptContext* context, ResourceTable* table) {
60 const ConfigDescription default_config = ConfigDescription::DefaultConfig();
61 for (auto& pkg : table->packages) {
62 for (auto& type : pkg->types) {
63 const auto end_iter = type->entries.end();
64 const auto new_end_iter =
65 std::stable_partition(type->entries.begin(), end_iter, KeepResource);
66 for (auto iter = new_end_iter; iter != end_iter; ++iter) {
67 const ResourceName name(pkg->name, type->type, (*iter)->name);
68 IDiagnostics* diag = context->GetDiagnostics();
69 diag->Warn(DiagMessage() << "removing resource " << name
70 << " without required default value");
71 if (context->IsVerbose()) {
72 diag->Note(DiagMessage() << " did you forget to remove all definitions?");
73 for (const auto& config_value : (*iter)->values) {
74 if (config_value->value != nullptr) {
75 diag->Note(DiagMessage(config_value->value->GetSource()) << "defined here");
76 }
77 }
78 }
79 }
80
81 type->entries.erase(new_end_iter, type->entries.end());
82 }
83 }
84 return true;
85}
86
87} // namespace aapt