Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] CModelBuilder & NewClassWizard updates


- 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 ).

Hoda Amer
Staff Software Engineer
Rational Software - IBM Software Group



Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/ChangeLog,v
retrieving revision 1.118
diff -u -r1.118 ChangeLog
--- ChangeLog	29 Jul 2003 19:51:30 -0000	1.118
+++ ChangeLog	30 Jul 2003 18:03:59 -0000
@@ -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
Index: model/org/eclipse/cdt/internal/core/model/CModelBuilder.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder.java,v
retrieving revision 1.9
diff -u -r1.9 CModelBuilder.java
--- model/org/eclipse/cdt/internal/core/model/CModelBuilder.java	24 Jul 2003 21:47:11 -0000	1.9
+++ model/org/eclipse/cdt/internal/core/model/CModelBuilder.java	30 Jul 2003 18:04:00 -0000
@@ -164,23 +164,8 @@
 		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 @@
 			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 @@
 		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 @@
 		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 @@
 		}else if(typeSpecifier instanceof IASTSimpleTypeSpecifier){		
 			IASTSimpleTypeSpecifier simpleSpecifier = (IASTSimpleTypeSpecifier) typeSpecifier;		
 			type.append(simpleSpecifier.getTypename());
-		} 
+		}
 		return type.toString();	
 	}
 	
@@ -686,21 +681,28 @@
 		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 @@
     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<paramList.size(); ++i){
 			parameterTypes[i] = (String)paramList.get(i); 
 		}
