Esempio n. 1
0
  @Override
  public void run() {
    ISearchQuery searchJob = null;

    ISelection selection = getSelection();
    if (selection instanceof IStructuredSelection) {
      Object object = ((IStructuredSelection) selection).getFirstElement();
      if (object instanceof ISourceReference) searchJob = createQuery((ISourceReference) object);
    } else if (selection instanceof ITextSelection) {
      ITextSelection selNode = (ITextSelection) selection;
      ICElement element = fEditor.getInputCElement();
      while (element != null && !(element instanceof ITranslationUnit))
        element = element.getParent();
      if (element != null) {
        if (selNode.getLength() == 0) {
          IDocument document = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
          IRegion reg = CWordFinder.findWord(document, selNode.getOffset());
          selNode = new TextSelection(document, reg.getOffset(), reg.getLength());
        }
        searchJob = createQuery(element, selNode);
      }
    }

    if (searchJob == null) {
      showStatusLineMessage(CSearchMessages.CSearchOperation_operationUnavailable_message);
      return;
    }

    clearStatusLine();
    NewSearchUI.activateSearchResultView();
    NewSearchUI.runQueryInBackground(searchJob);
  }
    /**
     * Searches for element.
     *
     * @param unit Unit to search in.
     * @param delta Delta.
     * @return Found element.
     */
    protected ICElementDelta findElement(ICElement unit, ICElementDelta delta) {
      if (delta == null || unit == null) {
        return null;
      }

      final ICElement element = delta.getElement();

      if (unit.equals(element)) {
        if (isPossibleStructuralChange(delta)) {
          return delta;
        }
        return null;
      }

      if (element.getElementType() > ICElement.C_UNIT) {
        return null;
      }

      final ICElementDelta[] children = delta.getAffectedChildren();
      if (children == null || children.length == 0) {
        return null;
      }

      for (ICElementDelta element2 : children) {
        final ICElementDelta d = findElement(unit, element2);
        if (d != null) {
          return d;
        }
      }

      return null;
    }
Esempio n. 3
0
  /** Set the program name attributes on the working copy based on the ICElement */
  protected void initializeProgramName(ICElement cElement, ILaunchConfigurationWorkingCopy config) {
    boolean renamed = false;

    if (!(cElement instanceof IBinary)) {
      cElement = cElement.getCProject();
    }

    if (cElement instanceof ICProject) {
      IProject project = cElement.getCProject().getProject();
      String name = project.getName();
      ICProjectDescription projDes = CCorePlugin.getDefault().getProjectDescription(project);
      if (projDes != null) {
        String buildConfigName = projDes.getActiveConfiguration().getName();
        name = name + " " + buildConfigName; // $NON-NLS-1$
      }
      name = getLaunchConfigurationDialog().generateName(name);
      config.rename(name);
      renamed = true;
    }

    IBinary binary = null;
    if (cElement instanceof ICProject) {
      IBinary[] bins = getBinaryFiles((ICProject) cElement);
      if (bins != null && bins.length == 1) {
        binary = bins[0];
      }
    } else if (cElement instanceof IBinary) {
      binary = (IBinary) cElement;
    }

    if (binary != null) {
      String path;
      path = binary.getResource().getProjectRelativePath().toOSString();
      config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, path);
      if (!renamed) {
        String name = binary.getElementName();
        int index = name.lastIndexOf('.');
        if (index > 0) {
          name = name.substring(0, index);
        }
        name = getLaunchConfigurationDialog().generateName(name);
        config.rename(name);
        renamed = true;
      }
    }

    if (!renamed) {
      String name =
          getLaunchConfigurationDialog().generateName(cElement.getCProject().getElementName());
      config.rename(name);
    }
  }
