srcML / srcSlice

Lightweight tool for slicing
34 stars 16 forks source link

Incorrect slices #31

Open rcourt-zp opened 3 years ago

rcourt-zp commented 3 years ago

Produces incorrect slices given the following source code (line numbers included for clarity):

     1  void bar(int a, int b) {
     2    if (a != 0) return;
     3    if (b == 42) return;
     4    printf("%d %d\n", a, b);
     5  }
     6  
     7  int foo(int x) {
     8    int v = 0;
     9    for (int i = 0; i < x; ++i) {
    10      v++;
    11    }
    12    bar(x, v);
    13    return 0;
    14  }
    15  
    16  int main() {
    17    int n = 3;
    18    return foo(n);
    19  }

Produces the following srcSlice output:

     1  example.cpp,main,n,def{17},use{18},dvars{},pointers{},cfuncs{}
     2  example.cpp,bar,b,def{1},use{3,4},dvars{},pointers{},cfuncs{printf{3}}
     3  example.cpp,bar,a,def{1},use{2,4},dvars{},pointers{},cfuncs{printf{2}}
     4  example.cpp,foo,i,def{9},use{9},dvars{},pointers{},cfuncs{}
     5  example.cpp,foo,v,def{8},use{1,2,4,10,12},dvars{},pointers{},cfuncs{printf{2},bar{2}}
     6  example.cpp,foo,x,def{7},use{9,12},dvars{},pointers{},cfuncs{bar{1}}

In the first line of the srcSlice output, n is correctly defined and used. However, I also expected n to appear in cfuncs{foo(1)}. Additionally, variable v is incorrectly stated to be used on lines 2 & 4 which appears to actually be where variable x should be used.

Is this the expected output? If not, is this resolved when building most recent version from source?

To be clear, I downloaded prebuilt srcML from https://www.srcml.org/#download and prebuilt srcSlice from https://www.srcml.org/tools.html

To produce the above, I used the following commands:

$ srcml example.cpp --position -o example.xml
$ srcslice example.xml > example.slice
$ cat -n example.slice
rcourt-zp commented 3 years ago

Okay so, after building srcSlice from master, the slice might still be slightly incorrect on the above example:

==========================================================================
Name and type: n 
Contains Declaration: 1 Containing class: 
Dvars: {}
Aliases: {}
Cfunctions: {printf 2,bar 1,foo 1,}
Use: {4,12,}
Def: {1,7,17,}
Control Edges: {(0, 17),}
==========================================================================
==========================================================================
Name and type: v 
Contains Declaration: 1 Containing class: 
Dvars: {}
Aliases: {}
Cfunctions: {printf 3,bar 2,}
Use: {4,10,12,}
Def: {1,8,}
Control Edges: {(8, 10),(8, 12),(10, 12),}
==========================================================================
==========================================================================
Name and type: x 
Contains Declaration: 1 Containing class: 
Dvars: {}
Aliases: {}
Cfunctions: {printf 2,bar 1,}
Use: {4,12,}
Def: {1,7,}
Control Edges: {(7, 12),}
==========================================================================
==========================================================================
Name and type: b 
Contains Declaration: 1 Containing class: 
Dvars: {}
Aliases: {}
Cfunctions: {printf 3,}
Use: {4,}
Def: {1,}
Control Edges: {(1, 4),}
==========================================================================
==========================================================================
Name and type: a 
Contains Declaration: 1 Containing class: 
Dvars: {}
Aliases: {}
Cfunctions: {printf 2,}
Use: {4,}
Def: {1,}
Control Edges: {(1, 4),}
==========================================================================

I expected variable n to be used on lines 2, 4, 9, 12, 18. I expected variable v to be used on lines 3, 4, 10, 12. I expected variable x to be used on lines 2, 4, 9, 12. I expected variable b to be used on lines 3, 4. I expected variable a to be used on lines 2, 4.

These are still better than the results from my original post, but what I got was still slightly different than what I expected. Please let me know if I'm interpreting this incorrectly.

Thanks

cnewman commented 3 years ago

Yes, there have been several updates to srcML which may affect the quality of srcSlice output, beside the known bugs that already existed. The tool needs maintenance and I have very little time to give it the attention it needs.

I will be working on it some in the last couple weeks before the semester begins again. We will see how much I am able to complete.

rcourt-zp commented 3 years ago

Understandable, thanks for the reply.

If there are any specific issues or features of srcSlice that you can elaborate on, I would be happy to take a look and submit pull requests where I can.