001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.phases.verify;
024
025import com.oracle.graal.debug.*;
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.nodes.*;
029import com.oracle.graal.nodes.java.*;
030import com.oracle.graal.phases.*;
031import com.oracle.graal.phases.tiers.*;
032
033/**
034 * Verifies that no argument to one of the {@link Debug#log(String)} methods is the result of
035 * {@link StringBuilder#toString()} or {@link StringBuffer#toString()}. Instead, one of the
036 * multi-parameter {@code log()} methods should be used. The goal is to minimize/prevent allocation
037 * at logging call sites.
038 */
039public class VerifyDebugUsage extends VerifyPhase<PhaseContext> {
040
041    @Override
042    protected boolean verify(StructuredGraph graph, PhaseContext context) {
043        for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
044            ResolvedJavaMethod callee = t.targetMethod();
045            ResolvedJavaType debugType = context.getMetaAccess().lookupJavaType(Debug.class);
046            if (callee.getDeclaringClass().equals(debugType)) {
047                if (callee.getName().equals("log")) {
048                    int argIdx = 1;
049                    for (ValueNode arg : t.arguments()) {
050                        if (arg instanceof Invoke) {
051                            Invoke invoke = (Invoke) arg;
052                            CallTargetNode callTarget = invoke.callTarget();
053                            if (callTarget instanceof MethodCallTargetNode) {
054                                ResolvedJavaMethod m = ((MethodCallTargetNode) callTarget).targetMethod();
055                                if (m.getName().equals("toString")) {
056                                    String holder = m.getDeclaringClass().getName();
057                                    if (holder.equals("Ljava/lang/StringBuilder;") || holder.equals("Ljava/lang/StringBuffer;")) {
058                                        StackTraceElement e = graph.method().asStackTraceElement(invoke.bci());
059                                        throw new VerificationError(String.format("%s: parameter %d of call to %s appears to be a String concatenation expression.%n"
060                                                        + "    Use one of the multi-parameter Debug.log() methods or Debug.logv() instead.", e, argIdx, callee.format("%H.%n(%p)")));
061                                    }
062                                }
063                            }
064                        }
065                        argIdx++;
066                    }
067                }
068            }
069        }
070        return true;
071    }
072}