From 2d3fadb88c5708679a5466edb6a23cc7d5ef72fb Mon Sep 17 00:00:00 2001 From: Andrew Niefer Date: Thu, 29 Apr 2004 22:06:21 +0000 Subject: [PATCH] - fix bug 60149 - External Linkage is not recognized by the parser in Structural mode - create StructureParseTest and add it to ParserTestSuite --- .../core/parser/tests/ParserTestSuite.java | 1 + .../parser/tests/StructuralParseTest.java | 96 +++++++++++++++++++ .../core/parser/StructuralParseCallback.java | 10 +- .../ast/complete/ASTLinkageSpecification.java | 11 ++- 4 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/StructuralParseTest.java diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java index e907c0c6885..3534dc29d16 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java @@ -44,6 +44,7 @@ public class ParserTestSuite extends TestCase { suite.addTestSuite( CompleteParseASTExpressionTest.class ); suite.addTestSuite( CompleteParseASTSymbolIteratorTest.class ); suite.addTestSuite( CompleteParseASTTemplateTest.class ); + suite.addTestSuite( StructuralParseTest.class ); return suite; } } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/StructuralParseTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/StructuralParseTest.java new file mode 100644 index 00000000000..d7100b25cf3 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/StructuralParseTest.java @@ -0,0 +1,96 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v0.5 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v05.html + * + * Contributors: + * IBM Corp. - Rational Software - initial implementation + ******************************************************************************/ +/* + * Created on Apr 29, 2004 + */ +package org.eclipse.cdt.core.parser.tests; + +import java.io.StringReader; +import java.util.Iterator; + +import junit.framework.TestCase; + +import org.eclipse.cdt.core.parser.IParser; +import org.eclipse.cdt.core.parser.NullLogService; +import org.eclipse.cdt.core.parser.ParserFactory; +import org.eclipse.cdt.core.parser.ParserFactoryError; +import org.eclipse.cdt.core.parser.ParserLanguage; +import org.eclipse.cdt.core.parser.ParserMode; +import org.eclipse.cdt.core.parser.ScannerInfo; +import org.eclipse.cdt.core.parser.ast.IASTCompilationUnit; +import org.eclipse.cdt.core.parser.ast.IASTFunction; +import org.eclipse.cdt.core.parser.ast.IASTLinkageSpecification; +import org.eclipse.cdt.core.parser.ast.IASTTemplateDeclaration; +import org.eclipse.cdt.core.parser.ast.IASTVariable; +import org.eclipse.cdt.internal.core.parser.ParserException; +import org.eclipse.cdt.internal.core.parser.StructuralParseCallback; + +/** + * @author aniefer + */ +public class StructuralParseTest extends TestCase { + + public StructuralParseTest() + { + super(); + } + + protected StructuralParseCallback callback; + + protected IASTCompilationUnit parse( String code ) throws ParserException, ParserFactoryError + { + return parse( code, true, ParserLanguage.CPP ); + } + + protected IASTCompilationUnit parse( String code, boolean throwOnError ) throws ParserException, ParserFactoryError + { + return parse( code, throwOnError, ParserLanguage.CPP ); + } + + protected IASTCompilationUnit parse(String code, boolean throwOnError, ParserLanguage language) throws ParserException, ParserFactoryError + { + callback = new StructuralParseCallback(); + IParser parser = ParserFactory.createParser( + ParserFactory.createScanner( new StringReader( code ), "test-code", new ScannerInfo(), + ParserMode.STRUCTURAL_PARSE, language, callback, new NullLogService(), null ), + callback, ParserMode.STRUCTURAL_PARSE, language, null + ); + if( ! parser.parse() && throwOnError ) throw new ParserException( "FAILURE"); + return callback.getCompilationUnit(); + } + + public void testBug60149() throws Exception + { + IASTCompilationUnit cu = parse( "extern \"C\" { int v; } " ); + + Iterator i = cu.getDeclarations(); + + IASTLinkageSpecification ls = (IASTLinkageSpecification) i.next(); + assertFalse( i.hasNext() ); + + i = ls.getDeclarations(); + IASTVariable v = (IASTVariable) i.next(); + assertEquals( v.getName(), "v" ); + assertFalse( i.hasNext() ); + } + + public void testBug60480() throws Exception + { + IASTCompilationUnit cu = parse( "template < int > void foo();" ); + Iterator i = cu.getDeclarations(); + + IASTTemplateDeclaration template = (IASTTemplateDeclaration) i.next(); + assertFalse( i.hasNext() ); + + IASTFunction foo = (IASTFunction) template.getOwnedDeclaration(); + assertEquals( foo.getName(), "foo" ); + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/StructuralParseCallback.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/StructuralParseCallback.java index 5b658d0fc05..de08142b989 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/StructuralParseCallback.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/StructuralParseCallback.java @@ -30,6 +30,7 @@ import org.eclipse.cdt.core.parser.ast.IASTTemplateDeclaration; import org.eclipse.cdt.core.parser.ast.IASTTypedefDeclaration; import org.eclipse.cdt.core.parser.ast.IASTVariable; import org.eclipse.cdt.internal.core.parser.QuickParseCallback; +import org.eclipse.cdt.internal.core.parser.ast.complete.ASTLinkageSpecification; import org.eclipse.cdt.internal.core.parser.ast.complete.ASTScope; /** @@ -46,8 +47,12 @@ public class StructuralParseCallback extends QuickParseCallback{ private void addElement (IASTDeclaration element){ - if(inclusionLevel == 0) - ((ASTScope)currentScope).addDeclaration(element); + if(inclusionLevel == 0){ + if( currentScope instanceof ASTScope ) + ((ASTScope)currentScope).addDeclaration(element); + else if( currentScope instanceof ASTLinkageSpecification ) + ((ASTLinkageSpecification)currentScope).addDeclaration( element ); + } } private void enterScope(IASTNode node){ @@ -158,6 +163,7 @@ public class StructuralParseCallback extends QuickParseCallback{ * @see org.eclipse.cdt.core.parser.ISourceElementRequestor#enterLinkageSpecification(org.eclipse.cdt.core.parser.ast.IASTLinkageSpecification) */ public void enterLinkageSpecification(IASTLinkageSpecification linkageSpec) { + addElement(linkageSpec); enterScope(linkageSpec); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/ASTLinkageSpecification.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/ASTLinkageSpecification.java index 9631eb9f7cd..8d57657da91 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/ASTLinkageSpecification.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ast/complete/ASTLinkageSpecification.java @@ -10,10 +10,13 @@ ***********************************************************************/ package org.eclipse.cdt.internal.core.parser.ast.complete; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import org.eclipse.cdt.core.parser.ISourceElementRequestor; import org.eclipse.cdt.core.parser.ast.ASTNotImplementedException; +import org.eclipse.cdt.core.parser.ast.IASTDeclaration; import org.eclipse.cdt.core.parser.ast.IASTLinkageSpecification; import org.eclipse.cdt.internal.core.parser.ast.Offsets; import org.eclipse.cdt.internal.core.parser.pst.IContainerSymbol; @@ -24,6 +27,7 @@ import org.eclipse.cdt.internal.core.parser.pst.IContainerSymbol; */ public class ASTLinkageSpecification extends ASTAnonymousDeclaration implements IASTLinkageSpecification { + private List declarations = new ArrayList(); private final String linkageString; private Offsets offsets = new Offsets(); /** @@ -47,7 +51,7 @@ public class ASTLinkageSpecification extends ASTAnonymousDeclaration implements */ public Iterator getDeclarations() throws ASTNotImplementedException { - throw new ASTNotImplementedException(); + return declarations.iterator(); } /* (non-Javadoc) * @see org.eclipse.cdt.core.parser.ast.IASTOffsetableElement#setStartingOffset(int) @@ -126,5 +130,10 @@ public class ASTLinkageSpecification extends ASTAnonymousDeclaration implements public int getEndingLine() { return offsets.getEndingLine(); } + + public void addDeclaration(IASTDeclaration declaration) + { + declarations.add(declaration); + } }