290 likes | 379 Views
Systematic Editing: Generating Program Transformations from an Example. Na Meng Miryung Kim Kathryn S. McKinley The University of Texas at Austin. Motivating Scenario. Pat needs to update database transaction code to prevent SQL injection attacks. C old. A old. B old. A new.
E N D
Systematic Editing: Generating Program Transformations from an Example Na MengMiryungKim Kathryn S. McKinley The University of Texas at Austin
Motivating Scenario Pat needs to update database transaction code to prevent SQL injection attacks Cold Aold Bold Anew Cnew Bnew
Systematic Editing • Similar but not identical changes to multiple contexts • Manual, tedious, and error-prone • Refactoring engines automate pre-defined semantic preserving edits • Source transformation tools require describing edits in a formal language
Our Solution: Sydit Identifier & edit position abstraction Abstract edit script application Abstract edit script application Program differencing Context extraction Context Aold Cold Bold • DELETE: config = (ILaunchConfiguration)iter.next(); Cnew Anew • DELETE: v1 = (t1)v2.m1(); Bnew Cnew Sydit improves programmer productivity.
Outline • Program Transformation Generation Approach • Evaluation on Open Source Projects • Related Work • Conclusion
Approach Overview • Phase I: Creating Abstract Edit Scripts • Phase II: Applying Abstract Edit Scripts • Step 1. Syntactic Program Differencing • Step 2. Edit Context Extraction • Step 3. Identifier and Edit Position Abstraction Cold Bold Bnew Cnew
Step 1. Syntactic Program Differencing • ChangeDistiller(Aold, Anew) AST edit script • insert(u, v, k): insert node u and position it as the (k+1)th child of node v • delete(u): delete node u • update(u, v): replace u withv • move(u, v, k): delete u from its current position and insert u as the (k+1)th child of v
Step 1. Syntactic Program Differencing Anew Aold N1 method_decl method_decl N4 N5 return iter_decl while configs_ decl configs_ decl while return iter_decl config_ decl O6 N8 config_ decl if config_ asgn if if N7 N9 then then then update(O6,N4) N10 move(O6, N1, 2) configs_mi config_ mi configs_mi insert(N7, N5, 0) insert(N8, N5, 1) insert(N9, N8, 0) insert(N10, N9, 0)
Step 2. Edit Context Extraction Identify unchanged nodes on which the edited nodes depend or nodes that dependon the edits • Containment dependence • Control dependence • Data dependence
Step 2. Edit Context Extraction Anew Aold O1 N1 method_decl method_decl O2 N2 N4 O4 N5 return iter_decl while configs_decl configs_decl while return iter_decl config_ decl O6 N8 N7 config_ decl if config_ asgn if if N9 then then then N10 configs_mi config_ mi configs_mi
Step 3. Identifier and Edit Position Abstraction • Identifier Abstraction • Edit Position Abstraction • config = (ILaunchConfiguration) iter.next() • => v1 = (T1) v2.m1() • insert (“i++”, “while(i<j)”, 2) • => insert (“v1++”, “while(v1<v2)”, 1)
Step 3. Identifier and Edit Position Abstraction Abstractold Abstractnew method_decl method_decl AN1 AN3 AN4 iter_decl iter_decl config_decl while while T1 v1 = m1().m2() T1 v1 = m1().m2() while(v1.m3()) T2 v2 = null while(v1.m3()) AN5 AN6 AO4 config_asgn if config_decl v2 = (T2)v1.m4() if(v2.m5()) T2 v2 = (T2)v1.m4() update(AO4,AN3) AN7 move(AO4, AN1, 1) then insert(AN5, AN4, 0) AN8 insert(AN6, AN4, 1) configs.mi insert(AN7, AN6, 0) v2.m6() insert(AN8, AN7, 0)
Approach Overview • Phase I: Creating Abstract Edit Scripts • Phase II: Applying Abstract Edit Scripts • Step 1. Syntactic Program Differencing • Step 2. Edit Context Extraction • Step 3. Identifier and Edit Position Abstraction Cold Bold • Step 4. Context Matching • Step 5. Identifier and Edit Position Concretization • Step 6. Concrete Edit Application Bnew Cnew
Step 4. Context Matching Abstractold Bold AO1 method_decl method_ decl O2 AO2 AO3 v2_decl v1_decl v1_decl while while while T1 v1 = m1().m9() T1 v2 = new T1() T1 v1 = m1().m9() while(v1.m3()) while(v2.m3()) while(v2.m3()) T1 v1 = m1().m2() T1 v1 = m1().m2() AO5 AO4 AO6 v1_mi v3_decl v3_mi v1_mi v1.m8() v1= (T2)v3.m6() T3 v3 = m5().m4() v1.m7() v3_decl v3_mi v2_mi v3_mi T3 v3 = m5().m4() v2 =(T2)v3.m6() v2.m7() v1 = (T2)v3.m6()
Step 4. Context Matching Abstracto mBo method_decl method_decl iter_decl return cfgs_decl iter_decl while while T1 v1 = m1().m2() while(v1.m3()) while(v1.m3()) T2 v2 = new T2() return v2 T1 v1 = m1().m2() config_decl file_decl cfg_decl if T2 v2 = (T2)v1.m4() if(v4!=null && v4.m6().m7(v5)) T4 v3 = (T4)v1.m4() T5 v4 = v3.m5() then cfgs.mi v2.m8(v4)
Step 5. Identifier and Edit Position Concretization • Identifier Concretization • Edit Position Concretization • v1 = (T1) v2.m1() • => cfg = (ILaunchConfiguration) iter.next() • insert (“v1++”, “while(v1<v2)”, 1) • => insert (“i++”, “while(i<j)”, 2)
Step 6. Concrete Edit Script Application mBsuggested mBo N1 method_decl method_decl N3 N5 iter_decl cfg_decl cfgs_decl while return iter_decl cfgs_decl return while O6 N8 cfg_decl if cfg_asgn if if N7 N9 then then then update(O6,N3) N10 move(O6, N1, 1) cfgs_mi cfg_mi cfgs_mi insert(N7, N5, 0) insert(N8, N5, 1) insert(N9, N8, 0) insert(N10, N9, 0)
Outline • Program Transformation Generation Approach • Evaluation on Open Source Projects • Related Work • Conclusion
Test Suite • 56 pairs of exemplar edits from • org.eclipse.compare • org.eclipse.core.runtime • org.debug.core • Method pairs are at least 40% similar and share at least one common edit • jdt.core • jEdit
RQ1: Coverage, Accuracy and Similarity Coverage: What percentage of examples can Sydit match the context and produce some edits? Accuracy: What percentage of examples can Sydit apply edits correctly? Similarity: How similar is Sydit-generated version to developer-generated version?
RQ1: Coverage, Accuracy and Similarity Sydit creates edits for 82% of examples, produces correct edits for 70%, & with 96% similarity to developer’s edits
Related Work • Program differencing • Refactoring • Source transformation • Simultaneous text editing [Miller et al.] • Generic patch inference [Andersen & Lawall]
Conclusion Syditautomates systematic edits by generating transformations from an example and applies them to different contexts with 82% coverage 70% accuracy, and 96% similarity Future Work • Select target methods automatically • Present proposed edits in a diff-style view for programmers to preview • Integrate with automated compilation and testing