blob: 84f438520e902ddccd630ec4da17193e35a8b9c9 [file] [log] [blame]
Adam Lesinski330edcd2015-05-04 17:40:56 -07001/*
2 * Copyright (C) 2015 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 "Debug.h"
18#include "ResourceTable.h"
19#include "ResourceValues.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include "util/Util.h"
21#include "ValueVisitor.h"
Adam Lesinski330edcd2015-05-04 17:40:56 -070022
23#include <algorithm>
24#include <iostream>
25#include <map>
26#include <memory>
Adam Lesinskid13fb242015-05-12 20:40:48 -070027#include <queue>
Adam Lesinski330edcd2015-05-04 17:40:56 -070028#include <set>
29#include <vector>
30
31namespace aapt {
32
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033struct PrintVisitor : public ValueVisitor {
34 using ValueVisitor::visit;
35
36 void visit(Attribute* attr) override {
Adam Lesinski330edcd2015-05-04 17:40:56 -070037 std::cout << "(attr) type=";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038 attr->printMask(&std::cout);
Adam Lesinski330edcd2015-05-04 17:40:56 -070039 static constexpr uint32_t kMask = android::ResTable_map::TYPE_ENUM |
40 android::ResTable_map::TYPE_FLAGS;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041 if (attr->typeMask & kMask) {
42 for (const auto& symbol : attr->symbols) {
43 std::cout << "\n " << symbol.symbol.name.value().entry;
44 if (symbol.symbol.id) {
45 std::cout << " (" << symbol.symbol.id.value() << ")";
46 }
47 std::cout << " = " << symbol.value;
Adam Lesinski330edcd2015-05-04 17:40:56 -070048 }
49 }
50 }
51
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052 void visit(Style* style) override {
Adam Lesinski330edcd2015-05-04 17:40:56 -070053 std::cout << "(style)";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070054 if (style->parent) {
Adam Lesinski330edcd2015-05-04 17:40:56 -070055 std::cout << " parent=";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056 if (style->parent.value().name) {
57 std::cout << style->parent.value().name.value() << " ";
Adam Lesinski330edcd2015-05-04 17:40:56 -070058 }
59
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060 if (style->parent.value().id) {
61 std::cout << style->parent.value().id.value();
Adam Lesinski330edcd2015-05-04 17:40:56 -070062 }
63 }
64
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065 for (const auto& entry : style->entries) {
Adam Lesinski330edcd2015-05-04 17:40:56 -070066 std::cout << "\n ";
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067 if (entry.key.name) {
68 std::cout << entry.key.name.value().package << ":" << entry.key.name.value().entry;
Adam Lesinski330edcd2015-05-04 17:40:56 -070069 }
70
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071 if (entry.key.id) {
72 std::cout << "(" << entry.key.id.value() << ")";
Adam Lesinski330edcd2015-05-04 17:40:56 -070073 }
74
75 std::cout << "=" << *entry.value;
76 }
77 }
78
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079 void visit(Array* array) override {
80 array->print(&std::cout);
Adam Lesinski330edcd2015-05-04 17:40:56 -070081 }
82
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083 void visit(Plural* plural) override {
84 plural->print(&std::cout);
Adam Lesinski330edcd2015-05-04 17:40:56 -070085 }
86
Adam Lesinski1ab598f2015-08-14 14:26:04 -070087 void visit(Styleable* styleable) override {
88 styleable->print(&std::cout);
Adam Lesinski330edcd2015-05-04 17:40:56 -070089 }
90
Adam Lesinski1ab598f2015-08-14 14:26:04 -070091 void visitItem(Item* item) override {
92 item->print(&std::cout);
Adam Lesinski330edcd2015-05-04 17:40:56 -070093 }
94};
95
Adam Lesinski1ab598f2015-08-14 14:26:04 -070096void Debug::printTable(ResourceTable* table) {
97 for (auto& package : table->packages) {
98 std::cout << "Package name=" << package->name;
99 if (package->id) {
100 std::cout << " id=" << std::hex << (int) package->id.value() << std::dec;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700101 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102 std::cout << std::endl;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700103
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700104 for (const auto& type : package->types) {
105 std::cout << " type " << type->type;
106 if (type->id) {
107 std::cout << " id=" << std::hex << (int) type->id.value() << std::dec;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700108 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109 std::cout << " entryCount=" << type->entries.size() << std::endl;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700110
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111 std::vector<const ResourceEntry*> sortedEntries;
112 for (const auto& entry : type->entries) {
113 auto iter = std::lower_bound(sortedEntries.begin(), sortedEntries.end(), entry.get(),
114 [](const ResourceEntry* a, const ResourceEntry* b) -> bool {
115 if (a->id && b->id) {
116 return a->id.value() < b->id.value();
117 } else if (a->id) {
118 return true;
119 } else {
120 return false;
121 }
122 });
123 sortedEntries.insert(iter, entry.get());
124 }
125
126 for (const ResourceEntry* entry : sortedEntries) {
127 ResourceId id = {
128 package->id ? package->id.value() : uint8_t(0),
129 type->id ? type->id.value() : uint8_t(0),
130 entry->id ? entry->id.value() : uint16_t(0)
131 };
132
133 ResourceName name = { package->name, type->type, entry->name };
134 std::cout << " spec resource " << id << " " << name;
135 if (entry->publicStatus.isPublic) {
136 std::cout << " PUBLIC";
137 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700138 std::cout << std::endl;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139
140 PrintVisitor visitor;
141 for (const auto& value : entry->values) {
142 std::cout << " (" << value.config << ") ";
143 value.value->accept(&visitor);
144 std::cout << std::endl;
145 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700146 }
147 }
148 }
149}
150
151static size_t getNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) {
152 auto iter = std::lower_bound(names.begin(), names.end(), name);
153 assert(iter != names.end() && *iter == name);
154 return std::distance(names.begin(), iter);
155}
156
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157void Debug::printStyleGraph(ResourceTable* table, const ResourceName& targetStyle) {
Adam Lesinski330edcd2015-05-04 17:40:56 -0700158 std::map<ResourceName, std::set<ResourceName>> graph;
159
Adam Lesinskid13fb242015-05-12 20:40:48 -0700160 std::queue<ResourceName> stylesToVisit;
161 stylesToVisit.push(targetStyle);
162 for (; !stylesToVisit.empty(); stylesToVisit.pop()) {
163 const ResourceName& styleName = stylesToVisit.front();
164 std::set<ResourceName>& parents = graph[styleName];
165 if (!parents.empty()) {
166 // We've already visited this style.
167 continue;
168 }
169
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170 Maybe<ResourceTable::SearchResult> result = table->findResource(styleName);
171 if (result) {
172 ResourceEntry* entry = result.value().entry;
Adam Lesinski330edcd2015-05-04 17:40:56 -0700173 for (const auto& value : entry->values) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700174 if (Style* style = valueCast<Style>(value.value.get())) {
175 if (style->parent && style->parent.value().name) {
176 parents.insert(style->parent.value().name.value());
177 stylesToVisit.push(style->parent.value().name.value());
Adam Lesinski330edcd2015-05-04 17:40:56 -0700178 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700180 }
181 }
182 }
183
Adam Lesinskid13fb242015-05-12 20:40:48 -0700184 std::vector<ResourceName> names;
185 for (const auto& entry : graph) {
186 names.push_back(entry.first);
187 }
Adam Lesinski330edcd2015-05-04 17:40:56 -0700188
189 std::cout << "digraph styles {\n";
Adam Lesinski330edcd2015-05-04 17:40:56 -0700190 for (const auto& name : names) {
191 std::cout << " node_" << getNodeIndex(names, name)
Adam Lesinskid13fb242015-05-12 20:40:48 -0700192 << " [label=\"" << name << "\"];\n";
Adam Lesinski330edcd2015-05-04 17:40:56 -0700193 }
194
195 for (const auto& entry : graph) {
Adam Lesinskid13fb242015-05-12 20:40:48 -0700196 const ResourceName& styleName = entry.first;
197 size_t styleNodeIndex = getNodeIndex(names, styleName);
Adam Lesinski330edcd2015-05-04 17:40:56 -0700198
Adam Lesinskid13fb242015-05-12 20:40:48 -0700199 for (const auto& parentName : entry.second) {
200 std::cout << " node_" << styleNodeIndex << " -> "
201 << "node_" << getNodeIndex(names, parentName) << ";\n";
Adam Lesinski330edcd2015-05-04 17:40:56 -0700202 }
203 }
204
205 std::cout << "}" << std::endl;
206}
207
208} // namespace aapt