1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "module_record.h" 17 #include <algorithm> 18 #include "libpandabase/macros.h" 19 20 namespace panda::defect_scan_aux { SetModuleFilename(std::string_view module_filename)21void ModuleRecord::SetModuleFilename(std::string_view module_filename) 22 { 23 module_filename_ = module_filename; 24 } 25 SetRequestModules(std::vector<std::string> & request_modules)26void ModuleRecord::SetRequestModules(std::vector<std::string> &request_modules) 27 { 28 request_modules_ = request_modules; 29 } 30 AddImportEntry(const ImportEntry & import_entry)31void ModuleRecord::AddImportEntry(const ImportEntry &import_entry) 32 { 33 import_entries_.push_back(import_entry); 34 } 35 AddExportEntry(const ExportEntry & export_entry)36void ModuleRecord::AddExportEntry(const ExportEntry &export_entry) 37 { 38 export_entries_.push_back(export_entry); 39 } 40 SetRegularImportNum(size_t regular_import_num)41void ModuleRecord::SetRegularImportNum(size_t regular_import_num) 42 { 43 regular_import_num_ = regular_import_num; 44 } 45 SetLocalExportNum(size_t local_export_num)46void ModuleRecord::SetLocalExportNum(size_t local_export_num) 47 { 48 local_export_num_ = local_export_num; 49 } 50 GetRegularImportNum() const51size_t ModuleRecord::GetRegularImportNum() const 52 { 53 return regular_import_num_; 54 } 55 GetLocalExportNum() const56size_t ModuleRecord::GetLocalExportNum() const 57 { 58 return local_export_num_; 59 } 60 GetImportInternalNameByIndex(size_t index) const61const std::string &ModuleRecord::GetImportInternalNameByIndex(size_t index) const 62 { 63 ASSERT(index < regular_import_num_); 64 return import_entries_[index].local_name_; 65 } 66 GetImportNamespaceNameByIndex(size_t index) const67const std::string &ModuleRecord::GetImportNamespaceNameByIndex(size_t index) const 68 { 69 ASSERT(index < request_modules_.size()); 70 const std::string &module_name = request_modules_[index]; 71 auto iter = std::find_if(import_entries_.begin() + regular_import_num_, import_entries_.end(), 72 [&](const ImportEntry &entry) { return entry.module_request_ == module_name; }); 73 ASSERT(iter != import_entries_.end()); 74 return iter->local_name_; 75 } 76 GetExportNameByIndex(size_t index) const77const std::string &ModuleRecord::GetExportNameByIndex(size_t index) const 78 { 79 ASSERT(index < local_export_num_); 80 return export_entries_[index].export_name_; 81 } 82 GetInternalNameByExportName(std::string_view export_name) const83std::string ModuleRecord::GetInternalNameByExportName(std::string_view export_name) const 84 { 85 for (auto &export_entry : export_entries_) { 86 if (export_entry.export_name_ == export_name) { 87 return export_entry.local_name_; 88 } 89 } 90 return ""; 91 } 92 GetImportNameByExportName(std::string_view export_name) const93std::string ModuleRecord::GetImportNameByExportName(std::string_view export_name) const 94 { 95 for (auto &export_entry : export_entries_) { 96 if (export_entry.export_name_ == export_name) { 97 return export_entry.import_name_; 98 } 99 } 100 return ""; 101 } 102 GetModuleNameByExportName(std::string_view export_name) const103std::string ModuleRecord::GetModuleNameByExportName(std::string_view export_name) const 104 { 105 for (auto &export_entry : export_entries_) { 106 if (export_entry.export_name_ == export_name) { 107 return export_entry.module_request_; 108 } 109 } 110 return ""; 111 } 112 GetModuleNameByInternalName(std::string_view local_name) const113std::string ModuleRecord::GetModuleNameByInternalName(std::string_view local_name) const 114 { 115 for (auto &import_entry : import_entries_) { 116 if (import_entry.local_name_ == local_name) { 117 return import_entry.module_request_; 118 } 119 } 120 return ""; 121 } 122 GetImportNameByInternalName(std::string_view local_name) const123std::string ModuleRecord::GetImportNameByInternalName(std::string_view local_name) const 124 { 125 for (auto &import_entry : import_entries_) { 126 if (import_entry.local_name_ == local_name) { 127 return import_entry.import_name_; 128 } 129 } 130 return ""; 131 } 132 } // namespace panda::defect_scan_aux