From 5aae8a1f5d903f59e1cc964b0a73e68acfeb5d84 Mon Sep 17 00:00:00 2001 From: John Camelon Date: Wed, 30 Jul 2003 19:17:14 +0000 Subject: [PATCH] Patch for Hoda Amer. - The core patch modifies the CModelBuilder to recognize pointers to functions. - The tests patch changes the CModelElementsTests and puts the pointer to function test back in its original place (as a variable). - The ui patch modifies the NewClassWizard to use search in finding a base class ( the new indexer must be on for it to work ). --- core/org.eclipse.cdt.core.tests/ChangeLog | 4 + .../core/model/tests/CModelElementsTests.java | 18 +-- core/org.eclipse.cdt.core/ChangeLog | 3 + .../internal/core/model/CModelBuilder.java | 113 +++++++++--------- .../cdt/core/search/BasicSearchMatch.java | 8 ++ .../org/eclipse/cdt/core/search/IMatch.java | 3 + core/org.eclipse.cdt.ui/ChangeLog | 3 + .../cdt/ui/wizards/NewClassWizardPage.java | 68 +++++++---- 8 files changed, 131 insertions(+), 89 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/ChangeLog b/core/org.eclipse.cdt.core.tests/ChangeLog index 59d9c446c8e..979027ef9c6 100644 --- a/core/org.eclipse.cdt.core.tests/ChangeLog +++ b/core/org.eclipse.cdt.core.tests/ChangeLog @@ -1,3 +1,7 @@ +2003-07-30 Hoda Amer + The CModelElementsTests has the pointer to function test back in its original place + (a variable) + 2003-07-30 Victor Mozgin Moved testBug39532() from ASTFailedTests.java to QuickParseASTTests.java. diff --git a/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CModelElementsTests.java b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CModelElementsTests.java index 9dcd9c4ddad..9f9d75fb76f 100644 --- a/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CModelElementsTests.java +++ b/core/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CModelElementsTests.java @@ -306,10 +306,10 @@ public class CModelElementsTests extends TestCase { checkLineNumbers((CElement)var3, 75, 75); // MyPackage ---> function pointer: orig_malloc_hook -// IVariable vDecl2 = (IVariable) nsVars.get(3); -// assertEquals(vDecl2.getElementName(), new String("orig_malloc_hook")); -// assertEquals(vDecl2.getTypeName(), new String ("void*(*)(const char*, int, size_t)")); -// checkLineNumbers((CElement)vDecl2, 81, 81); + IVariable vDecl2 = (IVariable) nsVars.get(3); + assertEquals(vDecl2.getElementName(), new String("orig_malloc_hook")); + assertEquals(vDecl2.getTypeName(), new String ("void*(*)(const char*, int, size_t)")); + checkLineNumbers((CElement)vDecl2, 81, 81); } private void checkVariableDeclarations(IParent namespace){ @@ -324,20 +324,14 @@ public class CModelElementsTests extends TestCase { private void checkFunctions(IParent namespace){ ArrayList nsFunctionDeclarations = namespace.getChildrenOfType(ICElement.C_FUNCTION_DECLARATION); - // MyPackage ---> function pointer: orig_malloc_hook - IFunctionDeclaration pointerToFunction = (IFunctionDeclaration) nsFunctionDeclarations.get(0); - assertEquals(pointerToFunction.getElementName(), new String("orig_malloc_hook")); -// assertEquals(pointerToFunction.getReturnType(), new String ("void*(*)(const char*, int, size_t)")); - checkLineNumbers((CElement)pointerToFunction, 81, 81); - // MyPackage ---> function: void foo() - IFunctionDeclaration f1 = (IFunctionDeclaration) nsFunctionDeclarations.get(1); + IFunctionDeclaration f1 = (IFunctionDeclaration) nsFunctionDeclarations.get(0); assertEquals(f1.getElementName(), new String("foo")); assertEquals(f1.getReturnType(), new String("void")); checkLineNumbers((CElement)f1, 85, 85); // MyPackage ---> function: char* foo(int&, char**) - IFunctionDeclaration f2 = (IFunctionDeclaration) nsFunctionDeclarations.get(2); + IFunctionDeclaration f2 = (IFunctionDeclaration) nsFunctionDeclarations.get(1); assertEquals(f2.getElementName(), new String("foo")); assertEquals(f2.getReturnType(), new String("char*")); checkLineNumbers((CElement)f2, 87, 88); diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog index 7315bf2efce..5a40d48ba6c 100644 --- a/core/org.eclipse.cdt.core/ChangeLog +++ b/core/org.eclipse.cdt.core/ChangeLog @@ -1,3 +1,6 @@ +2003-07-30 Hoda Amer + The C Model recognizes pointers to functions. + 2003-07-29 Alain Magloire To discover if an application has debug info for DWARF-2 format diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder.java index e864a0d9995..fa680f92577 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder.java @@ -164,23 +164,8 @@ public class CModelBuilder { if(declaration instanceof IASTTypedefDeclaration ) { generateModelElements(parent, (IASTTypedefDeclaration) declaration); } - -/* if ((declaration instanceof IASTPointerToFunction) - || (declaration instanceof IASTPointerToMethod)) - { - createPointerToFunction(parent, declaration, false); - } - // variable or field - else */ - if (declaration instanceof IASTVariable) - { - createVariableSpecification(parent, (IASTVariable)declaration, false); - } - // function or method - else if(declaration instanceof IASTFunction ) - { - createFunctionSpecification(parent, (IASTFunction)declaration, false); - } + + createSimpleElement(parent, declaration, false); } protected void generateModelElements (Parent parent, IASTNamespaceDefinition declaration) throws ASTNotImplementedException{ @@ -208,31 +193,13 @@ public class CModelBuilder { CElement element = createAbstractElement(parent, abstractDeclaration , true); // set the element position element.setPos(templateDeclaration.getStartingOffset(), templateDeclaration.getEndingOffset() - templateDeclaration.getStartingOffset()); - // set the element lines - //element.setLines(templateDeclaration.getTopLine(), templateDeclaration.getBottomLine()); // set the template parameters String[] parameterTypes = getTemplateParameters(templateDeclaration); ITemplate classTemplate = (ITemplate) element; classTemplate.setTemplateParameterTypes(parameterTypes); } ITemplate template = null; - -/* if ((declaration instanceof IASTPointerToFunction) - || (declaration instanceof IASTPointerToMethod)) - { - template = (ITemplate) createPointerToFunction(parent, declaration, true); - } - // template of variable or field - else */ - if (declaration instanceof IASTVariable) - { - template = (ITemplate) createVariableSpecification(parent, (IASTVariable)declaration, true); - } - // Template of function or method - else if(declaration instanceof IASTFunction ) - { - template = (ITemplate) createFunctionSpecification(parent, (IASTFunction)declaration, true); - } + template = (ITemplate) createSimpleElement(parent, declaration, true); if(template != null){ CElement element = (CElement)template; @@ -278,6 +245,21 @@ public class CModelBuilder { return element; } + private CElement createSimpleElement(Parent parent, IASTDeclaration declaration, boolean isTemplate)throws ASTNotImplementedException{ + + CElement element = null; + if (declaration instanceof IASTVariable) + { + element = createVariableSpecification(parent, (IASTVariable)declaration, isTemplate); + } + // function or method + else if(declaration instanceof IASTFunction ) + { + element = createFunctionSpecification(parent, (IASTFunction)declaration, isTemplate); + } + return element; + } + protected Include createInclusion(Parent parent, IASTInclusion inclusion){ // create element Include element = new Include((CElement)parent, inclusion.getName(), !inclusion.isLocal()); @@ -642,9 +624,22 @@ public class CModelBuilder { type.append(getPointerOperation(declaration)); type.append(getArrayQualifiers(declaration)); + type.append(getPointerToFunctionType(declaration)); return type.toString(); } + private String getPointerToFunctionType(IASTAbstractDeclaration declaration){ + StringBuffer type = new StringBuffer(); + ASTPointerOperator po = declaration.getPointerToFunctionOperator(); + if(po != null){ + type.append("("); + type.append(getPointerOperator(po)); + type.append(")"); + String[] parameters =getParameterTypes(declaration.getParameters()); + type.append(getParametersString(parameters)); + } + return type.toString(); + } private String getDeclarationType(IASTAbstractDeclaration declaration){ StringBuffer type = new StringBuffer(); @@ -657,7 +652,7 @@ public class CModelBuilder { }else if(typeSpecifier instanceof IASTSimpleTypeSpecifier){ IASTSimpleTypeSpecifier simpleSpecifier = (IASTSimpleTypeSpecifier) typeSpecifier; type.append(simpleSpecifier.getTypename()); - } + } return type.toString(); } @@ -686,21 +681,28 @@ public class CModelBuilder { Iterator i = declaration.getPointerOperators(); while(i.hasNext()){ ASTPointerOperator po = (ASTPointerOperator) i.next(); - if(po == ASTPointerOperator.POINTER) - pointerString.append("*"); - - if(po == ASTPointerOperator.REFERENCE) - pointerString.append("&"); - - if(po == ASTPointerOperator.CONST_POINTER) - pointerString.append(" const"); - - if(po == ASTPointerOperator.VOLATILE_POINTER) - pointerString.append(" volatile"); + pointerString.append(getPointerOperator(po)); } return pointerString.toString(); } + + private String getPointerOperator(ASTPointerOperator po){ + String pointerString =""; + if(po == ASTPointerOperator.POINTER) + pointerString = ("*"); + if(po == ASTPointerOperator.REFERENCE) + pointerString =("&"); + + if(po == ASTPointerOperator.CONST_POINTER) + pointerString =("* const"); + + if(po == ASTPointerOperator.VOLATILE_POINTER) + pointerString =("* volatile"); + + return pointerString; + } + private String getArrayQualifiers(IASTAbstractDeclaration declaration){ StringBuffer arrayString = new StringBuffer(); Iterator i = declaration.getArrayModifiers(); @@ -714,18 +716,21 @@ public class CModelBuilder { private String[] getFunctionParameterTypes(IASTFunction functionDeclaration) { Iterator parameters = functionDeclaration.getParameters(); - List paramList = new ArrayList(); - while (parameters.hasNext()){ + return getParameterTypes(parameters); + } + + private String[] getParameterTypes(Iterator parameters){ + List paramList = new ArrayList(); + while (parameters.hasNext()){ IASTParameterDeclaration param = (IASTParameterDeclaration)parameters.next(); paramList.add(getType(param)); - } + } String[] parameterTypes = new String[paramList.size()]; for(int i=0; i 0)) { extendingBase = true; - ArrayList classElements = findClassElementsInProject(); - ICElement baseClass = findInList(baseClassName, classElements); + List classElements = findClassElementsInProject(); + BasicSearchMatch baseClass = (BasicSearchMatch)findInList(baseClassName, classElements); if(baseClass != null){ - baseClassFileName = baseClass.getUnderlyingResource().getName(); + baseClassFileName = baseClass.getLocation().toString(); } else { baseClassFileName = baseClassName + HEADER_EXT; } @@ -930,7 +952,7 @@ public class NewClassWizardPage extends WizardPage implements Listener { // class name must follow the C/CPP convensions // if class does not exist, give warning - ArrayList elementsFound = findClassElementsInProject(); + List elementsFound = findClassElementsInProject(); if(!foundInList(getBaseClassName(), elementsFound)){ status.setWarning(NewWizardMessages.getString("NewClassWizardPage.warning.BaseClassNotExists")); //$NON-NLS-1$ } @@ -938,17 +960,17 @@ public class NewClassWizardPage extends WizardPage implements Listener { } - private ICElement findInList(String name, ArrayList elements){ + private Object findInList(String name, List elements){ Iterator i = elements.iterator(); while (i.hasNext()){ - ICElement element = (ICElement)i.next(); - if (name.equals(element.getElementName())) + BasicSearchMatch element = (BasicSearchMatch)i.next(); + if (name.equals(element.getName())) return element; } return null; } - private boolean foundInList(String name, ArrayList elements){ + private boolean foundInList(String name, List elements){ if(findInList(name, elements) != null) return true; else