Esempio n. 4
0
  /**
   * Run the analysis on the current selection (file, container, or multiple-selection)
   *
   * @param monitor progress monitor on which to report progress.
   * @param indent indent amount, in number of spaces, used only for debug printing.
   * @param includes
   * @return true if any errors were found.
   * @throws InterruptedException
   */
  @SuppressWarnings("unchecked")
  // on Iterator
  protected boolean runResources(IProgressMonitor monitor, int indent, List<String> includes)
      throws InterruptedException {
    boolean foundError = false;
    // First, count files so we know how much work to do.
    // note this is number of files of any type, not necessarily number of
    // files that will be analyzed.
    int count = countFilesSelected();

    monitor.beginTask(Messages.RunAnalyseHandlerBase_29, count);
    if (traceOn) {
      System.out.println("RAHB.runResources(): using selection: " + selection); // $NON-NLS-1$
    }
    // Get elements of a possible multiple selection
    IStructuredSelection lastSel = AnalysisDropdownHandler.getInstance().getLastSelection();
    Iterator<IStructuredSelection> iter = lastSel.iterator(); // fix analysis
    // selection
    // bug
    // 327122

    while (iter.hasNext()) {
      if (monitor.isCanceled()) {
        // this is usually caught here while processing
        // multiple-selection of files
        throw new InterruptedException();
      }
      Object obj = iter.next(); // piece of selection
      // It can be a Project, Folder, File, etc...
      if (obj instanceof IAdaptable) {
        // ICElement covers folders and translationunits
        // If fortran file (*.f*) and Photran not installed, ce is null
        // and we ignore it (first) here.
        final ICElement ce = (ICElement) ((IAdaptable) obj).getAdapter(ICElement.class); // cdt40
        if (ce != null) {
          // cdt40
          // IASTTranslationUnit atu = tu.getAST(); not yet
          boolean err = runResource(monitor, ce, indent, includes);
          if (traceOn) {
            System.out.println(
                "Error (err="
                    + err
                    + ")running analysis on "
                    + ce.getResource().getName()); // $NON-NLS-1$ //$NON-NLS-2$
          }
        }
      }
    }
    monitor.done();
    return foundError;
  }
 @Override
 public void init(ICElement cElement, List<CPElement> cPaths) {
   fCurrCElement = cElement;
   fCurrCProject = cElement.getCProject();
   List<CPElementGroup> elements = createGroups(cElement, cPaths);
   fIncludeSymPathsList.setElements(elements);
   updateStatus();
 }
Esempio n. 6
0
  private void processDelta(ICElementDelta delta, Set<IResource> handled) throws CoreException {
    final int flags = delta.getFlags();

    final boolean hasChildren = (flags & ICElementDelta.F_CHILDREN) != 0;
    if (hasChildren) {
      for (ICElementDelta child : delta.getAffectedChildren()) {
        processDelta(child, handled);
      }
    }

    final ICElement element = delta.getElement();
    switch (element.getElementType()) {
      case ICElement.C_UNIT:
        ITranslationUnit tu = (ITranslationUnit) element;
        if (!tu.isWorkingCopy()) {
          handled.add(element.getResource());
          switch (delta.getKind()) {
            case ICElementDelta.CHANGED:
              if ((flags & ICElementDelta.F_CONTENT) != 0) {
                fChanged.add(tu);
              }
              break;
            case ICElementDelta.ADDED:
              fChanged.add(tu);
              break;
            case ICElementDelta.REMOVED:
              fRemoved.add(tu);
              break;
          }
        }
        break;
      case ICElement.C_CCONTAINER:
        ICContainer folder = (ICContainer) element;
        if (delta.getKind() == ICElementDelta.ADDED) {
          handled.add(element.getResource());
          collectSources(folder, fChanged);
        }
        break;
    }

    if (!sSuppressPotentialTUs) {
      // Always look at the children of the resource delta (bug 343538)
      final IResourceDelta[] rDeltas = delta.getResourceDeltas();
      processResourceDelta(rDeltas, element, handled);
    }
  }
