[cfe-dev] Modifying class definitions in Header files via Src-to-Src translation

Prakash Prabhu prakash.prabhu at gmail.com
Sat Dec 17 14:49:32 PST 2011


Hi,

I would like to modify class definitions that are part of a header file and
generate another file (preferably overwrite the original header file).
Essentially, I want to add a few fields to an existing class and change
access specifiers of few other fields. For instance, transform something
like:

///  Shape.h
class Shape {
  public:
     int x;
     int y;
  private:
     float A;
     float B;
     float C;
};


to

/// Shape.h
class Shape {
   public:
      int x;
      int y;
      int z; // Newly-added field
   public:     // Changed access specifier of A and B
      float A;
      float B;
   private:
      float C;
};

The information on the exact class on which the transformation is to be
performed, and fields whose access specifiers have to be changed is
available after semantic analysis (within the Sema object).  I don't
particularly care about the formatting of the output header file, as long
as the transformations are done correctly.

What's the best way to achieve this within clang?

I took a look at RewriteObjC, and tried to write a ASTConsumer class based
on it, but I was not able to apply it directly to header files: the output
was a .gch file which is in binary and as it stands now, I am also not sure
how to override HandleTagDecl() to make the necessary changes to the class
in question. Is there a better/easier way than using either
ASTConsumer/SemaConsumer ?

thanks,
Prakash
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20111217/946e212e/attachment.html>


More information about the cfe-dev mailing list