| 38 | import java.util.concurrent.TimeUnit; |
| 39 | |
| 40 | class IRBuilder implements pascal.taie.ir.IRBuilder { |
| 41 | |
| 42 | private static final Logger logger = LogManager.getLogger(IRBuilder.class); |
| 43 | |
| 44 | private final transient Converter converter; |
| 45 | |
| 46 | IRBuilder(Converter converter) { |
| 47 | this.converter = converter; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public IR buildIR(JMethod method) { |
| 52 | try { |
| 53 | return new MethodIRBuilder(method, converter).build(); |
| 54 | } catch (RuntimeException e) { |
| 55 | if (e.getStackTrace()[0].getClassName().startsWith("soot")) { |
| 56 | logger.warn("Soot front failed to build method body for {}," + |
| 57 | " constructs an empty IR instead", method); |
| 58 | return new IRBuildHelper(method).buildEmpty(); |
| 59 | } else { |
| 60 | throw e; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Builds IR for all methods in given class hierarchy. |
| 67 | */ |
| 68 | @Override |
| 69 | public void buildAll(ClassHierarchy hierarchy) { |
| 70 | Timer timer = new Timer("Build IR for all methods"); |
| 71 | timer.start(); |
| 72 | int nThreads = Runtime.getRuntime().availableProcessors(); |
| 73 | // Group all methods by number of threads |
| 74 | List<List<JMethod>> groups = new ArrayList<>(); |
| 75 | for (int i = 0; i < nThreads; ++i) { |
| 76 | groups.add(new ArrayList<>()); |
| 77 | } |
| 78 | List<JClass> classes = hierarchy.allClasses().toList(); |
| 79 | int i = 0; |
| 80 | for (JClass c : classes) { |
| 81 | for (JMethod m : c.getDeclaredMethods()) { |
| 82 | if (!m.isAbstract() || m.isNative()) { |
| 83 | groups.get(i++ % nThreads).add(m); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | // Build IR for all methods in parallel |
| 88 | ExecutorService service = Executors.newFixedThreadPool(nThreads); |
| 89 | for (List<JMethod> group : groups) { |
| 90 | service.execute(() -> group.forEach(JMethod::getIR)); |
| 91 | } |
| 92 | service.shutdown(); |
| 93 | try { |
| 94 | service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
| 95 | } catch (InterruptedException e) { |
| 96 | throw new RuntimeException(e); |
| 97 | } |
nothing calls this directly
no outgoing calls
no test coverage detected