Title: Program Slicing for Refactoring
1Program Slicing for Refactoring
- Advanced SW Tools Seminar
Jan 2005 Yossi Peery
2Agenda
- Slicing Overview
- Slicing Algorithms
- Slicing with Inference Rules
- Refactoring Overview
- Slice Extraction Refactoring
- Example
- NATE Slicing Based Refactoring Tool
3Starter
4Program Slicing History
- Mark Weiser, 1981
- Experimented with programmers to show that slices
are - The mental abstraction people make when they are
debugging a program Weiser - Used Data Flow Equations
- Ottenstein Ottenstein PDG, 1984
- Horowitz, Reps Binkly SDG, 1990
5What is a Slice?
- All the statements of a program that may affect
the values of some variables in a set V at some
point of interest p. - Slicing Criterion
- C (p , V)
-
6Slice Example
- A slice for the criterion (10 , product)
7What is it good for?
- Debugging
- Program Comprehension
- Reverse Engineering
- Program Testing
- Measuring Program Metrics
- Coverage, Overlap, Clustering
- Refactoring
8Slicing Properties
- Static Slicing
- Statically available information only
- No assumptions made on input
- Computed slice can never be accurate (minimal
slice) - Problem is undecidable reduction to the halting
problem - Current static methods can only compute
approximations - Result may not be usefull
9Slicing Properties
- Dynamic Slicing
- Computed on a given input
- actual instead of might
- Useful for applications that provide are input
driven (debugging, testing) - Criterion (n-3, 5, sign)
10Slicing Properties
- Amorphous Semantic Slicing
- Allows any semantic preserving transformations
- Used for program comprehension and reverse
engineering - Instead of We write
- if (n gt 0) if (n lt 0)
- sign -1
- else
- sign -1
11Slicing Properties
- Backward Slicing
- Original Slicing Method
- Backward Traversal of Program Flow
- Slicing starts from point p (C (p , V))
- Examines statements that are executed before p
(in run-time) - Keep statements that affect value of V at p, or
execution of p. - Not only statements that appear before p
12Slicing Properties
- Forward Slicing
- Forward Traversal of Program Flow
- Slicing starts from p (C (p , V))
- Examine all statements that are executed after p
- Keep statements that are affected by the values
of V at p or by the execution of p - Shows downstream code that depend on a specific
variable or statement - Can show the code affected by a modification to a
single statement
13Slicing Properties
- Intraprocedural Slicing
- Computes slice within one procedure
- Assumes worse case for function calls
- Interprocedural Slicing
- Compute slice over an entire program
- Two ways for crossing procedure boundary
- Up going from sliced procedure into calling
procedure - Down going from sliced procedure into called
procedure - Must Be Context Sensitive
14Slicing Algorithm
- CFG Control Flow Graph
- Each program statement is a node
- A directed edge will connect between any 2 nodes
that represent statements with a possible control
flow between them. - Special nodes Start, Stop
- Definitions
- - There is a directed path from I
to j - - Set of nodes that are
influenced by i - - all of the variables that are
defined (modified) at statement i. - - all of the variables that are
referenced (used) at statement i.
15Slicing Algorithms
16Slicing Algorithms
- Data Flow Equations (Weiser)
- Iterative Process (Over CFG)
- Compute consecutive sets of relevant variables
for each node in the CFG using data dependencies - Control dependences are not computed explicitly
- Variables of control predicates (if, while) are
indirectly relevant if any one of the
statements in their body is relevant - Start with slicing criterion C (p, V)
- Continue until a fixed point is reached last
iteration didnt find new relevant statements
17Slicing Algorithms
18Slicing Algorithm
19Slicing Algorithms
- Issues with algorithm
- Output statements are not included in slice
- Solution print(x) out out x , out
V - Interprocedural Slicing
- Solution proposed by Weiser
- Can go up or down procedure calls
- Actual parameters of function call are changed to
call parameters (or the opposite) - Variables not in scope are removed
- Is not context sensitive too inaccurate
20Slicing Algorithms
- PDG Program Dependance Graphs
- Each node represents a statement (like CFG)
- Directed Edges represent
- Control Dependence (Bold Lines) between a
predicate and the statements it controls - Data Dependence (Regular Lines) between
statements modifying a variable and those that
may reference it - Special Entry node is connected to all nodes
that are not control dependant
21Slicing Algorithms
22Slicing Algorithm
- Slicing with PDG
- Slicing criterion is less general
- C ( p, Def(p) n Ref(p) )
- Graph is computed for a single procedure
- Slicing becomes a reachability problem
- A slice consits of all the nodes that have a
directed path to the node in the slicing
criterion are in the - Linear in time, after graph is calculated
- Issues
- Method isnt interprocedural
23Slicing Algorithms
- SDG System Dependence Graph
- New nodes
- Call Site, Procedure Entry, Actual-in-argument,
Actual-out-argument, Formal-in-parameter,
Formal-out-parameter - New edges
- Call Edge connect call site and procedure
entry - Parameter-In Edge connect Actual-in with
Formal-in - Parameter-Out-Edge connect Actual-out with
Formal-out
24Slicing Algorithm
25(No Transcript)
26Slicing Algorithms
- Context Sensitivity
- Can not be solved by data flow equations
- The ltaddgt procedure, included through the
ltmultiplygt procedure will include the call site
ltadd(sum,i)gt and consequently ltsum0gt
- Solved by SDG
- New summary edges (dotted) represent transitive
dependences between actual-in and actual-out
nodes. - Slice is calculated in 2 passes (instead of 1)
- Follow all edges except parameter-out
- Follow all edges except parameter-in
27(No Transcript)
28Slicing Algorithm
- SDG - Issues
- Slicing remains a reachability problem
- SDG of a program is complex and costly to build
(time, space) - After computation, many different slices can be
found using the same graph - Is not efficient for developing code
- OO concepts and unstructured control flow (jump
statements, exceptions) further complicate the
graph
29Slicing with Inference Rules
- Concept
- Use inference rules when traversing backwards the
flow of the program to determine relevant
statements - Rules are applied on a specific configuration of
ltS,G,Rgt - S Statement or sequence of statements that have
been analyized - G Current slicing context
- R Set of statements that are relevant (so far)
- Similar in nature to data flow equations method
30Slicing with Inference Rules
- Context
- Inference Rule
- Initial Configuration
- Our Example
- Final Configuration
31Slicing with Inference Rules
- Rule Example
- Inference rules are defined so that, at each
step, there is at most one rule that matches the
configuration
32Slicing with Inference Rules
- Features
- Supports interprocedural slicing
- Context Sensitive
- Can be extended to support other language
features such as - Complex expressions
- Array access
- Variable declarations
- Structured Jumps (break, continue)
- Object-oriented slicing (scoping, polymorphism)
- Aliasing
33Refactoring Overview
- Gradually improving design of existing code
- Source code transformations that,
- Preserve behavior of original system
- Manually or Automated
- Introduced by William Opdyke, 1992
- Formally defined the reasonable behavior
preservation degree expected from a refactoring
tool - Formally Disciplined by Martin Fowler 2000
- Formal description of a refactoring
- Catalog of refactoring techniques
34Refactoring Overview
- Over 70 refactoring techniques can be found at
- www.refactoring.com/catalog/index.html
- Refactoring categories
- Composing Methods, Moving features between
Objects, Organizing Data, Making Method Calls
Simpler - Some refactorings
- Rename Method, Extract Method, Move Method,
Replace Conditional with Polymorphism
35Refactoring Overview
36Slice Extraction Refactoring
37Slice Extraction Refactoring
38Slice Extraction Refactoring
- Idea introduced by K. Maruyama, 2001
- Is not limited to consecutive statements (like
extract method) - Allows the untangling of a single concern from a
complex method - Extracted slice can be refactored to
- New Method
- New Object
- New Aspect
39Slice Extraction Refactoring
- Slice Extraction Refactoring Concerns
- Not all of the statements in the slice can be
deleted - Deleted statements are determined by re-slicing
for variables in statements that were not sliced - Preconditions Limitations
- Clean compilation
- Return statement
- Global-scoped variables
- Input/Output statements
40Example original code
41Example Extract as Method
42Example Extract as Object
43Example Extract as Aspect
44Example Extract as Aspect
45NATE Slicing based Refactoring Tool
- Oxford University Programming Tools Group
- http//web.comlab.ox.ac.uk/oucl/research/areas/pr
ogtools/projects/nate/nate.html - Slicing based refactoring techniques for the Java
programming language - Currently supports a small subset of Java (March
2004) - Extract Slice as Method Refactoring
- An Eclipse Plug-in
46NATE Slicing based Refactoring Tool
- How is it used?
- Programmer selects
- slicing criterion
- Name for new extracted method
- Tool performs
- Compute Slice
- Check refactoring preconditions
- If extraction is possible perform
transformation - Show original and transformed code in a preview
dialog to the user for confirmation - User can cancel any operation
47NATE Slicing based Refactoring Tool
- Implementation
- Uses JDT plug-in for access to the AST
- Slicing is done with inference rules
- The AST node of a statement is associated with a
related inference rule - Intensive use of visitor pattern to visit the AST
and its related rules - Each AST node can be marked as relevant or not
48(No Transcript)
49Refernces
- Untangling A Slice Extraction Refactoring -
- Ran Ettinger and Mathieu Verbaere (March 2004)
- Program Slicing for Refactoring -
- Mathieu Verbaere (September 2003)
- Automated Tools for Refactoring -
- Ran Ettinger (June 2003)
- Program Slicing -
- Mark Weiser (1981)