Adding a global static instance of a class and having the constructor invoked on initialization
Stephan Friedl
stephanf01@yahoo.com
Sat Jan 25 05:38:00 GMT 2014
I am writing a GCC plugin and am trying to add a global variable which is a class instance. The class I am trying to create a global static variable from is:
class CTestClass
{
public :
CTestClass()
{
std::cout << "Test Class Initialized." << std::endl;
}
};
The key code snippet looks like this (with the appropriate type in declType):
tree globalDeclaration = build_decl( UNKNOWN_LOCATION, VAR_DECL, get_identifier( globalDecl.name().c_str() ), declType );
/* allocate static storage for this variable */
TREE_STATIC( globalDeclaration ) = 1;
/* static: internal linkage */
TREE_PUBLIC( globalDeclaration ) = 1;
DECL_EXTERNAL( globalDeclaration ) = 0;
layout_decl( globalDeclaration, false );
rest_of_decl_compilation( globalDeclaration, 1, 0 );
I have tried inserting this into a number of passes and though there do not appear to be any errors, I have not been able to get the constructor for the global to appear in the __static_initialization_and_destruction_0() function dumped using the -fdump-tree-gimple command line option.
What I am wondering is how the variables to be initialized in the __static_initialization_and_destruction_0() function are identified and if it is possible to insert a global instance of a class such that it will automatically get initialized in the __static_initialization_and_destruction_0() function -or- if I have to modify the __static_initialization_and_destruction_0() function's gimple representation and inject the call to the constructor there. I have poked through the source code for the cpp parser and nothing has leapt out at me as a possible code segment to follow as an example.
Any suggestions or pointers would be most appreciated.
Thanks,
Stephan
More information about the Gcc-help
mailing list