From 4382e44c42d2a1edf92a9ef11dbf34843025e64f Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Wed, 14 Jul 2021 12:53:01 -0700 Subject: Replace Maybe with std::optional With c++17, std::optional provides the functionality that Maybe provided. Bug: 183215655 Test: aapt2_tests Change-Id: I62bb9c2fa4985dc5217a6ed153df92b85ad9a034 --- tools/aapt2/ResourceTable.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'tools/aapt2/ResourceTable.cpp') diff --git a/tools/aapt2/ResourceTable.cpp b/tools/aapt2/ResourceTable.cpp index 8ab1493c6ab3..ad014a23be87 100644 --- a/tools/aapt2/ResourceTable.cpp +++ b/tools/aapt2/ResourceTable.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include "android-base/logging.h" @@ -68,7 +69,7 @@ struct NameEqualRange { template bool less_than_struct_with_name_and_id(const T& lhs, - const std::pair>& rhs) { + const std::pair>& rhs) { if (lhs.id != rhs.second) { return lhs.id < rhs.second; } @@ -341,20 +342,20 @@ struct EntryViewComparer { void InsertEntryIntoTableView(ResourceTableView& table, const ResourceTablePackage* package, const ResourceTableType* type, const std::string& entry_name, - const Maybe& id, const Visibility& visibility, - const Maybe& allow_new, - const Maybe& overlayable_item, - const Maybe& staged_id, + const std::optional& id, const Visibility& visibility, + const std::optional& allow_new, + const std::optional& overlayable_item, + const std::optional& staged_id, const std::vector>& values) { SortedVectorInserter package_inserter; SortedVectorInserter type_inserter; SortedVectorInserter entry_inserter; ResourceTablePackageView new_package{package->name, - id ? id.value().package_id() : Maybe{}}; + id ? id.value().package_id() : std::optional{}}; auto view_package = package_inserter.Insert(table.packages, std::move(new_package)); - ResourceTableTypeView new_type{type->type, id ? id.value().type_id() : Maybe{}}; + ResourceTableTypeView new_type{type->type, id ? id.value().type_id() : std::optional{}}; auto view_type = type_inserter.Insert(view_package->types, std::move(new_type)); if (visibility.level == Visibility::Level::kPublic) { @@ -363,7 +364,7 @@ void InsertEntryIntoTableView(ResourceTableView& table, const ResourceTablePacka } ResourceTableEntryView new_entry{.name = entry_name, - .id = id ? id.value().entry_id() : Maybe{}, + .id = id ? id.value().entry_id() : std::optional{}, .visibility = visibility, .allow_new = allow_new, .overlayable_item = overlayable_item, @@ -585,7 +586,8 @@ bool ResourceTable::AddResource(NewResource&& res, IDiagnostics* diag) { return true; } -Maybe ResourceTable::FindResource(const ResourceNameRef& name) const { +std::optional ResourceTable::FindResource( + const ResourceNameRef& name) const { ResourceTablePackage* package = FindPackage(name.package); if (package == nullptr) { return {}; @@ -603,8 +605,8 @@ Maybe ResourceTable::FindResource(const ResourceNam return SearchResult{package, type, entry}; } -Maybe ResourceTable::FindResource(const ResourceNameRef& name, - ResourceId id) const { +std::optional ResourceTable::FindResource(const ResourceNameRef& name, + ResourceId id) const { ResourceTablePackage* package = FindPackage(name.package); if (package == nullptr) { return {}; -- cgit v1.2.3-59-g8ed1b From 76786b9034dead4075e30f66f97e0c75ac0c5bcd Mon Sep 17 00:00:00 2001 From: Yurii Zubrytskyi Date: Thu, 7 Oct 2021 14:33:55 -0700 Subject: [aapt2] Update the new project tracking for duplicate types When tracking the types that require creating a new package update the map every time we see it, not only when adding one Bug: 201388769 Test: make aapt2_tests && aapt2_tests Change-Id: I3ed5bac1d64c0e7a831f59d58952fe4c34035776 --- tools/aapt2/ResourceTable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools/aapt2/ResourceTable.cpp') diff --git a/tools/aapt2/ResourceTable.cpp b/tools/aapt2/ResourceTable.cpp index ad014a23be87..77f0ef0ed94c 100644 --- a/tools/aapt2/ResourceTable.cpp +++ b/tools/aapt2/ResourceTable.cpp @@ -436,12 +436,12 @@ ResourceTableView ResourceTable::GetPartitionedView(const ResourceTableViewOptio const size_t index = type_index_iter->second; if (new_packages.size() == index) { new_packages.emplace_back(ResourceTablePackageView{package.name, package.id}); - type_new_package_index[type.type] = index + 1; } // Move the type into a new package auto& other_package = new_packages[index]; type_inserter.Insert(other_package.types, std::move(type)); + type_new_package_index[type.type] = index + 1; type_it = package.types.erase(type_it); } } -- cgit v1.2.3-59-g8ed1b From b40f3ab7a144c8b949586f94f3d9e6dc6c731e06 Mon Sep 17 00:00:00 2001 From: Greg Kaiser Date: Mon, 18 Oct 2021 06:37:26 -0700 Subject: [aapt2] Move type usage before std::move() We want to access type.type before we std::move(type). Bug: 201388769 Test: TreeHugger Change-Id: I8167d12a3a203be28c0d8b796f1f70757dfd99b4 --- tools/aapt2/ResourceTable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools/aapt2/ResourceTable.cpp') diff --git a/tools/aapt2/ResourceTable.cpp b/tools/aapt2/ResourceTable.cpp index 77f0ef0ed94c..dae89b0a1de5 100644 --- a/tools/aapt2/ResourceTable.cpp +++ b/tools/aapt2/ResourceTable.cpp @@ -440,8 +440,8 @@ ResourceTableView ResourceTable::GetPartitionedView(const ResourceTableViewOptio // Move the type into a new package auto& other_package = new_packages[index]; - type_inserter.Insert(other_package.types, std::move(type)); type_new_package_index[type.type] = index + 1; + type_inserter.Insert(other_package.types, std::move(type)); type_it = package.types.erase(type_it); } } -- cgit v1.2.3-59-g8ed1b From cff10cec6cb6f406f71d42b064d527549cc0cca8 Mon Sep 17 00:00:00 2001 From: Iurii Makhno Date: Tue, 15 Feb 2022 19:33:50 +0000 Subject: Switch ResourceName to use ResourceNamedType instead of ResourceType. DD: go/custom-resource-types-in-aapt2 Bug: b/215108200 Test: Resource_test.cpp Change-Id: I0b97fc0024523700e01adce788bb934d388da288 --- tools/aapt2/Debug.cpp | 2 +- tools/aapt2/Resource.h | 29 +++++++++--- tools/aapt2/ResourceParser.cpp | 53 +++++++++++++--------- tools/aapt2/ResourceTable.cpp | 8 ++-- tools/aapt2/ResourceUtils.cpp | 30 ++++++------ tools/aapt2/ResourceValues.cpp | 4 +- tools/aapt2/cmd/Link.cpp | 2 +- tools/aapt2/compile/IdAssigner.cpp | 10 ++-- tools/aapt2/compile/XmlIdCollector.cpp | 2 +- tools/aapt2/format/binary/BinaryResourceParser.cpp | 10 ++-- tools/aapt2/java/JavaClassGenerator.cpp | 12 ++--- tools/aapt2/java/ProguardRules.cpp | 4 +- tools/aapt2/link/ReferenceLinker.cpp | 4 +- tools/aapt2/link/TableMerger.cpp | 2 +- tools/aapt2/process/SymbolTable.cpp | 8 ++-- tools/aapt2/process/SymbolTable.h | 2 +- 16 files changed, 104 insertions(+), 78 deletions(-) (limited to 'tools/aapt2/ResourceTable.cpp') diff --git a/tools/aapt2/Debug.cpp b/tools/aapt2/Debug.cpp index 7103944bc12e..f47d66ea5e87 100644 --- a/tools/aapt2/Debug.cpp +++ b/tools/aapt2/Debug.cpp @@ -80,7 +80,7 @@ class ValueHeadlinePrinter : public ConstValueVisitor { printer_->Print(parent_name.package); printer_->Print(":"); } - printer_->Print(to_string(parent_name.type)); + printer_->Print(parent_name.type.to_string()); printer_->Print("/"); printer_->Print(parent_name.entry); if (parent_ref.id) { diff --git a/tools/aapt2/Resource.h b/tools/aapt2/Resource.h index d394b24d9947..b41d8514230b 100644 --- a/tools/aapt2/Resource.h +++ b/tools/aapt2/Resource.h @@ -128,10 +128,12 @@ std::optional ParseResourceNamedType(const android::String */ struct ResourceName { std::string package; - ResourceType type = ResourceType::kRaw; + ResourceNamedType type; std::string entry; ResourceName() = default; + ResourceName(const android::StringPiece& p, const ResourceNamedTypeRef& t, + const android::StringPiece& e); ResourceName(const android::StringPiece& p, ResourceType t, const android::StringPiece& e); int compare(const ResourceName& other) const; @@ -148,13 +150,15 @@ struct ResourceName { */ struct ResourceNameRef { android::StringPiece package; - ResourceType type = ResourceType::kRaw; + ResourceNamedTypeRef type; android::StringPiece entry; ResourceNameRef() = default; ResourceNameRef(const ResourceNameRef&) = default; ResourceNameRef(ResourceNameRef&&) = default; ResourceNameRef(const ResourceName& rhs); // NOLINT(google-explicit-constructor) + ResourceNameRef(const android::StringPiece& p, const ResourceNamedTypeRef& t, + const android::StringPiece& e); ResourceNameRef(const android::StringPiece& p, ResourceType t, const android::StringPiece& e); ResourceNameRef& operator=(const ResourceNameRef& rhs) = default; ResourceNameRef& operator=(ResourceNameRef&& rhs) = default; @@ -418,14 +422,20 @@ inline ::std::ostream& operator<<(::std::ostream& out, const ResourceNamedTypeRe // ResourceName implementation. // +inline ResourceName::ResourceName(const android::StringPiece& p, const ResourceNamedTypeRef& t, + const android::StringPiece& e) + : package(p.to_string()), type(t.ToResourceNamedType()), entry(e.to_string()) { +} + inline ResourceName::ResourceName(const android::StringPiece& p, ResourceType t, const android::StringPiece& e) - : package(p.to_string()), type(t), entry(e.to_string()) {} + : ResourceName(p, ResourceNamedTypeWithDefaultName(t), e) { +} inline int ResourceName::compare(const ResourceName& other) const { int cmp = package.compare(other.package); if (cmp != 0) return cmp; - cmp = static_cast(type) - static_cast(other.type); + cmp = type.compare(other.type); if (cmp != 0) return cmp; cmp = entry.compare(other.entry); return cmp; @@ -461,9 +471,16 @@ inline ::std::ostream& operator<<(::std::ostream& out, const ResourceName& name) inline ResourceNameRef::ResourceNameRef(const ResourceName& rhs) : package(rhs.package), type(rhs.type), entry(rhs.entry) {} +inline ResourceNameRef::ResourceNameRef(const android::StringPiece& p, + const ResourceNamedTypeRef& t, + const android::StringPiece& e) + : package(p), type(t), entry(e) { +} + inline ResourceNameRef::ResourceNameRef(const android::StringPiece& p, ResourceType t, const android::StringPiece& e) - : package(p), type(t), entry(e) {} + : ResourceNameRef(p, ResourceNamedTypeWithDefaultName(t), e) { +} inline ResourceNameRef& ResourceNameRef::operator=(const ResourceName& rhs) { package = rhs.package; @@ -520,7 +537,7 @@ struct hash { size_t operator()(const aapt::ResourceName& name) const { android::hash_t h = 0; h = android::JenkinsHashMix(h, static_cast(hash()(name.package))); - h = android::JenkinsHashMix(h, static_cast(name.type)); + h = android::JenkinsHashMix(h, static_cast(hash()(name.type.name))); h = android::JenkinsHashMix(h, static_cast(hash()(name.entry))); return static_cast(h); } diff --git a/tools/aapt2/ResourceParser.cpp b/tools/aapt2/ResourceParser.cpp index 792a3066f4f6..42715f9c3592 100644 --- a/tools/aapt2/ResourceParser.cpp +++ b/tools/aapt2/ResourceParser.cpp @@ -604,7 +604,8 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, return false; } - out_resource->name.type = ResourceType::kId; + out_resource->name.type = + ResourceNamedTypeWithDefaultName(ResourceType::kId).ToResourceNamedType(); out_resource->name.entry = maybe_name.value().to_string(); // Ids either represent a unique resource id or reference another resource id @@ -623,7 +624,7 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, // A null reference also means there is no inner element when ids are in the form: // out_resource->value = util::make_unique(); - } else if (!ref || ref->name.value().type != ResourceType::kId) { + } else if (!ref || ref->name.value().type.type != ResourceType::kId) { // If an inner element exists, the inner element must be a reference to another resource id diag_->Error(DiagMessage(out_resource->source) << "<" << parser->element_name() @@ -640,7 +641,8 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, return false; } - out_resource->name.type = ResourceType::kMacro; + out_resource->name.type = + ResourceNamedTypeWithDefaultName(ResourceType::kMacro).ToResourceNamedType(); out_resource->name.entry = maybe_name.value().to_string(); return ParseMacro(parser, out_resource); } @@ -656,7 +658,8 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, return false; } - out_resource->name.type = item_iter->second.type; + out_resource->name.type = + ResourceNamedTypeWithDefaultName(item_iter->second.type).ToResourceNamedType(); out_resource->name.entry = maybe_name.value().to_string(); // Only use the implied format of the type when there is no explicit format. @@ -699,7 +702,7 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, if (can_be_item) { // Try parsing the elementName (or type) as a resource. These shall only be // resources like 'layout' or 'xml' and they can only be references. - const ResourceType* parsed_type = ParseResourceType(resource_type); + std::optional parsed_type = ParseResourceNamedType(resource_type); if (parsed_type) { if (!maybe_name) { diag_->Error(DiagMessage(out_resource->source) @@ -708,7 +711,7 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, return false; } - out_resource->name.type = *parsed_type; + out_resource->name.type = parsed_type->ToResourceNamedType(); out_resource->name.entry = maybe_name.value().to_string(); out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString); if (!out_resource->value) { @@ -933,7 +936,7 @@ bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out return false; } - const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); + std::optional parsed_type = ParseResourceNamedType(maybe_type.value()); if (!parsed_type) { diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '" << maybe_type.value() @@ -941,7 +944,7 @@ bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out return false; } - out_resource->name.type = *parsed_type; + out_resource->name.type = parsed_type->ToResourceNamedType(); if (std::optional maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) { std::optional maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); @@ -953,7 +956,7 @@ bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out out_resource->id = maybe_id.value(); } - if (*parsed_type == ResourceType::kId) { + if (parsed_type->type == ResourceType::kId) { // An ID marked as public is also the definition of an ID. out_resource->value = util::make_unique(); } @@ -978,7 +981,7 @@ bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resou return false; } - const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); + std::optional parsed_type = ParseResourceNamedType(maybe_type.value()); if (!parsed_type) { diag->Error(DiagMessage(out_resource->source) << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">"); @@ -1096,7 +1099,7 @@ bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser, return false; } - const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); + std::optional parsed_type = ParseResourceNamedType(maybe_type.value()); if (!parsed_type) { diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '" << maybe_type.value() << "' in <" @@ -1104,7 +1107,7 @@ bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser, return false; } - out_resource->name.type = *parsed_type; + out_resource->name.type = parsed_type->ToResourceNamedType(); return true; } @@ -1208,8 +1211,8 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource continue; } - const ResourceType* type = ParseResourceType(item_type.value()); - if (type == nullptr) { + std::optional type = ParseResourceNamedType(item_type.value()); + if (!type) { diag_->Error(DiagMessage(element_source) << "invalid resource type '" << item_type.value() << "' in within an "); @@ -1223,7 +1226,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource overlayable_item.source = element_source; ParsedResource child_resource{}; - child_resource.name.type = *type; + child_resource.name.type = type->ToResourceNamedType(); child_resource.name.entry = item_name.value().to_string(); child_resource.overlayable_item = overlayable_item; out_resource->child_resources.push_back(std::move(child_resource)); @@ -1289,7 +1292,8 @@ bool ResourceParser::ParseAttr(xml::XmlPullParser* parser, bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, ParsedResource* out_resource, bool weak) { - out_resource->name.type = ResourceType::kAttr; + out_resource->name.type = + ResourceNamedTypeWithDefaultName(ResourceType::kAttr).ToResourceNamedType(); // Attributes only end up in default configuration. if (out_resource->config != ConfigDescription::DefaultConfig()) { @@ -1475,7 +1479,8 @@ std::optional ResourceParser::ParseEnumOrFlagItem(xml::XmlPul } return Attribute::Symbol{ - Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())), + Reference(ResourceNameRef({}, ResourceNamedTypeWithDefaultName(ResourceType::kId), + maybe_name.value())), val.data, val.dataType}; } @@ -1509,7 +1514,7 @@ bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) { bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser, ParsedResource* out_resource) { - out_resource->name.type = type; + out_resource->name.type = ResourceNamedTypeWithDefaultName(type).ToResourceNamedType(); std::unique_ptr