| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 library kernel.treeshaker_dump; | 4 library kernel.treeshaker_dump; |
| 5 | 5 |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 import 'package:kernel/class_hierarchy.dart'; |
| 7 import 'package:kernel/core_types.dart'; | 8 import 'package:kernel/core_types.dart'; |
| 8 import 'package:kernel/kernel.dart'; | 9 import 'package:kernel/kernel.dart'; |
| 9 import 'package:kernel/transformations/treeshaker.dart'; | 10 import 'package:kernel/transformations/treeshaker.dart'; |
| 10 import 'package:args/args.dart'; | 11 import 'package:args/args.dart'; |
| 11 import 'package:path/path.dart' as pathlib; | 12 import 'package:path/path.dart' as pathlib; |
| 12 import 'package:kernel/text/ast_to_text.dart'; | 13 import 'package:kernel/text/ast_to_text.dart'; |
| 13 | 14 |
| 14 ArgParser parser = new ArgParser(allowTrailingOptions: true) | 15 ArgParser parser = new ArgParser(allowTrailingOptions: true) |
| 15 ..addFlag('used', help: 'Print used members', negatable: false) | 16 ..addFlag('used', help: 'Print used members', negatable: false) |
| 16 ..addFlag('unused', help: 'Print unused members', negatable: false) | 17 ..addFlag('unused', help: 'Print unused members', negatable: false) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 60 |
| 60 if (options['output'] != null && !options['diff']) { | 61 if (options['output'] != null && !options['diff']) { |
| 61 print('--output must be used with --diff'); | 62 print('--output must be used with --diff'); |
| 62 exit(1); | 63 exit(1); |
| 63 } | 64 } |
| 64 | 65 |
| 65 bool strong = options['strong']; | 66 bool strong = options['strong']; |
| 66 | 67 |
| 67 Program program = loadProgramFromBinary(filename); | 68 Program program = loadProgramFromBinary(filename); |
| 68 CoreTypes coreTypes = new CoreTypes(program); | 69 CoreTypes coreTypes = new CoreTypes(program); |
| 69 TreeShaker shaker = new TreeShaker(coreTypes, program, strongMode: strong); | 70 ClassHierarchy hierarchy = new ClosedWorldClassHierarchy(program); |
| 71 TreeShaker shaker = |
| 72 new TreeShaker(coreTypes, hierarchy, program, strongMode: strong); |
| 70 int totalClasses = 0; | 73 int totalClasses = 0; |
| 71 int totalInstantiationCandidates = 0; | 74 int totalInstantiationCandidates = 0; |
| 72 int totalMembers = 0; | 75 int totalMembers = 0; |
| 73 int usedClasses = 0; | 76 int usedClasses = 0; |
| 74 int instantiatedClasses = 0; | 77 int instantiatedClasses = 0; |
| 75 int usedMembers = 0; | 78 int usedMembers = 0; |
| 76 | 79 |
| 77 void visitMember(Member member) { | 80 void visitMember(Member member) { |
| 78 if (member.isAbstract) return; // Abstract members are not relevant. | 81 if (member.isAbstract) return; // Abstract members are not relevant. |
| 79 ++totalMembers; | 82 ++totalMembers; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 125 |
| 123 if (options['diff']) { | 126 if (options['diff']) { |
| 124 String name = pathlib.basenameWithoutExtension(filename); | 127 String name = pathlib.basenameWithoutExtension(filename); |
| 125 String outputDir = options['output'] ?? ''; | 128 String outputDir = options['output'] ?? ''; |
| 126 String beforeFile = pathlib.join(outputDir, '$name.before.txt'); | 129 String beforeFile = pathlib.join(outputDir, '$name.before.txt'); |
| 127 String afterFile = pathlib.join(outputDir, '$name.after.txt'); | 130 String afterFile = pathlib.join(outputDir, '$name.after.txt'); |
| 128 NameSystem names = new NameSystem(); | 131 NameSystem names = new NameSystem(); |
| 129 StringBuffer before = new StringBuffer(); | 132 StringBuffer before = new StringBuffer(); |
| 130 new Printer(before, syntheticNames: names).writeProgramFile(program); | 133 new Printer(before, syntheticNames: names).writeProgramFile(program); |
| 131 new File(beforeFile).writeAsStringSync('$before'); | 134 new File(beforeFile).writeAsStringSync('$before'); |
| 132 new TreeShaker(coreTypes, program, strongMode: strong).transform(program); | 135 new TreeShaker(coreTypes, hierarchy, program, strongMode: strong) |
| 136 .transform(program); |
| 133 StringBuffer after = new StringBuffer(); | 137 StringBuffer after = new StringBuffer(); |
| 134 new Printer(after, syntheticNames: names).writeProgramFile(program); | 138 new Printer(after, syntheticNames: names).writeProgramFile(program); |
| 135 new File(afterFile).writeAsStringSync('$after'); | 139 new File(afterFile).writeAsStringSync('$after'); |
| 136 print('Text written to $beforeFile and $afterFile'); | 140 print('Text written to $beforeFile and $afterFile'); |
| 137 } | 141 } |
| 138 } | 142 } |
| 139 | 143 |
| 140 String ratio(num x, num total) { | 144 String ratio(num x, num total) { |
| 141 return '$x / $total (${percent(x, total)})'; | 145 return '$x / $total (${percent(x, total)})'; |
| 142 } | 146 } |
| 143 | 147 |
| 144 String percent(num x, num total) { | 148 String percent(num x, num total) { |
| 145 return total == 0 ? '0%' : ((100 * x / total).toStringAsFixed(0) + '%'); | 149 return total == 0 ? '0%' : ((100 * x / total).toStringAsFixed(0) + '%'); |
| 146 } | 150 } |
| OLD | NEW |