diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index f6ada576c2b..c75e54db43b 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -1707,7 +1707,95 @@ public class AST2Tests extends AST2BaseTest { 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$ diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java index 0d358958819..2866fae2dec 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CFunction.java @@ -1,4 +1,3 @@ - /********************************************************************** * Copyright (c) 2002-2004 IBM Canada and others. * All rights reserved. This program and the accompanying materials diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java index 4179e9a462f..bc70841229e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java @@ -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 ( 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