Skip to content

Commit

Permalink
Reformat control statements
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoticgd committed Oct 22, 2024
1 parent 2526a67 commit e1a032d
Show file tree
Hide file tree
Showing 36 changed files with 1,073 additions and 1,073 deletions.
166 changes: 83 additions & 83 deletions src/ccc/ast.cpp

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions src/ccc/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,10 @@ enum ExplorationMode {
template <typename ThisNode, typename Callback>
void for_each_node(ThisNode& node, TraversalOrder order, Callback callback)
{
if(order == PREORDER_TRAVERSAL && callback(node) == DONT_EXPLORE_CHILDREN) {
if (order == PREORDER_TRAVERSAL && callback(node) == DONT_EXPLORE_CHILDREN) {
return;
}
switch(node.descriptor) {
switch (node.descriptor) {
case ARRAY: {
auto& array = node.template as<Array>();
for_each_node(*array.element_type.get(), order, callback);
Expand All @@ -338,11 +338,11 @@ void for_each_node(ThisNode& node, TraversalOrder order, Callback callback)
}
case FUNCTION: {
auto& func = node.template as<Function>();
if(func.return_type.has_value()) {
if (func.return_type.has_value()) {
for_each_node(*func.return_type->get(), order, callback);
}
if(func.parameters.has_value()) {
for(auto& child : *func.parameters) {
if (func.parameters.has_value()) {
for (auto& child : *func.parameters) {
for_each_node(*child.get(), order, callback);
}
}
Expand All @@ -361,13 +361,13 @@ void for_each_node(ThisNode& node, TraversalOrder order, Callback callback)
}
case STRUCT_OR_UNION: {
auto& struct_or_union = node.template as<StructOrUnion>();
for(auto& child : struct_or_union.base_classes) {
for (auto& child : struct_or_union.base_classes) {
for_each_node(*child.get(), order, callback);
}
for(auto& child : struct_or_union.fields) {
for (auto& child : struct_or_union.fields) {
for_each_node(*child.get(), order, callback);
}
for(auto& child : struct_or_union.member_functions) {
for (auto& child : struct_or_union.member_functions) {
for_each_node(*child.get(), order, callback);
}
break;
Expand All @@ -376,7 +376,7 @@ void for_each_node(ThisNode& node, TraversalOrder order, Callback callback)
break;
}
}
if(order == POSTORDER_TRAVERSAL) {
if (order == POSTORDER_TRAVERSAL) {
callback(node);
}
}
Expand Down
54 changes: 27 additions & 27 deletions src/ccc/ast_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,60 @@ void write_json(JsonWriter& json, const Node* ptr, const SymbolDatabase& databas
json.Key("descriptor");
json.String(node_type_to_string(node));

if(!node.name.empty()) {
if (!node.name.empty()) {
json.Key("name");
json.String(node.name.c_str());
}
if(node.offset_bytes != -1) {
if (node.offset_bytes != -1) {
json.Key("offset_bytes");
json.Int(node.offset_bytes);
}
if(node.size_bytes != -1) {
if (node.size_bytes != -1) {
json.Key("size_bytes");
json.Int(node.size_bytes);
}
if(node.size_bits != -1) {
if (node.size_bits != -1) {
json.Key("size_bits");
json.Int(node.size_bits);
}
if(node.storage_class != STORAGE_CLASS_NONE) {
if (node.storage_class != STORAGE_CLASS_NONE) {
json.Key("storage_class");
json.String(storage_class_to_string((StorageClass) node.storage_class));
}
if(node.access_specifier != AS_PUBLIC) {
if (node.access_specifier != AS_PUBLIC) {
json.Key("access_specifier");
json.String(access_specifier_to_string((AccessSpecifier) node.access_specifier));
}
if(node.is_const) {
if (node.is_const) {
json.Key("is_const");
json.Bool(node.is_const);
}
if(node.is_volatile) {
if (node.is_volatile) {
json.Key("is_volatile");
json.Bool(node.is_volatile);
}
if(node.is_virtual_base_class) {
if (node.is_virtual_base_class) {
json.Key("is_virtual_base_class");
json.Bool(node.is_virtual_base_class);
}
if(node.is_vtable_pointer) {
if (node.is_vtable_pointer) {
json.Key("is_vtable_pointer");
json.Bool(node.is_vtable_pointer);
}
if(node.is_constructor_or_destructor) {
if (node.is_constructor_or_destructor) {
json.Key("is_constructor_or_destructor");
json.Bool(node.is_constructor_or_destructor);
}
if(node.is_special_member_function) {
if (node.is_special_member_function) {
json.Key("is_special_member_function");
json.Bool(node.is_special_member_function);
}
if(node.is_operator_member_function) {
if (node.is_operator_member_function) {
json.Key("is_operator_member_function");
json.Bool(node.is_operator_member_function);
}

switch(node.descriptor) {
switch (node.descriptor) {
case ARRAY: {
const Array& array = node.as<Array>();
json.Key("element_type");
Expand All @@ -97,7 +97,7 @@ void write_json(JsonWriter& json, const Node* ptr, const SymbolDatabase& databas
const Enum& enumeration = node.as<Enum>();
json.Key("constants");
json.StartArray();
for(const auto& [value, name] : enumeration.constants) {
for (const auto& [value, name] : enumeration.constants) {
json.StartObject();
json.Key("value");
json.Int(value);
Expand All @@ -116,23 +116,23 @@ void write_json(JsonWriter& json, const Node* ptr, const SymbolDatabase& databas
}
case FUNCTION: {
const Function& function = node.as<Function>();
if(function.return_type.has_value()) {
if (function.return_type.has_value()) {
json.Key("return_type");
write_json(json, function.return_type->get(), database);
}
if(function.parameters.has_value()) {
if (function.parameters.has_value()) {
json.Key("parameters");
json.StartArray();
for(const std::unique_ptr<Node>& node : *function.parameters) {
for (const std::unique_ptr<Node>& node : *function.parameters) {
write_json(json, node.get(), database);
}
json.EndArray();
}
if(function.modifier != MemberFunctionModifier::NONE) {
if (function.modifier != MemberFunctionModifier::NONE) {
json.Key("modifier");
json.String(member_function_modifier_to_string(function.modifier));
}
if(function.vtable_index > -1) {
if (function.vtable_index > -1) {
json.Key("vtable_index");
json.Int(function.vtable_index);
}
Expand All @@ -154,26 +154,26 @@ void write_json(JsonWriter& json, const Node* ptr, const SymbolDatabase& databas
}
case STRUCT_OR_UNION: {
const StructOrUnion& struct_or_union = node.as<StructOrUnion>();
if(!struct_or_union.base_classes.empty()) {
if (!struct_or_union.base_classes.empty()) {
json.Key("base_classes");
json.StartArray();
for(const std::unique_ptr<Node>& base_class : struct_or_union.base_classes) {
for (const std::unique_ptr<Node>& base_class : struct_or_union.base_classes) {
write_json(json, base_class.get(), database);
}
json.EndArray();
}
if(!struct_or_union.fields.empty()) {
if (!struct_or_union.fields.empty()) {
json.Key("fields");
json.StartArray();
for(const std::unique_ptr<Node>& node : struct_or_union.fields) {
for (const std::unique_ptr<Node>& node : struct_or_union.fields) {
write_json(json, node.get(), database);
}
json.EndArray();
}
if(!struct_or_union.member_functions.empty()) {
if (!struct_or_union.member_functions.empty()) {
json.Key("member_functions");
json.StartArray();
for(const std::unique_ptr<Node>& node : struct_or_union.member_functions) {
for (const std::unique_ptr<Node>& node : struct_or_union.member_functions) {
write_json(json, node.get(), database);
}
json.EndArray();
Expand All @@ -186,7 +186,7 @@ void write_json(JsonWriter& json, const Node* ptr, const SymbolDatabase& databas
json.String(type_name_source_to_string(type_name.source));
json.Key("data_type");
json.Int(database.data_types.index_from_handle(type_name.data_type_handle));
if(const TypeName::UnresolvedStabs* unresolved = type_name.unresolved_stabs.get()) {
if (const TypeName::UnresolvedStabs* unresolved = type_name.unresolved_stabs.get()) {
json.Key("type_name");
json.String(unresolved->type_name);
}
Expand Down
50 changes: 25 additions & 25 deletions src/ccc/data_refinement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ static std::string stringf(const char* format, ...);

bool can_refine_variable(const VariableToRefine& variable)
{
if(!variable.storage) return false;
if(variable.storage->location == GlobalStorageLocation::BSS) return false;
if(variable.storage->location == GlobalStorageLocation::SBSS) return false;
if(!variable.address.valid()) return false;
if(!variable.type) return false;
if (!variable.storage) return false;
if (variable.storage->location == GlobalStorageLocation::BSS) return false;
if (variable.storage->location == GlobalStorageLocation::SBSS) return false;
if (!variable.address.valid()) return false;
if (!variable.type) return false;
return true;
}

Expand All @@ -43,7 +43,7 @@ Result<RefinedData> refine_variable(
static Result<RefinedData> refine_node(
u32 virtual_address, const ast::Node& type, const SymbolDatabase& database, const ElfFile& elf, s32 depth)
{
if(depth > 200) {
if (depth > 200) {
const char* error_message = "Call depth greater than 200 in refine_node, probably infinite recursion.";

CCC_WARN(error_message);
Expand All @@ -53,13 +53,13 @@ static Result<RefinedData> refine_node(
return data;
}

switch(type.descriptor) {
switch (type.descriptor) {
case ast::ARRAY: {
const ast::Array& array = type.as<ast::Array>();
CCC_CHECK(array.element_type->size_bytes > -1, "Cannot compute element size for '%s' array.", array.name.c_str());
RefinedData list;
std::vector<RefinedData>& elements = list.value.emplace<std::vector<RefinedData>>();
for(s32 i = 0; i < array.element_count; i++) {
for (s32 i = 0; i < array.element_count; i++) {
s32 offset = i * array.element_type->size_bytes;
Result<RefinedData> element = refine_node(virtual_address + offset, *array.element_type.get(), database, elf, depth + 1);
CCC_RETURN_IF_ERROR(element);
Expand All @@ -84,8 +84,8 @@ static Result<RefinedData> refine_node(
CCC_RETURN_IF_ERROR(read_result);

RefinedData data;
for(const auto& [number, name] : enumeration.constants) {
if(number == value) {
for (const auto& [number, name] : enumeration.constants) {
if (number == value) {
data.value = name;
return data;
}
Expand All @@ -111,16 +111,16 @@ static Result<RefinedData> refine_node(
RefinedData list;
std::vector<RefinedData>& children = list.value.emplace<std::vector<RefinedData>>();

for(s32 i = 0; i < (s32) struct_or_union.base_classes.size(); i++) {
for (s32 i = 0; i < (s32) struct_or_union.base_classes.size(); i++) {
const std::unique_ptr<ast::Node>& base_class = struct_or_union.base_classes[i];
Result<RefinedData> child = refine_node(virtual_address + base_class->offset_bytes, *base_class.get(), database, elf, depth + 1);
CCC_RETURN_IF_ERROR(child);
child->field_name = "base class " + std::to_string(i);
children.emplace_back(std::move(*child));
}

for(const std::unique_ptr<ast::Node>& field : struct_or_union.fields) {
if(field->storage_class == STORAGE_CLASS_STATIC) {
for (const std::unique_ptr<ast::Node>& field : struct_or_union.fields) {
if (field->storage_class == STORAGE_CLASS_STATIC) {
continue;
}
Result<RefinedData> child = refine_node(virtual_address + field->offset_bytes, *field.get(), database, elf, depth + 1);
Expand All @@ -147,7 +147,7 @@ static Result<RefinedData> refine_bitfield(
ast::BuiltInClass storage_unit_type = bit_field.storage_unit_type(database);

u128 value;
switch(storage_unit_type) {
switch (storage_unit_type) {
case ast::BuiltInClass::UNSIGNED_8:
case ast::BuiltInClass::UNQUALIFIED_8: {
Result<u8> storage_unit = elf.get_object_virtual<u8>(virtual_address);
Expand Down Expand Up @@ -234,7 +234,7 @@ static Result<RefinedData> refine_builtin(
u32 virtual_address, ast::BuiltInClass bclass, const SymbolDatabase& database, const ElfFile& elf)
{
u128 value;
switch(bclass) {
switch (bclass) {
case ast::BuiltInClass::VOID_TYPE: {
break;
}
Expand Down Expand Up @@ -287,26 +287,26 @@ static Result<RefinedData> refine_pointer_or_reference(
CCC_RETURN_IF_ERROR(read_result);

std::string string;
if(pointer != 0) {
if (pointer != 0) {
FunctionHandle function_handle = database.functions.first_handle_from_starting_address(pointer);
const Function* function_symbol = database.functions.symbol_from_handle(function_handle);

GlobalVariableHandle global_variable_handle = database.global_variables.first_handle_from_starting_address(pointer);
const GlobalVariable* global_variable_symbol = database.global_variables.symbol_from_handle(global_variable_handle);

if(function_symbol) {
if (function_symbol) {
bool is_pointer = type.descriptor == ast::POINTER_OR_REFERENCE
&& type.as<ast::PointerOrReference>().is_pointer;
if(is_pointer) {
if (is_pointer) {
string += "&";
}
string += function_symbol->name();
} else if(global_variable_symbol) {
} else if (global_variable_symbol) {
bool is_pointer = type.descriptor == ast::POINTER_OR_REFERENCE
&& type.as<ast::PointerOrReference>().is_pointer;
bool pointing_at_array = global_variable_symbol->type()
&& global_variable_symbol->type()->descriptor == ast::ARRAY;
if(is_pointer && !pointing_at_array) {
if (is_pointer && !pointing_at_array) {
string += "&";
}
string += global_variable_symbol->name();
Expand All @@ -325,7 +325,7 @@ static std::string builtin_to_string(u128 value, ast::BuiltInClass bclass)
{
std::string result;

switch(bclass) {
switch (bclass) {
case ast::BuiltInClass::VOID_TYPE: {
break;
}
Expand Down Expand Up @@ -361,7 +361,7 @@ static std::string builtin_to_string(u128 value, ast::BuiltInClass bclass)
static_assert(sizeof(double) == 8);

std::string string = stringf("%g", value.low);
if(strtof(string.c_str(), nullptr) != value.low) {
if (strtof(string.c_str(), nullptr) != value.low) {
string = stringf("%.17g", value.low);
}

Expand All @@ -383,7 +383,7 @@ static std::string builtin_to_string(u128 value, ast::BuiltInClass bclass)

static const char* generate_format_string(s32 size, bool is_signed)
{
switch(size) {
switch (size) {
case 1: return is_signed ? "%hhd" : "%hhu";
case 2: return is_signed ? "%hd" : "%hu";
case 4: return is_signed ? "%d" : "%hu";
Expand All @@ -394,10 +394,10 @@ static const char* generate_format_string(s32 size, bool is_signed)
static std::string single_precision_float_to_string(float value)
{
std::string result = stringf("%g", value);
if(strtof(result.c_str(), nullptr) != value) {
if (strtof(result.c_str(), nullptr) != value) {
result = stringf("%.9g", value);
}
if(result.find(".") == std::string::npos) {
if (result.find(".") == std::string::npos) {
result += ".";
}
result += "f";
Expand Down
Loading

0 comments on commit e1a032d

Please sign in to comment.