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 18namespace panda::ecmascript::kungfu { 19std::string DebugInfo::FuncDebugInfo::EMPTY_COMMENT = ""; 20 21DebugInfo::DebugInfo(NativeAreaAllocator* allocator, bool enable) 22 : chunk_(allocator), 23 funcToDInfo_(&chunk_), 24 dInfos_(&chunk_), 25 enable_(enable) 26{ 27} 28 29DebugInfo::~DebugInfo() 30{ 31 for (auto info : dInfos_) { 32 if (info != nullptr) { 33 delete info; 34 } 35 } 36 dInfos_.clear(); 37} 38 39void 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 51size_t DebugInfo::AddComment(std::string &&str) 52{ 53 ASSERT(enable_); 54 ASSERT(dInfos_.size() > 0); 55 FuncDebugInfo *info = dInfos_.back(); 56 size_t index = info->Add(std::move(str)); 57 return index; 58} 59 60void DebugInfo::AppendComment(size_t index, std::string &&str, std::string_view separator) 61{ 62 ASSERT(enable_); 63 ASSERT(dInfos_.size() > 0); 64 FuncDebugInfo *info = dInfos_.back(); 65 info->Append(index, std::move(str), separator); 66} 67 68void DebugInfo::AddFuncDebugInfo(const std::string& name) 69{ 70 ASSERT(enable_); 71 FuncDebugInfo *info = new FuncDebugInfo(&chunk_); 72 dInfos_.push_back(info); 73 AddFuncName(name); 74} 75 76const std::string &DebugInfo::GetComment(const std::string &funcName, size_t index) const 77{ 78 auto it = funcToDInfo_.find(funcName); 79 if (it != funcToDInfo_.end()) { 80 ASSERT(dInfos_.size() > it->second); 81 FuncDebugInfo* info = dInfos_[it->second]; 82 ASSERT(info->Name() == funcName); 83 return info->GetComment(index); 84 } 85 return FuncDebugInfo::EmptyComment(); 86} 87 88const std::string &DebugInfo::GetComment(size_t index) const 89{ 90 ASSERT(!dInfos_.empty()); 91 auto *info = dInfos_.back(); 92 return info->GetComment(index); 93} 94} // namespace panda::ecmascript::kungfu 95