Purpose Insert in the ROSE compiler after AssignOp

Recently, I worked with the ROSE compiler, and I managed to apply several code attachments to the C source code and get a successful output. However, when I visited SgAssignOps, I was unable to insert an assignment operator. This is a simplified version of my code to show the 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 all Assignment operations
    std::vector<SgNode* > assignOpList = NodeQuery::querySubTree (project, V_SgAssignOp);

    std::vector<SgNode*>::iterator iter;

    for (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 only
    project->unparse();
}

Simple input code:

int main(int argc, char* argv[])
{
    int a;

    a = 3;

    return a;

}

The code is compiling. However..

When applied to the input code, I get a segmentation error, no error messages.

If I delete the insertion of an assignment statement, pragmatic insertion works.

If I remove the call to runAllTests (), I get the following error message when disclosing:

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 that I use "a" in this example and in the input code, so the variable is already declared.

What would be the correct way to insert such an assignment?

, , . Scope node ScopeStack? , ? -.

+4
2

, .

, buildVarRefExp buildPragmaDeclaration. doxygen, SgScopeStatement. , SageBuilder ( github). , ScopeStack. , ScopeStack , , .

, :

  • ScopeStack SgScopeStatement (, pushScopeStack)
  • SgScopeStatement buildVarRefExp buildPragmaDeclaration .

:

SgScopeStatement scope = TransformationSupport::getFunctionDeclaration(node)->get_scope();

SageBuilder :

buildPragmaDeclaration(osPragma.str(), scope);
buildVarRefExp("a", scope);
+1

... , .

, , :

    pushScopeStack (TransformationSupport::getFunctionDeclaration(node)->get_scope());
    SgExprStatement* newAsmt = buildAssignStatement(buildVarRefExp("a"),buildIntVal(9));        
    insertStatementAfter(asmStmt,newAsmt);
    popScopeStack();

, , , ScopeStack .

0

All Articles