-    	return parameterTypes;
-    }
-	
+		return parameterTypes;			
+	}
 	private String getParametersString(String[] parameterTypes) 
 	{
 		StringBuffer parameters = new StringBuffer("");
Index: search/org/eclipse/cdt/core/search/BasicSearchMatch.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/BasicSearchMatch.java,v
retrieving revision 1.1
diff -u -r1.1 BasicSearchMatch.java
--- search/org/eclipse/cdt/core/search/BasicSearchMatch.java	29 Jul 2003 22:47:57 -0000	1.1
+++ search/org/eclipse/cdt/core/search/BasicSearchMatch.java	30 Jul 2003 18:04:01 -0000
@@ -71,6 +71,14 @@
 	public IResource getResource() {
 		return resource;
 	}
+	
+	public IPath getLocation() {
+		if(resource != null)
+			return resource.getLocation();
+		else if (path != null)
+			return path;
+		else return null;
+	}
 
 	public int getStartOffset() {
 		return startOffset;
Index: search/org/eclipse/cdt/core/search/IMatch.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/IMatch.java,v
retrieving revision 1.2
diff -u -r1.2 IMatch.java
--- search/org/eclipse/cdt/core/search/IMatch.java	29 Jul 2003 22:47:57 -0000	1.2
+++ search/org/eclipse/cdt/core/search/IMatch.java	30 Jul 2003 18:04:01 -0000
@@ -14,6 +14,7 @@
 package org.eclipse.cdt.core.search;
 
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IPath;
 
 /**
  * @author aniefer
@@ -32,6 +33,8 @@
 	String getParentName();
 
 	IResource getResource();
+	
+	IPath getLocation();
 
 	int getStartOffset();
 	
Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core.tests/ChangeLog,v
retrieving revision 1.45
diff -u -r1.45 ChangeLog
--- ChangeLog	30 Jul 2003 13:04:38 -0000	1.45
+++ ChangeLog	30 Jul 2003 18:03:41 -0000
@@ -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.
 
Index: model/org/eclipse/cdt/core/model/tests/CModelElementsTests.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core.tests/model/org/eclipse/cdt/core/model/tests/CModelElementsTests.java,v
retrieving revision 1.4
diff -u -r1.4 CModelElementsTests.java
--- model/org/eclipse/cdt/core/model/tests/CModelElementsTests.java	24 Jul 2003 17:18:44 -0000	1.4
+++ model/org/eclipse/cdt/core/model/tests/CModelElementsTests.java	30 Jul 2003 18:03:41 -0000
@@ -306,10 +306,10 @@
 		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 @@
 	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);
Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/ChangeLog,v
retrieving revision 1.130
diff -u -r1.130 ChangeLog
--- ChangeLog	29 Jul 2003 22:47:59 -0000	1.130
+++ ChangeLog	30 Jul 2003 18:08:26 -0000
@@ -1,3 +1,6 @@
+2003-07-30 Hoda Amer
+	The New Class Wizard uses search to look for base classes in the workspace.
+
 2003-07-29 Andrew Niefer
 	- Refactoring Search Result Collecting:
 		* CSearchResultCollector now extends BasicSearchResultCollector
Index: src/org/eclipse/cdt/ui/wizards/NewClassWizardPage.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/wizards/NewClassWizardPage.java,v
retrieving revision 1.4
diff -u -r1.4 NewClassWizardPage.java
--- src/org/eclipse/cdt/ui/wizards/NewClassWizardPage.java	6 Jun 2003 01:16:10 -0000	1.4
+++ src/org/eclipse/cdt/ui/wizards/NewClassWizardPage.java	30 Jul 2003 18:08:27 -0000
@@ -15,7 +15,7 @@
 import java.io.InputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.text.MessageFormat;
-import java.util.ArrayList;
+import java.util.LinkedList;
 import java.util.Iterator;
 import java.util.List;
 
@@ -27,6 +27,12 @@
 import org.eclipse.cdt.core.model.IParent;
 import org.eclipse.cdt.core.model.IStructure;
 import org.eclipse.cdt.core.model.ITranslationUnit;
+import org.eclipse.cdt.core.search.BasicSearchMatch;
+import org.eclipse.cdt.core.search.BasicSearchResultCollector;
+import org.eclipse.cdt.core.search.ICSearchConstants;
+import org.eclipse.cdt.core.search.ICSearchPattern;
+import org.eclipse.cdt.core.search.ICSearchScope;
+import org.eclipse.cdt.core.search.SearchEngine;
 import org.eclipse.cdt.internal.core.model.IWorkingCopy;
 import org.eclipse.cdt.internal.ui.dialogs.StatusInfo;
 import org.eclipse.cdt.internal.ui.dialogs.StatusUtil;
@@ -42,7 +48,7 @@
 import org.eclipse.cdt.internal.ui.wizards.dialogfields.Separator;
 import org.eclipse.cdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
 import org.eclipse.cdt.internal.ui.wizards.dialogfields.StringDialogField;
-import org.eclipse.cdt.ui.CElementLabelProvider;
+import org.eclipse.cdt.ui.CSearchResultLabelProvider;
 import org.eclipse.cdt.ui.CUIPlugin;
 import org.eclipse.cdt.ui.CodeGeneration;
 import org.eclipse.cdt.ui.PreferenceConstants;
@@ -103,7 +109,7 @@
 	// the created class element
 	private /*IStructure*/ ICElement createdClass = null;
 	
-	private ArrayList elementsOfTypeClassInProject = null;
+	private List elementsOfTypeClassInProject = null;
 	
 	// Controls
 	private StringDialogField fClassNameDialogField;
@@ -119,6 +125,9 @@
 	protected IStatus fClassNameStatus;
 	protected IStatus fBaseClassStatus;
 
+	BasicSearchResultCollector  resultCollector;
+	SearchEngine searchEngine;
+
 	// -------------------- Initialization ------------------
 	public NewClassWizardPage(IStructuredSelection selection) {
 		super(PAGE_NAME);	
@@ -163,6 +172,9 @@
 		fClassNameStatus=  new StatusInfo();
 		fBaseClassStatus=  new StatusInfo();
 
+		resultCollector = new BasicSearchResultCollector ();
+		searchEngine = new SearchEngine();
+
 	}
 	
 	public void init() {
@@ -297,9 +309,9 @@
 	
 	private void classPageChangeControlPressed(DialogField field) {
 		if (field == fBaseClassDialogField) {
-			ICElement element= chooseBaseClass();
+			BasicSearchMatch element= (BasicSearchMatch)chooseBaseClass();
 			if (element != null) {
-				fBaseClassDialogField.setText(element.getElementName());
+				fBaseClassDialogField.setText(element.getName());
 			}
 		}
 	}
@@ -376,7 +388,7 @@
 		return null;
 	}
 	
-	private void getChildrenOfTypeClass(IParent parent, ArrayList elementsFound, IProgressMonitor monitor, int worked){
+	private void getChildrenOfTypeClass(IParent parent, List elementsFound, IProgressMonitor monitor, int worked){
 		ICElement[] elements = parent.getChildren();
 		monitor.worked( worked );
 		
@@ -391,20 +403,29 @@
 		}
 	}
 	
-	private ArrayList getClassElementsInProject(){
+	private void searchForClasses(ICProject cProject, List elementsFound, IProgressMonitor monitor, int worked){
+		ICSearchPattern pattern = SearchEngine.createSearchPattern( "*", ICSearchConstants.CLASS, ICSearchConstants.DECLARATIONS, false );
+		//  TODO: change that to project scope later
+		ICSearchScope scope = SearchEngine.createWorkspaceScope();;
+
+		searchEngine.search(CUIPlugin.getWorkspace(), pattern, scope, resultCollector);
+		elementsFound.addAll(resultCollector.getSearchResults());
+	}
+	
+	private List getClassElementsInProject(){
 		return elementsOfTypeClassInProject;
 	}
 	
-	private ArrayList findClassElementsInProject(){		
+	private List findClassElementsInProject(){		
 		if(eSelection == null){
-			return new ArrayList();			
+			return new LinkedList();			
 		}
 
 		if(	elementsOfTypeClassInProject != null ){
 			return elementsOfTypeClassInProject;
 		}
 
-		elementsOfTypeClassInProject = new ArrayList();		
+		elementsOfTypeClassInProject = new LinkedList();		
 		IRunnableWithProgress runnable= new IRunnableWithProgress() {
 			public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
 				if (monitor == null) {
@@ -413,7 +434,8 @@
 				monitor.beginTask(NewWizardMessages.getString("NewClassWizardPage.operations.getProjectClasses"), 5); //$NON-NLS-1$
 				try{
 					ICProject cProject = eSelection.getCProject();
-					getChildrenOfTypeClass((IParent)cProject, elementsOfTypeClassInProject, monitor, 1);
+					searchForClasses(cProject, elementsOfTypeClassInProject, monitor, 1);
+					//getChildrenOfTypeClass((IParent)cProject, elementsOfTypeClassInProject, monitor, 1);
 					monitor.worked(5);
 				} finally{
 					monitor.done();
@@ -431,18 +453,18 @@
 		return elementsOfTypeClassInProject;				
 	}
 	
-	protected ICElement chooseBaseClass(){
+	protected Object chooseBaseClass(){
 		// find the available classes in this project
-		ArrayList elementsFound = findClassElementsInProject();
+		List elementsFound = findClassElementsInProject();
 		
-		ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), new CElementLabelProvider());
+		ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), new CSearchResultLabelProvider());
 		dialog.setTitle(NewWizardMessages.getString("BaseClassSelectionDialog.title")); //$NON-NLS-1$
 		dialog.setMessage(NewWizardMessages.getString("BaseClassSelectionDialog.message")); //$NON-NLS-1$
 		dialog.setElements(elementsFound.toArray());
 		dialog.setFilter("*");
 		
 		if (dialog.open() == ElementListSelectionDialog.OK) {
-			ICElement element= (ICElement) dialog.getFirstResult();
+			Object element= dialog.getFirstResult();
 			return element;
 		}		
 		return null;
@@ -738,11 +760,11 @@
 		if((baseClassName != null) && (baseClassName.length() > 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 @@
 		// 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 @@
 		
 	}
 	
-	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

Back to the top