// Tests that local variables are correctly recognized and that
  // cast expressions are skipped appropriately
  public void testLocalTypedef() throws SVParseException {
    String content =
        "function void foobar();\n"
            + "    typedef foo #(BAR) foo_t;\n"
            + "    foo_t              a;\n"
            + "    a = 5;\n"
            + "endfunction\n";

    //		ParserSVDBFileFactory parser = new ParserSVDBFileFactory(null);

    SVDBTask func = parse_tf(content, "testLocalTypedef");

    SVDBVarDeclItem a = null;
    for (ISVDBItemBase it : func.getChildren()) {
      if (it.getType() == SVDBItemType.VarDeclStmt) {
        for (ISVDBChildItem vi : ((SVDBVarDeclStmt) it).getChildren()) {
          if (SVDBItem.getName(vi).equals("a")) {
            a = (SVDBVarDeclItem) vi;
          }
        }
      }
    }

    assertEquals(3, SVDBUtil.getChildrenSize(func));
    assertEquals("foo_t", SVDBItem.getName(SVDBUtil.getFirstChildItem(func)));
    assertNotNull(a);
  }
  // Tests that local variables are correctly recognized and that
  // cast expressions are skipped appropriately
  public void testLocalVarsWithCast() throws SVParseException {
    String content =
        "function void foobar();\n"
            + "    int a = integer'(5);\n"
            + "    int b = longint'(6);\n"
            + "    a = 5;\n"
            + "endfunction\n";

    SVCorePlugin.getDefault().enableDebug(false);

    SVDBTask func = parse_tf(content, "testLocalVarsWithCast");

    assertEquals(3, SVDBUtil.getChildrenSize(func));
    SVDBVarDeclItem a = null, b = null;
    for (ISVDBItemBase it_t : func.getChildren()) {
      if (it_t.getType() == SVDBItemType.VarDeclStmt) {
        SVDBVarDeclStmt v = (SVDBVarDeclStmt) it_t;
        for (ISVDBChildItem vi : v.getChildren()) {
          if (SVDBItem.getName(vi).equals("a")) {
            a = (SVDBVarDeclItem) vi;
          } else if (SVDBItem.getName(vi).equals("b")) {
            b = (SVDBVarDeclItem) vi;
          }
        }
      }
    }
    assertNotNull(a);
    assertNotNull(b);
  }
  public void testTypedefClass() {
    String content =
        "class foobar;\n"
            + "\n"
            + "    typedef class other_foo_t;\n"
            + "    typedef class other_foo_t1;\n"
            + "    typedef class other_foo_t2;\n"
            + "    typedef class other_foo_t3;\n"
            + "    typedef class other_foo_t4;\n"
            + "\n"
            + "    other_foo_t	    foo_f;"
            + "\n"
            + "endclass\n";

    SVDBFile file = SVDBTestUtils.parse(content, "testClassStringFields");

    SVDBClassDecl foobar = null;
    for (ISVDBItemBase it : file.getChildren()) {
      if (SVDBItem.getName(it).equals("foobar")) {
        foobar = (SVDBClassDecl) it;
        break;
      }
    }

    SVDBTypedefStmt foobar_td = null;
    ISVDBItemBase foobar_i = null;
    ISVDBItemBase foobar_i1 = null;

    for (ISVDBItemBase it : foobar.getChildren()) {
      if (SVDBItem.getName(it).equals("other_foo_t")) {
        foobar_i = it;
      } else if (SVDBItem.getName(it).equals("other_foo_t1")) {
        foobar_i1 = it;
      }
    }

    assertNotNull("Failed to find other_foo_t", foobar_i);
    assertNotNull("Failed to find other_foo_t1", foobar_i1);
    assertEquals("other_foo_t is of wrong type", foobar_i.getType(), SVDBItemType.TypedefStmt);

    foobar_td = (SVDBTypedefStmt) foobar_i;

    assertEquals(
        "other_foo_t type-info is of wrong type",
        SVDBItemType.TypeInfoFwdDecl,
        foobar_td.getTypeInfo().getType());
  }
  public void testTypedef() {
    String content =
        "class foobar;\n"
            + "\n"
            + "    typedef enum {\n"
            + "        FOO,\n"
            + "        BAR\n"
            + "    } foobar_t;\n"
            + "\n"
            + "\n"
            + "    foobar_t     foo_f;"
            + "\n"
            + "endclass\n";

    SVDBFile file = SVDBTestUtils.parse(content, "testClassStringFields");

    SVDBClassDecl foobar = null;
    for (ISVDBItemBase it : file.getChildren()) {
      if (SVDBItem.getName(it).equals("foobar")) {
        foobar = (SVDBClassDecl) it;
        break;
      }
    }

    SVDBTypedefStmt foobar_td = null;
    ISVDBItemBase foobar_i = null;

    for (ISVDBItemBase it : foobar.getChildren()) {
      if (SVDBItem.getName(it).equals("foobar_t")) {
        foobar_i = it;
      }
    }

    assertNotNull("Failed to find foobar_t", foobar_i);
    assertEquals("foobar_t is of wrong type", foobar_i.getType(), SVDBItemType.TypedefStmt);

    foobar_td = (SVDBTypedefStmt) foobar_i;

    assertEquals(
        "foobar_t type-info is of wrong type",
        SVDBItemType.TypeInfoEnum,
        foobar_td.getTypeInfo().getType());

    SVDBTypeInfoEnum enum_t = (SVDBTypeInfoEnum) foobar_td.getTypeInfo();
    assertEquals(
        "foobar_t doesn't have correct number of elements", 2, enum_t.getEnumerators().size());
  }
  public void testClassStringFields() {
    String content =
        "class __sv_builtin_covergroup_options;\n"
            + "int 	weight;\n"
            + "\n"
            + "real 	goal;\n"
            + "\n"
            + "string 	name;\n"
            + "\n"
            + "string 	comment;\n"
            + "\n"
            + "int		at_least;\n"
            + "\n"
            + "bit		detect_overlap;\n"
            + "\n"
            + "int		auto_bin_max;\n"
            + "\n"
            + "bit		per_instance;\n"
            + "\n"
            + "bit		cross_num_print_missing;\n"
            + "\n"
            + "endclass\n";
    LogHandle log = LogFactory.getLogHandle("testClassStringFields");
    SVDBFile file = SVDBTestUtils.parse(content, "testClassStringFields");

    SVDBClassDecl cg_options = null;
    for (ISVDBItemBase it : file.getChildren()) {
      if (SVDBItem.getName(it).equals("__sv_builtin_covergroup_options")) {
        cg_options = (SVDBClassDecl) it;
      }
      log.debug("Item: " + it.getType() + " " + SVDBItem.getName(it));
    }

    assertNotNull("Failed to find class __sv_builtin_covergroup_options", cg_options);

    for (ISVDBItemBase it : cg_options.getChildren()) {
      log.debug("    Item: " + it.getType() + " " + SVDBItem.getName(it));
      assertNotNull("Item " + SVDBItem.getName(it) + " does not have a location", it.getLocation());
      if (SVDBStmt.isType(it, SVDBItemType.VarDeclStmt)) {
        assertNotNull(
            "Field " + SVDBItem.getName(it) + " does not have a type",
            ((SVDBVarDeclStmt) it).getTypeInfo());
      }
    }
  }
 public SVDBItemType getItemType() {
   if (fCacheItem != null) {
     return fCacheItem.getType();
   } else if (fItem != null) {
     return fItem.getType();
   } else {
     return null;
   }
 }
  private void assertNoErrors(LogHandle log, ISVDBIndexIterator index_it) {
    ISVDBItemIterator it_i = index_it.getItemIterator(new NullProgressMonitor());
    List<SVDBMarker> errors = new ArrayList<SVDBMarker>();

    while (it_i.hasNext()) {
      ISVDBItemBase it = it_i.nextItem();
      if (it.getType() == SVDBItemType.Marker) {
        SVDBMarker marker = (SVDBMarker) it;
        if (marker.getMarkerType() == MarkerType.Error) {
          errors.add(marker);
        }
      }
    }

    for (SVDBMarker m : errors) {
      log.debug("[ERROR] " + m.getMessage() + " @ " + ":" + m.getLocation().getLine());
    }

    assertEquals(0, errors.size());
  }