Esempio n. 7
0
 /**
  * Determine if the project is a C++ project
  *
  * @param ce the ICElement representing a file
  * @return
  */
 protected boolean isCPPproject(ICElement ce) {
   IProject p = ce.getCProject().getProject();
   try {
     IProjectNature nature = p.getNature("org.eclipse.cdt.core.ccnature"); // $NON-NLS-1$
     if (nature != null) {
       return true;
     }
   } catch (CoreException e) {
     // TODO Auto-generated catch block
     // e.printStackTrace();
   }
   return false;
 }
    @Override
    public String resolve(TemplateContext context) {
      ICElement element =
          ((TranslationUnitContext) context).findEnclosingElement(ICElement.C_FUNCTION);
      if (element == null) {
        element =
            ((TranslationUnitContext) context)
                .findEnclosingElement(ICElement.C_FUNCTION_DECLARATION);
        if (element == null) {
          element = ((TranslationUnitContext) context).findEnclosingElement(ICElement.C_METHOD);
          if (element == null) {
            element =
                ((TranslationUnitContext) context)
                    .findEnclosingElement(ICElement.C_METHOD_DECLARATION);
          }
        }
      }

      if (element instanceof IFunctionDeclaration) {
        return element.getElementName();
      }
      return null;
    }
    public int category(Object obj) {
      if (obj instanceof ICElement) {
        ICElement elem = (ICElement) obj;
        switch (elem.getElementType()) {
          case ICElement.C_MACRO:
            return 1;
          case ICElement.C_INCLUDE:
            return 2;

          case ICElement.C_CLASS:
            return 3;
          case ICElement.C_STRUCT:
            return 4;
          case ICElement.C_UNION:
            return 5;

          case ICElement.C_FIELD:
            return 6;
          case ICElement.C_FUNCTION:
            return 7;
        }
      }
      return 0;
    }
  private List<CPElementGroup> createGroups(ICElement element, List<CPElement> cPaths) {
    // create resource groups
    List<CPElementGroup> resourceGroups = new ArrayList<CPElementGroup>(5);
    fTopGroup = new CPElementGroup(element.getResource());
    resourceGroups.add(fTopGroup);
    // add containers first so that they appear at top of list
    for (int i = 0; i < cPaths.size(); i++) {
      CPElement cpelement = cPaths.get(i);
      switch (cpelement.getEntryKind()) {
        case IPathEntry.CDT_CONTAINER:
          fTopGroup.addChild(cpelement);
          break;
      }
    }
    for (int i = 0; i < cPaths.size(); i++) {
      CPElement cpelement = cPaths.get(i);
      switch (cpelement.getEntryKind()) {
        case IPathEntry.CDT_INCLUDE:
        case IPathEntry.CDT_INCLUDE_FILE:
        case IPathEntry.CDT_MACRO:
        case IPathEntry.CDT_MACRO_FILE:
          CPElementGroup resGroup = new CPElementGroup(cpelement.getResource());
          int ndx = resourceGroups.indexOf(resGroup);
          if (ndx == -1) {
            resourceGroups.add(resGroup);
          } else {
            resGroup = resourceGroups.get(ndx);
          }
          resGroup.addChild(cpelement);
      }
    }

    // place each path in its appropriate inherited group (or not if
    // excluded)
    for (int i = 0; i < cPaths.size(); i++) {
      CPElement cpelement = cPaths.get(i);
      switch (cpelement.getEntryKind()) {
        case IPathEntry.CDT_INCLUDE:
        case IPathEntry.CDT_INCLUDE_FILE:
        case IPathEntry.CDT_MACRO:
        case IPathEntry.CDT_MACRO_FILE:
          addPathToResourceGroups(cpelement, null, resourceGroups);
      }
    }
    return resourceGroups;
  }
 private String getMethodName(IMethod method) {
   StringBuffer name = new StringBuffer();
   String methodName = method.getElementName();
   ICElement parent = method.getParent();
   while (parent != null
       && (parent.getElementType() == ICElement.C_NAMESPACE
           || parent.getElementType() == ICElement.C_CLASS
           || parent.getElementType() == ICElement.C_STRUCT
           || parent.getElementType() == ICElement.C_UNION)) {
     name.append(parent.getElementName()).append("::"); // $NON-NLS-1$
     parent = parent.getParent();
   }
   name.append(methodName);
   appendParameters(name, method);
   return name.toString();
 }
