summaryrefslogtreecommitdiff
path: root/tools/aapt2/ResourceParser.cpp
diff options
context:
space:
mode:
author Ryan Mitchell <rtmitchell@google.com> 2021-07-14 12:53:01 -0700
committer Ryan Mitchell <rtmitchell@google.com> 2021-07-15 13:42:13 -0700
commit4382e44c42d2a1edf92a9ef11dbf34843025e64f (patch)
treef4b2855b1df909e526a298300ee55f79a65ccc79 /tools/aapt2/ResourceParser.cpp
parent3947c9b56c2e79d76b6941fb5bdcdf25e359b0a4 (diff)
Replace Maybe with std::optional
With c++17, std::optional provides the functionality that Maybe provided. Bug: 183215655 Test: aapt2_tests Change-Id: I62bb9c2fa4985dc5217a6ed153df92b85ad9a034
Diffstat (limited to 'tools/aapt2/ResourceParser.cpp')
-rw-r--r--tools/aapt2/ResourceParser.cpp102
1 files changed, 49 insertions, 53 deletions
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 <limits>
#include <sstream>
-#include "android-base/logging.h"
+#include <android-base/logging.h>
+#include <idmap2/Policies.h>
#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<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;
@@ -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;
@@ -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<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();
}
@@ -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()))
@@ -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.
@@ -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()))
@@ -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) {
@@ -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");
@@ -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");
@@ -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");
@@ -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>");
@@ -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");
@@ -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 << ">");
@@ -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()
@@ -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");
@@ -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 '"
@@ -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");
@@ -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");
@@ -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(), '|')) {
@@ -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) {
@@ -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);
@@ -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);
@@ -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();
@@ -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 << ">");
@@ -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;
@@ -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()) {
@@ -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()))
@@ -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");
@@ -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 "
@@ -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;
@@ -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() << "'");