From 8c8fddbf72cb212505a0ca7e01122bf7f533eed2 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Tue, 10 Jul 2007 11:34:12 +0000 Subject: [PATCH] Fix and Testcase for 195822, failures navigating in modified buffers. --- .../core/dom/parser/cpp/CPPVisitor.java | 10 ++-- .../selection/CPPSelectionTestsNoIndexer.java | 51 ++++++++++++++++--- .../actions/OpenDeclarationsAction.java | 28 +++++++++- .../cdt/internal/ui/viewsupport/IndexUI.java | 22 ++++---- 4 files changed, 87 insertions(+), 24 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java index a0bf57c3354..19b7bf2144c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java @@ -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 ) diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java index 9a51b68c9b0..ea52ca30e3b 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CPPSelectionTestsNoIndexer.java @@ -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); + } + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java index 7ae8fc0cb37..b18f191f9fd 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java @@ -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 ? diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/IndexUI.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/IndexUI.java index 2190e8193c5..3aa0817419d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/IndexUI.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/IndexUI.java @@ -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 null. + */ 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);