From c13b7289c199f4efbbc56cebf4db095918592fe2 Mon Sep 17 00:00:00 2001 From: Ajay Brahmakshatriya Date: Mon, 18 Nov 2024 15:33:44 -0500 Subject: [PATCH] Fixed issue with custom_struct global constructors --- include/builder/block_type_extractor.h | 14 +++++++++----- samples/outputs.var_names/sample56 | 7 +++++++ samples/outputs/sample56 | 7 +++++++ samples/sample56.cpp | 4 ++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/include/builder/block_type_extractor.h b/include/builder/block_type_extractor.h index 3c78de2..9bb2cd4 100644 --- a/include/builder/block_type_extractor.h +++ b/include/builder/block_type_extractor.h @@ -19,16 +19,20 @@ extern int type_naming_counter; template struct type_namer { - static std::string obtained_name; + // Use a pointer to a string instead of + // a string because it is possible get_type_name is called + // from global constructors before obtained_name is initialized + static std::string *obtained_name; static std::string get_type_name() { - if (obtained_name == "") { - obtained_name = "custom_struct" + std::to_string(type_naming_counter++); + if (obtained_name == nullptr) { + obtained_name = new std::string(); + *obtained_name = "custom_struct" + std::to_string(type_naming_counter++); } - return obtained_name; + return *obtained_name; } }; template -std::string type_namer::obtained_name = ""; +std::string *type_namer::obtained_name = nullptr; template struct type_namer::type> { diff --git a/samples/outputs.var_names/sample56 b/samples/outputs.var_names/sample56 index 4844794..66ef6ee 100644 --- a/samples/outputs.var_names/sample56 +++ b/samples/outputs.var_names/sample56 @@ -39,6 +39,12 @@ FUNC_DECL VAR (a_0) INT_CONST (1) INT_CONST (1) + EXPR_STMT + ASSIGN_EXPR + MEMBER_ACCESS_EXPR (mem0) + VAR_EXPR + VAR (p) + INT_CONST (0) struct custom_struct0 { int mem0; float mem1; @@ -53,5 +59,6 @@ void bar (void) { a_0.nested = b_1; (a_0.nested).mem0 = a_0.mem0; ((a_0.nested).mem1 = (a_0.nested).mem1 + 1) - 1; + p.mem0 = 0; } diff --git a/samples/outputs/sample56 b/samples/outputs/sample56 index b10f3aa..ddcb18d 100644 --- a/samples/outputs/sample56 +++ b/samples/outputs/sample56 @@ -39,6 +39,12 @@ FUNC_DECL VAR (var0) INT_CONST (1) INT_CONST (1) + EXPR_STMT + ASSIGN_EXPR + MEMBER_ACCESS_EXPR (mem0) + VAR_EXPR + VAR (p) + INT_CONST (0) struct custom_struct0 { int mem0; float mem1; @@ -53,5 +59,6 @@ void bar (void) { var0.nested = var1; (var0.nested).mem0 = var0.mem0; ((var0.nested).mem1 = (var0.nested).mem1 + 1) - 1; + p.mem0 = 0; } diff --git a/samples/sample56.cpp b/samples/sample56.cpp index 9ca4fe5..6dda718 100644 --- a/samples/sample56.cpp +++ b/samples/sample56.cpp @@ -21,6 +21,9 @@ struct my_type { dyn_var another; }; + +dyn_var p = builder::as_global("p"); + static void bar(void) { dyn_var a; dyn_var b; @@ -29,6 +32,7 @@ static void bar(void) { a.nested.x = a.another; a.nested.y++; + p.x = 0; } int main(int argc, char *argv[]) {