[cfe-dev] Way to compare two Function bodies.

Pavan Pattanada pattanad at uci.edu
Fri Jul 19 11:54:55 PDT 2013


Hi,
Could you please tell me what the idea behind the *canonical* form in
general is?
Basically when I profile the statement with the canonical boolean set, It
is internally calling

if(Canonical)
   T = Context.getCanonicalType(T)

and while profilling,
ID.ADDPointer(T.getAsOpaquePtr())
is being done, which gives out different results for both the versions of
the same function.

Also, in VisitDecl(), the profiler is adding :
ID.AddPointer(D->getCanonicalDecl());
which is again different for each version of the same function.

Thanks,
Pavan



On Wed, Jul 17, 2013 at 5:54 PM, Pavan Pattanada <pattanad at uci.edu> wrote:

> Thats pretty cool :) I will check what could be done. Thanks a lot!!
>
> ~Pavan
>
>
> On Wed, Jul 17, 2013 at 5:44 PM, Richard Smith <richard at metafoo.co.uk>wrote:
>
>> On Wed, Jul 17, 2013 at 5:20 PM, Pavan Pattanada <pattanad at uci.edu>wrote:
>>
>>> Yes, What you have mentioned is exactly what I want. So does it mean
>>> that Stmt::Profile would generate unique ID (llvm::FoldingSetNodeID)
>>> irrespective of it being a compound statement or not? If so, could I use it
>>> to compare the two functions using the IDs that are generated using
>>> FunctionDecl::getBody()::profile() ?
>>>
>>
>> Yes, you can Stmt::Profile to compare two function bodies. But as noted
>> below, it won't quite do what you want, because it doesn't consider
>> variables declared within the statement to be "the same". You could fix
>> this by teaching StmtProfiler to map declarations to some kind of
>> declaration index if they are declared within the statement being profiled.
>> You'd need similar treatment for LabelDecls. I don't see a good reason why
>> we wouldn't accept such a patch for upstream Clang.
>>
>>
>>> Thanks,
>>> Pavan
>>>
>>>
>>> On Wed, Jul 17, 2013 at 5:01 PM, Richard Smith <richard at metafoo.co.uk>wrote:
>>>
>>>> On Wed, Jul 17, 2013 at 4:18 PM, Pavan Pattanada <pattanad at uci.edu>wrote:
>>>>
>>>>> By "same", I mean, their ASTs to be the same to start with. I am
>>>>> working on a tool,  which is used to syntactic/semantic similarity between
>>>>> two versions of the code base. So far, I have been able to get the
>>>>> FunctionDecls of both versions of a function into memory. But I am not sure
>>>>> how to compare them.
>>>>>
>>>>
>>>> This is still a little imprecise. Should functions compare equal if
>>>> variables have been renamed between them? What if an expression has been
>>>> rewritten into a trivially equivalent form (for instance, parentheses were
>>>> added or removed, or p->x was changed to (*p).x)?
>>>>
>>>> Stmt::Profile does nearly what you want (it's the mechanism we use to
>>>> determine if two dependent expressions are equivalent for the purposes of
>>>> template redeclaration matching), but it will treat
>>>>
>>>>   void f() { int n; }
>>>>   void g() { int n; }
>>>>
>>>> as having different bodies, because they do not declare the same
>>>> variable. That's probably not very hard to fix.
>>>>
>>>>
>>>>> ~Pavan
>>>>>
>>>>>
>>>>> On Wed, Jul 17, 2013 at 3:42 PM, Richard Smith <richard at metafoo.co.uk>wrote:
>>>>>
>>>>>> On Wed, Jul 17, 2013 at 10:10 AM, Pavan Pattanada <pattanad at uci.edu>wrote:
>>>>>>
>>>>>>> Hi,
>>>>>>> I need to compare two versions of the same clang::FunctionDecl
>>>>>>> function body(a function in C). I would need to compare them statement by
>>>>>>> statement and say if the two versions are the same. Is there a neat way to
>>>>>>> do this in the clang framework?
>>>>>>>
>>>>>>
>>>>>> This question is too vague to answer: what do you mean by "the same"?
>>>>>> Same source text, same tokens, same tokens other than variable naming,
>>>>>> something else?
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130719/7e284be8/attachment.html>


More information about the cfe-dev mailing list