1

I'm working on a research project where I aim to build an a tool that automatically detect the implementation of certain design patterns in C++ code.

C++ code being complex, I want to have a simplified view of the given C++ project source code, let's say XML.

My first question is : would this be a good approach ? Is there any tool that can help me achieve this ?

  • 1
    C++ simplified as XML? <if><condition>...</condition><then>...</then><else>...</else></if> OMG... – Thomas Weller Sep 19 '17 at 11:55
  • 2
    @ThomasWeller I think you didn't get it, it's rather something like this : <Class name="ClassName" Visibility="Public" IsStatic="false"> <Fields> ... </Fields> <Methods> <Method name="DoSomething" visibility="Public" returnType="void"> <Parameters> ... </Parameters> ... </Methods> ... </Class> – AymenDaoudi Sep 19 '17 at 13:14
2

You can try CppDepend and the code query langugae CQLinq to detect some design patterns. Here's an article that explains how to do it.

2

Your "simplified view" would need to apply the C/C++ preprocessor, apply includes, and parse - at the very least. That's most of the front-end part of a compiler. It's really not a good idea to try writing something like that yourself. It is a good idea - if you want to do static analysis of code in a compiled language like C++ - to put yourself on top of an existing compiler or compiler framework.

Your go-to compiler framework is likely LLVM, on top of which the clang++ compiler is built. It's quite well-instrumented, I'm told, although I have not yet gotten to use it myself.

GCC may also be a possibility, albeit somewhat less popular.

Finally, if you have the abstract syntax tree in whatever representation LLVM or some other framework keeps it, I believe you would not want to convert it to XML, but rather do something with it as it is; but of course I may be wrong.

1

Our C++ front end does C++ language-precise parsing, builds ASTs, and does full name and type resolution, and constructions control and data flow graphs of each function/method. [As a practical matter, you cannot reason effectively about source code from just the AST. You must have information about the meaning of names.]

The underlying machinery, the DMS Software Reengineering Toolkit, provide the capability to do source pattern matches using explicit patterns with C++ syntax, as well as many other capabilities useful in general code analysis.

Runs natively under Windows, and seamlessly under Wine/Linux using sh scripts.

If you insist, you can export the AST and other artifacts as XML. When you do that, you lose access to all the other useful machinery provided by DMS. It is our experience that you lose far more capability than you gain by XMLifying and then having to re-invent all the necessary analysis machinery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.