This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: C/C++ codes searching/browsing


Hello Everyone,

A few weeks ago I noticed a message on this list asking about a tool to do
C++ code searching/browsing.  While there were a few third-party tools
listed in a response to this message, there doesn't seem to be a general
purpose solution.

I am working on an addition to GCC's C++ front-end that adds a compiler
flag (-fxml) which triggers special processing at the end of a translation
unit.  Eventually, this processor will walk the entire translation unit,
starting at the global namespace (or perhaps a specified scope name?),
and write out an XML file describing the contents of each namespace/class
scope.  These files could then be read by any tool for any purpose,
freeing these tools of the nasty task of parsing C++.  Source
file/line number element attributes could be added to allow these tools
to go back to the source and extract comments corresponding to each
definition.

I would like to know if this is something that could be added to GCC
permanently, and thus be included in the distribution and maintained
as GCC is updated.  Obviously we would have to agree on a DTD for the
XML output.  The example below demonstrates an approach I've been thinking
about.  It uses a minimal amount of XML's constructs, but something more
advanced could be designed if it is appropriate.

I would appreciate any comments or suggestions on this, especially about
the format of the XML output.  Also, is anyone else working on such a
project, even if using a different output format?

Thanks,
-Brad

Here is an example with some C++ code, and XML output of its structure.
I just made up this example off the top of my head while writing this
message, so this is not intended as an example of real use.  The
format of the XML is similar to that produced by a preliminary
implementation I've added to my own copy of GCC's C++ front end.


Example C++ code:

namespace N
{
  class X
  {
  public:
    X(const int& x): m_Int(x) {}
    void Print(ostream& os) { os << m_Int; }
    int Get(void) const { return m_Int; }
    operator int() { return m_Int; }
    X& operator += (const X& r) { m_Int += r.m_Int; return *this; }
    bool operator < (const X& r) const { return (m_Int < r.m_Int); }
    static const char* GetClassName(void) { return "X"; }
  private:
    int m_Int;
  };

  X* Make_X(int x) { return new X(x); }
}


Resulting XML output (minus headers):

<Namespace name="N" id_name="_1N">
  <Class name="C" id_name="_1C" sourcefile="X.h" sourceline="3">
    <Constructor name="X" argc="1" access="public">
      <Argument type="int&" cv="const" name="x"></Argument>
    </Constructor>
    <Method name="Print" argc="1" access="public" static="0" cv="">
      <Argument type="ostream&" cv="" name="os"></Argument>
      <Returns type="void" cv=""></Returns>
    </Method>
    <Method name="Get" argc="0" access="public" static="0" cv="const">
      <Returns type="int" cv=""></Returns>
    </Method>
    <Converter name="operator int" access="public">
      <Returns type="int" cv=""></Returns>
    </Converter>
    <Operator name="+=" argc="1" access="public" id_name="__apl" cv="">
      <Argument type="X&" cv="const" name="r"></Argument>
      <Returns type="X&" cv=""></Returns>
    </Operator>
    <Operator name="&#x003C;" argc="1" access="public" id_name="__lt"
              cv="const">
      <Argument type="X&" cv="const" name="r"></Argument>
      <Returns type="bool" cv=""></Returns>
    </Operator>
    <Method name="GetClassName" argc="0" access="public" static="1" cv="">
      <Returns type="char*" cv="const"></Returns>
    </Method>
    <DataMember name="m_Int" type="int" access="private" static="0" cv="">
    </DataMember>
  </Class>
  <Function name="Make_X" argc="1" static="0">
    <Argument type="int" cv="" name="x"></Argument>
    <Returns type="X*" cv=""></Returns>
  </Function>
</Namespace>


If you care why I'm interested in doing this:

FYI, my motivation for creating this tool is that I'm writing a new tool
to generate wrappers for C++ programs in interpreted languages.  SWIG is
good for C, but lacks support for much of C++.  This tool will be used to
wrap classes in an open project I'm involved with for the National
Laboratory of Medicine.  Many of the classes are templated, so preselected
specializations will be wrapped.  I experimented with several parsers
to deal with the templated C++ code, but soon realized that GCC's
front-end did all the parsing and specialization work already.  This
XML-output addition to the front-end seems to be a solution that does what
I need, and can be used by others as well.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]