srcML / srcSlice

Lightweight tool for slicing
34 stars 16 forks source link

srcSlice fails with basic example #35

Closed rudiejd closed 2 months ago

rudiejd commented 2 years ago

I tried to use srcSlice against a basic example provided from the srcML website (https://www.srcml.org/tutorials/creating-srcml.html. The srcML XML output is as follows:

<s:unit xmlns="http://www.srcML.org/srcML/src" xmlns:cpp="http://www.srcML.org/srcML/cpp" revision="1.0.0" language="C++" filename="src/rotate.cpp">
<cpp:include>#<cpp:directive>include</cpp:directive> <cpp:file>"rotate.h"</cpp:file></cpp:include>

<s:comment type="line">// rotate three values</s:comment>
<s:function><s:type><s:name>void</s:name></s:type> <s:name>rotate</s:name><s:parameter_list>(<s:parameter><s:decl><s:type><s:name>int</s:name><s:modifier>&amp;</s:modifier></s:type> <s:name>n1</s:name></s:decl></s:parameter>, <s:parameter><s:decl><s:type><s:name>int</s:name><s:modifier>&amp;</s:modifier></s:type> <s:name>n2</s:name></s:decl></s:parameter>, <s:parameter><s:decl><s:type><s:name>int</s:name><s:modifier>&amp;</s:modifier></s:type> <s:name>n3</s:name></s:decl></s:parameter>)</s:parameter_list>
<s:block>{<s:block_content>
  <s:comment type="line">// copy original values</s:comment>
  <s:decl_stmt><s:decl><s:type><s:name>int</s:name></s:type> <s:name>tn1</s:name> <s:init>= <s:expr><s:name>n1</s:name></s:expr></s:init></s:decl>, <s:decl><s:type ref="prev"/><s:name>tn2</s:name> <s:init>= <s:expr><s:name>n2</s:name></s:expr></s:init></s:decl>, <s:decl><s:type ref="prev"/><s:name>tn3</s:name> <s:init>= <s:expr><s:name>n3</s:name></s:expr></s:init></s:decl>;</s:decl_stmt>

  <s:comment type="line">// move</s:comment>
  <s:expr_stmt><s:expr><s:name>n1</s:name> <s:operator>=</s:operator> <s:name>tn3</s:name></s:expr>;</s:expr_stmt>
  <s:expr_stmt><s:expr><s:name>n2</s:name> <s:operator>=</s:operator> <s:name>tn1</s:name></s:expr>;</s:expr_stmt>
  <s:expr_stmt><s:expr><s:name>n3</s:name> <s:operator>=</s:operator> <s:name>tn2</s:name></s:expr>;</s:expr_stmt>
</s:block_content>}</s:block></s:function>
</s:unit>

This is for the program:

#include "rotate.h"

// rotate three values
void rotate(int& n1, int& n2, int& n3)
{
  // copy original values
  int tn1 = n1, tn2 = n2, tn3 = n3;

  // move
  n1 = tn3;
  n2 = tn1;
  n3 = tn2;
}

This gives the following output

==========================================================================
Name and type: tn3 
Contains Declaration: 1 Containing class: 
Dvars: {}
Aliases: {n1,}
Cfunctions: {}
Use: {0,}
Def: {0,}
Control Edges: {}
==========================================================================
==========================================================================
Name and type: tn2 
Contains Declaration: 1 Containing class: 
Dvars: {}
Aliases: {n3,}
Cfunctions: {}
Use: {0,}
Def: {0,}
Control Edges: {}
==========================================================================
==========================================================================
Name and type: tn1 
Contains Declaration: 1 Containing class: 
Dvars: {}
Aliases: {n2,}
Cfunctions: {}
Use: {0,}
Def: {0,}
Control Edges: {}
==========================================================================
==========================================================================
Name and type: n3 
Contains Declaration: 1 Containing class: 
Dvars: {tn3,}
Aliases: {n3,}
Cfunctions: {}
Use: {0,}
Def: {0,}
Control Edges: {}
==========================================================================
==========================================================================
Name and type: n2 
Contains Declaration: 1 Containing class: 
Dvars: {tn2,}
Aliases: {n2,}
Cfunctions: {}
Use: {0,}
Def: {0,}
Control Edges: {}
==========================================================================
==========================================================================
Name and type: n1 
Contains Declaration: 1 Containing class: 
Dvars: {tn1,}
Aliases: {n1,}
Cfunctions: {}
Use: {0,}
Def: {0,}
Control Edges: {}
==========================================================================

It seems like all the uses and definitions are on line zero, whereas the source code does not reflect that. Shouldn't this example work? Is there a specific version of srcML that is compatible with srcSlice?

rudiejd commented 2 years ago

After redoing the srcML file with --position enabled, I got the right output;

==========================================================================
Name and type: tn3 
Contains Declaration: 1 Containing class: 
Dvars: {}
Aliases: {n1,}
Cfunctions: {}
Use: {10,}
Def: {7,}
Control Edges: {(7, 10),}
==========================================================================
==========================================================================
Name and type: tn2 
Contains Declaration: 1 Containing class: 
Dvars: {}
Aliases: {n3,}
Cfunctions: {}
Use: {12,}
Def: {7,}
Control Edges: {(7, 12),}
==========================================================================
==========================================================================
Name and type: tn1 
Contains Declaration: 1 Containing class: 
Dvars: {}
Aliases: {n2,}
Cfunctions: {}
Use: {11,}
Def: {7,}
Control Edges: {(7, 11),}
==========================================================================
==========================================================================
Name and type: n3 
Contains Declaration: 1 Containing class: 
Dvars: {tn3,}
Aliases: {n3,}
Cfunctions: {}
Use: {7,}
Def: {4,12,}
Control Edges: {(4, 7),(7, 12),}
==========================================================================
==========================================================================
Name and type: n2 
Contains Declaration: 1 Containing class: 
Dvars: {tn2,}
Aliases: {n2,}
Cfunctions: {}
Use: {7,}
Def: {4,11,}
Control Edges: {(4, 7),(7, 11),}
==========================================================================
==========================================================================
Name and type: n1 
Contains Declaration: 1 Containing class: 
Dvars: {tn1,}
Aliases: {n1,}
Cfunctions: {}
Use: {7,}
Def: {4,10,}
Control Edges: {(4, 7),(7, 10),}
==========================================================================

This is correct. There should probably be some documentation stating that only srcML outputs generated with position enabled will work correctly with srcSlice

wumingqiang commented 6 months ago

Thanks for your share.This problem has bothered me for a long time.