Loading tools/aapt2/AppInfo.h +6 −7 Original line number Diff line number Diff line Loading @@ -17,11 +17,10 @@ #ifndef AAPT_APP_INFO_H #define AAPT_APP_INFO_H #include <optional> #include <set> #include <string> #include "util/Maybe.h" namespace aapt { // Information relevant to building an app, parsed from the app's AndroidManifest.xml. Loading @@ -30,19 +29,19 @@ struct AppInfo { std::string package; // The app's minimum SDK version, if it is defined. Maybe<int> min_sdk_version; std::optional<int> min_sdk_version; // The app's version code (the lower 32 bits of the long version code), if it is defined. Maybe<uint32_t> version_code; std::optional<uint32_t> version_code; // The app's version code major (the upper 32 bits of the long version code), if it is defined. Maybe<uint32_t> version_code_major; std::optional<uint32_t> version_code_major; // The app's revision code, if it is defined. Maybe<uint32_t> revision_code; std::optional<uint32_t> revision_code; // The app's split name, if it is a split. Maybe<std::string> split_name; std::optional<std::string> split_name; // The split names that this split depends on. std::set<std::string> split_name_dependencies; Loading tools/aapt2/Debug.cpp +3 −5 Original line number Diff line number Diff line Loading @@ -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(" "); Loading Loading @@ -362,7 +361,7 @@ void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_sty continue; } Maybe<ResourceTable::SearchResult> result = table->FindResource(style_name); std::optional<ResourceTable::SearchResult> result = table->FindResource(style_name); if (result) { ResourceEntry* entry = result.value().entry; for (const auto& value : entry->values) { Loading Loading @@ -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("="); Loading tools/aapt2/Main.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -147,7 +147,7 @@ class DaemonCommand : public Command { private: io::FileOutputStream* out_; IDiagnostics* diagnostics_; Maybe<std::string> trace_folder_; std::optional<std::string> trace_folder_; }; } // namespace aapt Loading tools/aapt2/NameMangler.h +1 −2 Original line number Diff line number Diff line Loading @@ -21,7 +21,6 @@ #include <string> #include "Resource.h" #include "util/Maybe.h" namespace aapt { Loading @@ -44,7 +43,7 @@ class NameMangler { public: explicit NameMangler(NameManglerPolicy policy) : policy_(policy) {} Maybe<ResourceName> MangleName(const ResourceName& name) { std::optional<ResourceName> MangleName(const ResourceName& name) { if (policy_.target_package_name == name.package || policy_.packages_to_mangle.count(name.package) == 0) { return {}; Loading tools/aapt2/ResourceParser.cpp +49 −53 Original line number Diff line number Diff line Loading @@ -20,7 +20,8 @@ #include <limits> #include <sstream> #include "android-base/logging.h" #include <android-base/logging.h> #include <idmap2/Policies.h> #include "ResourceTable.h" #include "ResourceUtils.h" Loading @@ -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; Loading Loading @@ -109,8 +108,8 @@ struct ParsedResource { Visibility::Level visibility_level = Visibility::Level::kUndefined; bool staged_api = false; bool allow_new = false; Maybe<OverlayableItem> overlayable_item; Maybe<StagedId> staged_alias; std::optional<OverlayableItem> overlayable_item; std::optional<StagedId> staged_alias; std::string comment; std::unique_ptr<Value> value; Loading Loading @@ -252,7 +251,7 @@ bool ResourceParser::FlattenXmlSubtree( std::string current_text; // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal. Maybe<size_t> untranslatable_start_depth; std::optional<size_t> untranslatable_start_depth; Node root; std::vector<Node*> node_stack; Loading Loading @@ -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 = {}; } Loading Loading @@ -468,7 +467,7 @@ bool ResourceParser::ParseResources(xml::XmlPullParser* parser) { } // Extract the product name if it exists. if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) { if (std::optional<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) { parsed_resource.product = maybe_product.value().to_string(); } Loading Loading @@ -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<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { resource_type = maybe_type.value().to_string(); } else { diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) Loading @@ -568,7 +567,7 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, return false; } if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) { if (std::optional<StringPiece> 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. Loading @@ -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<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { resource_type = maybe_type.value().to_string(); } else { diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) Loading @@ -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<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); if (resource_type == "id") { if (!maybe_name) { Loading Loading @@ -835,10 +834,8 @@ std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub bool ResourceParser::ParseString(xml::XmlPullParser* parser, ParsedResource* out_resource) { bool formatted = true; if (Maybe<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) { Maybe<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value()); if (std::optional<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) { std::optional<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value()); if (!maybe_formatted) { diag_->Error(DiagMessage(out_resource->source) << "invalid value for 'formatted'. Must be a boolean"); Loading @@ -848,8 +845,8 @@ bool ResourceParser::ParseString(xml::XmlPullParser* parser, } bool translatable = options_.translatable; if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); if (!maybe_translatable) { diag_->Error(DiagMessage(out_resource->source) << "invalid value for 'translatable'. Must be a boolean"); Loading Loading @@ -929,7 +926,7 @@ bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out << "ignoring configuration '" << out_resource->config << "' for <public> tag"); } Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); if (!maybe_type) { diag_->Error(DiagMessage(out_resource->source) << "<public> must have a 'type' attribute"); Loading @@ -946,8 +943,8 @@ bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out out_resource->name.type = *parsed_type; if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) { Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); if (std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) { std::optional<ResourceId> 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 <public>"); Loading @@ -974,7 +971,7 @@ bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resou << "> tag"); } Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); if (!maybe_type) { diag->Error(DiagMessage(out_resource->source) << "<" << tag_name << "> must have a 'type' attribute"); Loading @@ -988,14 +985,14 @@ bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resou return false; } Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id"); std::optional<StringPiece> 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<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); std::optional<ResourceId> 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 << ">"); Loading Loading @@ -1090,7 +1087,7 @@ bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* out_resource) { Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); if (!maybe_type) { diag_->Error(DiagMessage(out_resource->source) << "<" << parser->element_name() Loading Loading @@ -1137,7 +1134,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource << "' for <overlayable> tag"); } Maybe<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name"); if (!overlayable_name) { diag_->Error(DiagMessage(out_resource->source) << "<overlayable> tag must have a 'name' attribute"); Loading @@ -1146,7 +1143,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource const std::string kActorUriScheme = android::base::StringPrintf("%s://", Overlayable::kActorScheme); Maybe<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor"); std::optional<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor"); if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) { diag_->Error(DiagMessage(out_resource->source) << "specified <overlayable> tag 'actor' attribute must use the scheme '" Loading Loading @@ -1194,7 +1191,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource } // Items specify the name and type of resource that should be overlayable Maybe<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name"); if (!item_name) { diag_->Error(DiagMessage(element_source) << "<item> within an <overlayable> must have a 'name' attribute"); Loading @@ -1202,7 +1199,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource continue; } Maybe<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type"); std::optional<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type"); if (!item_type) { diag_->Error(DiagMessage(element_source) << "<item> within an <overlayable> must have a 'type' attribute"); Loading Loading @@ -1236,7 +1233,8 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested"); error = true; break; } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { } else if (std::optional<StringPiece> 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(), '|')) { Loading Loading @@ -1302,7 +1300,7 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, uint32_t type_mask = 0; Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format"); std::optional<StringPiece> maybe_format = xml::FindAttribute(parser, "format"); if (maybe_format) { type_mask = ParseFormatAttribute(maybe_format.value()); if (type_mask == 0) { Loading @@ -1312,9 +1310,9 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, } } Maybe<int32_t> maybe_min, maybe_max; std::optional<int32_t> maybe_min, maybe_max; if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) { if (std::optional<StringPiece> 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); Loading @@ -1331,7 +1329,7 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, } } if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) { if (std::optional<StringPiece> 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); Loading Loading @@ -1398,8 +1396,7 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, type_mask |= android::ResTable_map::TYPE_FLAGS; } if (Maybe<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) { if (std::optional<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) { Attribute::Symbol& symbol = s.value(); ParsedResource child_resource; child_resource.name = symbol.symbol.name.value(); Loading Loading @@ -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<Attribute::Symbol>(items.begin(), items.end()); attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min()); attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max()); attr->min_int = maybe_min.value_or(std::numeric_limits<int32_t>::min()); attr->max_int = maybe_max.value_or(std::numeric_limits<int32_t>::max()); out_resource->value = std::move(attr); return true; } Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem( xml::XmlPullParser* parser, const StringPiece& tag) { std::optional<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(xml::XmlPullParser* parser, const StringPiece& tag) { const Source source = source_.WithLine(parser->line_number()); Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); if (!maybe_name) { diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">"); return {}; } Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value"); std::optional<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value"); if (!maybe_value) { diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">"); Loading @@ -1484,13 +1481,13 @@ Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem( bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) { const Source source = source_.WithLine(parser->line_number()); Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); if (!maybe_name) { diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute"); return false; } Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); std::optional<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); if (!maybe_key) { diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'"); return false; Loading @@ -1515,7 +1512,7 @@ bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* par std::unique_ptr<Style> style = util::make_unique<Style>(); Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent"); std::optional<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent"); if (maybe_parent) { // If the parent is empty, we don't have a parent, but we also don't infer either. if (!maybe_parent.value().empty()) { Loading Loading @@ -1571,7 +1568,7 @@ bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* par bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) { uint32_t resource_format = android::ResTable_map::TYPE_ANY; if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) { if (std::optional<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) { resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value()); if (resource_format == 0u) { diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) Loading @@ -1598,8 +1595,8 @@ bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser, std::unique_ptr<Array> array = util::make_unique<Array>(); bool translatable = options_.translatable; if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); if (!maybe_translatable) { diag_->Error(DiagMessage(out_resource->source) << "invalid value for 'translatable'. Must be a boolean"); Loading Loading @@ -1664,8 +1661,7 @@ bool ResourceParser::ParsePlural(xml::XmlPullParser* parser, const std::string& element_namespace = parser->element_namespace(); const std::string& element_name = parser->element_name(); if (element_namespace.empty() && element_name == "item") { Maybe<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity"); std::optional<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity"); if (!maybe_quantity) { diag_->Error(DiagMessage(item_source) << "<item> in <plurals> requires attribute " Loading Loading @@ -1767,7 +1763,7 @@ bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser, const std::string& element_namespace = parser->element_namespace(); const std::string& element_name = parser->element_name(); if (element_namespace.empty() && element_name == "attr") { Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); if (!maybe_name) { diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute"); error = true; Loading @@ -1777,7 +1773,7 @@ bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser, // If this is a declaration, the package name may be in the name. Separate // these out. // Eg. <attr name="android:text" /> Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); std::optional<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); if (!maybe_ref) { diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '" << maybe_name.value() << "'"); Loading Loading
tools/aapt2/AppInfo.h +6 −7 Original line number Diff line number Diff line Loading @@ -17,11 +17,10 @@ #ifndef AAPT_APP_INFO_H #define AAPT_APP_INFO_H #include <optional> #include <set> #include <string> #include "util/Maybe.h" namespace aapt { // Information relevant to building an app, parsed from the app's AndroidManifest.xml. Loading @@ -30,19 +29,19 @@ struct AppInfo { std::string package; // The app's minimum SDK version, if it is defined. Maybe<int> min_sdk_version; std::optional<int> min_sdk_version; // The app's version code (the lower 32 bits of the long version code), if it is defined. Maybe<uint32_t> version_code; std::optional<uint32_t> version_code; // The app's version code major (the upper 32 bits of the long version code), if it is defined. Maybe<uint32_t> version_code_major; std::optional<uint32_t> version_code_major; // The app's revision code, if it is defined. Maybe<uint32_t> revision_code; std::optional<uint32_t> revision_code; // The app's split name, if it is a split. Maybe<std::string> split_name; std::optional<std::string> split_name; // The split names that this split depends on. std::set<std::string> split_name_dependencies; Loading
tools/aapt2/Debug.cpp +3 −5 Original line number Diff line number Diff line Loading @@ -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(" "); Loading Loading @@ -362,7 +361,7 @@ void Debug::PrintStyleGraph(ResourceTable* table, const ResourceName& target_sty continue; } Maybe<ResourceTable::SearchResult> result = table->FindResource(style_name); std::optional<ResourceTable::SearchResult> result = table->FindResource(style_name); if (result) { ResourceEntry* entry = result.value().entry; for (const auto& value : entry->values) { Loading Loading @@ -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("="); Loading
tools/aapt2/Main.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -147,7 +147,7 @@ class DaemonCommand : public Command { private: io::FileOutputStream* out_; IDiagnostics* diagnostics_; Maybe<std::string> trace_folder_; std::optional<std::string> trace_folder_; }; } // namespace aapt Loading
tools/aapt2/NameMangler.h +1 −2 Original line number Diff line number Diff line Loading @@ -21,7 +21,6 @@ #include <string> #include "Resource.h" #include "util/Maybe.h" namespace aapt { Loading @@ -44,7 +43,7 @@ class NameMangler { public: explicit NameMangler(NameManglerPolicy policy) : policy_(policy) {} Maybe<ResourceName> MangleName(const ResourceName& name) { std::optional<ResourceName> MangleName(const ResourceName& name) { if (policy_.target_package_name == name.package || policy_.packages_to_mangle.count(name.package) == 0) { return {}; Loading
tools/aapt2/ResourceParser.cpp +49 −53 Original line number Diff line number Diff line Loading @@ -20,7 +20,8 @@ #include <limits> #include <sstream> #include "android-base/logging.h" #include <android-base/logging.h> #include <idmap2/Policies.h> #include "ResourceTable.h" #include "ResourceUtils.h" Loading @@ -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; Loading Loading @@ -109,8 +108,8 @@ struct ParsedResource { Visibility::Level visibility_level = Visibility::Level::kUndefined; bool staged_api = false; bool allow_new = false; Maybe<OverlayableItem> overlayable_item; Maybe<StagedId> staged_alias; std::optional<OverlayableItem> overlayable_item; std::optional<StagedId> staged_alias; std::string comment; std::unique_ptr<Value> value; Loading Loading @@ -252,7 +251,7 @@ bool ResourceParser::FlattenXmlSubtree( std::string current_text; // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal. Maybe<size_t> untranslatable_start_depth; std::optional<size_t> untranslatable_start_depth; Node root; std::vector<Node*> node_stack; Loading Loading @@ -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 = {}; } Loading Loading @@ -468,7 +467,7 @@ bool ResourceParser::ParseResources(xml::XmlPullParser* parser) { } // Extract the product name if it exists. if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) { if (std::optional<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) { parsed_resource.product = maybe_product.value().to_string(); } Loading Loading @@ -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<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { resource_type = maybe_type.value().to_string(); } else { diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) Loading @@ -568,7 +567,7 @@ bool ResourceParser::ParseResource(xml::XmlPullParser* parser, return false; } if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) { if (std::optional<StringPiece> 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. Loading @@ -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<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { resource_type = maybe_type.value().to_string(); } else { diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) Loading @@ -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<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); if (resource_type == "id") { if (!maybe_name) { Loading Loading @@ -835,10 +834,8 @@ std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub bool ResourceParser::ParseString(xml::XmlPullParser* parser, ParsedResource* out_resource) { bool formatted = true; if (Maybe<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) { Maybe<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value()); if (std::optional<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) { std::optional<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value()); if (!maybe_formatted) { diag_->Error(DiagMessage(out_resource->source) << "invalid value for 'formatted'. Must be a boolean"); Loading @@ -848,8 +845,8 @@ bool ResourceParser::ParseString(xml::XmlPullParser* parser, } bool translatable = options_.translatable; if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); if (!maybe_translatable) { diag_->Error(DiagMessage(out_resource->source) << "invalid value for 'translatable'. Must be a boolean"); Loading Loading @@ -929,7 +926,7 @@ bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out << "ignoring configuration '" << out_resource->config << "' for <public> tag"); } Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); if (!maybe_type) { diag_->Error(DiagMessage(out_resource->source) << "<public> must have a 'type' attribute"); Loading @@ -946,8 +943,8 @@ bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out out_resource->name.type = *parsed_type; if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) { Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); if (std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) { std::optional<ResourceId> 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 <public>"); Loading @@ -974,7 +971,7 @@ bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resou << "> tag"); } Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); if (!maybe_type) { diag->Error(DiagMessage(out_resource->source) << "<" << tag_name << "> must have a 'type' attribute"); Loading @@ -988,14 +985,14 @@ bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resou return false; } Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id"); std::optional<StringPiece> 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<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); std::optional<ResourceId> 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 << ">"); Loading Loading @@ -1090,7 +1087,7 @@ bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* out_resource) { Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); if (!maybe_type) { diag_->Error(DiagMessage(out_resource->source) << "<" << parser->element_name() Loading Loading @@ -1137,7 +1134,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource << "' for <overlayable> tag"); } Maybe<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name"); if (!overlayable_name) { diag_->Error(DiagMessage(out_resource->source) << "<overlayable> tag must have a 'name' attribute"); Loading @@ -1146,7 +1143,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource const std::string kActorUriScheme = android::base::StringPrintf("%s://", Overlayable::kActorScheme); Maybe<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor"); std::optional<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor"); if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) { diag_->Error(DiagMessage(out_resource->source) << "specified <overlayable> tag 'actor' attribute must use the scheme '" Loading Loading @@ -1194,7 +1191,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource } // Items specify the name and type of resource that should be overlayable Maybe<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name"); if (!item_name) { diag_->Error(DiagMessage(element_source) << "<item> within an <overlayable> must have a 'name' attribute"); Loading @@ -1202,7 +1199,7 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource continue; } Maybe<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type"); std::optional<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type"); if (!item_type) { diag_->Error(DiagMessage(element_source) << "<item> within an <overlayable> must have a 'type' attribute"); Loading Loading @@ -1236,7 +1233,8 @@ bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested"); error = true; break; } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { } else if (std::optional<StringPiece> 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(), '|')) { Loading Loading @@ -1302,7 +1300,7 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, uint32_t type_mask = 0; Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format"); std::optional<StringPiece> maybe_format = xml::FindAttribute(parser, "format"); if (maybe_format) { type_mask = ParseFormatAttribute(maybe_format.value()); if (type_mask == 0) { Loading @@ -1312,9 +1310,9 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, } } Maybe<int32_t> maybe_min, maybe_max; std::optional<int32_t> maybe_min, maybe_max; if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) { if (std::optional<StringPiece> 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); Loading @@ -1331,7 +1329,7 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, } } if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) { if (std::optional<StringPiece> 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); Loading Loading @@ -1398,8 +1396,7 @@ bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, type_mask |= android::ResTable_map::TYPE_FLAGS; } if (Maybe<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) { if (std::optional<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) { Attribute::Symbol& symbol = s.value(); ParsedResource child_resource; child_resource.name = symbol.symbol.name.value(); Loading Loading @@ -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<Attribute::Symbol>(items.begin(), items.end()); attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min()); attr->max_int = maybe_max.value_or_default(std::numeric_limits<int32_t>::max()); attr->min_int = maybe_min.value_or(std::numeric_limits<int32_t>::min()); attr->max_int = maybe_max.value_or(std::numeric_limits<int32_t>::max()); out_resource->value = std::move(attr); return true; } Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem( xml::XmlPullParser* parser, const StringPiece& tag) { std::optional<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(xml::XmlPullParser* parser, const StringPiece& tag) { const Source source = source_.WithLine(parser->line_number()); Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); if (!maybe_name) { diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">"); return {}; } Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value"); std::optional<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value"); if (!maybe_value) { diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">"); Loading @@ -1484,13 +1481,13 @@ Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem( bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) { const Source source = source_.WithLine(parser->line_number()); Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); if (!maybe_name) { diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute"); return false; } Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); std::optional<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); if (!maybe_key) { diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'"); return false; Loading @@ -1515,7 +1512,7 @@ bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* par std::unique_ptr<Style> style = util::make_unique<Style>(); Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent"); std::optional<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent"); if (maybe_parent) { // If the parent is empty, we don't have a parent, but we also don't infer either. if (!maybe_parent.value().empty()) { Loading Loading @@ -1571,7 +1568,7 @@ bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* par bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) { uint32_t resource_format = android::ResTable_map::TYPE_ANY; if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) { if (std::optional<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) { resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value()); if (resource_format == 0u) { diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) Loading @@ -1598,8 +1595,8 @@ bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser, std::unique_ptr<Array> array = util::make_unique<Array>(); bool translatable = options_.translatable; if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); if (!maybe_translatable) { diag_->Error(DiagMessage(out_resource->source) << "invalid value for 'translatable'. Must be a boolean"); Loading Loading @@ -1664,8 +1661,7 @@ bool ResourceParser::ParsePlural(xml::XmlPullParser* parser, const std::string& element_namespace = parser->element_namespace(); const std::string& element_name = parser->element_name(); if (element_namespace.empty() && element_name == "item") { Maybe<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity"); std::optional<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity"); if (!maybe_quantity) { diag_->Error(DiagMessage(item_source) << "<item> in <plurals> requires attribute " Loading Loading @@ -1767,7 +1763,7 @@ bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser, const std::string& element_namespace = parser->element_namespace(); const std::string& element_name = parser->element_name(); if (element_namespace.empty() && element_name == "attr") { Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); if (!maybe_name) { diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute"); error = true; Loading @@ -1777,7 +1773,7 @@ bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser, // If this is a declaration, the package name may be in the name. Separate // these out. // Eg. <attr name="android:text" /> Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); std::optional<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); if (!maybe_ref) { diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '" << maybe_name.value() << "'"); Loading