From 85ba3615e308b8b263f62c679807c0c612dbfa33 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 11 Feb 2025 18:24:46 -0500 Subject: [PATCH] gen_c11 segregated: metaprogram now splits stb_sprintf_h content between metadesk_deps header and source --- gen_c11/gen_c11.c | 80 +++++++++++++++++++++++------ gen_c11/gen_common.h | 6 +-- third_party/gencpp_c11/gencpp_c11.h | 9 ++-- 3 files changed, 71 insertions(+), 24 deletions(-) diff --git a/gen_c11/gen_c11.c b/gen_c11/gen_c11.c index 61f5316..f8c4e15 100644 --- a/gen_c11/gen_c11.c +++ b/gen_c11/gen_c11.c @@ -7,7 +7,7 @@ #endif #ifndef GENERATE_SINGLEHEADER -#define GENERATE_SINGLEHEADER 1 +#define GENERATE_SINGLEHEADER 0 #endif #define path_refactor_script path_gen_c11 "c11.refactor" @@ -28,20 +28,6 @@ int main() register_library_macros(); - gen_CodeBody tb_stb_printf_h_parsed = gen_parse_file(path_third_party "stb/stb_sprintf.h"); - gen_CodeBody tb_stb_printf_header = gen_def_body(CT_Global_Body); - gen_CodeBody tb_stb_printf_source = gen_def_body(CT_Global_Body); - - for (gen_Code stb_code = gen_iterator(CodeBody, tb_stb_printf_h_parsed, stb_code)) switch(stb_code->Type) - { - case CT_Preprocess_Define: - { - - } - break; - - } - gen_Str generation_notice = lit( "// This file was generated automatially by metadesk's gen_c11.c " "(See: https://github.com/Ed94/metadesk/tree/master)\n\n" @@ -122,6 +108,65 @@ int main() #pragma region Refactored / Formatted gen_Code r_tp_stb_sprintf_h = refactor(tp_stb_sprintf_h); + gen_CodeBody r_tp_stb_sprintf_h_parsed = gen_parse_global_body(tp_stb_sprintf_h->Content); + gen_CodeBody r_tp_stb_sprintf_header = gen_def_body(CT_Global_Body); + gen_CodeBody r_tp_stb_sprintf_source = gen_def_body(CT_Global_Body); + + gen_b32 past_header = false; + gen_b32 past_source = false; + for (gen_Code stb_code = gen_iterator(CodeBody, r_tp_stb_sprintf_h_parsed, stb_code)) switch(stb_code->Type) + { + case CT_Preprocess_IfNotDef: + { + gen_CodePreprocessCond cond_if = gen_cast(gen_CodePreprocessCond, stb_code); + if (gen_str_are_equal(cond_if->Content, lit("STB_SPRINTF_H_INCLUDE"))) + { + gen_body_append(r_tp_stb_sprintf_header, cond_if); + + gen_Code header_code = gen_next_CodeBody(r_tp_stb_sprintf_h_parsed, stb_code); + while (header_code->Type != CT_Comment || !gen_str_contains(header_code->Content, lit("STB_SPRINTF_H_INCLUDE"))) + { + gen_body_append(r_tp_stb_sprintf_header, header_code); + header_code = gen_next_CodeBody(r_tp_stb_sprintf_h_parsed, header_code); + } + // should be: + gen_body_append(r_tp_stb_sprintf_header, header_code); + past_header = true; + + stb_code = gen_next_CodeBody(r_tp_stb_sprintf_h_parsed, header_code); + } + } + break; + case CT_Preprocess_IfDef: + { + gen_CodePreprocessCond cond_if = gen_cast(gen_CodePreprocessCond, stb_code); + if (gen_str_are_equal(cond_if->Content, lit("STB_SPRINTF_IMPLEMENTATION"))) + { + gen_body_append(r_tp_stb_sprintf_source, cond_if); + + gen_Code source_code = gen_next_CodeBody(r_tp_stb_sprintf_h_parsed, stb_code); + while (source_code->Type != CT_Comment || !gen_str_contains(source_code->Content, lit("STB_SPRINTF_IMPLEMENTATION"))) + { + gen_body_append(r_tp_stb_sprintf_source, source_code); + source_code = gen_next_CodeBody(r_tp_stb_sprintf_h_parsed, source_code); + } + // should be: + gen_body_append(r_tp_stb_sprintf_source, source_code); + past_source = true; + + stb_code = gen_next_CodeBody(r_tp_stb_sprintf_h_parsed, source_code); + } + } + break; + + default: + if ( ! past_header || past_source) { + gen_body_append(r_tp_stb_sprintf_header, stb_code); + } + gen_body_append(r_tp_stb_sprintf_source, stb_code); + break; + } + gen_Code r_base_context_cracking_h = refactor(base_context_cracking_h); gen_Code r_base_platform_h = refactor(base_platform_h); gen_Code r_base_linkage_h = refactor(base_linkage_h); @@ -393,7 +438,7 @@ int main() preprocess_endif(); new_line(); - print_section(r_tp_stb_sprintf_h, lit("STB snprintf Header")); + print_section(refactor_and_format(r_tp_stb_sprintf_header), lit("STB snprintf Header")); new_line(); print_section(r_base_base_types_h, lit("Types")); @@ -490,7 +535,8 @@ int main() preprocess_endif(); new_line(); - print_section(r_tp_stb_sprintf_h, lit("STB snprintf Header")); + define(lit("STB_SPRINTF_IMPLEMENTATION"), MT_Statement); + print_section(refactor_and_format(r_tp_stb_sprintf_source), lit("STB snprintf Source")); new_line(); print_section(r_base_platform_c, lit("Platform")); diff --git a/gen_c11/gen_common.h b/gen_c11/gen_common.h index 8d894ae..551024e 100644 --- a/gen_c11/gen_common.h +++ b/gen_c11/gen_common.h @@ -70,14 +70,14 @@ void register_library_macros() gen_register_macros( args( ((gen_Macro) { lit("STB_SPRINTF_H_INCLUDE"), MT_Statement, }), ((gen_Macro) { lit("STBSP__ASAN"), MT_Statement, MF_Allow_As_Attribute }), - ((gen_Macro) { lit("STBSP__PUBLICDEC"), MT_Statement, MF_Allow_As_Attribute }), - ((gen_Macro) { lit("STBSP__ATTRIBUTE_FORMAT"), MT_Statement, MF_Functional | MF_Allow_As_Attribute }), + ((gen_Macro) { lit("STBSP__PUBLICDEC"), MT_Expression, MF_Allow_As_Attribute }), + ((gen_Macro) { lit("STBSP__ATTRIBUTE_FORMAT"), MT_Expression, MF_Functional | MF_Allow_As_Attribute }), ((gen_Macro) { lit("STBSP__NOTUSED"), MT_Expression, MF_Functional | MF_Allow_As_Attribute }), ((gen_Macro) { lit("STBSP__NOTUSED"), MT_Expression, MF_Functional }), ((gen_Macro) { lit("STB_SPRINTF_MIN"), MT_Expression, MF_Allow_As_Identifier }), ((gen_Macro) { lit("STB_SPRINTF_DECORATE"), MT_Expression, MF_Functional | MF_Allow_As_Identifier }), ((gen_Macro) { lit("STB_SPRINTF_MSVC_MODE"), MT_Expression, }), - ((gen_Macro) { lit("STBSP__PUBLICDEF"), MT_Statement, MF_Allow_As_Attribute }), + ((gen_Macro) { lit("STBSP__PUBLICDEF"), MT_Expression, MF_Allow_As_Attribute }), ((gen_Macro) { lit("STBSP__UNALIGNED"), MT_Expression, MF_Functional }), ((gen_Macro) { lit("STBSP__SPECIAL"), MT_Expression, }), ((gen_Macro) { lit("STBSP__LEFTJUST"), MT_Expression, }), diff --git a/third_party/gencpp_c11/gencpp_c11.h b/third_party/gencpp_c11/gencpp_c11.h index 0d87d2d..d74b45f 100644 --- a/third_party/gencpp_c11/gencpp_c11.h +++ b/third_party/gencpp_c11/gencpp_c11.h @@ -10105,7 +10105,7 @@ inline void gen_body_to_strbuilder_ref(gen_CodeBody body, gen_StrBuilder* result { gen_code_to_strbuilder_ref(curr, result); // gen_strbuilder_append_fmt( result, "%SB", gen_code_to_strbuilder(curr) ); - ++curr; + curr = curr->Next; } } @@ -16069,13 +16069,13 @@ void gen_body_to_strbuilder_export( gen_CodeBody body, gen_StrBuilder* result ) GEN_ASSERT(result != gen_nullptr); gen_strbuilder_append_fmt( result, "export\n{\n" ); - gen_Code curr = gen_cast(gen_Code, body); + gen_Code curr = body->Front; gen_s32 left = body->NumEntries; while ( left-- ) { gen_code_to_strbuilder_ref(curr, result); // gen_strbuilder_append_fmt( result, "%SB", gen_code_to_strbuilder(curr) ); - ++curr; + curr = curr->Next; } gen_strbuilder_append_fmt( result, "};\n" ); @@ -23953,7 +23953,7 @@ gen_internal gen_neverinline gen_CodeBody gen_parse_global_nspace(gen_CodeType w case Tok_Preprocess_Macro_Expr: { - if (gen_tok_is_attribute(currtok)) + if (!gen_tok_is_attribute(currtok)) { gen_log_failure("Unbounded macro expression residing in class/struct body\n%S", gen_parser_to_strbuilder(gen__ctx->parser)); return gen_InvalidCode; @@ -25364,6 +25364,7 @@ gen_internal gen_Code gen_parse_simple_preprocess(gen_TokType which) Leave_Scope_Early: gen_Code result = gen_untyped_str(full_macro.Text); + // gen_Code result = gen_untyped_str(gen_strbuilder_to_str(gen_strbuilder_fmt_buf(gen__ctx->Allocator_Temp, "%S ", full_macro.Text))); gen__ctx->parser.Scope->Name = full_macro.Text; gen_parser_pop(&gen__ctx->parser);