001package daikon.inv.filter;
002
003import daikon.PptSlice;
004import daikon.PptTopLevel;
005import daikon.VarInfo;
006import daikon.inv.Implication;
007import daikon.inv.Invariant;
008import daikon.inv.unary.OneOf;
009import daikon.inv.unary.scalar.OneOfFloat;
010import daikon.inv.unary.scalar.OneOfScalar;
011import org.checkerframework.dataflow.qual.Pure;
012
013public class OnlyConstantVariablesFilter extends InvariantFilter {
014  @Override
015  public String getDescription() {
016    return "Suppress invariants containing only constants";
017  }
018
019  /** Boolean. If true, OnlyConstantVariablesFilter is initially turned on. */
020  public static boolean dkconfig_enabled = true;
021
022  public OnlyConstantVariablesFilter() {
023    isOn = dkconfig_enabled;
024  }
025
026  @Override
027  boolean shouldDiscardInvariant(Invariant invariant) {
028    // System.out.println("OnlyConstantVariablesFilter: " + invariant.format());
029    if (invariant.isEqualityComparison()) {
030      return false;
031    }
032    if (invariant instanceof OneOf) {
033      return false;
034    }
035    if (invariant instanceof Implication) {
036      Implication impl = (Implication) invariant;
037      // jhp 11/04/03, comment out if below.  It seems to never make sense
038      // to look at constant variables for the implication itself since
039      // the implication is in a PptSlice0 (which will always fail the test
040      // below
041      // if (impl.predicate().isGuardingPredicate)
042      // only consider the consequent
043      invariant = impl.consequent();
044    }
045
046    VarInfo[] vis = invariant.ppt.var_infos;
047    for (int i = 0; i < vis.length; i++) {
048      if (!isConstant(vis[i])) {
049        // System.out.println("In " + invariant.format() + ": non-constant " + vis[i].name);
050        return false;
051      }
052    }
053    return true;
054  }
055
056  @Pure
057  boolean isConstant(VarInfo vi) {
058    PptTopLevel ppt = vi.ppt;
059    boolean isStaticConstant = vi.is_static_constant;
060    boolean isDynamicConstant = ((ppt.constants != null) && ppt.constants.is_constant(vi));
061    PptSlice view = ppt.findSlice(vi);
062    // TODO: This should be generalized to other types of scalar
063    OneOfScalar oos = (view == null) ? null : OneOfScalar.find(view);
064    OneOfFloat oof = (view == null) ? null : OneOfFloat.find(view);
065    boolean isOneOfConstant =
066        (((oos != null) && (oos.num_elts() == 1) && !oos.is_hashcode())
067            || ((oof != null) && (oof.num_elts() == 1)
068            // no hashcode test for floats
069            // && (! oof.is_hashcode())
070            ));
071    return isStaticConstant || isDynamicConstant || isOneOfConstant;
072  }
073}