rose-compiler / rose

Developed at Lawrence Livermore National Laboratory (LLNL), ROSE is an open source compiler infrastructure to build source-to-source program transformation and analysis tools for large-scale C (C89 and C98), C++ (C++98 and C++11), UPC, Fortran (77/95/2003), OpenMP, Java, Python and PHP applications.
http://rosecompiler.org
Other
597 stars 132 forks source link

How to unparse type declarations? #237

Open david-c-wong opened 1 year ago

david-c-wong commented 1 year ago

I have written an outliner for a code region to a new source file with that code region put in a function. In order for this new source file to be compilable, I need to include all type declarations needed for that code region. I have successfully collected all the types referenced.

Are there some utilities I can use to unparse the type declaration taking care of the order of type declaration in the presences of various C/C++ language features like namespace, typedef, nested type declaration, etc?

Thanks a lot!

chunhualiao commented 1 year ago

Hi, we already have an outliner. It has some support of moving dependent declarations into the newly generated source file containing the outlined code region enclosed in a function. We have a development branch which has even better support for this feature but there is no timeline to merge it.

The relevant function is: SageInterface::appendStatementWithDependentDeclaration(func,glob_scope,func_orig,exclude_headers);

The example use in line 636 of https://github.com/rose-compiler/rose/blob/weekly/src/midend/programTransformation/astOutlining/Transform.cc

For more information about the outliner, please check: https://en.wikibooks.org/wiki/ROSE_Compiler_Framework/outliner

david-c-wong commented 1 year ago

Thanks for the suggestion. Since there is an Outliner already implemented by you all, I am thinking of using that for my work. I may need to customize it a bit to insert a few function calls before and after the outlined function. Is it easy to do?

I also tested a simple program (adapted from the array1.c) and encountered an issue. Below is the code:

typedef struct {
        int x;
} MyType;

void foo()
{
  int i,j=100, sum[100];
  MyType bar;
#pragma rose_outline
  for (i=0;i<100;i++)
  {
    sum[i] =i*2+j + bar.x;
  }

#pragma rose_outline
  for (i=0;i<100;i++)
  {
    sum[i] =i*2+j;
  }

}

Then I run the outliner using the following command /usr/rose/bin/outline -rose:outline:new_file -rose:outline:exclude_headers array1.c

and got some error due to type declaration of second outlined code:

void OUT__2__2158__(int *ip__,int *jp__,int (*sump__)[100],void *barp__);
/* REQUIRED CPP DIRECTIVES */
/* REQUIRED DEPENDENT DECLARATIONS */
typedef struct {}MyType;

struct 
{
  int x;
}
;
/* OUTLINED FUNCTION */

void OUT__2__2158__(int *ip__,int *jp__,int (*sump__)[100],void *barp__)
{
  int *i = (int *)ip__;
  int *j = (int *)jp__;
  int (*sum)[100] = (int (*)[100])sump__;
  MyType *bar = (MyType *)barp__;
  for ( *i = 0;  *i < 100; ( *i)++) {
    ( *sum)[ *i] =  *i * 2 +  *j + ( *bar) . x;
  }
}

Is this issue fixed already? I am running ROSE 0.11.125.0.

Thanks for the help!