From c444d50d17b77d3b09e3c53ea556a234d5e33b3c Mon Sep 17 00:00:00 2001 From: doyle Date: Sat, 6 Apr 2019 01:30:09 +1100 Subject: [PATCH] Fix some ordering issues --- Code/DqnInspect.h | 337 +++++++++++++------------- Data/DqnInspect_TestData.h | 1 + Data/DqnInspect_TestDataGenerated.cpp | 38 +-- 3 files changed, 188 insertions(+), 188 deletions(-) diff --git a/Code/DqnInspect.h b/Code/DqnInspect.h index 8041ac3..a511d86 100644 --- a/Code/DqnInspect.h +++ b/Code/DqnInspect.h @@ -122,6 +122,18 @@ struct DeferHelper Defer operator+(Lambda lambda) { return Defer(lambda); } }; +#define INSPECT_MAX(a, b) ((a) > (b)) ? (a) : (b) +#define INSPECT_MIN(a, b) ((a) < (b)) ? (a) : (b) +#define ARRAY_COUNT(str) sizeof(str)/sizeof(str[0]) +#define CHAR_COUNT(str) (ARRAY_COUNT(str) - 1) +#define TOKEN_COMBINE(x, y) x ## y +#define TOKEN_COMBINE2(x, y) TOKEN_COMBINE(x, y) +#define DEFER auto const TOKEN_COMBINE(defer_lambda_, __COUNTER__) = DeferHelper() + [&]() + +#define KILOBYTE(val) (1024ULL * (val)) +#define MEGABYTE(val) (1024ULL * KILOBYTE(val)) + + #define SLICE_LITERAL(str) Slice{str, CHAR_COUNT(str)} template struct Slice @@ -155,16 +167,6 @@ struct LinkedList LinkedList *next; }; -#define INSPECT_MAX(a, b) ((a) > (b)) ? (a) : (b) -#define ARRAY_COUNT(str) sizeof(str)/sizeof(str[0]) -#define CHAR_COUNT(str) (ARRAY_COUNT(str) - 1) -#define TOKEN_COMBINE(x, y) x ## y -#define TOKEN_COMBINE2(x, y) TOKEN_COMBINE(x, y) -#define DEFER auto const TOKEN_COMBINE(defer_lambda_, __COUNTER__) = DeferHelper() + [&]() - -#define KILOBYTE(val) (1024ULL * (val)) -#define MEGABYTE(val) (1024ULL * KILOBYTE(val)) - // // Memory Utilities // @@ -868,6 +870,7 @@ struct ParsingResult char *file_include_contents_hash_define; int max_func_return_type_decl_len; int max_func_name_decl_len; + Slice file_name; }; @@ -1452,23 +1455,9 @@ int main(int argc, char *argv[]) parsing_results.file_include_contents_hash_define = extracted_file_name_buf + file_name_len - extracted_file_name_len; parsing_results.file_include_contents_hash_define_len = extracted_file_name_len; + parsing_results.file_name = Slice(file_name, file_name_len); } - fprintf(output_file, - "//\n" - "// %s\n" - "//\n" - "\n" - "#ifndef DQN_INSPECT_%.*s\n" - "#define DQN_INSPECT_%.*s\n" - "\n", - file_name, - parsing_results.file_include_contents_hash_define_len, - parsing_results.file_include_contents_hash_define, - parsing_results.file_include_contents_hash_define_len, - parsing_results.file_include_contents_hash_define - ); - CPPTokeniser tokeniser = {}; tokeniser.tokens_max = 16384; tokeniser.tokens = MEM_ARENA_ALLOC_ARRAY(&global_main_arena, CPPToken, tokeniser.tokens_max); @@ -1519,8 +1508,6 @@ int main(int argc, char *argv[]) CPPToken *sentinel = CPPTokeniser_MakeToken(&tokeniser); sentinel->type = CPPTokenType::EndOfStream; - int max_func_return_type_decl_len = 0; - int max_func_name_decl_len = 0; for (CPPToken token = CPPTokeniser_PeekToken(&tokeniser); ; token = CPPTokeniser_PeekToken(&tokeniser)) @@ -1549,8 +1536,8 @@ int main(int argc, char *argv[]) if (ParseCPPInspectPrototype(&tokeniser, &parsed_code.parsed_func_prototype)) { parsed_code.type = ParsedCodeType::FunctionPrototype; - parsing_results.max_func_return_type_decl_len = INSPECT_MAX(max_func_return_type_decl_len, parsed_code.parsed_func_prototype.return_type.len); - parsing_results.max_func_name_decl_len = INSPECT_MAX(max_func_name_decl_len, parsed_code.parsed_func_prototype.name.len); + parsing_results.max_func_return_type_decl_len = INSPECT_MAX(parsing_results.max_func_return_type_decl_len, parsed_code.parsed_func_prototype.return_type.len); + parsing_results.max_func_name_decl_len = INSPECT_MAX(parsing_results.max_func_name_decl_len, parsed_code.parsed_func_prototype.name.len); } } } @@ -1568,161 +1555,189 @@ int main(int argc, char *argv[]) parsing_results_per_file.push_back(std::move(parsing_results)); } - // - // NOTE: Build the global definition table - // int indent_level = 0; - FprintfIndented(output_file, indent_level, "enum struct DqnInspectMemberType\n{\n"); - indent_level++; - for (ParsingResult &parsing_results : parsing_results_per_file) + if (mode == InspectMode::All || mode == InspectMode::Code) { - for (ParsedCode &code : parsing_results.code) + // + // NOTE: Build the global definition table + // { - switch(code.type) + std::set> member_type_table; + for (ParsingResult &parsing_results : parsing_results_per_file) { - case ParsedCodeType::Enum: + for (ParsedCode &code : parsing_results.code) { - ParsedEnum const *parsed_enum = &code.parsed_enum; - FprintfIndented(output_file, indent_level, "%.*s,\n", parsed_enum->name.len, parsed_enum->name.str); - for (CPPDeclLinkedList> const *link = parsed_enum->members; - link; - link = link->next) + switch(code.type) { - FprintfIndented(output_file, indent_level, - "%.*s_%.*s,\n", - parsed_enum->name.len, parsed_enum->name.str, - link->value.len, link->value.str - ); - } - - } - break; - - case ParsedCodeType::Struct: - { - ParsedStruct const *parsed_struct = &code.parsed_struct; - FprintfIndented(output_file, indent_level, "%.*s,\n", parsed_struct->name.len, parsed_struct->name.str); - for (CPPDeclLinkedList const *link = parsed_struct->members; - link; - link = link->next) - { - FprintfIndented(output_file, indent_level, - "%.*s_%.*s,\n", - parsed_struct->name.len, parsed_struct->name.str, - link->value.name.len, link->value.name.str - ); - } - } - break; - } - } - } - indent_level--; - FprintfIndented(output_file, indent_level, "};\n\n"); - - // - // NOTE: Build the global type table - // - { - std::set> unique_decl_type_table; - for (ParsingResult &parsing_results : parsing_results_per_file) - { - for (ParsedCode &code : parsing_results.code) - { - switch(code.type) - { - case ParsedCodeType::Enum: - { - ParsedEnum const *parsed_enum = &code.parsed_enum; - unique_decl_type_table.insert(parsed_enum->name); - } - break; - - case ParsedCodeType::Struct: - { - ParsedStruct const *parsed_struct = &code.parsed_struct; - unique_decl_type_table.insert(parsed_struct->name); - - for (CPPDeclLinkedList const *link = parsed_struct->members; - link; - link = link->next) + case ParsedCodeType::Enum: { - CPPVariableDecl const *decl = &link->value; - Slice type_name = {}; - if (decl->template_expr.len > 0) type_name = Asprintf(&global_main_arena, "%.*s<%.*s>", decl->type.len, decl->type.str, decl->template_expr.len, decl->template_expr.str); - else type_name = Asprintf(&global_main_arena, "%.*s", decl->type.len, decl->type.str); - unique_decl_type_table.insert(type_name); - - for (CPPDeclLinkedList const *meta_link = link->metadata_list; - meta_link; - meta_link = meta_link->next) + ParsedEnum const *parsed_enum = &code.parsed_enum; + member_type_table.insert(parsed_enum->name); + for (CPPDeclLinkedList> const *link = parsed_enum->members; + link; + link = link->next) { - CPPVariableDecl const *meta_decl = &meta_link->value; - Slice meta_type_name = {}; - if (meta_decl->template_expr.len > 0) meta_type_name = Asprintf(&global_main_arena, "%.*s<%.*s>", meta_decl->type.len, meta_decl->type.str, meta_decl->template_expr.len, meta_decl->template_expr.str); - else meta_type_name = Asprintf(&global_main_arena, "%.*s", meta_decl->type.len, meta_decl->type.str); - unique_decl_type_table.insert(meta_type_name); + Slice entry = Asprintf(&global_main_arena, "%.*s_%.*s", + parsed_enum->name.len, parsed_enum->name.str, + link->value.len, link->value.str + ); + member_type_table.insert(entry); + } + + } + break; + + case ParsedCodeType::Struct: + { + ParsedStruct const *parsed_struct = &code.parsed_struct; + member_type_table.insert(parsed_struct->name); + for (CPPDeclLinkedList const *link = parsed_struct->members; + link; + link = link->next) + { + Slice entry = Asprintf(&global_main_arena, "%.*s_%.*s", + parsed_struct->name.len, parsed_struct->name.str, + link->value.name.len, link->value.name.str + ); + member_type_table.insert(entry); } } + break; } - break; } } - } - FprintfIndented(output_file, indent_level, "enum struct DqnInspectDeclType\n{\n"); - indent_level++; - for(Slice const &type : unique_decl_type_table ) - { - FprintfIndented(output_file, indent_level, ""); - FprintDeclType(output_file, type); - fprintf(output_file, ",\n"); - } - indent_level--; - FprintfIndented(output_file, indent_level, "};\n\n"); - } - - // - // NOTE: Build the global metadata type table - // - { - std::set> unique_meta_types; - for (ParsingResult &parsing_results : parsing_results_per_file) - { - for (ParsedCode &code : parsing_results.code) + FprintfIndented(output_file, indent_level, "enum struct DqnInspectMemberType\n{\n"); + indent_level++; + for (Slice const &member : member_type_table) { - switch(code.type) + FprintfIndented(output_file, indent_level, "%.*s,\n", member.len, member.str); + } + indent_level--; + FprintfIndented(output_file, indent_level, "};\n\n"); + } + + // + // NOTE: Build the global type table + // + { + std::set> unique_decl_type_table; + for (ParsingResult &parsing_results : parsing_results_per_file) + { + for (ParsedCode &code : parsing_results.code) { - case ParsedCodeType::Struct: + switch(code.type) { - ParsedStruct const *parsed_struct = &code.parsed_struct; - for (CPPDeclLinkedList const *member = parsed_struct->members; member; member = member->next) + case ParsedCodeType::Enum: { - for (CPPDeclLinkedList const *metadata_link = member->metadata_list; - metadata_link; - metadata_link = metadata_link->next) + ParsedEnum const *parsed_enum = &code.parsed_enum; + unique_decl_type_table.insert(parsed_enum->name); + } + break; + + case ParsedCodeType::Struct: + { + ParsedStruct const *parsed_struct = &code.parsed_struct; + unique_decl_type_table.insert(parsed_struct->name); + + for (CPPDeclLinkedList const *link = parsed_struct->members; + link; + link = link->next) { - CPPVariableDecl const *metadata = &metadata_link->value; - unique_meta_types.insert(metadata->name); + CPPVariableDecl const *decl = &link->value; + Slice type_name = {}; + if (decl->template_expr.len > 0) type_name = Asprintf(&global_main_arena, "%.*s<%.*s>", decl->type.len, decl->type.str, decl->template_expr.len, decl->template_expr.str); + else type_name = Asprintf(&global_main_arena, "%.*s", decl->type.len, decl->type.str); + unique_decl_type_table.insert(type_name); + + for (CPPDeclLinkedList const *meta_link = link->metadata_list; + meta_link; + meta_link = meta_link->next) + { + CPPVariableDecl const *meta_decl = &meta_link->value; + Slice meta_type_name = {}; + if (meta_decl->template_expr.len > 0) meta_type_name = Asprintf(&global_main_arena, "%.*s<%.*s>", meta_decl->type.len, meta_decl->type.str, meta_decl->template_expr.len, meta_decl->template_expr.str); + else meta_type_name = Asprintf(&global_main_arena, "%.*s", meta_decl->type.len, meta_decl->type.str); + unique_decl_type_table.insert(meta_type_name); + } } } + break; } - break; } } + + FprintfIndented(output_file, indent_level, "enum struct DqnInspectDeclType\n{\n"); + indent_level++; + for (Slice const &type : unique_decl_type_table) + { + FprintfIndented(output_file, indent_level, ""); + FprintDeclType(output_file, type); + fprintf(output_file, ",\n"); + } + indent_level--; + FprintfIndented(output_file, indent_level, "};\n\n"); + } + + // + // NOTE: Build the global metadata type table + // + { + std::set> unique_meta_types; + for (ParsingResult &parsing_results : parsing_results_per_file) + { + for (ParsedCode &code : parsing_results.code) + { + switch(code.type) + { + case ParsedCodeType::Struct: + { + ParsedStruct const *parsed_struct = &code.parsed_struct; + for (CPPDeclLinkedList const *member = parsed_struct->members; member; member = member->next) + { + for (CPPDeclLinkedList const *metadata_link = member->metadata_list; + metadata_link; + metadata_link = metadata_link->next) + { + CPPVariableDecl const *metadata = &metadata_link->value; + unique_meta_types.insert(metadata->name); + } + } + } + break; + } + } + } + + FprintfIndented(output_file, indent_level, "enum struct DqnInspectMetaType\n{\n"); + indent_level++; + for (Slice const &metadata : unique_meta_types) + FprintfIndented(output_file, indent_level, "%.*s,\n", metadata.len, metadata.str); + indent_level--; + FprintfIndented(output_file, indent_level, "};\n\n"); } - FprintfIndented(output_file, indent_level, "enum struct DqnInspectMetaType\n{\n"); - indent_level++; - for (Slice const &metadata : unique_meta_types) - FprintfIndented(output_file, indent_level, "%.*s,\n", metadata.len, metadata.str); - indent_level--; - FprintfIndented(output_file, indent_level, "};\n\n"); } assert(indent_level == 0); for (ParsingResult &parsing_results : parsing_results_per_file) { + fprintf(output_file, + "//\n" + "// %.*s\n" + "//\n" + "\n" + "#ifndef DQN_INSPECT_%.*s\n" + "#define DQN_INSPECT_%.*s\n" + "\n", + parsing_results.file_name.len, + parsing_results.file_name.str, + parsing_results.file_include_contents_hash_define_len, + parsing_results.file_include_contents_hash_define, + parsing_results.file_include_contents_hash_define_len, + parsing_results.file_include_contents_hash_define + ); + for (ParsedCode &code : parsing_results.code) { switch(code.type) @@ -2116,23 +2131,7 @@ int main(int argc, char *argv[]) for (CPPDeclLinkedList *param_link = parsed_func->members; param_link; param_link = param_link->next) { - // TODO(doyle): HACK. We should parse ptrs into the CPPVariableDecl, fixed size arrays into the name and const-ness into the type CPPVariableDecl *decl = ¶m_link->value; -#if 0 - Slice *type = &decl->type; - char *type_end = (decl->template_expr.len > 0) - ? decl->template_expr.str + decl->template_expr.len + 1 // +1 for the ending ">" on the template - : type->str + type->len; - - Slice *name = &decl->name; - Slice hack_decl_name = {}; - if (name->len > 0) - { - char *name_start = type_end + 1; - char *name_end = name->str + name->len; - hack_decl_name = Slice(name_start, static_cast(name_end - name_start)); - } -#endif fprintf(output_file, "%.*s", decl->type.len, decl->type.str); if (decl->template_expr.len > 0) fprintf(output_file, "<%.*s>", decl->template_expr.len, decl->template_expr.str); diff --git a/Data/DqnInspect_TestData.h b/Data/DqnInspect_TestData.h index 4ccf198..f7c5df3 100644 --- a/Data/DqnInspect_TestData.h +++ b/Data/DqnInspect_TestData.h @@ -30,6 +30,7 @@ DQN_INSPECT struct SampleStruct int *const h; int const* i; int *********const j, k, ******l, *m; + }; DQN_INSPECT enum struct EnumWithMetadata diff --git a/Data/DqnInspect_TestDataGenerated.cpp b/Data/DqnInspect_TestDataGenerated.cpp index 3a4e63e..4ef2f9a 100644 --- a/Data/DqnInspect_TestDataGenerated.cpp +++ b/Data/DqnInspect_TestDataGenerated.cpp @@ -5,32 +5,14 @@ #define CHAR_COUNT(str) (ARRAY_COUNT(str) - 1) #define STR_AND_LEN(str) str, CHAR_COUNT(str) -// -// ..\Data\DqnInspect_TestData.h -// - -#ifndef DQN_INSPECT_DQNINSPECT_TESTDATA_H -#define DQN_INSPECT_DQNINSPECT_TESTDATA_H - enum struct DqnInspectMemberType { SampleStruct, - SampleStruct_ebo, - SampleStruct_vbo, - SampleStruct_vao, SampleStruct_a, SampleStruct_b, SampleStruct_c, SampleStruct_d, SampleStruct_e, - SampleStruct_draw_color, - SampleStruct_lights, - SampleStruct_camera_matrixes, - SampleStruct_bitmaps, - SampleStruct_shaders, - SampleStruct_win32_handle, - SampleStruct_lighting_ambient_coeff, - SampleStruct_draw_call_count, SampleStruct_f, SampleStruct_g, SampleStruct_h, @@ -40,9 +22,20 @@ enum struct DqnInspectMemberType SampleStruct_l, SampleStruct_m, EnumWithMetadata, + SampleStruct_ebo, + SampleStruct_vao, + SampleStruct_vbo, + SampleStruct_lights, + SampleStruct_bitmaps, + SampleStruct_shaders, EnumWithMetadata_Rect, - EnumWithMetadata_Square, EnumWithMetadata_Count, + EnumWithMetadata_Square, + SampleStruct_draw_color, + SampleStruct_win32_handle, + SampleStruct_camera_matrixes, + SampleStruct_draw_call_count, + SampleStruct_lighting_ambient_coeff, }; enum struct DqnInspectDeclType @@ -71,6 +64,13 @@ enum struct DqnInspectMetaType OpenGLVersion, }; +// +// ..\Data\DqnInspect_TestData.h +// + +#ifndef DQN_INSPECT_DQNINSPECT_TESTDATA_H +#define DQN_INSPECT_DQNINSPECT_TESTDATA_H + char const * DqnInspectMetadata_SampleStruct_ebo_DisplayName = "Element Buffer Object"; char const * DqnInspectMetadata_SampleStruct_vao_DisplayName = "Vertex Array Object"; int DqnInspectMetadata_SampleStruct_vao_OpenGLVersion = 330;