Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] updated getDeclarations() patch


- added more tests to the getDeclarations() patch to make sure other conditions are handled
- added CStructure.findField(String) instead of relying on TranslationUnit.getDeclaration() when resolving FieldDesignators, much faster and more correct (i.e. had TODO to use getDefinition)

Devin Steffler
IBM's Eclipse CDT
Ottawa (Palladium), Ontario, Canada


Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java,v
retrieving revision 1.3
diff -u -r1.3 CFunction.java
--- parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java	21 Dec 2004 16:27:04 -0000	1.3
+++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java	17 Jan 2005 14:58:27 -0000
@@ -1,4 +1,3 @@
-
 /**********************************************************************
  * Copyright (c) 2002-2004 IBM Canada and others.
  * All rights reserved.   This program and the accompanying materials
Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java,v
retrieving revision 1.9
diff -u -r1.9 CVisitor.java
--- parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java	14 Jan 2005 16:52:45 -0000	1.9
+++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java	17 Jan 2005 14:58:27 -0000
@@ -65,6 +65,7 @@
 import org.eclipse.cdt.core.dom.ast.ICompositeType;
 import org.eclipse.cdt.core.dom.ast.IEnumeration;
 import org.eclipse.cdt.core.dom.ast.IEnumerator;
+import org.eclipse.cdt.core.dom.ast.IField;
 import org.eclipse.cdt.core.dom.ast.IFunction;
 import org.eclipse.cdt.core.dom.ast.IFunctionType;
 import org.eclipse.cdt.core.dom.ast.ILabel;
@@ -667,30 +668,35 @@
 		} else if( node instanceof ICASTFieldDesignator ) {
 			IASTNode blockItem = getContainingBlockItem( node );
 			
-			IASTNode parent = node.getParent();
-			while ( parent != null && !(parent.getParent() instanceof IASTTranslationUnit) )
-				parent = parent.getParent();
-			
-			if ( parent.getParent() instanceof IASTTranslationUnit &&
-					blockItem instanceof IASTDeclarationStatement &&
-					((IASTDeclarationStatement)blockItem).getDeclaration() instanceof IASTSimpleDeclaration &&
-					((IASTSimpleDeclaration)((IASTDeclarationStatement)blockItem).getDeclaration()).getDeclSpecifier() instanceof IASTNamedTypeSpecifier ) {
-				// TODO use getDefinitions below instead of getDeclarations (i.e. want collection of defined members and the declaration doesn't always have it)
-				IASTName declNames[] = ((IASTTranslationUnit)parent.getParent()).getDeclarations(((IASTNamedTypeSpecifier)((IASTSimpleDeclaration)((IASTDeclarationStatement)blockItem).getDeclaration()).getDeclSpecifier()).getName().resolveBinding());
-				for (int i=0; i<declNames.length; i++) {
-					IASTNode declBlockItem = getContainingBlockItem(declNames[i]);
-					if ( declBlockItem instanceof IASTSimpleDeclaration ) {
-						if (((IASTSimpleDeclaration)declBlockItem).getDeclSpecifier() instanceof IASTCompositeTypeSpecifier ) {
-							IASTDeclaration[] decls = ((IASTCompositeTypeSpecifier)((IASTSimpleDeclaration)declBlockItem).getDeclSpecifier()).getMembers();
-							for (int j=0; j<decls.length; j++) {
-								if (decls[j] instanceof IASTSimpleDeclaration) {
-									IASTDeclarator[] decltors = ((IASTSimpleDeclaration)decls[j]).getDeclarators();
-									for(int k=0; k<decltors.length; k++)
-										if (CharArrayUtils.equals(decltors[k].getName().toCharArray(), ((ICASTFieldDesignator)node).getName().toCharArray()))
-											return decltors[k].getName().resolveBinding();
-								}
-							}
+			if ( (blockItem instanceof IASTSimpleDeclaration ||
+					(blockItem instanceof IASTDeclarationStatement && ((IASTDeclarationStatement)blockItem).getDeclaration() instanceof IASTSimpleDeclaration)) ) {
+				
+				IASTSimpleDeclaration simpleDecl = null;
+				if (blockItem instanceof IASTDeclarationStatement &&
+					((IASTDeclarationStatement)blockItem).getDeclaration() instanceof IASTSimpleDeclaration )
+					simpleDecl = (IASTSimpleDeclaration)((IASTDeclarationStatement)blockItem).getDeclaration();
+				else if ( blockItem instanceof IASTSimpleDeclaration )
+					simpleDecl = (IASTSimpleDeclaration)blockItem;
+		
+				if ( simpleDecl != null ) {
+					IBinding struct = null;
+					if ( simpleDecl.getDeclSpecifier() instanceof IASTNamedTypeSpecifier )
+						struct = ((IASTNamedTypeSpecifier)simpleDecl.getDeclSpecifier()).getName().resolveBinding();
+					else if ( simpleDecl.getDeclSpecifier() instanceof IASTElaboratedTypeSpecifier )
+						struct = ((IASTElaboratedTypeSpecifier)simpleDecl.getDeclSpecifier()).getName().resolveBinding();
+					else if ( simpleDecl.getDeclSpecifier() instanceof IASTCompositeTypeSpecifier )
+						struct = ((IASTCompositeTypeSpecifier)simpleDecl.getDeclSpecifier()).getName().resolveBinding();
+					
+					if ( struct instanceof CStructure ) {
+						return ((CStructure)struct).findField(((ICASTFieldDesignator)node).getName().toString());
+					} else if ( struct instanceof ITypeContainer ) {
+						IType type = ((ITypeContainer)struct).getType();
+						while ( type instanceof ITypeContainer && !(type instanceof CStructure) ) {
+							type = ((ITypeContainer)type).getType();
 						}
+						
+						if ( type instanceof CStructure )
+							return ((CStructure)type).findField(((ICASTFieldDesignator)node).getName().toString());
 					}
 				}
 			}
Index: parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java,v
retrieving revision 1.28
diff -u -r1.28 AST2Tests.java
--- parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java	14 Jan 2005 16:52:43 -0000	1.28
+++ parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java	17 Jan 2005 14:58:44 -0000
@@ -71,6 +71,7 @@
 import org.eclipse.cdt.core.dom.ast.c.ICFunctionScope;
 import org.eclipse.cdt.core.dom.ast.c.ICScope;
 import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPointerToMember;
+import org.eclipse.cdt.core.model.IStructure;
 import org.eclipse.cdt.core.parser.ParserLanguage;
 import org.eclipse.cdt.internal.core.dom.parser.c.CFunction;
 import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
@@ -1707,7 +1708,95 @@
 		assertEquals( decls.length, 1 );
 		assertEquals( decls[0], name_xy );
     }
-    
+
+    public void testMoreGetDeclarations1() throws Exception {
+        StringBuffer buffer = new StringBuffer(); //$NON-NLS-1$
+   		buffer.append( "struct S {\n" ); //$NON-NLS-1$
+   		buffer.append( " int a;\n" ); //$NON-NLS-1$
+   		buffer.append( " int b;\n" ); //$NON-NLS-1$
+   		buffer.append( "} s;\n" ); //$NON-NLS-1$
+   		buffer.append( "int f() {\n" ); //$NON-NLS-1$
+   		buffer.append( "struct S s = {.a=1,.b=2};\n}\n" ); //$NON-NLS-1$
+   		IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
+    	
+   		IASTSimpleDeclaration S_decl = (IASTSimpleDeclaration)tu.getDeclarations()[0];
+   		IASTFunctionDefinition f_def = (IASTFunctionDefinition)tu.getDeclarations()[1];
+
+   		IASTName a1 = ((IASTSimpleDeclaration)((IASTCompositeTypeSpecifier)S_decl.getDeclSpecifier()).getMembers()[0]).getDeclarators()[0].getName();
+   		IASTName b1 = ((IASTSimpleDeclaration)((IASTCompositeTypeSpecifier)S_decl.getDeclSpecifier()).getMembers()[1]).getDeclarators()[0].getName();
+   		IASTName a2 = ((ICASTFieldDesignator)((ICASTDesignatedInitializer)((IASTInitializerList)((IASTSimpleDeclaration)((IASTDeclarationStatement)((IASTCompoundStatement)f_def.getBody()).getStatements()[0]).getDeclaration()).getDeclarators()[0].getInitializer()).getInitializers()[0]).getDesignators()[0]).getName();
+   		IASTName b2 = ((ICASTFieldDesignator)((ICASTDesignatedInitializer)((IASTInitializerList)((IASTSimpleDeclaration)((IASTDeclarationStatement)((IASTCompoundStatement)f_def.getBody()).getStatements()[0]).getDeclaration()).getDeclarators()[0].getInitializer()).getInitializers()[1]).getDesignators()[0]).getName();
+   		
+   		assertEquals( a1.resolveBinding(), a2.resolveBinding() );
+   		assertEquals( b1.resolveBinding(), b2.resolveBinding() );
+   		
+   		IASTName[] decls = tu.getDeclarations(a1.resolveBinding());
+   		assertEquals( decls.length, 1 );
+   		assertEquals( a1, decls[0] );
+   		
+   		decls = tu.getDeclarations(b1.resolveBinding());
+   		assertEquals( decls.length, 1 );
+   		assertEquals( b1, decls[0] );
+    }
+
+    public void testMoreGetDeclarations2() throws Exception {
+        StringBuffer buffer = new StringBuffer(); //$NON-NLS-1$
+   		buffer.append( " struct S { \n" ); //$NON-NLS-1$
+   		buffer.append( " int a; \n" ); //$NON-NLS-1$
+   		buffer.append( " int b; \n" ); //$NON-NLS-1$
+   		buffer.append( "} s = {.a=1,.b=2};\n" ); //$NON-NLS-1$
+   		IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
+    	
+   		IASTSimpleDeclaration S_decl = (IASTSimpleDeclaration)tu.getDeclarations()[0];
+
+   		IASTName a1 = ((IASTSimpleDeclaration)((IASTCompositeTypeSpecifier)S_decl.getDeclSpecifier()).getMembers()[0]).getDeclarators()[0].getName();
+   		IASTName b1 = ((IASTSimpleDeclaration)((IASTCompositeTypeSpecifier)S_decl.getDeclSpecifier()).getMembers()[1]).getDeclarators()[0].getName();
+   		IASTName a2 = ((ICASTFieldDesignator)((ICASTDesignatedInitializer)((IASTInitializerList)S_decl.getDeclarators()[0].getInitializer()).getInitializers()[0]).getDesignators()[0]).getName();
+   		IASTName b2 = ((ICASTFieldDesignator)((ICASTDesignatedInitializer)((IASTInitializerList)S_decl.getDeclarators()[0].getInitializer()).getInitializers()[1]).getDesignators()[0]).getName(); 
+   		
+   		assertEquals( a1.resolveBinding(), a2.resolveBinding() );
+   		assertEquals( b1.resolveBinding(), b2.resolveBinding() );
+   		
+   		IASTName[] decls = tu.getDeclarations(a1.resolveBinding());
+   		assertEquals( decls.length, 1 );
+   		assertEquals( a1, decls[0] );
+   		
+   		decls = tu.getDeclarations(b1.resolveBinding());
+   		assertEquals( decls.length, 1 );
+   		assertEquals( b1, decls[0] );
+    }
+
+    public void testMoreGetDeclarations3() throws Exception {
+        StringBuffer buffer = new StringBuffer(); //$NON-NLS-1$
+   		buffer.append( " typedef struct S { \n" ); //$NON-NLS-1$
+   		buffer.append( " int a; \n" ); //$NON-NLS-1$
+   		buffer.append( " int b; \n" ); //$NON-NLS-1$
+   		buffer.append( "} s;\n" ); //$NON-NLS-1$
+   		buffer.append( "typedef s t;\n" ); //$NON-NLS-1$
+   		buffer.append( "typedef t y;\n" ); //$NON-NLS-1$
+   		buffer.append( "y x = {.a=1,.b=2};\n" ); //$NON-NLS-1$
+   		IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C ); // TODO Devin make sure that loop I put in works properly for types
+    	
+   		IASTSimpleDeclaration S_decl = (IASTSimpleDeclaration)tu.getDeclarations()[0];
+   		IASTSimpleDeclaration x_decl = (IASTSimpleDeclaration)tu.getDeclarations()[3]; 
+
+   		IASTName a1 = ((IASTSimpleDeclaration)((IASTCompositeTypeSpecifier)S_decl.getDeclSpecifier()).getMembers()[0]).getDeclarators()[0].getName();
+   		IASTName b1 = ((IASTSimpleDeclaration)((IASTCompositeTypeSpecifier)S_decl.getDeclSpecifier()).getMembers()[1]).getDeclarators()[0].getName();
+   		IASTName a2 = ((ICASTFieldDesignator)((ICASTDesignatedInitializer)((IASTInitializerList)x_decl.getDeclarators()[0].getInitializer()).getInitializers()[0]).getDesignators()[0]).getName();
+   		IASTName b2 = ((ICASTFieldDesignator)((ICASTDesignatedInitializer)((IASTInitializerList)x_decl.getDeclarators()[0].getInitializer()).getInitializers()[1]).getDesignators()[0]).getName();
+   		
+   		assertEquals( a1.resolveBinding(), a2.resolveBinding() );
+   		assertEquals( b1.resolveBinding(), b2.resolveBinding() );
+   		
+   		IASTName[] decls = tu.getDeclarations(a1.resolveBinding());
+   		assertEquals( decls.length, 1 );
+   		assertEquals( a1, decls[0] );
+   		
+   		decls = tu.getDeclarations(b1.resolveBinding());
+   		assertEquals( decls.length, 1 );
+   		assertEquals( b1, decls[0] );
+    }
+
     public void testFnReturningPtrToFn() throws Exception {
     	IASTTranslationUnit tu = parse( "void ( * f( int ) )(){}", ParserLanguage.C ); //$NON-NLS-1$
     	

Back to the top