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,161 +1555,189 @@ 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));
} }
//
// NOTE: Build the global definition table
//
int indent_level = 0; int indent_level = 0;
FprintfIndented(output_file, indent_level, "enum struct DqnInspectMemberType\n{\n"); if (mode == InspectMode::All || mode == InspectMode::Code)
indent_level++;
for (ParsingResult &parsing_results : parsing_results_per_file)
{ {
for (ParsedCode &code : parsing_results.code) //
// NOTE: Build the global definition table
//
{ {
switch(code.type) std::set<Slice<char>> 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; switch(code.type)
FprintfIndented(output_file, indent_level, "%.*s,\n", parsed_enum->name.len, parsed_enum->name.str);
for (CPPDeclLinkedList<Slice<char>> const *link = parsed_enum->members;
link;
link = link->next)
{ {
FprintfIndented(output_file, indent_level, case ParsedCodeType::Enum:
"%.*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<CPPVariableDecl> 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<Slice<char>> 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<CPPVariableDecl> const *link = parsed_struct->members;
link;
link = link->next)
{ {
CPPVariableDecl const *decl = &link->value; ParsedEnum const *parsed_enum = &code.parsed_enum;
Slice<char> type_name = {}; member_type_table.insert(parsed_enum->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); for (CPPDeclLinkedList<Slice<char>> const *link = parsed_enum->members;
else type_name = Asprintf(&global_main_arena, "%.*s", decl->type.len, decl->type.str); link;
unique_decl_type_table.insert(type_name); link = link->next)
for (CPPDeclLinkedList<CPPVariableDecl> const *meta_link = link->metadata_list;
meta_link;
meta_link = meta_link->next)
{ {
CPPVariableDecl const *meta_decl = &meta_link->value; Slice<char> entry = Asprintf(&global_main_arena, "%.*s_%.*s",
Slice<char> meta_type_name = {}; parsed_enum->name.len, parsed_enum->name.str,
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); link->value.len, link->value.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); 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<CPPVariableDecl> const *link = parsed_struct->members;
link;
link = link->next)
{
Slice<char> 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"); FprintfIndented(output_file, indent_level, "enum struct DqnInspectMemberType\n{\n");
indent_level++; indent_level++;
for(Slice<char> const &type : unique_decl_type_table ) for (Slice<char> const &member : member_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<Slice<char>> unique_meta_types;
for (ParsingResult &parsing_results : parsing_results_per_file)
{
for (ParsedCode &code : parsing_results.code)
{ {
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<Slice<char>> 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; case ParsedCodeType::Enum:
for (CPPDeclLinkedList<CPPVariableDecl> const *member = parsed_struct->members; member; member = member->next)
{ {
for (CPPDeclLinkedList<CPPVariableDecl> const *metadata_link = member->metadata_list; ParsedEnum const *parsed_enum = &code.parsed_enum;
metadata_link; unique_decl_type_table.insert(parsed_enum->name);
metadata_link = metadata_link->next) }
break;
case ParsedCodeType::Struct:
{
ParsedStruct const *parsed_struct = &code.parsed_struct;
unique_decl_type_table.insert(parsed_struct->name);
for (CPPDeclLinkedList<CPPVariableDecl> const *link = parsed_struct->members;
link;
link = link->next)
{ {
CPPVariableDecl const *metadata = &metadata_link->value; CPPVariableDecl const *decl = &link->value;
unique_meta_types.insert(metadata->name); Slice<char> 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<CPPVariableDecl> const *meta_link = link->metadata_list;
meta_link;
meta_link = meta_link->next)
{
CPPVariableDecl const *meta_decl = &meta_link->value;
Slice<char> 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<char> 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<Slice<char>> 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<CPPVariableDecl> const *member = parsed_struct->members; member; member = member->next)
{
for (CPPDeclLinkedList<CPPVariableDecl> 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<char> 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<char> 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); 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;