From b1a00b33c41c11636534a219816e184f3ab2f0ff Mon Sep 17 00:00:00 2001 From: chenqy930 Date: Sun, 12 Mar 2023 18:25:13 +0800 Subject: [PATCH] Fix gen abc in multi-thread failed Issue:https://gitee.com/open_harmony/dashboard?issue_id=I6LQWC Signed-off-by: chenqy930 Change-Id: Ib5223dd7f0276f2a1776336bb4bd74a071bf4635 --- assembler/assembly-emitter.cpp | 12 +-- assembler/assembly-emitter.h | 2 +- libpandafile/file_item_container.cpp | 24 +++--- libpandafile/file_item_container.h | 21 ++++- libpandafile/file_items.cpp | 37 +++++---- libpandafile/file_items.h | 78 ++++++++++--------- .../tests/file_item_container_test.cpp | 69 ++++++++++++++++ libpandafile/tests/file_items_test.cpp | 2 +- 8 files changed, 173 insertions(+), 72 deletions(-) diff --git a/assembler/assembly-emitter.cpp b/assembler/assembly-emitter.cpp index 60380978c7..9b48a257b3 100644 --- a/assembler/assembly-emitter.cpp +++ b/assembler/assembly-emitter.cpp @@ -218,7 +218,7 @@ ScalarValueItem *AsmEmitter::CreateScalarStringValueItem(ItemContainer *containe { auto *string_item = container->GetOrCreateStringItem(value->GetAsScalar()->GetValue()); if (out != nullptr) { - out->emplace_back(string_item); + out->emplace_back(string_item, container); return &out->back(); } @@ -245,7 +245,7 @@ ScalarValueItem *AsmEmitter::CreateScalarRecordValueItem( } if (out != nullptr) { - out->emplace_back(class_item); + out->emplace_back(class_item, container); return &out->back(); } @@ -268,7 +268,7 @@ ScalarValueItem *AsmEmitter::CreateScalarMethodValueItem( auto *method_item = it->second; if (out != nullptr) { - out->emplace_back(method_item); + out->emplace_back(method_item, container); return &out->back(); } @@ -285,7 +285,7 @@ ScalarValueItem *AsmEmitter::CreateScalarLiteralArrayItem( ASSERT(it != literalarrays.end()); auto *literalarray_item = it->second; if (out != nullptr) { - out->emplace_back(literalarray_item); + out->emplace_back(literalarray_item, container); return &out->back(); } @@ -305,7 +305,7 @@ ScalarValueItem *AsmEmitter::CreateScalarEnumValueItem(ItemContainer *container, auto *field_item = it->second; if (out != nullptr) { - out->emplace_back(field_item); + out->emplace_back(field_item, container); return &out->back(); } @@ -324,7 +324,7 @@ ScalarValueItem *AsmEmitter::CreateScalarAnnotationValueItem( } if (out != nullptr) { - out->emplace_back(annotation_item); + out->emplace_back(annotation_item, container); return &out->back(); } diff --git a/assembler/assembly-emitter.h b/assembler/assembly-emitter.h index e4abd65078..28ddcc94ae 100644 --- a/assembler/assembly-emitter.h +++ b/assembler/assembly-emitter.h @@ -164,7 +164,7 @@ private: static_assert(std::is_arithmetic::value); auto v = value->GetAsScalar()->GetValue(); if (out != nullptr) { - out->emplace_back(v); + out->emplace_back(v, container); return &out->back(); } diff --git a/libpandafile/file_item_container.cpp b/libpandafile/file_item_container.cpp index 4ad2c49d67..25e6675c4b 100644 --- a/libpandafile/file_item_container.cpp +++ b/libpandafile/file_item_container.cpp @@ -174,12 +174,12 @@ ItemContainer::ItemContainer() ClassItem *ItemContainer::GetOrCreateClassItem(const std::string &str) { - return GetOrInsert(class_map_, items_, items_end_, str, false, str); + return GetOrInsert(class_map_, items_, items_end_, str, false, str, this); } ForeignClassItem *ItemContainer::GetOrCreateForeignClassItem(const std::string &str) { - return GetOrInsert(class_map_, foreign_items_, foreign_items_.end(), str, true, str); + return GetOrInsert(class_map_, foreign_items_, foreign_items_.end(), str, true, str, this); } StringItem *ItemContainer::GetOrCreateStringItem(const std::string &str) @@ -189,43 +189,43 @@ StringItem *ItemContainer::GetOrCreateStringItem(const std::string &str) return it->second->GetNameItem(); } - return GetOrInsert(string_map_, items_, items_end_, str, false, str); + return GetOrInsert(string_map_, items_, items_end_, str, false, str, this); } LiteralArrayItem *ItemContainer::GetOrCreateLiteralArrayItem(const std::string &id) { - return GetOrInsert(literalarray_map_, items_, items_end_, id, false); + return GetOrInsert(literalarray_map_, items_, items_end_, id, false, this); } ScalarValueItem *ItemContainer::GetOrCreateIntegerValueItem(uint32_t v) { - return GetOrInsert(int_value_map_, items_, items_end_, v, false, v); + return GetOrInsert(int_value_map_, items_, items_end_, v, false, v, this); } ScalarValueItem *ItemContainer::GetOrCreateLongValueItem(uint64_t v) { - return GetOrInsert(long_value_map_, items_, items_end_, v, false, v); + return GetOrInsert(long_value_map_, items_, items_end_, v, false, v, this); } ScalarValueItem *ItemContainer::GetOrCreateFloatValueItem(float v) { - return GetOrInsert(float_value_map_, items_, items_end_, bit_cast(v), false, v); + return GetOrInsert(float_value_map_, items_, items_end_, bit_cast(v), false, v, this); } ScalarValueItem *ItemContainer::GetOrCreateDoubleValueItem(double v) { - return GetOrInsert(double_value_map_, items_, items_end_, bit_cast(v), false, v); + return GetOrInsert(double_value_map_, items_, items_end_, bit_cast(v), false, v, this); } ScalarValueItem *ItemContainer::GetOrCreateIdValueItem(BaseItem *v) { - return GetOrInsert(id_value_map_, items_, items_end_, v, false, v); + return GetOrInsert(id_value_map_, items_, items_end_, v, false, v, this); } ProtoItem *ItemContainer::GetOrCreateProtoItem(TypeItem *ret_type, const std::vector ¶ms) { ProtoKey key(ret_type, params); - return GetOrInsert(proto_map_, items_, items_end_, key, false, ret_type, params); + return GetOrInsert(proto_map_, items_, items_end_, key, false, ret_type, params, this); } PrimitiveTypeItem *ItemContainer::GetOrCreatePrimitiveTypeItem(Type type) @@ -235,12 +235,12 @@ PrimitiveTypeItem *ItemContainer::GetOrCreatePrimitiveTypeItem(Type type) PrimitiveTypeItem *ItemContainer::GetOrCreatePrimitiveTypeItem(Type::TypeId type) { - return GetOrInsert(primitive_type_map_, items_, items_end_, type, false, type); + return GetOrInsert(primitive_type_map_, items_, items_end_, type, false, type, this); } LineNumberProgramItem *ItemContainer::CreateLineNumberProgramItem() { - auto it = items_.insert(debug_items_end_, std::make_unique()); + auto it = items_.insert(debug_items_end_, std::make_unique(this)); auto *item = static_cast(it->get()); [[maybe_unused]] auto res = line_number_program_index_item_.Add(item); ASSERT(res); diff --git a/libpandafile/file_item_container.h b/libpandafile/file_item_container.h index e39865d695..ee8acc40ec 100644 --- a/libpandafile/file_item_container.h +++ b/libpandafile/file_item_container.h @@ -86,7 +86,15 @@ public: static_assert(!std::is_same_v, "Use ClassItem instance to create MethodItem"); static_assert(!std::is_same_v, "Use ClassItem instance to create FieldItem"); - auto ptr = std::make_unique(std::forward(args)...); + std::unique_ptr ptr = nullptr; + if constexpr (std::is_same_v || std::is_same_v || + std::is_same_v || std::is_same_v || + std::is_same_v) { + ptr = std::make_unique(std::forward(args)..., this); + } else { + ptr = std::make_unique(std::forward(args)...); + } + auto ret = ptr.get(); if (ptr->IsForeign()) { foreign_items_.emplace_back(std::move(ptr)); @@ -186,6 +194,16 @@ public: void DeduplicateDebugInfo(MethodItem *method, ItemDeduper *debug_info_deduper, ItemDeduper *line_number_program_deduper); + size_t GetIndexedItemCount() const + { + return indexed_item_count_; + } + + void IncIndexedItemCount() + { + indexed_item_count_++; + } + private: template auto GetInsertPosition() @@ -551,6 +569,7 @@ private: std::list>::iterator debug_items_end_; BaseItem *end_; + size_t indexed_item_count_ {0}; }; } // namespace panda::panda_file diff --git a/libpandafile/file_items.cpp b/libpandafile/file_items.cpp index e36e5ae2e0..2b88181ac9 100644 --- a/libpandafile/file_items.cpp +++ b/libpandafile/file_items.cpp @@ -14,6 +14,7 @@ */ #include "file_items.h" +#include "file_item_container.h" #include "macros.h" #include "utils/bit_utils.h" #include "utils/leb128.h" @@ -23,8 +24,6 @@ namespace panda::panda_file { -size_t IndexedItem::indexed_item_count_ = 0; - bool IsDynamicLanguage(panda::panda_file::SourceLang lang) { return lang == panda::panda_file::SourceLang::ECMASCRIPT; @@ -182,7 +181,15 @@ std::string BaseItem::GetName() const return ItemTypeToString(GetItemType()); } -StringItem::StringItem(std::string str) : str_(std::move(str)) +IndexedItem::IndexedItem(ItemContainer *container) +{ + if (container != nullptr) { + item_global_index_ = container->GetIndexedItemCount(); + container->IncIndexedItemCount(); + } +} + +StringItem::StringItem(std::string str, ItemContainer *container) : IndexedItem(container), str_(std::move(str)) { str_.push_back(0); utf16_length_ = utf::MUtf8ToUtf16Size(utf::CStringAsMutf8(str_.data())); @@ -196,8 +203,8 @@ StringItem::StringItem(std::string str) : str_(std::move(str)) } } -StringItem::StringItem(File::StringData data) - : str_(reinterpret_cast(data.data)), utf16_length_(data.utf16_length) +StringItem::StringItem(File::StringData data, ItemContainer *container) + : IndexedItem(container), str_(reinterpret_cast(data.data)), utf16_length_(data.utf16_length) { } @@ -485,7 +492,8 @@ bool ParamAnnotationsItem::Write(Writer *writer) return true; } -ProtoItem::ProtoItem(TypeItem *ret_type, const std::vector ¶ms) +ProtoItem::ProtoItem(TypeItem *ret_type, const std::vector ¶ms, ItemContainer *itemContainer) + : IndexedItem(itemContainer) { size_t n = 0; shorty_.push_back(0); @@ -537,8 +545,9 @@ bool ProtoItem::Write(Writer *writer) return true; } -BaseMethodItem::BaseMethodItem(BaseClassItem *cls, StringItem *name, ProtoItem *proto, uint32_t access_flags) - : class_(cls), name_(name), proto_(proto), access_flags_(access_flags) +BaseMethodItem::BaseMethodItem(BaseClassItem *cls, StringItem *name, ProtoItem *proto, uint32_t access_flags, + ItemContainer *container) + : IndexedItem(container), class_(cls), name_(name), proto_(proto), access_flags_(access_flags) { AddIndexDependency(cls); AddIndexDependency(proto); @@ -576,8 +585,8 @@ bool BaseMethodItem::Write(Writer *writer) } MethodItem::MethodItem(ClassItem *cls, StringItem *name, ProtoItem *proto, uint32_t access_flags, - std::vector params) - : BaseMethodItem(cls, name, proto, access_flags), + std::vector params, ItemContainer *container) + : BaseMethodItem(cls, name, proto, access_flags, container), params_(std::move(params)), source_lang_(SourceLang::PANDA_ASSEMBLY), code_(nullptr), @@ -1225,8 +1234,8 @@ bool LiteralArrayItem::Write(Writer *writer) return true; } -BaseFieldItem::BaseFieldItem(BaseClassItem *cls, StringItem *name, TypeItem *type) - : class_(cls), name_(name), type_(type) +BaseFieldItem::BaseFieldItem(BaseClassItem *cls, StringItem *name, TypeItem *type, ItemContainer *container) + : IndexedItem(container), class_(cls), name_(name), type_(type) { AddIndexDependency(cls); AddIndexDependency(type); @@ -1255,8 +1264,8 @@ bool BaseFieldItem::Write(Writer *writer) return writer->Write(name_->GetOffset()); } -FieldItem::FieldItem(ClassItem *cls, StringItem *name, TypeItem *type, uint32_t access_flags) - : BaseFieldItem(cls, name, type), access_flags_(access_flags), value_(nullptr) +FieldItem::FieldItem(ClassItem *cls, StringItem *name, TypeItem *type, uint32_t access_flags, ItemContainer *container) + : BaseFieldItem(cls, name, type, container), access_flags_(access_flags), value_(nullptr) { } diff --git a/libpandafile/file_items.h b/libpandafile/file_items.h index a5b737c460..fe80aa97af 100644 --- a/libpandafile/file_items.h +++ b/libpandafile/file_items.h @@ -157,6 +157,7 @@ enum class IndexType { static constexpr size_t INDEX_COUNT_16 = static_cast(IndexType::LAST_16) + 1; class IndexedItem; +class ItemContainer; class BaseItem { public: @@ -279,10 +280,7 @@ private: class IndexedItem : public BaseItem { public: - IndexedItem() - { - item_index_ = indexed_item_count_++; - } + explicit IndexedItem(ItemContainer *itemContainer); uint32_t GetIndex(const BaseItem *item) const { @@ -330,7 +328,7 @@ public: size_t GetIndexedItemCount() const { - return item_index_; + return item_global_index_; } private: @@ -372,15 +370,14 @@ private: std::vector indexes_; size_t ref_count_ {1}; - size_t item_index_ {0}; - static size_t indexed_item_count_; + size_t item_global_index_ {0}; }; class TypeItem : public IndexedItem { public: - explicit TypeItem(Type type) : type_(type) {} + explicit TypeItem(Type type, ItemContainer *container) : IndexedItem(container), type_(type) {} - explicit TypeItem(Type::TypeId type_id) : type_(type_id) {} + explicit TypeItem(Type::TypeId type_id, ItemContainer *container) : IndexedItem(container), type_(type_id) {} ~TypeItem() override = default; @@ -403,9 +400,9 @@ private: class PrimitiveTypeItem : public TypeItem { public: - explicit PrimitiveTypeItem(Type type) : PrimitiveTypeItem(type.GetId()) {} + explicit PrimitiveTypeItem(Type type, ItemContainer *container) : PrimitiveTypeItem(type.GetId(), container) {} - explicit PrimitiveTypeItem(Type::TypeId type_id) : TypeItem(type_id) + explicit PrimitiveTypeItem(Type::TypeId type_id, ItemContainer *container) : TypeItem(type_id, container) { ASSERT(GetType().IsPrimitive()); SetNeedsEmit(false); @@ -435,9 +432,9 @@ public: class StringItem : public IndexedItem { public: - explicit StringItem(std::string str); + explicit StringItem(std::string str, ItemContainer *container); - explicit StringItem(File::StringData data); + explicit StringItem(File::StringData data, ItemContainer *container); ~StringItem() override = default; @@ -498,7 +495,7 @@ public: DEFAULT_COPY_SEMANTIC(BaseFieldItem); protected: - BaseFieldItem(BaseClassItem *cls, StringItem *name, TypeItem *type); + BaseFieldItem(BaseClassItem *cls, StringItem *name, TypeItem *type, ItemContainer *container); size_t CalculateSize() const override; @@ -512,7 +509,7 @@ private: class FieldItem : public BaseFieldItem { public: - FieldItem(ClassItem *cls, StringItem *name, TypeItem *type, uint32_t access_flags); + FieldItem(ClassItem *cls, StringItem *name, TypeItem *type, uint32_t access_flags, ItemContainer *container); ~FieldItem() override = default; @@ -610,6 +607,8 @@ public: static constexpr int32_t LINE_RANGE = 15; static constexpr int32_t LINE_BASE = -4; + explicit LineNumberProgramItem(ItemContainer *container) : IndexedItem(container) {} + void EmitEnd(); void EmitAdvancePc(std::vector *constant_pool, uint32_t value); @@ -773,7 +772,8 @@ public: DEFAULT_COPY_SEMANTIC(BaseMethodItem); protected: - BaseMethodItem(BaseClassItem *cls, StringItem *name, ProtoItem *proto, uint32_t access_flags); + BaseMethodItem(BaseClassItem *cls, StringItem *name, ProtoItem *proto, uint32_t access_flags, + ItemContainer *container); size_t CalculateSize() const override; @@ -854,7 +854,7 @@ class BaseClassItem; class MethodItem : public BaseMethodItem { public: MethodItem(ClassItem *cls, StringItem *name, ProtoItem *proto, uint32_t access_flags, - std::vector params); + std::vector params, ItemContainer *container); ~MethodItem() override = default; @@ -990,7 +990,8 @@ public: } protected: - explicit BaseClassItem(const std::string &name) : TypeItem(Type::TypeId::REFERENCE), name_(name) {} + explicit BaseClassItem(const std::string &name, ItemContainer *container) + : TypeItem(Type::TypeId::REFERENCE, container), name_(name, container) {} ~BaseClassItem() override = default; @@ -1009,12 +1010,13 @@ private: class ClassItem : public BaseClassItem { public: - explicit ClassItem(const std::string &name) - : BaseClassItem(name), + explicit ClassItem(const std::string &name, ItemContainer *container) + : BaseClassItem(name, container), super_class_(nullptr), access_flags_(0), source_lang_(SourceLang::PANDA_ASSEMBLY), - source_file_(nullptr) + source_file_(nullptr), + container_(container) { } @@ -1064,7 +1066,7 @@ public: template FieldItem *AddField(Args... args) { - fields_.emplace_back(std::make_unique(this, std::forward(args)...)); + fields_.emplace_back(std::make_unique(this, std::forward(args)..., container_)); return fields_.back().get(); } @@ -1072,7 +1074,7 @@ public: MethodItem *AddMethod(Args... args) { // insert new method to set ordered by method name - return methods_.insert(std::make_unique(this, std::forward(args)...))->get(); + return methods_.insert(std::make_unique(this, std::forward(args)..., container_))->get(); } void SetSourceFile(StringItem *item) @@ -1170,11 +1172,12 @@ private: StringItem *source_file_; std::vector> fields_; std::multiset, MethodCompByName> methods_; + ItemContainer *container_; }; class ForeignClassItem : public BaseClassItem { public: - explicit ForeignClassItem(const std::string &name) : BaseClassItem(name) {} + explicit ForeignClassItem(const std::string &name, ItemContainer *container) : BaseClassItem(name, container) {} ~ForeignClassItem() override = default; @@ -1194,7 +1197,8 @@ public: class ForeignFieldItem : public BaseFieldItem { public: - ForeignFieldItem(BaseClassItem *cls, StringItem *name, TypeItem *type) : BaseFieldItem(cls, name, type) {} + ForeignFieldItem(BaseClassItem *cls, StringItem *name, TypeItem *type, ItemContainer *container) + : BaseFieldItem(cls, name, type, container) {} ~ForeignFieldItem() override = default; @@ -1214,8 +1218,8 @@ public: class ForeignMethodItem : public BaseMethodItem { public: - ForeignMethodItem(BaseClassItem *cls, StringItem *name, ProtoItem *proto, uint32_t access_flags) - : BaseMethodItem(cls, name, proto, access_flags) + ForeignMethodItem(BaseClassItem *cls, StringItem *name, ProtoItem *proto, uint32_t access_flags, + ItemContainer *container) : BaseMethodItem(cls, name, proto, access_flags, container) { } @@ -1261,7 +1265,7 @@ private: class ProtoItem : public IndexedItem { public: - ProtoItem(TypeItem *ret_type, const std::vector ¶ms); + ProtoItem(TypeItem *ret_type, const std::vector ¶ms, ItemContainer *itemContainer); ~ProtoItem() override = default; @@ -1463,7 +1467,7 @@ class ValueItem : public IndexedItem { public: enum class Type { INTEGER, LONG, FLOAT, DOUBLE, ID, ARRAY }; - explicit ValueItem(Type type) : type_(type) {} + explicit ValueItem(Type type, ItemContainer *container) : IndexedItem(container), type_(type) {} ~ValueItem() override = default; @@ -1500,15 +1504,15 @@ private: class ScalarValueItem : public ValueItem { public: - explicit ScalarValueItem(uint32_t v) : ValueItem(Type::INTEGER), value_(v) {} + explicit ScalarValueItem(uint32_t v, ItemContainer *container) : ValueItem(Type::INTEGER, container), value_(v) {} - explicit ScalarValueItem(uint64_t v) : ValueItem(Type::LONG), value_(v) {} + explicit ScalarValueItem(uint64_t v, ItemContainer *container) : ValueItem(Type::LONG, container), value_(v) {} - explicit ScalarValueItem(float v) : ValueItem(Type::FLOAT), value_(v) {} + explicit ScalarValueItem(float v, ItemContainer *container) : ValueItem(Type::FLOAT, container), value_(v) {} - explicit ScalarValueItem(double v) : ValueItem(Type::DOUBLE), value_(v) {} + explicit ScalarValueItem(double v, ItemContainer *container) : ValueItem(Type::DOUBLE, container), value_(v) {} - explicit ScalarValueItem(BaseItem *v) : ValueItem(Type::ID), value_(v) {} + explicit ScalarValueItem(BaseItem *v, ItemContainer *container) : ValueItem(Type::ID, container), value_(v) {} ~ScalarValueItem() override = default; @@ -1544,8 +1548,8 @@ private: class ArrayValueItem : public ValueItem { public: - ArrayValueItem(panda_file::Type component_type, std::vector items) - : ValueItem(Type::ARRAY), component_type_(component_type), items_(std::move(items)) + ArrayValueItem(panda_file::Type component_type, std::vector items, ItemContainer *container) + : ValueItem(Type::ARRAY, container), component_type_(component_type), items_(std::move(items)) { } @@ -1634,7 +1638,7 @@ private: class LiteralArrayItem : public ValueItem { public: - explicit LiteralArrayItem() : ValueItem(Type::ARRAY) {} + explicit LiteralArrayItem(ItemContainer *container) : ValueItem(Type::ARRAY, container) {} ~LiteralArrayItem() override = default; diff --git a/libpandafile/tests/file_item_container_test.cpp b/libpandafile/tests/file_item_container_test.cpp index 40cbdaa2b1..80e79369d6 100644 --- a/libpandafile/tests/file_item_container_test.cpp +++ b/libpandafile/tests/file_item_container_test.cpp @@ -1059,4 +1059,73 @@ HWTEST(ItemContainer, GettersTest, testing::ext::TestSize.Level0) rclass_item->VisitFields(cb_field); } +HWTEST(ItemContainer, IndexedItemGlobalIndexTest, testing::ext::TestSize.Level0) +{ + ItemContainer container; + EXPECT_EQ(container.GetIndexedItemCount(), 0U); + + // Create foreign class + ForeignClassItem *foreign_class_item = container.GetOrCreateForeignClassItem("foreign_class"); + EXPECT_EQ(foreign_class_item->GetIndexedItemCount(), 0U); + // BaseClassItem will initialize one StringItem member, which will increase the count by 1. + EXPECT_EQ(container.GetIndexedItemCount(), foreign_class_item->GetIndexedItemCount() + 2); + + // Create foreign field + StringItem *foreign_field_name = container.GetOrCreateStringItem("foreign_field"); + PrimitiveTypeItem *foreign_field_type = container.GetOrCreatePrimitiveTypeItem(Type::TypeId::I32); + ForeignFieldItem *foreign_field_item = container.CreateItem(foreign_class_item, + foreign_field_name, foreign_field_type); + EXPECT_EQ(foreign_field_item->GetIndexedItemCount(), 4U); + EXPECT_EQ(container.GetIndexedItemCount(), foreign_field_item->GetIndexedItemCount() + 1); + + // Create foreign method + StringItem *foreign_method_name = container.GetOrCreateStringItem("foreign_method"); + PrimitiveTypeItem *foreign_ret_type = container.GetOrCreatePrimitiveTypeItem(Type::TypeId::VOID); + std::vector foreign_params; + foreign_params.emplace_back(container.GetOrCreatePrimitiveTypeItem(Type::TypeId::I64)); + ProtoItem *foreign_proto_item = container.GetOrCreateProtoItem(foreign_ret_type, foreign_params); + ForeignMethodItem *foreign_method_item = container.CreateItem(foreign_class_item, + foreign_method_name, foreign_proto_item, 0); + EXPECT_EQ(foreign_method_item->GetIndexedItemCount(), 9U); + EXPECT_EQ(container.GetIndexedItemCount(), foreign_method_item->GetIndexedItemCount() + 1); + + // Create class + ClassItem *class_item = container.GetOrCreateClassItem("classA"); + EXPECT_EQ(class_item->GetIndexedItemCount(), 10U); + EXPECT_EQ(container.GetIndexedItemCount(), class_item->GetIndexedItemCount() + 2); + + // Create method + StringItem *method_name = container.GetOrCreateStringItem("a"); + // TypeId::VOID is repeated, count won't increase + PrimitiveTypeItem *ret_type = container.GetOrCreatePrimitiveTypeItem(Type::TypeId::VOID); + std::vector params; + ProtoItem *proto_item = container.GetOrCreateProtoItem(ret_type, params); + MethodItem *method_item = class_item->AddMethod(method_name, proto_item, ACC_PUBLIC | ACC_STATIC, params); + EXPECT_EQ(method_item->GetIndexedItemCount(), 14U); + EXPECT_EQ(container.GetIndexedItemCount(), method_item->GetIndexedItemCount() + 1); + + // Create field + StringItem *field_name = container.GetOrCreateStringItem("field"); + PrimitiveTypeItem *field_type = container.GetOrCreatePrimitiveTypeItem(Type::TypeId::I32); + FieldItem *field_item = class_item->AddField(field_name, field_type, ACC_PUBLIC); + EXPECT_EQ(field_item->GetIndexedItemCount(), 16U); + EXPECT_EQ(container.GetIndexedItemCount(), field_item->GetIndexedItemCount() + 1); + + // Create code, item count is not expected to increase + std::vector instructions {1, 2, 3, 4}; + CodeItem *code_item = container.CreateItem(0, 2, instructions); + method_item->SetCode(code_item); + EXPECT_EQ(container.GetIndexedItemCount(), field_item->GetIndexedItemCount() + 1); + + // Create line number program + LineNumberProgramItem *line_number_program_item = container.CreateLineNumberProgramItem(); + EXPECT_EQ(line_number_program_item->GetIndexedItemCount(), 17U); + EXPECT_EQ(container.GetIndexedItemCount(), line_number_program_item->GetIndexedItemCount() + 1); + + // Create value items + ScalarValueItem *scalarValueItem = container.CreateItem(1.0); + EXPECT_EQ(scalarValueItem->GetIndexedItemCount(), 18U); + EXPECT_EQ(container.GetIndexedItemCount(), scalarValueItem->GetIndexedItemCount() + 1); +} + } // namespace panda::panda_file::test diff --git a/libpandafile/tests/file_items_test.cpp b/libpandafile/tests/file_items_test.cpp index 6942a6db4d..4399fcfc5d 100644 --- a/libpandafile/tests/file_items_test.cpp +++ b/libpandafile/tests/file_items_test.cpp @@ -27,7 +27,7 @@ namespace panda::panda_file::test { HWTEST(LineNumberProgramItem, EmitSpecialOpcode, testing::ext::TestSize.Level0) { - LineNumberProgramItem item; + LineNumberProgramItem item(nullptr); constexpr int32_t LINE_MAX_INC = LineNumberProgramItem::LINE_RANGE + LineNumberProgramItem::LINE_BASE - 1; constexpr int32_t LINE_MIN_INC = LineNumberProgramItem::LINE_BASE;