!2110 Codecheck fixes

Merge pull request !2110 from Shimenkov Mikhail/codecheck_fix
This commit is contained in:
openharmony_ci 2024-07-23 06:38:55 +00:00 committed by Gitee
commit 94a3aee0d3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
18 changed files with 111 additions and 125 deletions

View File

@ -27,15 +27,13 @@ inline auto BytecodeInst<MODE>::ReadHelper(size_t byteoffset, size_t bytecount,
{
constexpr size_t BYTE_WIDTH = 8;
size_t rightShift = offset % BYTE_WIDTH;
S v = 0;
for (size_t i = 0; i < bytecount; i++) {
S mask = static_cast<S>(ReadByte(byteoffset + i)) << (i * BYTE_WIDTH);
v |= mask;
}
v >>= rightShift;
v >>= offset % BYTE_WIDTH;
size_t leftShift = sizeof(R) * BYTE_WIDTH - width;
// Do sign extension using arithmetic shift. It's implementation defined
@ -48,7 +46,7 @@ inline auto BytecodeInst<MODE>::ReadHelper(size_t byteoffset, size_t bytecount,
}
template <const BytecodeInstMode MODE>
template <size_t OFFSET, size_t WIDTH, bool IS_SIGNED /* = false */>
template <size_t OFFSET, size_t WIDTH, bool IS_SIGNED>
inline auto BytecodeInst<MODE>::Read() const
{
constexpr size_t BYTE_WIDTH = 8;
@ -63,7 +61,7 @@ inline auto BytecodeInst<MODE>::Read() const
}
template <const BytecodeInstMode MODE>
template <bool IS_SIGNED /* = false */>
template <bool IS_SIGNED>
inline auto BytecodeInst<MODE>::Read64(size_t offset, size_t width) const
{
constexpr size_t BIT64 = 64;

View File

@ -163,8 +163,8 @@ inline std::optional<File::EntityId> ClassDataAccessor::GetSourceFileId()
}
template <class Callback, class Accessor>
static void EnumerateClassElements(const File &pf, Span<const uint8_t> sp, size_t elemNum, const Callback &cb,
Span<const uint8_t> *next)
inline void ClassDataAccessor::EnumerateClassElements(const File &pf, Span<const uint8_t> sp, size_t elemNum,
const Callback &cb, Span<const uint8_t> *next)
{
for (size_t i = 0; i < elemNum; i++) {
File::EntityId id = pf.GetIdFromPointer(sp.data());

View File

@ -200,6 +200,10 @@ public:
}
private:
template <class Callback, class Accessor>
void EnumerateClassElements(const File &pf, Span<const uint8_t> sp, size_t elemNum, const Callback &cb,
Span<const uint8_t> *next);
void SkipSourceLang();
void SkipRuntimeAnnotations();

View File

@ -183,61 +183,57 @@ private:
void DebugInfoExtractor::Extract(const File *pf)
{
const auto &pandaFile = *pf;
auto classes = pf->GetClasses();
for (size_t i = 0; i < classes.Size(); i++) {
File::EntityId id(classes[i]);
if (pandaFile.IsExternal(id)) {
if (pf->IsExternal(id)) {
continue;
}
ClassDataAccessor cda(pandaFile, id);
ClassDataAccessor cda(*pf, id);
auto sourceFileId = cda.GetSourceFileId();
cda.EnumerateMethods([this, &pandaFile, &pf, &sourceFileId, &cda](MethodDataAccessor &mda) {
cda.EnumerateMethods([this, pf, &sourceFileId, &cda](MethodDataAccessor &mda) {
auto debugInfoId = mda.GetDebugInfoId();
if (!debugInfoId) {
return;
}
DebugInfoDataAccessor dda(pandaFile, debugInfoId.value());
ProtoDataAccessor pda(pandaFile, mda.GetProtoId());
DebugInfoDataAccessor dda(*pf, debugInfoId.value());
ProtoDataAccessor pda(*pf, mda.GetProtoId());
std::vector<ParamInfo> paramInfo;
size_t idx = 0;
size_t idxRef = pda.GetReturnType().IsReference() ? 1 : 0;
bool firstParam = true;
const char *className = utf::Mutf8AsCString(pf->GetStringData(cda.GetClassId()).data);
dda.EnumerateParameters([&](File::EntityId &paramId) {
ParamInfo info;
if (paramId.IsValid()) {
info.name = utf::Mutf8AsCString(pf->GetStringData(paramId).data);
if (firstParam && !mda.IsStatic()) {
info.signature = className;
if (!paramId.IsValid()) {
return;
}
info.name = utf::Mutf8AsCString(pf->GetStringData(paramId).data);
if (firstParam && !mda.IsStatic()) {
info.signature = utf::Mutf8AsCString(pf->GetStringData(cda.GetClassId()).data);
} else {
Type paramType = pda.GetArgType(idx++);
if (paramType.IsPrimitive()) {
info.signature = Type::GetSignatureByTypeId(paramType);
} else {
Type paramType = pda.GetArgType(idx++);
if (paramType.IsPrimitive()) {
info.signature = Type::GetSignatureByTypeId(paramType);
} else {
auto refType = pda.GetReferenceType(idxRef++);
info.signature = utf::Mutf8AsCString(pf->GetStringData(refType).data);
}
auto refType = pda.GetReferenceType(idxRef++);
info.signature = utf::Mutf8AsCString(pf->GetStringData(refType).data);
}
}
firstParam = false;
paramInfo.emplace_back(info);
});
const uint8_t *program = dda.GetLineNumberProgram();
LineProgramState state(pandaFile, sourceFileId.value_or(File::EntityId(0)), dda.GetLineStart(),
LineProgramState state(*pf, sourceFileId.value_or(File::EntityId(0)), dda.GetLineStart(),
dda.GetConstantPool());
LineNumberProgramHandler handler(&state);
LineNumberProgramProcessor<LineNumberProgramHandler> programProcessor(program, &handler);
LineNumberProgramProcessor<LineNumberProgramHandler> programProcessor(dda.GetLineNumberProgram(), &handler);
programProcessor.Process();
File::EntityId methodId = mda.GetMethodId();

View File

@ -51,7 +51,6 @@ inline std::optional<T> FieldDataAccessor::GetValue()
}
auto v = GetValueInternal();
if (!v.has_value()) {
// NB! This is a workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
// which fails Release builds for GCC 8 and 9.
@ -91,7 +90,6 @@ inline std::optional<File::EntityId> FieldDataAccessor::GetValue()
}
auto v = GetValueInternal();
if (!v.has_value()) {
return {};
}

View File

@ -34,7 +34,6 @@ FieldDataAccessor::FieldDataAccessor(const File &pandaFile, File::EntityId field
nameOff_ = helpers::Read<ID_SIZE>(&sp);
isExternal_ = pandaFile_.IsExternal(fieldId_);
if (!isExternal_) {
accessFlags_ = helpers::ReadULeb128(&sp);
taggedValuesSp_ = sp;

View File

@ -186,6 +186,59 @@ std::unique_ptr<const panda_file::File> HandleArchive(ZipArchiveHandle &handle,
return file;
}
static std::unique_ptr<const panda_file::File> OpenZipPandaFile(FILE *fp, std::string_view location,
std::string_view archiveFilename,
panda_file::File::OpenMode openMode)
{
// Open Zipfile and do the extraction.
ZipArchiveHandle zipfile = nullptr;
auto openError = OpenArchiveFile(zipfile, fp);
if (openError != ZIPARCHIVE_OK) {
LOG(ERROR, PANDAFILE) << "Can't open archive " << location;
return nullptr;
}
bool tryDefault = archiveFilename.empty();
if (!tryDefault) {
if (LocateFile(zipfile, archiveFilename.data()) != ZIPARCHIVE_OK) {
LOG(INFO, PANDAFILE) << "Can't find entry with name '" << archiveFilename << "', will try "
<< ARCHIVE_FILENAME;
tryDefault = true;
}
}
if (tryDefault) {
if (LocateFile(zipfile, ARCHIVE_FILENAME) != ZIPARCHIVE_OK) {
OpenPandaFileFromZipErrorHandler(zipfile);
LOG(ERROR, PANDAFILE) << "Can't find entry with " << ARCHIVE_FILENAME;
fclose(fp);
return nullptr;
}
}
EntryFileStat entry = EntryFileStat();
if (GetCurrentFileInfo(zipfile, &entry) != ZIPARCHIVE_OK) {
OpenPandaFileFromZipErrorHandler(zipfile);
LOG(ERROR, PANDAFILE) << "GetCurrentFileInfo error";
return nullptr;
}
// check that file is not empty, otherwise crash at CloseArchiveFile
if (entry.GetUncompressedSize() == 0) {
OpenPandaFileFromZipErrorHandler(zipfile);
LOG(ERROR, PANDAFILE) << "Invalid panda file '" << (tryDefault ? ARCHIVE_FILENAME : archiveFilename) << "'";
return nullptr;
}
if (OpenCurrentFile(zipfile) != ZIPARCHIVE_OK) {
CloseCurrentFile(zipfile);
OpenPandaFileFromZipErrorHandler(zipfile);
LOG(ERROR, PANDAFILE) << "Can't OpenCurrentFile!";
return nullptr;
}
GetCurrentFileOffset(zipfile, &entry);
auto file = HandleArchive(zipfile, fp, location, entry, archiveFilename, openMode);
CloseCurrentFile(zipfile);
OpenPandaFileFromZipErrorHandler(zipfile);
return file;
}
std::unique_ptr<const panda_file::File> OpenPandaFile(std::string_view location, std::string_view archiveFilename,
panda_file::File::OpenMode openMode)
{
@ -212,52 +265,7 @@ std::unique_ptr<const panda_file::File> OpenPandaFile(std::string_view location,
fseek(fp, 0, SEEK_SET);
std::unique_ptr<const panda_file::File> file;
if (IsZipMagic(magic)) {
// Open Zipfile and do the extraction.
ZipArchiveHandle zipfile = nullptr;
auto openError = OpenArchiveFile(zipfile, fp);
if (openError != ZIPARCHIVE_OK) {
LOG(ERROR, PANDAFILE) << "Can't open archive " << location;
return nullptr;
}
bool tryDefault = archiveFilename.empty();
if (!tryDefault) {
if (LocateFile(zipfile, archiveFilename.data()) != ZIPARCHIVE_OK) {
LOG(INFO, PANDAFILE) << "Can't find entry with name '" << archiveFilename << "', will try "
<< ARCHIVE_FILENAME;
tryDefault = true;
}
}
if (tryDefault) {
if (LocateFile(zipfile, ARCHIVE_FILENAME) != ZIPARCHIVE_OK) {
OpenPandaFileFromZipErrorHandler(zipfile);
LOG(ERROR, PANDAFILE) << "Can't find entry with " << ARCHIVE_FILENAME;
fclose(fp);
return nullptr;
}
}
EntryFileStat entry = EntryFileStat();
if (GetCurrentFileInfo(zipfile, &entry) != ZIPARCHIVE_OK) {
OpenPandaFileFromZipErrorHandler(zipfile);
LOG(ERROR, PANDAFILE) << "GetCurrentFileInfo error";
return nullptr;
}
// check that file is not empty, otherwise crash at CloseArchiveFile
if (entry.GetUncompressedSize() == 0) {
OpenPandaFileFromZipErrorHandler(zipfile);
LOG(ERROR, PANDAFILE) << "Invalid panda file '" << (tryDefault ? ARCHIVE_FILENAME : archiveFilename) << "'";
return nullptr;
}
if (OpenCurrentFile(zipfile) != ZIPARCHIVE_OK) {
CloseCurrentFile(zipfile);
OpenPandaFileFromZipErrorHandler(zipfile);
LOG(ERROR, PANDAFILE) << "Can't OpenCurrentFile!";
return nullptr;
}
GetCurrentFileOffset(zipfile, &entry);
file = HandleArchive(zipfile, fp, location, entry, archiveFilename, openMode);
CloseCurrentFile(zipfile);
OpenPandaFileFromZipErrorHandler(zipfile);
file = OpenZipPandaFile(fp, location, archiveFilename, openMode);
} else {
file = panda_file::File::Open(location, openMode);
}
@ -455,7 +463,6 @@ std::unique_ptr<const File> File::Open(std::string_view filename, OpenMode openM
trace::ScopedTrace scopedTrace("Open panda file " + std::string(filename));
os::file::Mode mode = GetMode(openMode);
os::file::File file = os::file::Open(filename, mode);
if (!file.IsValid()) {
PLOG(ERROR, PANDAFILE) << "Failed to open panda file '" << filename << "'";
return nullptr;
@ -570,7 +577,6 @@ std::unique_ptr<const File> File::OpenFromMemory(os::mem::ConstBytePtr &&ptr, st
File::EntityId File::GetClassId(const uint8_t *mutf8Name) const
{
auto classHashTable = GetClassHashTable();
if (!classHashTable.empty()) {
return GetClassIdFromClassHashTable(mutf8Name);
}

View File

@ -691,11 +691,7 @@ bool ItemContainer::Write(Writer *writer, bool deduplicateItems, bool computeLay
}
for (auto &item : foreignItems_) {
if (!writer->Align(item->Alignment())) {
return false;
}
if (!item->Write(writer)) {
if (!writer->Align(item->Alignment()) || !item->Write(writer)) {
return false;
}
}
@ -705,11 +701,7 @@ bool ItemContainer::Write(Writer *writer, bool deduplicateItems, bool computeLay
continue;
}
if (!writer->Align(item->Alignment())) {
return false;
}
if (!item->Write(writer)) {
if (!writer->Align(item->Alignment()) || !item->Write(writer)) {
return false;
}
}

View File

@ -690,7 +690,6 @@ bool CodeItem::CatchBlock::Write(Writer *writer)
ASSERT(type_ == nullptr || type_->HasIndex(method_));
uint32_t typeOff = type_ != nullptr ? type_->GetIndex(method_) + 1 : 0;
if (!writer->WriteUleb128(typeOff)) {
return false;
}

View File

@ -968,18 +968,15 @@ ClassItem *FileReader::CreateClassItem(File::EntityId classId)
}
auto superClassId = classAcc.GetSuperClassId();
if (superClassId.GetOffset() != 0) {
if (superClassId.GetOffset() == classId.GetOffset()) {
LOG(FATAL, PANDAFILE) << "Class " << className << " has cyclic inheritance";
}
if (file_->IsExternal(superClassId)) {
auto *superClassItem = CreateForeignClassItem(superClassId);
classItem->SetSuperClass(superClassItem);
classItem->SetSuperClass(CreateForeignClassItem(superClassId));
} else {
auto *superClassItem = CreateClassItem(superClassId);
classItem->SetSuperClass(superClassItem);
classItem->SetSuperClass(CreateClassItem(superClassId));
}
}

View File

@ -99,7 +99,6 @@ private:
void SetIntegerFieldValue(FieldDataAccessor *fieldAcc, FieldItem *fieldItem)
{
auto value = fieldAcc->GetValue<T>();
if (!value) {
return;
}
@ -119,7 +118,6 @@ private:
void SetFloatFieldValue(FieldDataAccessor *fieldAcc, FieldItem *fieldItem)
{
auto value = fieldAcc->GetValue<T>();
if (!value) {
return;
}
@ -138,7 +136,6 @@ private:
void SetStringFieldValue(FieldDataAccessor *fieldAcc, FieldItem *fieldItem)
{
auto value = fieldAcc->GetValue<uint32_t>();
if (value) {
panda_file::File::EntityId stringId(value.value());
auto data = file_->GetStringData(stringId);

View File

@ -33,7 +33,6 @@ MethodDataAccessor::MethodDataAccessor(const File &pandaFile, File::EntityId met
accessFlags_ = helpers::ReadULeb128(&sp);
isExternal_ = pandaFile_.IsExternal(methodId);
if (!isExternal_) {
taggedValuesSp_ = sp;
size_ = 0;

View File

@ -24,9 +24,9 @@ std::string ProfileOptimizer::GetNameInfo(const std::unique_ptr<BaseItem> &item)
std::string identity;
if (item->GetName() == CLASS_ITEM) {
identity = static_cast<ClassItem *>(item.get())->GetNameItem()->GetData();
ASSERT(identity.find('L') == 0); // the first character must be 'L'
ASSERT(identity.find(";\0") == identity.length() - 2); // must end with ";\0"
identity = identity.substr(1, identity.length() - 3); // remove 'L' and ";\0"
ASSERT(identity.find('L') == 0); // the first character must be 'L'
ASSERT(identity.find(";\0") == identity.length() - 2U); // must end with ";\0"
identity = identity.substr(1, identity.length() - 3U); // remove 'L' and ";\0"
std::replace(identity.begin(), identity.end(), '/', '.');
} else if (item->GetName() == STRING_ITEM) {
identity = static_cast<StringItem *>(item.get())->GetData();

View File

@ -272,7 +272,7 @@ static Opcode GetOpcode(size_t instSize)
*/
static std::vector<uint8_t> EmitJmpFwdBwd(size_t n1, size_t n2)
{
std::array<std::tuple<size_t, int32_t, int32_t>, 3> jmps {
std::array<std::tuple<size_t, int32_t, int32_t>, 3U> jmps {
std::tuple {globals::IMM_2, std::numeric_limits<int8_t>::min(), std::numeric_limits<int8_t>::max()},
std::tuple {globals::IMM_3, std::numeric_limits<int16_t>::min(), std::numeric_limits<int16_t>::max()},
std::tuple {globals::IMM_5, std::numeric_limits<int32_t>::min(), std::numeric_limits<int32_t>::max()}};

View File

@ -97,7 +97,8 @@ TEST(ItemContainer, TestFileFormatVersionTooOld)
auto writer = FileWriter(fileName);
File::Header header {};
memset(&header, 0, sizeof(header));
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
std::fill(reinterpret_cast<uint8_t *>(&header), reinterpret_cast<uint8_t *>(&header) + sizeof(header), 0);
header.magic = File::MAGIC;
auto old = std::array<uint8_t, File::VERSION_SIZE>(MIN_VERSION);
@ -122,7 +123,8 @@ TEST(ItemContainer, TestFileFormatVersionTooNew)
auto writer = FileWriter(fileName);
File::Header header {};
memset(&header, 0, sizeof(header));
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
std::fill(reinterpret_cast<uint8_t *>(&header), reinterpret_cast<uint8_t *>(&header) + sizeof(header), 0);
header.magic = File::MAGIC;
auto newArr = std::array<uint8_t, File::VERSION_SIZE>(VERSION);
@ -147,7 +149,8 @@ TEST(ItemContainer, TestFileFormatVersionValid)
auto writer = FileWriter(fileName);
File::Header header {};
memset(&header, 0, sizeof(header));
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
std::fill(reinterpret_cast<uint8_t *>(&header), reinterpret_cast<uint8_t *>(&header) + sizeof(header), 0);
header.magic = File::MAGIC;
header.version = {0, 0, 0, 5};
header.fileSize = sizeof(File::Header);
@ -436,7 +439,7 @@ void TestProtos(size_t n)
params.emplace_back(container.GetOrCreatePrimitiveTypeItem(Type::TypeId::I32));
for (size_t i = 0; i < ELEM_PER16 * 2 - 2; i++) {
for (size_t i = 0; i < ELEM_PER16 * 2U - 2U; i++) {
params.emplace_back(container.GetOrCreateClassItem("B"));
types.push_back(Type::TypeId::REFERENCE);
refTypes.push_back(container.GetOrCreateClassItem("B"));

View File

@ -171,17 +171,17 @@ void MutexDestroy(struct fmutex *const m)
bool MutexLock(struct fmutex *const m, bool trylock)
{
if (current_tid == 0) {
current_tid = GET_CURRENT_THREAD;
}
if (m->recursiveMutex) {
if (IsHeld(m, current_tid)) {
m->recursiveCount++;
return true;
}
current_tid = current_tid == 0 ? GET_CURRENT_THREAD : current_tid;
if (m->recursiveMutex && IsHeld(m, current_tid)) {
m->recursiveCount++;
return true;
}
ASSERT(!IsHeld(m, current_tid));
#ifndef NDEBUG
[[maybe_unused]] bool isHeld = IsHeld(m, current_tid);
ASSERT(!isHeld);
#endif
bool done = false;
while (!done) {
// Atomic with relaxed order reason: mutex synchronization
@ -213,11 +213,9 @@ bool MutexLock(struct fmutex *const m, bool trylock)
FutexWait(&m->stateAndWaiters, curState);
#else
// NOLINTNEXTLINE(hicpp-signed-bitwise), NOLINTNEXTLINE(C_RULE_ID_FUNCTION_NESTING_LEVEL)
if (futex(GetStateAddr(m), FUTEX_WAIT_PRIVATE, curState, nullptr, nullptr, 0) != 0) {
// NOLINTNEXTLINE(C_RULE_ID_FUNCTION_NESTING_LEVEL)
if ((errno != EAGAIN) && (errno != EINTR)) {
LOG(FATAL, COMMON) << "Futex wait failed!";
}
if (futex(GetStateAddr(m), FUTEX_WAIT_PRIVATE, curState, nullptr, nullptr, 0) != 0 &&
(errno != EAGAIN) && (errno != EINTR)) {
LOG(FATAL, COMMON) << "Futex wait failed!";
}
#endif
// Atomic with relaxed order reason: mutex synchronization

View File

@ -46,7 +46,7 @@ std::string BuildNumber(int count)
if (0 <= count && count < STACK_FORMAT) {
ostr << "#0" << count;
}
if (STACK_FORMAT <= count) {
if (count >= STACK_FORMAT) {
ostr << "#" << count;
}
return ostr.str();

View File

@ -14,7 +14,7 @@
*/
#include "os/error.h"
#include <string.h>
#include <cstring>
namespace ark::os {