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:
parent
8ee8577dc8
commit
3ba300630d
3 changed files with 117 additions and 25 deletions
|
@ -1707,7 +1707,95 @@ public class AST2Tests extends AST2BaseTest {
|
||||||
assertEquals( decls.length, 1 );
|
assertEquals( decls.length, 1 );
|
||||||
assertEquals( decls[0], name_xy );
|
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 {
|
public void testFnReturningPtrToFn() throws Exception {
|
||||||
IASTTranslationUnit tu = parse( "void ( * f( int ) )(){}", ParserLanguage.C ); //$NON-NLS-1$
|
IASTTranslationUnit tu = parse( "void ( * f( int ) )(){}", ParserLanguage.C ); //$NON-NLS-1$
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
/**********************************************************************
|
/**********************************************************************
|
||||||
* Copyright (c) 2002-2004 IBM Canada and others.
|
* Copyright (c) 2002-2004 IBM Canada and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
|
|
@ -667,30 +667,35 @@ public class CVisitor {
|
||||||
} else if( node instanceof ICASTFieldDesignator ) {
|
} else if( node instanceof ICASTFieldDesignator ) {
|
||||||
IASTNode blockItem = getContainingBlockItem( node );
|
IASTNode blockItem = getContainingBlockItem( node );
|
||||||
|
|
||||||
IASTNode parent = node.getParent();
|
if ( (blockItem instanceof IASTSimpleDeclaration ||
|
||||||
while ( parent != null && !(parent.getParent() instanceof IASTTranslationUnit) )
|
(blockItem instanceof IASTDeclarationStatement && ((IASTDeclarationStatement)blockItem).getDeclaration() instanceof IASTSimpleDeclaration)) ) {
|
||||||
parent = parent.getParent();
|
|
||||||
|
IASTSimpleDeclaration simpleDecl = null;
|
||||||
if ( parent.getParent() instanceof IASTTranslationUnit &&
|
if (blockItem instanceof IASTDeclarationStatement &&
|
||||||
blockItem instanceof IASTDeclarationStatement &&
|
((IASTDeclarationStatement)blockItem).getDeclaration() instanceof IASTSimpleDeclaration )
|
||||||
((IASTDeclarationStatement)blockItem).getDeclaration() instanceof IASTSimpleDeclaration &&
|
simpleDecl = (IASTSimpleDeclaration)((IASTDeclarationStatement)blockItem).getDeclaration();
|
||||||
((IASTSimpleDeclaration)((IASTDeclarationStatement)blockItem).getDeclaration()).getDeclSpecifier() instanceof IASTNamedTypeSpecifier ) {
|
else if ( blockItem instanceof IASTSimpleDeclaration )
|
||||||
// TODO use getDefinitions below instead of getDeclarations (i.e. want collection of defined members and the declaration doesn't always have it)
|
simpleDecl = (IASTSimpleDeclaration)blockItem;
|
||||||
IASTName declNames[] = ((IASTTranslationUnit)parent.getParent()).getDeclarations(((IASTNamedTypeSpecifier)((IASTSimpleDeclaration)((IASTDeclarationStatement)blockItem).getDeclaration()).getDeclSpecifier()).getName().resolveBinding());
|
|
||||||
for (int i=0; i<declNames.length; i++) {
|
if ( simpleDecl != null ) {
|
||||||
IASTNode declBlockItem = getContainingBlockItem(declNames[i]);
|
IBinding struct = null;
|
||||||
if ( declBlockItem instanceof IASTSimpleDeclaration ) {
|
if ( simpleDecl.getDeclSpecifier() instanceof IASTNamedTypeSpecifier )
|
||||||
if (((IASTSimpleDeclaration)declBlockItem).getDeclSpecifier() instanceof IASTCompositeTypeSpecifier ) {
|
struct = ((IASTNamedTypeSpecifier)simpleDecl.getDeclSpecifier()).getName().resolveBinding();
|
||||||
IASTDeclaration[] decls = ((IASTCompositeTypeSpecifier)((IASTSimpleDeclaration)declBlockItem).getDeclSpecifier()).getMembers();
|
else if ( simpleDecl.getDeclSpecifier() instanceof IASTElaboratedTypeSpecifier )
|
||||||
for (int j=0; j<decls.length; j++) {
|
struct = ((IASTElaboratedTypeSpecifier)simpleDecl.getDeclSpecifier()).getName().resolveBinding();
|
||||||
if (decls[j] instanceof IASTSimpleDeclaration) {
|
else if ( simpleDecl.getDeclSpecifier() instanceof IASTCompositeTypeSpecifier )
|
||||||
IASTDeclarator[] decltors = ((IASTSimpleDeclaration)decls[j]).getDeclarators();
|
struct = ((IASTCompositeTypeSpecifier)simpleDecl.getDeclSpecifier()).getName().resolveBinding();
|
||||||
for(int k=0; k<decltors.length; k++)
|
|
||||||
if (CharArrayUtils.equals(decltors[k].getName().toCharArray(), ((ICASTFieldDesignator)node).getName().toCharArray()))
|
if ( struct instanceof CStructure ) {
|
||||||
return decltors[k].getName().resolveBinding();
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue