views:

148

answers:

5

Is there a tool in either Linux/Windows that would enable us to determine if a logic of the particular function in C is same as that of a particular function in C++ ?

A: 

I think there is such a tool, called an assembly listing.

Paul Richter
+11  A: 

In general, the equivalence of Turing machines is undecidable, so no.

FredOverflow
That the problem is impossible in general does not prove that a useful tool (i.e. one that can get the easy cases) is not possible...
dmckee
In fact, dmckee points out that matching ASTs for code gives an approximation of the answer, at least for copy-and-pasted code.
Ira Baxter
+1  A: 

If you are just talking of control structures, if/else, blocks of code, swtich/case, while, for, etc AND if you are willing to be able to accept "gettign a good feel for it", rather than 100% accuray, then a picture may be work a thousand words, and you might look at a code to flowchart program.

I won't recommend any, as I don't know them well enough (but have always wanted to try them out, espcially if round trip. It might not be easy to find something free. In general, you will see something like this ... alt text

is that what you have is mind? Do it for both C and C++ versions, and you can get a rough feel for similarity of logic.

Perhaps you can tell us a little more what exactly you are looking for? Help us to help you? Thanks.

LeonixSolutions
A more sophisticated approach than "looking at flowcharts" is constructing Program Dependence Graphs (with/without static single assignment) and attempting a Graph Match. This paper used this idea to find code clones: Jens Krinke: Identifying Similar Code with Program Dependence Graphs. WCRE 2001: 301-309 The good news is that it works to some degree; the bad news is that it doesn't scale to really big systems. More scalable approaches have used matching of lexical tokens or AST matching.
Ira Baxter
A: 

Look at the assembly code generated, thanks to your favorite debugger.

tojas
How does this help more than just reading the codes in the first place? Excluding trivial cases.
kaizer.se
You never know what a compiler can do, on your behalf. Machine Code never lies.
tojas
+1  A: 

You can imagine a tool that compares the structure of ASTs after the compiler has done the initial conversion to abstract representation or after one of more optimization passes.

This would probably

  1. Miss some real matches (i.e. generate false negatives)
  2. Identify some bogus matches (i.e. generate false positives)

With tuning you could force the second case to be more common. I have no feel for how good it would have to be to be useful as a front end to a vgrep process.

But it get worse, because you've asking for a cross-language implementation, and that will make it harder. Still, gcc uses the same abstract representation for everything, so it is not beyond imagining.

That said, I know of no such tool.

dmckee
There is a tool that does *exactly* this. It is called the CloneDR (http://www.semanticdesigns.com/Products/Clone) This compares the ASTs of source code to verify "similarity". CloneDR handles many langauges including C and C++ You pretty much don't need a cross-langauge implementation; C is generally close enough to be swallowed by a C++ parser, and its easier to make slight changes to the C code rather than invent such a cross language tool.
Ira Baxter
@Ira: Thanks...and wow! The website makes some pretty strong claims. Does anyone know how well it works in real life?
dmckee
@dmckee: I'm the author. I think it works pretty well. You'll have to form you own opinion. You can download a evaluation version to do so.
Ira Baxter
@dmckee: You appear to be a physicist. I can put you in contact with FermiLab people that have looked at CloneDR for application on their C++ modelling codes. My email address can be found at my SO icon.
Ira Baxter