1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 09:25:31 +02:00

Fix and Testcase for 195822, failures navigating in modified buffers.

This commit is contained in:
Markus Schorn 2007-07-10 11:34:12 +00:00
parent 1737387d1f
commit 8c8fddbf72
4 changed files with 87 additions and 24 deletions

View file

@ -10,11 +10,8 @@
* Markus Schorn (Wind River Systems)
* Andrew Ferguson (Symbian)
*******************************************************************************/
/*
* Created on Nov 29, 2004
*/
package org.eclipse.cdt.internal.core.dom.parser.cpp;
package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
@ -1177,7 +1174,10 @@ public class CPPVisitor {
break;
}
} else if( prop == IASTDeclarator.DECLARATOR_NAME ){
IASTNode p = name.getParent().getParent();
IASTNode p = name.getParent();
while (p instanceof IASTDeclarator) {
p= p.getParent();
}
if( p instanceof IASTSimpleDeclaration ){
IASTDeclSpecifier declSpec = ((IASTSimpleDeclaration)p).getDeclSpecifier();
if( declSpec.getStorageClass() == IASTDeclSpecifier.sc_typedef )

View file

@ -32,6 +32,7 @@ import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
@ -41,18 +42,19 @@ import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.FileManager;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.tests.BaseUITestCase;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
@ -175,7 +177,7 @@ public class CPPSelectionTestsNoIndexer extends BaseUITestCase {
//Obtain file handle
IFile file = project.getProject().getFile(fileName);
IPath location = new Path(project.getLocation().removeLastSegments(1).toOSString() + File.separator + fileName); //$NON-NLS-1$
IPath location = new Path(project.getLocation().removeLastSegments(1).toOSString() + File.separator + fileName);
File linkFile = new File(location.toOSString());
if (!linkFile.exists()) {
@ -237,8 +239,9 @@ public class CPPSelectionTestsNoIndexer extends BaseUITestCase {
assertFalse(true);
}
if (part instanceof AbstractTextEditor) {
((AbstractTextEditor)part).getSelectionProvider().setSelection(new TextSelection(offset,length));
if (part instanceof ITextEditor) {
ITextEditor editor= (ITextEditor) part;
editor.getSelectionProvider().setSelection(new TextSelection(offset,length));
final OpenDeclarationsAction action = (OpenDeclarationsAction) ((AbstractTextEditor)part).getAction("OpenDeclarations"); //$NON-NLS-1$
action.runSync();
@ -249,7 +252,7 @@ public class CPPSelectionTestsNoIndexer extends BaseUITestCase {
final IASTName[] result= {null};
if (sel instanceof ITextSelection) {
final ITextSelection textSel = (ITextSelection)sel;
ITranslationUnit tu = (ITranslationUnit)CoreModel.getDefault().create(file);
ITranslationUnit tu= CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editor.getEditorInput());
IStatus ok= ASTProvider.getASTProvider().runOnAST(tu, ASTProvider.WAIT_YES, monitor, new ASTRunnable() {
public IStatus runOnAST(ILanguage language, IASTTranslationUnit ast) throws CoreException {
IASTName[] names = language.getSelectedNames(ast, textSel.getOffset(), textSel.getLength());
@ -921,7 +924,7 @@ public class CPPSelectionTestsNoIndexer extends BaseUITestCase {
String code = buffer.toString();
IFile file = importFileWithLink("testBug78354.cpp", code); //$NON-NLS-1$
int offset = code.indexOf("TestTypeOne myFirstLink = 5;"); //$NON-NLS-1$ //$NON-NLS-2$
int offset = code.indexOf("TestTypeOne myFirstLink = 5;"); //$NON-NLS-1$
IASTNode decl = testF3(file, offset);
assertTrue(decl instanceof IASTName);
assertEquals(((IASTName)decl).toString(), "TestTypeOne"); //$NON-NLS-1$
@ -966,4 +969,40 @@ public class CPPSelectionTestsNoIndexer extends BaseUITestCase {
assertEquals(((ASTNode)decl).getOffset(), 4);
assertEquals(((ASTNode)decl).getLength(), 1);
}
// typedef int (*functionPointer)(int);
// functionPointer fctVariable;
// typedef int (functionPointerArray[2])(int);
// functionPointerArray fctVariablArray;
public void testBug195822() throws Exception {
StringBuffer[] contents= getContentsForTest(2);
String code= contents[0].toString();
String appendCode= contents[1].toString();
String[] filenames= {"testBug195822.c", "testBug195822.cpp"};
for (int i=0; i<2; i++) {
IFile file = importFile(filenames[i], code);
int od1 = code.indexOf("functionPointer");
int or1 = code.indexOf("functionPointer", od1+1);
IASTNode decl = testF3(file, or1);
assertTrue(decl instanceof IASTName);
assertEquals(((IASTName)decl).toString(), "functionPointer"); //$NON-NLS-1$
assertEquals(((ASTNode)decl).getOffset(), od1);
IEditorPart editor= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
assertNotNull(editor);
assertTrue(editor instanceof ITextEditor);
IDocument doc= ((ITextEditor) editor).getDocumentProvider().getDocument(editor.getEditorInput());
doc.replace(doc.getLength(), 0, appendCode);
int od2 = appendCode.indexOf("functionPointerArray");
int or2 = appendCode.indexOf("functionPointerArray", od2+1);
decl = testF3(file, code.length() + or2);
assertTrue(decl instanceof IASTName);
assertEquals(((IASTName)decl).toString(), "functionPointerArray");
assertEquals(((ASTNode)decl).getOffset(), code.length() + od2);
}
}
}

View file

@ -23,7 +23,9 @@ import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.Region;
import org.eclipse.swt.widgets.Display;
import org.eclipse.cdt.core.CCorePlugin;
@ -39,6 +41,7 @@ import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.index.IIndex;
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.ICProject;
import org.eclipse.cdt.core.model.ILanguage;
@ -51,6 +54,8 @@ import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
import org.eclipse.cdt.internal.core.model.ext.CElementHandleFactory;
import org.eclipse.cdt.internal.core.model.ext.ICElementHandle;
import org.eclipse.cdt.internal.ui.actions.OpenActionUtil;
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
@ -222,7 +227,7 @@ public class OpenDeclarationsAction extends SelectionParseAction {
final ArrayList elements= new ArrayList();
for (int i = 0; i < declNames.length; i++) {
try {
ICElement elem = IndexUI.getCElementForName(project, index, declNames[i]);
ICElement elem = getCElementForName(project, index, declNames[i]);
if (elem instanceof ISourceReference) {
elements.add(elem);
}
@ -266,6 +271,27 @@ public class OpenDeclarationsAction extends SelectionParseAction {
return true;
}
private ICElementHandle getCElementForName(ICProject project, IIndex index, IName declName)
throws CoreException {
if (declName instanceof IIndexName) {
return IndexUI.getCElementForName(project, index, (IIndexName) declName);
}
if (declName instanceof IASTName) {
IASTName astName = (IASTName) declName;
IBinding binding= astName.resolveBinding();
if (binding != null) {
ITranslationUnit tu= IndexUI.getTranslationUnit(project, astName);
if (tu != null) {
IASTFileLocation loc= astName.getFileLocation();
IRegion region= new Region(loc.getNodeOffset(), loc.getNodeLength());
return CElementHandleFactory.create(tu, binding, astName.isDefinition(), region, 0);
}
}
return null;
}
return null;
}
private IName[] findNames(IIndex index, IASTTranslationUnit ast,
boolean isDefinition, IBinding binding) throws CoreException {
IName[] declNames= isDefinition ?

View file

@ -258,19 +258,17 @@ public class IndexUI {
return EMPTY_ELEMENTS;
}
public static ICElementHandle getCElementForName(ICProject preferProject, IIndex index, IName declName)
throws CoreException {
if (declName instanceof IASTName) {
return getCElementForName(preferProject, index, (IASTName) declName);
}
else if (declName instanceof IIndexName) {
return getCElementForName(preferProject, index, (IIndexName) declName);
}
return null;
}
/**
* Creates CElementHandles for definitions or declarations when you expect to find those
* in the index.
* @param preferProject
* @param index
* @param declName
* @return the ICElementHandle or <code>null</code>.
*/
public static ICElementHandle getCElementForName(ICProject preferProject, IIndex index, IASTName declName)
throws CoreException {
assert !declName.isReference();
IBinding binding= declName.resolveBinding();
if (binding != null) {
ITranslationUnit tu= getTranslationUnit(preferProject, declName);
@ -289,7 +287,7 @@ public class IndexUI {
return null;
}
private static ITranslationUnit getTranslationUnit(ICProject cproject, IName name) {
public static ITranslationUnit getTranslationUnit(ICProject cproject, IName name) {
IPath path= Path.fromOSString(name.getFileLocation().getFileName());
try {
return CoreModelUtil.findTranslationUnitForLocation(path, cproject);