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 #ifndef ECMASCRIPT_COMPILER_DEBUG_INFO_H 17 #define ECMASCRIPT_COMPILER_DEBUG_INFO_H 18 19 #include "ecmascript/mem/chunk_containers.h" 20 21 namespace panda::ecmascript::kungfu { 22 class DebugInfo { 23 public: 24 DebugInfo(NativeAreaAllocator* allocator, bool enable = false); 25 ~DebugInfo(); IsEnable()26 bool IsEnable() const 27 { 28 return enable_; 29 } 30 void AppendComment(size_t index, std::string &&str, std::string_view separator = " | "); 31 size_t AddComment(std::string &&str); 32 void AddFuncDebugInfo(const std::string &name); 33 const std::string &GetComment(const std::string &funcName, size_t index) const; 34 const std::string &GetComment(size_t index) const; 35 36 private: 37 class FuncDebugInfo { 38 public: FuncDebugInfo(Chunk * chunk)39 FuncDebugInfo(Chunk *chunk) : chunk_(chunk), name_("") 40 { 41 comments_ = new ChunkVector<std::string>(chunk_); 42 } 43 ~FuncDebugInfo()44 ~FuncDebugInfo() 45 { 46 if (comments_ != nullptr) { 47 delete comments_; 48 comments_ = nullptr; 49 } 50 } 51 Name()52 const std::string &Name() const 53 { 54 return name_; 55 } 56 SetName(const std::string & n)57 void SetName(const std::string &n) 58 { 59 name_ = n; 60 } 61 Add(std::string && str)62 size_t Add(std::string &&str) 63 { 64 ASSERT(comments_ != nullptr); 65 comments_->push_back(std::move(str)); 66 return comments_->size() - 1; 67 } 68 Append(size_t index,std::string && str,std::string_view separator)69 void Append(size_t index, std::string &&str, std::string_view separator) 70 { 71 ASSERT(comments_ != nullptr); 72 ASSERT(index < comments_->size()); 73 comments_->at(index).append(separator).append(std::move(str)); 74 } 75 GetComment(size_t index)76 const std::string &GetComment(size_t index) 77 { 78 ASSERT(comments_ != nullptr); 79 if (index < comments_->size()) { 80 return comments_->at(index); 81 } 82 return EmptyComment(); 83 } 84 EmptyComment()85 static const std::string &EmptyComment() 86 { 87 return EMPTY_COMMENT; 88 } 89 90 private: 91 static std::string EMPTY_COMMENT; 92 Chunk* chunk_ {nullptr}; 93 std::string name_; 94 ChunkVector<std::string> *comments_ {nullptr}; 95 }; 96 97 void AddFuncName(const std::string &name); 98 99 Chunk chunk_; 100 ChunkMap<std::string, size_t> funcToDInfo_; 101 ChunkVector<FuncDebugInfo*> dInfos_; 102 bool enable_ {false}; 103 }; 104 } // namespace panda::ecmascript::kungfu 105 #endif // ECMASCRIPT_COMPILER_DEBUG_INFO_H 106 107