1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-19 06:55:23 +02:00

Index based name-resolution for K&R-style functions, bug 221635.

This commit is contained in:
Markus Schorn 2008-03-06 09:57:24 +00:00
parent 16d620eaa5
commit 6be8db253c
7 changed files with 90 additions and 2 deletions

View file

@ -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());
}
}

View file

@ -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

View file

@ -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 {

View file

@ -145,6 +145,7 @@ public class CPPSelectionTestsNoIndexer extends BaseUITestCase {
super.setUp();
initProject();
OpenDeclarationsAction.sIsJUnitTest= true;
OpenDeclarationsAction.sAllowFallback= false;
}
protected void tearDown() throws Exception {

View file

@ -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());
}
}

View file

@ -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());
}
}

View file

@ -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();