1 /* 2 * Copyright (c) 2023 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 "ecmascript/compiler/debug_info.h" 17 18 namespace panda::ecmascript::kungfu { 19 std::string DebugInfo::FuncDebugInfo::EMPTY_COMMENT = ""; 20 DebugInfo(NativeAreaAllocator * allocator,bool enable)21 DebugInfo::DebugInfo(NativeAreaAllocator* allocator, bool enable) 22 : chunk_(allocator), 23 funcToDInfo_(&chunk_), 24 dInfos_(&chunk_), 25 enable_(enable) 26 { 27 } 28 ~DebugInfo()29 DebugInfo::~DebugInfo() 30 { 31 for (auto info : dInfos_) { 32 if (info != nullptr) { 33 delete info; 34 } 35 } 36 dInfos_.clear(); 37 } 38 AddFuncName(const std::string & name)39 void DebugInfo::AddFuncName(const std::string &name) 40 { 41 ASSERT(enable_); 42 if (dInfos_.size() > 0) { 43 FuncDebugInfo *info = dInfos_.back(); 44 info->SetName(name); 45 ASSERT(funcToDInfo_.find(name) == funcToDInfo_.end()); 46 size_t index = dInfos_.size() - 1; 47 funcToDInfo_[name] = index; 48 } 49 } 50 AddComment(const char * str)51 size_t DebugInfo::AddComment(const char* str) 52 { 53 ASSERT(enable_); 54 ASSERT(dInfos_.size() > 0); 55 FuncDebugInfo *info = dInfos_.back(); 56 size_t index = info->Add(str); 57 return index; 58 } 59 AddFuncDebugInfo(const std::string & name)60 void DebugInfo::AddFuncDebugInfo(const std::string& name) 61 { 62 ASSERT(enable_); 63 FuncDebugInfo *info = new FuncDebugInfo(&chunk_); 64 dInfos_.push_back(info); 65 AddFuncName(name); 66 } 67 GetComment(const std::string & funcName,size_t index) const68 const std::string &DebugInfo::GetComment(const std::string &funcName, size_t index) const 69 { 70 auto it = funcToDInfo_.find(funcName); 71 if (it != funcToDInfo_.end()) { 72 ASSERT(dInfos_.size() > it->second); 73 FuncDebugInfo* info = dInfos_[it->second]; 74 ASSERT(info->Name() == funcName); 75 return info->GetComment(index); 76 } 77 return FuncDebugInfo::EmptyComment(); 78 } 79 } // namespace panda::ecmascript::kungfu 80