c - Assignment Insertion in ROSE compiler after AssignOp -


lately i've been working rose compiler , able apply few insertions of code c source , successful output. however, haven't been able insert assignment statement when visiting sgassignops. simplified version of code show problem:

#include "rose.h" #include <iostream> #include <transformationsupport.h> using namespace sageinterface; using namespace sagebuilder; using namespace std;  int main (int argc, char *argv[]) {     sgproject *project = frontend (argc, argv);      // find assignment operations     std::vector<sgnode* > assignoplist = nodequery::querysubtree (project, v_sgassignop);      std::vector<sgnode*>::iterator iter;      (iter = assignoplist.begin(); iter!= assignoplist.end(); iter++) {           sgnode * node = (*iter);          sgstatement * asmstmt = transformationsupport::getstatement(node);          //add pragma statement before                ostringstream ospragma;         ospragma << "example statement";         sgpragmadeclaration* pragmadecl = buildpragmadeclaration(ospragma.str());         insertstatementbefore(asmstmt, pragmadecl);          //add assignment statement after         sgexprstatement* newasmt = buildassignstatement(buildvarrefexp("a"),buildintval(9));         insertstatementafter(asmstmt,newasmt);     }      asttests::runalltests(project);      // translation     project->unparse(); } 

a simple input code:

int main(int argc, char* argv[]) {     int a;      = 3;      return a;  } 

the code compiles. however..

when applied input code segmentation fault, no error messages.

if remove assignment statement insertion, pragma insertion works.

if remove call runalltests() following error message when unparsing:

test: /mnt/data/rose/src/backend/unparser/namequalificationsupport.c:4986: virtual namequalificationinheritedattribute namequalificationtraversal::evaluateinheritedattribute(sgnode*, namequalificationinheritedattribute): assertion `initializedname->get_parent() != __null' failed. aborted (core dumped) 

notice i'm using "a" in example , in input code, variable declared.

what right way make assignment insertion that?

the provided example in tutorial uses scope on main function body , inserts @ end, need happen after assignment. need push scope on node scopestack? , if so, how expressed? i'm not familiar push , pop scope stack methods.

this old, answering people problem in future.

the error comes when calling buildvarrefexp , buildpragmadeclaration. can bee seen through doxygen, take optional parameter sgscopestatement. scope statement sagebuilder finds variable referenced (github source). if not given, takes scopestack. however, when haven't pushed onto scopestack , call these functions without optional parameter, not know look.

so, 2 solutions are:

  1. use scopestack storing sgscopestatement (eg pushscopestack)
  2. pass sgscopestatement buildvarrefexp , buildpragmadeclaration argument.

in case specifically, do:

sgscopestatement scope = transformationsupport::getfunctiondeclaration(node)->get_scope(); 

and replace sagebuilder statements with:

buildpragmadeclaration(ospragma.str(), scope); buildvarrefexp("a", scope); 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -