[Prev][Next][Index]

SgForStmt->setbody() and SgStatement->insertAfterStmt()




   Greetings, oh, sagacious bug fixers.  I ran into some problems
integrating my new stuff with recent sage changes.  Specifically,
the call graph manager was killing itself (i.e. seg faulting) 
trying to understand the way I muddled with the dep file.  

    One troubling instance involved using the setBody() method 
of SgForStmt.  It seems that when you use this method, it does
indeed set the body of the for statement to the new statement(s)
specified in the method call, but it does not remove the other
"old" body from the dep file.  The call graph manager was
examining the "old" body which had no legitimate place, scope,
or lexical order, and so the manager would die.  I hacked a fix, 
did my own setbody().  What is the right behavoir of setbody()?
Was I just using it incorrectly?

    The second troubling instance involved repeatedly inserting 
IF_NODE statements (I didn't test it on other statements with 
bodies).  Basically, the way I got it to work was by adding them
in reverse order.  While this is not ideal, I have it working in
my code.  I have the three approaches I tried, including the one
that works.  They involve calling the SgStatement method 
insertStmtAfter() method in different ways.  The three cases
are coded in the program below.  I included the dumpdep output of
the dep files that each case in the program produces - after the  
code.

good day,
darryl


//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////

#include "sage++user.h"

main(int argc, char **argv) {
  
  char *fn;
  SgProject *proj;
  SgFile *f;
  
  
  if (argc != 2) exit;

  proj = new SgProject (argv[1]);
  f = &(proj->file(0));
  
  SgExpression *fexp;
  SgStatement *st;
  SgStatement *body, *ifnode;
  SgFuncHedrStmt *fhdr, *findHedrStmt(char *, SgFile *);
  int i;

  fhdr = (SgFuncHedrStmt *) findHedrStmt("tmpfunc", f);
  // start with last declaration.
  st = fhdr->lexNext();
  fexp = new SgValueExp(1);


  for ( i= 0; i < 3; i++)   {

    //create the if statemnet and add it 
    body = new SgCExpStmt(*(new SgValueExp(1)));
    ifnode = new SgIfStmt(*fexp, *body);

    // --------------------------------------------
    // Case 1:
    //  Always insert after the last declaration.
    //  Works Correctly, but you have to add statements in
    //  reverse order. 
    //
    //  add the statement
    st->insertStmtAfter(*ifnode, *fhdr);

    // --------------------------------------------

    // --------------------------------------------
    // Case 2:
    //  Insert new statement after the last statement in the 
    //  body of the previous IF_NODE.
    //
    //  Works INCORRECTLY, dep file ptrs are right
    //  (i.e. control parent, child lists, etc.), but the CONTROL_END 
    //  statements that terminate the IF statements are all 
    //  at the end of the function in the dep file (lexically ?), 
    //  instead of directly following the IF_NODE statement they 
    //  supposedly terminate.  
    /* 
    st->insertStmtAfter(*ifnode, *fhdr);
     */
    //  for the next iteration, insert after body...
    /*
    st = body;
     */
    // --------------------------------------------

    // --------------------------------------------
    // Case 3:
    //  Insert after the last IF_NODE statement.  
    //
    //  Works INCORRECTLY, dep file ptrs are right
    //  (i.e. control parent, child lists, etc. ), but the IF_NODE 
    //  statements immediately follow each other in the dep file, 
    //  leaving the bodies of the IF_NODES and the CONTROL_ENDS
    //  of the IF_NODE until the end of the function.
    /* 
    st->insertStmtAfter(*ifnode, *fhdr);
     */
    //  for the next iteration, insert after ifnode...
    /*
    st = ifnode;
     */
    
  }
  
  //case 1:
  f->saveDepFile("good1.dep");
  //case 2:
  //f->saveDepFile("bad2.dep");
  //case 3:
  //f->saveDepFile("bad3.dep");
}


SgFuncHedrStmt * findHedrStmt(char *funcname, SgFile *f) {
  SgStatement *st;
  SgFuncHedrStmt *fhdr;

  st = f->firstStatement();
  while (st) {
    if ((fhdr = isSgFuncHedrStmt(st)) &&
	(strcmp(fhdr->symbol()->identifier(), funcname) == 0)) {
      return fhdr;
    }
    st = st->lexNext();
  }
  return NULL;
}



//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////

dumpdep good1.dep :

Source is "tmp.C"

 #blob  #bfnd  #llnd  #symb      #type  #labl  GblBf #deps  #cmnt  #file
-------------------------------------------------------------------------
   11     12      8      4	   11     0      1      0      0      1


***BIF NODES***

  id  var  cp   bp1  cmnt  sym    lp1    lp2    lp3    dp1  dp2   lab  lbl glin llin  file  thread
  1-B 100  ----  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F     2-B       GLOBAL
               2-B    --
              --
         decl_specs(bits set)= 
  2-B 130   1-B  --  ----   3-S    2-E   ----   ----   --   --   ----  --   1    0     1-F     3-B    FUNC_HEDR
               3-B     4-B     7-B    10-B    --
              --
         decl_specs(bits set)= 
  3-B 154   2-B  --  ----  ----    4-E   ----   ----   --   --   ----  --   2    0     1-F     4-B     VAR_DECL
              --
              --
         decl_specs(bits set)= 
  4-B 105   2-B  --  ----  ----    5-E   ----   ----   --   --   ----  --   0    0     1-F     5-B      IF_NODE
               5-B     6-B    --
              --
         decl_specs(bits set)= 
  5-B 173   4-B  --  ----  ----    8-E   ----   ----   --   --   ----  --   0    0     1-F     6-B EXPR_STMT_NODE
              --
              --
         decl_specs(bits set)= 
  6-B 104   4-B  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F     7-B  CONTROL_END
              --
              --
         decl_specs(bits set)= 
  7-B 105   2-B  --  ----  ----    5-E   ----   ----   --   --   ----  --   0    0     1-F     8-B      IF_NODE
               8-B     9-B    --
              --
         decl_specs(bits set)= 
  8-B 173   7-B  --  ----  ----    7-E   ----   ----   --   --   ----  --   0    0     1-F     9-B EXPR_STMT_NODE
              --
              --
         decl_specs(bits set)= 
  9-B 104   7-B  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F    10-B  CONTROL_END
              --
              --
         decl_specs(bits set)= 
 10-B 105   2-B  --  ----  ----    5-E   ----   ----   --   --   ----  --   0    0     1-F    11-B      IF_NODE
              11-B    12-B    --
              --
         decl_specs(bits set)= 
 11-B 173  10-B  --  ----  ----    6-E   ----   ----   --   --   ----  --   0    0     1-F    12-B EXPR_STMT_NODE
              --
              --
         decl_specs(bits set)= 
 12-B 104  10-B  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F    ----  CONTROL_END
              --
              --
         decl_specs(bits set)= 

***L0W-LEVEL NODES***

    id    var    type    symb    ll1     ll2                     tag...
    1-E   396    ----     3-S    ----    ----                  FUNCTION_REF
    2-E   312     1-T    ----     1-E    ----                     EXPR_LIST
    3-E   307    ----     4-S    ----    ----                       VAR_REF
    4-E   312     1-T    ----     3-E    ----                     EXPR_LIST
    5-E   300     1-T                       1                       INT_VAL
    6-E   300     1-T                       1                       INT_VAL
    7-E   300     1-T                       1                       INT_VAL
    8-E   300     1-T                       1                       INT_VAL