Esempio n. 12
0
  /**
   * Run analysis on a resource (e.g. File or Folder) Will descend to members of folder
   *
   * @param atu the resource
   * @param indent number of levels of nesting/recursion for prettyprinting
   * @param includes contains header files include paths from the Preference page
   * @return true if an error was encountered
   * @throws InterruptedException
   */
  public boolean runResource(
      IProgressMonitor monitor, ICElement ce, int indent, List<String> includes)
      throws InterruptedException {
    indent += INDENT_INCR;
    ScanReturn results;
    boolean foundError = false;

    if (!monitor.isCanceled()) {
      if (ce instanceof ITranslationUnit) {
        IResource res = ce.getResource(); // null if not part of C
        // project in ws
        // cdt40: eventually shd be able to deal with just tu;
        // tu.getResource() can always work later...
        if (res instanceof IFile) { // shd always be true (but might be
          // null)
          IFile file = (IFile) res;
          String filename = file.getName();
          // String fn2 = ce.getElementName();// shd be filename too
          // cdt40
          boolean cpp = isCPPproject(ce);
          // if (AnalysisUtil.validForAnalysis(filename,cpp)) {

          if (validForAnalysis(filename, cpp)) {
            if (traceOn) {
              println(getSpaces(indent) + "file: " + filename); // $NON-NLS-1$
            }
            results = analyse(monitor, (ITranslationUnit) ce, includes);

            foundError = foundError || results == null || results.wasError();
            if (foundError) {
              int stopHere = 0;
              System.out.println(
                  "found error on " //$NON-NLS-1$
                      + file.getName()
                      + " "
                      + stopHere); //$NON-NLS-1$
            }
            if (traceOn) {
              println(
                  "******** RunAnalyseBase, analysis complete; ScanReturn=" //$NON-NLS-1$
                      + results);
            }
            if (results != null) {
              // apply markers to the file
              processResults(results, file);
            }

          } else {
            if (traceOn) {
              println(getSpaces(indent) + "---omit: not valid file: " + filename); // $NON-NLS-1$
            }
          }
          return foundError;
        }
      } else if (ce instanceof ICContainer) {
        ICContainer container = (ICContainer) ce;
        try {
          ICElement[] mems = container.getChildren();
          for (int i = 0; i < mems.length; i++) {
            if (monitor.isCanceled()) {
              // this is usually hit while processing normal
              // analysis of e.g. container
              throw new InterruptedException();
            }
            boolean err = runResource(monitor, mems[i], indent, includes);
            foundError = foundError || err;
          }
        } catch (CoreException e) {
          e.printStackTrace();
        }

      } else if (ce instanceof ICProject) {
        ICProject proj = (ICProject) ce;
        try {
          ICElement[] mems = proj.getChildren();
          for (int i = 0; i < mems.length; i++) {
            if (monitor.isCanceled()) {
              // this is usually hit while processing normal
              // analysis of e.g. container
              throw new InterruptedException();
            }
            boolean err = runResource(monitor, mems[i], indent, includes);
            foundError = foundError || err;
          }
        } catch (CoreException e) {
          e.printStackTrace();
        }
      }
      // container could be project or folder
    } // end if !monitor.isCanceled()
    else {
      String name = ""; // $NON-NLS-1$
      // cdt40
      name = ce.getElementName();
      // String p=ce.getPath().toString();

      System.out.println(
          "Cancelled by User, aborting analysis on subsequent files... " //$NON-NLS-1$
              + name);
      throw new InterruptedException();
    }

    return foundError;
  }
 @Override
 public String resolve(TemplateContext context) {
   ICElement element = ((TranslationUnitContext) context).findEnclosingElement(fElementType);
   return (element == null) ? null : element.getElementName();
 }