Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 1 | /* |
| 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" |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <memory> |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 22 | #include <queue> |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 23 | #include <set> |
| 24 | #include <vector> |
| 25 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "android-base/logging.h" |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 27 | #include "android-base/stringprintf.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 28 | |
| 29 | #include "ResourceTable.h" |
| 30 | #include "ResourceValues.h" |
| 31 | #include "ValueVisitor.h" |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 32 | #include "text/Printer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | #include "util/Util.h" |
| 34 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 35 | using ::aapt::text::Printer; |
| 36 | using ::android::StringPiece; |
| 37 | using ::android::base::StringPrintf; |
| 38 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 39 | namespace aapt { |
| 40 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 41 | namespace { |
| 42 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 43 | class ValueHeadlinePrinter : public ConstValueVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | public: |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 45 | using ConstValueVisitor::Visit; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 47 | explicit ValueHeadlinePrinter(const std::string& package, Printer* printer) |
| 48 | : package_(package), printer_(printer) { |
| 49 | } |
| 50 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 51 | void Visit(const Attribute* attr) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 52 | printer_->Print("(attr) type="); |
| 53 | printer_->Print(attr->MaskString()); |
| 54 | if (!attr->symbols.empty()) { |
| 55 | printer_->Print(StringPrintf(" size=%zd", attr->symbols.size())); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void Visit(const Style* style) override { |
| 60 | printer_->Print(StringPrintf("(style) size=%zd", style->entries.size())); |
| 61 | if (style->parent) { |
| 62 | printer_->Print(" parent="); |
| 63 | |
| 64 | const Reference& parent_ref = style->parent.value(); |
| 65 | if (parent_ref.name) { |
| 66 | if (parent_ref.private_reference) { |
| 67 | printer_->Print("*"); |
| 68 | } |
| 69 | |
| 70 | const ResourceName& parent_name = parent_ref.name.value(); |
| 71 | if (package_ != parent_name.package) { |
| 72 | printer_->Print(parent_name.package); |
| 73 | printer_->Print(":"); |
| 74 | } |
| 75 | printer_->Print(to_string(parent_name.type)); |
| 76 | printer_->Print("/"); |
| 77 | printer_->Print(parent_name.entry); |
| 78 | if (parent_ref.id) { |
| 79 | printer_->Print(" ("); |
| 80 | printer_->Print(parent_ref.id.value().to_string()); |
| 81 | printer_->Print(")"); |
| 82 | } |
| 83 | } else if (parent_ref.id) { |
| 84 | printer_->Print(parent_ref.id.value().to_string()); |
| 85 | } else { |
| 86 | printer_->Print("???"); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void Visit(const Array* array) override { |
| 92 | printer_->Print(StringPrintf("(array) size=%zd", array->elements.size())); |
| 93 | } |
| 94 | |
| 95 | void Visit(const Plural* plural) override { |
| 96 | size_t count = std::count_if(plural->values.begin(), plural->values.end(), |
| 97 | [](const std::unique_ptr<Item>& v) { return v != nullptr; }); |
| 98 | printer_->Print(StringPrintf("(plurals) size=%zd", count)); |
| 99 | } |
| 100 | |
| 101 | void Visit(const Styleable* styleable) override { |
| 102 | printer_->Println(StringPrintf("(styleable) size=%zd", styleable->entries.size())); |
| 103 | } |
| 104 | |
| 105 | void VisitItem(const Item* item) override { |
| 106 | // Pretty much guaranteed to be one line. |
| 107 | if (const Reference* ref = ValueCast<Reference>(item)) { |
| 108 | // Special case Reference so that we can print local resources without a package name. |
| 109 | ref->PrettyPrint(package_, printer_); |
| 110 | } else { |
| 111 | item->PrettyPrint(printer_); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | std::string package_; |
| 117 | Printer* printer_; |
| 118 | }; |
| 119 | |
| 120 | class ValueBodyPrinter : public ConstValueVisitor { |
| 121 | public: |
| 122 | using ConstValueVisitor::Visit; |
| 123 | |
| 124 | explicit ValueBodyPrinter(const std::string& package, Printer* printer) |
| 125 | : package_(package), printer_(printer) { |
| 126 | } |
| 127 | |
| 128 | void Visit(const Attribute* attr) override { |
| 129 | constexpr uint32_t kMask = android::ResTable_map::TYPE_ENUM | android::ResTable_map::TYPE_FLAGS; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 130 | if (attr->type_mask & kMask) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | for (const auto& symbol : attr->symbols) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 132 | printer_->Print(symbol.symbol.name.value().entry); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 133 | if (symbol.symbol.id) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 134 | printer_->Print("("); |
| 135 | printer_->Print(symbol.symbol.id.value().to_string()); |
| 136 | printer_->Print(")"); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 137 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 138 | printer_->Println(StringPrintf("=0x%08x", symbol.value)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 140 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 142 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 143 | void Visit(const Style* style) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | for (const auto& entry : style->entries) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | if (entry.key.name) { |
| 146 | const ResourceName& name = entry.key.name.value(); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 147 | if (!name.package.empty() && name.package != package_) { |
| 148 | printer_->Print(name.package); |
| 149 | printer_->Print(":"); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 150 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 151 | printer_->Print(name.entry); |
| 152 | |
| 153 | if (entry.key.id) { |
| 154 | printer_->Print("("); |
| 155 | printer_->Print(entry.key.id.value().to_string()); |
| 156 | printer_->Print(")"); |
| 157 | } |
| 158 | } else if (entry.key.id) { |
| 159 | printer_->Print(entry.key.id.value().to_string()); |
| 160 | } else { |
| 161 | printer_->Print("???"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 164 | printer_->Print("="); |
| 165 | PrintItem(*entry.value); |
| 166 | printer_->Println(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 167 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 169 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 170 | void Visit(const Array* array) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 171 | const size_t count = array->elements.size(); |
| 172 | printer_->Print("["); |
| 173 | if (count > 0) { |
| 174 | for (size_t i = 0u; i < count; i++) { |
| 175 | if (i != 0u && i % 4u == 0u) { |
| 176 | printer_->Println(); |
| 177 | printer_->Print(" "); |
| 178 | } |
| 179 | PrintItem(*array->elements[i]); |
| 180 | if (i != count - 1) { |
| 181 | printer_->Print(", "); |
| 182 | } |
| 183 | } |
| 184 | printer_->Println("]"); |
| 185 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 186 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 187 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 188 | void Visit(const Plural* plural) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 189 | constexpr std::array<const char*, Plural::Count> kPluralNames = { |
| 190 | {"zero", "one", "two", "few", "many", "other"}}; |
| 191 | |
| 192 | for (size_t i = 0; i < Plural::Count; i++) { |
| 193 | if (plural->values[i] != nullptr) { |
| 194 | printer_->Print(StringPrintf("%s=", kPluralNames[i])); |
| 195 | PrintItem(*plural->values[i]); |
| 196 | printer_->Println(); |
| 197 | } |
| 198 | } |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 199 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 200 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 201 | void Visit(const Styleable* styleable) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 202 | for (const auto& attr : styleable->entries) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | if (attr.name) { |
| 204 | const ResourceName& name = attr.name.value(); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 205 | if (!name.package.empty() && name.package != package_) { |
| 206 | printer_->Print(name.package); |
| 207 | printer_->Print(":"); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 208 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 209 | printer_->Print(name.entry); |
| 210 | |
| 211 | if (attr.id) { |
| 212 | printer_->Print("("); |
| 213 | printer_->Print(attr.id.value().to_string()); |
| 214 | printer_->Print(")"); |
| 215 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 216 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 217 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | if (attr.id) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 219 | printer_->Print(attr.id.value().to_string()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 220 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 221 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 224 | void VisitItem(const Item* item) override { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 225 | // Intentionally left empty, we already printed the Items. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 226 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 227 | |
| 228 | private: |
| 229 | void PrintItem(const Item& item) { |
| 230 | if (const Reference* ref = ValueCast<Reference>(&item)) { |
| 231 | // Special case Reference so that we can print local resources without a package name. |
| 232 | ref->PrettyPrint(package_, printer_); |
| 233 | } else { |
| 234 | item.PrettyPrint(printer_); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | std::string package_; |
| 239 | Printer* printer_; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 240 | }; |
| 241 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 242 | } // namespace |
| 243 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 244 | void Debug::PrintTable(const ResourceTable& table, const DebugPrintTableOptions& options, |
| 245 | Printer* printer) { |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 246 | for (const auto& package : table.packages) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 247 | ValueHeadlinePrinter headline_printer(package->name, printer); |
| 248 | ValueBodyPrinter body_printer(package->name, printer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 249 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 250 | printer->Print("Package name="); |
| 251 | printer->Print(package->name); |
| 252 | if (package->id) { |
| 253 | printer->Print(StringPrintf(" id=%02x", package->id.value())); |
| 254 | } |
| 255 | printer->Println(); |
| 256 | |
| 257 | printer->Indent(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 258 | for (const auto& type : package->types) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 259 | printer->Print("type "); |
| 260 | printer->Print(to_string(type->type)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 261 | if (type->id) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 262 | printer->Print(StringPrintf(" id=%02x", type->id.value())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 263 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 264 | printer->Println(StringPrintf(" entryCount=%zd", type->entries.size())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 265 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | std::vector<const ResourceEntry*> sorted_entries; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 267 | for (const auto& entry : type->entries) { |
| 268 | auto iter = std::lower_bound( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 269 | sorted_entries.begin(), sorted_entries.end(), entry.get(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 270 | [](const ResourceEntry* a, const ResourceEntry* b) -> bool { |
| 271 | if (a->id && b->id) { |
| 272 | return a->id.value() < b->id.value(); |
| 273 | } else if (a->id) { |
| 274 | return true; |
| 275 | } else { |
| 276 | return false; |
| 277 | } |
| 278 | }); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 279 | sorted_entries.insert(iter, entry.get()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 282 | printer->Indent(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 283 | for (const ResourceEntry* entry : sorted_entries) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 284 | const ResourceId id(package->id.value_or_default(0), type->id.value_or_default(0), |
| 285 | entry->id.value_or_default(0)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 286 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 287 | printer->Print("resource "); |
| 288 | printer->Print(id.to_string()); |
| 289 | printer->Print(" "); |
| 290 | |
| 291 | // Write the name without the package (this is obvious and too verbose). |
| 292 | printer->Print(to_string(type->type)); |
| 293 | printer->Print("/"); |
| 294 | printer->Print(entry->name); |
| 295 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | switch (entry->symbol_status.state) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 297 | case SymbolState::kPublic: |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 298 | printer->Print(" PUBLIC"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 299 | break; |
| 300 | case SymbolState::kPrivate: |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 301 | printer->Print(" _PRIVATE_"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 302 | break; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 303 | case SymbolState::kUndefined: |
| 304 | // Print nothing. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 305 | break; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 306 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 307 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 308 | printer->Println(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 309 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 310 | printer->Indent(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 311 | for (const auto& value : entry->values) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 312 | printer->Print("("); |
| 313 | printer->Print(value->config.to_string()); |
| 314 | printer->Print(") "); |
| 315 | value->value->Accept(&headline_printer); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 316 | if (options.show_sources && !value->value->GetSource().path.empty()) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 317 | printer->Print(" src="); |
| 318 | printer->Print(value->value->GetSource().to_string()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 319 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 320 | printer->Println(); |
| 321 | printer->Indent(); |
| 322 | value->value->Accept(&body_printer); |
| 323 | printer->Undent(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 324 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 325 | printer->Undent(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 326 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 327 | printer->Undent(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 328 | } |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 329 | printer->Undent(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 330 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 333 | static size_t GetNodeIndex(const std::vector<ResourceName>& names, const ResourceName& name) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 334 | auto iter = std::lower_bound(names.begin(), names.end(), name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 335 | CHECK(iter != names.end()); |
| 336 | CHECK(*iter == name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 337 | return std::distance(names.begin(), iter); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 340 | void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_style) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 341 | std::map<ResourceName, std::set<ResourceName>> graph; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 342 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 343 | std::queue<ResourceName> styles_to_visit; |
| 344 | styles_to_visit.push(target_style); |
| 345 | for (; !styles_to_visit.empty(); styles_to_visit.pop()) { |
| 346 | const ResourceName& style_name = styles_to_visit.front(); |
| 347 | std::set<ResourceName>& parents = graph[style_name]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 348 | if (!parents.empty()) { |
| 349 | // We've already visited this style. |
| 350 | continue; |
| 351 | } |
| 352 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 353 | Maybe<ResourceTable::SearchResult> result = table->FindResource(style_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 354 | if (result) { |
| 355 | ResourceEntry* entry = result.value().entry; |
| 356 | for (const auto& value : entry->values) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 357 | if (Style* style = ValueCast<Style>(value->value.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 358 | if (style->parent && style->parent.value().name) { |
| 359 | parents.insert(style->parent.value().name.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 360 | styles_to_visit.push(style->parent.value().name.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 361 | } |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 362 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 363 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 364 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 365 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 366 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 367 | std::vector<ResourceName> names; |
| 368 | for (const auto& entry : graph) { |
| 369 | names.push_back(entry.first); |
| 370 | } |
| 371 | |
| 372 | std::cout << "digraph styles {\n"; |
| 373 | for (const auto& name : names) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 374 | std::cout << " node_" << GetNodeIndex(names, name) << " [label=\"" << name << "\"];\n"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | for (const auto& entry : graph) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 378 | const ResourceName& style_name = entry.first; |
| 379 | size_t style_node_index = GetNodeIndex(names, style_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 380 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 381 | for (const auto& parent_name : entry.second) { |
| 382 | std::cout << " node_" << style_node_index << " -> " |
| 383 | << "node_" << GetNodeIndex(names, parent_name) << ";\n"; |
Adam Lesinski | d13fb24 | 2015-05-12 20:40:48 -0700 | [diff] [blame] | 384 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 385 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 386 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 387 | std::cout << "}" << std::endl; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 390 | void Debug::DumpHex(const void* data, size_t len) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 391 | const uint8_t* d = (const uint8_t*)data; |
| 392 | for (size_t i = 0; i < len; i++) { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 393 | std::cerr << std::hex << std::setfill('0') << std::setw(2) << (uint32_t)d[i] << " "; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 394 | if (i % 8 == 7) { |
| 395 | std::cerr << "\n"; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 396 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 397 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 398 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 399 | if (len - 1 % 8 != 7) { |
| 400 | std::cerr << std::endl; |
| 401 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 404 | namespace { |
| 405 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 406 | class XmlPrinter : public xml::ConstVisitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 407 | public: |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 408 | using xml::ConstVisitor::Visit; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 409 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 410 | void Visit(const xml::Element* el) override { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 411 | const size_t previous_size = prefix_.size(); |
| 412 | |
| 413 | for (const xml::NamespaceDecl& decl : el->namespace_decls) { |
| 414 | std::cerr << prefix_ << "N: " << decl.prefix << "=" << decl.uri |
| 415 | << " (line=" << decl.line_number << ")\n"; |
| 416 | prefix_ += " "; |
| 417 | } |
| 418 | |
| 419 | std::cerr << prefix_ << "E: "; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 420 | if (!el->namespace_uri.empty()) { |
| 421 | std::cerr << el->namespace_uri << ":"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 422 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 423 | std::cerr << el->name << " (line=" << el->line_number << ")\n"; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 424 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 425 | for (const xml::Attribute& attr : el->attributes) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 426 | std::cerr << prefix_ << " A: "; |
| 427 | if (!attr.namespace_uri.empty()) { |
| 428 | std::cerr << attr.namespace_uri << ":"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 429 | } |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 430 | std::cerr << attr.name; |
| 431 | |
| 432 | if (attr.compiled_attribute) { |
| 433 | std::cerr << "(" << attr.compiled_attribute.value().id.value_or_default(ResourceId(0x0)) |
| 434 | << ")"; |
| 435 | } |
Shane Farmer | 6ed4061 | 2017-09-06 10:00:07 -0700 | [diff] [blame] | 436 | std::cerr << "="; |
| 437 | if (attr.compiled_value != nullptr) { |
| 438 | std::cerr << *attr.compiled_value; |
| 439 | } else { |
| 440 | std::cerr << attr.value; |
| 441 | } |
| 442 | std::cerr << "\n"; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 445 | prefix_ += " "; |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 446 | xml::ConstVisitor::Visit(el); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 447 | prefix_.resize(previous_size); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 448 | } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 449 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 450 | void Visit(const xml::Text* text) override { |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 451 | std::cerr << prefix_ << "T: '" << text->text << "'\n"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 455 | std::string prefix_; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 456 | }; |
| 457 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 458 | } // namespace |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 459 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 460 | void Debug::DumpXml(const xml::XmlResource& doc) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 461 | XmlPrinter printer; |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 462 | doc.root->Accept(&printer); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 463 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 464 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 465 | } // namespace aapt |