***SYMBOL NODES***

   id   var   type  attr  next   scope  variable-name
   1-S  550   ----    0   ----   ----              *                                               DEFAULT
   2-S  504   ----    0    3-S    1-B    ElementType                                             TYPE_NAME
   3-S  509    1-T    0   ----    1-B        tmpfunc n_in=    0 n_out=    0 n_io=    0
	                               in_l=   ---- out_l=   ---- sy_l=   ---- p_hdr=    2-B     FUNCTION_NAME
   4-S  503    1-T    0   ----    2-B         tester local=   0 nxt_in=  ---- nxt_out=  ----     VARIABLE_NAME

***TYPE NODES***

   id   var name length
   1-T  551   --   --                                                                T_INT
   2-T  552   --   --                                                              T_FLOAT
   3-T  553   --   --                                                             T_DOUBLE
   4-T  554   --   --                                                               T_CHAR
   5-T  556   --   --                                                             T_STRING
   6-T  555   --   --                                                               T_BOOL
   7-T  564   --   --                                                            T_COMPLEX
   8-T  550   --   --                                                              DEFAULT
   9-T  565   --   --                                                               T_VOID
  10-T  563   --   --                                                            T_UNKNOWN
  11-T  573   --   --  der_typ=      2-S                                     T_DERIVED_TYPE

  bits set:   0 syn/protected,  1 shared/public,  2 private,  3 future,  4 virtual,
                5 inline,  6 unsigned,  7 signed,  8 short,  9 long,  10 volatile,
                11 const,  12 typedef,  13 extern,  14 friend,  15 static,  16 register,
                17 auto,  18 global,  19 Sync,  20 atomic,  21 __private, 22 restrict

***LABEL NODES***


***COMMENT NODES***


***FILENAME NODES***

    1-F tmp.C



//////////////////////////////////////////////////////////

dumpdep bad2.dep :

Source is "tmp.C"

 #blob  #bfnd  #llnd  #symb      #type  #labl  GblBf #deps  #cmnt  #file
-------------------------------------------------------------------------
   11     12      8      4	   11     0      1      0      0      1


***BIF NODES***

  id  var  cp   bp1  cmnt  sym    lp1    lp2    lp3    dp1  dp2   lab  lbl glin llin  file  thread
  1-B 100  ----  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F     2-B       GLOBAL
               2-B    --
              --
         decl_specs(bits set)= 
  2-B 130   1-B  --  ----   3-S    2-E   ----   ----   --   --   ----  --   1    0     1-F     3-B    FUNC_HEDR
               3-B     4-B     6-B     8-B    --
              --
         decl_specs(bits set)= 
  3-B 154   2-B  --  ----  ----    4-E   ----   ----   --   --   ----  --   2    0     1-F     4-B     VAR_DECL
              --
              --
         decl_specs(bits set)= 
  4-B 105   2-B  --  ----  ----    5-E   ----   ----   --   --   ----  --   0    0     1-F     5-B      IF_NODE
               5-B    12-B    --
              --
         decl_specs(bits set)= 
  5-B 173   4-B  --  ----  ----    6-E   ----   ----   --   --   ----  --   0    0     1-F     6-B EXPR_STMT_NODE
              --
              --
         decl_specs(bits set)= 
  6-B 105   2-B  --  ----  ----    5-E   ----   ----   --   --   ----  --   0    0     1-F     7-B      IF_NODE
               7-B    11-B    --
              --
         decl_specs(bits set)= 
  7-B 173   6-B  --  ----  ----    7-E   ----   ----   --   --   ----  --   0    0     1-F     8-B EXPR_STMT_NODE
              --
              --
         decl_specs(bits set)= 
  8-B 105   2-B  --  ----  ----    5-E   ----   ----   --   --   ----  --   0    0     1-F     9-B      IF_NODE
               9-B    10-B    --
              --
         decl_specs(bits set)= 
  9-B 173   8-B  --  ----  ----    8-E   ----   ----   --   --   ----  --   0    0     1-F    10-B EXPR_STMT_NODE
              --
              --
         decl_specs(bits set)= 
 10-B 104   8-B  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F    11-B  CONTROL_END
              --
              --
         decl_specs(bits set)= 
 11-B 104   6-B  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F    12-B  CONTROL_END
              --
              --
         decl_specs(bits set)= 
 12-B 104   4-B  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F    ----  CONTROL_END
              --
              --
         decl_specs(bits set)= 

