Skip to content

Commit

Permalink
refactor: clang-tidy fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
F0bes committed Mar 31, 2024
1 parent 81f3b72 commit b187c92
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 184 deletions.
31 changes: 25 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ project(gifscript CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})

# Using CMAKE_CXX_STANDARD doesn't work for GCC...
# This also doesn't work for Clang 14...
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CPP_23_ARG "-std=c++2b")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CPP_23_ARG "-std=c++23")
endif()

find_package(RAGEL REQUIRED)

Expand All @@ -25,13 +31,25 @@ FetchContent_Declare(fmt
)
FetchContent_MakeAvailable(fmt)

if(NOT WIN32)
find_program(CLANG_TIDY_EXE NAMES "clang-tidy")
if(CLANG_TIDY_EXE AND NOT DISABLE_CLANG_TIDY AND NOT WIN32)
set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=-*,readability-*,modernize-*,performance-*,portability-*,bugprone-*,clang-analyzer-*)
endif()
endif()

RAGEL_TARGET(gifscript
${CMAKE_SOURCE_DIR}/gifscript.rl
${CMAKE_CURRENT_BINARY_DIR}/gifscript.cpp
COMPILE_FLAGS -G2
)

set(GENERATED_SOURCES
parser.h
parser.c
${RAGEL_gifscript_OUTPUTS}
)

set(BACKEND_SOURCES
backend/backend.hpp
backend/c_code.cpp
Expand All @@ -41,16 +59,17 @@ set(BACKEND_SOURCES
set(CORE_SOURCES
version.h
logger.h
parser.h
parser.c
machine.h
machine.cpp
registers.h
registers.cpp
${RAGEL_gifscript_OUTPUTS}
)

add_executable(gifscript ${BACKEND_SOURCES} ${CORE_SOURCES})
add_executable(gifscript ${BACKEND_SOURCES} ${CORE_SOURCES} ${GENERATED_SOURCES})

set_source_files_properties(${GENERATED_SOURCES} PROPERTIES
SKIP_LINTING ON
)

if(WIN32)
target_compile_options(gifscript PRIVATE /std:c++latest)
Expand All @@ -63,7 +82,7 @@ else()
)
string(CONCAT GIT_VERSION "\"" ${GIT_VERSION} "\"")
message("git version: ${GIT_VERSION}")
target_compile_options(gifscript PRIVATE -DGIT_VERSION=${GIT_VERSION} -std=c++23 -Wall -Werror -Wno-unused-const-variable)
target_compile_options(gifscript PRIVATE -DGIT_VERSION=${GIT_VERSION} -Wall -Werror -Wno-unused-const-variable ${CPP_23_ARG})
endif()

target_include_directories(gifscript PRIVATE ${CMAKE_SOURCE_DIR})
Expand Down
102 changes: 46 additions & 56 deletions backend/c_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
#include <functional>
#include "logger.h"


c_code_backend::c_code_backend() noexcept
{
}

bool c_code_backend::arg_parse(int argc, char** argv)
auto c_code_backend::arg_parse(int argc, char** argv) -> bool
{
for(int i = 0; i < argc; i++)
{
Expand Down Expand Up @@ -81,14 +76,15 @@ void c_code_backend::emit(GIFBlock& block)
}
}

const size_t bytes_per_register = 16;
std::string buffer = fmt::format("u64 {1}_data_size = {0};\n"
"u64 {1}_data[] __attribute__((aligned(16))) = {{\n\t"
"GIF_SET_TAG({2},1,{3},{4},0,1),GIF_REG_AD,\n\t",
(block.registers.size() + 1) * 16, block.name, block.registers.size(), block.prim ? 1 : 0, block.prim ? prim_str : "0");
(block.registers.size() + 1) * bytes_per_register, block.name, block.registers.size(), block.prim ? 1 : 0, block.prim ? prim_str : "0");
fmt::print("Emitting block: {}\n", block.name);
for(const auto& reg : block.registers)
{
buffer += dispatch_table[reg->GetID()](this, *reg);
buffer += dispatch_table[static_cast<uint32_t>(reg->GetID())](this, *reg);
buffer += "\n\t";
}

Expand All @@ -105,11 +101,12 @@ void c_code_backend::emit(GIFBlock& block)
else
{
file = fopen(output.c_str(), "w");
if(file == nullptr)
{
logger::error("Failed to open file: %s\n", output.cbegin());
return;
}
}

if(file == nullptr)
{
logger::error("Failed to open file: %s\n", output.cbegin());
return;
}
const std::string prologue = "#include <tamtypes.h>\n#include <gs_gp.h>\n#include <gif_tags.h>\n";
fwrite(prologue.c_str(), 1, prologue.size(), file);
Expand All @@ -118,10 +115,9 @@ void c_code_backend::emit(GIFBlock& block)
fwrite(buffer.c_str(), 1, buffer.size(), file);
}


