Fix some ordering issues

This commit is contained in:
doyle 2019-04-06 01:30:09 +11:00
parent 41ac05b401
commit c444d50d17
3 changed files with 188 additions and 188 deletions

View File

@ -122,6 +122,18 @@ struct DeferHelper
Defer<Lambda> operator+(Lambda lambda) { return Defer<Lambda>(lambda); } Defer<Lambda> operator+(Lambda lambda) { return Defer<Lambda>(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<char const>{str, CHAR_COUNT(str)} #define SLICE_LITERAL(str) Slice<char const>{str, CHAR_COUNT(str)}
template <typename T> template <typename T>
struct Slice struct Slice
@ -155,16 +167,6 @@ struct LinkedList
LinkedList *next; 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 // Memory Utilities
// //
@ -868,6 +870,7 @@ struct ParsingResult
char *file_include_contents_hash_define; char *file_include_contents_hash_define;
int max_func_return_type_decl_len; int max_func_return_type_decl_len;
int max_func_name_decl_len; int max_func_name_decl_len;
Slice<char> 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 = 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_include_contents_hash_define_len = extracted_file_name_len;
parsing_results.file_name = Slice<char>(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 = {}; CPPTokeniser tokeniser = {};
tokeniser.tokens_max = 16384; tokeniser.tokens_max = 16384;
tokeniser.tokens = MEM_ARENA_ALLOC_ARRAY(&global_main_arena, CPPToken, tokeniser.tokens_max); 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); CPPToken *sentinel = CPPTokeniser_MakeToken(&tokeniser);
sentinel->type = CPPTokenType::EndOfStream; 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); for (CPPToken token = CPPTokeniser_PeekToken(&tokeniser);
; ;
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)) if (ParseCPPInspectPrototype(&tokeniser, &parsed_code.parsed_func_prototype))
{ {
parsed_code.type = ParsedCodeType::FunctionPrototype; 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_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(max_func_name_decl_len, parsed_code.parsed_func_prototype.name.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,12 +1555,14 @@ int main(int argc, char *argv[])
parsing_results_per_file.push_back(std::move(parsing_results)); parsing_results_per_file.push_back(std::move(parsing_results));
} }
int indent_level = 0;
if (mode == InspectMode::All || mode == InspectMode::Code)
{
// //
// NOTE: Build the global definition table // NOTE: Build the global definition table
// //
int indent_level = 0; {
FprintfIndented(output_file, indent_level, "enum struct DqnInspectMemberType\n{\n"); std::set<Slice<char>> member_type_table;
indent_level++;
for (ParsingResult &parsing_results : parsing_results_per_file) for (ParsingResult &parsing_results : parsing_results_per_file)
{ {
for (ParsedCode &code : parsing_results.code) for (ParsedCode &code : parsing_results.code)
@ -1583,16 +1572,16 @@ int main(int argc, char *argv[])
case ParsedCodeType::Enum: case ParsedCodeType::Enum:
{ {
ParsedEnum const *parsed_enum = &code.parsed_enum; ParsedEnum const *parsed_enum = &code.parsed_enum;
FprintfIndented(output_file, indent_level, "%.*s,\n", parsed_enum->name.len, parsed_enum->name.str); member_type_table.insert(parsed_enum->name);
for (CPPDeclLinkedList<Slice<char>> const *link = parsed_enum->members; for (CPPDeclLinkedList<Slice<char>> const *link = parsed_enum->members;
link; link;
link = link->next) link = link->next)
{ {
FprintfIndented(output_file, indent_level, Slice<char> entry = Asprintf(&global_main_arena, "%.*s_%.*s",
"%.*s_%.*s,\n",
parsed_enum->name.len, parsed_enum->name.str, parsed_enum->name.len, parsed_enum->name.str,
link->value.len, link->value.str link->value.len, link->value.str
); );
member_type_table.insert(entry);
} }
} }
@ -1601,24 +1590,32 @@ int main(int argc, char *argv[])
case ParsedCodeType::Struct: case ParsedCodeType::Struct:
{ {
ParsedStruct const *parsed_struct = &code.parsed_struct; ParsedStruct const *parsed_struct = &code.parsed_struct;
FprintfIndented(output_file, indent_level, "%.*s,\n", parsed_struct->name.len, parsed_struct->name.str); member_type_table.insert(parsed_struct->name);
for (CPPDeclLinkedList<CPPVariableDecl> const *link = parsed_struct->members; for (CPPDeclLinkedList<CPPVariableDecl> const *link = parsed_struct->members;
link; link;
link = link->next) link = link->next)
{ {
FprintfIndented(output_file, indent_level, Slice<char> entry = Asprintf(&global_main_arena, "%.*s_%.*s",
"%.*s_%.*s,\n",
parsed_struct->name.len, parsed_struct->name.str, parsed_struct->name.len, parsed_struct->name.str,
link->value.name.len, link->value.name.str link->value.name.len, link->value.name.str
); );
member_type_table.insert(entry);
} }
} }
break; break;
} }
} }
} }
FprintfIndented(output_file, indent_level, "enum struct DqnInspectMemberType\n{\n");
indent_level++;
for (Slice<char> const &member : member_type_table)
{
FprintfIndented(output_file, indent_level, "%.*s,\n", member.len, member.str);
}
indent_level--; indent_level--;
FprintfIndented(output_file, indent_level, "};\n\n"); FprintfIndented(output_file, indent_level, "};\n\n");
}
// //
// NOTE: Build the global type table // NOTE: Build the global type table
@ -1672,7 +1669,7 @@ int main(int argc, char *argv[])
FprintfIndented(output_file, indent_level, "enum struct DqnInspectDeclType\n{\n"); FprintfIndented(output_file, indent_level, "enum struct DqnInspectDeclType\n{\n");
indent_level++; indent_level++;
for(Slice<char> const &type : unique_decl_type_table ) for (Slice<char> const &type : unique_decl_type_table)
{ {
FprintfIndented(output_file, indent_level, ""); FprintfIndented(output_file, indent_level, "");
FprintDeclType(output_file, type); FprintDeclType(output_file, type);
@ -1720,9 +1717,27 @@ int main(int argc, char *argv[])
FprintfIndented(output_file, indent_level, "};\n\n"); FprintfIndented(output_file, indent_level, "};\n\n");
} }
}
assert(indent_level == 0); assert(indent_level == 0);
for (ParsingResult &parsing_results : parsing_results_per_file) 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) for (ParsedCode &code : parsing_results.code)
{ {
switch(code.type) switch(code.type)
@ -2116,23 +2131,7 @@ int main(int argc, char *argv[])
for (CPPDeclLinkedList<CPPVariableDecl> *param_link = parsed_func->members; param_link; param_link = param_link->next) for (CPPDeclLinkedList<CPPVariableDecl> *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 = &param_link->value; CPPVariableDecl *decl = &param_link->value;
#if 0
Slice<char> *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<char> *name = &decl->name;
Slice<char> hack_decl_name = {};
if (name->len > 0)
{
char *name_start = type_end + 1;
char *name_end = name->str + name->len;
hack_decl_name = Slice<char>(name_start, static_cast<int>(name_end - name_start));
}
#endif
fprintf(output_file, "%.*s", decl->type.len, decl->type.str); fprintf(output_file, "%.*s", decl->type.len, decl->type.str);
if (decl->template_expr.len > 0) if (decl->template_expr.len > 0)
fprintf(output_file, "<%.*s>", decl->template_expr.len, decl->template_expr.str); fprintf(output_file, "<%.*s>", decl->template_expr.len, decl->template_expr.str);

View File

@ -30,6 +30,7 @@ DQN_INSPECT struct SampleStruct
int *const h; int *const h;
int const* i; int const* i;
int *********const j, k, ******l, *m; int *********const j, k, ******l, *m;
}; };
DQN_INSPECT enum struct EnumWithMetadata DQN_INSPECT enum struct EnumWithMetadata

View File

@ -5,32 +5,14 @@
#define CHAR_COUNT(str) (ARRAY_COUNT(str) - 1) #define CHAR_COUNT(str) (ARRAY_COUNT(str) - 1)
#define STR_AND_LEN(str) str, CHAR_COUNT(str) #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 enum struct DqnInspectMemberType
{ {
SampleStruct, SampleStruct,
SampleStruct_ebo,
SampleStruct_vbo,
SampleStruct_vao,
SampleStruct_a, SampleStruct_a,
SampleStruct_b, SampleStruct_b,
SampleStruct_c, SampleStruct_c,
SampleStruct_d, SampleStruct_d,
SampleStruct_e, 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_f,
SampleStruct_g, SampleStruct_g,
SampleStruct_h, SampleStruct_h,
@ -40,9 +22,20 @@ enum struct DqnInspectMemberType
SampleStruct_l, SampleStruct_l,
SampleStruct_m, SampleStruct_m,
EnumWithMetadata, EnumWithMetadata,
SampleStruct_ebo,
SampleStruct_vao,
SampleStruct_vbo,
SampleStruct_lights,
SampleStruct_bitmaps,
SampleStruct_shaders,
EnumWithMetadata_Rect, EnumWithMetadata_Rect,
EnumWithMetadata_Square,
EnumWithMetadata_Count, EnumWithMetadata_Count,
EnumWithMetadata_Square,
SampleStruct_draw_color,
SampleStruct_win32_handle,
SampleStruct_camera_matrixes,
SampleStruct_draw_call_count,
SampleStruct_lighting_ambient_coeff,
}; };
enum struct DqnInspectDeclType enum struct DqnInspectDeclType
@ -71,6 +64,13 @@ enum struct DqnInspectMetaType
OpenGLVersion, 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_ebo_DisplayName = "Element Buffer Object";
char const * DqnInspectMetadata_SampleStruct_vao_DisplayName = "Vertex Array Object"; char const * DqnInspectMetadata_SampleStruct_vao_DisplayName = "Vertex Array Object";
int DqnInspectMetadata_SampleStruct_vao_OpenGLVersion = 330; int DqnInspectMetadata_SampleStruct_vao_OpenGLVersion = 330;