/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "link/ReferenceLinker.h" #include "android-base/logging.h" #include "android-base/stringprintf.h" #include "androidfw/ResourceTypes.h" #include "Diagnostics.h" #include "ResourceParser.h" #include "ResourceTable.h" #include "ResourceUtils.h" #include "ResourceValues.h" #include "ValueVisitor.h" #include "link/Linkers.h" #include "process/IResourceTableConsumer.h" #include "process/SymbolTable.h" #include "trace/TraceBuffer.h" #include "util/Util.h" #include "xml/XmlUtil.h" using ::aapt::ResourceUtils::StringBuilder; using ::android::StringPiece; using ::android::base::StringPrintf; namespace aapt { namespace { struct LoggingResourceName { LoggingResourceName(const Reference& ref, const CallSite& callsite, const xml::IPackageDeclStack* decls) : ref_(ref), callsite_(callsite), decls_(decls) { } const Reference& ref_; const CallSite& callsite_; const xml::IPackageDeclStack* decls_; }; inline ::std::ostream& operator<<(::std::ostream& out, const LoggingResourceName& name) { if (!name.ref_.name) { out << name.ref_.id.value(); return out; } out << name.ref_.name.value(); Reference fully_qualified = name.ref_; xml::ResolvePackage(name.decls_, &fully_qualified); ResourceName& full_name = fully_qualified.name.value(); if (full_name.package.empty()) { full_name.package = name.callsite_.package; } if (full_name != name.ref_.name.value()) { out << " (aka " << full_name << ")"; } return out; } } // namespace std::unique_ptr ReferenceLinkerTransformer::TransformDerived(const Reference* value) { auto linked_item = ReferenceLinker::LinkReference(callsite_, *value, context_, symbols_, table_, package_decls_); if (linked_item) { auto linked_item_ptr = linked_item.release(); if (auto ref = ValueCast(linked_item_ptr)) { return std::unique_ptr(ref); } context_->GetDiagnostics()->Error(DiagMessage(value->GetSource()) << "value of '" << LoggingResourceName(*value, callsite_, package_decls_) << "' must be a resource reference"); delete linked_item_ptr; } error_ = true; return CloningValueTransformer::TransformDerived(value); } std::unique_ptr