49
Program Slicing for Refactoring Advanced SW Tools Seminar Jan 2005 Yossi Peery

Program Slicing for Refactoring

  • Upload
    werner

  • View
    54

  • Download
    0

Embed Size (px)

DESCRIPTION

Program Slicing for Refactoring. Advanced SW Tools Seminar. Jan 2005Yossi Peery. Agenda. Slicing Overview Slicing Algorithms Slicing with Inference Rules Refactoring Overview Slice Extraction Refactoring Example NATE – Slicing Based Refactoring Tool. Starter. - PowerPoint PPT Presentation

Citation preview

Page 1: Program Slicing for Refactoring

Program Slicing for Refactoring

Advanced SW Tools Seminar

Jan 2005 Yossi Peery

Page 2: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Agenda Slicing Overview Slicing Algorithms Slicing with Inference Rules Refactoring Overview Slice Extraction Refactoring Example NATE – Slicing Based Refactoring Tool

Page 3: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Starter

Page 4: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Program 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

Page 5: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

What 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)

Page 6: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slice Example

A slice for the criterion (10 , {product})

Page 7: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

What is it good for? Debugging Program Comprehension Reverse Engineering Program Testing Measuring Program Metrics

Coverage, Overlap, Clustering Refactoring

Page 8: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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

Page 9: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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})

Page 10: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing Properties Amorphous & Semantic Slicing

• Allows any semantic preserving transformations

• Used for program comprehension and reverse engineering

Instead of: We write if (n >= 0) if (n < 0)

; sign := -1else

sign := -1

Page 11: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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

Page 12: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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

Page 13: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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

Page 14: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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.

Page 15: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing Algorithms

Page 16: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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 didn’t find new relevant statements

Page 17: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing AlgorithmsIteration 0:

Iteration k+1:

Page 18: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing Algorithm

Page 19: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing Algorithms Issues with algorithm

• Output statements are not included in sliceSolution: 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

Page 20: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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

Page 21: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing Algorithms

Page 22: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing Algorithm Slicing with PDG

• Slicing criterion is less general:C = ( p, Def(p) ∩ 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 isn’t interprocedural

Page 23: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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”

Page 24: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing Algorithm

Page 25: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Page 26: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing Algorithms Context Sensitivity

• Can not be solved by data flow equations• The <add> procedure, included through the

<multiply> procedure will include the call site <add(sum,i)> and consequently <sum:=0>

• 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):

1.Follow all edges except “parameter-out”

2.Follow all edges except “parameter-in”

Page 27: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Page 28: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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

Page 29: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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 <S,Γ,R>

S – Statement or sequence of statements that have been analyized

Γ – Current slicing contextR – Set of statements that are relevant (so far)• Similar in nature to data flow equations method

Page 30: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing with Inference Rules Context:

Inference Rule:

Initial Configuration:

• Our Example

Final Configuration:

Page 31: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing with Inference Rules Rule Example

Inference rules are defined so that, at each step, there is at most one rule that matches the configuration

Page 32: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slicing 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

Page 33: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Refactoring 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

Page 34: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Refactoring 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

Page 35: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Refactoring Overview

Page 36: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slice Extraction Refactoring

Page 37: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slice Extraction Refactoring

Page 38: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slice 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

Page 39: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Slice 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

Page 40: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Example – original code

Page 41: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Example – Extract as Method

Page 42: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Example – Extract as Object

Page 43: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Example – Extract as Aspect

Page 44: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Example – Extract as Aspect

Page 45: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

NATE – Slicing based Refactoring Tool Oxford University – Programming Tools

Grouphttp://web.comlab.ox.ac.uk/oucl/research/areas/progtools/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

Page 46: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

NATE – 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

Page 47: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

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

NATE – Slicing based Refactoring Tool

Page 48: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Page 49: Program Slicing for Refactoring

Jan-05Yossi Peery Advanced SW Tools Seminar

Refernces “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)