From 6be8db253cdf7cbe0489b9f7520e297700fd2503 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Thu, 6 Mar 2008 09:57:24 +0000 Subject: [PATCH] Index based name-resolution for K&R-style functions, bug 221635. --- .../tests/IndexCBindingResolutionBugs.java | 21 ++++++++++++ .../internal/core/dom/parser/c/CVisitor.java | 12 ++++++- .../selection/BaseSelectionTestsIndexer.java | 1 + .../selection/CPPSelectionTestsNoIndexer.java | 1 + .../selection/CSelectionTestsAnyIndexer.java | 33 +++++++++++++++++++ .../selection/CSelectionTestsNoIndexer.java | 21 ++++++++++++ .../actions/OpenDeclarationsAction.java | 3 +- 7 files changed, 90 insertions(+), 2 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCBindingResolutionBugs.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCBindingResolutionBugs.java index d25ce8255e8..d7168f6dc2e 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCBindingResolutionBugs.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCBindingResolutionBugs.java @@ -371,4 +371,25 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase assertTrue(outer instanceof ICCompositeTypeScope); assertEquals("outer", outer.getScopeName().toString()); } + + // int myFunc(); + + // int myFunc(var) + // int var; + // { + // return var; + // } + // int main(void) { + // return myFunc(0); + // } + public void testKRStyleFunction_Bug216791() throws DOMException { + // struct + IBinding b = getBindingFromASTName("myFunc(", 6); + assertTrue(b instanceof IFunction); + IFunction f= (IFunction) b; + IParameter[] params= f.getParameters(); + assertEquals(1, params.length); + assertTrue(params[0].getType() instanceof IBasicType); + assertEquals(IBasicType.t_int, ((IBasicType)params[0].getType()).getType()); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java index 79f4ae9df62..a064428a6d3 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java @@ -117,6 +117,7 @@ public class CVisitor { { shouldVisitNames = true; } + @Override public int visit(IASTName name) { if ( name.getBinding() != null ) { try { @@ -180,6 +181,7 @@ public class CVisitor { /* (non-Javadoc) * @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclaration) */ + @Override public int visit(IASTDeclaration declaration) { if ( declaration instanceof IASTProblemHolder ) addProblem(((IASTProblemHolder)declaration).getProblem()); @@ -190,6 +192,7 @@ public class CVisitor { /* (non-Javadoc) * @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processExpression(org.eclipse.cdt.core.dom.ast.IASTExpression) */ + @Override public int visit(IASTExpression expression) { if ( expression instanceof IASTProblemHolder ) addProblem(((IASTProblemHolder)expression).getProblem()); @@ -200,6 +203,7 @@ public class CVisitor { /* (non-Javadoc) * @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processStatement(org.eclipse.cdt.core.dom.ast.IASTStatement) */ + @Override public int visit(IASTStatement statement) { if ( statement instanceof IASTProblemHolder ) addProblem(((IASTProblemHolder)statement).getProblem()); @@ -210,6 +214,7 @@ public class CVisitor { /* (non-Javadoc) * @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processTypeId(org.eclipse.cdt.core.dom.ast.IASTTypeId) */ + @Override public int visit(IASTTypeId typeId) { if ( typeId instanceof IASTProblemHolder ) addProblem(((IASTProblemHolder)typeId).getProblem()); @@ -269,6 +274,7 @@ public class CVisitor { /* (non-Javadoc) * @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processDeclarator(org.eclipse.cdt.core.dom.ast.IASTDeclarator) */ + @Override public int visit(IASTDeclarator declarator) { //GCC allows declarations in expressions, so we have to continue from the //declarator in case there is something in the initializer expression @@ -308,6 +314,7 @@ public class CVisitor { /* (non-Javadoc) * @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processDeclSpecifier(org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier) */ + @Override public int visit(IASTDeclSpecifier declSpec) { if ( compositeTypeDeclared && declSpec instanceof ICASTTypedefNameSpecifier ) return PROCESS_CONTINUE; @@ -345,6 +352,7 @@ public class CVisitor { /* (non-Javadoc) * @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processEnumerator(org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator) */ + @Override public int visit(IASTEnumerator enumerator) { if( binding instanceof IEnumerator && enumerator.getName().resolveBinding() == binding ){ addName( enumerator.getName() ); @@ -356,6 +364,7 @@ public class CVisitor { /* (non-Javadoc) * @see org.eclipse.cdt.internal.core.dom.parser.c.CVisitor.CBaseVisitorAction#processStatement(org.eclipse.cdt.core.dom.ast.IASTStatement) */ + @Override public int visit(IASTStatement statement) { if ( statement instanceof IASTLabelStatement && binding instanceof ILabel ){ if ( ((IASTLabelStatement)statement).getName().resolveBinding() == binding ) @@ -395,6 +404,7 @@ public class CVisitor { kind = KIND_OBJ_FN; } + @Override public int visit( IASTName name ){ ASTNodeProperty prop = name.getPropertyInParent(); switch( kind ){ @@ -811,7 +821,7 @@ public class CVisitor { IASTNode parent = declarator.getParent(); if ( CharArrayUtils.equals(declarator.getName().toCharArray(), name.toCharArray()) ){ binding = resolveBinding( parent, CURRENT_SCOPE ); - if( binding != null ) { + if( binding != null && binding instanceof IIndexBinding == false) { if( binding instanceof ICInternalFunction ) ((ICInternalFunction)binding).addDeclarator( (ICASTKnRFunctionDeclarator) declarator ); else diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/BaseSelectionTestsIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/BaseSelectionTestsIndexer.java index d17454d6f25..ff1fcd615e2 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/BaseSelectionTestsIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/BaseSelectionTestsIndexer.java @@ -77,6 +77,7 @@ public class BaseSelectionTestsIndexer extends BaseUITestCase { protected void setUp() throws Exception { super.setUp(); OpenDeclarationsAction.sIsJUnitTest= true; + OpenDeclarationsAction.sAllowFallback= false; } public void waitForIndex(int maxSec) throws Exception { 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 21e216af613..9db50c1c939 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 @@ -145,6 +145,7 @@ public class CPPSelectionTestsNoIndexer extends BaseUITestCase { super.setUp(); initProject(); OpenDeclarationsAction.sIsJUnitTest= true; + OpenDeclarationsAction.sAllowFallback= false; } protected void tearDown() throws Exception { diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CSelectionTestsAnyIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CSelectionTestsAnyIndexer.java index 8fef004739a..84a6917dab4 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CSelectionTestsAnyIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CSelectionTestsAnyIndexer.java @@ -33,6 +33,8 @@ import org.eclipse.cdt.ui.testplugin.CTestPlugin; import org.eclipse.cdt.internal.core.dom.parser.ASTNode; +import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction; + public abstract class CSelectionTestsAnyIndexer extends BaseSelectionTestsIndexer { private static final int MAX_WAIT_TIME = 8000; @@ -503,6 +505,8 @@ public abstract class CSelectionTestsAnyIndexer extends BaseSelectionTestsIndexe // #define DR_ACCESS_FNS(DR) public void testNavigationInMacroDefinition_Bug102643() throws Exception { + OpenDeclarationsAction.sAllowFallback= true; + StringBuffer[] buffers= getContents(2); String hcode= buffers[0].toString(); String scode= buffers[1].toString(); @@ -517,5 +521,34 @@ public abstract class CSelectionTestsAnyIndexer extends BaseSelectionTestsIndexe IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); IEditorInput input = part.getEditorInput(); assertEquals("source.cpp", ((FileEditorInput)input).getFile().getName()); + } + + // int myFunc(); + + // int myFunc(var) + // int var; + // { + // return var; + // } + // + // int main(void) + // { + // return myFunc(0); + // } + public void testKRstyleFunctions_Bug221635() throws Exception { + final StringBuffer[] contents = getContentsForTest(2); + String hcode= contents[0].toString(); + String code= contents[1].toString(); + IFile hfile = importFile("aheader.h", hcode); + IFile file = importFile("source.c", code); + int offset= code.indexOf("myFunc(0)"); + TestSourceReader.waitUntilFileIsIndexed(index, file, MAX_WAIT_TIME); + + IASTNode decl= testF3(file, offset); + assertTrue(decl instanceof IASTName); + final IASTName name = (IASTName) decl; + assertTrue(name.isDefinition()); + assertEquals("myFunc", name.toString()); } + } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CSelectionTestsNoIndexer.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CSelectionTestsNoIndexer.java index 8a6a92f345b..c1986578c03 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CSelectionTestsNoIndexer.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/selection/CSelectionTestsNoIndexer.java @@ -118,6 +118,7 @@ public class CSelectionTestsNoIndexer extends BaseUITestCase { protected void setUp() throws Exception { super.setUp(); OpenDeclarationsAction.sIsJUnitTest= true; + OpenDeclarationsAction.sAllowFallback= false; initProject(); } @@ -684,4 +685,24 @@ public class CSelectionTestsNoIndexer extends BaseUITestCase { assertEquals(((ASTNode)decl).getLength(), 11); } + // int myFunc(var) + // int var; + // { + // return var; + // } + // + // int main(void) + // { + // return myFunc(0); + // } + public void testKRstyleFunctions_Bug221635() throws Exception { + String code= getContentsForTest(1)[0].toString(); + IFile file = importFile("source.c", code); + int offset= code.indexOf("myFunc(0)"); + IASTNode decl= testF3(file, offset); + assertTrue(decl instanceof IASTName); + final IASTName name = (IASTName) decl; + assertTrue(name.isDefinition()); + assertEquals("myFunc", name.toString()); + } } 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 00ae274329f..593899483b1 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 @@ -76,6 +76,7 @@ import org.eclipse.cdt.internal.ui.viewsupport.IndexUI; public class OpenDeclarationsAction extends SelectionParseAction implements ASTRunnable { public static boolean sIsJUnitTest = false; + public static boolean sAllowFallback= true; private static final int KIND_OTHER = 0; private static final int KIND_USING_DECL = 1; @@ -218,7 +219,7 @@ public class OpenDeclarationsAction extends SelectionParseAction implements ASTR private boolean navigationFallBack(IASTTranslationUnit ast) { // bug 102643, as a fall-back we look up the selected word in the index - if (fSelectedText != null && fSelectedText.length() > 0) { + if (sAllowFallback && fSelectedText != null && fSelectedText.length() > 0) { try { final ICProject project = fWorkingCopy.getCProject(); final char[] name = fSelectedText.toCharArray();