GumTreeDiff / gumtree

An awesome code differencing tool
https://github.com/GumTreeDiff/gumtree/wiki
GNU Lesser General Public License v3.0
933 stars 174 forks source link

C Struct Diff Not Recognized #278

Closed SherloqueChang closed 2 years ago

SherloqueChang commented 2 years ago

Hi! I'm using GumTree Java API for C code file differencing. However, I found that the change inside struct ( adding a field variable, for example ) cannot be recognized. Is this a defection of GumTree or I'm not using it correctly?

jrfaller commented 2 years ago

Hi!

Do you know which parser you are using? cgum?

SherloqueChang commented 2 years ago

Yes, I'm using cgum as parser.

jrfaller commented 2 years ago

can you try using the cgum command is wrong inside the AST?

SherloqueChang commented 2 years ago

Sorry I didn't get your point. Could you elaborate a bit more? Here is my Java code:

Tree src = TreeGenerators.getInstance().getTree(srcFile).getRoot();;
Tree dst = TreeGenerators.getInstance().getTree(dstFile).getRoot();
Matcher defaultMatcher = Matchers.getInstance().getMatcher();
MappingStore mappings = defaultMatcher.match(src, dst);
EditScriptGenerator editScriptGenerator = new ChawatheScriptGenerator();
EditScript actions = editScriptGenerator.computeActions(mappings); // computes the edit script

I guess by invoking TreeGenerators I can get the AST of the code file via cgum.

And I use TreeIoUtils.toXml() to get the xml format AST. For the struct defined in my example file, there is

<tree type="Declaration" pos="66" length="47">
      <tree type="DeclList" pos="66" length="47"></tree>
</tree>

and there isn't any information about the members of struct. So I guess this is due to cgum?

jrfaller commented 2 years ago

Yes I believe this is because of cgum. Maybe you could try with the brand new tree-sitter C parser or srcml ?

Cheers!

jrfaller commented 2 years ago

for instance for the code

// create struct with person1 variable
struct Person {
  char name[50];
  int citNo;
  float salary;
} person1;

I get

<?xml version="1.0" ?>
<tree type="translation_unit" pos="0" length="111">
    <tree type="comment" pos="0" length="38" label="// create struct with person1 variable"/>
    <tree type="declaration" pos="39" length="72">
        <tree type="struct_specifier" pos="39" length="63">
            <tree type="struct" pos="39" length="6" label="struct"/>
            <tree type="type_identifier" pos="46" length="6" label="Person"/>
            <tree type="field_declaration_list" pos="53" length="49">
                <tree type="{" pos="53" length="1" label="{"/>
                <tree type="field_declaration" pos="57" length="14">
                    <tree type="primitive_type" pos="57" length="4" label="char"/>
                    <tree type="array_declarator" pos="62" length="8">
                        <tree type="field_identifier" pos="62" length="4" label="name"/>
                        <tree type="[" pos="66" length="1" label="["/>
                        <tree type="number_literal" pos="67" length="2" label="50"/>
                        <tree type="]" pos="69" length="1" label="]"/>
                    </tree>
                    <tree type=";" pos="70" length="1" label=";"/>
                </tree>
                <tree type="field_declaration" pos="74" length="10">
                    <tree type="primitive_type" pos="74" length="3" label="int"/>
                    <tree type="field_identifier" pos="78" length="5" label="citNo"/>
                    <tree type=";" pos="83" length="1" label=";"/>
                </tree>
                <tree type="field_declaration" pos="87" length="13">
                    <tree type="primitive_type" pos="87" length="5" label="float"/>
                    <tree type="field_identifier" pos="93" length="6" label="salary"/>
                    <tree type=";" pos="99" length="1" label=";"/>
                </tree>
                <tree type="}" pos="101" length="1" label="}"/>
            </tree>
        </tree>
        <tree type="identifier" pos="103" length="7" label="person1"/>
        <tree type=";" pos="110" length="1" label=";"/>
    </tree>
</tree>

with tree-sitter C parser.

SherloqueChang commented 2 years ago

Thanks! I'll give it a try.

SherloqueChang commented 2 years ago

emmm... Another question here.

If I'd like to use tree-sitter C parser by invoking Java API, how should I code?

jrfaller commented 2 years ago

Look here : https://github.com/GumTreeDiff/gumtree/wiki/GumTree-API#using-a-specific-generator

The one you are interested in is : https://github.com/GumTreeDiff/gumtree/blob/main/gen.treesitter/src/main/java/com/github/gumtreediff/gen/treesitter/CTreeSitterTreeGenerator.java

Also, you need tree-sitter-parser installed

Cheers.

SherloqueChang commented 2 years ago

OK. Truly thanks!