user3696279
user3696279

Reputation: 109

converting c++ code to xml files for code analysis

I would like a simple windows tool for converting c++\c header and source code to xml for analysis.

For instance,

#include "something.h"
void CSomething::CheckIt(DWORD x)
{
    if (x > 1)
    { 
       // Do something
    }
}

Will be converted to

<XML>
<CodeFile>
<IncludeCommand filename="something.h"/>

<Function namespace="CSomething" name="CheckIt" returnType="void"/>
<Arguments>
<Argument name="x" type="DWORD" />
</Arguments>
<Body>
<IfCondition>
<Expression ... />
<Body>
...
</Body>
</IfCondition>
</Body>
</Function>

</CodeFile>

Commercial products are also ok, but open source (simple) solutions are best.

Upvotes: 2

Views: 2080

Answers (2)

Tom Wilson
Tom Wilson

Reputation: 698

Take a look at gcc_xml and then proceed to its successor CastXML

Upvotes: 1

Ira Baxter
Ira Baxter

Reputation: 95334

The words "simple", "C++" and "tool" don't belong in the same sentence.

If you want to analyze C++ code, you presumably want accurate answers, and that requires accurate parsers.

Our DMS Software Reengineering Toolkit with its C++14 front end can do this. DMS runs under windows, and its C++ front end can handle ANSI C++14, GCC/Clang or Visual Studio dialects of C++.

You can see example ASTs produced by DMS at get human readable AST from c++ code DMS has an option to export such ASTs as XML, which would satisfy OP's request as explicitly stated.

He probably doesn't really want this. For any serious-size chunk of source code, such XML files are huge. A thousand line C++ program will produce approximately 10,000 lines/500K characters of XML output. This is clumsy to produce/read/process. If you include typical header files, you can easily reach 100K lines of C++ code; if you leave them out, you can't analyze the code very well. DMS itself provide lots of machinery to navigate the ASTs it generates; it will be a lot easier to write an analyzer using the machinery provided by DMS than to re-invent all of that to work with XML.

As a practical matter, to do any serious analysis of C++ you need what amounts to symbol table information, and you will probably want control and data flow analysis information. DMS can provide these, too. See Life After Parsing.

Upvotes: 3

Related Questions