1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

patch from Devin Steffler: field designators

This commit is contained in:
Andrew Niefer 2005-01-17 19:07:16 +00:00
parent 8ee8577dc8
commit 3ba300630d
3 changed files with 117 additions and 25 deletions

View file

@ -1708,6 +1708,94 @@ public class AST2Tests extends AST2BaseTest {
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$

View file

@ -1,4 +1,3 @@
/**********************************************************************
* Copyright (c) 2002-2004 IBM Canada and others.
* All rights reserved. This program and the accompanying materials

View file

@ -667,30 +667,35 @@ public class CVisitor {
} else if( node instanceof ICASTFieldDesignator ) {
IASTNode blockItem = getContainingBlockItem( node );
IASTNode parent = node.getParent();
while ( parent != null && !(parent.getParent() instanceof IASTTranslationUnit) )
parent = parent.getParent();
if ( (blockItem instanceof IASTSimpleDeclaration ||
(blockItem instanceof IASTDeclarationStatement && ((IASTDeclarationStatement)blockItem).getDeclaration() instanceof IASTSimpleDeclaration)) ) {
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();
}
}
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());
}
}
}