1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-10 12:03:16 +02:00

Patch for John Camelon:

CORE
	Brought DOM up to par wrt functionality with Code Model.  
	Added isFunctionDefinition() to SimpleDeclaration.  
	Added visiblity indicators for member declarations.  
	Added offset information that was previously unavailable.  
	Added preprocessor statements (inclusions, macros) to DOM.  
	Updated TypeSpecifier hierarchy to allow for parameter declarations to own elaborated types. 
	Added new constructor to parser to aid testing. 
	Fixed bug36065.  

TESTS
	Added testMemberDeclarations(), testPreprocessor(),
testElaboratedParms() to DOMTests.
This commit is contained in:
Doug Schaefer 2003-04-08 03:41:05 +00:00
parent f2c83a4b67
commit 78e61be446
22 changed files with 538 additions and 75 deletions

View file

@ -37,7 +37,7 @@ public class BaseSpecifier {
public boolean isVirtual() { return isVirtual; }
private AccessSpecifier access = new AccessSpecifier();
private AccessSpecifier access = new AccessSpecifier( AccessSpecifier.v_unknown );
public void setAccess(int access) { this.access.setAccess(access); }
public int getAccess() { return access.getAccess(); }

View file

@ -8,14 +8,15 @@ import org.eclipse.cdt.internal.core.parser.util.AccessSpecifier;
import org.eclipse.cdt.internal.core.parser.util.ClassKey;
import org.eclipse.cdt.internal.core.parser.util.Name;
public class ClassSpecifier extends TypeSpecifier implements IScope {
public class ClassSpecifier extends TypeSpecifier implements IScope, IOffsettable {
AccessSpecifier access = new AccessSpecifier();
ClassKey key = new ClassKey();
private AccessSpecifier access = new AccessSpecifier( AccessSpecifier.v_private );
private ClassKey key = new ClassKey();
private int startingOffset = 0, totalLength = 0;
public int getClassKey() { return key.getClassKey(); }
public ClassSpecifier(int classKey, SimpleDeclaration declaration) {
public ClassSpecifier(int classKey, TypeSpecifier.IOwner declaration) {
super(declaration);
this.key.setClassKey(classKey);
}
@ -54,4 +55,32 @@ public class ClassSpecifier extends TypeSpecifier implements IScope {
access.setAccess(currentVisiblity);
}
/**
* @return
*/
public int getStartingOffset() {
return startingOffset;
}
/**
* @return
*/
public int getTotalLength() {
return totalLength;
}
/**
* @param i
*/
public void setStartingOffset(int i) {
startingOffset = i;
}
/**
* @param i
*/
public void setTotalLength(int i) {
totalLength = i;
}
}

View file

@ -38,7 +38,7 @@ public class DOMBuilder implements IParserCallback
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#classBegin(java.lang.String, org.eclipse.cdt.internal.core.newparser.Token)
*/
public Object classSpecifierBegin(Object container, Token classKey) {
SimpleDeclaration decl = (SimpleDeclaration)container;
TypeSpecifier.IOwner decl = (TypeSpecifier.IOwner)container;
int kind = ClassKey.t_struct;
int visibility = AccessSpecifier.v_public;
@ -58,6 +58,7 @@ public class DOMBuilder implements IParserCallback
ClassSpecifier classSpecifier = new ClassSpecifier(kind, decl);
classSpecifier.setCurrentVisibility( visibility );
classSpecifier.setStartingOffset( classKey.getOffset() );
decl.setTypeSpecifier(classSpecifier);
return classSpecifier;
}
@ -74,6 +75,8 @@ public class DOMBuilder implements IParserCallback
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#classEnd()
*/
public void classSpecifierEnd(Object classSpecifier, Token closingBrace) {
ClassSpecifier c = (ClassSpecifier)classSpecifier;
c.setTotalLength( closingBrace.getOffset() + closingBrace.getLength() - c.getStartingOffset() );
}
/**
@ -138,6 +141,8 @@ public class DOMBuilder implements IParserCallback
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#functionBodyBegin()
*/
public Object functionBodyBegin(Object declaration) {
SimpleDeclaration simpleDec = (SimpleDeclaration)declaration;
simpleDec.setFunctionDefinition(true);
return null;
}
@ -151,6 +156,7 @@ public class DOMBuilder implements IParserCallback
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#inclusionBegin(java.lang.String)
*/
public void inclusionBegin(String includeFile, int offset, int inclusionBeginOffset) {
translationUnit.addInclusion( new Inclusion( includeFile, offset, inclusionBeginOffset, offset + includeFile.length() + 1 ) );
}
/**
@ -163,6 +169,7 @@ public class DOMBuilder implements IParserCallback
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#macro(java.lang.String)
*/
public void macro(String macroName, int offset, int macroBeginOffset, int macroEndOffset) {
translationUnit.addMacro( new Macro( macroName, offset, macroBeginOffset, macroEndOffset - macroBeginOffset));
}
/**
@ -171,6 +178,9 @@ public class DOMBuilder implements IParserCallback
public Object simpleDeclarationBegin(Object container, Token firstToken) {
SimpleDeclaration decl = new SimpleDeclaration();
((IScope)container).addDeclaration(decl);
if( container instanceof ClassSpecifier )
decl.setAccessSpecifier(new AccessSpecifier( ((ClassSpecifier)container).getCurrentVisibility() ));
((IOffsettable)decl).setStartingOffset( firstToken.getOffset() );
return decl;
}
@ -178,6 +188,8 @@ public class DOMBuilder implements IParserCallback
* @see org.eclipse.cdt.internal.core.newparser.IParserCallback#simpleDeclarationEnd(org.eclipse.cdt.internal.core.newparser.Token)
*/
public void simpleDeclarationEnd(Object declaration, Token lastToken) {
IOffsettable offsetable = (IOffsettable)declaration;
offsetable.setTotalLength( lastToken.getOffset() + lastToken.getLength() - offsetable.getStartingOffset());
}
/**
@ -299,7 +311,7 @@ public class DOMBuilder implements IParserCallback
*/
public void classSpecifierAbort(Object classSpecifier) {
ClassSpecifier cs = (ClassSpecifier)classSpecifier;
cs.getDeclaration().setTypeSpecifier(null);
cs.getOwner().setTypeSpecifier(null);
}
/**
@ -313,7 +325,6 @@ public class DOMBuilder implements IParserCallback
* @see org.eclipse.cdt.internal.core.parser.IParserCallback#elaboratedTypeSpecifierBegin(java.lang.Object)
*/
public Object elaboratedTypeSpecifierBegin(Object container, Token classKey) {
SimpleDeclaration declaration = (SimpleDeclaration)container;
int kind = ClassKey.t_struct;
switch (classKey.getType()) {
@ -328,7 +339,9 @@ public class DOMBuilder implements IParserCallback
break;
}
ElaboratedTypeSpecifier elab = new ElaboratedTypeSpecifier( kind, declaration );
ElaboratedTypeSpecifier elab = null;
TypeSpecifier.IOwner declaration = (TypeSpecifier.IOwner)container;
elab = new ElaboratedTypeSpecifier( kind, declaration );
declaration.setTypeSpecifier( elab );
return elab;
}
@ -507,6 +520,7 @@ public class DOMBuilder implements IParserCallback
public Object namespaceDefinitionBegin(Object container, Token namespace) {
IScope ownerScope = (IScope)container;
NamespaceDefinition namespaceDef = new NamespaceDefinition(ownerScope);
((IOffsettable)namespaceDef).setStartingOffset( namespace.getOffset() );
return namespaceDef;
}
@ -531,6 +545,7 @@ public class DOMBuilder implements IParserCallback
*/
public void namespaceDefinitionEnd(Object namespace, Token closingBrace) {
NamespaceDefinition ns = (NamespaceDefinition)namespace;
ns.setTotalLength( closingBrace.getOffset() + closingBrace.getLength() - ns.getStartingOffset() );
ns.getOwnerScope().addDeclaration(ns);
}
@ -622,7 +637,8 @@ public class DOMBuilder implements IParserCallback
public Object enumSpecifierBegin(Object container, Token enumKey) {
SimpleDeclaration decl = (SimpleDeclaration)container;
EnumerationSpecifier es = new EnumerationSpecifier( decl );
decl.setTypeSpecifier(es);
decl.setTypeSpecifier(es);
((IOffsettable)decl).setStartingOffset( enumKey.getOffset() );
return es;
}
@ -640,13 +656,15 @@ public class DOMBuilder implements IParserCallback
*/
public void enumSpecifierAbort(Object enumSpec) {
EnumerationSpecifier es = (EnumerationSpecifier)enumSpec;
es.getDeclaration().setTypeSpecifier(null);
es.getOwner().setTypeSpecifier(null);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.IParserCallback#enumSpecifierEnd(java.lang.Object)
*/
public void enumSpecifierEnd(Object enumSpec, Token closingBrace) {
IOffsettable offsetable = (IOffsettable)enumSpec;
offsetable.setTotalLength( closingBrace.getOffset() + closingBrace.getLength() - offsetable.getStartingOffset());
}
/* (non-Javadoc)
@ -665,6 +683,7 @@ public class DOMBuilder implements IParserCallback
public Object enumeratorId(Object enumDefn) {
EnumeratorDefinition definition = (EnumeratorDefinition)enumDefn;
definition.setName( currName );
((IOffsettable)enumDefn).setStartingOffset( currName.getStartOffset() );
return definition;
}
@ -672,6 +691,8 @@ public class DOMBuilder implements IParserCallback
* @see org.eclipse.cdt.internal.core.parser.IParserCallback#enumDefinitionEnd(java.lang.Object)
*/
public void enumeratorEnd(Object enumDefn, Token lastToken) {
IOffsettable offsetable = (IOffsettable)enumDefn;
offsetable.setTotalLength( lastToken.getOffset() + lastToken.getLength() - offsetable.getStartingOffset());
}
/* (non-Javadoc)

View file

@ -21,23 +21,9 @@ public class ElaboratedTypeSpecifier extends TypeSpecifier {
{
this.classKey.setClassKey( classKey );
}
/**
* @see org.eclipse.cdt.internal.core.dom.TypeSpecifier#getDeclaration()
*/
public SimpleDeclaration getDeclaration() {
return super.getDeclaration();
}
/**
* @see org.eclipse.cdt.internal.core.dom.TypeSpecifier#setDeclaration(org.eclipse.cdt.internal.core.dom.SimpleDeclaration)
*/
public void setDeclaration(SimpleDeclaration declaration) {
super.setDeclaration(declaration);
}
public ElaboratedTypeSpecifier(int classKey, SimpleDeclaration declaration) {
super(declaration);
public ElaboratedTypeSpecifier(int classKey, TypeSpecifier.IOwner owner) {
super(owner);
this.classKey.setClassKey( classKey );
}

View file

@ -22,7 +22,7 @@ import org.eclipse.cdt.internal.core.parser.util.Name;
* @author jcamelon
*
*/
public class EnumerationSpecifier extends TypeSpecifier {
public class EnumerationSpecifier extends TypeSpecifier implements IOffsettable {
public EnumerationSpecifier(SimpleDeclaration declaration) {
super(declaration);
@ -30,6 +30,7 @@ public class EnumerationSpecifier extends TypeSpecifier {
private Name name = null;
private List enumeratorDefinitions = new ArrayList();
private int startingOffset = 0, totalLength = 0;
public void addEnumeratorDefinition( EnumeratorDefinition def )
{
@ -59,4 +60,32 @@ public class EnumerationSpecifier extends TypeSpecifier {
this.name = name;
}
/**
* @return
*/
public int getStartingOffset() {
return startingOffset;
}
/**
* @return
*/
public int getTotalLength() {
return totalLength;
}
/**
* @param i
*/
public void setStartingOffset(int i) {
startingOffset = i;
}
/**
* @param i
*/
public void setTotalLength(int i) {
totalLength = i;
}
}

View file

@ -18,10 +18,11 @@ import org.eclipse.cdt.internal.core.parser.util.Name;
* @author jcamelon
*
*/
public class EnumeratorDefinition implements IExpressionOwner {
public class EnumeratorDefinition implements IExpressionOwner, IOffsettable {
private Expression initialValue = null;
private Name name = null;
private int startingOffset = 0, totalLength = 0;
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.dom.IExpressionOwner#getExpression()
@ -52,4 +53,32 @@ public class EnumeratorDefinition implements IExpressionOwner {
this.name = name;
}
/**
* @return
*/
public int getStartingOffset() {
return startingOffset;
}
/**
* @return
*/
public int getTotalLength() {
return totalLength;
}
/**
* @param i
*/
public void setStartingOffset(int i) {
startingOffset = i;
}
/**
* @param i
*/
public void setTotalLength(int i) {
totalLength = i;
}
}

View file

@ -0,0 +1,38 @@
/**********************************************************************
* Created on Apr 7, 2003
*
* Copyright (c) 2002,2003 IBM/Rational Software 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 Ltd. - Rational Software - Initial API and implementation
************************************************************************/
package org.eclipse.cdt.internal.core.dom;
/**
* @author jcamelon
*
*/
public interface IOffsettable {
/**
* @return
*/
public abstract int getStartingOffset();
/**
* @return
*/
public abstract int getTotalLength();
/**
* @param i
*/
public abstract void setStartingOffset(int i);
/**
* @param i
*/
public abstract void setTotalLength(int i);
}

View file

@ -0,0 +1,25 @@
/**********************************************************************
* Created on Apr 7, 2003
*
* Copyright (c) 2002,2003 IBM/Rational Software 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 Ltd. - Rational Software - Initial API and implementation
************************************************************************/
package org.eclipse.cdt.internal.core.dom;
/**
* @author jcamelon
*
*/
public class Inclusion extends PreprocessorStatement {
public Inclusion( String name, int nameOffset, int startingOffset, int totalLength )
{
super( name, nameOffset, startingOffset, totalLength );
}
}

View file

@ -0,0 +1,26 @@
/**********************************************************************
* Created on Apr 7, 2003
*
* Copyright (c) 2002,2003 IBM/Rational Software 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 Ltd. - Rational Software - Initial API and implementation
************************************************************************/
package org.eclipse.cdt.internal.core.dom;
/**
* @author jcamelon
*
*/
public class Macro extends PreprocessorStatement {
public Macro( String name, int nameOffset, int startingOffset, int totalLength )
{
super( name, nameOffset, startingOffset, totalLength );
}
}

View file

@ -1,25 +0,0 @@
package org.eclipse.cdt.internal.core.dom;
import org.eclipse.cdt.internal.core.parser.util.AccessSpecifier;
/**
* @author dschaefe
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class MemberDeclaration {
public MemberDeclaration(int access, Declaration declaration) {
this.access.setAccess( access );
this.declaration = declaration;
}
private AccessSpecifier access = new AccessSpecifier();
public int getAccess() { return access.getAccess(); }
private Declaration declaration;
public Declaration getDeclaration() { return declaration; }
}

View file

@ -22,11 +22,12 @@ import org.eclipse.cdt.internal.core.parser.util.Name;
* @author jcamelon
*
*/
public class NamespaceDefinition extends Declaration implements IScope {
public class NamespaceDefinition extends Declaration implements IScope, IOffsettable {
private List declarations = new LinkedList();
private IScope ownerScope;
private Name name = null;
private int startingOffset = 0, totalLength = 0;
public NamespaceDefinition( IScope owner )
{
@ -71,4 +72,32 @@ public class NamespaceDefinition extends Declaration implements IScope {
this.name = name;
}
/**
* @return
*/
public int getStartingOffset() {
return startingOffset;
}
/**
* @return
*/
public int getTotalLength() {
return totalLength;
}
/**
* @param i
*/
public void setStartingOffset(int i) {
startingOffset = i;
}
/**
* @param i
*/
public void setTotalLength(int i) {
totalLength = i;
}
}

View file

@ -15,10 +15,28 @@ import org.eclipse.cdt.internal.core.parser.util.DeclSpecifier;
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class ParameterDeclaration extends Declaration implements DeclSpecifier.Container {
public class ParameterDeclaration extends Declaration implements DeclSpecifier.Container, TypeSpecifier.IOwner {
DeclSpecifier declSpec = null;
private TypeSpecifier typeSpecifier;
/**
* Returns the typeSpecifier.
* @return TypeSpecifier
*/
public TypeSpecifier getTypeSpecifier() {
return typeSpecifier;
}
/**
* Sets the typeSpecifier.
* @param typeSpecifier The typeSpecifier to set
*/
public void setTypeSpecifier(TypeSpecifier typeSpecifier) {
getDeclSpecifier().setType(DeclSpecifier.t_type);
this.typeSpecifier = typeSpecifier;
}
/**
* @see org.eclipse.cdt.internal.core.dom.DeclarationSpecifier.CElementWrapper#getDeclSpecifier()
*/

View file

@ -0,0 +1,66 @@
/**********************************************************************
* Created on Apr 7, 2003
*
* Copyright (c) 2002,2003 IBM/Rational Software 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 Ltd. - Rational Software - Initial API and implementation
************************************************************************/
package org.eclipse.cdt.internal.core.dom;
/**
* @author jcamelon
*
*/
public class PreprocessorStatement {
private final int startingOffset, totalLength;
final private int nameOffset;
final private String name;
public PreprocessorStatement( String name, int nameOffset, int startingOffset, int totalLength )
{
this.name =name;
this.nameOffset = nameOffset;
this.startingOffset = startingOffset;
this.totalLength = totalLength;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.dom.IOffsettable#getStartingOffset()
*/
public int getStartingOffset() {
return startingOffset;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.dom.IOffsettable#getTotalLength()
*/
public int getTotalLength() {
return totalLength;
}
/**
* @return
*/
public String getName() {
return name;
}
/**
* @return
*/
public int getNameOffset() {
return nameOffset;
}
public int getNameLength() {
return name.length();
}
}

View file

@ -4,11 +4,15 @@ import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.cdt.internal.core.parser.util.AccessSpecifier;
import org.eclipse.cdt.internal.core.parser.util.DeclSpecifier;
public class SimpleDeclaration extends Declaration implements DeclSpecifier.Container {
public class SimpleDeclaration extends Declaration implements DeclSpecifier.Container, IOffsettable, TypeSpecifier.IOwner {
private int startingOffset = 0, totalLength = 0;
private AccessSpecifier accessSpecifier = null;
private DeclSpecifier declSpec = null;
private boolean isFunctionDefinition = false;
public DeclSpecifier getDeclSpecifier()
{
@ -61,4 +65,60 @@ public class SimpleDeclaration extends Declaration implements DeclSpecifier.Cont
public void removeDeclarator(Object declarator) {
declarators.remove( declarator );
}
/**
* @return
*/
public AccessSpecifier getAccessSpecifier() {
return accessSpecifier;
}
/**
* @param specifier
*/
public void setAccessSpecifier(AccessSpecifier specifier) {
accessSpecifier = specifier;
}
/**
* @return
*/
public boolean isFunctionDefinition() {
return isFunctionDefinition;
}
/**
* @param b
*/
public void setFunctionDefinition(boolean b) {
isFunctionDefinition = b;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.dom.IOffsettable#getStartingOffset()
*/
public int getStartingOffset() {
return startingOffset;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.dom.IOffsettable#getTotalLength()
*/
public int getTotalLength() {
return totalLength;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.dom.IOffsettable#setStartingOffset(int)
*/
public void setStartingOffset(int i) {
startingOffset = i;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.dom.IOffsettable#setTotalLength(int)
*/
public void setTotalLength(int i) {
totalLength = i;
}
}

View file

@ -1,5 +1,6 @@
package org.eclipse.cdt.internal.core.dom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@ -10,6 +11,8 @@ import java.util.List;
public class TranslationUnit implements IScope {
private List declarations = new LinkedList();
private List macros = new ArrayList();
private List inclusions = new ArrayList();
public void addDeclaration(Declaration declaration) {
declarations.add(declaration);
@ -18,4 +21,28 @@ public class TranslationUnit implements IScope {
public List getDeclarations() {
return Collections.unmodifiableList( declarations );
}
/**
* @return
*/
public List getInclusions() {
return Collections.unmodifiableList( inclusions );
}
/**
* @return
*/
public List getMacros() {
return Collections.unmodifiableList( macros );
}
public void addMacro(Macro macro) {
macros.add(macro);
}
public void addInclusion(Inclusion inclusion) {
inclusions.add(inclusion);
}
}

View file

@ -2,29 +2,28 @@ package org.eclipse.cdt.internal.core.dom;
public class TypeSpecifier {
public TypeSpecifier(SimpleDeclaration declaration) {
this.declaration = declaration;
interface IOwner
{
public TypeSpecifier getTypeSpecifier();
public void setTypeSpecifier( TypeSpecifier typespec );
}
public TypeSpecifier(IOwner owner) {
this.owner = owner;
}
/**
* Owner declaration.
*/
private SimpleDeclaration declaration;
private IOwner owner;
/**
* Returns the declaration.
* @return SimpleDeclaration
*/
public SimpleDeclaration getDeclaration() {
return declaration;
public IOwner getOwner() {
return owner;
}
/**
* Sets the declaration.
* @param declaration The declaration to set
*/
public void setDeclaration(SimpleDeclaration declaration) {
this.declaration = declaration;
}
}

View file

@ -235,7 +235,7 @@ public class SimpleDeclarationWrapper extends DeclSpecifier implements DeclSpeci
this.functionDefinition = functionDefinition;
}
private AccessSpecifier currentVisibility = new AccessSpecifier();
private AccessSpecifier currentVisibility = new AccessSpecifier( AccessSpecifier.v_unknown );
/**
* @return int
*/

View file

@ -52,6 +52,13 @@ public class Parser {
), c, false);
}
public Parser(String code, IParserCallback c, boolean quickParse ) throws Exception {
this(new Scanner().initialize( new StringReader( code ), null
), c, quickParse);
}
public Parser(InputStream stream, IParserCallback c, boolean quick) throws Exception {
this(new Scanner().initialize( new InputStreamReader(stream), null ),
c, quick);
@ -1069,7 +1076,8 @@ c, quick);
switch (LT(1)) {
case Token.tLPAREN:
// temporary fix for initializer/function declaration ambiguity
if( LT(2) != Token.tINTEGER && LT(2) != Token.t_false && LT(2) != Token.t_true )
if( LT(2) != Token.tINTEGER && LT(2) != Token.t_false && LT(2) != Token.t_true && LT(2) != Token.tSTRING &&
LT(2) != Token.tLSTRING )
{
// parameterDeclarationClause
Object clause = null;

View file

@ -718,7 +718,7 @@ public class Scanner implements IScanner {
currentContext);
} else if (c == '#') {
int beginningOffset = currentContext.getOffset();
int beginningOffset = currentContext.getOffset() - 1;
// lets prepare for a preprocessor statement
StringBuffer buff = new StringBuffer();
buff.append((char) c);

View file

@ -21,9 +21,14 @@ public class AccessSpecifier {
public static final int v_private = 0;
public static final int v_protected = 1;
public static final int v_public = 2;
public static final int v_unknown = 3;
private int access;
public void setAccess(int access) { this.access = access; }
public int getAccess() { return access; }
public AccessSpecifier( int value )
{
setAccess( value );
}
}

View file

@ -22,7 +22,9 @@ import org.eclipse.cdt.internal.core.dom.EnumeratorDefinition;
import org.eclipse.cdt.internal.core.dom.ExceptionSpecifier;
import org.eclipse.cdt.internal.core.dom.ExplicitTemplateDeclaration;
import org.eclipse.cdt.internal.core.dom.Expression;
import org.eclipse.cdt.internal.core.dom.Inclusion;
import org.eclipse.cdt.internal.core.dom.LinkageSpecification;
import org.eclipse.cdt.internal.core.dom.Macro;
import org.eclipse.cdt.internal.core.dom.NamespaceDefinition;
import org.eclipse.cdt.internal.core.dom.ParameterDeclaration;
import org.eclipse.cdt.internal.core.dom.ParameterDeclarationClause;
@ -51,9 +53,14 @@ public class DOMTests extends TestCase {
super( arg );
}
public TranslationUnit parse(String code) throws Exception {
public TranslationUnit parse( String code ) throws Exception
{
return parse( code, false );
}
public TranslationUnit parse(String code, boolean quickParse ) throws Exception {
DOMBuilder domBuilder = new DOMBuilder();
Parser parser = new Parser(code, domBuilder);
Parser parser = new Parser(code, domBuilder, quickParse );
if( ! parser.parse() ) throw new ParserException( "Parse failure" );
return domBuilder.getTranslationUnit();
@ -690,6 +697,93 @@ public class DOMTests extends TestCase {
assertNull( q2.getExpression() );
}
public void testElaboratedParms() throws Exception
{
TranslationUnit tu = parse( "int x( struct A myA ) { /* junk */ }", true);
assertEquals( tu.getDeclarations().size(), 1 );
SimpleDeclaration declaration = (SimpleDeclaration)tu.getDeclarations().get(0);
assertEquals( declaration.getDeclSpecifier().getType(), DeclSpecifier.t_int );
assertEquals( declaration.getDeclarators().size(), 1 );
Declarator declarator = (Declarator)declaration.getDeclarators().get(0);
assertEquals( declarator.getName().toString(), "x" );
assertTrue( declaration.isFunctionDefinition() );
assertEquals( declarator.getParms().getDeclarations().size(), 1 );
ParameterDeclaration parm = (ParameterDeclaration)declarator.getParms().getDeclarations().get(0);
ElaboratedTypeSpecifier typeSpec = (ElaboratedTypeSpecifier)parm.getTypeSpecifier();
assertEquals( typeSpec.getClassKey(), ClassKey.t_struct );
assertEquals( typeSpec.getName().toString(), "A" );
assertEquals( parm.getDeclarators().size(), 1 );
Declarator subDeclarator = (Declarator)parm.getDeclarators().get(0);
assertEquals( subDeclarator.getName().toString(), "myA" );
}
public void testPreprocessor() throws Exception
{
Writer code = new StringWriter();
code.write( "#include <stdio.h>\n#define DEF VALUE\n");
TranslationUnit tu = parse( code.toString(), true );
assertEquals( tu.getInclusions().size(), 1 );
Inclusion i = (Inclusion)tu.getInclusions().get(0);
assertEquals( i.getName(), "stdio.h");
assertEquals( i.getStartingOffset(), 0 );
assertEquals( i.getNameLength(), 7 );
assertEquals( i.getNameOffset(), 10 );
assertEquals( i.getTotalLength(), 18 );
assertEquals( tu.getMacros().size(), 1 );
Macro m = (Macro)tu.getMacros().get(0);
assertEquals( m.getName(), "DEF" );
assertEquals( m.getStartingOffset(), 19 );
assertEquals( m.getNameLength(), 3 );
assertEquals( m.getNameOffset(), 27 );
assertEquals( m.getTotalLength(), 18 );
}
public void testMemberDeclarations() throws Exception
{
Writer code = new StringWriter();
code.write( "class A {\n" );
code.write( "public:\n");
code.write( " int isPublic;\n" );
code.write( "private:\n");
code.write( " int isPrivate;\n" );
code.write( "protected:\n");
code.write( " int isProtected;\n" );
code.write( "};");
TranslationUnit translationUnit = parse( code.toString() );
assertEquals( translationUnit.getDeclarations().size(), 1 );
SimpleDeclaration classDeclaration = (SimpleDeclaration)
translationUnit.getDeclarations().get(0);
assertEquals( classDeclaration.getDeclarators().size(), 0 );
ClassSpecifier classSpec = (ClassSpecifier)classDeclaration.getTypeSpecifier();
assertEquals( "A", classSpec.getName().toString() );
assertEquals( 3, classSpec.getDeclarations().size());
for( int i = 0; i < 3; ++i )
{
SimpleDeclaration subDecl = (SimpleDeclaration)classSpec.getDeclarations().get( i );
int visibility = AccessSpecifier.v_unknown;
switch( i )
{
case 0:
visibility = AccessSpecifier.v_public;
break;
case 1:
visibility = AccessSpecifier.v_private;
break;
case 2:
visibility = AccessSpecifier.v_protected;
break;
default:
break;
}
assertEquals( visibility, subDecl.getAccessSpecifier().getAccess() );
}
}
public void testPointerOperators() throws Exception
{
// Parse and get the translaton unit

View file

@ -1,7 +1,6 @@
package org.eclipse.cdt.core.parser.tests;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import junit.framework.Test;