From 27c9ffd611d33b8f7f44c948f530ea771da89e46 Mon Sep 17 00:00:00 2001 From: chaoticgd <43898262+chaoticgd@users.noreply.github.com> Date: Sun, 29 Oct 2023 17:12:14 +0000 Subject: [PATCH 1/4] Tweak formatting of STABS_DEBUG macro --- ccc/stabs.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ccc/stabs.cpp b/ccc/stabs.cpp index ba92c29d..185e00be 100644 --- a/ccc/stabs.cpp +++ b/ccc/stabs.cpp @@ -713,9 +713,13 @@ std::optional eat_dodgy_stabs_identifier(const char*& input) { return std::nullopt; } -STABS_DEBUG(static void print_field(const StabsField& field) { +STABS_DEBUG( + +static void print_field(const StabsField& field) { printf("\t%04x %04x %04x %04x %s\n", field.offset_bits / 8, field.size_bits / 8, field.offset_bits, field.size_bits, field.name.c_str()); -}) +} + +) const char* builtin_class_to_string(BuiltInClass bclass) { switch(bclass) { From e5ff86c99922e879e7408fc862c5274bee294462 Mon Sep 17 00:00:00 2001 From: chaoticgd <43898262+chaoticgd@users.noreply.github.com> Date: Sun, 29 Oct 2023 17:27:58 +0000 Subject: [PATCH 2/4] Replace some usages of stringf with std::to_string --- ccc/data_refinement.cpp | 16 ++++++++++++++++ ccc/print_json.cpp | 3 ++- ccc/stabs_to_ast.cpp | 6 +++++- ccc/util.cpp | 14 -------------- ccc/util.h | 3 --- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/ccc/data_refinement.cpp b/ccc/data_refinement.cpp index d2269f04..d9b7b20b 100644 --- a/ccc/data_refinement.cpp +++ b/ccc/data_refinement.cpp @@ -14,6 +14,8 @@ static std::unique_ptr refine_builtin(s32 virtual_address, BuiltInCla static std::unique_ptr refine_pointer_or_reference(s32 virtual_address, const ast::Node& type, const DataRefinementContext& context); static const char* generate_format_string(s32 size, bool is_signed); static std::string single_precision_float_to_string(float value); +static std::string string_format(const char* format, va_list args); +static std::string stringf(const char* format, ...); void refine_variables(HighSymbolTable& high, const std::vector& modules) { // Build a map of where all functions and globals are in memory, so that we @@ -305,4 +307,18 @@ static std::string single_precision_float_to_string(float value) { return result; } +static std::string string_format(const char* format, va_list args) { + static char buffer[16 * 1024]; + vsnprintf(buffer, sizeof(buffer), format, args); + return std::string(buffer); +} + +static std::string stringf(const char* format, ...) { + va_list args; + va_start(args, format); + std::string string = string_format(format, args); + va_end(args); + return string; +} + } diff --git a/ccc/print_json.cpp b/ccc/print_json.cpp index efe79be9..66bb6e9f 100644 --- a/ccc/print_json.cpp +++ b/ccc/print_json.cpp @@ -275,7 +275,8 @@ static void print_json_ast_node(JsonPrinter& json, const ast::Node* ptr) { json.property("stabs_type_number_to_deduplicated_type_index"); json.begin_object(); for(const auto [stabs_type_number, deduplicated_type_index] : source_file.stabs_type_number_to_deduplicated_type_index) { - json.number_property(stringf("%lld", merge_stabs_type_number_parts(stabs_type_number)).c_str(), deduplicated_type_index); + s64 merged_type_number = merge_stabs_type_number_parts(stabs_type_number); + json.number_property(std::to_string(merged_type_number).c_str(), deduplicated_type_index); } json.end_object(); break; diff --git a/ccc/stabs_to_ast.cpp b/ccc/stabs_to_ast.cpp index 4263e824..ded10cc3 100644 --- a/ccc/stabs_to_ast.cpp +++ b/ccc/stabs_to_ast.cpp @@ -84,7 +84,11 @@ Result> stabs_type_to_ast(const StabsType& type, cons if(type.anonymous || stabs_type == state.stabs_types->end()) { auto type_name = std::make_unique(); type_name->source = ast::TypeNameSource::ERROR; - type_name->type_name = stringf("CCC_BADTYPELOOKUP(%d,%d)", type.type_number.file, type.type_number.type); + type_name->type_name += "CCC_BADTYPELOOKUP("; + type_name->type_name += std::to_string(type.type_number.file); + type_name->type_name += ","; + type_name->type_name += std::to_string(type.type_number.type); + type_name->type_name += ")"; return std::unique_ptr(std::move(type_name)); } return stabs_type_to_ast(*stabs_type->second, state, abs_parent_offset_bytes, depth + 1, substitute_type_name, force_substitute); diff --git a/ccc/util.cpp b/ccc/util.cpp index 7307773c..a37566e4 100644 --- a/ccc/util.cpp +++ b/ccc/util.cpp @@ -54,20 +54,6 @@ Result get_string(const std::vector& bytes, u64 offset) { return CCC_FAILURE("Unexpected end of buffer while reading string."); } -std::string string_format(const char* format, va_list args) { - static char buffer[16 * 1024]; - vsnprintf(buffer, 16 * 1024, format, args); - return std::string(buffer); -} - -std::string stringf(const char* format, ...) { - va_list args; - va_start(args, format); - std::string string = string_format(format, args); - va_end(args); - return string; -} - std::string merge_paths(const std::string& base, const std::string& path) { // Try to figure out if we're dealing with a Windows path of a UNIX path. bool is_windows_path = false; diff --git a/ccc/util.h b/ccc/util.h index 80486ec7..606d0f2b 100644 --- a/ccc/util.h +++ b/ccc/util.h @@ -184,9 +184,6 @@ Result get_string(const std::vector& bytes, u64 offset); #define CCC_BEGIN_END(x) (x).begin(), (x).end() #define CCC_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -std::string string_format(const char* format, va_list args); -std::string stringf(const char* format, ...); - // These functions are to be used only for source file paths present in the // symbol table, since we want them to be handled consistently across different // platforms, which with std::filesystem::path doesn't seem to be possible. From e7b76fb4f9b0e0ff8eb0d82650bea42134b60ad9 Mon Sep 17 00:00:00 2001 From: chaoticgd <43898262+chaoticgd@users.noreply.github.com> Date: Sun, 29 Oct 2023 17:30:41 +0000 Subject: [PATCH 3/4] Make util.cpp include util.h instead of ccc.h --- ccc/util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ccc/util.cpp b/ccc/util.cpp index a37566e4..3f8e9f38 100644 --- a/ccc/util.cpp +++ b/ccc/util.cpp @@ -1,4 +1,4 @@ -#include "ccc.h" +#include "util.h" #include #include From 50996265fad4680bf462727520cb1fe434aad589 Mon Sep 17 00:00:00 2001 From: chaoticgd <43898262+chaoticgd@users.noreply.github.com> Date: Sun, 29 Oct 2023 17:31:54 +0000 Subject: [PATCH 4/4] Remove useless includes from util.cpp --- ccc/util.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/ccc/util.cpp b/ccc/util.cpp index 3f8e9f38..437ac6bb 100644 --- a/ccc/util.cpp +++ b/ccc/util.cpp @@ -1,14 +1,5 @@ #include "util.h" -#include -#include -#include -#include -#include -#include -#include -#include - namespace ccc { Error* format_error(const char* source_file, int source_line, const char* format, ...) {