std::string c_code_backend::emit_primitive(c_code_backend* inst, const GifRegister& reg)
auto c_code_backend::emit_primitive(c_code_backend* inst, const GifRegister& reg) -> std::string
{
const PRIM& prim = dynamic_cast<const PRIM&>(reg);
const auto& prim = dynamic_cast<const PRIM&>(reg);
if(inst->emit_mode == EmitMode::USE_DEFS)
{
return fmt::format("GS_SET_PRIM({},{},{},{},0,{},GS_ENABLE,0,0),GS_REG_PRIM,",
Expand All @@ -131,50 +127,48 @@ std::string c_code_backend::emit_primitive(c_code_backend* inst, const GifRegist
prim.IsFogging() ? "GS_ENABLE" : "GS_DISABLE",
prim.IsAA1() ? "GS_ENABLE" : "GS_DISABLE");
}
else
{
return fmt::format("GS_SET_PRIM({},{:d},{:d},{:d},0,{:d},1,0,0),0x00,",
static_cast<int>(prim.GetType()),
prim.IsGouraud(),
prim.IsTextured(),
prim.IsFogging(),
prim.IsAA1());
}

return fmt::format("GS_SET_PRIM({},{:d},{:d},{:d},0,{:d},1,0,0),0x00,",
static_cast<int>(prim.GetType()),
prim.IsGouraud(),
prim.IsTextured(),
prim.IsFogging(),
prim.IsAA1());
}

std::string c_code_backend::emit_rgbaq(c_code_backend* inst, const GifRegister& reg)
auto c_code_backend::emit_rgbaq(c_code_backend* inst, const GifRegister& reg) -> std::string
{
const RGBAQ& rgbaq = dynamic_cast<const RGBAQ&>(reg);
const auto& rgbaq = dynamic_cast<const RGBAQ&>(reg);

auto val = rgbaq.GetValue();

return fmt::format("GS_SET_RGBAQ(0x{:02x},0x{:02x},0x{:02x},0x{:02x},0x{:02x}),{},",
val.x, val.y, val.z, val.w, 0, inst->emit_mode == EmitMode::USE_DEFS ? "GS_REG_RGBAQ" : "0x01");
}

std::string c_code_backend::emit_uv(c_code_backend* inst, const GifRegister& reg)
auto c_code_backend::emit_uv(c_code_backend* inst, const GifRegister& reg) -> std::string
{
const UV& uv = dynamic_cast<const UV&>(reg);
const auto& uv_reg = dynamic_cast<const UV&>(reg);

auto val = uv.GetValue();
auto val = uv_reg.GetValue();

return fmt::format("GS_SET_UV({}<<4,{}<<4),{},",
val.x, val.y, inst->emit_mode == EmitMode::USE_DEFS ? "GS_REG_UV" : "0x03");
}

std::string c_code_backend::emit_xyz2(c_code_backend* inst, const GifRegister& reg)
auto c_code_backend::emit_xyz2(c_code_backend* inst, const GifRegister& reg) -> std::string
{
const XYZ2& xyz2 = dynamic_cast<const XYZ2&>(reg);
const auto& xyz2 = dynamic_cast<const XYZ2&>(reg);

auto val = xyz2.GetValue();

return fmt::format("GS_SET_XYZ({}<<4,{}<<4,{}),{},",
val.x, val.y, val.z, inst->emit_mode == EmitMode::USE_DEFS ? "GS_REG_XYZ2" : "0x05");
}

std::string c_code_backend::emit_tex0(c_code_backend* inst, const GifRegister& reg)
auto c_code_backend::emit_tex0(c_code_backend* inst, const GifRegister& reg) -> std::string
{
const TEX0& tex0 = dynamic_cast<const TEX0&>(reg);
const auto& tex0 = dynamic_cast<const TEX0&>(reg);

if(inst->emit_mode == EmitMode::USE_DEFS)
{
Expand All @@ -197,72 +191,68 @@ std::string c_code_backend::emit_tex0(c_code_backend* inst, const GifRegister& r
tex0.GetTBP(), tex0.GetTBW(), PSM_STR,
tex0.GetTW(), tex0.GetTH(), tex0.GetTCC(), static_cast<uint32_t>(tex0.GetTFX()));
}
else
{
return fmt::format("GS_SET_TEX0(0x{:02x},0x{:02x},{},{:02x},{:02x},{:d},{},0,0,0,0,0),0x06,",
tex0.GetTBP(), tex0.GetTBW(), static_cast<uint32_t>(tex0.GetPSM()),
tex0.GetTW(), tex0.GetTH(), tex0.GetTCC(), static_cast<uint32_t>(tex0.GetTFX()));
}

return fmt::format("GS_SET_TEX0(0x{:02x},0x{:02x},{},{:02x},{:02x},{:d},{},0,0,0,0,0),0x06,",
tex0.GetTBP(), tex0.GetTBW(), static_cast<uint32_t>(tex0.GetPSM()),
tex0.GetTW(), tex0.GetTH(), tex0.GetTCC(), static_cast<uint32_t>(tex0.GetTFX()));
}

std::string c_code_backend::emit_fog(c_code_backend* inst, const GifRegister& reg)
auto c_code_backend::emit_fog(c_code_backend* inst, const GifRegister& reg) -> std::string
{
const FOG& fog = dynamic_cast<const FOG&>(reg);
const auto& fog = dynamic_cast<const FOG&>(reg);

auto val = fog.GetValue();

return fmt::format("GS_SET_FOG(0x{:02x}),{},",
val, inst->emit_mode == EmitMode::USE_DEFS ? "GS_REG_FOG" : "0x0A");
}

std::string c_code_backend::emit_fogcol(c_code_backend* inst, const GifRegister& reg)
auto c_code_backend::emit_fogcol(c_code_backend* inst, const GifRegister& reg) -> std::string
{
const FOGCOL& fogcol = dynamic_cast<const FOGCOL&>(reg);
const auto& fogcol = dynamic_cast<const FOGCOL&>(reg);

auto val = fogcol.GetValue();

return fmt::format("GS_SET_FOGCOL(0x{:02x},0x{:02x},0x{:02x}),{},",
val.x, val.y, val.z, inst->emit_mode == EmitMode::USE_DEFS ? "GS_REG_FOGCOL" : "0x3D");
}

std::string c_code_backend::emit_scissor(c_code_backend* inst, const GifRegister& reg)
auto c_code_backend::emit_scissor(c_code_backend* inst, const GifRegister& reg) -> std::string
{
const SCISSOR& scissor = dynamic_cast<const SCISSOR&>(reg);
const auto& scissor = dynamic_cast<const SCISSOR&>(reg);

auto val = scissor.GetValue();

return fmt::format("GS_SET_SCISSOR({},{},{},{}),{},",
val.x, val.y, val.z, val.w, inst->emit_mode == EmitMode::USE_DEFS ? "GS_REG_SCISSOR" : "0x40");
}

std::string c_code_backend::emit_signal(c_code_backend* inst, const GifRegister& reg)
auto c_code_backend::emit_signal(c_code_backend* inst, const GifRegister& reg) -> std::string
{
const SIGNAL& signal = dynamic_cast<const SIGNAL&>(reg);
const auto& signal = dynamic_cast<const SIGNAL&>(reg);

auto val = signal.GetValue();

return fmt::format("GS_SET_SIGNAL(0x{:02x},0x{:02x}),{},",
val.x, val.y, inst->emit_mode == EmitMode::USE_DEFS ? "GS_REG_SIGNAL" : "0x60");
}

std::string c_code_backend::emit_finish(c_code_backend* inst, const GifRegister& reg)
auto c_code_backend::emit_finish(c_code_backend* inst, const GifRegister& reg) -> std::string
{
const FINISH& finish = dynamic_cast<const FINISH&>(reg);
const auto& finish = dynamic_cast<const FINISH&>(reg);

auto val = finish.GetValue();
if(inst->emit_mode == EmitMode::USE_DEFS)
{
return fmt::format("GS_SET_FINISH({}),GS_REG_FINISH,", val);
}
else
{
return fmt::format("0x{:x},{},", val, "0x61");
}

return fmt::format("0x{:x},{},", val, "0x61");
}

std::string c_code_backend::emit_label(c_code_backend* inst, const GifRegister& reg)
auto c_code_backend::emit_label(c_code_backend* inst, const GifRegister& reg) -> std::string
{
const LABEL& label = dynamic_cast<const LABEL&>(reg);
const auto& label = dynamic_cast<const LABEL&>(reg);

auto val = label.GetValue();

Expand Down
2 changes: 1 addition & 1 deletion backend/c_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class c_code_backend : public Backend
};

public:
c_code_backend() noexcept;
c_code_backend() = default;
~c_code_backend();

bool arg_parse(int argc, char** argv) override;
Expand Down
4 changes: 2 additions & 2 deletions gifscript.rl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "version.h"

#include <string.h>
#include <stdlib.h>
#include <cstring>
#include <cstdlib>
#include <map>
#include <fcntl.h>
#include <fmt/format.h>
Expand Down
Loading

0 comments on commit b187c92

Please sign in to comment.