Beispiel #8
0
  private static void printItem(int indent, ISVDBItemBase item) {
    for (int i = 0; i < indent; i++) {
      System.out.print(" ");
    }

    System.out.print("" + item.getType());
    if (item instanceof ISVDBNamedItem) {
      System.out.print(" " + ((ISVDBNamedItem) item).getName());
    }

    if (item instanceof SVDBPreProcCond) {
      System.out.print(" " + ((SVDBPreProcCond) item).getConditional());
    }
    System.out.println();

    if (item instanceof ISVDBScopeItem) {
      for (ISVDBItemBase it : ((ISVDBScopeItem) item).getItems()) {
        printItem(indent + 4, it);
      }
    }
  }
  public synchronized List<ISVDBItemBase> findItems(
      ISVDBChildItem context, String name, boolean stop_on_first_match, SVDBItemType... types) {
    int scope_level = 0;
    List<ISVDBItemBase> ret = new ArrayList<ISVDBItemBase>();
    fLog.debug(
        "--> find: context="
            + ((context != null) ? SVDBItem.getName(context) : "null")
            + " type="
            + ((context != null) ? context.getType() : "null")
            + " name="
            + name);

    fRet = ret;

    // Search up the scope
    while (context != null
        && context.getType() != SVDBItemType.File
        && context instanceof ISVDBChildParent) {
      fLog.debug("Scope " + SVDBItem.getName(context) + " " + context.getType());

      if (context.getType() == SVDBItemType.ClassDecl) {
        SVDBClassDecl cls = (SVDBClassDecl) context;
        if (cls.getParameters() != null) {
          for (SVDBModIfcClassParam p : cls.getParameters()) {
            if (fMatcher.match(p, name)) {
              add(p, Scope.ScopeModIfcClsVars, scope_level);
            }
          }
        }
      } else if (context.getType() == SVDBItemType.Function
          || context.getType() == SVDBItemType.Task) {
        String tf_name = ((ISVDBNamedItem) context).getName();
        int idx;
        if (tf_name != null && (idx = tf_name.indexOf("::")) != -1) {
          // This is an external method. Go find the class that this is part of
          String clsname = tf_name.substring(0, idx);
          SVDBFindByName finder = new SVDBFindByName(fIndexIt);
          List<SVDBDeclCacheItem> cls_l = finder.find(clsname, false, SVDBItemType.ClassDecl);
          ISVDBItemBase cls_i;
          if (cls_l.size() > 0 && (cls_i = cls_l.get(0).getSVDBItem()) != null) {
            SVDBFindByNameInClassHierarchy cls_h_finder =
                new SVDBFindByNameInClassHierarchy(fIndexIt, fMatcher);
            List<ISVDBItemBase> it_l = cls_h_finder.find((SVDBClassDecl) cls_i, name);

            for (ISVDBItemBase it_t : it_l) {
              add(it_t, Scope.ScopeModIfcClsVars, scope_level);
            }
          }
        }
      }

      // First, search the local variables
      for (ISVDBItemBase it : ((ISVDBChildParent) context).getChildren()) {
        fLog.debug("Scope " + SVDBItem.getName(context) + " child " + SVDBItem.getName(it));
        if (it instanceof SVDBVarDeclStmt) {
          for (ISVDBItemBase it_t : ((SVDBVarDeclStmt) it).getChildren()) {
            fLog.debug("  Variable " + SVDBItem.getName(it_t) + " (match " + name + ")");
            if (it_t instanceof ISVDBNamedItem && fMatcher.match((ISVDBNamedItem) it_t, name)) {
              boolean match = (types.length == 0 || it_t.getType().isElemOf(types));

              if (match) {
                fLog.debug("    Matches Variable " + SVDBItem.getName(it_t));
                add(it_t, Scope.ScopeModIfcClsVars, scope_level);

                if (stop_on_first_match) {
                  break;
                }
              }
            }
          }
        } else if (it instanceof SVDBModIfcInst) {
          for (ISVDBItemBase it_t : ((SVDBModIfcInst) it).getChildren()) {
            if (it_t instanceof ISVDBNamedItem && fMatcher.match((ISVDBNamedItem) it_t, name)) {
              boolean match = (types.length == 0);

              for (SVDBItemType t : types) {
                if (it_t.getType() == t) {
                  match = true;
                  break;
                }
              }

              if (match) {
                add(it_t, Scope.ScopeModIfcClsVars, scope_level);

                if (stop_on_first_match) {
                  break;
                }
              }
            }
          }
        } else if (it.getType() == SVDBItemType.TypedefStmt
            && ((SVDBTypedefStmt) it).getTypeInfo().getType() == SVDBItemType.TypeInfoEnum) {
          // Check the enumerators for matches
          SVDBTypeInfoEnum e = (SVDBTypeInfoEnum) ((SVDBTypedefStmt) it).getTypeInfo();
          for (SVDBTypeInfoEnumerator en : e.getEnumerators()) {
            if (fMatcher.match(en, name)) {
              add(en, Scope.ScopeModIfcClsVars, scope_level);
              if (stop_on_first_match) {
                break;
              }
            }
          }
          if (ret.size() > 0 && stop_on_first_match) {
            break;
          }
        } else {
          if (it instanceof ISVDBNamedItem && fMatcher.match((ISVDBNamedItem) it, name)) {
            boolean match = (types.length == 0);

            for (SVDBItemType t : types) {
              if (it.getType() == t) {
                match = true;
                break;
              }
            }

            if (match) {
              add(it, Scope.ScopeModIfcClsVars, scope_level);

              if (stop_on_first_match) {
                break;
              }
            }
          }
        }
      }

      if (ret.size() > 0 && stop_on_first_match) {
        break;
      }

      // Next, search the parameters, if we're in a function/task scope
      if (context.getType().isElemOf(SVDBItemType.Function, SVDBItemType.Task)) {
        for (SVDBParamPortDecl p : ((SVDBTask) context).getParams()) {
          for (ISVDBChildItem pi : p.getChildren()) {
            fLog.debug("check param \"" + SVDBItem.getName(pi) + "\"");
            if (fMatcher.match((ISVDBNamedItem) pi, name)) {
              add(pi, Scope.ScopeLocalVars, scope_level);

              if (stop_on_first_match) {
                break;
              }
            }
          }
          if (ret.size() > 0 && stop_on_first_match) {
            break;
          }
        }
      }

      if (ret.size() > 0 && stop_on_first_match) {
        break;
      }

      // Next, if we check the class parameters if we're in a class scope,
      // or the module ports/parameters if we're in an interface/module scope
      fLog.debug("context type: " + context.getType());
      if (context.getType() == SVDBItemType.ClassDecl) {
        //				SVDBClassDecl cls = (SVDBClassDecl)context;

      } else if (context.getType() == SVDBItemType.ModuleDecl
          || context.getType() == SVDBItemType.InterfaceDecl) {
        List<SVDBParamPortDecl> p_list = ((SVDBModIfcDecl) context).getPorts();

        // Check ports
        for (SVDBParamPortDecl p : p_list) {
          for (ISVDBChildItem c : p.getChildren()) {
            SVDBVarDeclItem pi = (SVDBVarDeclItem) c;
            fLog.debug("  Check port " + pi.getName() + " == " + name);
            if (fMatcher.match((ISVDBNamedItem) pi, name)) {
              add(pi, Scope.ScopeModIfcClsVars, scope_level);

              if (ret.size() > 0 && stop_on_first_match) {
                break;
              }
            }
            if (ret.size() > 0 && stop_on_first_match) {
              break;
            }
          }
        }

        // Check parameters
        List<SVDBModIfcClassParam> param_l = ((SVDBModIfcDecl) context).getParameters();
        if (param_l != null && (ret.size() == 0 || !stop_on_first_match)) {
          for (SVDBModIfcClassParam p : param_l) {
            if (fMatcher.match(p, name)) {
              add(p, Scope.ScopeModIfcClsVars, scope_level);
            }

            if (ret.size() > 0 && stop_on_first_match) {
              break;
            }
          }
        }
      }

      if (ret.size() > 0 && stop_on_first_match) {
        break;
      }

      while ((context = context.getParent()) != null && !(context instanceof ISVDBChildParent)) {
        fLog.debug("SKIP: " + context.getType() + " " + SVDBItem.getName(context));
      }

      fLog.debug("parent: " + ((context != null) ? context.getType() : "NULL"));
      scope_level++;
    }

    fLog.debug(
        "<-- find: context="
            + ((context != null) ? SVDBItem.getName(context) : "null")
            + " name="
            + name);

    return ret;
  }