diff --git a/tools/aapt2/AppInfo.h b/tools/aapt2/AppInfo.h index d3ca357b03059..cabbe7ea74460 100644 --- a/tools/aapt2/AppInfo.h +++ b/tools/aapt2/AppInfo.h @@ -17,11 +17,10 @@ #ifndef AAPT_APP_INFO_H #define AAPT_APP_INFO_H +#include #include #include -#include "util/Maybe.h" - namespace aapt { // Information relevant to building an app, parsed from the app's AndroidManifest.xml. @@ -30,19 +29,19 @@ struct AppInfo { std::string package; // The app's minimum SDK version, if it is defined. - Maybe min_sdk_version; + std::optional min_sdk_version; // The app's version code (the lower 32 bits of the long version code), if it is defined. - Maybe version_code; + std::optional version_code; // The app's version code major (the upper 32 bits of the long version code), if it is defined. - Maybe version_code_major; + std::optional version_code_major; // The app's revision code, if it is defined. - Maybe revision_code; + std::optional revision_code; // The app's split name, if it is a split. - Maybe split_name; + std::optional split_name; // The split names that this split depends on. std::set split_name_dependencies; diff --git a/tools/aapt2/Debug.cpp b/tools/aapt2/Debug.cpp index ef3a62f4efcc4..df444ba7b169d 100644 --- a/tools/aapt2/Debug.cpp +++ b/tools/aapt2/Debug.cpp @@ -280,8 +280,7 @@ void Debug::PrintTable(const ResourceTable& table, const DebugPrintTableOptions& printer->Indent(); for (const ResourceTableEntryView& entry : type.entries) { printer->Print("resource "); - printer->Print(ResourceId(package.id.value_or_default(0), type.id.value_or_default(0), - entry.id.value_or_default(0)) + printer->Print(ResourceId(package.id.value_or(0), type.id.value_or(0), entry.id.value_or(0)) .to_string()); printer->Print(" "); @@ -362,7 +361,7 @@ void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_sty continue; } - Maybe result = table->FindResource(style_name); + std::optional result = table->FindResource(style_name); if (result) { ResourceEntry* entry = result.value().entry; for (const auto& value : entry->values) { @@ -482,8 +481,7 @@ class XmlPrinter : public xml::ConstVisitor { if (attr.compiled_attribute) { printer_->Print("("); - printer_->Print( - attr.compiled_attribute.value().id.value_or_default(ResourceId(0)).to_string()); + printer_->Print(attr.compiled_attribute.value().id.value_or(ResourceId(0)).to_string()); printer_->Print(")"); } printer_->Print("="); diff --git a/tools/aapt2/Main.cpp b/tools/aapt2/Main.cpp index 8a43bb4ede356..b249c6c128e13 100644 --- a/tools/aapt2/Main.cpp +++ b/tools/aapt2/Main.cpp @@ -147,7 +147,7 @@ class DaemonCommand : public Command { private: io::FileOutputStream* out_; IDiagnostics* diagnostics_; - Maybe trace_folder_; + std::optional trace_folder_; }; } // namespace aapt diff --git a/tools/aapt2/NameMangler.h b/tools/aapt2/NameMangler.h index f1aad29a5c58a..0b4905253d206 100644 --- a/tools/aapt2/NameMangler.h +++ b/tools/aapt2/NameMangler.h @@ -21,7 +21,6 @@ #include #include "Resource.h" -#include "util/Maybe.h" namespace aapt { @@ -44,7 +43,7 @@ class NameMangler { public: explicit NameMangler(NameManglerPolicy policy) : policy_(policy) {} - Maybe MangleName(const ResourceName& name) { + std::optional MangleName(const ResourceName& name) { if (policy_.target_package_name == name.package || policy_.packages_to_mangle.count(name.package) == 0) { return {}; diff --git a/tools/aapt2/ResourceParser.cpp b/tools/aapt2/ResourceParser.cpp index f1e2da9f41e21..f49c25483e3b1 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