diff --git a/src/ccc/ast.cpp b/src/ccc/ast.cpp index 9c307648..62e7d767 100644 --- a/src/ccc/ast.cpp +++ b/src/ccc/ast.cpp @@ -15,7 +15,7 @@ static bool try_to_match_wobbly_typedefs( void Node::set_access_specifier(AccessSpecifier specifier, u32 importer_flags) { - if((importer_flags & NO_ACCESS_SPECIFIERS) == 0) { + if ((importer_flags & NO_ACCESS_SPECIFIERS) == 0) { access_specifier = specifier; } } @@ -24,7 +24,7 @@ std::pair Node::physical_type(SymbolDatabase& database, s32 ma { Node* type = this; DataType* symbol = nullptr; - for(s32 i = 0; i < max_depth && type->descriptor == TYPE_NAME; i++) { + for (s32 i = 0; i < max_depth && type->descriptor == TYPE_NAME; i++) { DataType* data_type = database.data_types.symbol_from_handle(type->as().data_type_handle); if (!data_type || !data_type->type()) { break; @@ -46,9 +46,9 @@ BuiltInClass BitField::storage_unit_type(const SymbolDatabase& database) const { BuiltInClass result = BuiltInClass::VOID_TYPE; - if(underlying_type.get()) { + if (underlying_type.get()) { auto [type, symbol] = underlying_type->physical_type(database); - switch(type->descriptor) { + switch (type->descriptor) { case BUILTIN: result = type->as().bclass; break; @@ -85,7 +85,7 @@ u128 BitField::pack_signed(s128 bitfield) const const char* member_function_modifier_to_string(MemberFunctionModifier modifier) { - switch(modifier) { + switch (modifier) { case MemberFunctionModifier::NONE: return "none"; case MemberFunctionModifier::STATIC: return "static"; case MemberFunctionModifier::VIRTUAL: return "virtual"; @@ -102,12 +102,12 @@ bool StructOrUnion::flatten_fields( s32 max_fields, s32 max_depth) const { - if(max_depth == 0) { + if (max_depth == 0) { return false; } - for(const std::unique_ptr& type_name : base_classes) { - if(type_name->descriptor != TYPE_NAME) { + for (const std::unique_ptr& type_name : base_classes) { + if (type_name->descriptor != TYPE_NAME) { continue; } @@ -115,22 +115,22 @@ bool StructOrUnion::flatten_fields( DataTypeHandle handle = type_name->as().data_type_handle; const DataType* base_class_symbol = database.data_types.symbol_from_handle(handle); - if(!base_class_symbol || !base_class_symbol->type() || base_class_symbol->type()->descriptor != STRUCT_OR_UNION) { + if (!base_class_symbol || !base_class_symbol->type() || base_class_symbol->type()->descriptor != STRUCT_OR_UNION) { continue; } const StructOrUnion& base_class = base_class_symbol->type()->as(); - if(!base_class.flatten_fields(output, base_class_symbol, database, skip_statics, new_base_offset, max_fields, max_depth - 1)) { + if (!base_class.flatten_fields(output, base_class_symbol, database, skip_statics, new_base_offset, max_fields, max_depth - 1)) { return false; } } - for(const std::unique_ptr& field : fields) { - if(skip_statics && field->storage_class == STORAGE_CLASS_STATIC) { + for (const std::unique_ptr& field : fields) { + if (skip_statics && field->storage_class == STORAGE_CLASS_STATIC) { continue; } - if((s32) output.size() >= max_fields) { + if ((s32) output.size() >= max_fields) { return false; } @@ -145,7 +145,7 @@ bool StructOrUnion::flatten_fields( const char* type_name_source_to_string(TypeNameSource source) { - switch(source) { + switch (source) { case TypeNameSource::REFERENCE: return "reference"; case TypeNameSource::CROSS_REFERENCE: return "cross_reference"; case TypeNameSource::UNNAMED_THIS: return "this"; @@ -155,7 +155,7 @@ const char* type_name_source_to_string(TypeNameSource source) const char* forward_declared_type_to_string(ForwardDeclaredType type) { - switch(type) { + switch (type) { case ForwardDeclaredType::STRUCT: return "struct"; case ForwardDeclaredType::UNION: return "union"; case ForwardDeclaredType::ENUM: return "enum"; @@ -165,7 +165,7 @@ const char* forward_declared_type_to_string(ForwardDeclaredType type) DataTypeHandle TypeName::data_type_handle_unless_forward_declared() const { - if(!is_forward_declared) { + if (!is_forward_declared) { return data_type_handle; } else { return DataTypeHandle(); @@ -177,18 +177,18 @@ CompareResult compare_nodes( { CompareResult result = CompareResultType::MATCHES_NO_SWAP; - if(node_lhs.descriptor != node_rhs.descriptor) { + if (node_lhs.descriptor != node_rhs.descriptor) { return CompareFailReason::DESCRIPTOR; } - if(check_intrusive_fields) { - if(node_lhs.storage_class != node_rhs.storage_class) { + if (check_intrusive_fields) { + if (node_lhs.storage_class != node_rhs.storage_class) { // In some cases we can determine that a type was typedef'd for C // translation units, but not for C++ translation units, so we need // to add a special case for that here. - if(node_lhs.storage_class == STORAGE_CLASS_TYPEDEF && node_rhs.storage_class == STORAGE_CLASS_NONE) { + if (node_lhs.storage_class == STORAGE_CLASS_TYPEDEF && node_rhs.storage_class == STORAGE_CLASS_NONE) { result = CompareResultType::MATCHES_FAVOUR_LHS; - } else if(node_lhs.storage_class == STORAGE_CLASS_NONE && node_rhs.storage_class == STORAGE_CLASS_TYPEDEF) { + } else if (node_lhs.storage_class == STORAGE_CLASS_NONE && node_rhs.storage_class == STORAGE_CLASS_TYPEDEF) { result = CompareResultType::MATCHES_FAVOUR_RHS; } else { return CompareFailReason::STORAGE_CLASS; @@ -200,32 +200,32 @@ CompareResult compare_nodes( // compare them. bool is_vtable_pointer = node_lhs.is_vtable_pointer && node_rhs.is_vtable_pointer; bool is_numbered_constructor = node_lhs.name.starts_with("$_") && node_rhs.name.starts_with("$_"); - if(node_lhs.name != node_rhs.name && !is_vtable_pointer && !is_numbered_constructor) { + if (node_lhs.name != node_rhs.name && !is_vtable_pointer && !is_numbered_constructor) { return CompareFailReason::NAME; } - if(node_lhs.offset_bytes != node_rhs.offset_bytes) { + if (node_lhs.offset_bytes != node_rhs.offset_bytes) { return CompareFailReason::RELATIVE_OFFSET_BYTES; } - if(node_lhs.size_bits != node_rhs.size_bits) { + if (node_lhs.size_bits != node_rhs.size_bits) { return CompareFailReason::SIZE_BITS; } - if(node_lhs.is_const != node_rhs.is_const) { + if (node_lhs.is_const != node_rhs.is_const) { return CompareFailReason::CONSTNESS; } } - switch(node_lhs.descriptor) { + switch (node_lhs.descriptor) { case ARRAY: { const auto [lhs, rhs] = Node::as(node_lhs, node_rhs); - if(compare_nodes_and_merge(result, *lhs.element_type.get(), *rhs.element_type.get(), database)) { + if (compare_nodes_and_merge(result, *lhs.element_type.get(), *rhs.element_type.get(), database)) { return result; } - if(lhs.element_count != rhs.element_count) { + if (lhs.element_count != rhs.element_count) { return CompareFailReason::ARRAY_ELEMENT_COUNT; } @@ -234,11 +234,11 @@ CompareResult compare_nodes( case BITFIELD: { const auto [lhs, rhs] = Node::as(node_lhs, node_rhs); - if(lhs.bitfield_offset_bits != rhs.bitfield_offset_bits) { + if (lhs.bitfield_offset_bits != rhs.bitfield_offset_bits) { return CompareFailReason::BITFIELD_OFFSET_BITS; } - if(compare_nodes_and_merge(result, *lhs.underlying_type.get(), *rhs.underlying_type.get(), database)) { + if (compare_nodes_and_merge(result, *lhs.underlying_type.get(), *rhs.underlying_type.get(), database)) { return result; } @@ -247,7 +247,7 @@ CompareResult compare_nodes( case BUILTIN: { const auto [lhs, rhs] = Node::as(node_lhs, node_rhs); - if(lhs.bclass != rhs.bclass) { + if (lhs.bclass != rhs.bclass) { return CompareFailReason::BUILTIN_CLASS; } @@ -256,7 +256,7 @@ CompareResult compare_nodes( case ENUM: { const auto [lhs, rhs] = Node::as(node_lhs, node_rhs); - if(lhs.constants != rhs.constants) { + if (lhs.constants != rhs.constants) { return CompareFailReason::ENUM_CONSTANTS; } @@ -268,30 +268,30 @@ CompareResult compare_nodes( case FUNCTION: { const auto [lhs, rhs] = Node::as(node_lhs, node_rhs); - if(lhs.return_type.has_value() != rhs.return_type.has_value()) { + if (lhs.return_type.has_value() != rhs.return_type.has_value()) { return CompareFailReason::FUNCTION_RETURN_TYPE_HAS_VALUE; } - if(lhs.return_type.has_value()) { - if(compare_nodes_and_merge(result, *lhs.return_type->get(), *rhs.return_type->get(), database)) { + if (lhs.return_type.has_value()) { + if (compare_nodes_and_merge(result, *lhs.return_type->get(), *rhs.return_type->get(), database)) { return result; } } - if(lhs.parameters.has_value() && rhs.parameters.has_value()) { - if(lhs.parameters->size() != rhs.parameters->size()) { + if (lhs.parameters.has_value() && rhs.parameters.has_value()) { + if (lhs.parameters->size() != rhs.parameters->size()) { return CompareFailReason::FUNCTION_PARAMAETER_COUNT; } - for(size_t i = 0; i < lhs.parameters->size(); i++) { - if(compare_nodes_and_merge(result, *(*lhs.parameters)[i].get(), *(*rhs.parameters)[i].get(), database)) { + for (size_t i = 0; i < lhs.parameters->size(); i++) { + if (compare_nodes_and_merge(result, *(*lhs.parameters)[i].get(), *(*rhs.parameters)[i].get(), database)) { return result; } } - } else if(lhs.parameters.has_value() != rhs.parameters.has_value()) { + } else if (lhs.parameters.has_value() != rhs.parameters.has_value()) { return CompareFailReason::FUNCTION_PARAMETERS_HAS_VALUE; } - if(lhs.modifier != rhs.modifier) { + if (lhs.modifier != rhs.modifier) { return CompareFailReason::FUNCTION_MODIFIER; } @@ -300,11 +300,11 @@ CompareResult compare_nodes( case POINTER_OR_REFERENCE: { const auto [lhs, rhs] = Node::as(node_lhs, node_rhs); - if(lhs.is_pointer != rhs.is_pointer) { + if (lhs.is_pointer != rhs.is_pointer) { return CompareFailReason::DESCRIPTOR; } - if(compare_nodes_and_merge(result, *lhs.value_type.get(), *rhs.value_type.get(), database)) { + if (compare_nodes_and_merge(result, *lhs.value_type.get(), *rhs.value_type.get(), database)) { return result; } @@ -313,11 +313,11 @@ CompareResult compare_nodes( case POINTER_TO_DATA_MEMBER: { const auto [lhs, rhs] = Node::as(node_lhs, node_rhs); - if(compare_nodes_and_merge(result, *lhs.class_type.get(), *rhs.class_type.get(), database)) { + if (compare_nodes_and_merge(result, *lhs.class_type.get(), *rhs.class_type.get(), database)) { return result; } - if(compare_nodes_and_merge(result, *lhs.member_type.get(), *rhs.member_type.get(), database)) { + if (compare_nodes_and_merge(result, *lhs.member_type.get(), *rhs.member_type.get(), database)) { return result; } @@ -326,36 +326,36 @@ CompareResult compare_nodes( case STRUCT_OR_UNION: { const auto [lhs, rhs] = Node::as(node_lhs, node_rhs); - if(lhs.is_struct != rhs.is_struct) { + if (lhs.is_struct != rhs.is_struct) { return CompareFailReason::DESCRIPTOR; } - if(lhs.base_classes.size() != rhs.base_classes.size()) { + if (lhs.base_classes.size() != rhs.base_classes.size()) { return CompareFailReason::BASE_CLASS_COUNT; } - for(size_t i = 0; i < lhs.base_classes.size(); i++) { - if(compare_nodes_and_merge(result, *lhs.base_classes[i].get(), *rhs.base_classes[i].get(), database)) { + for (size_t i = 0; i < lhs.base_classes.size(); i++) { + if (compare_nodes_and_merge(result, *lhs.base_classes[i].get(), *rhs.base_classes[i].get(), database)) { return result; } } - if(lhs.fields.size() != rhs.fields.size()) { + if (lhs.fields.size() != rhs.fields.size()) { return CompareFailReason::FIELDS_SIZE; } - for(size_t i = 0; i < lhs.fields.size(); i++) { - if(compare_nodes_and_merge(result, *lhs.fields[i].get(), *rhs.fields[i].get(), database)) { + for (size_t i = 0; i < lhs.fields.size(); i++) { + if (compare_nodes_and_merge(result, *lhs.fields[i].get(), *rhs.fields[i].get(), database)) { return result; } } - if(lhs.member_functions.size() != rhs.member_functions.size()) { + if (lhs.member_functions.size() != rhs.member_functions.size()) { return CompareFailReason::MEMBER_FUNCTION_COUNT; } - for(size_t i = 0; i < lhs.member_functions.size(); i++) { - if(compare_nodes_and_merge(result, *lhs.member_functions[i].get(), *rhs.member_functions[i].get(), database)) { + for (size_t i = 0; i < lhs.member_functions.size(); i++) { + if (compare_nodes_and_merge(result, *lhs.member_functions[i].get(), *rhs.member_functions[i].get(), database)) { return result; } } @@ -367,17 +367,17 @@ CompareResult compare_nodes( // Don't check the source so that REFERENCE and CROSS_REFERENCE are // treated as the same. - if(lhs.data_type_handle != rhs.data_type_handle) { + if (lhs.data_type_handle != rhs.data_type_handle) { return CompareFailReason::TYPE_NAME; } const TypeName::UnresolvedStabs* lhs_unresolved_stabs = lhs.unresolved_stabs.get(); const TypeName::UnresolvedStabs* rhs_unresolved_stabs = rhs.unresolved_stabs.get(); - if(lhs_unresolved_stabs && rhs_unresolved_stabs) { - if(lhs_unresolved_stabs->type_name != rhs_unresolved_stabs->type_name) { + if (lhs_unresolved_stabs && rhs_unresolved_stabs) { + if (lhs_unresolved_stabs->type_name != rhs_unresolved_stabs->type_name) { return CompareFailReason::TYPE_NAME; } - } else if(lhs_unresolved_stabs || rhs_unresolved_stabs) { + } else if (lhs_unresolved_stabs || rhs_unresolved_stabs) { return CompareFailReason::TYPE_NAME; } @@ -391,41 +391,41 @@ static bool compare_nodes_and_merge( CompareResult& dest, const Node& node_lhs, const Node& node_rhs, const SymbolDatabase* database) { CompareResult result = compare_nodes(node_lhs, node_rhs, database, true); - if(database) { - if(result.type == CompareResultType::DIFFERS && try_to_match_wobbly_typedefs(node_lhs, node_rhs, *database)) { + if (database) { + if (result.type == CompareResultType::DIFFERS && try_to_match_wobbly_typedefs(node_lhs, node_rhs, *database)) { result.type = CompareResultType::MATCHES_FAVOUR_LHS; - } else if(result.type == CompareResultType::DIFFERS && try_to_match_wobbly_typedefs(node_rhs, node_lhs, *database)) { + } else if (result.type == CompareResultType::DIFFERS && try_to_match_wobbly_typedefs(node_rhs, node_lhs, *database)) { result.type = CompareResultType::MATCHES_FAVOUR_RHS; } } - if(dest.type != result.type) { - if(dest.type == CompareResultType::DIFFERS || result.type == CompareResultType::DIFFERS) { + if (dest.type != result.type) { + if (dest.type == CompareResultType::DIFFERS || result.type == CompareResultType::DIFFERS) { // If any of the inner types differ, the outer type does too. dest.type = CompareResultType::DIFFERS; - } else if(dest.type == CompareResultType::MATCHES_CONFUSED || result.type == CompareResultType::MATCHES_CONFUSED) { + } else if (dest.type == CompareResultType::MATCHES_CONFUSED || result.type == CompareResultType::MATCHES_CONFUSED) { // Propagate confusion. dest.type = CompareResultType::MATCHES_CONFUSED; - } else if(dest.type == CompareResultType::MATCHES_FAVOUR_LHS && result.type == CompareResultType::MATCHES_FAVOUR_RHS) { + } else if (dest.type == CompareResultType::MATCHES_FAVOUR_LHS && result.type == CompareResultType::MATCHES_FAVOUR_RHS) { // One of the results favours the LHS node and the other favours the // RHS node so we are confused. dest.type = CompareResultType::MATCHES_CONFUSED; - } else if(dest.type == CompareResultType::MATCHES_FAVOUR_RHS && result.type == CompareResultType::MATCHES_FAVOUR_LHS) { + } else if (dest.type == CompareResultType::MATCHES_FAVOUR_RHS && result.type == CompareResultType::MATCHES_FAVOUR_LHS) { // One of the results favours the LHS node and the other favours the // RHS node so we are confused. dest.type = CompareResultType::MATCHES_CONFUSED; - } else if(dest.type == CompareResultType::MATCHES_FAVOUR_LHS || result.type == CompareResultType::MATCHES_FAVOUR_LHS) { + } else if (dest.type == CompareResultType::MATCHES_FAVOUR_LHS || result.type == CompareResultType::MATCHES_FAVOUR_LHS) { // One of the results favours the LHS node and the other is neutral // so go with the LHS node. dest.type = CompareResultType::MATCHES_FAVOUR_LHS; - } else if(dest.type == CompareResultType::MATCHES_FAVOUR_RHS || result.type == CompareResultType::MATCHES_FAVOUR_RHS) { + } else if (dest.type == CompareResultType::MATCHES_FAVOUR_RHS || result.type == CompareResultType::MATCHES_FAVOUR_RHS) { // One of the results favours the RHS node and the other is neutral // so go with the RHS node. dest.type = CompareResultType::MATCHES_FAVOUR_RHS; } } - if(dest.fail_reason == CompareFailReason::NONE) { + if (dest.fail_reason == CompareFailReason::NONE) { dest.fail_reason = result.fail_reason; } @@ -437,13 +437,13 @@ static bool try_to_match_wobbly_typedefs( { // Detect if one side has a typedef when the other just has the plain type. // This was previously a common reason why type deduplication would fail. - if(type_name_node.descriptor != TYPE_NAME) { + if (type_name_node.descriptor != TYPE_NAME) { return false; } const TypeName& type_name = type_name_node.as(); - if(const TypeName::UnresolvedStabs* unresolved_stabs = type_name.unresolved_stabs.get()) { - if(unresolved_stabs->referenced_file_handle == SourceFileHandle() || !unresolved_stabs->stabs_type_number.valid()) { + if (const TypeName::UnresolvedStabs* unresolved_stabs = type_name.unresolved_stabs.get()) { + if (unresolved_stabs->referenced_file_handle == SourceFileHandle() || !unresolved_stabs->stabs_type_number.valid()) { return false; } @@ -452,12 +452,12 @@ static bool try_to_match_wobbly_typedefs( CCC_ASSERT(source_file); auto handle = source_file->stabs_type_number_to_handle.find(unresolved_stabs->stabs_type_number); - if(handle != source_file->stabs_type_number_to_handle.end()) { + if (handle != source_file->stabs_type_number_to_handle.end()) { const DataType* referenced_type = database.data_types.symbol_from_handle(handle->second); CCC_ASSERT(referenced_type && referenced_type->type()); // Don't compare 'intrusive' fields e.g. the offset. CompareResult new_result = compare_nodes(*referenced_type->type(), raw_node, &database, false); - if(new_result.type != CompareResultType::DIFFERS) { + if (new_result.type != CompareResultType::DIFFERS) { return true; } } @@ -468,7 +468,7 @@ static bool try_to_match_wobbly_typedefs( const char* compare_fail_reason_to_string(CompareFailReason reason) { - switch(reason) { + switch (reason) { case CompareFailReason::NONE: return "error"; case CompareFailReason::DESCRIPTOR: return "descriptor"; case CompareFailReason::STORAGE_CLASS: return "storage class"; @@ -500,7 +500,7 @@ const char* compare_fail_reason_to_string(CompareFailReason reason) const char* node_type_to_string(const Node& node) { - switch(node.descriptor) { + switch (node.descriptor) { case ARRAY: return "array"; case BITFIELD: return "bitfield"; case BUILTIN: return "builtin"; @@ -509,7 +509,7 @@ const char* node_type_to_string(const Node& node) case FUNCTION: return "function"; case POINTER_OR_REFERENCE: { const PointerOrReference& pointer_or_reference = node.as(); - if(pointer_or_reference.is_pointer) { + if (pointer_or_reference.is_pointer) { return "pointer"; } else { return "reference"; @@ -518,7 +518,7 @@ const char* node_type_to_string(const Node& node) case POINTER_TO_DATA_MEMBER: return "pointer_to_data_member"; case STRUCT_OR_UNION: { const StructOrUnion& struct_or_union = node.as(); - if(struct_or_union.is_struct) { + if (struct_or_union.is_struct) { return "struct"; } else { return "union"; @@ -531,7 +531,7 @@ const char* node_type_to_string(const Node& node) const char* storage_class_to_string(StorageClass storage_class) { - switch(storage_class) { + switch (storage_class) { case STORAGE_CLASS_NONE: return "none"; case STORAGE_CLASS_TYPEDEF: return "typedef"; case STORAGE_CLASS_EXTERN: return "extern"; @@ -544,7 +544,7 @@ const char* storage_class_to_string(StorageClass storage_class) const char* access_specifier_to_string(AccessSpecifier specifier) { - switch(specifier) { + switch (specifier) { case AS_PUBLIC: return "public"; case AS_PROTECTED: return "protected"; case AS_PRIVATE: return "private"; @@ -554,7 +554,7 @@ const char* access_specifier_to_string(AccessSpecifier specifier) const char* builtin_class_to_string(BuiltInClass bclass) { - switch(bclass) { + switch (bclass) { case BuiltInClass::VOID_TYPE: return "void"; case BuiltInClass::UNSIGNED_8: return "8-bit unsigned integer"; case BuiltInClass::SIGNED_8: return "8-bit signed integer"; @@ -578,7 +578,7 @@ const char* builtin_class_to_string(BuiltInClass bclass) s32 builtin_class_size(BuiltInClass bclass) { - switch(bclass) { + switch (bclass) { case BuiltInClass::VOID_TYPE: return 0; case BuiltInClass::UNSIGNED_8: return 1; case BuiltInClass::SIGNED_8: return 1; diff --git a/src/ccc/ast.h b/src/ccc/ast.h index 51581209..615a61c5 100644 --- a/src/ccc/ast.h +++ b/src/ccc/ast.h @@ -313,10 +313,10 @@ enum ExplorationMode { template 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(); for_each_node(*array.element_type.get(), order, callback); @@ -338,11 +338,11 @@ void for_each_node(ThisNode& node, TraversalOrder order, Callback callback) } case FUNCTION: { auto& func = node.template as(); - 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); } } @@ -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(); - 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; @@ -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); } } diff --git a/src/ccc/ast_json.cpp b/src/ccc/ast_json.cpp index 66f5802c..19889aba 100644 --- a/src/ccc/ast_json.cpp +++ b/src/ccc/ast_json.cpp @@ -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(); json.Key("element_type"); @@ -97,7 +97,7 @@ void write_json(JsonWriter& json, const Node* ptr, const SymbolDatabase& databas const Enum& enumeration = node.as(); 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); @@ -116,23 +116,23 @@ void write_json(JsonWriter& json, const Node* ptr, const SymbolDatabase& databas } case FUNCTION: { const Function& function = node.as(); - 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 : *function.parameters) { + for (const std::unique_ptr& 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); } @@ -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(); - 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& base_class : struct_or_union.base_classes) { + for (const std::unique_ptr& 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 : struct_or_union.fields) { + for (const std::unique_ptr& 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 : struct_or_union.member_functions) { + for (const std::unique_ptr& node : struct_or_union.member_functions) { write_json(json, node.get(), database); } json.EndArray(); @@ -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); } diff --git a/src/ccc/data_refinement.cpp b/src/ccc/data_refinement.cpp index 38b81b52..39b17813 100644 --- a/src/ccc/data_refinement.cpp +++ b/src/ccc/data_refinement.cpp @@ -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; } @@ -43,7 +43,7 @@ Result refine_variable( static Result 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); @@ -53,13 +53,13 @@ static Result refine_node( return data; } - switch(type.descriptor) { + switch (type.descriptor) { case ast::ARRAY: { const ast::Array& array = type.as(); CCC_CHECK(array.element_type->size_bytes > -1, "Cannot compute element size for '%s' array.", array.name.c_str()); RefinedData list; std::vector& elements = list.value.emplace>(); - 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 element = refine_node(virtual_address + offset, *array.element_type.get(), database, elf, depth + 1); CCC_RETURN_IF_ERROR(element); @@ -84,8 +84,8 @@ static Result 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; } @@ -111,7 +111,7 @@ static Result refine_node( RefinedData list; std::vector& children = list.value.emplace>(); - 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& base_class = struct_or_union.base_classes[i]; Result child = refine_node(virtual_address + base_class->offset_bytes, *base_class.get(), database, elf, depth + 1); CCC_RETURN_IF_ERROR(child); @@ -119,8 +119,8 @@ static Result refine_node( children.emplace_back(std::move(*child)); } - for(const std::unique_ptr& field : struct_or_union.fields) { - if(field->storage_class == STORAGE_CLASS_STATIC) { + for (const std::unique_ptr& field : struct_or_union.fields) { + if (field->storage_class == STORAGE_CLASS_STATIC) { continue; } Result child = refine_node(virtual_address + field->offset_bytes, *field.get(), database, elf, depth + 1); @@ -147,7 +147,7 @@ static Result 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 storage_unit = elf.get_object_virtual(virtual_address); @@ -234,7 +234,7 @@ static Result 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; } @@ -287,26 +287,26 @@ static Result 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().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().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(); @@ -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; } @@ -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); } @@ -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"; @@ -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"; diff --git a/src/ccc/dependency.cpp b/src/ccc/dependency.cpp index 1f8ee458..334792ed 100644 --- a/src/ccc/dependency.cpp +++ b/src/ccc/dependency.cpp @@ -32,38 +32,38 @@ static void map_types_to_files_based_on_reference_count_single_pass(SymbolDataba void map_types_to_files_based_on_this_pointers(SymbolDatabase& database) { - for(const Function& function : database.functions) { - if(!function.parameter_variables().has_value() || function.parameter_variables()->empty()) { + for (const Function& function : database.functions) { + if (!function.parameter_variables().has_value() || function.parameter_variables()->empty()) { continue; } ParameterVariableHandle parameter_handle = (*function.parameter_variables())[0]; const ParameterVariable* parameter_variable = database.parameter_variables.symbol_from_handle(parameter_handle); - if(!parameter_variable) { + if (!parameter_variable) { continue; } const ast::Node* parameter_type = parameter_variable->type(); - if(!parameter_type) { + if (!parameter_type) { continue; } // Check if the first argument is a this pointer. bool is_pointer = parameter_type->descriptor == ast::POINTER_OR_REFERENCE && parameter_type->as().is_pointer; - if(parameter_variable->name() != "this" || !is_pointer) { + if (parameter_variable->name() != "this" || !is_pointer) { continue; } const ast::Node& class_node = *parameter_type->as().value_type.get(); - if(class_node.descriptor != ast::TYPE_NAME) { + if (class_node.descriptor != ast::TYPE_NAME) { continue; } // Lookup the type pointed to by the this pointer. const ast::TypeName& type_name = class_node.as(); DataType* class_type = database.data_types.symbol_from_handle(type_name.data_type_handle_unless_forward_declared()); - if(!class_type) { + if (!class_type) { continue; } @@ -80,65 +80,65 @@ void map_types_to_files_based_on_reference_count(SymbolDatabase& database) static void map_types_to_files_based_on_reference_count_single_pass(SymbolDatabase& database, bool do_types) { - for(DataType& type : database.data_types) { - if(type.files.size() == 1) { + for (DataType& type : database.data_types) { + if (type.files.size() == 1) { continue; } SourceFileHandle most_referenced_file; s32 most_references = 0; - for(SourceFileHandle file_handle : type.files) { + for (SourceFileHandle file_handle : type.files) { SourceFile* file = database.source_files.symbol_from_handle(file_handle); - if(!file) { + if (!file) { continue; } s32 reference_count = 0; auto count_references = [&](const ast::Node& node) { - if(node.descriptor == ast::TYPE_NAME) { + if (node.descriptor == ast::TYPE_NAME) { const ast::TypeName& type_name = node.as(); DataType* data_type = database.data_types.symbol_from_handle(type_name.data_type_handle_unless_forward_declared()); - if(data_type && data_type->handle() == type.handle()) { + if (data_type && data_type->handle() == type.handle()) { reference_count++; } } return ast::EXPLORE_CHILDREN; }; - if(do_types) { - for(const DataType& data_type : database.data_types) { - if(data_type.files.size() == 1 && data_type.files[0] == file_handle) { + if (do_types) { + for (const DataType& data_type : database.data_types) { + if (data_type.files.size() == 1 && data_type.files[0] == file_handle) { CCC_ASSERT(data_type.type()); ast::for_each_node(*data_type.type(), ast::PREORDER_TRAVERSAL, count_references); } } } else { - for(const Function* function : database.functions.symbols_from_handles(file->functions())) { - if(function->storage_class != STORAGE_CLASS_STATIC) { - if(function->type()) { + for (const Function* function : database.functions.symbols_from_handles(file->functions())) { + if (function->storage_class != STORAGE_CLASS_STATIC) { + if (function->type()) { ast::for_each_node(*function->type(), ast::PREORDER_TRAVERSAL, count_references); } - for(const ParameterVariable* parameter : database.parameter_variables.optional_symbols_from_handles(function->parameter_variables())) { - if(parameter->type()) { + for (const ParameterVariable* parameter : database.parameter_variables.optional_symbols_from_handles(function->parameter_variables())) { + if (parameter->type()) { ast::for_each_node(*parameter->type(), ast::PREORDER_TRAVERSAL, count_references); } } } } - for(const GlobalVariable* global_variable : database.global_variables.symbols_from_handles(file->global_variables())) { - if(global_variable->storage_class != STORAGE_CLASS_STATIC) { - if(global_variable->type()) { + for (const GlobalVariable* global_variable : database.global_variables.symbols_from_handles(file->global_variables())) { + if (global_variable->storage_class != STORAGE_CLASS_STATIC) { + if (global_variable->type()) { ast::for_each_node(*global_variable->type(), ast::PREORDER_TRAVERSAL, count_references); } } } } - if(reference_count > most_references) { + if (reference_count > most_references) { most_referenced_file = file_handle; most_references = reference_count; } } - if(most_referenced_file.valid()) { + if (most_referenced_file.valid()) { type.files = {most_referenced_file}; } } @@ -147,15 +147,15 @@ static void map_types_to_files_based_on_reference_count_single_pass(SymbolDataba TypeDependencyAdjacencyList build_type_dependency_graph(const SymbolDatabase& database) { TypeDependencyAdjacencyList graph; - for(const DataType& data_type : database.data_types) { + for (const DataType& data_type : database.data_types) { std::set dependencies; CCC_ASSERT(data_type.type()); ast::for_each_node(*data_type.type(), ast::PREORDER_TRAVERSAL, [&](const ast::Node& node) { - if(node.descriptor == ast::TYPE_NAME) { + if (node.descriptor == ast::TYPE_NAME) { const ast::TypeName& type_name = node.as(); - if(type_name.source == ast::TypeNameSource::REFERENCE) { + if (type_name.source == ast::TypeNameSource::REFERENCE) { DataTypeHandle dependency_handle = type_name.data_type_handle_unless_forward_declared(); - if(dependency_handle.valid()) { + if (dependency_handle.valid()) { dependencies.emplace(dependency_handle); } } @@ -171,24 +171,24 @@ void print_type_dependency_graph(FILE* out, const SymbolDatabase& database, cons { GraphPrinter printer(out); printer.begin_graph("type_dependencies", DIRECTED); - for(const DataType& data_type : database.data_types) { + for (const DataType& data_type : database.data_types) { CCC_ASSERT(data_type.type()); - if(!data_type.name().empty() && data_type.type()->descriptor != ast::BUILTIN && data_type.name() != "void") { + if (!data_type.name().empty() && data_type.type()->descriptor != ast::BUILTIN && data_type.name() != "void") { printer.node(data_type.name().c_str(), data_type.name().c_str()); } } - for(const auto& [handle, dependencies] : graph) { + for (const auto& [handle, dependencies] : graph) { const DataType* out_node = database.data_types.symbol_from_handle(handle); - if(!out_node) { + if (!out_node) { continue; } CCC_ASSERT(out_node->type()); - if(!out_node->name().empty() && out_node->type()->descriptor != ast::BUILTIN && out_node->name() != "void") { - for(DataTypeHandle in : dependencies) { + if (!out_node->name().empty() && out_node->type()->descriptor != ast::BUILTIN && out_node->name() != "void") { + for (DataTypeHandle in : dependencies) { const DataType* in_node = database.data_types.symbol_from_handle(in); CCC_ASSERT(in_node->type()); - if(!in_node->name().empty() && in_node->type()->descriptor != ast::BUILTIN && in_node->name() != "void") { + if (!in_node->name().empty() && in_node->type()->descriptor != ast::BUILTIN && in_node->name() != "void") { printer.edge(out_node->name().c_str(), in_node->name().c_str()); } } @@ -225,10 +225,10 @@ void GraphPrinter::edge(const char* out_name, const char* in_name) void GraphPrinter::new_line() { - if(!no_lines_printed) { + if (!no_lines_printed) { fprintf(out, "\n"); } - for(s32 i = 0; i < indent_level; i++) { + for (s32 i = 0; i < indent_level; i++) { fprintf(out, "\t"); } no_lines_printed = false; diff --git a/src/ccc/elf.cpp b/src/ccc/elf.cpp index 8c3ed99e..46c2b217 100644 --- a/src/ccc/elf.cpp +++ b/src/ccc/elf.cpp @@ -22,7 +22,7 @@ Result ElfFile::parse(std::vector image) const ElfSectionHeader* shstr_section_header = get_packed(elf.image, header->shoff + header->shstrndx * sizeof(ElfSectionHeader)); CCC_CHECK(shstr_section_header, "ELF section name header out of range."); - for(u32 i = 0; i < header->shnum; i++) { + for (u32 i = 0; i < header->shnum; i++) { u64 header_offset = header->shoff + i * sizeof(ElfSectionHeader); const ElfSectionHeader* section_header = get_packed(elf.image, header_offset); CCC_CHECK(section_header, "ELF section header out of range."); @@ -35,7 +35,7 @@ Result ElfFile::parse(std::vector image) section.header = *section_header; } - for(u32 i = 0; i < header->phnum; i++) { + for (u32 i = 0; i < header->phnum; i++) { u64 header_offset = header->phoff + i * sizeof(ElfProgramHeader); const ElfProgramHeader* program_header = get_packed(elf.image, header_offset); CCC_CHECK(program_header, "ELF program header out of range."); @@ -49,7 +49,7 @@ Result ElfFile::parse(std::vector image) Result ElfFile::create_section_symbols( SymbolDatabase& database, const SymbolGroup& group) const { - for(const ElfSection& section : sections) { + for (const ElfSection& section : sections) { Address address = Address::non_zero(section.header.addr); Result symbol = database.sections.create_symbol( @@ -64,8 +64,8 @@ Result ElfFile::create_section_symbols( const ElfSection* ElfFile::lookup_section(const char* name) const { - for(const ElfSection& section : sections) { - if(section.name == name) { + for (const ElfSection& section : sections) { + if (section.name == name) { return §ion; } } @@ -74,8 +74,8 @@ const ElfSection* ElfFile::lookup_section(const char* name) const std::optional ElfFile::file_offset_to_virtual_address(u32 file_offset) const { - for(const ElfProgramHeader& segment : segments) { - if(file_offset >= segment.offset && file_offset < segment.offset + segment.filesz) { + for (const ElfProgramHeader& segment : segments) { + if (file_offset >= segment.offset && file_offset < segment.offset + segment.filesz) { return segment.vaddr + file_offset - segment.offset; } } @@ -85,8 +85,8 @@ std::optional ElfFile::file_offset_to_virtual_address(u32 file_offset) cons const ElfProgramHeader* ElfFile::entry_point_segment() const { const ccc::ElfProgramHeader* entry_segment = nullptr; - for(const ccc::ElfProgramHeader& segment : segments) { - if(file_header.entry >= segment.vaddr && file_header.entry < segment.vaddr + segment.filesz) { + for (const ccc::ElfProgramHeader& segment : segments) { + if (file_header.entry >= segment.vaddr && file_header.entry < segment.vaddr + segment.filesz) { entry_segment = &segment; } } @@ -97,12 +97,12 @@ Result> ElfFile::get_virtual(u32 address, u32 size) const { u32 end_address = address + size; - if(end_address >= address) { - for(const ElfProgramHeader& segment : segments) { - if(address >= segment.vaddr && end_address <= segment.vaddr + segment.filesz) { + if (end_address >= address) { + for (const ElfProgramHeader& segment : segments) { + if (address >= segment.vaddr && end_address <= segment.vaddr + segment.filesz) { size_t begin_offset = segment.offset + (address - segment.vaddr); size_t end_offset = begin_offset + size; - if(begin_offset <= image.size() && end_offset <= image.size()) { + if (begin_offset <= image.size() && end_offset <= image.size()) { return std::span(image.data() + begin_offset, image.data() + end_offset); } } diff --git a/src/ccc/elf_symtab.cpp b/src/ccc/elf_symtab.cpp index 892ea5ec..0151f988 100644 --- a/src/ccc/elf_symtab.cpp +++ b/src/ccc/elf_symtab.cpp @@ -59,29 +59,29 @@ Result import_symbols( u32 importer_flags, DemanglerFunctions demangler) { - for(u32 i = 0; i < symtab.size() / sizeof(Symbol); i++) { + for (u32 i = 0; i < symtab.size() / sizeof(Symbol); i++) { const Symbol* symbol = get_packed(symtab, i * sizeof(Symbol)); CCC_ASSERT(symbol); Address address; - if(symbol->value != 0) { + if (symbol->value != 0) { address = symbol->value; } - if(!address.valid() || symbol->visibility() != SymbolVisibility::DEFAULT) { + if (!address.valid() || symbol->visibility() != SymbolVisibility::DEFAULT) { continue; } - if(!(importer_flags & DONT_DEDUPLICATE_SYMBOLS)) { - if(database.functions.first_handle_from_starting_address(address).valid()) { + if (!(importer_flags & DONT_DEDUPLICATE_SYMBOLS)) { + if (database.functions.first_handle_from_starting_address(address).valid()) { continue; } - if(database.global_variables.first_handle_from_starting_address(address).valid()) { + if (database.global_variables.first_handle_from_starting_address(address).valid()) { continue; } - if(database.local_variables.first_handle_from_starting_address(address).valid()) { + if (database.local_variables.first_handle_from_starting_address(address).valid()) { continue; } } @@ -89,7 +89,7 @@ Result import_symbols( const char* string = get_string(strtab, symbol->name); CCC_CHECK(string, "Symbol string out of range."); - switch(symbol->type()) { + switch (symbol->type()) { case SymbolType::NOTYPE: { Result label = database.labels.create_symbol( string, group.source, group.module_symbol, address, importer_flags, demangler); @@ -106,12 +106,12 @@ Result import_symbols( break; } case SymbolType::OBJECT: { - if(symbol->size != 0) { + if (symbol->size != 0) { Result global_variable = database.global_variables.create_symbol( string, group.source, group.module_symbol, address, importer_flags, demangler); CCC_RETURN_IF_ERROR(global_variable); - if(*global_variable) { + if (*global_variable) { (*global_variable)->set_size(symbol->size); } } else { @@ -127,7 +127,7 @@ Result import_symbols( string, group.source, group.module_symbol, address, importer_flags, demangler); CCC_RETURN_IF_ERROR(function); - if(*function) { + if (*function) { (*function)->set_size(symbol->size); } @@ -152,7 +152,7 @@ Result print_symbol_table(FILE* out, std::span symtab, std::span fprintf(out, "ELF SYMBOLS:\n"); fprintf(out, " Num: Value Size Type Bind Vis Ndx Name\n"); - for(u32 i = 0; i < symtab.size() / sizeof(Symbol); i++) { + for (u32 i = 0; i < symtab.size() / sizeof(Symbol); i++) { const Symbol* symbol = get_packed(symtab, i * sizeof(Symbol)); CCC_ASSERT(symbol); @@ -173,7 +173,7 @@ Result print_symbol_table(FILE* out, std::span symtab, std::span static const char* symbol_bind_to_string(SymbolBind bind) { - switch(bind) { + switch (bind) { case SymbolBind::LOCAL: return "LOCAL"; case SymbolBind::GLOBAL: return "GLOBAL"; case SymbolBind::WEAK: return "WEAK"; @@ -185,7 +185,7 @@ static const char* symbol_bind_to_string(SymbolBind bind) static const char* symbol_type_to_string(SymbolType type) { - switch(type) { + switch (type) { case SymbolType::NOTYPE: return "NOTYPE"; case SymbolType::OBJECT: return "OBJECT"; case SymbolType::FUNC: return "FUNC"; @@ -201,7 +201,7 @@ static const char* symbol_type_to_string(SymbolType type) static const char* symbol_visibility_to_string(SymbolVisibility visibility) { - switch(visibility) { + switch (visibility) { case SymbolVisibility::DEFAULT: return "DEFAULT"; case SymbolVisibility::INTERNAL: return "INTERNAL"; case SymbolVisibility::HIDDEN: return "HIDDEN"; diff --git a/src/ccc/importer_flags.cpp b/src/ccc/importer_flags.cpp index 204e15ff..eef1e609 100644 --- a/src/ccc/importer_flags.cpp +++ b/src/ccc/importer_flags.cpp @@ -70,8 +70,8 @@ const std::vector IMPORTER_FLAGS = { u32 parse_importer_flag(const char* argument) { - for(const ImporterFlagInfo& flag : IMPORTER_FLAGS) { - if(strcmp(flag.argument, argument) == 0) { + for (const ImporterFlagInfo& flag : IMPORTER_FLAGS) { + if (strcmp(flag.argument, argument) == 0) { return flag.flag; } } @@ -80,11 +80,11 @@ u32 parse_importer_flag(const char* argument) void print_importer_flags_help(FILE* out) { - for(const ImporterFlagInfo& flag : IMPORTER_FLAGS) { + for (const ImporterFlagInfo& flag : IMPORTER_FLAGS) { fprintf(out, "\n"); fprintf(out, " %-29s ", flag.argument); - for(size_t i = 0; i < flag.help_text.size(); i++) { - if(i > 0) { + for (size_t i = 0; i < flag.help_text.size(); i++) { + if (i > 0) { fprintf(out, " "); } fprintf(out, "%s\n", flag.help_text[i]); diff --git a/src/ccc/int128.cpp b/src/ccc/int128.cpp index b49c1ae6..5d01cb72 100644 --- a/src/ccc/int128.cpp +++ b/src/ccc/int128.cpp @@ -85,13 +85,13 @@ u128 u128::operator^(u128 rhs) u128 u128::operator<<(u64 bits) { u128 result; - if(bits == 0) { + if (bits == 0) { result.low = low; result.high = high; - } else if(bits < 64) { + } else if (bits < 64) { result.low = low << bits; result.high = (high << bits) | (low >> (64 - bits)); - } else if(bits < 128) { + } else if (bits < 128) { result.low = 0; result.high = low << (bits - 64); } else { @@ -104,13 +104,13 @@ u128 u128::operator<<(u64 bits) u128 u128::operator>>(u64 bits) { u128 result; - if(bits == 0) { + if (bits == 0) { result.low = low; result.high = high; - } else if(bits < 64) { + } else if (bits < 64) { result.low = (low >> bits) | (high << (64 - bits)); result.high = high >> bits; - } else if(bits < 128) { + } else if (bits < 128) { result.low = high >> (bits - 64); result.high = 0; } else { @@ -123,10 +123,10 @@ u128 u128::operator>>(u64 bits) std::string u128::to_string() { std::string result(32, '\0'); - for(u32 i = 0; i < 16; i++) { + for (u32 i = 0; i < 16; i++) { result[i] = HEX_DIGITS[(high >> ((15 - i) * 4)) & 0xf]; } - for(u32 i = 0; i < 16; i++) { + for (u32 i = 0; i < 16; i++) { result[16 + i] = HEX_DIGITS[(low >> ((15 - i) * 4)) & 0xf]; } return result; @@ -135,25 +135,25 @@ std::string u128::to_string() std::optional u128::from_string(const std::string&& hex) { u128 result; - for(u32 i = 0; i < 16; i++) { + for (u32 i = 0; i < 16; i++) { char c = hex[i]; - if(c >= '0' && c <= '9') { + if (c >= '0' && c <= '9') { result.high |= static_cast(c - '0') << ((15 - i) * 4); - } else if(c >= 'A' && c <= 'F') { + } else if (c >= 'A' && c <= 'F') { result.high |= static_cast(10 + c - 'A') << ((15 - i) * 4); - } else if(c >= 'a' && c <= 'f') { + } else if (c >= 'a' && c <= 'f') { result.high |= static_cast(10 + c - 'a') << ((15 - i) * 4); } else { return std::nullopt; } } - for(u32 i = 0; i < 16; i++) { + for (u32 i = 0; i < 16; i++) { char c = hex[16 + i]; - if(c >= '0' && c <= '9') { + if (c >= '0' && c <= '9') { result.low |= static_cast(c - '0') << ((15 - i) * 4); - } else if(c >= 'A' && c <= 'F') { + } else if (c >= 'A' && c <= 'F') { result.low |= static_cast(10 + c - 'A') << ((15 - i) * 4); - } else if(c >= 'a' && c <= 'f') { + } else if (c >= 'a' && c <= 'f') { result.low |= static_cast(10 + c - 'a') << ((15 - i) * 4); } else { return std::nullopt; @@ -240,13 +240,13 @@ s128 s128::operator^(s128 rhs) s128 s128::operator<<(u64 bits) { s128 result; - if(bits == 0) { + if (bits == 0) { result.low = low; result.high = high; - } else if(bits < 64) { + } else if (bits < 64) { result.low = low << bits; result.high = (high << bits) | (low >> (64 - bits)); - } else if(bits < 128) { + } else if (bits < 128) { result.low = 0; result.high = low << (bits - 64); } else { @@ -259,18 +259,18 @@ s128 s128::operator<<(u64 bits) s128 s128::operator>>(u64 bits) { s128 result; - if(bits == 0) { + if (bits == 0) { result.low = low; result.high = high; - } else if(bits < 64) { + } else if (bits < 64) { result.low = (low >> bits) | (high << (64 - bits)); result.high = (high >> bits) | ((high > 0x7fffffffffffffff) ? (((static_cast(1) << bits) - 1) << (64 - bits)) : 0); - } else if(bits == 64) { + } else if (bits == 64) { result.low = high; result.high = (high > 0x7fffffffffffffff) ? 0xffffffffffffffff : 0; - } else if(bits < 128) { + } else if (bits < 128) { result.low = (high >> (bits - 64)) | ((high > 0x7fffffffffffffff) ? (((static_cast(1) << (bits - 64)) - 1) << (128 - bits)) : 0); result.high = (high > 0x7fffffffffffffff) ? 0xffffffffffffffff : 0; @@ -284,10 +284,10 @@ s128 s128::operator>>(u64 bits) std::string s128::to_string() { std::string result(32, '\0'); - for(u32 i = 0; i < 16; i++) { + for (u32 i = 0; i < 16; i++) { result[i] = HEX_DIGITS[(high >> ((15 - i) * 4)) & 0xf]; } - for(u32 i = 0; i < 16; i++) { + for (u32 i = 0; i < 16; i++) { result[16 + i] = HEX_DIGITS[(low >> ((15 - i) * 4)) & 0xf]; } return result; @@ -296,25 +296,25 @@ std::string s128::to_string() std::optional s128::from_string(const std::string&& hex) { s128 result; - for(u32 i = 0; i < 16; i++) { + for (u32 i = 0; i < 16; i++) { char c = hex[i]; - if(c >= '0' && c <= '9') { + if (c >= '0' && c <= '9') { result.high |= static_cast(c - '0') << ((15 - i) * 4); - } else if(c >= 'A' && c <= 'F') { + } else if (c >= 'A' && c <= 'F') { result.high |= static_cast(10 + c - 'A') << ((15 - i) * 4); - } else if(c >= 'a' && c <= 'f') { + } else if (c >= 'a' && c <= 'f') { result.high |= static_cast(10 + c - 'a') << ((15 - i) * 4); } else { return std::nullopt; } } - for(u32 i = 0; i < 16; i++) { + for (u32 i = 0; i < 16; i++) { char c = hex[16 + i]; - if(c >= '0' && c <= '9') { + if (c >= '0' && c <= '9') { result.low |= static_cast(c - '0') << ((15 - i) * 4); - } else if(c >= 'A' && c <= 'F') { + } else if (c >= 'A' && c <= 'F') { result.low |= static_cast(10 + c - 'A') << ((15 - i) * 4); - } else if(c >= 'a' && c <= 'f') { + } else if (c >= 'a' && c <= 'f') { result.low |= static_cast(10 + c - 'a') << ((15 - i) * 4); } else { return std::nullopt; diff --git a/src/ccc/mdebug_analysis.cpp b/src/ccc/mdebug_analysis.cpp index 0309c858..cac91df1 100644 --- a/src/ccc/mdebug_analysis.cpp +++ b/src/ccc/mdebug_analysis.cpp @@ -14,7 +14,7 @@ Result LocalSymbolTableAnalyser::stab_magic(const char* magic) Result LocalSymbolTableAnalyser::source_file(const char* path, Address text_address) { - if(m_next_relative_path.empty()) { + if (m_next_relative_path.empty()) { m_next_relative_path = m_source_file.command_line_path; } @@ -27,7 +27,7 @@ Result LocalSymbolTableAnalyser::data_type(const ParsedSymbol& symbol) *symbol.name_colon_type.type.get(), nullptr, m_stabs_to_ast_state, 0, false, false); CCC_RETURN_IF_ERROR(node); - if(symbol.is_typedef && (*node)->descriptor == ast::STRUCT_OR_UNION) { + if (symbol.is_typedef && (*node)->descriptor == ast::STRUCT_OR_UNION) { ast::StructOrUnion& struct_or_union = (*node)->as(); const std::string& name = symbol.name_colon_type.name; StabsTypeNumber type_number = symbol.name_colon_type.type->type_number; @@ -41,14 +41,14 @@ Result LocalSymbolTableAnalyser::data_type(const ParsedSymbol& symbol) ((m_context.importer_flags & TYPEDEF_ALL_UNIONS) && (*node)->descriptor == ast::STRUCT_OR_UNION && !is_struct); (*node)->name = (symbol.name_colon_type.name == " ") ? "" : symbol.name_colon_type.name; - if(symbol.is_typedef || force_typedef) { + if (symbol.is_typedef || force_typedef) { (*node)->storage_class = STORAGE_CLASS_TYPEDEF; } const char* name = (*node)->name.c_str(); StabsTypeNumber number = symbol.name_colon_type.type->type_number; - if(m_context.importer_flags & DONT_DEDUPLICATE_TYPES) { + if (m_context.importer_flags & DONT_DEDUPLICATE_TYPES) { Result data_type = m_database.data_types.create_symbol( name, m_context.group.source, m_context.group.module_symbol); CCC_RETURN_IF_ERROR(data_type); @@ -79,7 +79,7 @@ Result LocalSymbolTableAnalyser::global_variable( Result> node = stabs_type_to_ast(type, nullptr, m_stabs_to_ast_state, 0, true, false); CCC_RETURN_IF_ERROR(node); - if(is_static) { + if (is_static) { (*global)->storage_class = STORAGE_CLASS_STATIC; } (*global)->set_type(std::move(*node)); @@ -91,7 +91,7 @@ Result LocalSymbolTableAnalyser::global_variable( Result LocalSymbolTableAnalyser::sub_source_file(const char* path, Address text_address) { - if(m_current_function && m_state == IN_FUNCTION_BEGINNING) { + if (m_current_function && m_state == IN_FUNCTION_BEGINNING) { Function::SubSourceFile& sub = m_current_function->sub_source_files.emplace_back(); sub.address = text_address; sub.relative_path = path; @@ -105,16 +105,16 @@ Result LocalSymbolTableAnalyser::sub_source_file(const char* path, Address Result LocalSymbolTableAnalyser::procedure( const char* mangled_name, Address address, const ProcedureDescriptor* procedure_descriptor, bool is_static) { - if(!m_current_function || strcmp(mangled_name, m_current_function->mangled_name().c_str()) != 0) { + if (!m_current_function || strcmp(mangled_name, m_current_function->mangled_name().c_str()) != 0) { Result result = create_function(mangled_name, address); CCC_RETURN_IF_ERROR(result); } - if(is_static) { + if (is_static) { m_current_function->storage_class = STORAGE_CLASS_STATIC; } - if(procedure_descriptor) { + if (procedure_descriptor) { m_current_function->stack_frame_size = procedure_descriptor->frame_size; } @@ -123,7 +123,7 @@ Result LocalSymbolTableAnalyser::procedure( Result LocalSymbolTableAnalyser::label(const char* label, Address address, s32 line_number) { - if(address.valid() && m_current_function && label[0] == '$') { + if (address.valid() && m_current_function && label[0] == '$') { Function::LineNumberPair& pair = m_current_function->line_numbers.emplace_back(); pair.address = address; pair.line_number = line_number; @@ -134,7 +134,7 @@ Result LocalSymbolTableAnalyser::label(const char* label, Address address, Result LocalSymbolTableAnalyser::text_end(const char* name, s32 function_size) { - if(m_state == IN_FUNCTION_BEGINNING) { + if (m_state == IN_FUNCTION_BEGINNING) { CCC_CHECK(m_current_function, "END TEXT symbol outside of function."); m_current_function->set_size(function_size); m_state = IN_FUNCTION_END; @@ -145,7 +145,7 @@ Result LocalSymbolTableAnalyser::text_end(const char* name, s32 function_s Result LocalSymbolTableAnalyser::function(const char* mangled_name, const StabsType& return_type, Address address) { - if(!m_current_function || strcmp(mangled_name, m_current_function->mangled_name().c_str()) != 0) { + if (!m_current_function || strcmp(mangled_name, m_current_function->mangled_name().c_str()) != 0) { Result result = create_function(mangled_name, address); CCC_RETURN_IF_ERROR(result); } else { @@ -157,7 +157,7 @@ Result LocalSymbolTableAnalyser::function(const char* mangled_name, const // well add in a hack here to deal with it. bool no_module_base_address = m_context.group.module_symbol && m_context.group.module_symbol->address().get_or_zero() == 0; bool new_address_greater = address.valid() && address > m_current_function->address(); - if(no_module_base_address && new_address_greater) { + if (no_module_base_address && new_address_greater) { m_database.functions.move_symbol(m_current_function->handle(), address); } } @@ -171,7 +171,7 @@ Result LocalSymbolTableAnalyser::function(const char* mangled_name, const Result LocalSymbolTableAnalyser::function_end() { - if(m_current_function) { + if (m_current_function) { m_current_function->set_parameter_variables(std::move(m_current_parameter_variables), m_database); m_current_function->set_local_variables(std::move(m_current_local_variables), m_database); } @@ -203,7 +203,7 @@ Result LocalSymbolTableAnalyser::parameter( CCC_RETURN_IF_ERROR(node); (*parameter_variable)->set_type(std::move(*node)); - if(is_stack) { + if (is_stack) { StackStorage& stack_storage = (*parameter_variable)->storage.emplace(); stack_storage.stack_pointer_offset = value; } else { @@ -218,7 +218,7 @@ Result LocalSymbolTableAnalyser::parameter( Result LocalSymbolTableAnalyser::local_variable( const char* name, const StabsType& type, u32 value, StabsSymbolDescriptor desc, SymbolClass sclass) { - if(!m_current_function) { + if (!m_current_function) { return Result(); } @@ -233,7 +233,7 @@ Result LocalSymbolTableAnalyser::local_variable( Result> node = stabs_type_to_ast(type, nullptr, m_stabs_to_ast_state, 0, true, false); CCC_RETURN_IF_ERROR(node); - if(desc == StabsSymbolDescriptor::STATIC_LOCAL_VARIABLE) { + if (desc == StabsSymbolDescriptor::STATIC_LOCAL_VARIABLE) { GlobalStorage& global_storage = (*local_variable)->storage.emplace(); std::optional location_opt = symbol_class_to_global_variable_location(sclass); @@ -242,10 +242,10 @@ Result LocalSymbolTableAnalyser::local_variable( symbol_class(sclass)); global_storage.location = *location_opt; (*node)->storage_class = STORAGE_CLASS_STATIC; - } else if(desc == StabsSymbolDescriptor::REGISTER_VARIABLE) { + } else if (desc == StabsSymbolDescriptor::REGISTER_VARIABLE) { RegisterStorage& register_storage = (*local_variable)->storage.emplace(); register_storage.dbx_register_number = (s32) value; - } else if(desc == StabsSymbolDescriptor::LOCAL_VARIABLE) { + } else if (desc == StabsSymbolDescriptor::LOCAL_VARIABLE) { StackStorage& stack_storage = (*local_variable)->storage.emplace(); stack_storage.stack_pointer_offset = (s32) value; } else { @@ -259,8 +259,8 @@ Result LocalSymbolTableAnalyser::local_variable( Result LocalSymbolTableAnalyser::lbrac(s32 begin_offset) { - for(LocalVariableHandle local_variable_handle : m_pending_local_variables) { - if(LocalVariable* local_variable = m_database.local_variables.symbol_from_handle(local_variable_handle)) { + for (LocalVariableHandle local_variable_handle : m_pending_local_variables) { + if (LocalVariable* local_variable = m_database.local_variables.symbol_from_handle(local_variable_handle)) { local_variable->live_range.low = m_source_file.address().value + begin_offset; } } @@ -276,8 +276,8 @@ Result LocalSymbolTableAnalyser::rbrac(s32 end_offset) CCC_CHECK(!m_blocks.empty(), "RBRAC symbol without a matching LBRAC symbol."); std::vector& variables = m_blocks.back(); - for(LocalVariableHandle local_variable_handle : variables) { - if(LocalVariable* local_variable = m_database.local_variables.symbol_from_handle(local_variable_handle)) { + for (LocalVariableHandle local_variable_handle : variables) { + if (LocalVariable* local_variable = m_database.local_variables.symbol_from_handle(local_variable_handle)) { local_variable->live_range.high = m_source_file.address().value + end_offset; } } @@ -292,7 +292,7 @@ Result LocalSymbolTableAnalyser::finish() CCC_CHECK(m_state != IN_FUNCTION_BEGINNING, "Unexpected end of symbol table for '%s'.", m_source_file.name().c_str()); - if(m_current_function) { + if (m_current_function) { Result result = function_end(); CCC_RETURN_IF_ERROR(result); } @@ -305,7 +305,7 @@ Result LocalSymbolTableAnalyser::finish() Result LocalSymbolTableAnalyser::create_function(const char* mangled_name, Address address) { - if(m_current_function) { + if (m_current_function) { Result result = function_end(); CCC_RETURN_IF_ERROR(result); } @@ -320,7 +320,7 @@ Result LocalSymbolTableAnalyser::create_function(const char* mangled_name, m_state = IN_FUNCTION_BEGINNING; - if(!m_next_relative_path.empty() && m_current_function->relative_path != m_source_file.command_line_path) { + if (!m_next_relative_path.empty() && m_current_function->relative_path != m_source_file.command_line_path) { m_current_function->relative_path = m_next_relative_path; } @@ -330,7 +330,7 @@ Result LocalSymbolTableAnalyser::create_function(const char* mangled_name, std::optional symbol_class_to_global_variable_location(SymbolClass symbol_class) { std::optional location; - switch(symbol_class) { + switch (symbol_class) { case SymbolClass::NIL: location = GlobalStorageLocation::NIL; break; case SymbolClass::DATA: location = GlobalStorageLocation::DATA; break; case SymbolClass::BSS: location = GlobalStorageLocation::BSS; break; diff --git a/src/ccc/mdebug_importer.cpp b/src/ccc/mdebug_importer.cpp index ab5c9278..20071230 100644 --- a/src/ccc/mdebug_importer.cpp +++ b/src/ccc/mdebug_importer.cpp @@ -41,12 +41,12 @@ Result import_symbol_table( // symbol table with the entries in the external symbol table. std::map external_functions; std::map external_globals; - for(const mdebug::Symbol& external : *external_symbols) { - if(external.symbol_type == mdebug::SymbolType::PROC) { + for (const mdebug::Symbol& external : *external_symbols) { + if (external.symbol_type == mdebug::SymbolType::PROC) { external_functions[external.value] = &external; } - if(external.symbol_type == mdebug::SymbolType::GLOBAL + if (external.symbol_type == mdebug::SymbolType::GLOBAL && (external.symbol_class != mdebug::SymbolClass::UNDEFINED)) { external_globals[external.string] = &external; } @@ -72,8 +72,8 @@ Result import_files(SymbolDatabase& database, const AnalysisContext& conte Result file_count = context.reader->file_count(); CCC_RETURN_IF_ERROR(file_count); - for(s32 i = 0; i < *file_count; i++) { - if(interrupt && *interrupt) { + for (s32 i = 0; i < *file_count; i++) { + if (interrupt && *interrupt) { return CCC_FAILURE("Operation interrupted by user."); } @@ -86,8 +86,8 @@ Result import_files(SymbolDatabase& database, const AnalysisContext& conte // The files field may be modified by further analysis passes, so we // need to save this information here. - for(DataType& data_type : database.data_types) { - if(context.group.is_in_group(data_type) && data_type.files.size() == 1) { + for (DataType& data_type : database.data_types) { + if (context.group.is_in_group(data_type) && data_type.files.size() == 1) { data_type.only_defined_in_single_translation_unit = true; } } @@ -98,41 +98,41 @@ Result import_files(SymbolDatabase& database, const AnalysisContext& conte // Compute the size in bytes of all the AST nodes. database.for_each_symbol([&](ccc::Symbol& symbol) { - if(context.group.is_in_group(symbol) && symbol.type()) { + if (context.group.is_in_group(symbol) && symbol.type()) { compute_size_bytes(*symbol.type(), database); } }); // Propagate the size information to the global variable symbols. - for(GlobalVariable& global_variable : database.global_variables) { - if(global_variable.type() && global_variable.type()->size_bytes > -1) { + for (GlobalVariable& global_variable : database.global_variables) { + if (global_variable.type() && global_variable.type()->size_bytes > -1) { global_variable.set_size((u32) global_variable.type()->size_bytes); } } // Propagate the size information to the static local variable symbols. - for(LocalVariable& local_variable : database.local_variables) { + for (LocalVariable& local_variable : database.local_variables) { bool is_static_local = std::holds_alternative(local_variable.storage); - if(is_static_local && local_variable.type() && local_variable.type()->size_bytes > -1) { + if (is_static_local && local_variable.type() && local_variable.type()->size_bytes > -1) { local_variable.set_size((u32) local_variable.type()->size_bytes); } } // Some games (e.g. Jet X2O) have multiple function symbols across different // translation units with the same name and address. - if(context.importer_flags & UNIQUE_FUNCTIONS) { + if (context.importer_flags & UNIQUE_FUNCTIONS) { detect_duplicate_functions(database, context.group); } // If multiple functions appear at the same address, discard the addresses // of all of them except the real one. - if(context.external_functions) { + if (context.external_functions) { detect_fake_functions(database, *context.external_functions, context.group); } // Remove functions with no address. If there are any such functions, this // will invalidate all pointers to symbols. - if(context.importer_flags & NO_OPTIMIZED_OUT_FUNCTIONS) { + if (context.importer_flags & NO_OPTIMIZED_OUT_FUNCTIONS) { destroy_optimized_out_functions(database, context.group); } @@ -150,8 +150,8 @@ Result import_file(SymbolDatabase& database, const mdebug::File& input, co // In stabs, types can be referenced by their number from other stabs, // so here we build a map of type numbers to the parsed types. std::map stabs_types; - for(const ParsedSymbol& symbol : *symbols) { - if(symbol.type == ParsedSymbolType::NAME_COLON_TYPE) { + for (const ParsedSymbol& symbol : *symbols) { + if (symbol.type == ParsedSymbolType::NAME_COLON_TYPE) { symbol.name_colon_type.type->enumerate_numbered_types(stabs_types); } } @@ -165,8 +165,8 @@ Result import_file(SymbolDatabase& database, const mdebug::File& input, co // Sometimes the INFO symbols contain information about what toolchain // version was used for building the executable. - for(const mdebug::Symbol& symbol : input.symbols) { - if(symbol.symbol_class == mdebug::SymbolClass::INFO && strcmp(symbol.string, "@stabs") != 0) { + for (const mdebug::Symbol& symbol : input.symbols) { + if (symbol.symbol_class == mdebug::SymbolClass::INFO && strcmp(symbol.string, "@stabs") != 0) { (*source_file)->toolchain_version_info.emplace(symbol.string); } } @@ -179,14 +179,14 @@ Result import_file(SymbolDatabase& database, const mdebug::File& input, co // Convert the parsed stabs symbols to a more standard C AST. LocalSymbolTableAnalyser analyser(database, stabs_to_ast_state, context, **source_file); - for(const ParsedSymbol& symbol : *symbols) { - if(symbol.duplicate) { + for (const ParsedSymbol& symbol : *symbols) { + if (symbol.duplicate) { continue; } - switch(symbol.type) { + switch (symbol.type) { case ParsedSymbolType::NAME_COLON_TYPE: { - switch(symbol.name_colon_type.descriptor) { + switch (symbol.name_colon_type.descriptor) { case StabsSymbolDescriptor::LOCAL_FUNCTION: case StabsSymbolDescriptor::GLOBAL_FUNCTION: { const char* name = symbol.name_colon_type.name.c_str(); @@ -225,14 +225,14 @@ Result import_file(SymbolDatabase& database, const mdebug::File& input, co u32 address = -1; std::optional location = symbol_class_to_global_variable_location(symbol.raw->symbol_class); - if(symbol.name_colon_type.descriptor == StabsSymbolDescriptor::GLOBAL_VARIABLE) { + if (symbol.name_colon_type.descriptor == StabsSymbolDescriptor::GLOBAL_VARIABLE) { // The address for non-static global variables is // only stored in the external symbol table (and // the ELF symbol table), so we pull that // information in here. - if(context.external_globals) { + if (context.external_globals) { auto global_symbol = context.external_globals->find(symbol.name_colon_type.name); - if(global_symbol != context.external_globals->end()) { + if (global_symbol != context.external_globals->end()) { address = (u32) global_symbol->second->value; location = symbol_class_to_global_variable_location(global_symbol->second->symbol_class); } @@ -284,17 +284,17 @@ Result import_file(SymbolDatabase& database, const mdebug::File& input, co break; } case ParsedSymbolType::NON_STABS: { - if(symbol.raw->symbol_class == mdebug::SymbolClass::TEXT) { - if(symbol.raw->symbol_type == mdebug::SymbolType::PROC) { + if (symbol.raw->symbol_class == mdebug::SymbolClass::TEXT) { + if (symbol.raw->symbol_type == mdebug::SymbolType::PROC) { Result result = analyser.procedure(symbol.raw->string, symbol.raw->value, symbol.raw->procedure_descriptor, false); CCC_RETURN_IF_ERROR(result); - } else if(symbol.raw->symbol_type == mdebug::SymbolType::STATICPROC) { + } else if (symbol.raw->symbol_type == mdebug::SymbolType::STATICPROC) { Result result = analyser.procedure(symbol.raw->string, symbol.raw->value, symbol.raw->procedure_descriptor, true); CCC_RETURN_IF_ERROR(result); - } else if(symbol.raw->symbol_type == mdebug::SymbolType::LABEL) { + } else if (symbol.raw->symbol_type == mdebug::SymbolType::LABEL) { Result result = analyser.label(symbol.raw->string, symbol.raw->value, symbol.raw->index); CCC_RETURN_IF_ERROR(result); - } else if(symbol.raw->symbol_type == mdebug::SymbolType::END) { + } else if (symbol.raw->symbol_type == mdebug::SymbolType::END) { Result result = analyser.text_end(symbol.raw->string, symbol.raw->value); CCC_RETURN_IF_ERROR(result); } @@ -315,11 +315,11 @@ static Result resolve_type_names( { Result result; database.for_each_symbol([&](ccc::Symbol& symbol) { - if(group.is_in_group(symbol) && symbol.type()) { + if (group.is_in_group(symbol) && symbol.type()) { ast::for_each_node(*symbol.type(), ast::PREORDER_TRAVERSAL, [&](ast::Node& node) { - if(node.descriptor == ast::TYPE_NAME) { + if (node.descriptor == ast::TYPE_NAME) { Result type_name_result = resolve_type_name(node.as(), database, group, importer_flags); - if(!type_name_result.success()) { + if (!type_name_result.success()) { result = std::move(type_name_result); } } @@ -337,17 +337,17 @@ static Result resolve_type_name( u32 importer_flags) { ast::TypeName::UnresolvedStabs* unresolved_stabs = type_name.unresolved_stabs.get(); - if(!unresolved_stabs) { + if (!unresolved_stabs) { return Result(); } // Lookup the type by its STABS type number. This path ensures that the // correct type is found even if multiple types have the same name. - if(unresolved_stabs->referenced_file_handle != SourceFileHandle() && unresolved_stabs->stabs_type_number.valid()) { + if (unresolved_stabs->referenced_file_handle != SourceFileHandle() && unresolved_stabs->stabs_type_number.valid()) { const SourceFile* source_file = database.source_files.symbol_from_handle(unresolved_stabs->referenced_file_handle); CCC_ASSERT(source_file); auto handle = source_file->stabs_type_number_to_handle.find(unresolved_stabs->stabs_type_number); - if(handle != source_file->stabs_type_number_to_handle.end()) { + if (handle != source_file->stabs_type_number_to_handle.end()) { type_name.data_type_handle = handle->second; type_name.is_forward_declared = false; type_name.unresolved_stabs.reset(); @@ -358,10 +358,10 @@ static Result resolve_type_name( // Looking up the type by its STABS type number failed, so look for it by // its name instead. This happens when a type is forward declared but not // defined in a given translation unit. - if(!unresolved_stabs->type_name.empty()) { - for(DataTypeHandle& handle : database.data_types.handles_from_name(unresolved_stabs->type_name)) { + if (!unresolved_stabs->type_name.empty()) { + for (DataTypeHandle& handle : database.data_types.handles_from_name(unresolved_stabs->type_name)) { DataType* data_type = database.data_types.symbol_from_handle(handle); - if(data_type && group.is_in_group(*data_type)) { + if (data_type && group.is_in_group(*data_type)) { type_name.data_type_handle = handle; type_name.is_forward_declared = true; type_name.unresolved_stabs.reset(); @@ -374,7 +374,7 @@ static Result resolve_type_name( // automatically generated member function of a nested struct trying to // reference the struct (for the this parameter). We shouldn't create a // forward declared type in this case. - if(type_name.source == ast::TypeNameSource::UNNAMED_THIS) { + if (type_name.source == ast::TypeNameSource::UNNAMED_THIS) { return Result(); } @@ -382,8 +382,8 @@ static Result resolve_type_name( // translation unit with symbols but is not defined in one. We haven't // already created a forward declared type, so we create one now. std::unique_ptr forward_declared_node; - if(unresolved_stabs->type.has_value()) { - switch(*unresolved_stabs->type) { + if (unresolved_stabs->type.has_value()) { + switch (*unresolved_stabs->type) { case ast::ForwardDeclaredType::STRUCT: { std::unique_ptr node = std::make_unique(); node->is_struct = true; @@ -404,7 +404,7 @@ static Result resolve_type_name( } } - if(forward_declared_node) { + if (forward_declared_node) { Result forward_declared_type = database.data_types.create_symbol( unresolved_stabs->type_name, group.source, group.module_symbol); CCC_RETURN_IF_ERROR(forward_declared_type); @@ -420,7 +420,7 @@ static Result resolve_type_name( } const char* error_message = "Unresolved %s type name '%s' with STABS type number (%d,%d)."; - if(importer_flags & STRICT_PARSING) { + if (importer_flags & STRICT_PARSING) { return CCC_FAILURE(error_message, ast::type_name_source_to_string(type_name.source), type_name.unresolved_stabs->type_name.c_str(), @@ -441,17 +441,17 @@ static void compute_size_bytes(ast::Node& node, SymbolDatabase& database) { for_each_node(node, ast::POSTORDER_TRAVERSAL, [&](ast::Node& node) { // Skip nodes that have already been processed. - if(node.size_bytes > -1 || node.cannot_compute_size) { + if (node.size_bytes > -1 || node.cannot_compute_size) { return ast::EXPLORE_CHILDREN; } // Can't compute size recursively. node.cannot_compute_size = true; - switch(node.descriptor) { + switch (node.descriptor) { case ast::ARRAY: { ast::Array& array = node.as(); - if(array.element_type->size_bytes > -1) { + if (array.element_type->size_bytes > -1) { array.size_bytes = array.element_type->size_bytes * array.element_count; } break; @@ -488,10 +488,10 @@ static void compute_size_bytes(ast::Node& node, SymbolDatabase& database) case ast::TYPE_NAME: { ast::TypeName& type_name = node.as(); DataType* resolved_type = database.data_types.symbol_from_handle(type_name.data_type_handle_unless_forward_declared()); - if(resolved_type) { + if (resolved_type) { ast::Node* resolved_node = resolved_type->type(); CCC_ASSERT(resolved_node); - if(resolved_node->size_bytes < 0 && !resolved_node->cannot_compute_size) { + if (resolved_node->size_bytes < 0 && !resolved_node->cannot_compute_size) { compute_size_bytes(*resolved_node, database); } type_name.size_bytes = resolved_node->size_bytes; @@ -500,7 +500,7 @@ static void compute_size_bytes(ast::Node& node, SymbolDatabase& database) } } - if(node.size_bytes > -1) { + if (node.size_bytes > -1) { node.cannot_compute_size = false; } @@ -512,17 +512,17 @@ static void detect_duplicate_functions(SymbolDatabase& database, const SymbolGro { std::vector duplicate_functions; - for(Function& test_function : database.functions) { - if(!test_function.address().valid() && !group.is_in_group(test_function)) { + for (Function& test_function : database.functions) { + if (!test_function.address().valid() && !group.is_in_group(test_function)) { continue; } // Find cases where there are two or more functions at the same address. auto functions_with_same_address = database.functions.handles_from_starting_address(test_function.address()); - if(functions_with_same_address.begin() == functions_with_same_address.end()) { + if (functions_with_same_address.begin() == functions_with_same_address.end()) { continue; } - if(++functions_with_same_address.begin() == functions_with_same_address.end()) { + if (++functions_with_same_address.begin() == functions_with_same_address.end()) { continue; } @@ -531,13 +531,13 @@ static void detect_duplicate_functions(SymbolDatabase& database, const SymbolGro // comes from. We can't just check which source file the symbol comes // from because it may be present in multiple. u32 source_file_address = UINT32_MAX; - for(SourceFile& source_file : database.source_files) { - if(source_file.address() < test_function.address()) { + for (SourceFile& source_file : database.source_files) { + if (source_file.address() < test_function.address()) { source_file_address = std::min(source_file.address().value, source_file_address); } } - if(source_file_address == UINT32_MAX) { + if (source_file_address == UINT32_MAX) { continue; } @@ -545,14 +545,14 @@ static void detect_duplicate_functions(SymbolDatabase& database, const SymbolGro // translation units. FunctionHandle best_handle; u32 best_offset = UINT32_MAX; - for(FunctionHandle handle : functions_with_same_address) { + for (FunctionHandle handle : functions_with_same_address) { ccc::Function* function = database.functions.symbol_from_handle(handle); - if(!function || !group.is_in_group(*function) || function->mangled_name() != test_function.mangled_name()) { + if (!function || !group.is_in_group(*function) || function->mangled_name() != test_function.mangled_name()) { continue; } - if(test_function.address().value - source_file_address < best_offset) { - if(best_handle.valid()) { + if (test_function.address().value - source_file_address < best_offset) { + if (best_handle.valid()) { duplicate_functions.emplace_back(best_handle); } best_handle = function->handle(); @@ -562,7 +562,7 @@ static void detect_duplicate_functions(SymbolDatabase& database, const SymbolGro } } - for(FunctionHandle duplicate_function : duplicate_functions) { + for (FunctionHandle duplicate_function : duplicate_functions) { database.functions.move_symbol(duplicate_function, Address()); } duplicate_functions.clear(); @@ -575,27 +575,27 @@ static void detect_fake_functions(SymbolDatabase& database, const std::mapsecond->string) != 0) { + if (external_function == external_functions.end() || strcmp(function.mangled_name().c_str(), external_function->second->string) != 0) { database.functions.move_symbol(function.handle(), Address()); - if(fake_function_count < 10) { + if (fake_function_count < 10) { CCC_WARN("Discarding address of function symbol '%s' as it is probably incorrect.", function.mangled_name().c_str()); - } else if(fake_function_count == 10) { + } else if (fake_function_count == 10) { CCC_WARN("Discarding more addresses of function symbols."); } @@ -609,14 +609,14 @@ static void destroy_optimized_out_functions( { bool marked = false; - for(Function& function : database.functions) { - if(group.is_in_group(function) && !function.address().valid()) { + for (Function& function : database.functions) { + if (group.is_in_group(function) && !function.address().valid()) { function.mark_for_destruction(); marked = true; } } - if(marked) { + if (marked) { // This will invalidate all pointers to symbols in the database. database.destroy_marked_symbols(); } @@ -625,10 +625,10 @@ static void destroy_optimized_out_functions( void fill_in_pointers_to_member_function_definitions(SymbolDatabase& database) { // Fill in pointers from member function declaration to corresponding definitions. - for(Function& function : database.functions) { + for (Function& function : database.functions) { const std::string& qualified_name = function.name(); std::string::size_type name_separator_pos = qualified_name.find_last_of("::"); - if(name_separator_pos == std::string::npos || name_separator_pos < 2) { + if (name_separator_pos == std::string::npos || name_separator_pos < 2) { continue; } @@ -637,28 +637,28 @@ void fill_in_pointers_to_member_function_definitions(SymbolDatabase& database) // This won't work for some template types. std::string::size_type type_separator_pos = qualified_name.find_last_of("::", name_separator_pos - 2); std::string type_name; - if(type_separator_pos != std::string::npos) { + if (type_separator_pos != std::string::npos) { type_name = qualified_name.substr(type_separator_pos + 1, name_separator_pos - type_separator_pos - 2); } else { type_name = qualified_name.substr(0, name_separator_pos - 1); } - for(DataTypeHandle handle : database.data_types.handles_from_name(type_name)) { + for (DataTypeHandle handle : database.data_types.handles_from_name(type_name)) { DataType* data_type = database.data_types.symbol_from_handle(handle); - if(!data_type || !data_type->type() || data_type->type()->descriptor != ast::STRUCT_OR_UNION) { + if (!data_type || !data_type->type() || data_type->type()->descriptor != ast::STRUCT_OR_UNION) { continue; } ast::StructOrUnion& struct_or_union = data_type->type()->as(); - for(std::unique_ptr& declaration : struct_or_union.member_functions) { - if(declaration->name == function_name) { + for (std::unique_ptr& declaration : struct_or_union.member_functions) { + if (declaration->name == function_name) { declaration->as().definition_handle = function.handle().value; function.is_member_function_ish = true; break; } } - if(function.is_member_function_ish) { + if (function.is_member_function_ish) { break; } } diff --git a/src/ccc/mdebug_section.cpp b/src/ccc/mdebug_section.cpp index e5b574de..42f7ff1b 100644 --- a/src/ccc/mdebug_section.cpp +++ b/src/ccc/mdebug_section.cpp @@ -138,12 +138,12 @@ Result SymbolTableReader::parse_file(s32 index) const s32 rel_raw_path_offset = fd_header->strings_offset + fd_header->file_path_string_offset; s32 raw_path_offset = m_hdrr->local_strings_offset + rel_raw_path_offset + m_fudge_offset; const char* command_line_path = get_string(m_elf, raw_path_offset); - if(command_line_path) { + if (command_line_path) { file.command_line_path = command_line_path; } // Parse local symbols. - for(s64 j = 0; j < fd_header->symbol_count; j++) { + for (s64 j = 0; j < fd_header->symbol_count; j++) { u64 rel_symbol_offset = (fd_header->isym_base + j) * sizeof(SymbolHeader); u64 symbol_offset = m_hdrr->local_symbols_offset + rel_symbol_offset + m_fudge_offset; const SymbolHeader* symbol_header = get_packed(m_elf, symbol_offset); @@ -154,9 +154,9 @@ Result SymbolTableReader::parse_file(s32 index) const CCC_RETURN_IF_ERROR(sym); bool string_offset_equal = (s32) symbol_header->iss == fd_header->file_path_string_offset; - if(file.working_dir.empty() && string_offset_equal && sym->is_stabs() && sym->code() == N_SO && file.symbols.size() > 2) { + if (file.working_dir.empty() && string_offset_equal && sym->is_stabs() && sym->code() == N_SO && file.symbols.size() > 2) { const Symbol& working_dir = file.symbols.back(); - if(working_dir.is_stabs() && working_dir.code() == N_SO) { + if (working_dir.is_stabs() && working_dir.code() == N_SO) { file.working_dir = working_dir.string; } } @@ -165,7 +165,7 @@ Result SymbolTableReader::parse_file(s32 index) const } // Parse procedure descriptors. - for(s64 i = 0; i < fd_header->procedure_descriptor_count; i++) { + for (s64 i = 0; i < fd_header->procedure_descriptor_count; i++) { u64 rel_procedure_offset = (fd_header->ipd_first + i) * sizeof(ProcedureDescriptor); u64 procedure_offset = m_hdrr->procedure_descriptors_offset + rel_procedure_offset + m_fudge_offset; const ProcedureDescriptor* procedure_descriptor = get_packed(m_elf, procedure_offset); @@ -186,7 +186,7 @@ Result> SymbolTableReader::parse_external_symbols() const CCC_ASSERT(m_ready); std::vector external_symbols; - for(s64 i = 0; i < m_hdrr->external_symbols_count; i++) { + for (s64 i = 0; i < m_hdrr->external_symbols_count; i++) { u64 sym_offset = m_hdrr->external_symbols_offset + i * sizeof(ExternalSymbolHeader); const ExternalSymbolHeader* external_header = get_packed(m_elf, sym_offset + m_fudge_offset); CCC_CHECK(external_header != nullptr, "External header out of bounds."); @@ -254,29 +254,29 @@ void SymbolTableReader::print_header(FILE* dest) const Result SymbolTableReader::print_symbols(FILE* out, bool print_locals, bool print_procedure_descriptors, bool print_externals) const { - if(print_locals || print_procedure_descriptors) { + if (print_locals || print_procedure_descriptors) { s32 count = file_count(); - for(s32 i = 0; i < count; i++) { + for (s32 i = 0; i < count; i++) { Result file = parse_file(i); CCC_RETURN_IF_ERROR(file); fprintf(out, "FILE %s:\n", file->command_line_path.c_str()); - for(const Symbol& symbol : file->symbols) { - if(print_locals || symbol.procedure_descriptor) { + for (const Symbol& symbol : file->symbols) { + if (print_locals || symbol.procedure_descriptor) { print_symbol(out, symbol); } - if(print_procedure_descriptors && symbol.procedure_descriptor) { + if (print_procedure_descriptors && symbol.procedure_descriptor) { print_procedure_descriptor(out, *symbol.procedure_descriptor); } } } } - if(print_externals) { + if (print_externals) { fprintf(out, "EXTERNAL SYMBOLS:\n"); Result> external_symbols = parse_external_symbols(); CCC_RETURN_IF_ERROR(external_symbols); - for(const Symbol& symbol : *external_symbols) { + for (const Symbol& symbol : *external_symbols) { print_symbol(out, symbol); } } @@ -289,14 +289,14 @@ static void print_symbol(FILE* out, const Symbol& symbol) fprintf(out, " %8x ", symbol.value); const char* symbol_type_str = symbol_type(symbol.symbol_type); - if(symbol_type_str) { + if (symbol_type_str) { fprintf(out, "%-11s ", symbol_type_str); } else { fprintf(out, "ST(%7u) ", (u32) symbol.symbol_type); } const char* symbol_class_str = symbol_class(symbol.symbol_class); - if(symbol_class_str) { + if (symbol_class_str) { fprintf(out, "%-4s ", symbol_class_str); } else if ((u32) symbol.symbol_class == 0) { fprintf(out, " "); @@ -304,7 +304,7 @@ static void print_symbol(FILE* out, const Symbol& symbol) fprintf(out, "SC(%4u) ", (u32) symbol.symbol_class); } - if(symbol.is_stabs()) { + if (symbol.is_stabs()) { fprintf(out, "%-8s ", stabs_code_to_string(symbol.code())); } else { fprintf(out, "SI(%4u) ", symbol.index); @@ -337,23 +337,23 @@ static Result get_corruption_fixing_fudge_offset(s32 section_offset, const // header, so if the header says it's somewhere else we know the section has // probably been moved without updating its contents. s32 right_after_header = INT32_MAX; - if(hdrr.line_numbers_offset > 0) right_after_header = std::min(hdrr.line_numbers_offset, right_after_header); - if(hdrr.dense_numbers_offset > 0) right_after_header = std::min(hdrr.dense_numbers_offset, right_after_header); - if(hdrr.procedure_descriptors_offset > 0) right_after_header = std::min(hdrr.procedure_descriptors_offset, right_after_header); - if(hdrr.local_symbols_offset > 0) right_after_header = std::min(hdrr.local_symbols_offset, right_after_header); - if(hdrr.optimization_symbols_offset > 0) right_after_header = std::min(hdrr.optimization_symbols_offset, right_after_header); - if(hdrr.auxiliary_symbols_offset > 0) right_after_header = std::min(hdrr.auxiliary_symbols_offset, right_after_header); - if(hdrr.local_strings_offset > 0) right_after_header = std::min(hdrr.local_strings_offset, right_after_header); - if(hdrr.external_strings_offset > 0) right_after_header = std::min(hdrr.external_strings_offset, right_after_header); - if(hdrr.file_descriptors_offset > 0) right_after_header = std::min(hdrr.file_descriptors_offset, right_after_header); - if(hdrr.relative_file_descriptors_offset > 0) right_after_header = std::min(hdrr.relative_file_descriptors_offset, right_after_header); - if(hdrr.external_symbols_offset > 0) right_after_header = std::min(hdrr.external_symbols_offset, right_after_header); + if (hdrr.line_numbers_offset > 0) right_after_header = std::min(hdrr.line_numbers_offset, right_after_header); + if (hdrr.dense_numbers_offset > 0) right_after_header = std::min(hdrr.dense_numbers_offset, right_after_header); + if (hdrr.procedure_descriptors_offset > 0) right_after_header = std::min(hdrr.procedure_descriptors_offset, right_after_header); + if (hdrr.local_symbols_offset > 0) right_after_header = std::min(hdrr.local_symbols_offset, right_after_header); + if (hdrr.optimization_symbols_offset > 0) right_after_header = std::min(hdrr.optimization_symbols_offset, right_after_header); + if (hdrr.auxiliary_symbols_offset > 0) right_after_header = std::min(hdrr.auxiliary_symbols_offset, right_after_header); + if (hdrr.local_strings_offset > 0) right_after_header = std::min(hdrr.local_strings_offset, right_after_header); + if (hdrr.external_strings_offset > 0) right_after_header = std::min(hdrr.external_strings_offset, right_after_header); + if (hdrr.file_descriptors_offset > 0) right_after_header = std::min(hdrr.file_descriptors_offset, right_after_header); + if (hdrr.relative_file_descriptors_offset > 0) right_after_header = std::min(hdrr.relative_file_descriptors_offset, right_after_header); + if (hdrr.external_symbols_offset > 0) right_after_header = std::min(hdrr.external_symbols_offset, right_after_header); CCC_CHECK(right_after_header >= 0 && right_after_header < INT32_MAX, "Invalid symbolic header."); // Figure out how much we need to adjust all the file offsets by. s32 fudge_offset = section_offset - (right_after_header - sizeof(SymbolicHeader)); - if(fudge_offset != 0) { + if (fudge_offset != 0) { CCC_WARN("The .mdebug section was moved without updating its contents. Adjusting file offsets by %d bytes.", fudge_offset); } @@ -373,7 +373,7 @@ static Result get_symbol(const SymbolHeader& header, std::span symbol.symbol_class = static_cast(header.symbol_class()); symbol.index = header.index(); - if(symbol.is_stabs()) { + if (symbol.is_stabs()) { CCC_CHECK(stabs_code_to_string(symbol.code()) != nullptr, "Bad stabs symbol code '%x'.", symbol.code()); } @@ -382,7 +382,7 @@ static Result get_symbol(const SymbolHeader& header, std::span const char* symbol_type(SymbolType type) { - switch(type) { + switch (type) { case SymbolType::NIL: return "NIL"; case SymbolType::GLOBAL: return "GLOBAL"; case SymbolType::STATIC: return "STATIC"; @@ -403,7 +403,7 @@ const char* symbol_type(SymbolType type) const char* symbol_class(SymbolClass symbol_class) { - switch(symbol_class) { + switch (symbol_class) { case SymbolClass::NIL: return "NIL"; case SymbolClass::TEXT: return "TEXT"; case SymbolClass::DATA: return "DATA"; @@ -438,7 +438,7 @@ const char* symbol_class(SymbolClass symbol_class) const char* stabs_code_to_string(StabsCode code) { - switch(code) { + switch (code) { case STAB: return "STAB"; case N_GSYM: return "GSYM"; case N_FNAME: return "FNAME"; diff --git a/src/ccc/mdebug_symbols.cpp b/src/ccc/mdebug_symbols.cpp index eacd9bf8..104f8bf7 100644 --- a/src/ccc/mdebug_symbols.cpp +++ b/src/ccc/mdebug_symbols.cpp @@ -13,9 +13,9 @@ Result> parse_symbols(const std::vector output; std::string prefix; - for(const mdebug::Symbol& symbol : input) { - if(symbol.is_stabs()) { - switch(symbol.code()) { + for (const mdebug::Symbol& symbol : input) { + if (symbol.is_stabs()) { + switch (symbol.code()) { case mdebug::N_GSYM: // Global variable case mdebug::N_FUN: // Function case mdebug::N_STSYM: // Data section static global variable @@ -24,13 +24,13 @@ Result> parse_symbols(const std::vector> parse_symbols(const std::vector parse_result = parse_stabs_symbol(input); - if(parse_result.success()) { - if(*input != '\0') { - if(importer_flags & STRICT_PARSING) { + if (parse_result.success()) { + if (*input != '\0') { + if (importer_flags & STRICT_PARSING) { return CCC_FAILURE("Unknown data '%s' at the end of the '%s' stab.", input, parse_result->name.c_str()); } else { CCC_WARN("Unknown data '%s' at the end of the '%s' stab.", input, parse_result->name.c_str()); @@ -53,7 +53,7 @@ Result> parse_symbols(const std::vector> parse_symbols(const std::vector> parse_symbols(const std::vector& symbols) { std::map stabs_type_number_to_symbol; - for(size_t i = 0; i < symbols.size(); i++) { + for (size_t i = 0; i < symbols.size(); i++) { ParsedSymbol& symbol = symbols[i]; - if(symbol.type == ParsedSymbolType::NAME_COLON_TYPE) { + if (symbol.type == ParsedSymbolType::NAME_COLON_TYPE) { StabsType& type = *symbol.name_colon_type.type; - if(type.type_number.valid() && type.descriptor.has_value()) { + if (type.type_number.valid() && type.descriptor.has_value()) { stabs_type_number_to_symbol.emplace(type.type_number, i); } } } - for(ParsedSymbol& symbol : symbols) { + for (ParsedSymbol& symbol : symbols) { symbol.is_typedef = symbol.type == ParsedSymbolType::NAME_COLON_TYPE && symbol.name_colon_type.descriptor == StabsSymbolDescriptor::TYPE_NAME && symbol.name_colon_type.type->descriptor != StabsTypeDescriptor::ENUM; } - for(size_t i = 0; i < symbols.size(); i++) { + for (size_t i = 0; i < symbols.size(); i++) { ParsedSymbol& symbol = symbols[i]; - if(symbol.type != ParsedSymbolType::NAME_COLON_TYPE) { + if (symbol.type != ParsedSymbolType::NAME_COLON_TYPE) { continue; } bool is_type = symbol.name_colon_type.descriptor == StabsSymbolDescriptor::TYPE_NAME || symbol.name_colon_type.descriptor == StabsSymbolDescriptor::ENUM_STRUCT_OR_TYPE_TAG; - if(!is_type) { + if (!is_type) { continue; } StabsType& type = *symbol.name_colon_type.type; - if(!type.descriptor.has_value()) { + if (!type.descriptor.has_value()) { auto referenced_index = stabs_type_number_to_symbol.find(type.type_number); - if(referenced_index != stabs_type_number_to_symbol.end()) { + if (referenced_index != stabs_type_number_to_symbol.end()) { ParsedSymbol& referenced = symbols[referenced_index->second]; - if(referenced.name_colon_type.name == symbol.name_colon_type.name) { + if (referenced.name_colon_type.name == symbol.name_colon_type.name) { // symbol: "Struct:T(1,1)=s1;" // referenced: "Struct:t(1,1)" symbol.duplicate = true; @@ -193,12 +193,12 @@ static void mark_duplicate_symbols(std::vector& symbols) } } - if(type.descriptor.has_value() && type.descriptor == StabsTypeDescriptor::TYPE_REFERENCE) { + if (type.descriptor.has_value() && type.descriptor == StabsTypeDescriptor::TYPE_REFERENCE) { auto referenced_index = stabs_type_number_to_symbol.find(type.as().type->type_number); - if(referenced_index != stabs_type_number_to_symbol.end() && referenced_index->second != i) { + if (referenced_index != stabs_type_number_to_symbol.end() && referenced_index->second != i) { ParsedSymbol& referenced = symbols[referenced_index->second]; - if(referenced.name_colon_type.name == " ") { + if (referenced.name_colon_type.name == " ") { // referenced: " :T(1,1)=e;" // symbol: "ErraticEnum:t(1,2)=(1,1)" referenced.name_colon_type.name = symbol.name_colon_type.name; @@ -206,7 +206,7 @@ static void mark_duplicate_symbols(std::vector& symbols) symbol.duplicate = true; } - if(referenced.name_colon_type.name == symbol.name_colon_type.name) { + if (referenced.name_colon_type.name == symbol.name_colon_type.name) { // referenced: "NamedTypedefedStruct:T(1,1)=s1;" // symbol: "NamedTypedefedStruct:t(1,2)=(1,1)" referenced.is_typedef = true; diff --git a/src/ccc/print_cpp.cpp b/src/ccc/print_cpp.cpp index bbbf3c16..d1453a60 100644 --- a/src/ccc/print_cpp.cpp +++ b/src/ccc/print_cpp.cpp @@ -23,14 +23,14 @@ static void indent(FILE* out, s32 level); void CppPrinter::comment_block_beginning(const char* input_file, const char* tool_name, const char* tool_version) { - if(m_has_anything_been_printed) { + if (m_has_anything_been_printed) { fprintf(out, "\n"); } fprintf(out, "// File written by %s%s%s", tool_name, (tool_name && tool_version) ? " " : "", tool_version); time_t cftime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); tm* t = std::localtime(&cftime); - if(t) { + if (t) { fprintf(out, " on %04d-%02d-%02d", 1900 + t->tm_year, t->tm_mon + 1, t->tm_mday); } fprintf(out, "\n"); @@ -45,9 +45,9 @@ void CppPrinter::comment_block_beginning(const char* input_file, const char* too void CppPrinter::comment_block_toolchain_version_info(const SymbolDatabase& database) { std::set toolchain_version_info; - for(const SourceFile& source_file : database.source_files) { - if(!source_file.toolchain_version_info.empty()) { - for(const std::string& string : source_file.toolchain_version_info) { + for (const SourceFile& source_file : database.source_files) { + if (!source_file.toolchain_version_info.empty()) { + for (const std::string& string : source_file.toolchain_version_info) { toolchain_version_info.emplace(string); } } else { @@ -56,7 +56,7 @@ void CppPrinter::comment_block_toolchain_version_info(const SymbolDatabase& data } fprintf(out, "// Toolchain version(s):\n"); - for(const std::string& string : toolchain_version_info) { + for (const std::string& string : toolchain_version_info) { fprintf(out, "// %s\n", string.c_str()); } @@ -67,17 +67,17 @@ void CppPrinter::comment_block_toolchain_version_info(const SymbolDatabase& data void CppPrinter::comment_block_builtin_types(const SymbolDatabase& database, SourceFileHandle file) { std::set> builtins; - for(const DataType& data_type : database.data_types) { + for (const DataType& data_type : database.data_types) { CCC_ASSERT(data_type.type()); - if(data_type.type()->descriptor == ast::BUILTIN && (!file.valid() || (data_type.files.size() == 0 && data_type.files[0] == file))) { + if (data_type.type()->descriptor == ast::BUILTIN && (!file.valid() || (data_type.files.size() == 0 && data_type.files[0] == file))) { builtins.emplace(data_type.type()->name, data_type.type()->as().bclass); } } - if(!builtins.empty()) { + if (!builtins.empty()) { fprintf(out, "// Built-in types:\n"); - for(const auto& [type, bclass] : builtins) { + for (const auto& [type, bclass] : builtins) { fprintf(out, "// %-25s%s\n", type.c_str(), ast::builtin_class_to_string(bclass)); } } @@ -88,7 +88,7 @@ void CppPrinter::comment_block_builtin_types(const SymbolDatabase& database, Sou void CppPrinter::comment_block_file(const char* path) { - if(m_has_anything_been_printed) { + if (m_has_anything_been_printed) { fprintf(out, "\n"); } @@ -103,7 +103,7 @@ void CppPrinter::comment_block_file(const char* path) void CppPrinter::begin_include_guard(const char* macro) { - if(m_has_anything_been_printed) { + if (m_has_anything_been_printed) { fprintf(out, "\n"); } @@ -116,7 +116,7 @@ void CppPrinter::begin_include_guard(const char* macro) void CppPrinter::end_include_guard(const char* macro) { - if(m_has_anything_been_printed) { + if (m_has_anything_been_printed) { fprintf(out, "\n"); } @@ -128,7 +128,7 @@ void CppPrinter::end_include_guard(const char* macro) void CppPrinter::include_directive(const char* path) { - if(m_has_anything_been_printed) { + if (m_has_anything_been_printed) { fprintf(out, "\n"); } @@ -143,23 +143,23 @@ bool CppPrinter::data_type(const DataType& symbol, const SymbolDatabase& databas CCC_ASSERT(symbol.type()); const ast::Node& node = *symbol.type(); - if(node.descriptor == ast::BUILTIN) { + if (node.descriptor == ast::BUILTIN) { return false; } bool wants_spacing = !symbol.not_defined_in_any_translation_unit && (node.descriptor == ast::ENUM || node.descriptor == ast::STRUCT_OR_UNION); - if(m_has_anything_been_printed && (m_last_wants_spacing || wants_spacing)) { + if (m_has_anything_been_printed && (m_last_wants_spacing || wants_spacing)) { fprintf(out, "\n"); } - if(symbol.compare_fail_reason && (node.descriptor != ast::ENUM || !node.name.empty())) { + if (symbol.compare_fail_reason && (node.descriptor != ast::ENUM || !node.name.empty())) { fprintf(out, "// warning: multiple differing types with the same name (%s not equal)\n", symbol.compare_fail_reason); } VariableName name; name.identifier = &symbol.name(); - if(node.descriptor == ast::STRUCT_OR_UNION && node.size_bits > 0) { + if (node.descriptor == ast::STRUCT_OR_UNION && node.size_bits > 0) { m_digits_for_offset = (s32) ceilf(log2(node.size_bits / 8.f) / 4.f); } ast_node(node, name, 0, 0, database, SymbolDescriptor::DATA_TYPE, !symbol.not_defined_in_any_translation_unit); @@ -173,11 +173,11 @@ bool CppPrinter::data_type(const DataType& symbol, const SymbolDatabase& databas void CppPrinter::function(const Function& symbol, const SymbolDatabase& database, const ElfFile* elf) { - if(m_config.skip_statics && symbol.storage_class == STORAGE_CLASS_STATIC) { + if (m_config.skip_statics && symbol.storage_class == STORAGE_CLASS_STATIC) { return; } - if(m_config.skip_member_functions_outside_types && symbol.is_member_function_ish) { + if (m_config.skip_member_functions_outside_types && symbol.is_member_function_ish) { return; } @@ -186,20 +186,20 @@ void CppPrinter::function(const Function& symbol, const SymbolDatabase& database bool wants_spacing = m_config.print_function_bodies && (!local_variables.empty() || function_bodies); - if(m_has_anything_been_printed && (m_last_wants_spacing || wants_spacing)) { + if (m_has_anything_been_printed && (m_last_wants_spacing || wants_spacing)) { fprintf(out, "\n"); } VariableName name; name.identifier = &symbol.name(); - if(m_config.print_storage_information) { + if (m_config.print_storage_information) { fprintf(out, "/* %08x %08x */ ", symbol.address().value, symbol.size()); } // Print out the storage class, return type and function name. print_cpp_storage_class(out, symbol.storage_class); - if(symbol.type()) { + if (symbol.type()) { VariableName dummy; ast_node(*symbol.type(), dummy, 0, 0, database, SymbolDescriptor::FUNCTION); fprintf(out, " "); @@ -208,7 +208,7 @@ void CppPrinter::function(const Function& symbol, const SymbolDatabase& database // Print out the parameter list. fprintf(out, "("); - if(symbol.parameter_variables().has_value()) { + if (symbol.parameter_variables().has_value()) { function_parameters(parameter_variables, database, symbol.stack_frame_size); } else { fprintf(out, "/* parameters unknown */"); @@ -216,37 +216,37 @@ void CppPrinter::function(const Function& symbol, const SymbolDatabase& database fprintf(out, ")"); // Print out the function body. - if(m_config.print_function_bodies) { + if (m_config.print_function_bodies) { fprintf(out, " "); const std::span* body = nullptr; - if(function_bodies) { + if (function_bodies) { auto body_iter = function_bodies->find(symbol.address().value); - if(body_iter != function_bodies->end()) { + if (body_iter != function_bodies->end()) { body = &body_iter->second; } } - if(!local_variables.empty() || body) { + if (!local_variables.empty() || body) { fprintf(out, "{\n"); - if(!local_variables.empty()) { - for(const LocalVariable* variable : local_variables) { + if (!local_variables.empty()) { + for (const LocalVariable* variable : local_variables) { indent(out, 1); - if(const GlobalStorage* storage = std::get_if(&variable->storage)) { + if (const GlobalStorage* storage = std::get_if(&variable->storage)) { global_storage_comment(*storage, variable->address()); } - if(const RegisterStorage* storage = std::get_if(&variable->storage)) { + if (const RegisterStorage* storage = std::get_if(&variable->storage)) { register_storage_comment(*storage); } - if(const StackStorage* storage = std::get_if(&variable->storage)) { + if (const StackStorage* storage = std::get_if(&variable->storage)) { stack_storage_comment(*storage, symbol.stack_frame_size); } - if(variable->type()) { + if (variable->type()) { VariableName local_name; local_name.identifier = &variable->name(); - if(variable->type()) { + if (variable->type()) { ast_node(*variable->type(), local_name, 0, 1, database, SymbolDescriptor::LOCAL_VARIABLE); } else { print_cpp_variable_name(out, local_name, NO_VAR_PRINT_FLAGS); @@ -254,15 +254,15 @@ void CppPrinter::function(const Function& symbol, const SymbolDatabase& database } else { } - if(elf) { + if (elf) { VariableToRefine to_refine; to_refine.address = variable->address(); to_refine.storage = std::get_if(&variable->storage); to_refine.type = variable->type(); - if(can_refine_variable(to_refine)) { + if (can_refine_variable(to_refine)) { fprintf(out, " = "); Result refine_result = refine_variable(to_refine, database, *elf); - if(refine_result.success()) { + if (refine_result.success()) { refined_data(*refine_result, 1); } else { report_warning(refine_result.error()); @@ -272,8 +272,8 @@ void CppPrinter::function(const Function& symbol, const SymbolDatabase& database fprintf(out, ";\n"); } } - if(body) { - if(!local_variables.empty()) { + if (body) { + if (!local_variables.empty()) { indent(out, 1); fprintf(out, "\n"); } @@ -297,19 +297,19 @@ void CppPrinter::function(const Function& symbol, const SymbolDatabase& database void CppPrinter::global_variable( const GlobalVariable& symbol, const SymbolDatabase& database, const ElfFile* elf) { - if(m_config.skip_statics && symbol.storage_class == STORAGE_CLASS_STATIC) { + if (m_config.skip_statics && symbol.storage_class == STORAGE_CLASS_STATIC) { return; } std::optional data; - if(elf) { + if (elf) { VariableToRefine to_refine; to_refine.address = symbol.address(); to_refine.storage = &symbol.storage; to_refine.type = symbol.type(); - if(can_refine_variable(to_refine)) { + if (can_refine_variable(to_refine)) { Result refine_result = refine_variable(to_refine, database, *elf); - if(refine_result.success()) { + if (refine_result.success()) { data = std::move(*refine_result); } else { report_warning(refine_result.error()); @@ -320,26 +320,26 @@ void CppPrinter::global_variable( bool wants_spacing = m_config.print_variable_data && data.has_value() && std::get_if>(&data->value); - if(m_has_anything_been_printed && (m_last_wants_spacing || wants_spacing)) { + if (m_has_anything_been_printed && (m_last_wants_spacing || wants_spacing)) { fprintf(out, "\n"); } global_storage_comment(symbol.storage, symbol.address()); - if(symbol.storage_class != STORAGE_CLASS_NONE) { + if (symbol.storage_class != STORAGE_CLASS_NONE) { print_cpp_storage_class(out, symbol.storage_class); - } else if(m_config.make_globals_extern) { + } else if (m_config.make_globals_extern) { print_cpp_storage_class(out, STORAGE_CLASS_EXTERN); } VariableName name; name.identifier = &symbol.name(); - if(symbol.type()) { + if (symbol.type()) { ast_node(*symbol.type(), name, 0, 0, database, SymbolDescriptor::GLOBAL_VARIABLE); } else { print_cpp_variable_name(out, name, NO_VAR_PRINT_FLAGS); } - if(data.has_value()) { + if (data.has_value()) { fprintf(out, " = "); refined_data(*data, 0); } @@ -360,23 +360,23 @@ void CppPrinter::ast_node( VariableName this_name{&node.name}; VariableName& name = node.name.empty() ? parent_name : this_name; - if(node.descriptor == ast::FUNCTION) { + if (node.descriptor == ast::FUNCTION) { const ast::Function& func_type = node.as(); - if(func_type.vtable_index > -1) { + if (func_type.vtable_index > -1) { fprintf(out, "/* vtable[%d] */ ", func_type.vtable_index); } } print_cpp_storage_class(out, (StorageClass) node.storage_class); - if(node.is_const) { + if (node.is_const) { fprintf(out, "const "); } - if(node.is_volatile) { + if (node.is_volatile) { fprintf(out, "volatile "); } - switch(node.descriptor) { + switch (node.descriptor) { case ast::ARRAY: { const ast::Array& array = node.as(); CCC_ASSERT(array.element_type.get()); @@ -393,7 +393,7 @@ void CppPrinter::ast_node( } case ast::BUILTIN: { const ast::BuiltIn& builtin = node.as(); - switch(builtin.bclass) { + switch (builtin.bclass) { case ast::BuiltInClass::VOID_TYPE: fprintf(out, "void"); break; @@ -418,16 +418,16 @@ void CppPrinter::ast_node( const ast::Enum& enumeration = node.as(); fprintf(out, "enum"); bool name_on_top = (indentation_level == 0) && (enumeration.storage_class != STORAGE_CLASS_TYPEDEF); - if(name_on_top) { + if (name_on_top) { print_cpp_variable_name(out, name, INSERT_SPACE_TO_LEFT); } - if(print_body) { + if (print_body) { fprintf(out, " {"); - if(enumeration.size_bits > -1) { + if (enumeration.size_bits > -1) { fprintf(out, " // 0x%x", enumeration.size_bits / 8); } fprintf(out, "\n"); - for(size_t i = 0; i < enumeration.constants.size(); i++) { + for (size_t i = 0; i < enumeration.constants.size(); i++) { s32 value = enumeration.constants[i].first; const std::string& name = enumeration.constants[i].second; bool is_last = i == enumeration.constants.size() - 1; @@ -437,7 +437,7 @@ void CppPrinter::ast_node( indent(out, indentation_level); } fprintf(out, "}"); - if(!name_on_top) { + if (!name_on_top) { print_cpp_variable_name(out, name, INSERT_SPACE_TO_LEFT); } break; @@ -448,13 +448,13 @@ void CppPrinter::ast_node( } case ast::FUNCTION: { const ast::Function& function = node.as(); - if(function.modifier == ast::MemberFunctionModifier::STATIC) { + if (function.modifier == ast::MemberFunctionModifier::STATIC) { fprintf(out, "static "); - } else if(function.modifier == ast::MemberFunctionModifier::VIRTUAL) { + } else if (function.modifier == ast::MemberFunctionModifier::VIRTUAL) { fprintf(out, "virtual "); } - if(!function.is_constructor_or_destructor) { - if(function.return_type.has_value()) { + if (!function.is_constructor_or_destructor) { + if (function.return_type.has_value()) { VariableName dummy; ast_node(*function.return_type->get(), dummy, 0, indentation_level, database, symbol_descriptor); fprintf(out, " "); @@ -462,15 +462,15 @@ void CppPrinter::ast_node( } print_cpp_variable_name(out, name, BRACKETS_IF_POINTER); fprintf(out, "("); - if(function.parameters.has_value()) { + if (function.parameters.has_value()) { const std::vector>* parameters = &(*function.parameters); // The parameters provided in STABS member function declarations // are wrong, so are swapped out for the correct ones here. bool parameters_printed = false; - if(m_config.substitute_parameter_lists) { + if (m_config.substitute_parameter_lists) { const Function* function_definition = database.functions.symbol_from_handle(function.definition_handle); - if(function_definition && function_definition->parameter_variables().has_value()) { + if (function_definition && function_definition->parameter_variables().has_value()) { std::vector substitute_handles = *function_definition->parameter_variables(); std::vector substitute_parameters = database.parameter_variables.optional_symbols_from_handles(substitute_handles); @@ -479,17 +479,17 @@ void CppPrinter::ast_node( } } - if(!parameters_printed) { + if (!parameters_printed) { size_t start; - if(m_config.omit_this_parameter && parameters->size() >= 1 && (*parameters)[0]->name == "this") { + if (m_config.omit_this_parameter && parameters->size() >= 1 && (*parameters)[0]->name == "this") { start = 1; } else { start = 0; } - for(size_t i = start; i < parameters->size(); i++) { + for (size_t i = start; i < parameters->size(); i++) { VariableName dummy; ast_node(*(*parameters)[i].get(), dummy, 0, indentation_level, database, symbol_descriptor); - if(i != parameters->size() - 1) { + if (i != parameters->size() - 1) { fprintf(out, ", "); } } @@ -503,7 +503,7 @@ void CppPrinter::ast_node( case ast::POINTER_OR_REFERENCE: { const ast::PointerOrReference& pointer_or_reference = node.as(); CCC_ASSERT(pointer_or_reference.value_type.get()); - if(pointer_or_reference.is_pointer) { + if (pointer_or_reference.is_pointer) { name.pointer_chars.emplace_back('*'); } else { name.pointer_chars.emplace_back('&'); @@ -527,7 +527,7 @@ void CppPrinter::ast_node( case ast::STRUCT_OR_UNION: { const ast::StructOrUnion& struct_or_union = node.as(); s32 access_specifier = ast::AS_PUBLIC; - if(struct_or_union.is_struct) { + if (struct_or_union.is_struct) { fprintf(out, "struct"); } else { fprintf(out, "union"); @@ -536,42 +536,42 @@ void CppPrinter::ast_node( indentation_level == 0 && struct_or_union.storage_class != STORAGE_CLASS_TYPEDEF && symbol_descriptor == SymbolDescriptor::DATA_TYPE; - if(name_on_top) { + if (name_on_top) { print_cpp_variable_name(out, name, INSERT_SPACE_TO_LEFT); } // Print base classes. - if(!struct_or_union.base_classes.empty()) { + if (!struct_or_union.base_classes.empty()) { fprintf(out, " : "); - for(size_t i = 0; i < struct_or_union.base_classes.size(); i++) { + for (size_t i = 0; i < struct_or_union.base_classes.size(); i++) { ast::Node& base_class = *struct_or_union.base_classes[i].get(); CCC_ASSERT(base_class.descriptor == ast::TypeName::DESCRIPTOR); offset(base_class, 0); - if(base_class.access_specifier != ast::AS_PUBLIC) { + if (base_class.access_specifier != ast::AS_PUBLIC) { fprintf(out, "%s ", ast::access_specifier_to_string((ast::AccessSpecifier) base_class.access_specifier)); } - if(base_class.is_virtual_base_class) { + if (base_class.is_virtual_base_class) { fprintf(out, "virtual "); } VariableName dummy; ast_node(base_class, dummy, 0, indentation_level + 1, database, symbol_descriptor); - if(i != struct_or_union.base_classes.size() - 1) { + if (i != struct_or_union.base_classes.size() - 1) { fprintf(out, ", "); } } } - if(print_body) { + if (print_body) { fprintf(out, " {"); - if(m_config.print_offsets_and_sizes) { + if (m_config.print_offsets_and_sizes) { fprintf(out, " // 0x%x", struct_or_union.size_bits / 8); } fprintf(out, "\n"); // Print fields. - for(const std::unique_ptr& field : struct_or_union.fields) { + for (const std::unique_ptr& field : struct_or_union.fields) { CCC_ASSERT(field.get()); - if(access_specifier != field->access_specifier) { + if (access_specifier != field->access_specifier) { indent(out, indentation_level); fprintf(out, "%s:\n", ast::access_specifier_to_string((ast::AccessSpecifier) field->access_specifier)); access_specifier = field->access_specifier; @@ -584,15 +584,15 @@ void CppPrinter::ast_node( } // Print member functions. - if(!struct_or_union.member_functions.empty()) { - if(!struct_or_union.fields.empty()) { + if (!struct_or_union.member_functions.empty()) { + if (!struct_or_union.fields.empty()) { indent(out, indentation_level + 1); fprintf(out, "\n"); } - for(const std::unique_ptr& member_function : struct_or_union.member_functions) { - if(member_function->descriptor == ast::FUNCTION) { + for (const std::unique_ptr& member_function : struct_or_union.member_functions) { + if (member_function->descriptor == ast::FUNCTION) { ast::Function& member_func = member_function->as(); - if(access_specifier != member_func.access_specifier) { + if (access_specifier != member_func.access_specifier) { indent(out, indentation_level); fprintf(out, "%s:\n", ast::access_specifier_to_string((ast::AccessSpecifier) member_func.access_specifier)); access_specifier = member_func.access_specifier; @@ -609,7 +609,7 @@ void CppPrinter::ast_node( fprintf(out, "}"); } - if(!name_on_top) { + if (!name_on_top) { print_cpp_variable_name(out, name, INSERT_SPACE_TO_LEFT); } @@ -618,12 +618,12 @@ void CppPrinter::ast_node( case ast::TYPE_NAME: { const ast::TypeName& type_name = node.as(); const DataType* data_type = database.data_types.symbol_from_handle(type_name.data_type_handle); - if(data_type) { + if (data_type) { fprintf(out, "%s", data_type->name().c_str()); - } else if(type_name.source == ast::TypeNameSource::UNNAMED_THIS) { + } else if (type_name.source == ast::TypeNameSource::UNNAMED_THIS) { fprintf(out, "CCC_THIS_TYPE"); } else { - if(type_name.unresolved_stabs) { + if (type_name.unresolved_stabs) { fprintf(out, "CCC_ERROR(\"Unresolved %s type name '%s' with STABS type number (%d,%d).\")", ast::type_name_source_to_string(type_name.source), type_name.unresolved_stabs->type_name.c_str(), @@ -642,25 +642,25 @@ void CppPrinter::ast_node( void CppPrinter::function_parameters(std::span parameters, const SymbolDatabase& database, s32 stack_frame_size) { bool skip_this = m_config.omit_this_parameter && !parameters.empty() && parameters[0]->name() == "this"; - for(size_t i = skip_this ? 1 : 0; i < parameters.size(); i++) { + for (size_t i = skip_this ? 1 : 0; i < parameters.size(); i++) { const ParameterVariable& parameter_variable = *parameters[i]; - if(const RegisterStorage* storage = std::get_if(¶meter_variable.storage)) { + if (const RegisterStorage* storage = std::get_if(¶meter_variable.storage)) { register_storage_comment(*storage); } - if(const StackStorage* storage = std::get_if(¶meter_variable.storage)) { + if (const StackStorage* storage = std::get_if(¶meter_variable.storage)) { stack_storage_comment(*storage, stack_frame_size); } VariableName variable_name; variable_name.identifier = ¶meter_variable.name(); - if(parameter_variable.type()) { + if (parameter_variable.type()) { ast_node(*parameter_variable.type(), variable_name, 0, 0, database, SymbolDescriptor::PARAMETER_VARIABLE); } else { print_cpp_variable_name(out, variable_name, NO_VAR_PRINT_FLAGS); } - if(i + 1 != parameters.size()) { + if (i + 1 != parameters.size()) { fprintf(out, ", "); } } @@ -668,21 +668,21 @@ void CppPrinter::function_parameters(std::span paramet void CppPrinter::refined_data(const RefinedData& data, s32 indentation_level) { - if(!data.field_name.empty()) { + if (!data.field_name.empty()) { fprintf(out, "/* %s = */ ", data.field_name.c_str()); } - if(const std::string* string = std::get_if(&data.value)) { + if (const std::string* string = std::get_if(&data.value)) { fprintf(out, "%s", string->c_str()); } - if(const std::vector* list = std::get_if>(&data.value)) { + if (const std::vector* list = std::get_if>(&data.value)) { fprintf(out, "{\n"); - for(size_t i = 0; i < list->size(); i++) { + for (size_t i = 0; i < list->size(); i++) { indent(out, indentation_level + 1); VariableName dummy; refined_data((*list)[i], indentation_level + 1); - if(i != list->size() - 1) { + if (i != list->size() - 1) { fprintf(out, ","); } fprintf(out, "\n"); @@ -694,7 +694,7 @@ void CppPrinter::refined_data(const RefinedData& data, s32 indentation_level) static void print_cpp_storage_class(FILE* out, StorageClass storage_class) { - switch(storage_class) { + switch (storage_class) { case STORAGE_CLASS_NONE: break; case STORAGE_CLASS_TYPEDEF: fprintf(out, "typedef "); break; case STORAGE_CLASS_EXTERN: fprintf(out, "extern "); break; @@ -708,35 +708,35 @@ static void print_cpp_variable_name(FILE* out, VariableName& name, u32 flags) { bool has_name = name.identifier != nullptr && !name.identifier->empty(); bool has_brackets = (flags & BRACKETS_IF_POINTER) && !name.pointer_chars.empty(); - if(has_name && (flags & INSERT_SPACE_TO_LEFT)) { + if (has_name && (flags & INSERT_SPACE_TO_LEFT)) { fprintf(out, " "); } - if(has_brackets) { + if (has_brackets) { fprintf(out, "("); } - for(s32 i = (s32) name.pointer_chars.size() - 1; i >= 0; i--) { + for (s32 i = (s32) name.pointer_chars.size() - 1; i >= 0; i--) { fprintf(out, "%c", name.pointer_chars[i]); } name.pointer_chars.clear(); - if(has_name) { + if (has_name) { fprintf(out, "%s", name.identifier->c_str()); name.identifier = nullptr; } - for(s32 index : name.array_indices) { + for (s32 index : name.array_indices) { fprintf(out, "[%d]", index); } name.array_indices.clear(); - if(has_brackets) { + if (has_brackets) { fprintf(out, ")"); } } void CppPrinter::global_storage_comment(const GlobalStorage& storage, Address address) { - if(m_config.print_storage_information) { + if (m_config.print_storage_information) { fprintf(out, "/* "); fprintf(out, "%s", global_storage_location_to_string(storage.location)); - if(address.valid()) { + if (address.valid()) { fprintf(out, " %x", address.value); } fprintf(out, " */ "); @@ -745,7 +745,7 @@ void CppPrinter::global_storage_comment(const GlobalStorage& storage, Address ad void CppPrinter::register_storage_comment(const RegisterStorage& storage) { - if(m_config.print_storage_information) { + if (m_config.print_storage_information) { fprintf(out, "/* "); auto [register_class, register_index_relative] = mips::map_dbx_register_index(storage.dbx_register_number); @@ -759,16 +759,16 @@ void CppPrinter::register_storage_comment(const RegisterStorage& storage) void CppPrinter::stack_storage_comment(const StackStorage& storage, s32 stack_frame_size) { - if(m_config.print_storage_information) { + if (m_config.print_storage_information) { fprintf(out, "/* "); s32 display_offset = storage.stack_pointer_offset; const char* prefix = ""; - if(stack_frame_size > -1 && !m_config.caller_stack_offsets) { + if (stack_frame_size > -1 && !m_config.caller_stack_offsets) { display_offset += stack_frame_size; } else { prefix = "caller "; } - if(display_offset >= 0) { + if (display_offset >= 0) { fprintf(out, "0x%x(%ssp)", display_offset, prefix); } else { fprintf(out, "-0x%x(%ssp)", -display_offset, prefix); @@ -779,10 +779,10 @@ void CppPrinter::stack_storage_comment(const StackStorage& storage, s32 stack_fr void CppPrinter::offset(const ast::Node& node, s32 base_offset) { - if(m_config.print_offsets_and_sizes && node.storage_class != STORAGE_CLASS_STATIC && node.offset_bytes > -1) { + if (m_config.print_offsets_and_sizes && node.storage_class != STORAGE_CLASS_STATIC && node.offset_bytes > -1) { CCC_ASSERT(m_digits_for_offset > -1 && m_digits_for_offset < 100); fprintf(out, "/* 0x%0*x", m_digits_for_offset, base_offset + node.offset_bytes); - if(node.descriptor == ast::BITFIELD) { + if (node.descriptor == ast::BITFIELD) { fprintf(out, ":%02d", node.as().bitfield_offset_bits); } fprintf(out, " */ "); @@ -791,7 +791,7 @@ void CppPrinter::offset(const ast::Node& node, s32 base_offset) static void indent(FILE* out, s32 level) { - for(s32 i = 0; i < level; i++) { + for (s32 i = 0; i < level; i++) { fputc('\t', out); } } diff --git a/src/ccc/registers.cpp b/src/ccc/registers.cpp index 4f306bad..58716eb2 100644 --- a/src/ccc/registers.cpp +++ b/src/ccc/registers.cpp @@ -189,9 +189,9 @@ const char* VU0_STRINGS[32] = { std::pair map_dbx_register_index(s32 index) { - if(index >= 0 && index <= 31) { + if (index >= 0 && index <= 31) { return {RegisterClass::GPR, index}; - } else if(index >= 38 && index <= 69) { + } else if (index >= 38 && index <= 69) { return {RegisterClass::FPR, index - 38}; } else { return{RegisterClass::INVALID, 0}; diff --git a/src/ccc/sndll.cpp b/src/ccc/sndll.cpp index 894deba6..24c4661e 100644 --- a/src/ccc/sndll.cpp +++ b/src/ccc/sndll.cpp @@ -58,7 +58,7 @@ Result parse_sndll_file(std::span image, Address address, S CCC_CHECK((*magic & 0xffffff) == CCC_FOURCC("SNR\00"), "Not a SNDLL %s.", address.valid() ? "section" : "file"); char version = *magic >> 24; - switch(version) { + switch (version) { case '1': { const SNDLLHeaderV1* header = get_packed(image, 0); CCC_CHECK(header, "File too small to contain SNDLL V1 header."); @@ -83,9 +83,9 @@ static Result parse_sndll_common( sndll.type = type; sndll.version = version; - if(common.elf_path) { + if (common.elf_path) { const char* elf_path = get_string(image, common.elf_path); - if(elf_path) { + if (elf_path) { sndll.elf_path = elf_path; } } @@ -93,13 +93,13 @@ static Result parse_sndll_common( CCC_CHECK(common.symbol_count < (32 * 1024 * 1024) / sizeof(SNDLLSymbol), "SNDLL symbol count is too high."); sndll.symbols.reserve(common.symbol_count); - for(u32 i = 0; i < common.symbol_count; i++) { + for (u32 i = 0; i < common.symbol_count; i++) { u32 symbol_offset = common.symbols - address.get_or_zero() + i * sizeof(SNDLLSymbolHeader); const SNDLLSymbolHeader* symbol_header = get_packed(image, symbol_offset); CCC_CHECK(symbol_header, "SNDLL symbol out of range."); const char* string = nullptr; - if(symbol_header->string) { + if (symbol_header->string) { string = get_string(image, symbol_header->string - address.get_or_zero()); } @@ -119,38 +119,38 @@ Result import_sndll_symbols( u32 importer_flags, DemanglerFunctions demangler) { - for(const SNDLLSymbol& symbol : sndll.symbols) { - if(symbol.value == 0 || symbol.string.empty()) { + for (const SNDLLSymbol& symbol : sndll.symbols) { + if (symbol.value == 0 || symbol.string.empty()) { continue; } u32 address = symbol.value; - if(symbol.type != SNDLL_ABSOLUTE && sndll.type == SNDLLType::DYNAMIC_LIBRARY) { + if (symbol.type != SNDLL_ABSOLUTE && sndll.type == SNDLLType::DYNAMIC_LIBRARY) { address += sndll.address.get_or_zero(); } - if(!(importer_flags & DONT_DEDUPLICATE_SYMBOLS)) { - if(database.functions.first_handle_from_starting_address(address).valid()) { + if (!(importer_flags & DONT_DEDUPLICATE_SYMBOLS)) { + if (database.functions.first_handle_from_starting_address(address).valid()) { continue; } - if(database.global_variables.first_handle_from_starting_address(address).valid()) { + if (database.global_variables.first_handle_from_starting_address(address).valid()) { continue; } - if(database.local_variables.first_handle_from_starting_address(address).valid()) { + if (database.local_variables.first_handle_from_starting_address(address).valid()) { continue; } } const Section* section = database.sections.symbol_overlapping_address(address); - if(section) { - if(section->contains_code()) { + if (section) { + if (section->contains_code()) { Result function = database.functions.create_symbol( symbol.string, group.source, group.module_symbol, address, importer_flags, demangler); CCC_RETURN_IF_ERROR(function); continue; - } else if(section->contains_data()) { + } else if (section->contains_data()) { Result global_variable = database.global_variables.create_symbol( symbol.string, group.source, group.module_symbol, address, importer_flags, demangler); CCC_RETURN_IF_ERROR(global_variable); @@ -169,7 +169,7 @@ Result import_sndll_symbols( void print_sndll_symbols(FILE* out, const SNDLLFile& sndll) { fprintf(out, "SNDLL SYMBOLS:\n"); - for(const SNDLLSymbol& symbol : sndll.symbols) { + for (const SNDLLSymbol& symbol : sndll.symbols) { const char* type = sndll_symbol_type_to_string(symbol.type); const char* string = !symbol.string.empty() ? symbol.string.c_str() : "(no string)"; fprintf(out, "%8s %08x %s\n", type, symbol.value, string); @@ -178,7 +178,7 @@ void print_sndll_symbols(FILE* out, const SNDLLFile& sndll) static const char* sndll_symbol_type_to_string(SNDLLSymbolType type) { - switch(type) { + switch (type) { case SNDLL_NIL: return "NIL"; case SNDLL_EXTERNAL: return "EXTERNAL"; case SNDLL_RELATIVE: return "RELATIVE"; diff --git a/src/ccc/stabs.cpp b/src/ccc/stabs.cpp index 0fe10dec..3b8c010e 100644 --- a/src/ccc/stabs.cpp +++ b/src/ccc/stabs.cpp @@ -32,7 +32,7 @@ Result parse_stabs_symbol(const char*& input) CCC_EXPECT_CHAR(input, ':', "identifier"); CCC_CHECK(*input != '\0', "Unexpected end of input."); - if((*input >= '0' && *input <= '9') || *input == '(') { + if ((*input >= '0' && *input <= '9') || *input == '(') { symbol.descriptor = StabsSymbolDescriptor::LOCAL_VARIABLE; } else { char symbol_descriptor = *(input++); @@ -43,7 +43,7 @@ Result parse_stabs_symbol(const char*& input) "Invalid symbol descriptor '%c'.", (char) symbol.descriptor); CCC_CHECK(*input != '\0', "Unexpected end of input."); - if(*input == 't') { + if (*input == 't') { input++; } @@ -54,11 +54,11 @@ Result parse_stabs_symbol(const char*& input) bool is_function = symbol.descriptor == StabsSymbolDescriptor::LOCAL_FUNCTION || symbol.descriptor == StabsSymbolDescriptor::GLOBAL_FUNCTION; - if(is_function && input[0] == ',') { + if (is_function && input[0] == ',') { input++; - while(*input != ',' && *input != '\0') input++; // enclosing function + while (*input != ',' && *input != '\0') input++; // enclosing function CCC_EXPECT_CHAR(input, ',', "nested function suffix"); - while(*input != ',' && *input != '\0') input++; // function + while (*input != ',' && *input != '\0') input++; // function } symbol.type = std::move(*type); @@ -68,7 +68,7 @@ Result parse_stabs_symbol(const char*& input) // the type name is not "somevar". bool is_type = symbol.descriptor == StabsSymbolDescriptor::TYPE_NAME || symbol.descriptor == StabsSymbolDescriptor::ENUM_STRUCT_OR_TYPE_TAG; - if(is_type) { + if (is_type) { symbol.type->name = symbol.name; } @@ -81,7 +81,7 @@ Result parse_stabs_symbol(const char*& input) static bool validate_symbol_descriptor(StabsSymbolDescriptor descriptor) { bool valid; - switch(descriptor) { + switch (descriptor) { case StabsSymbolDescriptor::LOCAL_VARIABLE: case StabsSymbolDescriptor::REFERENCE_PARAMETER_A: case StabsSymbolDescriptor::LOCAL_FUNCTION: @@ -110,7 +110,7 @@ Result> parse_top_level_stabs_type(const char*& input CCC_RETURN_IF_ERROR(type); // Handle first base class suffixes. - if((*type)->descriptor == StabsTypeDescriptor::STRUCT && input[0] == '~' && input[1] == '%') { + if ((*type)->descriptor == StabsTypeDescriptor::STRUCT && input[0] == '~' && input[1] == '%') { input += 2; Result> first_base_class = parse_stabs_type(input); @@ -121,7 +121,7 @@ Result> parse_top_level_stabs_type(const char*& input } // Handle extra live range information. - if(input[0] == ';' && input[1] == 'l') { + if (input[0] == ';' && input[1] == 'l') { input += 2; CCC_EXPECT_CHAR(input, '(', "live range suffix"); CCC_EXPECT_CHAR(input, '#', "live range suffix"); @@ -143,7 +143,7 @@ static Result> parse_stabs_type(const char*& input) CCC_CHECK(*input != '\0', "Unexpected end of input."); - if(*input == '(') { + if (*input == '(') { // This file has type numbers made up of two pieces: an include file // index and a type number. @@ -162,11 +162,11 @@ static Result> parse_stabs_type(const char*& input) type_number.file = *file_index; type_number.type = *type_index; - if(*input != '=') { + if (*input != '=') { return std::make_unique(type_number); } input++; - } else if(*input >= '0' && *input <= '9') { + } else if (*input >= '0' && *input <= '9') { // This file has type numbers which are just a single number. This is // the more common case for games. @@ -174,7 +174,7 @@ static Result> parse_stabs_type(const char*& input) CCC_CHECK(type_index.has_value(), "Failed to parse type number."); type_number.type = *type_index; - if(*input != '=') { + if (*input != '=') { return std::make_unique(type_number); } input++; @@ -183,7 +183,7 @@ static Result> parse_stabs_type(const char*& input) CCC_CHECK(*input != '\0', "Unexpected end of input."); StabsTypeDescriptor descriptor; - if((*input >= '0' && *input <= '9') || *input == '(') { + if ((*input >= '0' && *input <= '9') || *input == '(') { descriptor = StabsTypeDescriptor::TYPE_REFERENCE; } else { char descriptor_char = *(input++); @@ -193,7 +193,7 @@ static Result> parse_stabs_type(const char*& input) std::unique_ptr out_type; - switch(descriptor) { + switch (descriptor) { case StabsTypeDescriptor::TYPE_REFERENCE: { // 0..9 auto type_reference = std::make_unique(type_number); @@ -221,7 +221,7 @@ static Result> parse_stabs_type(const char*& input) case StabsTypeDescriptor::ENUM: { // e auto enum_type = std::make_unique(type_number); STABS_DEBUG_PRINTF("enum {\n"); - while(*input != ';') { + while (*input != ';') { std::optional name = parse_stabs_identifier(input, ':'); CCC_CHECK(name.has_value(), "Failed to parse enum field name."); @@ -301,18 +301,18 @@ static Result> parse_stabs_type(const char*& input) CCC_CHECK(struct_size.has_value(), "Failed to parse struct size."); struct_type->size = *struct_size; - if(*input == '!') { + if (*input == '!') { input++; std::optional base_class_count = parse_number_s32(input); CCC_CHECK(base_class_count.has_value(), "Failed to parse base class count."); CCC_EXPECT_CHAR(input, ',', "base class section"); - for(s64 i = 0; i < *base_class_count; i++) { + for (s64 i = 0; i < *base_class_count; i++) { StabsStructOrUnionType::BaseClass base_class; char is_virtual = *(input++); - switch(is_virtual) { + switch (is_virtual) { case '0': base_class.is_virtual = false; break; case '1': base_class.is_virtual = true; break; default: return CCC_FAILURE("Failed to parse base class (virtual character)."); @@ -377,7 +377,7 @@ static Result> parse_stabs_type(const char*& input) char cross_reference_type = *(input++); CCC_CHECK(cross_reference_type != '\0', "Failed to parse cross reference type."); - switch(cross_reference_type) { + switch (cross_reference_type) { case 'e': cross_reference->type = ast::ForwardDeclaredType::ENUM; break; case 's': cross_reference->type = ast::ForwardDeclaredType::STRUCT; break; case 'u': cross_reference->type = ast::ForwardDeclaredType::UNION; break; @@ -421,14 +421,14 @@ static Result> parse_stabs_type(const char*& input) case StabsTypeDescriptor::METHOD: { // # auto method = std::make_unique(type_number); - if(*input == '#') { + if (*input == '#') { input++; auto return_type = parse_stabs_type(input); CCC_RETURN_IF_ERROR(return_type); method->return_type = std::move(*return_type); - if(*input == ';') { + if (*input == ';') { input++; } } else { @@ -442,8 +442,8 @@ static Result> parse_stabs_type(const char*& input) CCC_RETURN_IF_ERROR(return_type); method->return_type = std::move(*return_type); - while(*input != '\0') { - if(*input == ';') { + while (*input != '\0') { + if (*input == ';') { input++; break; } @@ -480,7 +480,7 @@ static Result> parse_stabs_type(const char*& input) break; } case StabsTypeDescriptor::TYPE_ATTRIBUTE: { // @ - if((*input >= '0' && *input <= '9') || *input == '(') { + if ((*input >= '0' && *input <= '9') || *input == '(') { auto member_pointer = std::make_unique(type_number); auto class_type = parse_stabs_type(input); @@ -538,8 +538,8 @@ static Result> parse_field_list(const { std::vector fields; - while(*input != '\0') { - if(*input == ';') { + while (*input != '\0') { + if (*input == ';') { input++; break; } @@ -552,14 +552,14 @@ static Result> parse_field_list(const field.name = std::move(*name); CCC_EXPECT_CHAR(input, ':', "identifier"); - if(*input == '/') { + if (*input == '/') { input++; Result visibility = parse_visibility_character(input); CCC_RETURN_IF_ERROR(visibility); field.visibility = *visibility; } - if(*input == ':') { + if (*input == ':') { input = before_field; break; } @@ -567,7 +567,7 @@ static Result> parse_field_list(const CCC_RETURN_IF_ERROR(type); field.type = std::move(*type); - if(field.name.size() >= 1 && field.name[0] == '$') { + if (field.name.size() >= 1 && field.name[0] == '$') { // Virtual function table pointers and virtual base class pointers. CCC_EXPECT_CHAR(input, ',', "field type"); @@ -576,7 +576,7 @@ static Result> parse_field_list(const field.offset_bits = *offset_bits; CCC_EXPECT_CHAR(input, ';', "field offset"); - } else if(*input == ':') { + } else if (*input == ':') { // Static fields. input++; field.is_static = true; @@ -587,7 +587,7 @@ static Result> parse_field_list(const field.type_name = std::move(*type_name); CCC_EXPECT_CHAR(input, ';', "identifier"); - } else if(*input == ',') { + } else if (*input == ',') { // Normal fields. input++; @@ -619,13 +619,13 @@ static Result> parse_memb // Check for if the next character is from an enclosing field list. If this // is the case, the next character will be ',' for normal fields and ':' for // static fields (see above). - if(*input == ',' || *input == ':') { + if (*input == ',' || *input == ':') { return std::vector(); } std::vector member_functions; - while(*input != '\0') { - if(*input == ';') { + while (*input != '\0') { + if (*input == ';') { input++; break; } @@ -637,8 +637,8 @@ static Result> parse_memb CCC_EXPECT_CHAR(input, ':', "member function"); CCC_EXPECT_CHAR(input, ':', "member function"); - while(*input != '\0') { - if(*input == ';') { + while (*input != '\0') { + if (*input == ';') { input++; break; } @@ -661,7 +661,7 @@ static Result> parse_memb char modifiers = *(input++); CCC_CHECK(modifiers != '\0', "Failed to parse member function modifiers."); - switch(modifiers) { + switch (modifiers) { case 'A': function.is_const = false; function.is_volatile = false; @@ -687,7 +687,7 @@ static Result> parse_memb char flag = *(input++); CCC_CHECK(flag != '\0', "Failed to parse member function type."); - switch(flag) { + switch (flag) { case '.': { // normal member function function.modifier = ast::MemberFunctionModifier::NONE; break; @@ -725,7 +725,7 @@ static Result> parse_memb static Result parse_visibility_character(const char*& input) { char visibility = *(input++); - switch(visibility) { + switch (visibility) { case '0': return StabsStructOrUnionType::Visibility::PRIVATE; case '1': return StabsStructOrUnionType::Visibility::PROTECTED; case '2': return StabsStructOrUnionType::Visibility::PUBLIC; @@ -740,7 +740,7 @@ std::optional parse_number_s32(const char*& input) { char* end; s64 value = strtoll(input, &end, 10); - if(end == input) { + if (end == input) { return std::nullopt; } input = end; @@ -751,7 +751,7 @@ std::optional parse_number_s64(const char*& input) { char* end; s64 value = strtoll(input, &end, 10); - if(end == input) { + if (end == input) { return std::nullopt; } input = end; @@ -761,8 +761,8 @@ std::optional parse_number_s64(const char*& input) std::optional parse_stabs_identifier(const char*& input, char terminator) { const char* begin = input; - for(; *input != '\0'; input++) { - if(*input == terminator) { + for (; *input != '\0'; input++) { + if (*input == terminator) { return std::string(begin, input); } } @@ -778,17 +778,17 @@ Result parse_dodgy_stabs_identifier(const char*& input, char termin const char* begin = input; s32 template_depth = 0; - for(; *input != '\0'; input++) { + for (; *input != '\0'; input++) { // Skip past character literals. - if(*input == '\'') { + if (*input == '\'') { input++; - if(*input == '\'') { + if (*input == '\'') { input++; // Handle character literals containing a single quote. } - while(*input != '\'' && *input != '\0') { + while (*input != '\'' && *input != '\0') { input++; } - if(*input == '\0') { + if (*input == '\0') { break; } input++; @@ -796,14 +796,14 @@ Result parse_dodgy_stabs_identifier(const char*& input, char termin // Keep track of the template depth so we know when to expect the // terminator character. - if(*input == '<') { + if (*input == '<') { template_depth++; } - if(*input == '>') { + if (*input == '>') { template_depth--; } - if(*input == terminator && template_depth == 0) { + if (*input == terminator && template_depth == 0) { return std::string(begin, input); } } @@ -822,7 +822,7 @@ static void print_field(const StabsStructOrUnionType::Field& field) const char* stabs_field_visibility_to_string(StabsStructOrUnionType::Visibility visibility) { - switch(visibility) { + switch (visibility) { case StabsStructOrUnionType::Visibility::PRIVATE: return "private"; case StabsStructOrUnionType::Visibility::PROTECTED: return "protected"; case StabsStructOrUnionType::Visibility::PUBLIC: return "public"; diff --git a/src/ccc/stabs.h b/src/ccc/stabs.h index fef413ba..01330ae1 100644 --- a/src/ccc/stabs.h +++ b/src/ccc/stabs.h @@ -90,7 +90,7 @@ struct StabsType { virtual void enumerate_numbered_types(std::map& output) const { - if(type_number.valid() && descriptor.has_value()) { + if (type_number.valid() && descriptor.has_value()) { output.emplace(type_number, this); } } @@ -237,21 +237,21 @@ struct StabsStructOrUnionType : StabsType { void enumerate_numbered_types(std::map& output) const override { StabsType::enumerate_numbered_types(output); - for(const BaseClass& base_class : base_classes) { + for (const BaseClass& base_class : base_classes) { base_class.type->enumerate_numbered_types(output); } - for(const Field& field : fields) { + for (const Field& field : fields) { field.type->enumerate_numbered_types(output); } - for(const MemberFunctionSet& member_function_set : member_functions) { - for(const MemberFunction& member_function : member_function_set.overloads) { + for (const MemberFunctionSet& member_function_set : member_functions) { + for (const MemberFunction& member_function : member_function_set.overloads) { member_function.type->enumerate_numbered_types(output); - if(member_function.virtual_type.get()) { + if (member_function.virtual_type.get()) { member_function.virtual_type->enumerate_numbered_types(output); } } } - if(first_base_class.get()) { + if (first_base_class.get()) { first_base_class->enumerate_numbered_types(output); } } @@ -296,10 +296,10 @@ struct StabsMethodType : StabsType { { StabsType::enumerate_numbered_types(output); return_type->enumerate_numbered_types(output); - if(class_type.has_value()) { + if (class_type.has_value()) { (*class_type)->enumerate_numbered_types(output); } - for(const std::unique_ptr& parameter_type : parameter_types) { + for (const std::unique_ptr& parameter_type : parameter_types) { parameter_type->enumerate_numbered_types(output); } } diff --git a/src/ccc/stabs_to_ast.cpp b/src/ccc/stabs_to_ast.cpp index 67757972..4f65b6b0 100644 --- a/src/ccc/stabs_to_ast.cpp +++ b/src/ccc/stabs_to_ast.cpp @@ -48,9 +48,9 @@ Result> stabs_type_to_ast( type.type_number.file, type.type_number.type, type.name.has_value() ? type.name->c_str() : ""); - if(depth > 200) { + if (depth > 200) { const char* error_message = "Call depth greater than 200 in stabs_type_to_ast, probably infinite recursion."; - if(state.importer_flags & STRICT_PARSING) { + if (state.importer_flags & STRICT_PARSING) { return CCC_FAILURE(error_message); } else { CCC_WARN(error_message); @@ -63,7 +63,7 @@ Result> stabs_type_to_ast( // This makes sure that types are replaced with their type name in cases // where that would be more appropriate. - if(type.name.has_value()) { + if (type.name.has_value()) { bool try_substitute = depth > 0 && (type.is_root || type.descriptor == StabsTypeDescriptor::RANGE || type.descriptor == StabsTypeDescriptor::BUILTIN); @@ -73,7 +73,7 @@ Result> stabs_type_to_ast( // Cross references will be handled below. bool is_cross_reference = type.descriptor == StabsTypeDescriptor::CROSS_REFERENCE; bool is_void = is_void_like(type); - if((substitute_type_name || try_substitute) && !is_name_empty && !is_cross_reference && !is_void) { + if ((substitute_type_name || try_substitute) && !is_name_empty && !is_cross_reference && !is_void) { auto type_name = std::make_unique(); type_name->source = ast::TypeNameSource::REFERENCE; type_name->unresolved_stabs = std::make_unique(); @@ -87,7 +87,7 @@ Result> stabs_type_to_ast( // This prevents infinite recursion when an automatically generated member // function references an unnamed type. bool can_compare_type_numbers = type.type_number.valid() && enclosing_struct && enclosing_struct->type_number.valid(); - if(force_substitute && can_compare_type_numbers && type.type_number == enclosing_struct->type_number) { + if (force_substitute && can_compare_type_numbers && type.type_number == enclosing_struct->type_number) { // It's probably a this parameter (or return type) for an unnamed type. auto type_name = std::make_unique(); type_name->source = ast::TypeNameSource::UNNAMED_THIS; @@ -98,15 +98,15 @@ Result> stabs_type_to_ast( return std::unique_ptr(std::move(type_name)); } - if(!type.descriptor.has_value()) { + if (!type.descriptor.has_value()) { // The definition of the type has been defined previously, so we have to // look it up by its type number. CCC_CHECK(type.type_number.valid(), "Cannot lookup type (type is anonymous)."); auto stabs_type = state.stabs_types->find(type.type_number); - if(stabs_type == state.stabs_types->end()) { + if (stabs_type == state.stabs_types->end()) { std::string error_message = "Failed to lookup STABS type by its type number (" + std::to_string(type.type_number.file) + "," + std::to_string(type.type_number.type) + ")."; - if(state.importer_flags & STRICT_PARSING) { + if (state.importer_flags & STRICT_PARSING) { return CCC_FAILURE("%s", error_message.c_str()); } else { CCC_WARN("%s", error_message.c_str()); @@ -126,10 +126,10 @@ Result> stabs_type_to_ast( std::unique_ptr result; - switch(*type.descriptor) { + switch (*type.descriptor) { case StabsTypeDescriptor::TYPE_REFERENCE: { const auto& stabs_type_ref = type.as(); - if(!type.type_number.valid() || !stabs_type_ref.type->type_number.valid() || stabs_type_ref.type->type_number != type.type_number) { + if (!type.type_number.valid() || !stabs_type_ref.type->type_number.valid() || stabs_type_ref.type->type_number != type.type_number) { auto node = stabs_type_to_ast( *stabs_type_ref.type, enclosing_struct, @@ -175,7 +175,7 @@ Result> stabs_type_to_ast( s64 high_value = strtoll(high, &end, 10); CCC_CHECK(end != high, "Failed to parse low part of range as integer."); - if(high_value == 4294967295) { + if (high_value == 4294967295) { // Some compilers wrote out a wrapped around value here. array->element_count = 0; } else { @@ -250,7 +250,7 @@ Result> stabs_type_to_ast( case StabsTypeDescriptor::STRUCT: case StabsTypeDescriptor::UNION: { const StabsStructOrUnionType* stabs_struct_or_union; - if(type.descriptor == StabsTypeDescriptor::STRUCT) { + if (type.descriptor == StabsTypeDescriptor::STRUCT) { stabs_struct_or_union = &type.as(); } else { stabs_struct_or_union = &type.as(); @@ -260,7 +260,7 @@ Result> stabs_type_to_ast( struct_or_union->is_struct = type.descriptor == StabsTypeDescriptor::STRUCT; struct_or_union->size_bits = (s32) stabs_struct_or_union->size * 8; - for(const StabsStructOrUnionType::BaseClass& stabs_base_class : stabs_struct_or_union->base_classes) { + for (const StabsStructOrUnionType::BaseClass& stabs_base_class : stabs_struct_or_union->base_classes) { auto base_class = stabs_type_to_ast( *stabs_base_class.type, &type, @@ -273,7 +273,7 @@ Result> stabs_type_to_ast( (*base_class)->offset_bytes = stabs_base_class.offset; (*base_class)->set_access_specifier(stabs_field_visibility_to_access_specifier(stabs_base_class.visibility), state.importer_flags); - if(stabs_base_class.is_virtual) { + if (stabs_base_class.is_virtual) { (*base_class)->is_virtual_base_class = true; } @@ -281,7 +281,7 @@ Result> stabs_type_to_ast( } AST_DEBUG_PRINTF("%-*s beginfields\n", depth * 4, ""); - for(const StabsStructOrUnionType::Field& field : stabs_struct_or_union->fields) { + for (const StabsStructOrUnionType::Field& field : stabs_struct_or_union->fields) { auto node = field_to_ast(field, type, state, depth); CCC_RETURN_IF_ERROR(node); struct_or_union->fields.emplace_back(std::move(*node)); @@ -311,7 +311,7 @@ Result> stabs_type_to_ast( case ccc::StabsTypeDescriptor::FLOATING_POINT_BUILTIN: { const auto& fp_builtin = type.as(); auto builtin = std::make_unique(); - switch(fp_builtin.bytes) { + switch (fp_builtin.bytes) { case 1: builtin->bclass = ast::BuiltInClass::UNSIGNED_8; break; case 2: builtin->bclass = ast::BuiltInClass::UNSIGNED_16; break; case 4: builtin->bclass = ast::BuiltInClass::UNSIGNED_32; break; @@ -337,7 +337,7 @@ Result> stabs_type_to_ast( function->return_type = std::move(*return_node); function->parameters.emplace(); - for(const std::unique_ptr& parameter_type : stabs_method.parameter_types) { + for (const std::unique_ptr& parameter_type : stabs_method.parameter_types) { auto parameter_node = stabs_type_to_ast( *parameter_type, enclosing_struct, @@ -447,8 +447,8 @@ static bool is_void_like(const StabsType& type) // Unfortunately, a common case seems to be that various types (most // commonly __builtin_va_list) are indistinguishable from void or void*, so // we have to output them as a void built-in. - if(type.descriptor.has_value()) { - switch(*type.descriptor) { + if (type.descriptor.has_value()) { + switch (*type.descriptor) { case StabsTypeDescriptor::POINTER: { return is_void_like(*type.as().value_type.get()); } @@ -489,8 +489,8 @@ static Result classify_range(const StabsRangeType& type) {"0", "-1", ast::BuiltInClass::UNQUALIFIED_128} // Old homebrew toolchain }; - for(const auto& range : strings) { - if(strcmp(range.low, low) == 0 && strcmp(range.high, high) == 0) { + for (const auto& range : strings) { + if (strcmp(range.low, low) == 0 && strcmp(range.high, high) == 0) { return range.classification; } } @@ -512,8 +512,8 @@ static Result classify_range(const StabsRangeType& type) {-2147483648, 2147483647, ast::BuiltInClass::SIGNED_32}, }; - for(const auto& range : integers) { - if((range.low == low_value || range.low == -low_value) && range.high == high_value) { + for (const auto& range : integers) { + if ((range.low == low_value || range.low == -low_value) && range.high == high_value) { return range.classification; } } @@ -532,7 +532,7 @@ static Result> field_to_ast( Result bitfield_storage_unit_size_bits = detect_bitfield(field, state); CCC_RETURN_IF_ERROR(bitfield_storage_unit_size_bits); - if(*bitfield_storage_unit_size_bits) { + if (*bitfield_storage_unit_size_bits) { // Process bitfields. auto bitfield_node = stabs_type_to_ast( *field.type, @@ -568,11 +568,11 @@ static Result> field_to_ast( (*node)->size_bits = field.size_bits; (*node)->set_access_specifier(stabs_field_visibility_to_access_specifier(field.visibility), state.importer_flags); - if(field.name.starts_with("$vf") || field.name.starts_with("_vptr$") || field.name.starts_with("_vptr.")) { + if (field.name.starts_with("$vf") || field.name.starts_with("_vptr$") || field.name.starts_with("_vptr.")) { (*node)->is_vtable_pointer = true; } - if(field.is_static) { + if (field.is_static) { (*node)->storage_class = STORAGE_CLASS_STATIC; } @@ -583,41 +583,41 @@ static Result> field_to_ast( static Result detect_bitfield(const StabsStructOrUnionType::Field& field, const StabsToAstState& state) { // Static fields can't be bitfields. - if(field.is_static) { + if (field.is_static) { return 0; } // Resolve type references. const StabsType* type = field.type.get(); - for(s32 i = 0; i < 50; i++) { - if(!type->descriptor.has_value()) { - if(!type->type_number.valid()) { + for (s32 i = 0; i < 50; i++) { + if (!type->descriptor.has_value()) { + if (!type->type_number.valid()) { return 0; } auto next_type = state.stabs_types->find(type->type_number); - if(next_type == state.stabs_types->end() || next_type->second == type) { + if (next_type == state.stabs_types->end() || next_type->second == type) { return 0; } type = next_type->second; - } else if(type->descriptor == StabsTypeDescriptor::TYPE_REFERENCE) { + } else if (type->descriptor == StabsTypeDescriptor::TYPE_REFERENCE) { type = type->as().type.get(); - } else if(type->descriptor == StabsTypeDescriptor::CONST_QUALIFIER) { + } else if (type->descriptor == StabsTypeDescriptor::CONST_QUALIFIER) { type = type->as().type.get(); - } else if(type->descriptor == StabsTypeDescriptor::VOLATILE_QUALIFIER) { + } else if (type->descriptor == StabsTypeDescriptor::VOLATILE_QUALIFIER) { type = type->as().type.get(); } else { break; } // Prevent an infinite loop if there's a cycle (fatal frame). - if(i == 49) { + if (i == 49) { return 0; } } // Determine the size of the underlying storage unit. s32 storage_unit_size_bits = 0; - switch(*type->descriptor) { + switch (*type->descriptor) { case ccc::StabsTypeDescriptor::RANGE: { Result bclass = classify_range(type->as()); CCC_RETURN_IF_ERROR(bclass); @@ -625,7 +625,7 @@ static Result detect_bitfield(const StabsStructOrUnionType::Field& field, c break; } case ccc::StabsTypeDescriptor::CROSS_REFERENCE: { - if(type->as().type == ast::ForwardDeclaredType::ENUM) { + if (type->as().type == ast::ForwardDeclaredType::ENUM) { storage_unit_size_bits = 32; } else { return 0; @@ -645,7 +645,7 @@ static Result detect_bitfield(const StabsStructOrUnionType::Field& field, c } } - if(storage_unit_size_bits == field.size_bits) { + if (storage_unit_size_bits == field.size_bits) { return 0; } @@ -655,12 +655,12 @@ static Result detect_bitfield(const StabsStructOrUnionType::Field& field, c static Result>> member_functions_to_ast( const StabsStructOrUnionType& type, const StabsToAstState& state, s32 depth) { - if(state.importer_flags & NO_MEMBER_FUNCTIONS) { + if (state.importer_flags & NO_MEMBER_FUNCTIONS) { return std::vector>(); } std::string_view type_name_no_template_args; - if(type.name.has_value()) { + if (type.name.has_value()) { type_name_no_template_args = std::string_view(*type.name).substr(0, type.name->find("<")); } @@ -668,15 +668,15 @@ static Result>> member_functions_to_ast( std::vector> member_functions; bool only_special_functions = true; - for(const StabsStructOrUnionType::MemberFunctionSet& function_set : type.member_functions) { + for (const StabsStructOrUnionType::MemberFunctionSet& function_set : type.member_functions) { MemberFunctionInfo info = check_member_function( function_set.name, type_name_no_template_args, state.demangler, state.importer_flags); - if(!info.is_special_member_function) { + if (!info.is_special_member_function) { only_special_functions = false; } - for(const StabsStructOrUnionType::MemberFunction& stabs_func : function_set.overloads) { + for (const StabsStructOrUnionType::MemberFunction& stabs_func : function_set.overloads) { auto node = stabs_type_to_ast( *stabs_func.type, &type, @@ -693,7 +693,7 @@ static Result>> member_functions_to_ast( (*node)->name = info.name; (*node)->set_access_specifier(stabs_field_visibility_to_access_specifier(stabs_func.visibility), state.importer_flags); - if((*node)->descriptor == ast::FUNCTION) { + if ((*node)->descriptor == ast::FUNCTION) { ast::Function& function = (*node)->as(); function.modifier = stabs_func.modifier; function.vtable_index = stabs_func.vtable_index; @@ -703,7 +703,7 @@ static Result>> member_functions_to_ast( } } - if(only_special_functions && (state.importer_flags & INCLUDE_GENERATED_MEMBER_FUNCTIONS) == 0) { + if (only_special_functions && (state.importer_flags & INCLUDE_GENERATED_MEMBER_FUNCTIONS) == 0) { return std::vector>(); } @@ -720,14 +720,14 @@ static MemberFunctionInfo check_member_function( // Some compiler versions output gcc opnames for overloaded operators // instead of their proper names. - if((importer_flags & DONT_DEMANGLE_NAMES) == 0 && demangler.cplus_demangle_opname) { + if ((importer_flags & DONT_DEMANGLE_NAMES) == 0 && demangler.cplus_demangle_opname) { char* demangled_name = demangler.cplus_demangle_opname(mangled_name.c_str(), 0); - if(demangled_name) { + if (demangled_name) { info.name = demangled_name; free(static_cast(demangled_name)); } } - if(info.name.empty()) { + if (info.name.empty()) { info.name = mangled_name; } @@ -736,7 +736,7 @@ static MemberFunctionInfo check_member_function( info.name == "__comp_ctor" || // Constructs virtual base classes. info.name == "__base_ctor"; // Does not construct virtual base classes. - if(!is_constructor && !type_name_no_template_args.empty()) { + if (!is_constructor && !type_name_no_template_args.empty()) { is_constructor |= info.name == type_name_no_template_args; // Named constructor. } @@ -746,7 +746,7 @@ static MemberFunctionInfo check_member_function( info.name == "__base_dtor" || // Does not construct virtual base classes. info.name == "__deleting_dtor"; // Destructs virtual base clases then deletes the entire object. - if(!is_destructor && !info.name.empty()) { + if (!is_destructor && !info.name.empty()) { is_destructor |= info.name[0] == '~' && std::string_view(info.name).substr(1) == type_name_no_template_args; // Named destructor. } @@ -768,23 +768,23 @@ void fix_recursively_emitted_structures( // The game Sega Soccer Slam is affected by this. See the PeculiarParameter // test case in mdebug_importer_tests.cpp for a bare bones example. - for(std::unique_ptr& node : outer_struct.member_functions) { - if(node->descriptor != ast::FUNCTION) { + for (std::unique_ptr& node : outer_struct.member_functions) { + if (node->descriptor != ast::FUNCTION) { continue; } ast::Function& function = node->as(); - if(!function.parameters.has_value()) { + if (!function.parameters.has_value()) { continue; } - for(std::unique_ptr& parameter : *function.parameters) { - if(parameter->descriptor != ast::POINTER_OR_REFERENCE) { + for (std::unique_ptr& parameter : *function.parameters) { + if (parameter->descriptor != ast::POINTER_OR_REFERENCE) { continue; } ast::PointerOrReference& pointer_or_reference = parameter->as(); - if(pointer_or_reference.value_type->descriptor != ast::STRUCT_OR_UNION) { + if (pointer_or_reference.value_type->descriptor != ast::STRUCT_OR_UNION) { continue; } @@ -795,15 +795,15 @@ void fix_recursively_emitted_structures( // parameters aren't even filled in by GCC, this is a really rare // case, so here we only bother to do some very basic checks to // verify that the inner struct is similar to the outer struct. - if(inner_struct.base_classes.size() != outer_struct.base_classes.size()) { + if (inner_struct.base_classes.size() != outer_struct.base_classes.size()) { continue; } - if(inner_struct.fields.size() != outer_struct.fields.size()) { + if (inner_struct.fields.size() != outer_struct.fields.size()) { continue; } - if(inner_struct.member_functions.size() != outer_struct.member_functions.size()) { + if (inner_struct.member_functions.size() != outer_struct.member_functions.size()) { continue; } @@ -821,7 +821,7 @@ void fix_recursively_emitted_structures( ast::AccessSpecifier stabs_field_visibility_to_access_specifier(StabsStructOrUnionType::Visibility visibility) { ast::AccessSpecifier access_specifier = ast::AS_PUBLIC; - switch(visibility) { + switch (visibility) { case StabsStructOrUnionType::Visibility::NONE: access_specifier = ast::AS_PUBLIC; break; case StabsStructOrUnionType::Visibility::PUBLIC: access_specifier = ast::AS_PUBLIC; break; case StabsStructOrUnionType::Visibility::PROTECTED: access_specifier = ast::AS_PROTECTED; break; diff --git a/src/ccc/symbol_database.cpp b/src/ccc/symbol_database.cpp index 8448d8ac..27783cc2 100644 --- a/src/ccc/symbol_database.cpp +++ b/src/ccc/symbol_database.cpp @@ -11,12 +11,12 @@ namespace ccc { template SymbolType* SymbolList::symbol_from_handle(SymbolHandle handle) { - if(!handle.valid()) { + if (!handle.valid()) { return nullptr; } size_t index = binary_search(handle); - if(index >= m_symbols.size() || m_symbols[index].m_handle != handle) { + if (index >= m_symbols.size() || m_symbols[index].m_handle != handle) { return nullptr; } @@ -34,9 +34,9 @@ std::vector SymbolList::symbols_from_handles( const std::vector>& handles) { std::vector result; - for(SymbolHandle handle : handles) { + for (SymbolHandle handle : handles) { SymbolType* symbol = symbol_from_handle(handle); - if(symbol) { + if (symbol) { result.emplace_back(symbol); } } @@ -48,9 +48,9 @@ std::vector SymbolList::symbols_from_handles( const std::vector>& handles) const { std::vector result; - for(SymbolHandle handle : handles) { + for (SymbolHandle handle : handles) { const SymbolType* symbol = symbol_from_handle(handle); - if(symbol) { + if (symbol) { result.emplace_back(symbol); } } @@ -61,7 +61,7 @@ template std::vector SymbolList::optional_symbols_from_handles( const std::optional>>& handles) { - if(handles.has_value()) { + if (handles.has_value()) { return symbols_from_handles(*handles); } else { return std::vector(); @@ -72,7 +72,7 @@ template std::vector SymbolList::optional_symbols_from_handles( const std::optional>>& handles) const { - if(handles.has_value()) { + if (handles.has_value()) { return symbols_from_handles(*handles); } else { return std::vector(); @@ -109,7 +109,7 @@ std::vector> SymbolList::handles_from_start std::vector> handles; auto [begin, end] = m_address_to_handle.equal_range(address.value); - for(auto iterator = begin; iterator != end; iterator++) { + for (auto iterator = begin; iterator != end; iterator++) { handles.emplace_back(iterator->second); } @@ -122,10 +122,10 @@ std::vector> SymbolList::handles_from_addre std::vector> handles; typename AddressToHandleMap::const_iterator begin, end; - if(range.low.valid()) { + if (range.low.valid()) { begin = m_address_to_handle.lower_bound(range.low.value); end = m_address_to_handle.lower_bound(range.high.value); - } else if(range.high.valid()) { + } else if (range.high.valid()) { begin = m_address_to_handle.begin(); end = m_address_to_handle.lower_bound(range.high.value); } else { @@ -133,7 +133,7 @@ std::vector> SymbolList::handles_from_addre end = m_address_to_handle.end(); } - for(auto iterator = begin; iterator != end; iterator++) { + for (auto iterator = begin; iterator != end; iterator++) { handles.emplace_back(iterator->second); } @@ -144,7 +144,7 @@ template SymbolHandle SymbolList::first_handle_from_starting_address(Address address) const { auto iterator = m_address_to_handle.find(address.value); - if(iterator != m_address_to_handle.end()) { + if (iterator != m_address_to_handle.end()) { return iterator->second; } else { return SymbolHandle(); @@ -157,7 +157,7 @@ std::vector> SymbolList::handles_from_name( std::vector> handles; auto [begin, end] = m_name_to_handle.equal_range(name); - for(auto iterator = begin; iterator != end; iterator++) { + for (auto iterator = begin; iterator != end; iterator++) { handles.emplace_back(iterator->second); } @@ -168,7 +168,7 @@ template SymbolHandle SymbolList::first_handle_after_address(Address address) const { auto iterator = m_address_to_handle.upper_bound(address.value); - if(iterator != m_address_to_handle.end()) { + if (iterator != m_address_to_handle.end()) { return iterator->second; } else { return SymbolHandle(); @@ -179,7 +179,7 @@ template SymbolHandle SymbolList::first_handle_from_name(const std::string& name) const { auto iterator = m_name_to_handle.find(name); - if(iterator != m_name_to_handle.end()) { + if (iterator != m_name_to_handle.end()) { return iterator->second; } else { return SymbolHandle(); @@ -190,10 +190,10 @@ template SymbolType* SymbolList::symbol_overlapping_address(Address address) { auto iterator = m_address_to_handle.upper_bound(address.value); - if(iterator != m_address_to_handle.begin()) { + if (iterator != m_address_to_handle.begin()) { iterator--; // Find the greatest element that is less than or equal to the address. SymbolType* symbol = symbol_from_handle(iterator->second); - if(symbol && address.value < symbol->address().value + symbol->size()) { + if (symbol && address.value < symbol->address().value + symbol->size()) { return symbol; } } @@ -209,12 +209,12 @@ const SymbolType* SymbolList::symbol_overlapping_address(Address add template s32 SymbolList::index_from_handle(SymbolHandle handle) const { - if(!handle.valid()) { + if (!handle.valid()) { return -1; } size_t index = binary_search(handle); - if(index >= m_symbols.size() || m_symbols[index].handle() != handle) { + if (index >= m_symbols.size() || m_symbols[index].handle() != handle) { return -1; } @@ -255,7 +255,7 @@ Result SymbolList::create_symbol( handle = m_next_handle; CCC_CHECK(handle != NULL_SYMBOL_HANDLE, "Ran out of handles to use for %s symbols.", SymbolType::NAME); - } while(!m_next_handle.compare_exchange_weak(handle, handle + 1)); + } while (!m_next_handle.compare_exchange_weak(handle, handle + 1)); SymbolType& symbol = m_symbols.emplace_back(); @@ -263,7 +263,7 @@ Result SymbolList::create_symbol( symbol.m_name = std::move(name); symbol.m_source = source; - if(module_symbol) { + if (module_symbol) { symbol.m_address = address.add_base_address(module_symbol->address()); symbol.m_module = module_symbol->handle(); } else { @@ -296,12 +296,12 @@ Result SymbolList::create_symbol( std::string demangled_name; if constexpr(SymbolType::FLAGS & NAME_NEEDS_DEMANGLING) { - if((importer_flags & DONT_DEMANGLE_NAMES) == 0 && demangler.cplus_demangle) { + if ((importer_flags & DONT_DEMANGLE_NAMES) == 0 && demangler.cplus_demangle) { int demangler_flags = 0; - if(importer_flags & DEMANGLE_PARAMETERS) demangler_flags |= DMGL_PARAMS; - if(importer_flags & DEMANGLE_RETURN_TYPE) demangler_flags |= DMGL_RET_POSTFIX; + if (importer_flags & DEMANGLE_PARAMETERS) demangler_flags |= DMGL_PARAMS; + if (importer_flags & DEMANGLE_RETURN_TYPE) demangler_flags |= DMGL_RET_POSTFIX; char* demangled_name_ptr = demangler.cplus_demangle(name.c_str(), demangler_flags); - if(demangled_name_ptr) { + if (demangled_name_ptr) { demangled_name = demangled_name_ptr; free(static_cast(demangled_name_ptr)); } @@ -314,7 +314,7 @@ Result SymbolList::create_symbol( CCC_RETURN_IF_ERROR(symbol); if constexpr(SymbolType::FLAGS & NAME_NEEDS_DEMANGLING) { - if(!demangled_name.empty()) { + if (!demangled_name.empty()) { (*symbol)->set_mangled_name(name); } } @@ -326,11 +326,11 @@ template bool SymbolList::move_symbol(SymbolHandle handle, Address new_address) { SymbolType* symbol = symbol_from_handle(handle); - if(!symbol) { + if (!symbol) { return false; } - if(symbol->address() != new_address) { + if (symbol->address() != new_address) { unlink_address_map(*symbol); symbol->m_address = new_address; link_address_map(*symbol); @@ -343,11 +343,11 @@ template bool SymbolList::rename_symbol(SymbolHandle handle, std::string new_name) { SymbolType* symbol = symbol_from_handle(handle); - if(!symbol) { + if (!symbol) { return false; } - if(symbol->name() != new_name) { + if (symbol->name() != new_name) { unlink_name_map(*symbol); symbol->m_name = std::move(new_name); link_name_map(*symbol); @@ -370,11 +370,11 @@ void SymbolList::merge_from(SymbolList& list) size_t lhs_pos = 0; size_t rhs_pos = 0; - for(;;) { + for (;;) { SymbolType* symbol; - if(lhs_pos < lhs.size() && (rhs_pos >= rhs.size() || lhs[lhs_pos].handle() < rhs[rhs_pos].handle())) { + if (lhs_pos < lhs.size() && (rhs_pos >= rhs.size() || lhs[lhs_pos].handle() < rhs[rhs_pos].handle())) { symbol = &m_symbols.emplace_back(std::move(lhs[lhs_pos++])); - } else if(rhs_pos < rhs.size()) { + } else if (rhs_pos < rhs.size()) { symbol = &m_symbols.emplace_back(std::move(rhs[rhs_pos++])); } else { break; @@ -395,7 +395,7 @@ template bool SymbolList::mark_symbol_for_destruction(SymbolHandle handle, SymbolDatabase* database) { SymbolType* symbol = symbol_from_handle(handle); - if(!symbol) { + if (!symbol) { return false; } @@ -409,8 +409,8 @@ bool SymbolList::mark_symbol_for_destruction(SymbolHandle void SymbolList::mark_symbols_from_source_for_destruction(SymbolSourceHandle source, SymbolDatabase* database) { - for(SymbolType& symbol : m_symbols) { - if(symbol.source() != source) { + for (SymbolType& symbol : m_symbols) { + if (symbol.source() != source) { continue; } @@ -423,8 +423,8 @@ void SymbolList::mark_symbols_from_source_for_destruction(SymbolSour template void SymbolList::mark_symbols_from_module_for_destruction(ModuleHandle module_handle, SymbolDatabase* database) { - for(SymbolType& symbol : m_symbols) { - if(symbol.module_handle() != module_handle) { + for (SymbolType& symbol : m_symbols) { + if (symbol.module_handle() != module_handle) { continue; } @@ -438,8 +438,8 @@ template void SymbolList::destroy_marked_symbols() { std::vector remaining_symbols; - for(SymbolType& symbol : m_symbols) { - if(symbol.m_marked_for_destruction) { + for (SymbolType& symbol : m_symbols) { + if (symbol.m_marked_for_destruction) { unlink_address_map(symbol); unlink_name_map(symbol); } else { @@ -464,11 +464,11 @@ size_t SymbolList::binary_search(SymbolHandle handle) co size_t begin = 0; size_t end = m_symbols.size(); - while(begin < end) { + while (begin < end) { size_t mid = (begin + end) / 2; - if(m_symbols[mid].handle() < handle) { + if (m_symbols[mid].handle() < handle) { begin = mid + 1; - } else if(m_symbols[mid].handle() > handle) { + } else if (m_symbols[mid].handle() > handle) { end = mid; } else { return mid; @@ -482,7 +482,7 @@ template void SymbolList::link_address_map(SymbolType& symbol) { if constexpr((SymbolType::FLAGS & WITH_ADDRESS_MAP)) { - if(symbol.address().valid()) { + if (symbol.address().valid()) { m_address_to_handle.emplace(symbol.address().value, symbol.handle()); } } @@ -492,10 +492,10 @@ template void SymbolList::unlink_address_map(SymbolType& symbol) { if constexpr(SymbolType::FLAGS & WITH_ADDRESS_MAP) { - if(symbol.address().valid()) { + if (symbol.address().valid()) { auto iterators = m_address_to_handle.equal_range(symbol.address().value); - for(auto iterator = iterators.first; iterator != iterators.second; iterator++) { - if(iterator->second == symbol.handle()) { + for (auto iterator = iterators.first; iterator != iterators.second; iterator++) { + if (iterator->second == symbol.handle()) { m_address_to_handle.erase(iterator); break; } @@ -517,8 +517,8 @@ void SymbolList::unlink_name_map(SymbolType& symbol) { if constexpr(SymbolType::FLAGS & WITH_NAME_MAP) { auto iterators = m_name_to_handle.equal_range(symbol.name()); - for(auto iterator = iterators.first; iterator != iterators.second; iterator++) { - if(iterator->second == symbol.handle()) { + for (auto iterator = iterators.first; iterator != iterators.second; iterator++) { + if (iterator->second == symbol.handle()) { m_name_to_handle.erase(iterator); break; } @@ -550,7 +550,7 @@ void Symbol::set_type(std::unique_ptr type) const char* global_storage_location_to_string(GlobalStorageLocation location) { - switch(location) { + switch (location) { case NIL: return "nil"; case DATA: return "data"; case BSS: return "bss"; @@ -575,10 +575,10 @@ const std::optional>& Function::parameter_v void Function::set_parameter_variables( std::optional> parameter_variables, SymbolDatabase& database) { - if(m_parameter_variables.has_value()) { - for(ParameterVariableHandle parameter_variable_handle : *m_parameter_variables) { + if (m_parameter_variables.has_value()) { + for (ParameterVariableHandle parameter_variable_handle : *m_parameter_variables) { ParameterVariable* parameter_variable = database.parameter_variables.symbol_from_handle(parameter_variable_handle); - if(parameter_variable && parameter_variable->m_function == handle()) { + if (parameter_variable && parameter_variable->m_function == handle()) { parameter_variable->m_function = FunctionHandle(); } } @@ -586,10 +586,10 @@ void Function::set_parameter_variables( m_parameter_variables = std::move(parameter_variables); - if(m_parameter_variables.has_value()) { - for(ParameterVariableHandle parameter_variable_handle : *m_parameter_variables) { + if (m_parameter_variables.has_value()) { + for (ParameterVariableHandle parameter_variable_handle : *m_parameter_variables) { ParameterVariable* parameter_variable = database.parameter_variables.symbol_from_handle(parameter_variable_handle); - if(parameter_variable) { + if (parameter_variable) { parameter_variable->m_function = handle(); } } @@ -604,10 +604,10 @@ const std::optional>& Function::local_variables void Function::set_local_variables( std::optional> local_variables, SymbolDatabase& database) { - if(m_local_variables.has_value()) { - for(LocalVariableHandle local_variable_handle : *m_local_variables) { + if (m_local_variables.has_value()) { + for (LocalVariableHandle local_variable_handle : *m_local_variables) { LocalVariable* local_variable = database.local_variables.symbol_from_handle(local_variable_handle); - if(local_variable && local_variable->m_function == handle()) { + if (local_variable && local_variable->m_function == handle()) { local_variable->m_function = FunctionHandle(); } } @@ -615,10 +615,10 @@ void Function::set_local_variables( m_local_variables = std::move(local_variables); - if(m_local_variables.has_value()) { - for(LocalVariableHandle local_variable_handle : *m_local_variables) { + if (m_local_variables.has_value()) { + for (LocalVariableHandle local_variable_handle : *m_local_variables) { LocalVariable* local_variable = database.local_variables.symbol_from_handle(local_variable_handle); - if(local_variable) { + if (local_variable) { local_variable->m_function = handle(); } } @@ -627,7 +627,7 @@ void Function::set_local_variables( const std::string& Function::mangled_name() const { - if(!m_mangled_name.empty()) { + if (!m_mangled_name.empty()) { return m_mangled_name; } else { return name(); @@ -661,18 +661,18 @@ void Function::set_current_hash(FunctionHash hash) void Function::on_destroy(SymbolDatabase* database) { - if(!database) { + if (!database) { return; } - if(m_parameter_variables.has_value()) { - for(ParameterVariableHandle parameter_variable : *m_parameter_variables) { + if (m_parameter_variables.has_value()) { + for (ParameterVariableHandle parameter_variable : *m_parameter_variables) { database->parameter_variables.mark_symbol_for_destruction(parameter_variable, database); } } - if(m_local_variables.has_value()) { - for(LocalVariableHandle local_variable : *m_local_variables) { + if (m_local_variables.has_value()) { + for (LocalVariableHandle local_variable : *m_local_variables) { database->local_variables.mark_symbol_for_destruction(local_variable, database); } } @@ -682,7 +682,7 @@ void Function::on_destroy(SymbolDatabase* database) const std::string& GlobalVariable::mangled_name() const { - if(!m_mangled_name.empty()) { + if (!m_mangled_name.empty()) { return m_mangled_name; } else { return name(); @@ -731,18 +731,18 @@ const std::vector& SourceFile::functions() const void SourceFile::set_functions(std::vector functions, SymbolDatabase& database) { - for(FunctionHandle function_handle : m_functions) { + for (FunctionHandle function_handle : m_functions) { Function* function = database.functions.symbol_from_handle(function_handle); - if(function && function->m_source_file == handle()) { + if (function && function->m_source_file == handle()) { function->m_source_file = SourceFileHandle(); } } m_functions = std::move(functions); - for(FunctionHandle function_handle : m_functions) { + for (FunctionHandle function_handle : m_functions) { Function* function = database.functions.symbol_from_handle(function_handle); - if(function) { + if (function) { function->m_source_file = handle(); } } @@ -755,18 +755,18 @@ const std::vector& SourceFile::global_variables() const void SourceFile::set_global_variables(std::vector global_variables, SymbolDatabase& database) { - for(GlobalVariableHandle global_variable_handle : m_global_variables) { + for (GlobalVariableHandle global_variable_handle : m_global_variables) { GlobalVariable* global_variable = database.global_variables.symbol_from_handle(global_variable_handle); - if(global_variable && global_variable->m_source_file == handle()) { + if (global_variable && global_variable->m_source_file == handle()) { global_variable->m_source_file = SourceFileHandle(); } } m_global_variables = std::move(global_variables); - for(GlobalVariableHandle global_variable_handle : m_global_variables) { + for (GlobalVariableHandle global_variable_handle : m_global_variables) { GlobalVariable* global_variable = database.global_variables.symbol_from_handle(global_variable_handle); - if(global_variable) { + if (global_variable) { global_variable->m_source_file = handle(); } } @@ -781,13 +781,13 @@ void SourceFile::check_functions_match(const SymbolDatabase& database) { u32 matching = 0; u32 modified = 0; - for(FunctionHandle function_handle : functions()) { + for (FunctionHandle function_handle : functions()) { const ccc::Function* function = database.functions.symbol_from_handle(function_handle); - if(!function || function->original_hash() == 0) { + if (!function || function->original_hash() == 0) { continue; } - if(function->current_hash() == function->original_hash()) { + if (function->current_hash() == function->original_hash()) { matching++; } else { modified++; @@ -799,15 +799,15 @@ void SourceFile::check_functions_match(const SymbolDatabase& database) void SourceFile::on_destroy(SymbolDatabase* database) { - if(!database) { + if (!database) { return; } - for(FunctionHandle function : m_functions) { + for (FunctionHandle function : m_functions) { database->functions.mark_symbol_for_destruction(function, database); } - for(GlobalVariableHandle global_variable : m_global_variables) { + for (GlobalVariableHandle global_variable : m_global_variables) { database->global_variables.mark_symbol_for_destruction(global_variable, database); } } @@ -842,11 +842,11 @@ const Symbol* SymbolDatabase::symbol_starting_at_address( { #define CCC_X(SymbolType, symbol_list) \ if constexpr(SymbolType::FLAGS & WITH_ADDRESS_MAP) { \ - if(descriptors & SymbolType::DESCRIPTOR) { \ + if (descriptors & SymbolType::DESCRIPTOR) { \ const SymbolHandle handle = symbol_list.first_handle_from_starting_address(address); \ const SymbolType* symbol = symbol_list.symbol_from_handle(handle); \ - if(symbol) { \ - if(descriptor_out) { \ + if (symbol) { \ + if (descriptor_out) { \ *descriptor_out = SymbolType::DESCRIPTOR; \ } \ return symbol; \ @@ -864,11 +864,11 @@ const Symbol* SymbolDatabase::symbol_after_address( const Symbol* result = nullptr; #define CCC_X(SymbolType, symbol_list) \ if constexpr(SymbolType::FLAGS & WITH_ADDRESS_MAP) { \ - if(descriptors & SymbolType::DESCRIPTOR) { \ + if (descriptors & SymbolType::DESCRIPTOR) { \ const SymbolHandle handle = symbol_list.first_handle_after_address(address); \ const SymbolType* symbol = symbol_list.symbol_from_handle(handle); \ - if(symbol && (!result || symbol->address() < result->address())) { \ - if(descriptor_out) { \ + if (symbol && (!result || symbol->address() < result->address())) { \ + if (descriptor_out) { \ *descriptor_out = SymbolType::DESCRIPTOR; \ } \ result = symbol; \ @@ -885,10 +885,10 @@ const Symbol* SymbolDatabase::symbol_overlapping_address( { #define CCC_X(SymbolType, symbol_list) \ if constexpr(SymbolType::FLAGS & WITH_ADDRESS_MAP) { \ - if(descriptors & SymbolType::DESCRIPTOR) { \ + if (descriptors & SymbolType::DESCRIPTOR) { \ const SymbolType* symbol = symbol_list.symbol_overlapping_address(address); \ - if(symbol) { \ - if(descriptor_out) { \ + if (symbol) { \ + if (descriptor_out) { \ *descriptor_out = SymbolType::DESCRIPTOR; \ } \ return symbol; \ @@ -905,11 +905,11 @@ const Symbol* SymbolDatabase::symbol_with_name( { #define CCC_X(SymbolType, symbol_list) \ if constexpr(SymbolType::FLAGS & WITH_ADDRESS_MAP) { \ - if(descriptors & SymbolType::DESCRIPTOR) { \ + if (descriptors & SymbolType::DESCRIPTOR) { \ const SymbolHandle handle = symbol_list.first_handle_from_name(name); \ const SymbolType* symbol = symbol_list.symbol_from_handle(handle); \ - if(symbol) { \ - if(descriptor_out) { \ + if (symbol) { \ + if (descriptor_out) { \ *descriptor_out = SymbolType::DESCRIPTOR; \ } \ return symbol; \ @@ -924,7 +924,7 @@ const Symbol* SymbolDatabase::symbol_with_name( Result SymbolDatabase::get_symbol_source(const std::string& name) { SymbolSourceHandle handle = symbol_sources.first_handle_from_name(name); - if(!handle.valid()) { + if (!handle.valid()) { Result source = symbol_sources.create_symbol(name, SymbolSourceHandle(), nullptr); CCC_RETURN_IF_ERROR(source); handle = (*source)->handle(); @@ -941,13 +941,13 @@ Result SymbolDatabase::create_data_type_if_unique( { auto types_with_same_name = data_types.handles_from_name(name); const char* compare_fail_reason = nullptr; - if(types_with_same_name.begin() == types_with_same_name.end()) { + if (types_with_same_name.begin() == types_with_same_name.end()) { // No types with this name have previously been processed. Result data_type = data_types.create_symbol(name, group.source, group.module_symbol); CCC_RETURN_IF_ERROR(data_type); (*data_type)->files = {source_file.handle()}; - if(number.type > -1) { + if (number.type > -1) { source_file.stabs_type_number_to_handle[number] = (*data_type)->handle(); } @@ -958,34 +958,34 @@ Result SymbolDatabase::create_data_type_if_unique( // Types with this name have previously been processed, we need to // figure out if this one matches any of the previous ones. bool match = false; - for(DataTypeHandle existing_type_handle : types_with_same_name) { + for (DataTypeHandle existing_type_handle : types_with_same_name) { DataType* existing_type = data_types.symbol_from_handle(existing_type_handle); CCC_ASSERT(existing_type); // We don't want to merge together types from different sources or // modules so that we can destroy all the types from one source // without breaking anything else. - if(!group.is_in_group(*existing_type)) { + if (!group.is_in_group(*existing_type)) { continue; } CCC_ASSERT(existing_type->type()); ast::CompareResult compare_result = compare_nodes(*existing_type->type(), *node.get(), this, true); - if(compare_result.type == ast::CompareResultType::DIFFERS) { + if (compare_result.type == ast::CompareResultType::DIFFERS) { // The new node doesn't match this existing node. bool is_anonymous_enum = existing_type->type()->descriptor == ast::ENUM && existing_type->name().empty(); - if(!is_anonymous_enum) { + if (!is_anonymous_enum) { existing_type->compare_fail_reason = compare_fail_reason_to_string(compare_result.fail_reason); compare_fail_reason = compare_fail_reason_to_string(compare_result.fail_reason); } } else { // The new node matches this existing node. existing_type->files.emplace_back(source_file.handle()); - if(number.type > -1) { + if (number.type > -1) { source_file.stabs_type_number_to_handle[number] = existing_type->handle(); } - if(compare_result.type == ast::CompareResultType::MATCHES_FAVOUR_RHS) { + if (compare_result.type == ast::CompareResultType::MATCHES_FAVOUR_RHS) { // The new node almost matches the old one, but the new one // is slightly better, so we replace the old type. existing_type->set_type(std::move(node)); @@ -995,14 +995,14 @@ Result SymbolDatabase::create_data_type_if_unique( } } - if(!match) { + if (!match) { // This type doesn't match any of the others with the same name // that have already been processed. Result data_type = data_types.create_symbol(name, group.source, group.module_symbol); CCC_RETURN_IF_ERROR(data_type); (*data_type)->files = {source_file.handle()}; - if(number.type > -1) { + if (number.type > -1) { source_file.stabs_type_number_to_handle[number] = (*data_type)->handle(); } (*data_type)->compare_fail_reason = compare_fail_reason; @@ -1088,11 +1088,11 @@ RawSymbolHandle MultiSymbolHandle::handle() const Symbol* MultiSymbolHandle::lookup_symbol(SymbolDatabase& database) { - if(m_handle == NULL_SYMBOL_HANDLE) { + if (m_handle == NULL_SYMBOL_HANDLE) { return nullptr; } - switch(m_descriptor) { + switch (m_descriptor) { #define CCC_X(SymbolType, symbol_list) \ case SymbolType::DESCRIPTOR: \ return database.symbol_list.symbol_from_handle(m_handle); @@ -1110,8 +1110,8 @@ const Symbol* MultiSymbolHandle::lookup_symbol(const SymbolDatabase& database) c bool MultiSymbolHandle::is_flag_set(SymbolFlag flag) const { - if(m_handle != NULL_SYMBOL_HANDLE) { - switch(m_descriptor) { + if (m_handle != NULL_SYMBOL_HANDLE) { + switch (m_descriptor) { #define CCC_X(SymbolType, symbol_list) \ case SymbolType::DESCRIPTOR: \ return SymbolType::FLAGS & flag; @@ -1125,8 +1125,8 @@ bool MultiSymbolHandle::is_flag_set(SymbolFlag flag) const bool MultiSymbolHandle::move_symbol(Address new_address, SymbolDatabase& database) const { - if(m_handle != NULL_SYMBOL_HANDLE) { - switch(m_descriptor) { + if (m_handle != NULL_SYMBOL_HANDLE) { + switch (m_descriptor) { #define CCC_X(SymbolType, symbol_list) \ case SymbolType::DESCRIPTOR: \ return database.symbol_list.move_symbol(m_handle, new_address); @@ -1140,8 +1140,8 @@ bool MultiSymbolHandle::move_symbol(Address new_address, SymbolDatabase& databas bool MultiSymbolHandle::rename_symbol(std::string new_name, SymbolDatabase& database) const { - if(m_handle != NULL_SYMBOL_HANDLE) { - switch(m_descriptor) { + if (m_handle != NULL_SYMBOL_HANDLE) { + switch (m_descriptor) { #define CCC_X(SymbolType, symbol_list) \ case SymbolType::DESCRIPTOR: \ return database.symbol_list.rename_symbol(m_handle, std::move(new_name)); @@ -1157,10 +1157,10 @@ bool MultiSymbolHandle::destroy_symbol(SymbolDatabase& database, bool destroy_de { bool success = false; - if(m_handle != NULL_SYMBOL_HANDLE) { + if (m_handle != NULL_SYMBOL_HANDLE) { SymbolDatabase* database_ptr = destroy_descendants ? &database : nullptr; - switch(m_descriptor) { + switch (m_descriptor) { #define CCC_X(SymbolType, symbol_list) \ case SymbolType::DESCRIPTOR: \ success = database.symbol_list.mark_symbol_for_destruction(m_handle, database_ptr); \ @@ -1170,7 +1170,7 @@ bool MultiSymbolHandle::destroy_symbol(SymbolDatabase& database, bool destroy_de } } - if(success) { + if (success) { database.destroy_marked_symbols(); } @@ -1209,9 +1209,9 @@ const MultiSymbolHandle& NodeHandle::symbol() const const ast::Node* NodeHandle::lookup_node(const SymbolDatabase& database) const { - if(m_symbol.valid()) { + if (m_symbol.valid()) { const Symbol* symbol = m_symbol.lookup_symbol(database); - if(!symbol || symbol->generation() != m_generation) { + if (!symbol || symbol->generation() != m_generation) { return nullptr; } } diff --git a/src/ccc/symbol_database.h b/src/ccc/symbol_database.h index bf638725..827913c3 100644 --- a/src/ccc/symbol_database.h +++ b/src/ccc/symbol_database.h @@ -645,7 +645,7 @@ class SymbolDatabase { void for_each_symbol(Callback callback) { // Use indices here to avoid iterator invalidation. #define CCC_X(SymbolType, symbol_list) \ - for(s32 i = 0; i < symbol_list.size(); i++) { \ + for (s32 i = 0; i < symbol_list.size(); i++) { \ callback(symbol_list.symbol_from_index(i)); \ } CCC_FOR_EACH_SYMBOL_TYPE_DO_X diff --git a/src/ccc/symbol_file.cpp b/src/ccc/symbol_file.cpp index d645926a..6d4746de 100644 --- a/src/ccc/symbol_file.cpp +++ b/src/ccc/symbol_file.cpp @@ -12,7 +12,7 @@ Result> parse_symbol_file(std::vector image, std std::unique_ptr symbol_file; - switch(*magic) { + switch (*magic) { case CCC_FOURCC("\x7f""ELF"): { Result elf = ElfFile::parse(std::move(image)); CCC_RETURN_IF_ERROR(elf); @@ -50,14 +50,14 @@ Result>> ElfSymbolFile::get_all_symbol_ symbol_tables.emplace_back(std::make_unique(m_elf)); - for(size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { + for (size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { const SymbolTableFormatInfo& info = SYMBOL_TABLE_FORMATS[i]; const ElfSection* section = m_elf.lookup_section(info.section_name); - if(section) { + if (section) { Result> symbol_table = create_elf_symbol_table(*section, m_elf, info.format); CCC_RETURN_IF_ERROR(symbol_table); - if(*symbol_table) { + if (*symbol_table) { symbol_tables.emplace_back(std::move(*symbol_table)); } } @@ -71,13 +71,13 @@ Result>> ElfSymbolFile::get_symbol_tabl { std::vector> symbol_tables; - for(const SymbolTableLocation& location : sections) { + for (const SymbolTableLocation& location : sections) { const ElfSection* section = m_elf.lookup_section(location.section_name.c_str()); CCC_CHECK(section, "No '%s' section.", location.section_name.c_str()); Result> symbol_table = create_elf_symbol_table(*section, m_elf, location.format); CCC_RETURN_IF_ERROR(symbol_table); - if(*symbol_table) { + if (*symbol_table) { symbol_tables.emplace_back(std::move(*symbol_table)); } } diff --git a/src/ccc/symbol_json.cpp b/src/ccc/symbol_json.cpp index 1f6b66f0..a725eb54 100644 --- a/src/ccc/symbol_json.cpp +++ b/src/ccc/symbol_json.cpp @@ -48,7 +48,7 @@ void write_json( json.String(application_name); #define CCC_X(SymbolType, symbol_list) \ - if(!std::is_same_v) { \ + if (!std::is_same_v) { \ json.Key(#symbol_list); \ write_symbol_list(json, database.symbol_list, database, sources); \ } @@ -66,36 +66,36 @@ static void write_symbol_list( const std::set* sources) { json.StartArray(); - for(const SymbolType& symbol : list) { - if(sources && !sources->contains(symbol.source())) { + for (const SymbolType& symbol : list) { + if (sources && !sources->contains(symbol.source())) { continue; } json.StartObject(); - if(!symbol.name().empty()) { + if (!symbol.name().empty()) { json.Key("name"); json.String(symbol.name()); } - if(symbol.address().valid()) { + if (symbol.address().valid()) { json.Key("address"); json.Uint(symbol.address().value); } - if(symbol.size() != 0) { + if (symbol.size() != 0) { json.Key("size"); json.Uint(symbol.size()); } - if(symbol.module_handle().valid()) { + if (symbol.module_handle().valid()) { json.Key("module"); json.Uint(database.modules.index_from_handle(symbol.module_handle())); } write_json(json, symbol, database); - if(symbol.type()) { + if (symbol.type()) { json.Key("type"); ast::write_json(json, symbol.type(), database); } @@ -150,10 +150,10 @@ static void write_json(JsonWriter& json, const StackStorage& storage, const Symb static void write_json(JsonWriter& json, const DataType& symbol, const SymbolDatabase& database) { - if(symbol.files.empty()) { + if (symbol.files.empty()) { json.Key("files"); json.StartArray(); - for(SourceFileHandle file : symbol.files) { + for (SourceFileHandle file : symbol.files) { json.Uint(file.value); } json.EndArray(); @@ -162,25 +162,25 @@ static void write_json(JsonWriter& json, const DataType& symbol, const SymbolDat static void write_json(JsonWriter& json, const Function& symbol, const SymbolDatabase& database) { - if(!symbol.relative_path.empty()) { + if (!symbol.relative_path.empty()) { json.Key("relative_path"); json.String(symbol.relative_path); } - if(symbol.storage_class != STORAGE_CLASS_NONE) { + if (symbol.storage_class != STORAGE_CLASS_NONE) { json.Key("storage_class"); json.String(ast::storage_class_to_string(symbol.storage_class)); } - if(symbol.stack_frame_size > -1) { + if (symbol.stack_frame_size > -1) { json.Key("stack_frame_size"); json.Int(symbol.stack_frame_size); } - if(!symbol.line_numbers.empty()) { + if (!symbol.line_numbers.empty()) { json.Key("line_numbers"); json.StartArray(); - for(const Function::LineNumberPair& pair : symbol.line_numbers) { + for (const Function::LineNumberPair& pair : symbol.line_numbers) { json.StartArray(); json.Uint(pair.address.value); json.Int(pair.line_number); @@ -189,10 +189,10 @@ static void write_json(JsonWriter& json, const Function& symbol, const SymbolDat json.EndArray(); } - if(!symbol.sub_source_files.empty()) { + if (!symbol.sub_source_files.empty()) { json.Key("sub_source_files"); json.StartArray(); - for(const Function::SubSourceFile& sub : symbol.sub_source_files) { + for (const Function::SubSourceFile& sub : symbol.sub_source_files) { json.StartArray(); json.Uint(sub.address.value); json.String(sub.relative_path); @@ -201,36 +201,36 @@ static void write_json(JsonWriter& json, const Function& symbol, const SymbolDat json.EndArray(); } - if(symbol.is_member_function_ish) { + if (symbol.is_member_function_ish) { json.Key("is_member_function_ish"); json.Bool(symbol.is_member_function_ish); } - if(symbol.parameter_variables().has_value()) { + if (symbol.parameter_variables().has_value()) { json.Key("parameter_variables"); json.StartArray(); - for(ParameterVariableHandle handle : *symbol.parameter_variables()) { + for (ParameterVariableHandle handle : *symbol.parameter_variables()) { s32 index = database.parameter_variables.index_from_handle(handle); - if(index != -1) { + if (index != -1) { json.Int(index); } } json.EndArray(); } - if(symbol.local_variables().has_value()) { + if (symbol.local_variables().has_value()) { json.Key("local_variables"); json.StartArray(); - for(LocalVariableHandle handle : *symbol.local_variables()) { + for (LocalVariableHandle handle : *symbol.local_variables()) { s32 index = database.local_variables.index_from_handle(handle); - if(index != -1) { + if (index != -1) { json.Int(index); } } json.EndArray(); } - if(symbol.original_hash() != 0) { + if (symbol.original_hash() != 0) { json.Key("hash"); json.Uint(symbol.original_hash()); } @@ -240,7 +240,7 @@ static void write_json(JsonWriter& json, const GlobalVariable& symbol, const Sym { write_json(json, symbol.storage, database); - if(symbol.storage_class != STORAGE_CLASS_NONE) { + if (symbol.storage_class != STORAGE_CLASS_NONE) { json.Key("storage_class"); json.String(ast::storage_class_to_string(symbol.storage_class)); } @@ -250,19 +250,19 @@ static void write_json(JsonWriter& json, const Label& symbol, const SymbolDataba static void write_json(JsonWriter& json, const LocalVariable& symbol, const SymbolDatabase& database) { - if(const GlobalStorage* storage = std::get_if(&symbol.storage)) { + if (const GlobalStorage* storage = std::get_if(&symbol.storage)) { write_json(json, *storage, database); } - if(const RegisterStorage* storage = std::get_if(&symbol.storage)) { + if (const RegisterStorage* storage = std::get_if(&symbol.storage)) { write_json(json, *storage, database); } - if(const StackStorage* storage = std::get_if(&symbol.storage)) { + if (const StackStorage* storage = std::get_if(&symbol.storage)) { write_json(json, *storage, database); } - if(symbol.live_range.low.valid() && symbol.live_range.high.valid()) { + if (symbol.live_range.low.valid() && symbol.live_range.high.valid()) { json.Key("live_range"); json.StartArray(); json.Uint(symbol.live_range.low.value); @@ -275,11 +275,11 @@ static void write_json(JsonWriter& json, const Module& symbol, const SymbolDatab static void write_json(JsonWriter& json, const ParameterVariable& symbol, const SymbolDatabase& database) { - if(const RegisterStorage* storage = std::get_if(&symbol.storage)) { + if (const RegisterStorage* storage = std::get_if(&symbol.storage)) { write_json(json, *storage, database); } - if(const StackStorage* storage = std::get_if(&symbol.storage)) { + if (const StackStorage* storage = std::get_if(&symbol.storage)) { write_json(json, *storage, database); } } @@ -288,43 +288,43 @@ static void write_json(JsonWriter& json, const Section& symbol, const SymbolData static void write_json(JsonWriter& json, const SourceFile& symbol, const SymbolDatabase& database) { - if(!symbol.working_dir.empty()) { + if (!symbol.working_dir.empty()) { json.Key("working_dir"); json.String(symbol.working_dir); } - if(!symbol.command_line_path.empty()) { + if (!symbol.command_line_path.empty()) { json.Key("command_line_path"); json.String(symbol.command_line_path); } - if(!symbol.toolchain_version_info.empty()) { + if (!symbol.toolchain_version_info.empty()) { json.Key("toolchain_version"); json.StartArray(); - for(const std::string& info : symbol.toolchain_version_info) { + for (const std::string& info : symbol.toolchain_version_info) { json.String(info); } json.EndArray(); } - if(!symbol.functions().empty()) { + if (!symbol.functions().empty()) { json.Key("functions"); json.StartArray(); - for(FunctionHandle handle : symbol.functions()) { + for (FunctionHandle handle : symbol.functions()) { s32 index = database.functions.index_from_handle(handle); - if(index != -1) { + if (index != -1) { json.Int(index); } } json.EndArray(); } - if(!symbol.global_variables().empty()) { + if (!symbol.global_variables().empty()) { json.Key("global_variables"); json.StartArray(); - for(GlobalVariableHandle handle : symbol.global_variables()) { + for (GlobalVariableHandle handle : symbol.global_variables()) { s32 index = database.global_variables.index_from_handle(handle); - if(index != -1) { + if (index != -1) { json.Int(index); } } diff --git a/src/ccc/symbol_table.cpp b/src/ccc/symbol_table.cpp index 67d31eca..3686b5aa 100644 --- a/src/ccc/symbol_table.cpp +++ b/src/ccc/symbol_table.cpp @@ -19,8 +19,8 @@ const std::vector SYMBOL_TABLE_FORMATS = { const SymbolTableFormatInfo* symbol_table_format_from_enum(SymbolTableFormat format) { - for(size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { - if(SYMBOL_TABLE_FORMATS[i].format == format) { + for (size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { + if (SYMBOL_TABLE_FORMATS[i].format == format) { return &SYMBOL_TABLE_FORMATS[i]; } } @@ -29,8 +29,8 @@ const SymbolTableFormatInfo* symbol_table_format_from_enum(SymbolTableFormat for const SymbolTableFormatInfo* symbol_table_format_from_name(const char* format_name) { - for(size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { - if(strcmp(SYMBOL_TABLE_FORMATS[i].format_name, format_name) == 0) { + for (size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { + if (strcmp(SYMBOL_TABLE_FORMATS[i].format_name, format_name) == 0) { return &SYMBOL_TABLE_FORMATS[i]; } } @@ -39,8 +39,8 @@ const SymbolTableFormatInfo* symbol_table_format_from_name(const char* format_na const SymbolTableFormatInfo* symbol_table_format_from_section(const char* section_name) { - for(size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { - if(strcmp(SYMBOL_TABLE_FORMATS[i].section_name, section_name) == 0) { + for (size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { + if (strcmp(SYMBOL_TABLE_FORMATS[i].section_name, section_name) == 0) { return &SYMBOL_TABLE_FORMATS[i]; } } @@ -53,7 +53,7 @@ Result> create_elf_symbol_table( const ElfSection& section, const ElfFile& elf, SymbolTableFormat format) { std::unique_ptr symbol_table; - switch(format) { + switch (format) { case MDEBUG: { symbol_table = std::make_unique(elf.image, (s32) section.header.offset); break; @@ -82,7 +82,7 @@ Result> create_elf_symbol_table( "Section '%s' out of range.", section.name.c_str()); std::span data = std::span(elf.image).subspan(section.header.offset, section.header.size); - if(data.size() >= 4 && data[0] != '\0') { + if (data.size() >= 4 && data[0] != '\0') { Result file = parse_sndll_file(data, Address::non_zero(section.header.addr), SNDLLType::SNDATA_SECTION); CCC_RETURN_IF_ERROR(file); @@ -114,11 +114,11 @@ Result import_symbol_tables( ModuleHandle module_handle = (*module_symbol)->handle(); - for(const std::unique_ptr& symbol_table : symbol_tables) { + for (const std::unique_ptr& symbol_table : symbol_tables) { // Find a symbol source object with the right name, or create one if one // doesn't already exist. Result source = database.get_symbol_source(symbol_table->name()); - if(!source.success()) { + if (!source.success()) { database.destroy_symbols_from_module(module_handle, false); return source; } @@ -130,7 +130,7 @@ Result import_symbol_tables( Result result = symbol_table->import( database, group, importer_flags, demangler, interrupt); - if(!result.success()) { + if (!result.success()) { database.destroy_symbols_from_module(module_handle, false); return result; } diff --git a/src/ccc/util.cpp b/src/ccc/util.cpp index a16d9c56..425de368 100644 --- a/src/ccc/util.cpp +++ b/src/ccc/util.cpp @@ -13,7 +13,7 @@ Error format_error(const char* source_file, int source_line, const char* format, va_start(args, format); char message[4096]; - if(vsnprintf(message, sizeof(message), format, args) < 0) { + if (vsnprintf(message, sizeof(message), format, args) < 0) { strncpy(message, "Failed to generate error message.", sizeof(message)); } @@ -28,7 +28,7 @@ Error format_error(const char* source_file, int source_line, const char* format, void report_error(const Error& error) { - if(custom_error_callback) { + if (custom_error_callback) { custom_error_callback(error, ERROR_LEVEL_ERROR); } else { fprintf(stderr, "[%s:%d] " CCC_ANSI_COLOUR_RED "error:" CCC_ANSI_COLOUR_OFF " %s\n", @@ -38,7 +38,7 @@ void report_error(const Error& error) void report_warning(const Error& warning) { - if(custom_error_callback) { + if (custom_error_callback) { custom_error_callback(warning, ERROR_LEVEL_WARNING); } else { fprintf(stderr, "[%s:%d] " CCC_ANSI_COLOUR_MAGENTA "warning:" CCC_ANSI_COLOUR_OFF " %s\n", @@ -53,8 +53,8 @@ void set_custom_error_callback(CustomErrorCallback callback) const char* get_string(std::span bytes, u64 offset) { - for(const unsigned char* c = bytes.data() + offset; c < bytes.data() + bytes.size(); c++) { - if(*c == '\0') { + for (const unsigned char* c = bytes.data() + offset; c < bytes.data() + bytes.size(); c++) { + if (*c == '\0') { return (const char*) &bytes[offset]; } } @@ -65,7 +65,7 @@ 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; - if(base.empty()) { + if (base.empty()) { is_windows_path = guess_is_windows_path(path.c_str()); } else { is_windows_path = guess_is_windows_path(base.c_str()); @@ -75,7 +75,7 @@ std::string merge_paths(const std::string& base, const std::string& path) // append base onto the front, so check for that now. bool is_absolute_unix = (path.size() >= 1) && (path[0] == '/' || path[0] == '\\'); bool is_absolute_windows = (path.size() >= 3) && path[1] == ':' && (path[2] == '/' || path[2] == '\\'); - if(base.empty() || is_absolute_unix || is_absolute_windows) { + if (base.empty() || is_absolute_unix || is_absolute_windows) { return normalise_path(path.c_str(), is_windows_path); } return normalise_path((base + "/" + path).c_str(), is_windows_path); @@ -88,9 +88,9 @@ std::string normalise_path(const char* input, bool use_backslashes_as_path_separ std::vector parts; // Parse the beginning of the path. - if(*input == '/' || *input == '\\') { // UNIX path, drive relative Windows path or UNC Windows path. + if (*input == '/' || *input == '\\') { // UNIX path, drive relative Windows path or UNC Windows path. is_absolute = true; - } else if(isalpha(*input) && input[1] == ':' && (input[2] == '/' || input[2] == '\\')) { // Absolute Windows path. + } else if (isalpha(*input) && input[1] == ':' && (input[2] == '/' || input[2] == '\\')) { // Absolute Windows path. is_absolute = true; drive_letter = toupper(*input); input += 2; @@ -99,9 +99,9 @@ std::string normalise_path(const char* input, bool use_backslashes_as_path_separ } // Parse the rest of the path. - while(*input != 0) { - if(*input == '/' || *input == '\\') { - while(*input == '/' || *input == '\\') input++; + while (*input != 0) { + if (*input == '/' || *input == '\\') { + while (*input == '/' || *input == '\\') input++; parts.emplace_back(); } else { parts.back() += *(input++); @@ -109,11 +109,11 @@ std::string normalise_path(const char* input, bool use_backslashes_as_path_separ } // Remove "." and ".." parts. - for(s32 i = 0; i < (s32) parts.size(); i++) { - if(parts[i] == ".") { + for (s32 i = 0; i < (s32) parts.size(); i++) { + if (parts[i] == ".") { parts.erase(parts.begin() + i); i--; - } else if(parts[i] == ".." && i > 0 && parts[i - 1] != "..") { + } else if (parts[i] == ".." && i > 0 && parts[i - 1] != "..") { parts.erase(parts.begin() + i); parts.erase(parts.begin() + i - 1); i -= 2; @@ -122,16 +122,16 @@ std::string normalise_path(const char* input, bool use_backslashes_as_path_separ // Output the path in a normal form. std::string output; - if(is_absolute) { - if(drive_letter.has_value()) { + if (is_absolute) { + if (drive_letter.has_value()) { output += *drive_letter; output += ":"; } output += use_backslashes_as_path_separators ? '\\' : '/'; } - for(size_t i = 0; i < parts.size(); i++) { + for (size_t i = 0; i < parts.size(); i++) { output += parts[i]; - if(i != parts.size() - 1) { + if (i != parts.size() - 1) { output += use_backslashes_as_path_separators ? '\\' : '/'; } } @@ -141,10 +141,10 @@ std::string normalise_path(const char* input, bool use_backslashes_as_path_separ bool guess_is_windows_path(const char* path) { - for(const char* ptr = path; *ptr != 0; ptr++) { - if(*ptr == '\\') { + for (const char* ptr = path; *ptr != 0; ptr++) { + if (*ptr == '\\') { return true; - } else if(*ptr == '/') { + } else if (*ptr == '/') { return false; } } @@ -156,14 +156,14 @@ std::string extract_file_name(const std::string& path) std::string::size_type forward_pos = path.find_last_of('/'); std::string::size_type backward_pos = path.find_last_of('\\'); std::string::size_type pos; - if(forward_pos == std::string::npos) { + if (forward_pos == std::string::npos) { pos = backward_pos; - } else if(backward_pos == std::string::npos) { + } else if (backward_pos == std::string::npos) { pos = forward_pos; } else { pos = std::max(forward_pos, backward_pos); } - if(pos + 1 != path.size() && pos != std::string::npos) { + if (pos + 1 != path.size() && pos != std::string::npos) { return path.substr(pos + 1); } else { return path; diff --git a/src/ccc/util.h b/src/ccc/util.h index 72826f4f..fd5df225 100644 --- a/src/ccc/util.h +++ b/src/ccc/util.h @@ -65,14 +65,14 @@ void set_custom_error_callback(CustomErrorCallback callback); } #define CCC_EXIT_IF_FALSE(condition, ...) \ - if(!(condition)) { \ + if (!(condition)) { \ ccc::Error error = ccc::format_error(__FILE__, __LINE__, __VA_ARGS__); \ ccc::report_error(error); \ exit(1); \ } #define CCC_ABORT_IF_FALSE(condition, ...) \ - if(!(condition)) { \ + if (!(condition)) { \ ccc::Error error = ccc::format_error(__FILE__, __LINE__, __VA_ARGS__); \ ccc::report_error(error); \ abort(); \ @@ -165,7 +165,7 @@ class [[nodiscard]] Result : public Result { #define CCC_FAILURE(...) ccc::Result::failure(ccc::format_error(__FILE__, __LINE__, __VA_ARGS__)) #define CCC_CHECK(condition, ...) \ - if(!(condition)) { \ + if (!(condition)) { \ return CCC_FAILURE(__VA_ARGS__); \ } @@ -175,18 +175,18 @@ class [[nodiscard]] Result : public Result { c, context, *(input - 1), *(input - 1)) #define CCC_RETURN_IF_ERROR(result) \ - if(!(result).success()) { \ + if (!(result).success()) { \ return (result); \ } #define CCC_EXIT_IF_ERROR(result) \ - if(!(result).success()) { \ + if (!(result).success()) { \ ccc::report_error((result).error()); \ exit(1); \ } #define CCC_GTEST_FAIL_IF_ERROR(result) \ - if(!(result).success()) { \ + if (!(result).success()) { \ FAIL() << (result).error().message; \ } @@ -210,7 +210,7 @@ void warn_impl(const char* source_file, int source_line, const char* format, Arg template const T* get_packed(std::span bytes, u64 offset) { - if(offset + sizeof(T) <= bytes.size()) { + if (offset + sizeof(T) <= bytes.size()) { return reinterpret_cast(&bytes[offset]); } else { return nullptr; @@ -237,7 +237,7 @@ struct Address { u32 get_or_zero() const { - if(valid()) { + if (valid()) { return value; } else { return 0; @@ -246,7 +246,7 @@ struct Address { Address add_base_address(Address base_address) const { - if(valid()) { + if (valid()) { return base_address.get_or_zero() + value; } else { return Address(); @@ -256,7 +256,7 @@ struct Address { static Address non_zero(u32 address) { Address result; - if(address != 0) { + if (address != 0) { result = address; } return result; diff --git a/src/demangle.cpp b/src/demangle.cpp index fab14306..60d7a57d 100644 --- a/src/demangle.cpp +++ b/src/demangle.cpp @@ -11,7 +11,7 @@ extern const char* git_tag; int main(int argc, char** argv) { - if(argc == 2 && !(strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)) { + if (argc == 2 && !(strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)) { const char* demangled = cplus_demangle(argv[1], DMGL_PARAMS | DMGL_RET_POSTFIX); CCC_EXIT_IF_FALSE(demangled, "Cannot demangle input!"); printf("%s\n", demangled); diff --git a/src/mips/insn.cpp b/src/mips/insn.cpp index 609e6af7..32da439a 100644 --- a/src/mips/insn.cpp +++ b/src/mips/insn.cpp @@ -23,36 +23,36 @@ Insn::Insn(u32 val) : value(val) {} InsnClass Insn::iclass() const { - if(opcode() == OPCODE_SPECIAL) { + if (opcode() == OPCODE_SPECIAL) { return INSN_CLASS_MIPS_SPECIAL; - } else if(opcode() == OPCODE_COP0) { - if(rs() == COP0_BC0) { + } else if (opcode() == OPCODE_COP0) { + if (rs() == COP0_BC0) { return INSN_CLASS_COP0_BC0; - } else if(rs() == COP0_C0) { + } else if (rs() == COP0_C0) { return INSN_CLASS_COP0_C0; } else { return INSN_CLASS_COP0; } - } else if(opcode() == OPCODE_COP1) { - if(rs() == COP1_BC1) { + } else if (opcode() == OPCODE_COP1) { + if (rs() == COP1_BC1) { return INSN_CLASS_COP1_BC1; - } else if(rs() == COP1_S) { + } else if (rs() == COP1_S) { return INSN_CLASS_COP1_S; - } else if(rs() == COP1_W) { + } else if (rs() == COP1_W) { return INSN_CLASS_COP1_W; } else { return INSN_CLASS_COP1; } - } else if(opcode() == OPCODE_COP2) { + } else if (opcode() == OPCODE_COP2) { return INSN_CLASS_COP2; - } else if(opcode() == OPCODE_MMI) { - if(func() == MMI_MMI0) { + } else if (opcode() == OPCODE_MMI) { + if (func() == MMI_MMI0) { return INSN_CLASS_MMI0; - } else if(func() == MMI_MMI1) { + } else if (func() == MMI_MMI1) { return INSN_CLASS_MMI1; - } else if(func() == MMI_MMI2) { + } else if (func() == MMI_MMI2) { return INSN_CLASS_MMI2; - } else if(func() == MMI_MMI3) { + } else if (func() == MMI_MMI3) { return INSN_CLASS_MMI3; } else { return INSN_CLASS_MMI; @@ -64,7 +64,7 @@ InsnClass Insn::iclass() const const InsnInfo& Insn::info() const { - switch(iclass()) { + switch (iclass()) { case INSN_CLASS_MIPS: return MIPS_OPCODE_TABLE[opcode()]; case INSN_CLASS_MIPS_SPECIAL: return MIPS_SPECIAL_TABLE[func()]; case INSN_CLASS_MIPS_REGIMM: return MIPS_REGIMM_TABLE[rt()]; @@ -132,7 +132,7 @@ u32 Insn::target_bytes() const u32 Insn::field(InsnField field) const { - switch(field) { + switch (field) { case InsnField::NONE: return 0; case InsnField::RS: return rs(); case InsnField::RT: return rt(); diff --git a/src/objdump.cpp b/src/objdump.cpp index 7042c17e..902ad7ec 100644 --- a/src/objdump.cpp +++ b/src/objdump.cpp @@ -27,34 +27,34 @@ int main(int argc, char** argv) Result> insns = elf->get_array_virtual(*text_address, text->header.size / 4); CCC_EXIT_IF_ERROR(insns); - for(u32 i = 0; i < text->header.size / 4; i++) { + for (u32 i = 0; i < text->header.size / 4; i++) { mips::Insn insn = (*insns)[i]; const mips::InsnInfo& info = insn.info(); u32 insn_address = *text_address + i; printf("%08x:\t\t%08x %s ", insn_address, insn.value, info.mnemonic); - for(s32 i = 0; i < 16 - (s32) strlen(info.mnemonic); i++) { + for (s32 i = 0; i < 16 - (s32) strlen(info.mnemonic); i++) { printf(" "); } bool first_operand = true; mips::FlowType last_flow_type = mips::FlowType::IMMED; - for(const mips::FlowInfo& flow : info.data_flows) { - if(flow.is_past_end()) { + for (const mips::FlowInfo& flow : info.data_flows) { + if (flow.is_past_end()) { break; } - if(flow.field != mips::InsnField::NONE) { + if (flow.field != mips::InsnField::NONE) { bool is_mem_access = last_flow_type == mips::FlowType::IMMED && flow.type == mips::FlowType::REG; - if(!first_operand) { - if(is_mem_access) { + if (!first_operand) { + if (is_mem_access) { printf("("); } else { printf(","); } } u32 field = insn.field(flow.field); - switch(flow.type) { + switch (flow.type) { case mips::FlowType::IMMED: { - if(flow.field == mips::InsnField::IMMED) { + if (flow.field == mips::InsnField::IMMED) { s16 f = (s16) field; printf("%s0x%x", (f < 0) ? "-" : "", abs(f)); } else { @@ -63,7 +63,7 @@ int main(int argc, char** argv) break; } case mips::FlowType::REG: { - if(field < mips::REGISTER_STRING_TABLE_SIZES[(s32) flow.reg_class]) { + if (field < mips::REGISTER_STRING_TABLE_SIZES[(s32) flow.reg_class]) { printf("%s", mips::REGISTER_STRING_TABLES[(s32) flow.reg_class][insn.field(flow.field)]); } else { printf("error"); @@ -74,7 +74,7 @@ int main(int argc, char** argv) CCC_ASSERT(0); } } - if(!first_operand && is_mem_access) { + if (!first_operand && is_mem_access) { printf(")"); } first_operand = false; diff --git a/src/platform/file.cpp b/src/platform/file.cpp index 4c047cf5..ae8e71b7 100644 --- a/src/platform/file.cpp +++ b/src/platform/file.cpp @@ -28,7 +28,7 @@ std::optional read_text_file(const fs::path& path) { std::ifstream file_stream; file_stream.open(path); - if(!file_stream.is_open()) { + if (!file_stream.is_open()) { return std::nullopt; } std::stringstream string_stream; diff --git a/src/stdump.cpp b/src/stdump.cpp index edb7f6c8..913b5b09 100644 --- a/src/stdump.cpp +++ b/src/stdump.cpp @@ -110,12 +110,12 @@ int main(int argc, char** argv) Options options = parse_command_line_arguments(argc, argv); FILE* out = stdout; - if(!options.output_file.empty()) { + if (!options.output_file.empty()) { out = fopen(options.output_file.string().c_str(), "w"); CCC_EXIT_IF_FALSE(out, "Failed to open output file '%s'.", options.output_file.string().c_str()); } - if(options.function) { + if (options.function) { options.function(out, options); } else { print_help(out); @@ -125,21 +125,21 @@ int main(int argc, char** argv) static void identify_symbol_tables(FILE* out, const Options& options) { - if(fs::is_regular_file(options.input_file)) { + if (fs::is_regular_file(options.input_file)) { identify_symbol_tables_in_file(out, nullptr, nullptr, options.input_file); - } else if(fs::is_directory(options.input_file)) { + } else if (fs::is_directory(options.input_file)) { std::vector totals(SYMBOL_TABLE_FORMATS.size(), 0); u32 unknown_total = 0; - for(auto entry : fs::recursive_directory_iterator(options.input_file)) { - if(entry.is_regular_file()) { + for (auto entry : fs::recursive_directory_iterator(options.input_file)) { + if (entry.is_regular_file()) { identify_symbol_tables_in_file(out, totals.data(), &unknown_total, entry.path()); } } fprintf(out, "\n"); fprintf(out, "Totals:\n"); - for(size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { + for (size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { fprintf(out, " %4d %s sections\n", totals[i], SYMBOL_TABLE_FORMATS[i].section_name); } fprintf(out, " %4d unknown\n", unknown_total); @@ -156,31 +156,31 @@ static void identify_symbol_tables_in_file(FILE* out, u32* totals, u32* unknown_ CCC_EXIT_IF_ERROR(file); const u32* fourcc = get_packed(*file, 0); - if(!fourcc) { + if (!fourcc) { fprintf(out, " file too small\n"); return; } - switch(*fourcc) { + switch (*fourcc) { case CCC_FOURCC("\x7f""ELF"): { Result elf = ElfFile::parse(std::move(*file)); - if(!elf.success()) { + if (!elf.success()) { fprintf(out, " %s\n", elf.error().message.c_str()); break; } bool print_none = true; - for(size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { - if(elf->lookup_section(SYMBOL_TABLE_FORMATS[i].section_name)) { + for (size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { + if (elf->lookup_section(SYMBOL_TABLE_FORMATS[i].section_name)) { fprintf(out, " %s", SYMBOL_TABLE_FORMATS[i].section_name); - if(totals) { + if (totals) { totals[i]++; } print_none = false; } } - if(print_none) { + if (print_none) { fprintf(out, " none"); } @@ -190,14 +190,14 @@ static void identify_symbol_tables_in_file(FILE* out, u32* totals, u32* unknown_ } case CCC_FOURCC("SNR1"): case CCC_FOURCC("SNR2"): { - if(totals) { + if (totals) { totals[SNDLL]++; } fprintf(out, " sndll\n"); break; } default: { - if(unknown_total) { + if (unknown_total) { (*unknown_total)++; } fprintf(out, " unknown format\n"); @@ -212,11 +212,11 @@ static void print_functions(FILE* out, const Options& options) SymbolDatabase database = read_symbol_table(symbol_file, options); std::vector functions; - for(const Function& function : database.functions) { + for (const Function& function : database.functions) { functions.emplace_back(&function); } - if(options.flags & FLAG_SORT_BY_ADDRESS) { + if (options.flags & FLAG_SORT_BY_ADDRESS) { std::sort(CCC_BEGIN_END(functions), [&](const Function* lhs, const Function* rhs) { return lhs->address() < rhs->address(); }); @@ -231,10 +231,10 @@ static void print_functions(FILE* out, const Options& options) bool first_iteration = true; SourceFileHandle source_file_handle; - for(const Function* function : functions) { - if(function->source_file() != source_file_handle || first_iteration) { + for (const Function* function : functions) { + if (function->source_file() != source_file_handle || first_iteration) { SourceFile* source_file = database.source_files.symbol_from_handle(function->source_file()); - if(source_file) { + if (source_file) { printer.comment_block_file(source_file->full_path().c_str()); source_file_handle = source_file->handle(); } else { @@ -254,11 +254,11 @@ static void print_globals(FILE* out, const Options& options) SymbolDatabase database = read_symbol_table(symbol_file, options); std::vector global_variables; - for(const GlobalVariable& global_variable : database.global_variables) { + for (const GlobalVariable& global_variable : database.global_variables) { global_variables.emplace_back(&global_variable); } - if(options.flags & FLAG_SORT_BY_ADDRESS) { + if (options.flags & FLAG_SORT_BY_ADDRESS) { std::sort(CCC_BEGIN_END(global_variables), [&](const GlobalVariable* lhs, const GlobalVariable* rhs) { return lhs->address() < rhs->address(); }); @@ -273,10 +273,10 @@ static void print_globals(FILE* out, const Options& options) bool first_iteration = true; SourceFileHandle source_file_handle; - for(const GlobalVariable* global_variable : global_variables) { - if(global_variable->source_file() != source_file_handle || first_iteration) { + for (const GlobalVariable* global_variable : global_variables) { + if (global_variable->source_file() != source_file_handle || first_iteration) { SourceFile* source_file = database.source_files.symbol_from_handle(global_variable->source_file()); - if(source_file) { + if (source_file) { printer.comment_block_file(source_file->full_path().c_str()); source_file_handle = source_file->handle(); } else { @@ -295,7 +295,7 @@ static void print_types(FILE* out, const Options& options) std::unique_ptr symbol_file; SymbolDatabase database = read_symbol_table(symbol_file, options); - if((options.flags & DONT_DEDUPLICATE_TYPES) == 0) { + if ((options.flags & DONT_DEDUPLICATE_TYPES) == 0) { print_types_deduplicated(out, database, options); } else { print_types_per_file(out, database, options); @@ -313,7 +313,7 @@ static void print_types_deduplicated(FILE* out, SymbolDatabase& database, const printer.comment_block_toolchain_version_info(database); printer.comment_block_builtin_types(database); - for(const DataType& data_type : database.data_types) { + for (const DataType& data_type : database.data_types) { printer.data_type(data_type, database); } } @@ -327,12 +327,12 @@ static void print_types_per_file(FILE* out, SymbolDatabase& database, const Opti printer.comment_block_beginning(options.input_file.filename().string().c_str(), "stdump", get_version()); - for(const SourceFile& source_file : database.source_files) { + for (const SourceFile& source_file : database.source_files) { printer.comment_block_file(source_file.full_path().c_str()); printer.comment_block_toolchain_version_info(database); printer.comment_block_builtin_types(database, source_file.handle()); - for(const DataType& data_type : database.data_types) { - if(data_type.files.size() == 1 && data_type.files[0] == source_file.handle()) { + for (const DataType& data_type : database.data_types) { + if (data_type.files.size() == 1 && data_type.files[0] == source_file.handle()) { printer.data_type(data_type, database); } } @@ -354,17 +354,17 @@ static void print_labels(FILE* out, const Options& options) SymbolDatabase database = read_symbol_table(symbol_file, options); std::vector labels; - for(const Label& label : database.labels) { + for (const Label& label : database.labels) { labels.emplace_back(&label); } - if(options.flags & FLAG_SORT_BY_ADDRESS) { + if (options.flags & FLAG_SORT_BY_ADDRESS) { std::sort(CCC_BEGIN_END(labels), [&](const Label* lhs, const Label* rhs) { return lhs->address() < rhs->address(); }); } - for(const Label* label : labels) { + for (const Label* label : labels) { fprintf(out, "%08x %s\n", label->address().value, label->name().c_str()); } } @@ -391,16 +391,16 @@ static void print_symbols(FILE* out, const Options& options) std::vector> symbol_tables = select_symbol_tables(**symbol_file, options.sections); u32 print_flags = 0; - if(options.flags & FLAG_LOCAL_SYMBOLS) print_flags |= PRINT_LOCALS; - if(options.flags & FLAG_PROCEDURE_DESCRIPTORS) print_flags |= PRINT_PROCEDURE_DESCRIPTORS; - if(options.flags & FLAG_EXTERNAL_SYMBOLS) print_flags |= PRINT_EXTERNALS; + if (options.flags & FLAG_LOCAL_SYMBOLS) print_flags |= PRINT_LOCALS; + if (options.flags & FLAG_PROCEDURE_DESCRIPTORS) print_flags |= PRINT_PROCEDURE_DESCRIPTORS; + if (options.flags & FLAG_EXTERNAL_SYMBOLS) print_flags |= PRINT_EXTERNALS; - if(print_flags == 0) { + if (print_flags == 0) { print_flags |= PRINT_LOCALS; print_flags |= PRINT_EXTERNALS; } - for(const std::unique_ptr& symbol_table : symbol_tables) { + for (const std::unique_ptr& symbol_table : symbol_tables) { Result result = symbol_table->print_symbols(out, print_flags); CCC_EXIT_IF_ERROR(result); } @@ -417,7 +417,7 @@ static void print_headers(FILE* out, const Options& options) std::vector> symbol_tables = select_symbol_tables(**symbol_file, options.sections); - for(const std::unique_ptr& symbol_table : symbol_tables) { + for (const std::unique_ptr& symbol_table : symbol_tables) { Result result = symbol_table->print_headers(out); CCC_EXIT_IF_ERROR(result); } @@ -428,7 +428,7 @@ static void print_files(FILE* out, const Options& options) std::unique_ptr symbol_file; SymbolDatabase database = read_symbol_table(symbol_file, options); - for(const SourceFile& source_file : database.source_files) { + for (const SourceFile& source_file : database.source_files) { fprintf(out, "%08x %s\n", source_file.address().value, source_file.name().c_str()); } } @@ -439,20 +439,20 @@ static void print_includes(FILE* out, const Options& options) SymbolDatabase database = read_symbol_table(symbol_file, options); std::set includes; - for(const Function& function : database.functions) { + for (const Function& function : database.functions) { const SourceFile* source_file = database.source_files.symbol_from_handle(function.source_file()); - if(!source_file) { + if (!source_file) { continue; } - for(const Function::SubSourceFile& sub_source : function.sub_source_files) { - if(sub_source.relative_path != source_file->command_line_path) { + for (const Function::SubSourceFile& sub_source : function.sub_source_files) { + if (sub_source.relative_path != source_file->command_line_path) { includes.emplace(sub_source.relative_path); } } } - for(const std::string& include : includes) { + for (const std::string& include : includes) { fprintf(out, "%s\n", include.c_str()); } } @@ -462,8 +462,8 @@ static void print_sections(FILE* out, const Options& options) std::unique_ptr symbol_file; SymbolDatabase database = read_symbol_table(symbol_file, options); - for(const Section& section : database.sections) { - if(!section.address().valid()) { + for (const Section& section : database.sections) { + if (!section.address().valid()) { continue; } @@ -472,8 +472,8 @@ static void print_sections(FILE* out, const Options& options) fprintf(out, "%s:\n", section.name().c_str()); - for(const SourceFile& source_file : database.source_files) { - if(source_file.address().valid() && source_file.address() >= section_start && source_file.address() < section_end) { + for (const SourceFile& source_file : database.source_files) { + if (source_file.address().valid() && source_file.address() >= section_start && source_file.address() < section_end) { fprintf(out, "\t%s\n", source_file.full_path().c_str()); } } @@ -509,7 +509,7 @@ static std::vector> select_symbol_tables( SymbolFile& symbol_file, const std::vector& sections) { std::vector> symbol_tables; - if(!sections.empty()) { + if (!sections.empty()) { Result>> symbol_tables_result = symbol_file.get_symbol_tables_from_sections(sections); CCC_EXIT_IF_ERROR(symbol_tables_result); symbol_tables = std::move(*symbol_tables_result); @@ -524,14 +524,14 @@ static std::vector> select_symbol_tables( static Options parse_command_line_arguments(int argc, char** argv) { Options options; - if(argc < 2) { + if (argc < 2) { return options; } const char* name = argv[1]; bool require_input_path = false; - for(const StdumpCommand& command : commands) { - if(strcmp(name, command.name) == 0) { + for (const StdumpCommand& command : commands) { + if (strcmp(name, command.name) == 0) { options.function = command.function; require_input_path = true; break; @@ -539,30 +539,30 @@ static Options parse_command_line_arguments(int argc, char** argv) } bool input_path_provided = false; - for(s32 i = 2; i < argc; i++) { + for (s32 i = 2; i < argc; i++) { const char* arg = argv[i]; u32 importer_flag = parse_importer_flag(arg); - if(importer_flag != NO_IMPORTER_FLAGS) { + if (importer_flag != NO_IMPORTER_FLAGS) { options.importer_flags |= importer_flag; - } else if(strcmp(arg, "--sort-by-address") == 0) { + } else if (strcmp(arg, "--sort-by-address") == 0) { options.flags |= FLAG_SORT_BY_ADDRESS; - } else if(strcmp(arg, "--caller-stack-offsets") == 0) { + } else if (strcmp(arg, "--caller-stack-offsets") == 0) { options.flags |= FLAG_CALLER_STACK_OFFSETS; - } else if(strcmp(arg, "--locals") == 0) { + } else if (strcmp(arg, "--locals") == 0) { options.flags |= FLAG_LOCAL_SYMBOLS; - } else if(strcmp(arg, "--procedures") == 0) { + } else if (strcmp(arg, "--procedures") == 0) { options.flags |= FLAG_PROCEDURE_DESCRIPTORS; - } else if(strcmp(arg, "--externals") == 0) { + } else if (strcmp(arg, "--externals") == 0) { options.flags |= FLAG_EXTERNAL_SYMBOLS; - } else if(strcmp(arg, "--output") == 0 || strcmp(arg, "-o") == 0) { - if(i + 1 < argc) { + } else if (strcmp(arg, "--output") == 0 || strcmp(arg, "-o") == 0) { + if (i + 1 < argc) { options.output_file = argv[++i]; } else { CCC_EXIT("No output path specified."); } - } else if(strcmp(arg, "--section") == 0) { - if(i + 2 < argc) { + } else if (strcmp(arg, "--section") == 0) { + if (i + 2 < argc) { SymbolTableLocation& section = options.sections.emplace_back(); section.section_name = argv[++i]; @@ -570,14 +570,14 @@ static Options parse_command_line_arguments(int argc, char** argv) CCC_EXIT_IF_FALSE(info, "Invalid symbol table format specified."); section.format = info->format; - } else if(i + 1 < argc) { + } else if (i + 1 < argc) { CCC_EXIT("Missing format after --section."); } else { CCC_EXIT("Missing section name after --section."); } - } else if(strncmp(arg, "--", 2) == 0) { + } else if (strncmp(arg, "--", 2) == 0) { CCC_EXIT("Unknown option '%s'.", arg); - } else if(input_path_provided) { + } else if (input_path_provided) { CCC_EXIT("Multiple input paths specified."); } else { options.input_file = argv[i]; @@ -597,9 +597,9 @@ static void print_help(FILE* out) fprintf(out, "\n"); fprintf(out, "Commands:\n"); fprintf(out, "\n"); - for(const StdumpCommand& command : commands) { + for (const StdumpCommand& command : commands) { fprintf(out, " %s [options] \n", command.name); - for(const char* line : command.help_text) { + for (const char* line : command.help_text) { fprintf(out, " %s\n", line); } fprintf(out, "\n"); @@ -627,14 +627,14 @@ static void print_help(FILE* out) // Print out a line wrapped list of common section names. s32 column = 32 + (s32) strlen(common_section_names_are); - for(size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { + for (size_t i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { const SymbolTableFormatInfo& format = SYMBOL_TABLE_FORMATS[i]; - if(column + strlen(format.section_name) + 2 > 80) { + if (column + strlen(format.section_name) + 2 > 80) { fprintf(out, "\n "); column = 32; } fprintf(out, "%s", format.section_name); - if(i + 1 == SYMBOL_TABLE_FORMATS.size()) { + if (i + 1 == SYMBOL_TABLE_FORMATS.size()) { fprintf(out, ".\n"); } else { fprintf(out, ", "); @@ -647,14 +647,14 @@ static void print_help(FILE* out) fprintf(out, "\n"); fprintf(out, " %s", supported_formats_are); column = 32 + (s32) strlen(supported_formats_are); - for(u32 i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { + for (u32 i = 0; i < SYMBOL_TABLE_FORMATS.size(); i++) { const SymbolTableFormatInfo& format = SYMBOL_TABLE_FORMATS[i]; - if(column + strlen(format.format_name) + 2 > 80) { + if (column + strlen(format.format_name) + 2 > 80) { fprintf(out, "\n "); column = 32; } fprintf(out, "%s", format.format_name); - if(i + 1 == SYMBOL_TABLE_FORMATS.size()) { + if (i + 1 == SYMBOL_TABLE_FORMATS.size()) { fprintf(out, ".\n"); } else { fprintf(out, ", "); diff --git a/src/tests.cpp b/src/tests.cpp index 4bf8c595..c0844642 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -17,11 +17,11 @@ int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); int result = RUN_ALL_TESTS(); - if(result != 0) { + if (result != 0) { return result; } - if(argc != 2) { + if (argc != 2) { return 1; } @@ -38,8 +38,8 @@ static int main_test(const fs::path& input_directory) { CCC_EXIT_IF_FALSE(fs::is_directory(input_directory), "Input path is not a directory."); - for(auto entry : fs::recursive_directory_iterator(input_directory)) { - if(entry.is_regular_file()) { + for (auto entry : fs::recursive_directory_iterator(input_directory)) { + if (entry.is_regular_file()) { printf("%s ", entry.path().string().c_str()); fflush(stdout); @@ -47,7 +47,7 @@ static int main_test(const fs::path& input_directory) CCC_EXIT_IF_ERROR(image); Result> symbol_file = parse_symbol_file(*image, entry.path().filename().string()); - if(symbol_file.success()) { + if (symbol_file.success()) { SymbolDatabase database; Result>> symbol_tables = (*symbol_file)->get_all_symbol_tables(); @@ -71,13 +71,13 @@ static int main_test(const fs::path& input_directory) FILE* black_hole = fopen(compressor, "w"); CppPrinterConfig printer_config; CppPrinter printer(black_hole, printer_config); - for(const DataType& data_type : database.data_types) { + for (const DataType& data_type : database.data_types) { printer.data_type(data_type, database); } - for(const Function& function : database.functions) { + for (const Function& function : database.functions) { printer.function(function, database, nullptr); } - for(const GlobalVariable& global_variable : database.global_variables) { + for (const GlobalVariable& global_variable : database.global_variables) { printer.global_variable(global_variable, database, nullptr); } fclose(black_hole); diff --git a/src/uncc.cpp b/src/uncc.cpp index cb3e8540..61598fdc 100644 --- a/src/uncc.cpp +++ b/src/uncc.cpp @@ -45,7 +45,7 @@ static void print_help(int argc, char** argv); int main(int argc, char** argv) { Options options = parse_command_line_arguments(argc, argv); - if(options.elf_path.empty()) { + if (options.elf_path.empty()) { return 1; } @@ -55,7 +55,7 @@ int main(int argc, char** argv) std::vector source_paths = parse_sources_file(sources_file_path); FunctionsFile functions_file; - if(fs::exists(functions_file_path)) { + if (fs::exists(functions_file_path)) { functions_file = parse_functions_file(functions_file_path); } @@ -90,19 +90,19 @@ int main(int argc, char** argv) // the SOURCES.txt file. std::map> path_to_source_file; size_t path_index = 0; - for(SourceFile& source_file : database.source_files) { - if(path_index >= source_paths.size()) { + for (SourceFile& source_file : database.source_files) { + if (path_index >= source_paths.size()) { break; } std::string source_name = extract_file_name(source_file.full_path()); std::string path_name = extract_file_name(source_paths.at(path_index)); - if(source_name == path_name) { + if (source_name == path_name) { path_to_source_file[source_paths[path_index++]].emplace_back(source_file.handle()); } } // Write out all the source files. - for(auto& [relative_path, sources] : path_to_source_file) { + for (auto& [relative_path, sources] : path_to_source_file) { fs::path relative_header_path = relative_path; relative_header_path.replace_extension(".h"); @@ -110,15 +110,15 @@ int main(int argc, char** argv) fs::path header_path = options.output_path/relative_header_path; fs::create_directories(path.parent_path()); - if(path.extension() == ".c" || path.extension() == ".cpp") { + if (path.extension() == ".c" || path.extension() == ".cpp") { // Write .c/.cpp file. - if(should_overwrite_file(path)) { + if (should_overwrite_file(path)) { write_c_cpp_file(path, relative_header_path, database, sources, functions_file, (*symbol_file)->elf()); } else { printf(CCC_ANSI_COLOUR_GRAY "Skipping " CCC_ANSI_COLOUR_OFF " %s\n", path.string().c_str()); } // Write .h file. - if(should_overwrite_file(header_path)) { + if (should_overwrite_file(header_path)) { write_h_file(header_path, relative_header_path.string(), database, sources); } else { printf(CCC_ANSI_COLOUR_GRAY "Skipping " CCC_ANSI_COLOUR_OFF " %s\n", header_path.string().c_str()); @@ -130,7 +130,7 @@ int main(int argc, char** argv) // Write out a lost+found file for types that can't be mapped to a specific // source file if we need it. - if(needs_lost_and_found_file(database)) { + if (needs_lost_and_found_file(database)) { write_lost_and_found_file(options.output_path/"lost+found.h", database); } } @@ -141,7 +141,7 @@ static std::vector parse_sources_file(const fs::path& path) CCC_EXIT_IF_FALSE(file.has_value(), "Failed to open file '%s'", path.string().c_str()); std::string_view input(*file); std::vector sources; - while(skip_whitespace(input), input.size() > 0) { + while (skip_whitespace(input), input.size() > 0) { sources.emplace_back(eat_identifier(input)); } return sources; @@ -158,31 +158,31 @@ static FunctionsFile parse_functions_file(const fs::path& path) // Parse the file. std::span input(result.contents); std::span* function = nullptr; - for(std::span line = eat_line(input); line.data() != nullptr; line = eat_line(input)) { - if(line.size() >= 9 && memcmp(line.data(), "@function", 9) == 0) { + for (std::span line = eat_line(input); line.data() != nullptr; line = eat_line(input)) { + if (line.size() >= 9 && memcmp(line.data(), "@function", 9) == 0) { CCC_EXIT_IF_FALSE(line.size() > 10, "Bad @function directive in FUNCTIONS.txt file."); char* end = nullptr; u32 address = (u32) strtol(line.data() + 10, &end, 16); CCC_EXIT_IF_FALSE(end != line.data() + 10, "Bad @function directive in FUNCTIONS.txt file."); function = &result.functions[address]; *function = input.subspan(1); - } else if(function) { + } else if (function) { *function = std::span(function->data(), line.data() + line.size()); } } - for(auto& [address, code] : result.functions) { + for (auto& [address, code] : result.functions) { // Remove everything before the function body. - for(size_t i = 0; i + 1 < code.size(); i++) { - if(code[i] == '{' && code[i + 1] == '\n') { + for (size_t i = 0; i + 1 < code.size(); i++) { + if (code[i] == '{' && code[i + 1] == '\n') { code = code.subspan(i + 2); break; } } // Remove everything after the function body. - for(size_t i = code.size(); i > 1; i--) { - if(code[i - 2] == '}' && code[i - 1] == '\n') { + for (size_t i = code.size(); i > 1; i--) { + if (code[i - 2] == '}' && code[i - 1] == '\n') { code = code.subspan(0, i - 2); break; } @@ -194,8 +194,8 @@ static FunctionsFile parse_functions_file(const fs::path& path) static std::span eat_line(std::span& input) { - for(size_t i = 0; i < input.size(); i++) { - if(input[i] == '\n') { + for (size_t i = 0; i < input.size(); i++) { + if (input[i] == '\n') { std::span result = input.subspan(0, i); input = input.subspan(i + 1); return result; @@ -209,7 +209,7 @@ static std::string eat_identifier(std::string_view& input) skip_whitespace(input); std::string string; size_t i; - for(i = 0; i < input.size() && !isspace(input[i]); i++) { + for (i = 0; i < input.size() && !isspace(input[i]); i++) { string += input[i]; } input = input.substr(i); @@ -218,7 +218,7 @@ static std::string eat_identifier(std::string_view& input) static void skip_whitespace(std::string_view& input) { - while(input.size() > 0 && isspace(input[0])) { + while (input.size() > 0 && isspace(input[0])) { input = input.substr(1); } } @@ -255,32 +255,32 @@ static void write_c_cpp_file( printer.include_directive(header_path.filename().string().c_str()); // Print types. - for(SourceFileHandle file_handle : files) { - for(const DataType& data_type : database.data_types) { - if(data_type.only_defined_in_single_translation_unit && data_type.files.size() == 1 && data_type.files[0] == file_handle) { + for (SourceFileHandle file_handle : files) { + for (const DataType& data_type : database.data_types) { + if (data_type.only_defined_in_single_translation_unit && data_type.files.size() == 1 && data_type.files[0] == file_handle) { printer.data_type(data_type, database); } } } // Print globals. - for(SourceFileHandle file_handle : files) { + for (SourceFileHandle file_handle : files) { const SourceFile* source_file = database.source_files.symbol_from_handle(file_handle); CCC_ASSERT(source_file); const std::vector& global_variables = source_file->global_variables(); - for(const GlobalVariable* global_variable : database.global_variables.symbols_from_handles(global_variables)) { + for (const GlobalVariable* global_variable : database.global_variables.symbols_from_handles(global_variables)) { printer.global_variable(*global_variable, database, &elf); } } // Print functions. - for(SourceFileHandle file_handle : files) { + for (SourceFileHandle file_handle : files) { const SourceFile* source_file = database.source_files.symbol_from_handle(file_handle); CCC_ASSERT(source_file); const std::vector& functions = source_file->functions(); - for(const Function* function : database.functions.symbols_from_handles(functions)) { + for (const Function* function : database.functions.symbols_from_handles(functions)) { printer.function(*function, database, &elf); } } @@ -310,18 +310,18 @@ static void write_h_file( config.skip_member_functions_outside_types = true; CppPrinter printer(out, config); - for(char& c : relative_path) { + for (char& c : relative_path) { c = toupper(c); - if(!isalnum(c)) { + if (!isalnum(c)) { c = '_'; } } printer.begin_include_guard(relative_path.c_str()); // Print types. - for(SourceFileHandle file_handle : files) { - for(const DataType& data_type : database.data_types) { - if(!data_type.only_defined_in_single_translation_unit && data_type.files.size() == 1 && data_type.files[0] == file_handle) { + for (SourceFileHandle file_handle : files) { + for (const DataType& data_type : database.data_types) { + if (!data_type.only_defined_in_single_translation_unit && data_type.files.size() == 1 && data_type.files[0] == file_handle) { printer.data_type(data_type, database); } } @@ -329,28 +329,28 @@ static void write_h_file( // Print globals. bool has_global = false; - for(SourceFileHandle file_handle : files) { + for (SourceFileHandle file_handle : files) { const SourceFile* source_file = database.source_files.symbol_from_handle(file_handle); CCC_ASSERT(source_file); const std::vector& global_variables = source_file->global_variables(); - for(const GlobalVariable* global_variable : database.global_variables.symbols_from_handles(global_variables)) { + for (const GlobalVariable* global_variable : database.global_variables.symbols_from_handles(global_variables)) { printer.global_variable(*global_variable, database, nullptr); has_global = true; } } - if(has_global) { + if (has_global) { fprintf(out, "\n"); } // Print functions. - for(SourceFileHandle file_handle : files) { + for (SourceFileHandle file_handle : files) { const SourceFile* source_file = database.source_files.symbol_from_handle(file_handle); CCC_ASSERT(source_file); const std::vector& functions = source_file->functions(); - for(const Function* function : database.functions.symbols_from_handles(functions)) { + for (const Function* function : database.functions.symbols_from_handles(functions)) { printer.function(*function, database, nullptr); } } @@ -362,8 +362,8 @@ static void write_h_file( static bool needs_lost_and_found_file(const SymbolDatabase& database) { - for(const DataType& data_type : database.data_types) { - if(data_type.files.size() != 1) { + for (const DataType& data_type : database.data_types) { + if (data_type.files.size() != 1) { return true; } } @@ -383,9 +383,9 @@ static void write_lost_and_found_file(const fs::path& path, const SymbolDatabase CppPrinter printer(out, config); s32 nodes_printed = 0; - for(const DataType& data_type : database.data_types) { - if(data_type.files.size() != 1) { - if(printer.data_type(data_type, database)) { + for (const DataType& data_type : database.data_types) { + if (data_type.files.size() != 1) { + if (printer.data_type(data_type, database)) { nodes_printed++; } } @@ -400,17 +400,17 @@ static Options parse_command_line_arguments(int argc, char** argv) { Options options; s32 positional = 0; - for(s32 i = 1; i < argc; i++) { + for (s32 i = 1; i < argc; i++) { u32 importer_flag = parse_importer_flag(argv[i]); - if(importer_flag != NO_IMPORTER_FLAGS) { + if (importer_flag != NO_IMPORTER_FLAGS) { options.importer_flags |= importer_flag; - } else if(strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { + } else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { print_help(argc, argv); return Options(); - } else if(positional == 0) { + } else if (positional == 0) { options.elf_path = argv[i]; positional++; - } else if(positional == 1) { + } else if (positional == 1) { options.output_path = argv[i]; positional++; } else { @@ -418,7 +418,7 @@ static Options parse_command_line_arguments(int argc, char** argv) } } - if(options.elf_path.empty() || options.output_path.empty()) { + if (options.elf_path.empty() || options.output_path.empty()) { print_help(argc, argv); return Options(); } diff --git a/test/ccc/mdebug_importer_tests.cpp b/test/ccc/mdebug_importer_tests.cpp index 2ffab863..f7b5f6c8 100644 --- a/test/ccc/mdebug_importer_tests.cpp +++ b/test/ccc/mdebug_importer_tests.cpp @@ -22,7 +22,7 @@ static Result run_importer(const char* name, mdebug::File& input context.group.source = (*symbol_source)->handle(); context.importer_flags = DONT_DEDUPLICATE_SYMBOLS | STRICT_PARSING; - for(mdebug::Symbol& symbol : input.symbols) { + for (mdebug::Symbol& symbol : input.symbols) { symbol.procedure_descriptor = &procedure_descriptor; } @@ -264,8 +264,8 @@ MDEBUG_IMPORTER_TEST(SimpleFunctionIOP, // ee-g++ -gstabs // int ComplicatedFunction(int a, float b, char* c) { // int x = b < 0; -// if(a) { int y = b + *c; return y; } -// for(int i = 0; i < 5; i++) { int z = b + i; x += z; } +// if (a) { int y = b + *c; return y; } +// for (int i = 0; i < 5; i++) { int z = b + i; x += z; } // return x; // } MDEBUG_IMPORTER_TEST(ComplicatedFunction, @@ -307,8 +307,8 @@ MDEBUG_IMPORTER_TEST(ComplicatedFunction, // iop-gcc -gstabs // int ComplicatedFunctionIOP(int a, float b, char* c) { // int x = b < 0, i; -// if(a) { int y = b + *c; return y; } -// for(i = 0; i < 5; i++) { int z = b + i; x += z; } +// if (a) { int y = b + *c; return y; } +// for (i = 0; i < 5; i++) { int z = b + i; x += z; } // return x; // } MDEBUG_IMPORTER_TEST(ComplicatedFunctionIOP, diff --git a/test/ccc/symbol_database_tests.cpp b/test/ccc/symbol_database_tests.cpp index 87cdaa33..c2313c09 100644 --- a/test/ccc/symbol_database_tests.cpp +++ b/test/ccc/symbol_database_tests.cpp @@ -14,14 +14,14 @@ TEST(CCCSymbolDatabase, SymbolFromHandle) SymbolSourceHandle handles[10]; // Create the symbols. - for(s32 i = 0; i < 10; i++) { + for (s32 i = 0; i < 10; i++) { Result source = database.symbol_sources.create_symbol(std::to_string(i), SymbolSourceHandle()); CCC_GTEST_FAIL_IF_ERROR(source); handles[i] = (*source)->handle(); } // Make sure we can still look them up. - for(s32 i = 0; i < 10; i++) { + for (s32 i = 0; i < 10; i++) { SymbolSource* source = database.symbol_sources.symbol_from_handle(handles[i]); ASSERT_TRUE(source); EXPECT_EQ(source->name(), std::to_string(i)); @@ -37,7 +37,7 @@ TEST(CCCSymbolDatabase, HandlesFromAddressRange) CCC_GTEST_FAIL_IF_ERROR(source); // Create the symbols. - for(u32 address = 10; address < 20; address++) { + for (u32 address = 10; address < 20; address++) { Result function = database.functions.create_symbol("", address, (*source)->handle(), nullptr); CCC_GTEST_FAIL_IF_ERROR(function); handles[address] = (*function)->handle(); @@ -84,14 +84,14 @@ TEST(CCCSymbolDatabase, HandleFromStartingAddress) CCC_GTEST_FAIL_IF_ERROR(source); // Create the symbols. - for(u32 address = 0; address < 10; address++) { + for (u32 address = 0; address < 10; address++) { Result function = database.functions.create_symbol("", address, (*source)->handle(), nullptr); CCC_GTEST_FAIL_IF_ERROR(function); handles[address] = (*function)->handle(); } // Make sure we can look them up by their address. - for(u32 address = 0; address < 10; address++) { + for (u32 address = 0; address < 10; address++) { EXPECT_EQ(database.functions.first_handle_from_starting_address(address), handles[address]); } } @@ -142,7 +142,7 @@ static Result create_function(SymbolDatabase& database, SymbolSo static FunctionHandle handle_from_function(Function* function) { - if(function) { + if (function) { return function->handle(); } else { return FunctionHandle(); @@ -240,25 +240,25 @@ TEST(CCCSymbolDatabase, DestroySymbolsDanglingHandles) SymbolSourceHandle handles[10]; // Create the symbols. - for(s32 i = 0; i < 10; i++) { + for (s32 i = 0; i < 10; i++) { Result source = database.symbol_sources.create_symbol(std::to_string(i), SymbolSourceHandle()); CCC_GTEST_FAIL_IF_ERROR(source); handles[i] = (*source)->handle(); } // Destroy every other symbol. - for(s32 i = 0; i < 10; i += 2) { + for (s32 i = 0; i < 10; i += 2) { database.symbol_sources.mark_symbol_for_destruction(handles[i], &database); } database.destroy_marked_symbols(); // Make sure we can't look them up anymore. - for(s32 i = 0; i < 10; i += 2) { + for (s32 i = 0; i < 10; i += 2) { EXPECT_FALSE(database.symbol_sources.symbol_from_handle(handles[i])); } // Make sure we can still lookup the other ones. - for(s32 i = 1; i < 10; i += 2) { + for (s32 i = 1; i < 10; i += 2) { EXPECT_TRUE(database.symbol_sources.symbol_from_handle(handles[i])); } } @@ -275,22 +275,22 @@ TEST(CCCSymbolDatabase, DestroySymbolsFromSource) CCC_GTEST_FAIL_IF_ERROR(user_defined_source); SymbolSourceHandle user_defined_handle = (*user_defined_source)->handle(); - for(s32 i = 0; i < 5; i++) { + for (s32 i = 0; i < 5; i++) { Result result = database.data_types.create_symbol("SymbolTableType", symbol_table_handle); CCC_GTEST_FAIL_IF_ERROR(result); } - for(s32 i = 0; i < 5; i++) { + for (s32 i = 0; i < 5; i++) { Result result = database.data_types.create_symbol("UserDefinedType", user_defined_handle); CCC_GTEST_FAIL_IF_ERROR(result); } - for(s32 i = 0; i < 5; i++) { + for (s32 i = 0; i < 5; i++) { Result result = database.data_types.create_symbol("SymbolTableType", symbol_table_handle); CCC_GTEST_FAIL_IF_ERROR(result); } - for(s32 i = 0; i < 5; i++) { + for (s32 i = 0; i < 5; i++) { Result result = database.data_types.create_symbol("UserDefinedType", user_defined_handle); CCC_GTEST_FAIL_IF_ERROR(result); } @@ -299,7 +299,7 @@ TEST(CCCSymbolDatabase, DestroySymbolsFromSource) database.destroy_symbols_from_source(symbol_table_handle, true); s32 user_symbols_remaining = 0; - for(const DataType& data_type : database.data_types) { + for (const DataType& data_type : database.data_types) { ASSERT_TRUE(data_type.source() == user_defined_handle); user_symbols_remaining++; } diff --git a/test/demangler_tests.cpp b/test/demangler_tests.cpp index e9e6e5a1..eaea0583 100644 --- a/test/demangler_tests.cpp +++ b/test/demangler_tests.cpp @@ -9,7 +9,7 @@ TEST(GNUDemangler, name) { \ char* demangled = cplus_demangle_opname(mangled, 0); \ const char* expected = expected_demangled; \ - if(demangled) { \ + if (demangled) { \ ASSERT_TRUE(expected && strcmp(demangled, expected) == 0); \ free((void*) demangled); \ } else { \