***L0W-LEVEL NODES***

    id    var    type    symb    ll1     ll2                     tag...
    1-E   396    ----     3-S    ----    ----                  FUNCTION_REF
    2-E   312     1-T    ----     1-E    ----                     EXPR_LIST
    3-E   307    ----     4-S    ----    ----                       VAR_REF
    4-E   312     1-T    ----     3-E    ----                     EXPR_LIST
    5-E   300     1-T                       1                       INT_VAL
    6-E   300     1-T                       1                       INT_VAL
    7-E   300     1-T                       1                       INT_VAL
    8-E   300     1-T                       1                       INT_VAL

***SYMBOL NODES***

   id   var   type  attr  next   scope  variable-name
   1-S  550   ----    0   ----   ----              *                                               DEFAULT
   2-S  504   ----    0    3-S    1-B    ElementType                                             TYPE_NAME
   3-S  509    1-T    0   ----    1-B        tmpfunc n_in=    0 n_out=    0 n_io=    0
	                               in_l=   ---- out_l=   ---- sy_l=   ---- p_hdr=    2-B     FUNCTION_NAME
   4-S  503    1-T    0   ----    2-B         tester local=   0 nxt_in=  ---- nxt_out=  ----     VARIABLE_NAME

***TYPE NODES***

   id   var name length
   1-T  551   --   --                                                                T_INT
   2-T  552   --   --                                                              T_FLOAT
   3-T  553   --   --                                                             T_DOUBLE
   4-T  554   --   --                                                               T_CHAR
   5-T  556   --   --                                                             T_STRING
   6-T  555   --   --                                                               T_BOOL
   7-T  564   --   --                                                            T_COMPLEX
   8-T  550   --   --                                                              DEFAULT
   9-T  565   --   --                                                               T_VOID
  10-T  563   --   --                                                            T_UNKNOWN
  11-T  573   --   --  der_typ=      2-S                                     T_DERIVED_TYPE

  bits set:   0 syn/protected,  1 shared/public,  2 private,  3 future,  4 virtual,
                5 inline,  6 unsigned,  7 signed,  8 short,  9 long,  10 volatile,
                11 const,  12 typedef,  13 extern,  14 friend,  15 static,  16 register,
                17 auto,  18 global,  19 Sync,  20 atomic,  21 __private, 22 restrict

***LABEL NODES***


***COMMENT NODES***


***FILENAME NODES***

    1-F tmp.C



//////////////////////////////////////////////////////////

dumpdep bad3.dep :


Source is "tmp.C"

 #blob  #bfnd  #llnd  #symb      #type  #labl  GblBf #deps  #cmnt  #file
-------------------------------------------------------------------------
   11     12      8      4	   11     0      1      0      0      1


