1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Fix for 183941, call hierarchy opened with method declaration.

This commit is contained in:
Markus Schorn 2007-05-09 12:15:59 +00:00
parent 6c161d4b54
commit a469ebf89d
10 changed files with 251 additions and 131 deletions

View file

@ -53,7 +53,7 @@ public class IndexTypeReference extends TypeReference {
long timestamp= tu.getResource() != null ? tu.getResource().getLocalTimeStamp() : 0; long timestamp= tu.getResource() != null ? tu.getResource().getLocalTimeStamp() : 0;
IRegion region= new Region(getOffset(), getLength()); IRegion region= new Region(getOffset(), getLength());
try { try {
return CElementHandleFactory.create(tu, binding, region, timestamp); return CElementHandleFactory.create(tu, binding, true, region, timestamp);
} catch (CoreException exc) { } catch (CoreException exc) {
} catch (DOMException exc) { } catch (DOMException exc) {
} }

View file

@ -44,7 +44,7 @@ import org.eclipse.jface.text.IRegion;
public class CElementHandleFactory { public class CElementHandleFactory {
private CElementHandleFactory() {} private CElementHandleFactory() {}
public static ICElementHandle create(ITranslationUnit tu, IBinding binding, public static ICElementHandle create(ITranslationUnit tu, IBinding binding, boolean definition,
IRegion region, long timestamp) throws CoreException, DOMException { IRegion region, long timestamp) throws CoreException, DOMException {
ICElement parentElement= create(tu, binding.getScope()); ICElement parentElement= create(tu, binding.getScope());
@ -54,10 +54,14 @@ public class CElementHandleFactory {
CElementHandle element= null; CElementHandle element= null;
if (binding instanceof ICPPMethod) { if (binding instanceof ICPPMethod) {
element= new MethodHandle(parentElement, (ICPPMethod) binding); element= definition
} ? new MethodHandle(parentElement, (ICPPMethod) binding)
: new MethodDeclarationHandle(parentElement, (ICPPMethod) binding);
}
else if (binding instanceof IFunction) { else if (binding instanceof IFunction) {
element= new FunctionHandle(parentElement, (IFunction) binding); element= definition
? new FunctionHandle(parentElement, (IFunction) binding)
: new FunctionDeclarationHandle(parentElement, (IFunction) binding);
} }
else if (binding instanceof IField) { else if (binding instanceof IField) {
element= new FieldHandle(parentElement, (IField) binding); element= new FieldHandle(parentElement, (IField) binding);

View file

@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2006, 2007 Wind River Systems, Inc. and others.
* 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:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.model.ext;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IFunctionDeclaration;
import org.eclipse.cdt.internal.core.model.FunctionDeclaration;
public class FunctionDeclarationHandle extends CElementHandle implements org.eclipse.cdt.core.model.IFunctionDeclaration {
private String[] fParameterTypes;
private boolean fIsStatic;
public FunctionDeclarationHandle(ICElement parent, IFunction func) throws DOMException {
this(parent, ICElement.C_FUNCTION_DECLARATION, func);
}
protected FunctionDeclarationHandle(ICElement parent, int type, IFunction func) throws DOMException {
super(parent, type, func.getName());
fParameterTypes= extractParameterTypes(func);
try {
fIsStatic= func.isStatic();
} catch (DOMException e) {
CCorePlugin.log(e);
fIsStatic= false;
}
}
public boolean equals(Object obj) {
if (obj instanceof IFunctionDeclaration) {
return FunctionDeclaration.equals(this, (IFunctionDeclaration) obj);
}
return false;
}
public int getNumberOfParameters() {
return fParameterTypes.length;
}
public String[] getParameterTypes() {
return fParameterTypes;
}
public String getSignature() throws CModelException {
return FunctionDeclaration.getSignature(this);
}
public boolean isStatic() throws CModelException {
return fIsStatic;
}
}

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2007 Wind River Systems, Inc. and others. * Copyright (c) 2007 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -11,50 +11,13 @@
package org.eclipse.cdt.internal.core.model.ext; package org.eclipse.cdt.internal.core.model.ext;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IFunction; import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IFunctionDeclaration;
import org.eclipse.cdt.internal.core.model.FunctionDeclaration;
public class FunctionHandle extends CElementHandle implements org.eclipse.cdt.core.model.IFunction { public class FunctionHandle extends FunctionDeclarationHandle implements org.eclipse.cdt.core.model.IFunction {
private String[] fParameterTypes;
private boolean fIsStatic;
public FunctionHandle(ICElement parent, IFunction func) throws DOMException { public FunctionHandle(ICElement parent, IFunction func) throws DOMException {
super(parent, ICElement.C_FUNCTION, func.getName()); super(parent, ICElement.C_FUNCTION, func);
fParameterTypes= extractParameterTypes(func);
try {
fIsStatic= func.isStatic();
} catch (DOMException e) {
CCorePlugin.log(e);
fIsStatic= false;
}
}
public boolean equals(Object obj) {
if (obj instanceof IFunctionDeclaration) {
return FunctionDeclaration.equals(this, (IFunctionDeclaration) obj);
}
return false;
}
public int getNumberOfParameters() {
return fParameterTypes.length;
}
public String[] getParameterTypes() {
return fParameterTypes;
}
public String getSignature() throws CModelException {
return FunctionDeclaration.getSignature(this);
}
public boolean isStatic() throws CModelException {
return fIsStatic;
} }
} }

View file

@ -0,0 +1,85 @@
/*******************************************************************************
* Copyright (c) 2006, 2007 Wind River Systems, Inc. and others.
* 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:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.model.ext;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IMethodDeclaration;
import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;
import org.eclipse.cdt.internal.core.model.FunctionDeclaration;
import org.eclipse.cdt.internal.core.model.MethodDeclaration;
public class MethodDeclarationHandle extends CElementHandle implements IMethodDeclaration {
private String[] fParameterTypes;
private ASTAccessVisibility fVisibility;
private boolean fIsStatic;
private boolean fIsConstructor;
private boolean fIsDestructor;
public MethodDeclarationHandle(ICElement parent, ICPPMethod method) throws DOMException {
this(parent, ICElement.C_METHOD_DECLARATION, method);
}
protected MethodDeclarationHandle(ICElement parent, int type, ICPPMethod method) throws DOMException {
super(parent, type, method.getName());
fParameterTypes= extractParameterTypes(method);
fVisibility= getVisibility(method);
try {
fIsStatic= method.isStatic();
fIsConstructor= method instanceof ICPPConstructor;
if (!fIsConstructor)
fIsDestructor= method.isDestructor();
} catch (DOMException e) {
CCorePlugin.log(e);
}
}
public boolean equals(Object obj) {
if (obj instanceof IMethodDeclaration) {
return MethodDeclaration.equals(this, (IMethodDeclaration) obj);
}
return false;
}
public int getNumberOfParameters() {
return fParameterTypes.length;
}
public String[] getParameterTypes() {
return fParameterTypes;
}
public String getSignature() throws CModelException {
return FunctionDeclaration.getSignature(this);
}
public boolean isStatic() throws CModelException {
return fIsStatic;
}
public ASTAccessVisibility getVisibility() throws CModelException {
return fVisibility;
}
public boolean isConstructor() throws CModelException {
return fIsConstructor;
}
public boolean isDestructor() throws CModelException {
return fIsDestructor;
}
}

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2007 Wind River Systems, Inc. and others. * Copyright (c) 2007 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -11,72 +11,14 @@
package org.eclipse.cdt.internal.core.model.ext; package org.eclipse.cdt.internal.core.model.ext;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IMethod; import org.eclipse.cdt.core.model.IMethod;
import org.eclipse.cdt.core.model.IMethodDeclaration;
import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;
import org.eclipse.cdt.internal.core.model.FunctionDeclaration;
import org.eclipse.cdt.internal.core.model.MethodDeclaration;
public class MethodHandle extends CElementHandle implements IMethod { public class MethodHandle extends MethodDeclarationHandle implements IMethod {
private String[] fParameterTypes;
private ASTAccessVisibility fVisibility;
private boolean fIsStatic;
private boolean fIsConstructor;
private boolean fIsDestructor;
public MethodHandle(ICElement parent, ICPPMethod method) throws DOMException { public MethodHandle(ICElement parent, ICPPMethod method) throws DOMException {
super(parent, ICElement.C_METHOD, method.getName()); super(parent, ICElement.C_METHOD, method);
fParameterTypes= extractParameterTypes(method);
fVisibility= getVisibility(method);
try {
fIsStatic= method.isStatic();
fIsConstructor= method instanceof ICPPConstructor;
if (!fIsConstructor)
fIsDestructor= method.isDestructor();
} catch (DOMException e) {
CCorePlugin.log(e);
}
}
public boolean equals(Object obj) {
if (obj instanceof IMethodDeclaration) {
return MethodDeclaration.equals(this, (IMethodDeclaration) obj);
}
return false;
}
public int getNumberOfParameters() {
return fParameterTypes.length;
}
public String[] getParameterTypes() {
return fParameterTypes;
}
public String getSignature() throws CModelException {
return FunctionDeclaration.getSignature(this);
}
public boolean isStatic() throws CModelException {
return fIsStatic;
}
public ASTAccessVisibility getVisibility() throws CModelException {
return fVisibility;
}
public boolean isConstructor() throws CModelException {
return fIsConstructor;
}
public boolean isDestructor() throws CModelException {
return fIsDestructor;
} }
} }

View file

@ -20,7 +20,6 @@ import org.eclipse.jface.text.ITextSelection;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.ui.texteditor.ITextEditor;
@ -34,31 +33,51 @@ import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.c.ICExternalBinding; import org.eclipse.cdt.core.dom.ast.c.ICExternalBinding;
import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexManager; import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.util.CElementBaseLabels; import org.eclipse.cdt.core.model.util.CElementBaseLabels;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.core.model.ext.ICElementHandle;
import org.eclipse.cdt.internal.corext.util.CModelUtil;
import org.eclipse.cdt.internal.ui.actions.OpenActionUtil; import org.eclipse.cdt.internal.ui.actions.OpenActionUtil;
import org.eclipse.cdt.internal.ui.util.ExceptionHandler; import org.eclipse.cdt.internal.ui.util.ExceptionHandler;
import org.eclipse.cdt.internal.ui.util.StatusLineHandler; import org.eclipse.cdt.internal.ui.util.StatusLineHandler;
import org.eclipse.cdt.internal.ui.viewsupport.IndexUI; import org.eclipse.cdt.internal.ui.viewsupport.IndexUI;
public class CallHierarchyUI { public class CallHierarchyUI {
private static final ICElement[] NO_ELEMENTS = {};
private static boolean sIsJUnitTest= false; private static boolean sIsJUnitTest= false;
public static void setIsJUnitTest(boolean val) { public static void setIsJUnitTest(boolean val) {
sIsJUnitTest= val; sIsJUnitTest= val;
} }
public static CHViewPart open(ICElement input, IWorkbenchWindow window) { public static void open(final IWorkbenchWindow window, final ICElement input) {
if (input != null) { if (input != null) {
return openInViewPart(window, input); final Display display= Display.getCurrent();
Job job= new Job(CHMessages.CallHierarchyUI_label) {
protected IStatus run(IProgressMonitor monitor) {
final ICElement[] elems= findDefinitions(input);
if (elems != null && elems.length > 0) {
display.asyncExec(new Runnable() {
public void run() {
internalOpen(window, elems);
}});
}
return Status.OK_STATUS;
}
};
job.setUser(true);
job.schedule();
} }
return null;
} }
private static CHViewPart openInViewPart(IWorkbenchWindow window, ICElement input) { private static CHViewPart internalOpen(IWorkbenchWindow window, ICElement input) {
IWorkbenchPage page= window.getActivePage(); IWorkbenchPage page= window.getActivePage();
try { try {
CHViewPart result= (CHViewPart)page.showView(CUIPlugin.ID_CALL_HIERARCHY); CHViewPart result= (CHViewPart)page.showView(CUIPlugin.ID_CALL_HIERARCHY);
@ -70,9 +89,7 @@ public class CallHierarchyUI {
return null; return null;
} }
private static CHViewPart openInViewPart(IWorkbenchSite site, ICElement[] input) { private static CHViewPart internalOpen(IWorkbenchWindow window, ICElement[] input) {
IWorkbenchWindow window = site.getWorkbenchWindow();
StatusLineHandler.clearStatusLine(site);
ICElement elem = null; ICElement elem = null;
switch (input.length) { switch (input.length) {
case 0: case 0:
@ -82,7 +99,7 @@ public class CallHierarchyUI {
break; break;
default: default:
if (sIsJUnitTest) { if (sIsJUnitTest) {
throw new RuntimeException("ambigous input"); //$NON-NLS-1$ throw new RuntimeException("ambiguous input"); //$NON-NLS-1$
} }
elem = OpenActionUtil.selectCElement(input, window.getShell(), elem = OpenActionUtil.selectCElement(input, window.getShell(),
CHMessages.CallHierarchyUI_label, CHMessages.CallHierarchyUI_selectMessage, CHMessages.CallHierarchyUI_label, CHMessages.CallHierarchyUI_selectMessage,
@ -90,11 +107,8 @@ public class CallHierarchyUI {
break; break;
} }
if (elem != null) { if (elem != null) {
return openInViewPart(window, elem); return internalOpen(window, elem);
} else { }
StatusLineHandler.showStatusLineMessage(site,
CHMessages.CallHierarchyUI_openFailureMessage);
}
return null; return null;
} }
@ -111,10 +125,10 @@ public class CallHierarchyUI {
try { try {
StatusLineHandler.clearStatusLine(editor.getSite()); StatusLineHandler.clearStatusLine(editor.getSite());
final ICElement[] elems= findDefinitions(project, editorInput, sel); final ICElement[] elems= findDefinitions(project, editorInput, sel);
if (elems != null && elems.length > 0) { if (elems.length > 0) {
display.asyncExec(new Runnable() { display.asyncExec(new Runnable() {
public void run() { public void run() {
openInViewPart(editor.getSite(), elems); internalOpen(editor.getSite().getWorkbenchWindow(), elems);
}}); }});
} else { } else {
StatusLineHandler.showStatusLineMessage(editor.getSite(), StatusLineHandler.showStatusLineMessage(editor.getSite(),
@ -133,7 +147,8 @@ public class CallHierarchyUI {
} }
} }
private static ICElement[] findDefinitions(ICProject project, IEditorInput editorInput, ITextSelection sel) throws CoreException { private static ICElement[] findDefinitions(ICProject project, IEditorInput editorInput, ITextSelection sel)
throws CoreException {
try { try {
IIndex index= CCorePlugin.getIndexManager().getIndex(project, IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_DEPENDENT); IIndex index= CCorePlugin.getIndexManager().getIndex(project, IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_DEPENDENT);
@ -175,8 +190,50 @@ public class CallHierarchyUI {
CUIPlugin.getDefault().log(e); CUIPlugin.getDefault().log(e);
} }
catch (InterruptedException e) { catch (InterruptedException e) {
Thread.currentThread().interrupt();
} }
return null; return NO_ELEMENTS;
}
public static ICElement[] findDefinitions(ICElement input) {
try {
final ITranslationUnit tu= CModelUtil.getTranslationUnit(input);
if (tu != null) {
final ICProject project= tu.getCProject();
final IIndex index= CCorePlugin.getIndexManager().getIndex(project, IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_DEPENDENT);
index.acquireReadLock();
try {
IBinding binding= IndexUI.elementToBinding(index, input);
if (binding != null) {
ICElement[] result= IndexUI.findAllDefinitions(index, binding);
if (result.length > 0) {
return result;
}
IIndexName name= IndexUI.elementToName(index, input);
if (name != null) {
ICElementHandle handle= IndexUI.getCElementForName(tu, index, name);
return new ICElement[] {handle};
}
}
}
finally {
if (index != null) {
index.releaseReadLock();
}
}
}
}
catch (CoreException e) {
CUIPlugin.getDefault().log(e);
}
catch (DOMException e) {
CUIPlugin.getDefault().log(e);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return new ICElement[] {input};
} }
public static boolean isRelevantForCallHierarchy(IBinding binding) { public static boolean isRelevantForCallHierarchy(IBinding binding) {

View file

@ -51,7 +51,7 @@ public class OpenCallHierarchyAction extends SelectionDispatchAction {
Object selectedObject= selection.getFirstElement(); Object selectedObject= selection.getFirstElement();
ICElement elem= (ICElement) getAdapter(selectedObject, ICElement.class); ICElement elem= (ICElement) getAdapter(selectedObject, ICElement.class);
if (elem != null) { if (elem != null) {
CallHierarchyUI.open(elem, getSite().getWorkbenchWindow()); CallHierarchyUI.open(getSite().getWorkbenchWindow(), elem);
} }
} }
} }

View file

@ -63,7 +63,7 @@ public class OpenElementInCallHierarchyAction implements IWorkbenchWindowActionD
MessageDialog.openError(getShell(), title, message); MessageDialog.openError(getShell(), title, message);
} }
else { else {
CallHierarchyUI.open(elements[0], fWorkbenchWindow); CallHierarchyUI.open(fWorkbenchWindow, elements[0]);
} }
} }

View file

@ -158,7 +158,7 @@ public class IndexUI {
ArrayList result= new ArrayList(); ArrayList result= new ArrayList();
for (int i = 0; i < defs.length; i++) { for (int i = 0; i < defs.length; i++) {
IIndexName in = defs[i]; IIndexName in = defs[i];
ICElementHandle definition= getCElementForName(null, index, in); ICElementHandle definition= getCElementForName((ICProject) null, index, in);
if (definition != null) { if (definition != null) {
result.add(definition); result.add(definition);
} }
@ -183,7 +183,7 @@ public class IndexUI {
if (converter != null) { if (converter != null) {
region= converter.actualToHistoric(region); region= converter.actualToHistoric(region);
} }
return CElementHandleFactory.create(tu, binding, region, timestamp); return CElementHandleFactory.create(tu, binding, declName.isDefinition(), region, timestamp);
} }
} }
return null; return null;
@ -203,13 +203,18 @@ public class IndexUI {
assert !declName.isReference(); assert !declName.isReference();
ITranslationUnit tu= getTranslationUnit(preferProject, declName); ITranslationUnit tu= getTranslationUnit(preferProject, declName);
if (tu != null) { if (tu != null) {
IRegion region= new Region(declName.getNodeOffset(), declName.getNodeLength()); return getCElementForName(tu, index, declName);
long timestamp= declName.getFile().getTimestamp();
return CElementHandleFactory.create(tu, index.findBinding(declName), region, timestamp);
} }
return null; return null;
} }
public static ICElementHandle getCElementForName(ITranslationUnit tu, IIndex index, IIndexName declName) throws CoreException,
DOMException {
IRegion region= new Region(declName.getNodeOffset(), declName.getNodeLength());
long timestamp= declName.getFile().getTimestamp();
return CElementHandleFactory.create(tu, index.findBinding(declName), declName.isDefinition(), region, timestamp);
}
public static ICElementHandle findAnyDeclaration(IIndex index, ICProject preferProject, IBinding binding) throws CoreException, DOMException { public static ICElementHandle findAnyDeclaration(IIndex index, ICProject preferProject, IBinding binding) throws CoreException, DOMException {
if (binding != null) { if (binding != null) {
IIndexName[] names= index.findNames(binding, IIndex.FIND_DECLARATIONS); IIndexName[] names= index.findNames(binding, IIndex.FIND_DECLARATIONS);