Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do a tiny refactor #131

Merged
merged 4 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ccc/data_refinement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ static std::unique_ptr<ast::Node> refine_builtin(s32 virtual_address, BuiltInCla
static std::unique_ptr<ast::Node> 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<Module*>& modules) {
// Build a map of where all functions and globals are in memory, so that we
Expand Down Expand Up @@ -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;
}

}
3 changes: 2 additions & 1 deletion ccc/print_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions ccc/stabs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,9 +713,13 @@ std::optional<std::string> 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) {
Expand Down
6 changes: 5 additions & 1 deletion ccc/stabs_to_ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ Result<std::unique_ptr<ast::Node>> stabs_type_to_ast(const StabsType& type, cons
if(type.anonymous || stabs_type == state.stabs_types->end()) {
auto type_name = std::make_unique<ast::TypeName>();
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<ast::Node>(std::move(type_name));
}
return stabs_type_to_ast(*stabs_type->second, state, abs_parent_offset_bytes, depth + 1, substitute_type_name, force_substitute);
Expand Down
25 changes: 1 addition & 24 deletions ccc/util.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
#include "ccc.h"

#include <vector>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <sstream>
#include <iterator>
#include <stdexcept>
#include <filesystem>
#include "util.h"

namespace ccc {

Expand Down Expand Up @@ -54,20 +45,6 @@ Result<const char*> get_string(const std::vector<u8>& 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;
Expand Down
3 changes: 0 additions & 3 deletions ccc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ Result<const char*> get_string(const std::vector<u8>& 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.
Expand Down