code: Add clang-format target and CI workflow (#82)

* code: Add clang format target, rules and CI workflow

* code: Run clang format on sources
This commit is contained in:
GPUCode 2024-02-23 22:57:57 +02:00 committed by GitHub
parent 32a5ff15bb
commit 6f4c6ae0bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
90 changed files with 2942 additions and 1941 deletions

View file

@ -1,31 +1,32 @@
#include "common/disassembler.h"
#include <fmt/format.h>
#include "common/disassembler.h"
namespace Common {
Disassembler::Disassembler() {
ZydisDecoderInit(&m_decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
ZydisFormatterInit(&m_formatter, ZYDIS_FORMATTER_STYLE_INTEL);
ZydisDecoderInit(&m_decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
ZydisFormatterInit(&m_formatter, ZYDIS_FORMATTER_STYLE_INTEL);
}
Disassembler::~Disassembler() = default;
void Disassembler::printInstruction(void* code, u64 address) {
ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
ZyanStatus status = ZydisDecoderDecodeFull(&m_decoder, code, sizeof(code),
&instruction, operands);
ZydisDecodedInstruction instruction;
ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
ZyanStatus status =
ZydisDecoderDecodeFull(&m_decoder, code, sizeof(code), &instruction, operands);
if (!ZYAN_SUCCESS(status)) {
fmt::print("decode instruction failed at {}\n", fmt::ptr(code));
fmt::print("decode instruction failed at {}\n", fmt::ptr(code));
} else {
printInst(instruction, operands,address);
}
printInst(instruction, operands, address);
}
}
void Disassembler::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, u64 address) {
const int bufLen = 256;
char szBuffer[bufLen];
ZydisFormatterFormatInstruction(&m_formatter, &inst, operands,inst.operand_count_visible,
void Disassembler::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,
u64 address) {
const int bufLen = 256;
char szBuffer[bufLen];
ZydisFormatterFormatInstruction(&m_formatter, &inst, operands, inst.operand_count_visible,
szBuffer, sizeof(szBuffer), address, ZYAN_NULL);
fmt::print("instruction: {}\n", szBuffer);
}

View file

@ -7,15 +7,15 @@ namespace Common {
class Disassembler {
public:
Disassembler();
~Disassembler();
Disassembler();
~Disassembler();
void printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands, u64 address);
void printInstruction(void* code, u64 address);
private:
ZydisDecoder m_decoder;
ZydisFormatter m_formatter;
ZydisFormatter m_formatter;
};
} // namespace Common

View file

@ -4,34 +4,34 @@
#include <ctime>
void Discord::RPC::init() {
DiscordEventHandlers handlers{};
Discord_Initialize("1139939140494971051", &handlers, 1, nullptr);
DiscordEventHandlers handlers{};
Discord_Initialize("1139939140494971051", &handlers, 1, nullptr);
startTimestamp = time(nullptr);
enabled = true;
startTimestamp = time(nullptr);
enabled = true;
}
void Discord::RPC::update(Discord::RPCStatus status, const std::string& game) {
DiscordRichPresence rpc{};
DiscordRichPresence rpc{};
if (status == Discord::RPCStatus::Playing) {
rpc.details = "Playing a game";
rpc.state = game.c_str();
} else {
rpc.details = "Idle";
}
if (status == Discord::RPCStatus::Playing) {
rpc.details = "Playing a game";
rpc.state = game.c_str();
} else {
rpc.details = "Idle";
}
rpc.largeImageKey = "shadps4";
rpc.largeImageText = "ShadPS4 is a PS4 emulator";
rpc.startTimestamp = startTimestamp;
rpc.largeImageKey = "shadps4";
rpc.largeImageText = "ShadPS4 is a PS4 emulator";
rpc.startTimestamp = startTimestamp;
Discord_UpdatePresence(&rpc);
Discord_UpdatePresence(&rpc);
}
void Discord::RPC::stop() {
if (enabled) {
enabled = false;
Discord_ClearPresence();
Discord_Shutdown();
}
if (enabled) {
enabled = false;
Discord_ClearPresence();
Discord_Shutdown();
}
}

View file

@ -1,19 +1,19 @@
#pragma once
#include <discord_rpc.h>
#include <cstdint>
#include <string>
#include <discord_rpc.h>
namespace Discord {
enum class RPCStatus { Idling, Playing };
enum class RPCStatus { Idling, Playing };
class RPC {
std::uint64_t startTimestamp;
bool enabled = false;
class RPC {
std::uint64_t startTimestamp;
bool enabled = false;
public:
void init();
void update(RPCStatus status, const std::string& title);
void stop();
};
} // namespace Discord
public:
void init();
void update(RPCStatus status, const std::string& title);
void stop();
};
} // namespace Discord

View file

@ -24,8 +24,8 @@ bool File::open(const std::string& path, OpenMode mode) {
bool File::close() {
if (!isOpen() || std::fclose(m_file) != 0) [[unlikely]] {
m_file = nullptr;
return false;
m_file = nullptr;
return false;
}
m_file = nullptr;
@ -56,9 +56,9 @@ bool File::seek(s64 offset, SeekMode mode) {
u64 File::tell() const {
if (isOpen()) [[likely]] {
#ifdef _WIN64
return _ftelli64(m_file);
return _ftelli64(m_file);
#else
return ftello64(m_file);
return ftello64(m_file);
#endif
}
@ -69,22 +69,22 @@ u64 File::getFileSize() {
#ifdef _WIN64
const u64 pos = _ftelli64(m_file);
if (_fseeki64(m_file, 0, SEEK_END) != 0) {
return 0;
return 0;
}
const u64 size = _ftelli64(m_file);
if (_fseeki64(m_file, pos, SEEK_SET) != 0) {
return 0;
return 0;
}
#else
const u64 pos = ftello64(m_file);
if (fseeko64(m_file, 0, SEEK_END) != 0) {
return 0;
return 0;
}
const u64 size = ftello64(m_file);
if (fseeko64(m_file, pos, SEEK_SET) != 0) {
return 0;
return 0;
}
#endif
return size;

View file

@ -2,19 +2,15 @@
#include <bit>
#include <cstdio>
#include <string>
#include <span>
#include <string>
#include <vector>
#include "common/types.h"
namespace Common::FS {
enum class OpenMode : u32 {
Read = 0x1,
Write = 0x2,
ReadWrite = Read | Write
};
enum class OpenMode : u32 { Read = 0x1, Write = 0x2, ReadWrite = Read | Write };
enum class SeekMode : u32 {
Set,
@ -23,7 +19,7 @@ enum class SeekMode : u32 {
};
class File {
public:
public:
File();
explicit File(const std::string& path, OpenMode mode = OpenMode::Read);
~File();
@ -52,27 +48,27 @@ class File {
const char* getOpenMode(OpenMode mode) const {
switch (mode) {
case OpenMode::Read:
return "rb";
case OpenMode::Write:
return "wb";
case OpenMode::ReadWrite:
return "r+b";
default:
return "r";
case OpenMode::Read:
return "rb";
case OpenMode::Write:
return "wb";
case OpenMode::ReadWrite:
return "r+b";
default:
return "r";
}
}
int getSeekMode(SeekMode mode) const {
switch (mode) {
case SeekMode::Set:
return SEEK_SET;
case SeekMode::Cur:
return SEEK_CUR;
case SeekMode::End:
return SEEK_END;
default:
return SEEK_SET;
case SeekMode::Set:
return SEEK_SET;
case SeekMode::Cur:
return SEEK_CUR;
case SeekMode::End:
return SEEK_END;
default:
return SEEK_SET;
}
}
@ -80,7 +76,7 @@ class File {
return m_file;
}
private:
private:
std::FILE* m_file{};
};

View file

@ -1,6 +1,6 @@
#include "io_file.h"
//#include "helpers.hpp"
// #include "helpers.hpp"
#ifdef _MSC_VER
// 64 bit offsets for MSVC
@ -118,8 +118,8 @@ FILE* IOFile::getHandle() {
}
void IOFile::setAppDataDir(const std::filesystem::path& dir) {
//if (dir == "")
// Helpers::panic("Failed to set app data directory");
// if (dir == "")
// Helpers::panic("Failed to set app data directory");
appData = dir;
}

View file

@ -5,7 +5,8 @@
class IOFile {
FILE* handle = nullptr;
static inline std::filesystem::path appData =""; // Directory for holding app data. AppData on Windows
static inline std::filesystem::path appData =
""; // Directory for holding app data. AppData on Windows
public:
IOFile() : handle(nullptr) {}

View file

@ -1,10 +1,10 @@
#include <vector>
#include <Util/config.h>
#include <spdlog/common.h>
#include <spdlog/pattern_formatter.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <vector>
#include <Util/config.h>
#include "common/log.h"
#ifdef _WIN64
#include <windows.h>
@ -30,19 +30,20 @@ uint64_t tls_access(int64_t tls_offset) {
#ifdef _WIN64
static LONG WINAPI ExceptionHandler(PEXCEPTION_POINTERS pExp) noexcept {
auto orig_rip = pExp->ContextRecord->Rip;
while (*(uint8_t *)pExp->ContextRecord->Rip == 0x66) pExp->ContextRecord->Rip++;
while (*(uint8_t*)pExp->ContextRecord->Rip == 0x66)
pExp->ContextRecord->Rip++;
if (*(uint8_t *)pExp->ContextRecord->Rip == 0xcd) {
int reg = *(uint8_t *)(pExp->ContextRecord->Rip + 1) - 0x80;
int sizes = *(uint8_t *)(pExp->ContextRecord->Rip + 2);
if (*(uint8_t*)pExp->ContextRecord->Rip == 0xcd) {
int reg = *(uint8_t*)(pExp->ContextRecord->Rip + 1) - 0x80;
int sizes = *(uint8_t*)(pExp->ContextRecord->Rip + 2);
int pattern_size = sizes & 0xF;
int imm_size = sizes >> 4;
int64_t tls_offset;
if (imm_size == 4)
tls_offset = *(int32_t *)(pExp->ContextRecord->Rip + pattern_size);
tls_offset = *(int32_t*)(pExp->ContextRecord->Rip + pattern_size);
else
tls_offset = *(int64_t *)(pExp->ContextRecord->Rip + pattern_size);
tls_offset = *(int64_t*)(pExp->ContextRecord->Rip + pattern_size);
(&pExp->ContextRecord->Rax)[reg] = tls_access(tls_offset); /* TLS_ACCESS */
pExp->ContextRecord->Rip += pattern_size + imm_size;
@ -53,57 +54,61 @@ static LONG WINAPI ExceptionHandler(PEXCEPTION_POINTERS pExp) noexcept {
pExp->ContextRecord->Rip = orig_rip;
const u32 ec = pExp->ExceptionRecord->ExceptionCode;
switch (ec) {
case EXCEPTION_ACCESS_VIOLATION: {
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_ACCESS_VIOLATION ({:#x}). ", ec);
const auto info = pExp->ExceptionRecord->ExceptionInformation;
switch (info[0]) {
case 0:
LOG_CRITICAL_IF(log_file_exceptions, "Read violation at address {:#x}.", info[1]);
break;
case 1:
LOG_CRITICAL_IF(log_file_exceptions, "Write violation at address {:#x}.", info[1]);
break;
case 8:
LOG_CRITICAL_IF(log_file_exceptions, "DEP violation at address {:#x}.", info[1]);
break;
default:
break;
}
case EXCEPTION_ACCESS_VIOLATION: {
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_ACCESS_VIOLATION ({:#x}). ", ec);
const auto info = pExp->ExceptionRecord->ExceptionInformation;
switch (info[0]) {
case 0:
LOG_CRITICAL_IF(log_file_exceptions, "Read violation at address {:#x}.", info[1]);
break;
}
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_ARRAY_BOUNDS_EXCEEDED ({:#x}). ", ec);
case 1:
LOG_CRITICAL_IF(log_file_exceptions, "Write violation at address {:#x}.", info[1]);
break;
case EXCEPTION_DATATYPE_MISALIGNMENT:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_DATATYPE_MISALIGNMENT ({:#x}). ", ec);
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_FLT_DIVIDE_BY_ZERO ({:#x}). ", ec);
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_ILLEGAL_INSTRUCTION ({:#x}). ", ec);
break;
case EXCEPTION_IN_PAGE_ERROR:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_IN_PAGE_ERROR ({:#x}). ", ec);
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_INT_DIVIDE_BY_ZERO ({:#x}). ", ec);
break;
case EXCEPTION_PRIV_INSTRUCTION:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_PRIV_INSTRUCTION ({:#x}). ", ec);
break;
case EXCEPTION_STACK_OVERFLOW:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_STACK_OVERFLOW ({:#x}). ", ec);
case 8:
LOG_CRITICAL_IF(log_file_exceptions, "DEP violation at address {:#x}.", info[1]);
break;
default:
return EXCEPTION_CONTINUE_SEARCH;
break;
}
break;
}
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_ARRAY_BOUNDS_EXCEEDED ({:#x}). ",
ec);
break;
case EXCEPTION_DATATYPE_MISALIGNMENT:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_DATATYPE_MISALIGNMENT ({:#x}). ",
ec);
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_FLT_DIVIDE_BY_ZERO ({:#x}). ",
ec);
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_ILLEGAL_INSTRUCTION ({:#x}). ",
ec);
break;
case EXCEPTION_IN_PAGE_ERROR:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_IN_PAGE_ERROR ({:#x}). ", ec);
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_INT_DIVIDE_BY_ZERO ({:#x}). ",
ec);
break;
case EXCEPTION_PRIV_INSTRUCTION:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_PRIV_INSTRUCTION ({:#x}). ", ec);
break;
case EXCEPTION_STACK_OVERFLOW:
LOG_CRITICAL_IF(log_file_exceptions, "Exception EXCEPTION_STACK_OVERFLOW ({:#x}). ", ec);
break;
default:
return EXCEPTION_CONTINUE_SEARCH;
}
Flush();
return EXCEPTION_CONTINUE_SEARCH;
}
#endif
int Init(bool use_stdout) {
sinks.clear();
if (use_stdout) {
@ -114,8 +119,10 @@ int Init(bool use_stdout) {
#else
sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>("shadps4.txt", true));
#endif
spdlog::set_default_logger(std::make_shared<spdlog::logger>("shadps4 logger", begin(sinks), end(sinks)));
auto f = std::make_unique<spdlog::pattern_formatter>("%^|%L|: %v%$", spdlog::pattern_time_type::local, std::string("")); // disable eol
spdlog::set_default_logger(
std::make_shared<spdlog::logger>("shadps4 logger", begin(sinks), end(sinks)));
auto f = std::make_unique<spdlog::pattern_formatter>(
"%^|%L|: %v%$", spdlog::pattern_time_type::local, std::string("")); // disable eol
spdlog::set_formatter(std::move(f));
spdlog::set_level(static_cast<spdlog::level::level_enum>(Config::getLogLevel()));
@ -129,13 +136,14 @@ int Init(bool use_stdout) {
old_terminate = std::set_terminate([]() {
try {
std::rethrow_exception(std::current_exception());
} catch (const std::exception &e) {
} catch (const std::exception& e) {
LOG_CRITICAL_IF(log_file_exceptions, "Unhandled C++ exception. {}", e.what());
} catch (...) {
LOG_CRITICAL_IF(log_file_exceptions, "Unhandled C++ exception. UNKNOWN");
}
Flush();
if (old_terminate) old_terminate();
if (old_terminate)
old_terminate();
});
return 0;
@ -145,4 +153,4 @@ void SetLevel(spdlog::level::level_enum log_level) {
spdlog::set_level(log_level);
}
} // namespace Common::Log
} // namespace Common::Log

View file

@ -13,18 +13,24 @@ namespace Common::Log {
#define LOG_ERROR SPDLOG_ERROR
#define LOG_CRITICAL SPDLOG_CRITICAL
#define LOG_TRACE_IF(flag, ...) \
if (flag) LOG_TRACE(__VA_ARGS__)
#define LOG_DEBUG_IF(flag, ...) \
if (flag) LOG_DEBUG(__VA_ARGS__)
#define LOG_INFO_IF(flag, ...) \
if (flag) LOG_INFO(__VA_ARGS__)
#define LOG_WARN_IF(flag, ...) \
if (flag) LOG_WARN(__VA_ARGS__)
#define LOG_ERROR_IF(flag, ...) \
if (flag) LOG_ERROR(__VA_ARGS__)
#define LOG_CRITICAL_IF(flag, ...) \
if (flag) LOG_CRITICAL(__VA_ARGS__)
#define LOG_TRACE_IF(flag, ...) \
if (flag) \
LOG_TRACE(__VA_ARGS__)
#define LOG_DEBUG_IF(flag, ...) \
if (flag) \
LOG_DEBUG(__VA_ARGS__)
#define LOG_INFO_IF(flag, ...) \
if (flag) \
LOG_INFO(__VA_ARGS__)
#define LOG_WARN_IF(flag, ...) \
if (flag) \
LOG_WARN(__VA_ARGS__)
#define LOG_ERROR_IF(flag, ...) \
if (flag) \
LOG_ERROR(__VA_ARGS__)
#define LOG_CRITICAL_IF(flag, ...) \
if (flag) \
LOG_CRITICAL(__VA_ARGS__)
int Init(bool use_stdout);

View file

@ -1,9 +1,9 @@
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "common/uint128.h"
#include "common/native_clock.h"
#include "common/rdtsc.h"
#include "common/uint128.h"
#ifdef _WIN64
#include <pthread_time.h>
#else
@ -13,8 +13,8 @@
namespace Common {
NativeClock::NativeClock()
: rdtsc_frequency{EstimateRDTSCFrequency()}, ns_rdtsc_factor{GetFixedPoint64Factor(std::nano::den,
rdtsc_frequency)},
: rdtsc_frequency{EstimateRDTSCFrequency()}, ns_rdtsc_factor{GetFixedPoint64Factor(
std::nano::den, rdtsc_frequency)},
us_rdtsc_factor{GetFixedPoint64Factor(std::micro::den, rdtsc_frequency)},
ms_rdtsc_factor{GetFixedPoint64Factor(std::milli::den, rdtsc_frequency)} {}
@ -40,4 +40,4 @@ u64 NativeClock::GetProcessTimeUS() const {
return ret.tv_nsec / 1000 + ret.tv_sec * 1000000;
}
} // namespace Common::X64
} // namespace Common

View file

@ -28,7 +28,7 @@ static u64 GetTimeNs() {
FILETIME filetime;
GetSystemTimePreciseAsFileTime(&filetime);
return Multiplier * ((static_cast<u64>(filetime.dwHighDateTime) << 32) +
static_cast<u64>(filetime.dwLowDateTime) - WindowsEpochToUnixEpoch);
static_cast<u64>(filetime.dwLowDateTime) - WindowsEpochToUnixEpoch);
#elif defined(__APPLE__)
return clock_gettime_nsec_np(CLOCK_REALTIME);
#else

View file

@ -5,7 +5,7 @@
namespace Common {
std::vector<std::string> SplitString(const std::string &str, char delimiter) {
std::vector<std::string> SplitString(const std::string& str, char delimiter) {
std::istringstream iss(str);
std::vector<std::string> output(1);

View file

@ -1,7 +1,7 @@
#pragma once
#include <vector>
#include <string>
#include <vector>
namespace Common {

View file

@ -22,6 +22,12 @@ static_assert(sizeof(u128) == 16, "u128 must be 128 bits wide");
#define PS4_SYSV_ABI __attribute__((sysv_abi))
// UDLs for memory size values
constexpr unsigned long long operator""_KB(unsigned long long x) { return 1024ULL * x; }
constexpr unsigned long long operator""_MB(unsigned long long x) { return 1024_KB * x; }
constexpr unsigned long long operator""_GB(unsigned long long x) { return 1024_MB * x; }
constexpr unsigned long long operator""_KB(unsigned long long x) {
return 1024ULL * x;
}
constexpr unsigned long long operator""_MB(unsigned long long x) {
return 1024_KB * x;
}
constexpr unsigned long long operator""_GB(unsigned long long x) {
return 1024_MB * x;
}