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/ResourceParser.cpp | 102 ++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 53 deletions(-) (limited to 'tools/aapt2/ResourceParser.cpp') diff --git a/tools/aapt2/ResourceParser.cpp b/tools/aapt2/ResourceParser.cpp index f1e2da9f41e2..f49c25483e3b 100644 --- a/tools/aapt2/ResourceParser.cpp +++ b/tools/aapt2/ResourceParser.cpp @@ -20,7 +20,8 @@ #include #include -#include "android-base/logging.h" +#include +#include #include "ResourceTable.h" #include "ResourceUtils.h" @@ -28,12 +29,10 @@ #include "ValueVisitor.h" #include "text/Utf8Iterator.h" #include "util/ImmutableMap.h" -#include "util/Maybe.h" + #include "util/Util.h" #include "xml/XmlPullParser.h" -#include "idmap2/Policies.h" - using ::aapt::ResourceUtils::StringBuilder; using ::aapt::text::Utf8Iterator; using ::android::ConfigDescription; @@ -109,8 +108,8 @@ struct ParsedResource { Visibility::Level visibility_level = Visibility::Level::kUndefined; bool staged_api = false; bool allow_new = false; - Maybe overlayable_item; - Maybe staged_alias; + std::optional overlayable_item; + std::optional staged_alias; std::string comment; std::unique_ptr value; @@ -252,7 +251,7 @@ bool ResourceParser::FlattenXmlSubtree( std::string current_text; // The first occurrence of a tag. Nested tags are illegal. - Maybe untranslatable_start_depth; + std::optional untranslatable_start_depth; Node root; std::vector node_stack; @@ -342,7 +341,7 @@ bool ResourceParser::FlattenXmlSubtree( } node_stack.pop_back(); - if (untranslatable_start_depth == make_value(depth)) { + if (untranslatable_start_depth == depth) { // This is the end of an untranslatable section. untranslatable_start_depth = {}; } @@ -468,7 +467,7 @@ bool ResourceParser::ParseResources(xml::XmlPullParser* parser) { } // Extract the product name if it exists. - if (Maybe maybe_product = xml::FindNonEmptyAttribute(parser, "product")) { + if (std::optional maybe_product = xml::FindNonEmptyAttribute(parser, "product")) { parsed_resource.product = maybe_product.value().to_string(); } @@ -560,7 +559,7 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, resource_format = android::ResTable_map::TYPE_ANY; // Items have their type encoded in the type attribute. - if (Maybe maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { + if (std::optional maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { resource_type = maybe_type.value().to_string(); } else { diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) @@ -568,7 +567,7 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, return false; } - if (Maybe maybe_format = xml::FindNonEmptyAttribute(parser, "format")) { + if (std::optional maybe_format = xml::FindNonEmptyAttribute(parser, "format")) { // An explicit format for this resource was specified. The resource will // retain its type in its name, but the accepted value for this type is // overridden. @@ -584,7 +583,7 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, can_be_item = false; // Bags have their type encoded in the type attribute. - if (Maybe maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { + if (std::optional maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { resource_type = maybe_type.value().to_string(); } else { diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) @@ -595,7 +594,7 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, // Get the name of the resource. This will be checked later, because not all // XML elements require a name. - Maybe maybe_name = xml::FindNonEmptyAttribute(parser, "name"); + std::optional maybe_name = xml::FindNonEmptyAttribute(parser, "name"); if (resource_type == "id") { if (!maybe_name) { @@ -835,10 +834,8 @@ std::unique_ptr ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub bool ResourceParser::ParseString(xml::XmlPullParser* parser, ParsedResource* out_resource) { bool formatted = true; - if (Maybe formatted_attr = - xml::FindAttribute(parser, "formatted")) { - Maybe maybe_formatted = - ResourceUtils::ParseBool(formatted_attr.value()); + if (std::optional formatted_attr = xml::FindAttribute(parser, "formatted")) { + std::optional maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value()); if (!maybe_formatted) { diag_->Error(DiagMessage(out_resource->source) << "invalid value for 'formatted'. Must be a boolean"); @@ -848,8 +845,8 @@ bool ResourceParser::ParseString(xml::XmlPullParser* parser, } bool translatable = options_.translatable; - if (Maybe translatable_attr = xml::FindAttribute(parser, "translatable")) { - Maybe maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); + if (std::optional translatable_attr = xml::FindAttribute(parser, "translatable")) { + std::optional maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); if (!maybe_translatable) { diag_->Error(DiagMessage(out_resource->source) << "invalid value for 'translatable'. Must be a boolean"); @@ -929,7 +926,7 @@ bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out << "ignoring configuration '" << out_resource->config << "' for tag"); } - Maybe maybe_type = xml::FindNonEmptyAttribute(parser, "type"); + std::optional maybe_type = xml::FindNonEmptyAttribute(parser, "type"); if (!maybe_type) { diag_->Error(DiagMessage(out_resource->source) << " must have a 'type' attribute"); @@ -946,8 +943,8 @@ bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out out_resource->name.type = *parsed_type; - if (Maybe maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) { - Maybe maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); + if (std::optional maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) { + std::optional maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); if (!maybe_id) { diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '" << maybe_id_str.value() << "' in "); @@ -974,7 +971,7 @@ bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resou << "> tag"); } - Maybe maybe_type = xml::FindNonEmptyAttribute(parser, "type"); + std::optional maybe_type = xml::FindNonEmptyAttribute(parser, "type"); if (!maybe_type) { diag->Error(DiagMessage(out_resource->source) << "<" << tag_name << "> must have a 'type' attribute"); @@ -988,14 +985,14 @@ bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resou return false; } - Maybe maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id"); + std::optional maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id"); if (!maybe_id_str) { diag->Error(DiagMessage(out_resource->source) << "<" << tag_name << "> must have a 'first-id' attribute"); return false; } - Maybe maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); + std::optional maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); if (!maybe_id) { diag->Error(DiagMessage(out_resource->source) << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">"); @@ -1090,7 +1087,7 @@ bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* out_resource) { - Maybe maybe_type = xml::FindNonEmptyAttribute(parser, "type"); + std::optional maybe_type = xml::FindNonEmptyAttribute(parser, "type"); if (!maybe_type) { diag_->Error(DiagMessage(out_resource->source) << "<" << parser->element_name() @@ -1137,7 +1134,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource << "' for tag"); } - Maybe overlayable_name = xml::FindNonEmptyAttribute(parser, "name"); + std::optional overlayable_name = xml::FindNonEmptyAttribute(parser, "name"); if (!overlayable_name) { diag_->Error(DiagMessage(out_resource->source) << " tag must have a 'name' attribute"); @@ -1146,7 +1143,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource const std::string kActorUriScheme = android::base::StringPrintf("%s://", Overlayable::kActorScheme); - Maybe overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor"); + std::optional overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor"); if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) { diag_->Error(DiagMessage(out_resource->source) << "specified tag 'actor' attribute must use the scheme '" @@ -1194,7 +1191,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource } // Items specify the name and type of resource that should be overlayable - Maybe item_name = xml::FindNonEmptyAttribute(parser, "name"); + std::optional item_name = xml::FindNonEmptyAttribute(parser, "name"); if (!item_name) { diag_->Error(DiagMessage(element_source) << " within an must have a 'name' attribute"); @@ -1202,7 +1199,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource continue; } - Maybe item_type = xml::FindNonEmptyAttribute(parser, "type"); + std::optional item_type = xml::FindNonEmptyAttribute(parser, "type"); if (!item_type) { diag_->Error(DiagMessage(element_source) << " within an must have a 'type' attribute"); @@ -1236,7 +1233,8 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource diag_->Error(DiagMessage(element_source) << " blocks cannot be recursively nested"); error = true; break; - } else if (Maybe maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { + } else if (std::optional maybe_type = + xml::FindNonEmptyAttribute(parser, "type")) { // Parse the polices separated by vertical bar characters to allow for specifying multiple // policies. Items within the policy tag will have the specified policy. for (const StringPiece& part : util::Tokenize(maybe_type.value(), '|')) { @@ -1302,7 +1300,7 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, uint32_t type_mask = 0; - Maybe maybe_format = xml::FindAttribute(parser, "format"); + std::optional maybe_format = xml::FindAttribute(parser, "format"); if (maybe_format) { type_mask = ParseFormatAttribute(maybe_format.value()); if (type_mask == 0) { @@ -1312,9 +1310,9 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, } } - Maybe maybe_min, maybe_max; + std::optional maybe_min, maybe_max; - if (Maybe maybe_min_str = xml::FindAttribute(parser, "min")) { + if (std::optional maybe_min_str = xml::FindAttribute(parser, "min")) { StringPiece min_str = util::TrimWhitespace(maybe_min_str.value()); if (!min_str.empty()) { std::u16string min_str16 = util::Utf8ToUtf16(min_str); @@ -1331,7 +1329,7 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, } } - if (Maybe maybe_max_str = xml::FindAttribute(parser, "max")) { + if (std::optional maybe_max_str = xml::FindAttribute(parser, "max")) { StringPiece max_str = util::TrimWhitespace(maybe_max_str.value()); if (!max_str.empty()) { std::u16string max_str16 = util::Utf8ToUtf16(max_str); @@ -1398,8 +1396,7 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, type_mask |= android::ResTable_map::TYPE_FLAGS; } - if (Maybe s = - ParseEnumOrFlagItem(parser, element_name)) { + if (std::optional s = ParseEnumOrFlagItem(parser, element_name)) { Attribute::Symbol& symbol = s.value(); ParsedResource child_resource; child_resource.name = symbol.symbol.name.value(); @@ -1443,24 +1440,24 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY}); attr->SetWeak(weak); attr->symbols = std::vector(items.begin(), items.end()); - attr->min_int = maybe_min.value_or_default(std::numeric_limits::min()); - attr->max_int = maybe_max.value_or_default(std::numeric_limits::max()); + attr->min_int = maybe_min.value_or(std::numeric_limits::min()); + attr->max_int = maybe_max.value_or(std::numeric_limits::max()); out_resource->value = std::move(attr); return true; } -Maybe ResourceParser::ParseEnumOrFlagItem( - xml::XmlPullParser* parser, const StringPiece& tag) { +std::optional ResourceParser::ParseEnumOrFlagItem(xml::XmlPullParser* parser, + const StringPiece& tag) { const Source source = source_.WithLine(parser->line_number()); - Maybe maybe_name = xml::FindNonEmptyAttribute(parser, "name"); + std::optional maybe_name = xml::FindNonEmptyAttribute(parser, "name"); if (!maybe_name) { diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">"); return {}; } - Maybe maybe_value = xml::FindNonEmptyAttribute(parser, "value"); + std::optional maybe_value = xml::FindNonEmptyAttribute(parser, "value"); if (!maybe_value) { diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">"); @@ -1484,13 +1481,13 @@ Maybe ResourceParser::ParseEnumOrFlagItem( bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) { const Source source = source_.WithLine(parser->line_number()); - Maybe maybe_name = xml::FindNonEmptyAttribute(parser, "name"); + std::optional maybe_name = xml::FindNonEmptyAttribute(parser, "name"); if (!maybe_name) { diag_->Error(DiagMessage(source) << " must have a 'name' attribute"); return false; } - Maybe maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); + std::optional maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); if (!maybe_key) { diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'"); return false; @@ -1515,7 +1512,7 @@ bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* par std::unique_ptr