1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.core.test;
  26 
  27 import java.util.Map;
  28 import java.util.concurrent.ConcurrentHashMap;
  29 import java.util.stream.Collectors;
  30 
  31 import org.graalvm.compiler.api.replacements.Fold;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.graphbuilderconf.GeneratedInvocationPlugin;
  34 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
  35 import org.graalvm.compiler.nodes.spi.CoreProviders;
  36 import org.graalvm.compiler.phases.VerifyPhase;
  37 
  38 import jdk.vm.ci.meta.ResolvedJavaMethod;
  39 import jdk.vm.ci.meta.ResolvedJavaType;
  40 
  41 /**
  42  * Verifies that all {@link Fold} annotated methods have at least one caller.
  43  */
  44 public class VerifyFoldableMethods extends VerifyPhase<CoreProviders> {
  45 
  46     @Override
  47     public boolean checkContract() {
  48         return false;
  49     }
  50 
  51     private final Map<ResolvedJavaMethod, Boolean> foldables = new ConcurrentHashMap<>();
  52     ResolvedJavaType generatedInvocationPluginType;
  53 
  54     @Override
  55     protected void verify(StructuredGraph graph, CoreProviders context) {
  56         ResolvedJavaMethod method = graph.method();
  57         if (method.getAnnotation(Fold.class) != null) {
  58             foldables.putIfAbsent(method, false);
  59         } else {
  60             if (generatedInvocationPluginType == null) {
  61                 generatedInvocationPluginType = context.getMetaAccess().lookupJavaType(GeneratedInvocationPlugin.class);
  62             }
  63             if (!generatedInvocationPluginType.isAssignableFrom(method.getDeclaringClass())) {
  64                 for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
  65                     ResolvedJavaMethod callee = t.targetMethod();
  66                     if (callee.getAnnotation(Fold.class) != null) {
  67                         foldables.put(callee, true);
  68                     }
  69                 }
  70             }
  71         }
  72     }
  73 
  74     public void finish() {
  75         String uncalled = foldables.entrySet().stream().filter(e -> e.getValue() == false).map(e -> e.getKey().format("%H.%n(%p)")).collect(Collectors.joining(System.lineSeparator() + "  "));
  76         if (uncalled.length() != 0) {
  77             throw new VerificationError(String.format("Methods annotated with @" + Fold.class.getSimpleName() + " appear to have no usages:%n  %s", uncalled));
  78         }
  79     }
  80 }