/*******************************************************************************
 * Copyright (c) 2002 - 2006 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.ibm.wala.core.tests.callGraph;

import java.io.IOException;

import com.ibm.wala.ipa.callgraph.AnalysisCache;
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.callgraph.CallGraph;
import com.ibm.wala.ipa.callgraph.CallGraphBuilder;
import com.ibm.wala.ipa.callgraph.Entrypoint;
import com.ibm.wala.ipa.callgraph.impl.Util;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.util.CancelException;
import com.ibm.wala.util.config.AnalysisScopeReader;
import com.ibm.wala.util.io.FileProvider;
import com.ibm.wala.util.perf.StopwatchGC;

/**
 * Utilities for call graph tests
 */
public class CallGraphTestUtil {

  private static final ClassLoader MY_CLASSLOADER = CallGraphTestUtil.class.getClassLoader();

  public static AnalysisOptions makeAnalysisOptions(AnalysisScope scope, Iterable<Entrypoint> entrypoints) {
    AnalysisOptions options = new AnalysisOptions(scope, entrypoints);
    return options;
  }

  public static String REGRESSION_EXCLUSIONS = "Java60RegressionExclusions.txt";

  /**
   * should we check the heap footprint before and after CG construction?
   */
  private static final boolean CHECK_FOOTPRINT = false;

  public static AnalysisScope makeJ2SEAnalysisScope(String scopeFile, String exclusionsFile) throws IOException {
    AnalysisScope scope = AnalysisScopeReader.readJavaScope(scopeFile, (new FileProvider()).getFile(exclusionsFile), MY_CLASSLOADER);
    return scope;
  }

  public static CallGraph buildRTA(AnalysisOptions options, AnalysisCache cache, IClassHierarchy cha, AnalysisScope scope)
      throws IllegalArgumentException, CancelException {
    StopwatchGC S = null;
    if (CHECK_FOOTPRINT) {
      S = new StopwatchGC("build RTA graph");
      S.start();
    }

    CallGraphBuilder builder = Util.makeRTABuilder(options, cache, cha, scope);
    CallGraph cg = builder.makeCallGraph(options, null);

    if (CHECK_FOOTPRINT) {
      S.stop();
      System.err.println(S.report());
    }
    return cg;
  }

  public static CallGraph buildZeroCFA(AnalysisOptions options, AnalysisCache cache, IClassHierarchy cha, AnalysisScope scope,
      boolean testPAtoString) throws IllegalArgumentException, CancelException {
    StopwatchGC S = null;
    if (CHECK_FOOTPRINT) {
      S = new StopwatchGC("build RTA graph");
      S.start();
    }

    CallGraphBuilder builder = Util.makeZeroCFABuilder(options, cache, cha, scope);
    CallGraph cg = builder.makeCallGraph(options, null);
    if (testPAtoString) {
      builder.getPointerAnalysis().toString();
    }

    if (CHECK_FOOTPRINT) {
      S.stop();
      System.err.println(S.report());
    }
    return cg;
  }

  public static CallGraph buildVanillaZeroOneCFA(AnalysisOptions options, AnalysisCache cache, IClassHierarchy cha,
      AnalysisScope scope) throws IllegalArgumentException, CancelException {
    StopwatchGC S = null;
    if (CHECK_FOOTPRINT) {
      S = new StopwatchGC("build RTA graph");
      S.start();
    }

    CallGraphBuilder builder = Util.makeVanillaZeroOneCFABuilder(options, cache, cha, scope);
    CallGraph cg = builder.makeCallGraph(options, null);

    if (CHECK_FOOTPRINT) {
      S.stop();
      System.err.println(S.report());
    }
    return cg;
  }

  public static CallGraph buildZeroOneCFA(AnalysisOptions options, AnalysisCache cache, IClassHierarchy cha, AnalysisScope scope,
      boolean testPAtoString) throws IllegalArgumentException, CancelException {
    StopwatchGC S = null;
    if (CHECK_FOOTPRINT) {
      S = new StopwatchGC("build RTA graph");
      S.start();
    }

    CallGraphBuilder builder = Util.makeZeroOneCFABuilder(options, cache, cha, scope);
    CallGraph cg = builder.makeCallGraph(options, null);
    if (testPAtoString) {
      builder.getPointerAnalysis().toString();
    }

    if (CHECK_FOOTPRINT) {
      S.stop();
      System.err.println(S.report());
    }
    return cg;
  }

  public static CallGraph buildZeroContainerCFA(AnalysisOptions options, AnalysisCache cache, IClassHierarchy cha,
      AnalysisScope scope) throws IllegalArgumentException, CancelException {
    StopwatchGC S = null;
    if (CHECK_FOOTPRINT) {
      S = new StopwatchGC("build RTA graph");
      S.start();
    }

    CallGraphBuilder builder = Util.makeZeroContainerCFABuilder(options, cache, cha, scope);
    CallGraph cg = builder.makeCallGraph(options, null);

    if (CHECK_FOOTPRINT) {
      S.stop();
      System.err.println(S.report());
    }
    return cg;
  }

  public static CallGraph buildZeroOneContainerCFA(AnalysisOptions options, AnalysisCache cache, IClassHierarchy cha,
      AnalysisScope scope) throws IllegalArgumentException, CancelException {
    StopwatchGC S = null;
    if (CHECK_FOOTPRINT) {
      S = new StopwatchGC("build RTA graph");
      S.start();
    }

    CallGraphBuilder builder = Util.makeZeroOneContainerCFABuilder(options, cache, cha, scope);
    CallGraph cg = builder.makeCallGraph(options, null);

    if (CHECK_FOOTPRINT) {
      S.stop();
      System.err.println(S.report());
    }
    return cg;
  }

}
