blob: 027626152a6c9d8364f37ed1ab962fe1c7f86292 [file] [log] [blame]
Alexandria Cornwall77788eb2016-09-06 15:16:49 -07001/*
2 * Copyright (C) 2016 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
17#include "DominatorTree.h"
18#include "ResourceTable.h"
19#include "link/Linkers.h"
20
21#include <algorithm>
22
23namespace aapt {
24
25namespace {
26
27/**
28 * Remove duplicated key-value entries from dominated resources.
29 *
30 * Based on the dominator tree, we can remove a value of an entry if:
31 *
32 * 1. The configuration for the entry's value is dominated by a configuration
33 * with an equivalent entry value.
34 * 2. All compatible configurations for the entry (those not in conflict and
35 * unrelated by domination with the configuration for the entry's value) have
36 * an equivalent entry value.
37 */
38class DominatedKeyValueRemover : public DominatorTree::BottomUpVisitor {
39public:
40 using Node = DominatorTree::Node;
41
42 explicit DominatedKeyValueRemover(IAaptContext* context, ResourceEntry* entry) :
43 mContext(context), mEntry(entry) {
44 }
45
46 void visitConfig(Node* node) {
47 Node* parent = node->parent();
48 if (!parent) {
49 return;
50 }
51 ResourceConfigValue* nodeValue = node->value();
52 ResourceConfigValue* parentValue = parent->value();
53 if (!nodeValue || !parentValue) {
54 return;
55 }
56 if (!nodeValue->value->equals(parentValue->value.get())) {
57 return;
58 }
59
60 // Compare compatible configs for this entry and ensure the values are
61 // equivalent.
62 const ConfigDescription& nodeConfiguration = nodeValue->config;
63 for (const auto& sibling : mEntry->values) {
64 if (!sibling->value) {
65 // Sibling was already removed.
66 continue;
67 }
68 if (nodeConfiguration.isCompatibleWith(sibling->config)
69 && !nodeValue->value->equals(sibling->value.get())) {
70 // The configurations are compatible, but the value is
71 // different, so we can't remove this value.
72 return;
73 }
74 }
75 if (mContext->verbose()) {
76 mContext->getDiagnostics()->note(
77 DiagMessage(nodeValue->value->getSource())
78 << "removing dominated duplicate resource with name \""
79 << mEntry->name << "\"");
80 }
81 nodeValue->value = {};
82 }
83
84private:
85 IAaptContext* mContext;
86 ResourceEntry* mEntry;
87};
88
89static void dedupeEntry(IAaptContext* context, ResourceEntry* entry) {
90 DominatorTree tree(entry->values);
91 DominatedKeyValueRemover remover(context, entry);
92 tree.accept(&remover);
93
94 // Erase the values that were removed.
95 entry->values.erase(std::remove_if(entry->values.begin(), entry->values.end(),
96 [](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
97 return val == nullptr || val->value == nullptr;
98 }), entry->values.end());
99}
100
101} // namespace
102
103bool ResourceDeduper::consume(IAaptContext* context, ResourceTable* table) {
104 for (auto& package : table->packages) {
105 for (auto& type : package->types) {
106 for (auto& entry : type->entries) {
107 dedupeEntry(context, entry.get());
108 }
109 }
110 }
111 return true;
112}
113
114} // aapt