***BIF NODES***

  id  var  cp   bp1  cmnt  sym    lp1    lp2    lp3    dp1  dp2   lab  lbl glin llin  file  thread
  1-B 100  ----  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F     2-B       GLOBAL
               2-B    --
              --
         decl_specs(bits set)= 
  2-B 130   1-B  --  ----   3-S    2-E   ----   ----   --   --   ----  --   1    0     1-F     3-B    FUNC_HEDR
               3-B     4-B     5-B     6-B    --
              --
         decl_specs(bits set)= 
  3-B 154   2-B  --  ----  ----    4-E   ----   ----   --   --   ----  --   2    0     1-F     4-B     VAR_DECL
              --
              --
         decl_specs(bits set)= 
  4-B 105   2-B  --  ----  ----    5-E   ----   ----   --   --   ----  --   0    0     1-F     5-B      IF_NODE
              11-B    12-B    --
              --
         decl_specs(bits set)= 
  5-B 105   2-B  --  ----  ----    5-E   ----   ----   --   --   ----  --   0    0     1-F     6-B      IF_NODE
               9-B    10-B    --
              --
         decl_specs(bits set)= 
  6-B 105   2-B  --  ----  ----    5-E   ----   ----   --   --   ----  --   0    0     1-F     7-B      IF_NODE
               7-B     8-B    --
              --
         decl_specs(bits set)= 
  7-B 173   6-B  --  ----  ----    8-E   ----   ----   --   --   ----  --   0    0     1-F     8-B EXPR_STMT_NODE
              --
              --
         decl_specs(bits set)= 
  8-B 104   6-B  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F     9-B  CONTROL_END
              --
              --
         decl_specs(bits set)= 
  9-B 173   5-B  --  ----  ----    7-E   ----   ----   --   --   ----  --   0    0     1-F    10-B EXPR_STMT_NODE
              --
              --
         decl_specs(bits set)= 
 10-B 104   5-B  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F    11-B  CONTROL_END
              --
              --
         decl_specs(bits set)= 
 11-B 173   4-B  --  ----  ----    6-E   ----   ----   --   --   ----  --   0    0     1-F    12-B EXPR_STMT_NODE
              --
              --
         decl_specs(bits set)= 
 12-B 104   4-B  --  ----  ----   ----   ----   ----   --   --   ----  --   0    0     1-F    ----  CONTROL_END
              --
              --
         decl_specs(bits set)= 

***L0W-LEVEL NODES***

    id    var    type    symb    ll1     ll2                     tag...
    1-E   396    ----     3-S    ----    ----                  FUNCTION_REF
    2-E   312     1-T    ----     1-E    ----                     EXPR_LIST
    3-E   307    ----     4-S    ----    ----                       VAR_REF
    4-E   312     1-T    ----     3-E    ----                     EXPR_LIST
    5-E   300     1-T                       1                       INT_VAL
    6-E   300     1-T                       1                       INT_VAL
    7-E   300     1-T                       1                       INT_VAL
    8-E   300     1-T                       1                       INT_VAL

***SYMBOL NODES***

   id   var   type  attr  next   scope  variable-name
   1-S  550   ----    0   ----   ----              *                                               DEFAULT
   2-S  504   ----    0    3-S    1-B    ElementType                                             TYPE_NAME
   3-S  509    1-T    0   ----    1-B        tmpfunc n_in=    0 n_out=    0 n_io=    0
	                               in_l=   ---- out_l=   ---- sy_l=   ---- p_hdr=    2-B     FUNCTION_NAME
   4-S  503    1-T    0   ----    2-B         tester local=   0 nxt_in=  ---- nxt_out=  ----     VARIABLE_NAME

***TYPE NODES***

   id   var name length
   1-T  551   --   --                                                                T_INT
   2-T  552   --   --                                                              T_FLOAT
   3-T  553   --   --                                                             T_DOUBLE
   4-T  554   --   --                                                               T_CHAR
   5-T  556   --   --                                                             T_STRING
   6-T  555   --   --                                                               T_BOOL
   7-T  564   --   --                                                            T_COMPLEX
   8-T  550   --   --                                                              DEFAULT
   9-T  565   --   --                                                               T_VOID
  10-T  563   --   --                                                            T_UNKNOWN
  11-T  573   --   --  der_typ=      2-S                                     T_DERIVED_TYPE

  bits set:   0 syn/protected,  1 shared/public,  2 private,  3 future,  4 virtual,
                5 inline,  6 unsigned,  7 signed,  8 short,  9 long,  10 volatile,
                11 const,  12 typedef,  13 extern,  14 friend,  15 static,  16 register,
                17 auto,  18 global,  19 Sync,  20 atomic,  21 __private, 22 restrict

***LABEL NODES***


***COMMENT NODES***


***FILENAME NODES***

    1-F tmp.C