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

Patch for Devin Steffler.

ASTUtil and friends ported to DOM.
This commit is contained in:
John Camelon 2005-03-11 20:35:22 +00:00
parent 834e06e8fc
commit 6545e1c4c7
12 changed files with 2221 additions and 785 deletions

View file

@ -19,19 +19,26 @@ import java.util.List;
import junit.framework.TestCase;
import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTConditionalExpression;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.c.CASTVisitor;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
@ -64,7 +71,7 @@ import org.eclipse.cdt.internal.core.parser.scanner2.IScannerExtensionConfigurat
* @author aniefer
*/
public class AST2BaseTest extends TestCase {
private static final IParserLogService NULL_LOG = new NullLogService();
protected IASTTranslationUnit parse( String code, ParserLanguage lang ) throws ParserException {
@ -279,4 +286,40 @@ public class AST2BaseTest extends TestCase {
assertEquals( num, count );
}
protected void isExpressionStringEqual(IASTExpression exp, String str) {
assertEquals(str, ASTSignatureUtil.getExpressionString(exp));
}
protected void isParameterSignatureEqual(IASTDeclarator decltor, String str) {
assertEquals(str, ASTSignatureUtil.getParameterSignature(decltor));
}
protected void isSignatureEqual(IASTDeclarator decltor, String str) {
assertEquals(str, ASTSignatureUtil.getSignature(decltor));
}
protected void isSignatureEqual(IASTDeclSpecifier declSpec, String str) {
assertEquals(str, ASTSignatureUtil.getSignature(declSpec));
}
protected void isSignatureEqual(IASTTypeId typeId, String str) {
assertEquals(str, ASTSignatureUtil.getSignature(typeId));
}
protected void isTypeEqual(IASTDeclarator decltor, String str) {
assertEquals(str, ASTTypeUtil.getType(decltor));
}
protected void isTypeEqual(IASTTypeId typeId, String str) {
assertEquals(str, ASTTypeUtil.getType(typeId));
}
protected void isTypeEqual(IType type, String str) {
assertEquals(str, ASTTypeUtil.getType(type));
}
protected void isParameterTypeEqual(IFunctionType fType, String str) {
assertEquals(str, ASTTypeUtil.getParameterTypeString(fType));
}
}

View file

@ -0,0 +1,432 @@
package org.eclipse.cdt.core.parser.tests.ast2;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.parser.ParserLanguage;
public class AST2UtilOldTests extends AST2BaseTest {
// Kind PRIMARY_EMPTY : void
public void testPrimaryEmpty() throws Exception
{
IASTTranslationUnit tu = parse("int x = f();".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f()" ); //$NON-NLS-1$
}
// Kind PRIMARY_INTEGER_LITERAL : int
public void testPrimaryIntegerLiteral() throws Exception
{
IASTTranslationUnit tu = parse("int x = f(1, 2+3);".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f(1, 2 + 3)" ); //$NON-NLS-1$
}
// Kind PRIMARY_CHAR_LITERAL : char
public void testPrimaryCharLiteral() throws Exception
{
IASTTranslationUnit tu = parse("int x = f('c');".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f('c')" ); //$NON-NLS-1$
}
// Kind PRIMARY_FLOAT_LITERAL : float
public void testPrimaryFloatLiteral() throws Exception
{
IASTTranslationUnit tu = parse("int x = f(1.13);".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f(1.13)" ); //$NON-NLS-1$
}
// Kind PRIMARY_STRING_LITERAL : char*
public void testPrimaryStringLiteral() throws Exception
{
IASTTranslationUnit tu = parse("int x = f(\"str\");".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f(\"str\")" ); //$NON-NLS-1$
}
// Kind PRIMARY_BOOLEAN_LITERAL : bool
public void testPrimaryBooleanLiteral() throws Exception
{
IASTTranslationUnit tu = parse("int x = f(true);".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f(true)" ); //$NON-NLS-1$
}
// Kind PRIMARY_THIS : type of inner most enclosing structure scope
public void testPrimaryThis() throws Exception
{
IASTTranslationUnit tu = parse("int x = f(this);".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f(this)" ); //$NON-NLS-1$
}
// Kind PRIMARY_BRACKETED_EXPRESSION : LHS
public void testPrimaryBracketedExpression() throws Exception
{
IASTTranslationUnit tu = parse("int x = f(1, (2+3));".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f(1, (2 + 3))" ); //$NON-NLS-1$
}
// Kind ID_EXPRESSION : type of the ID
public void testIdExpression() throws Exception
{
IASTTranslationUnit tu = parse("int x = f(a);".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f(a)" ); //$NON-NLS-1$
}
// Kind POSTFIX_SUBSCRIPT
public void testPostfixSubscript() throws Exception
{
IASTTranslationUnit tu = parse("int x = f(pa[1]);".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f(pa[1])" ); //$NON-NLS-1$
}
public void testPostfixSubscriptA() throws Exception
{
IASTTranslationUnit tu = parse("int x = f(pa[1][2]);".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f(pa[1][2])" ); //$NON-NLS-1$
}
// Kind POSTFIX_FUNCTIONCALL : return type of called function
public void testPostfixFunctioncallBug42822() throws Exception
{
IASTTranslationUnit tu = parse("int x = bar( foo( 3.0 ), foo( 5.0 ) ) ;".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "bar(foo(3.0), foo(5.0))" ); //$NON-NLS-1$
}
// Kind POSTFIX_SIMPLETYPE_* : simple type
public void testPostfixSimpletypesBug42823() throws Exception
{
IASTTranslationUnit tu = parse("int someInt = foo( int(3), short(4), double(3.0), float(4.0), char( 'a'), wchar_t( 'a' ), signed( 2 ), unsigned( 3 ), bool( false ), long( 3L ) );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(int(3), short(4), double(3.0), float(4.0), char('a'), wchar_t('a'), signed(2), unsigned(3), bool(false), long(3L))" ); //$NON-NLS-1$
}
// Kind POSTFIX_DOT_IDEXPRESSION : type of member in the scope of the container
public void testPostfixDotExpression() throws Exception{
IASTTranslationUnit tu = parse("class A {int m;}; \n A a; \n int foo(char); int foo( int ); \n int x = foo( a.m );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[4]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a.m)" ); //$NON-NLS-1$
}
// Kind POSTFIX_ARROW_IDEXPRESSION : type of member in the scope of the container
public void testPostfixArrowExpression() throws Exception{
IASTTranslationUnit tu = parse("class A {int m;}; \n A * a; \n int foo(char); int foo( int ); \n int x = foo( a->m );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[4]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a->m)" ); //$NON-NLS-1$
}
// Kind POSTFIX_INCREMENT : LHS
public void testPostfixIncrement() throws Exception
{
IASTTranslationUnit tu = parse("int y = foo( x++ );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(x++)" ); //$NON-NLS-1$
}
// Kind POSTFIX_DECREMENT : LHS
public void testPostfixDecrement() throws Exception
{
IASTTranslationUnit tu = parse("int y = foo( x-- );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(x--)" ); //$NON-NLS-1$
}
// Kind POSTFIX_DYNAMIC_CAST
public void testPostfixDynamicCast() throws Exception{
IASTTranslationUnit tu = parse("int x = foo( dynamic_cast<B*>(a) );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(dynamic_cast<B *>(a))" ); //$NON-NLS-1$
}
// Kind POSTFIX_REINTERPRET_CAST
public void testPostfixReinterpretCast() throws Exception{
IASTTranslationUnit tu = parse("int x = foo( reinterpret_cast<double *>(a) );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(reinterpret_cast<double *>(a))" ); //$NON-NLS-1$
}
// Kind POSTFIX_STATIC_CAST
public void testPostfixStaticCast() throws Exception{
IASTTranslationUnit tu = parse("int x = foo( static_cast<char>(a) );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(static_cast<char>(a))" ); //$NON-NLS-1$
}
// Kind POSTFIX_CONST_CAST
public void testPostfixConstCast() throws Exception{
IASTTranslationUnit tu = parse("int x = foo( const_cast<int *>(&a) );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(const_cast<int *>(&a))" ); //$NON-NLS-1$
}
// Kind POSTFIX_TYPEID_EXPRESSION : LHS
public void testPostfixTypeIdExpression() throws Exception{
IASTTranslationUnit tu = parse("int x = foo( typeid(5) );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(typeid(5))" ); //$NON-NLS-1$
}
// Kind POSTFIX_TYPEID_EXPRESSION : type of the ID
public void testPostfixTypeIdExpression2() throws Exception{
IASTTranslationUnit tu = parse("int x = foo( typeid(a) );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(typeid(a))" ); //$NON-NLS-1$
}
// Kind POSTFIX_TYPEID_TYPEID : type of the ID
// TODO enable after 87807 is fixed
// public void testPostfixTypeIdTypeId2() throws Exception{
// IASTTranslationUnit tu = parse("int x = foo( typeid(const A) );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
// IASTDeclaration[] d = tu.getDeclarations();
// isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(typeid(const A))" ); //$NON-NLS-1$
// }
// Kind UNARY_INCREMENT : LHS
public void testUnaryIncrement() throws Exception
{
IASTTranslationUnit tu = parse("int y = foo( ++x );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(++x)" ); //$NON-NLS-1$
}
// Kind UNARY_DECREMENT : LHS
public void testUnaryDecrement() throws Exception
{
IASTTranslationUnit tu = parse("int y = foo( --x );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(--x)" ); //$NON-NLS-1$
}
// Kind UNARY_STAR_CASTEXPRESSION : LHS + t_pointer
public void testUnaryStarCastExpression() throws Exception
{
IASTTranslationUnit tu = parse("int x = f(*pa);".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f(*pa)" ); //$NON-NLS-1$
}
// Kind UNARY_AMPSND_CASTEXPRESSION : LHS + t_reference
public void testUnaryAmpersandCastExpression() throws Exception
{
IASTTranslationUnit tu = parse("int x = f(&pa);".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "f(&pa)" ); //$NON-NLS-1$
}
// Kind UNARY_PLUS_CASTEXPRESSION : LHS
public void testUnaryPlusCastExpression() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( +5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(+5)" ); //$NON-NLS-1$
}
// Kind UNARY_MINUS_CASTEXPRESSION : LHS
public void testUnaryMinusCastExpression() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( -5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(-5)" ); //$NON-NLS-1$
}
// Kind UNARY_NOT_CASTEXPRESSION : LHS
public void testUnaryNotCastExpression() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( !b );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(!b)" ); //$NON-NLS-1$
}
// Kind UNARY_TILDE_CASTEXPRESSION : LHS
public void testTildeNotCastExpression() throws Exception {
IASTTranslationUnit tu = parse("int y = foo( ~x );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(~x)" ); //$NON-NLS-1$
}
// Kind UNARY_SIZEOF_UNARYEXPRESSION : unsigned int
public void testUnarySizeofUnaryExpression() throws Exception {
IASTTranslationUnit tu = parse("int y = foo( sizeof(5) );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(sizeof (5))" ); //$NON-NLS-1$
}
// Kind UNARY_SIZEOF_TYPEID : unsigned int
public void testUnarySizeofTypeId() throws Exception {
IASTTranslationUnit tu = parse("int y = foo( sizeof(x) );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(sizeof (x))" ); //$NON-NLS-1$
}
// Kind NEW_TYPEID
public void testNewTypeId() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( new A() );".toString(), ParserLanguage.CPP); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(new A())" ); //$NON-NLS-1$
}
// Kind CASTEXPRESSION
public void testCastExpression() throws Exception{
IASTTranslationUnit tu = parse("int x = foo( (A*)b );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo((A *)b)" ); //$NON-NLS-1$
}
// Kind MULTIPLICATIVE_MULTIPLY : usual arithmetic conversions
public void testMultiplicativeMultiply() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a * b );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a * b)" ); //$NON-NLS-1$
}
// Kind MULTIPLICATIVE_DIVIDE : usual arithmetic conversions
public void testMultiplicativeDivide() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( b / a );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(b / a)" ); //$NON-NLS-1$
}
// Kind MULTIPLICATIVE_MODULUS : usual arithmetic conversions
public void testMultiplicativeModulus() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( b % a );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(b % a)" ); //$NON-NLS-1$
}
// Kind ADDITIVE_PLUS : usual arithmetic conversions
public void testAdditivePlus() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( b + a );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(b + a)" ); //$NON-NLS-1$
}
// Kind ADDITIVE_MINUS : usual arithmetic conversions
public void testAdditiveMinus() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( b - a );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(b - a)" ); //$NON-NLS-1$
}
// Kind SHIFT_LEFT : LHS
public void testShiftLeft() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a << 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a << 5)" ); //$NON-NLS-1$
}
// Kind SHIFT_RIGHT : LHS
public void testShiftRight() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a >> 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a >> 5)" ); //$NON-NLS-1$
}
// Kind RELATIONAL_LESSTHAN : bool
public void testRelationalLessThan() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( b < 3 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(b < 3)" ); //$NON-NLS-1$
}
// Kind RELATIONAL_GREATERTHAN : bool
public void testRelationalGreaterThan() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( b > 3 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(b > 3)" ); //$NON-NLS-1$
}
// Kind RELATIONAL_LESSTHANEQUALTO : bool
public void testRelationalLessThanOrEqual() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( b <= 3 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(b <= 3)" ); //$NON-NLS-1$
}
// Kind RELATIONAL_GREATERTHANEQUALTO : bool
public void testRelationalGreaterThanOrEqual() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( b >= 3 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(b >= 3)" ); //$NON-NLS-1$
}
// Kind EQUALITY_EQUALS : bool
public void testEqualityEquals() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( b == 3 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(b == 3)" ); //$NON-NLS-1$
}
// Kind EQUALITY_NOTEQUALS : bool
public void testEqualityNotEquals() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( b != 3 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(b != 3)" ); //$NON-NLS-1$
}
// Kind ANDEXPRESSION : usual arithmetic conversions
public void testAndExpression() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a & b );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a & b)" ); //$NON-NLS-1$
}
// Kind EXCLUSIVEOREXPRESSION : usual arithmetic conversions
public void testExclusiveOrExpression() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a ^ b );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a ^ b)" ); //$NON-NLS-1$
}
// Kind INCLUSIVEOREXPRESSION : : usual arithmetic conversions
public void testInclusiveOrExpression() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a | b );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a | b)" ); //$NON-NLS-1$
}
// Kind LOGICALANDEXPRESSION : bool
public void testLogicalAndExpression() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a && b );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a && b)" ); //$NON-NLS-1$
}
// Kind LOGICALOREXPRESSION : bool
public void testLogicalOrExpression() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a || b );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a || b)" ); //$NON-NLS-1$
}
// Kind CONDITIONALEXPRESSION : conditional Expression Conversions
public void testConditionalExpression() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a > 5 ? b : c );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a > 5 ? b : c)" ); //$NON-NLS-1$
}
// Kind ASSIGNMENTEXPRESSION_NORMAL : LHS
public void testAssignmentExpressionNormal() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a = 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a = 5)" ); //$NON-NLS-1$
}
// Kind ASSIGNMENTEXPRESSION_PLUS : LHS
public void testAssignmentExpressionPlus() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a += 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a += 5)" ); //$NON-NLS-1$
}
// Kind ASSIGNMENTEXPRESSION_MINUS : LHS
public void testAssignmentExpressionMinus() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a -= 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a -= 5)" ); //$NON-NLS-1$
}
// Kind ASSIGNMENTEXPRESSION_MULT : LHS
public void testAssignmentExpressionMulti() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a *= 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a *= 5)" ); //$NON-NLS-1$
}
// Kind ASSIGNMENTEXPRESSION_DIV : LHS
public void testAssignmentExpressionDiv() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a /= 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a /= 5)" ); //$NON-NLS-1$
}
// Kind ASSIGNMENTEXPRESSION_MOD : LHS
public void testAssignmentExpressionMod() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a %= 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a %= 5)" ); //$NON-NLS-1$
}
// Kind ASSIGNMENTEXPRESSION_LSHIFT : LHS
public void testAssignmentExpressionLShift() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a >>= 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a >>= 5)" ); //$NON-NLS-1$
}
// Kind ASSIGNMENTEXPRESSION_RSHIFT : LHS
public void testAssignmentExpressionRShift() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a <<= 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a <<= 5)" ); //$NON-NLS-1$
}
// Kind ASSIGNMENTEXPRESSION_AND : LHS
public void testAssignmentExpressionAnd() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a &= 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a &= 5)" ); //$NON-NLS-1$
}
// Kind ASSIGNMENTEXPRESSION_OR : LHS
public void testAssignmentExpressionOr() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a |= 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a |= 5)" ); //$NON-NLS-1$
}
// Kind ASSIGNMENTEXPRESSION_XOR : LHS
public void testAssignmentExpressionXOr() throws Exception {
IASTTranslationUnit tu = parse("int x = foo( a ^= 5 );".toString(), ParserLanguage.C); //$NON-NLS-1$
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual( ((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression(), "foo(a ^= 5)" ); //$NON-NLS-1$
}
}

View file

@ -0,0 +1,153 @@
/*******************************************************************************
* Copyright (c) 2005 Rational Software Corp. 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:
* Rational Software - initial implementation
******************************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;
import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.parser.ParserLanguage;
/**
* @author dsteffle
*/
public class AST2UtilTests extends AST2BaseTest {
public void testSimpleSignature() throws Exception {
StringBuffer buff = new StringBuffer();
buff.append("int l, m, n=0;\n"); //$NON-NLS-1$
buff.append("int j = l ? m : n;\n"); //$NON-NLS-1$
buff.append("int i = l^m;\n"); //$NON-NLS-1$
buff.append("int g = i<<=j;\n"); //$NON-NLS-1$
buff.append("int f = sizeof( int );\n"); //$NON-NLS-1$
buff.append("int e = ~f;\n"); //$NON-NLS-1$
buff.append("int d = ++e;\n"); //$NON-NLS-1$
buff.append("int b = d++;\n"); //$NON-NLS-1$
buff.append("int c = sizeof b;\n"); //$NON-NLS-1$
buff.append("int a = b + c;\n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buff.toString(), ParserLanguage.C);
IASTDeclaration[] d = tu.getDeclarations();
isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[2].getInitializer()).getExpression(), "0"); //$NON-NLS-1$
isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[1]).getDeclarators()[0].getInitializer()).getExpression(), "l ? m : n"); //$NON-NLS-1$
isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[2]).getDeclarators()[0].getInitializer()).getExpression(), "l ^ m"); //$NON-NLS-1$
isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[3]).getDeclarators()[0].getInitializer()).getExpression(), "i <<= j"); //$NON-NLS-1$
isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[4]).getDeclarators()[0].getInitializer()).getExpression(), "sizeof (int)"); //$NON-NLS-1$
isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[5]).getDeclarators()[0].getInitializer()).getExpression(), "~f"); //$NON-NLS-1$
isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[6]).getDeclarators()[0].getInitializer()).getExpression(), "++e"); //$NON-NLS-1$
isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[7]).getDeclarators()[0].getInitializer()).getExpression(), "d++"); //$NON-NLS-1$
isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[8]).getDeclarators()[0].getInitializer()).getExpression(), "sizeof b"); //$NON-NLS-1$
isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[9]).getDeclarators()[0].getInitializer()).getExpression(), "b + c"); //$NON-NLS-1$
}
public void testSimpleParameter() throws Exception {
StringBuffer buff = new StringBuffer();
buff.append("int a(int x);\n"); //$NON-NLS-1$
buff.append("int * b(char y, int x);\n"); //$NON-NLS-1$
buff.append("void c(int * z, float **b);\n"); //$NON-NLS-1$
buff.append("static int d(int a[restrict]);\n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buff.toString(), ParserLanguage.C);
IASTDeclaration[] d = tu.getDeclarations();
isParameterSignatureEqual(((IASTSimpleDeclaration)d[0]).getDeclarators()[0], "(int)"); //$NON-NLS-1$
isParameterSignatureEqual(((IASTSimpleDeclaration)d[1]).getDeclarators()[0], "(char, int)"); //$NON-NLS-1$
isParameterSignatureEqual(((IASTSimpleDeclaration)d[2]).getDeclarators()[0], "(int *, float **)"); //$NON-NLS-1$
isParameterSignatureEqual(((IASTSimpleDeclaration)d[3]).getDeclarators()[0], "(int [restrict])"); //$NON-NLS-1$
isSignatureEqual(((IASTSimpleDeclaration)d[0]).getDeclarators()[0], "int (int)"); //$NON-NLS-1$
isSignatureEqual(((IASTSimpleDeclaration)d[1]).getDeclarators()[0], "int *(char, int)"); //$NON-NLS-1$
isSignatureEqual(((IASTSimpleDeclaration)d[2]).getDeclarators()[0], "void (int *, float **)"); //$NON-NLS-1$
isSignatureEqual(((IASTSimpleDeclaration)d[3]).getDeclarators()[0], "static int (int [restrict])"); //$NON-NLS-1$
isSignatureEqual(((IASTSimpleDeclaration)d[0]).getDeclSpecifier(), "int"); //$NON-NLS-1$
isSignatureEqual(((IASTSimpleDeclaration)d[1]).getDeclSpecifier(), "int"); //$NON-NLS-1$
isSignatureEqual(((IASTSimpleDeclaration)d[2]).getDeclSpecifier(), "void"); //$NON-NLS-1$
isSignatureEqual(((IASTSimpleDeclaration)d[3]).getDeclSpecifier(), "static int"); //$NON-NLS-1$
isTypeEqual(((IASTSimpleDeclaration)d[0]).getDeclarators()[0], "int (int)"); //$NON-NLS-1$
isTypeEqual(((IASTSimpleDeclaration)d[1]).getDeclarators()[0], "int * (char, int)"); //$NON-NLS-1$
isTypeEqual(((IASTSimpleDeclaration)d[2]).getDeclarators()[0], "void (int *, float * *)"); //$NON-NLS-1$
isTypeEqual(((IASTSimpleDeclaration)d[3]).getDeclarators()[0], "int (int * restrict)"); //$NON-NLS-1$
isTypeEqual(((IFunction)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getName().resolveBinding()).getType(), "int (int)"); //$NON-NLS-1$
isTypeEqual(((IFunction)((IASTSimpleDeclaration)d[1]).getDeclarators()[0].getName().resolveBinding()).getType(), "int * (char, int)"); //$NON-NLS-1$
isTypeEqual(((IFunction)((IASTSimpleDeclaration)d[2]).getDeclarators()[0].getName().resolveBinding()).getType(), "void (int *, float * *)"); //$NON-NLS-1$
isTypeEqual(((IFunction)((IASTSimpleDeclaration)d[3]).getDeclarators()[0].getName().resolveBinding()).getType(), "int (int * restrict)"); //$NON-NLS-1$
isParameterTypeEqual(((IFunction)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getName().resolveBinding()).getType(), "(int)"); //$NON-NLS-1$
isParameterTypeEqual(((IFunction)((IASTSimpleDeclaration)d[1]).getDeclarators()[0].getName().resolveBinding()).getType(), "(char, int)"); //$NON-NLS-1$
isParameterTypeEqual(((IFunction)((IASTSimpleDeclaration)d[2]).getDeclarators()[0].getName().resolveBinding()).getType(), "(int *, float * *)"); //$NON-NLS-1$
isParameterTypeEqual(((IFunction)((IASTSimpleDeclaration)d[3]).getDeclarators()[0].getName().resolveBinding()).getType(), "(int * restrict)"); //$NON-NLS-1$
}
public void testSimpleCParameterSignature() throws Exception {
StringBuffer buff = new StringBuffer();
buff.append("int a(int x);\n"); //$NON-NLS-1$
buff.append("int * b(char y, int x);\n"); //$NON-NLS-1$
buff.append("void c(int * z, float **b);\n"); //$NON-NLS-1$
buff.append("static int d(int a[restrict]);\n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buff.toString(), ParserLanguage.C);
IASTDeclaration[] d = tu.getDeclarations();
isParameterSignatureEqual(((IASTSimpleDeclaration)d[0]).getDeclarators()[0], "(int)"); //$NON-NLS-1$
isParameterSignatureEqual(((IASTSimpleDeclaration)d[1]).getDeclarators()[0], "(char, int)"); //$NON-NLS-1$
isParameterSignatureEqual(((IASTSimpleDeclaration)d[2]).getDeclarators()[0], "(int *, float **)"); //$NON-NLS-1$
isParameterSignatureEqual(((IASTSimpleDeclaration)d[3]).getDeclarators()[0], "(int [restrict])"); //$NON-NLS-1$
}
public void testSimpleTypeId() throws Exception {
StringBuffer buff = new StringBuffer();
buff.append("int x = sizeof( int );\n"); //$NON-NLS-1$
buff.append("union Squaw { int x; double u; };\n"); //$NON-NLS-1$
buff.append("int main(int argc, char **argv) {\n"); //$NON-NLS-1$
buff.append("return sizeof( union Squaw );\n}\n"); //$NON-NLS-1$
buff.append("typedef short Z; typedef Z jc;\n"); //$NON-NLS-1$
buff.append("int y = 4;\n"); //$NON-NLS-1$
buff.append("jc myJc = (jc)y;\n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buff.toString(), ParserLanguage.C);
IASTDeclaration[] d = tu.getDeclarations();
// verify signatures
isSignatureEqual( ((IASTTypeIdExpression)((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression()).getTypeId(), "int"); //$NON-NLS-1$
isSignatureEqual( ((IASTTypeIdExpression)((IASTReturnStatement)((IASTCompoundStatement)((IASTFunctionDefinition)d[2]).getBody()).getStatements()[0]).getReturnValue()).getTypeId(), "union"); //$NON-NLS-1$
isSignatureEqual( ((IASTCastExpression)((IASTInitializerExpression)((IASTSimpleDeclaration)d[6]).getDeclarators()[0].getInitializer()).getExpression()).getTypeId() , "jc"); //$NON-NLS-1$
// verify types
isTypeEqual( ((IASTTypeIdExpression)((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression()).getTypeId(), "int"); //$NON-NLS-1$
isTypeEqual( ((IASTTypeIdExpression)((IASTReturnStatement)((IASTCompoundStatement)((IASTFunctionDefinition)d[2]).getBody()).getStatements()[0]).getReturnValue()).getTypeId(), "union"); //$NON-NLS-1$
isTypeEqual( ((IASTCastExpression)((IASTInitializerExpression)((IASTSimpleDeclaration)d[6]).getDeclarators()[0].getInitializer()).getExpression()).getTypeId() , "short"); //$NON-NLS-1$
}
public void testKnRC() throws Exception {
StringBuffer buff = new StringBuffer();
buff.append("int foo(x, y) char x; int y; {}\n"); //$NON-NLS-1$
buff.append("int foo2(char x, int y) {}\n"); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buff.toString(), ParserLanguage.C);
IASTDeclaration[] d = tu.getDeclarations();
String fooSignature = ASTSignatureUtil.getSignature(((IASTFunctionDefinition)d[0]).getDeclarator());
String foo2Signature = ASTSignatureUtil.getSignature(((IASTFunctionDefinition)d[1]).getDeclarator());
assertEquals(fooSignature, foo2Signature);
}
}

View file

@ -32,6 +32,8 @@ public class DOMParserTestSuite extends TestCase {
suite.addTestSuite( DOMLocationTests.class );
suite.addTest( DOMLocationInclusionTests.suite() );
suite.addTestSuite( AST2KnRTests.class );
suite.addTestSuite( AST2UtilTests.class );
suite.addTestSuite( AST2UtilOldTests.class );
return suite;
}

View file

@ -0,0 +1,380 @@
/*******************************************************************************
* Copyright (c) 2005 Rational Software Corp. 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:
* Rational Software - initial implementation
******************************************************************************/
package org.eclipse.cdt.core.dom.ast;
import org.eclipse.cdt.core.dom.ast.c.ICArrayType;
import org.eclipse.cdt.core.dom.ast.c.ICBasicType;
import org.eclipse.cdt.core.dom.ast.c.ICPointerType;
import org.eclipse.cdt.core.dom.ast.c.ICQualifierType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTypeParameter;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPBasicType;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPPointerType;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPQualifierType;
import org.eclipse.cdt.core.parser.GCCKeywords;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
import org.eclipse.cdt.internal.core.dom.parser.c.CASTTypeId;
import org.eclipse.cdt.internal.core.dom.parser.c.CExternalFunction;
import org.eclipse.cdt.internal.core.dom.parser.c.CExternalVariable;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTypeId;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
/**
* This is a utility class to help convert AST elements to Strings corresponding to the
* AST element's type.
*
* @author dsteffle
*/
public class ASTTypeUtil {
private static final String COMMA_SPACE = ", "; //$NON-NLS-1$
private static final String EMPTY_STRING = ""; //$NON-NLS-1$
private static final String SPACE = " "; //$NON-NLS-1$
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private static final int DEAULT_ITYPE_SIZE = 2;
/**
* Returns a String represnetation of the parameter type of an IFunctionType.
*
* This function calls ASTTypeUtil#getParameterTypeStringArray(IFunctionType) and wraps the
* results in "()" with a comma separated list.
*
* @param type
* @return the represnetation of the parameter type of an IFunctionType
*/
public static String getParameterTypeString(IFunctionType type) {
StringBuffer result = new StringBuffer();
String[] parms = getParameterTypeStringArray(type);
result.append(Keywords.cpLPAREN);
for(int i=0; i<parms.length; i++) {
if (parms[i] != null) {
result.append(parms[i]);
if (i<parms.length-1) result.append(COMMA_SPACE);
}
}
result.append(Keywords.cpRPAREN);
return result.toString();
}
/**
* Returns String[] corresponding to the types of the parameters for the IFunctionType.
*
* @param type
* @return the types of the parameters for the IFunctionType
*/
public static String[] getParameterTypeStringArray(IFunctionType type) {
IType[] parms = null;
try {
parms = type.getParameterTypes();
} catch (DOMException e) { return EMPTY_STRING_ARRAY; }
String[] result = new String[parms.length];
for(int i=0; i<parms.length; i++) {
if (parms[i] != null) {
result[i] = getType(parms[i]);
}
}
return result;
}
private static String getTypeString(IType type) {
StringBuffer result = new StringBuffer();
boolean needSpace = false;
if (type instanceof IArrayType) {
result.append(Keywords.cpLBRACKET);
if (type instanceof ICArrayType) {
try {
if (((ICArrayType)type).isConst()) { result.append(Keywords.CONST); needSpace=true; }
if (((ICArrayType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
if (((ICArrayType)type).isStatic()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.STATIC); needSpace=true; }
if (((ICArrayType)type).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); }
} catch (DOMException e) {}
}
result.append(Keywords.cpRBRACKET);
} else if (type instanceof IBasicType) {
try {
if (((IBasicType)type).isSigned()) { result.append(Keywords.SIGNED); needSpace = true; }
else if (((IBasicType)type).isUnsigned()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.UNSIGNED); needSpace=true; }
if (((IBasicType)type).isLong()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.LONG); needSpace = true; }
else if (((IBasicType)type).isShort()) { if (needSpace) { result.append(SPACE); needSpace=false; }result.append(Keywords.SHORT); needSpace = true; }
} catch (DOMException e) {}
if (type instanceof IGPPBasicType) {
try {
if (((IGPPBasicType)type).isLongLong()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.LONG_LONG); needSpace=true; }
switch (((IGPPBasicType)type).getType()) {
case IGPPBasicType.t_Complex:
result.append(Keywords.c_COMPLEX);
break;
case IGPPBasicType.t_Imaginary:
result.append(Keywords.c_IMAGINARY);
break;
case IGPPBasicType.t_typeof:
result.append(GCCKeywords.TYPEOF);
break;
}
} catch (DOMException e) {}
} else if (type instanceof ICPPBasicType) {
try {
switch (((ICPPBasicType)type).getType()) {
case ICPPBasicType.t_bool:
result.append(Keywords.BOOL);
break;
case ICPPBasicType.t_wchar_t:
result.append(Keywords.WCHAR_T);
break;
}
} catch (DOMException e) {}
} else if (type instanceof ICBasicType) {
try {
switch (((ICBasicType)type).getType()) {
case ICBasicType.t_Bool:
result.append(Keywords.c_BOOL);
break;
case ICBasicType.t_Complex:
result.append(Keywords.c_COMPLEX);
break;
case ICBasicType.t_Imaginary:
result.append(Keywords.c_IMAGINARY);
break;
}
} catch (DOMException e) {}
}
try {
switch (((IBasicType)type).getType()) {
case IBasicType.t_char:
result.append(Keywords.CHAR);
break;
case IBasicType.t_double:
result.append(Keywords.DOUBLE);
break;
case IBasicType.t_float:
result.append(Keywords.FLOAT);
break;
case IBasicType.t_int:
result.append(Keywords.INT);
break;
case IBasicType.t_void:
result.append(Keywords.VOID);
break;
}
} catch (DOMException e) {}
} else if (type instanceof ICompositeType) {
if (type instanceof ICPPClassType) {
try {
switch(((ICPPClassType)type).getKey()) {
case ICPPClassType.k_class:
result.append(Keywords.CLASS);
break;
}
} catch (DOMException e) {}
}
try {
switch(((ICompositeType)type).getKey()) {
case ICompositeType.k_struct:
result.append(Keywords.STRUCT);
break;
case ICompositeType.k_union:
result.append(Keywords.UNION);
break;
}
} catch (DOMException e) {}
} else if (type instanceof ICPPReferenceType) {
result.append(Keywords.cpAMPER);
} else if (type instanceof ICPPTemplateTypeParameter) {
try {
result.append(getType(((ICPPTemplateTypeParameter)type).getDefault()));
} catch (DOMException e) {}
} else if (type instanceof IEnumeration) {
result.append(Keywords.ENUM);
} else if (type instanceof IFunctionType) {
try {
String temp = getType(((IFunctionType)type).getReturnType());
if (temp != null && !temp.equals(EMPTY_STRING)) { result.append(temp); needSpace=true; }
if (needSpace) { result.append(SPACE); needSpace=false; }
temp = getParameterTypeString((IFunctionType)type);
if (temp != null && !temp.equals(EMPTY_STRING)) { result.append(temp); needSpace=false; }
} catch (DOMException e) {}
} else if (type instanceof IPointerType) {
result.append(Keywords.cpSTAR); needSpace=true;
if (type instanceof IGPPPointerType) {
if (((IGPPPointerType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
} else if (type instanceof ICPointerType) {
if (((ICPointerType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
}
try {
if (((IPointerType)type).isConst()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CONST); needSpace=true; }
if (((IPointerType)type).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); needSpace=true; }
} catch (DOMException e) {}
} else if (type instanceof IQualifierType) {
if (type instanceof ICQualifierType) {
if (((ICQualifierType)type).isRestrict()) { result.append(Keywords.RESTRICT); needSpace=true; }
} else if (type instanceof IGPPQualifierType) {
if (((IGPPQualifierType)type).isRestrict()) { result.append(Keywords.RESTRICT); needSpace=true; }
}
try {
if (((IQualifierType)type).isConst()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CONST); needSpace=true; }
if (((IQualifierType)type).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); needSpace=true; }
} catch (DOMException e) {}
}
return result.toString();
}
/**
* Returns the type represntation of the IType as a String. This function uses the IType interfaces to build the
* String representation of the IType.
* @param type
* @return the type represntation of the IType
*/
public static String getType(IType type) {
StringBuffer result = new StringBuffer();
IType[] types = new IType[DEAULT_ITYPE_SIZE];
// push all of the types onto the stack
while(type != null && type instanceof ITypeContainer) {
types = (IType[]) ArrayUtil.append( IType.class, types, type );
try {
type = ((ITypeContainer)type).getType();
} catch (DOMException e) {}
}
if (type != null && !(type instanceof ITypeContainer)) {
types = (IType[]) ArrayUtil.append( IType.class, types, type );
}
// pop all of the types off of the stack, and build the string representation while doing so
for(int j=types.length-1; j>=0; j--) {
if (types[j] instanceof ITypedef)
continue;
if (types[j] != null && result.length() > 0) result.append(SPACE); // only add a space if this is not the first type being added
if (types[j] != null)
result.append(getTypeString(types[j]));
}
return result.toString();
}
/**
* Returns the type representation of the declarator (including parameters) as a String.
*
* @param decltor
* @return the type representation of the IASTDeclarator (including parameters)
*/
public static String getType(IASTDeclarator decltor) {
// get the most nested declarator
while(decltor.getNestedDeclarator() != null)
decltor = decltor.getNestedDeclarator();
IBinding binding = decltor.getName().resolveBinding();
IType type = null;
try {
if (binding instanceof CExternalFunction) {
type = ((CExternalFunction)binding).getType();
} else if (binding instanceof CExternalVariable) {
type = ((CExternalVariable)binding).getType();
} else if (binding instanceof IEnumerator) {
type = ((IEnumerator)binding).getType();
} else if (binding instanceof IFunction) {
type = ((IFunction)binding).getType();
} else if (binding instanceof ITypedef) {
type = ((ITypedef)binding).getType();
} else if (binding instanceof IVariable) {
type = ((IVariable)binding).getType();
}
} catch (DOMException e) {
return EMPTY_STRING;
}
if (type != null) {
return getType(type);
}
return EMPTY_STRING;
}
/**
* Return's the String representation of a node's type (if available). This is
* currently only being used for testing.
*
* TODO Remove this function when done testing if it is no longer needed
*
* @param node
* @return the String representation of a node's type (if available)
*/
public static String getNodeType(IASTNode node) {
try {
if (node instanceof IASTDeclarator)
return getType((IASTDeclarator)node);
if (node instanceof IASTName && ((IASTName)node).resolveBinding() instanceof IVariable)
return getType(((IVariable)((IASTName)node).resolveBinding()).getType());
if (node instanceof IASTName && ((IASTName)node).resolveBinding() instanceof IFunction)
return getType(((IFunction)((IASTName)node).resolveBinding()).getType());
if (node instanceof IASTName && ((IASTName)node).resolveBinding() instanceof IType)
return getType((IType)((IASTName)node).resolveBinding());
if (node instanceof IASTTypeId)
return getType((IASTTypeId)node);
} catch (DOMException e) { return EMPTY_STRING; }
return EMPTY_STRING;
}
/**
* Retuns the type representation of the IASTTypeId as a String.
*
* @param typeId
* @return the type representation of the IASTTypeId as a String
*/
public static String getType(IASTTypeId typeId) {
if (typeId instanceof CASTTypeId)
return createCType(typeId.getAbstractDeclarator());
else if (typeId instanceof CPPASTTypeId)
return createCPPType(typeId.getAbstractDeclarator());
return EMPTY_STRING;
}
private static String createCType(IASTDeclarator declarator) {
IType type = CVisitor.createType(declarator);
return getType(type);
}
private static String createCPPType(IASTDeclarator declarator) {
IType type = CPPVisitor.createType(declarator);
return getType(type);
}
}

View file

@ -1,482 +0,0 @@
/*******************************************************************************
* Copyright (c) 2005 Rational Software Corp. 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:
* Rational Software - initial implementation
******************************************************************************/
package org.eclipse.cdt.core.dom.ast;
import org.eclipse.cdt.core.dom.ast.c.ICArrayType;
import org.eclipse.cdt.core.dom.ast.c.ICBasicType;
import org.eclipse.cdt.core.dom.ast.c.ICPointerType;
import org.eclipse.cdt.core.dom.ast.c.ICQualifierType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTypeParameter;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPBasicType;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPPointerType;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPQualifierType;
import org.eclipse.cdt.core.parser.GCCKeywords;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
import org.eclipse.cdt.internal.core.dom.parser.c.CASTTypeId;
import org.eclipse.cdt.internal.core.dom.parser.c.CExternalFunction;
import org.eclipse.cdt.internal.core.dom.parser.c.CExternalVariable;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTypeId;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
/**
* This is a utility class to help convert AST elements to Strings.
*/
public class ASTUtil {
private static final String COMMA_SPACE = ", "; //$NON-NLS-1$
private static final String EMPTY_STRING = ""; //$NON-NLS-1$
private static final String SPACE = " "; //$NON-NLS-1$
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private static final int DEAULT_ITYPE_SIZE = 2;
public static String getParameterTypeString(IFunctionType type) {
StringBuffer result = new StringBuffer();
String[] parms = getParameterTypeStringArray(type);
result.append(Keywords.cpLPAREN);
for (int i = 0; i < parms.length; i++) {
if (parms[i] != null) {
result.append(parms[i]);
if (i < parms.length - 1)
result.append(COMMA_SPACE);
}
}
result.append(Keywords.cpRPAREN);
return result.toString();
}
public static String[] getParameterTypeStringArray(IFunctionType type) {
IType[] parms = null;
try {
parms = type.getParameterTypes();
} catch (DOMException e) {
return EMPTY_STRING_ARRAY;
}
String[] result = new String[parms.length];
for (int i = 0; i < parms.length; i++) {
if (parms[i] != null) {
result[i] = getType(parms[i]);
}
}
return result;
}
private static String getTypeString(IType type) {
StringBuffer result = new StringBuffer();
boolean needSpace = false;
if (type instanceof IArrayType) {
result.append(Keywords.cpLBRACKET);
if (type instanceof ICArrayType) {
try {
if (((ICArrayType) type).isConst()) {
result.append(Keywords.CONST);
needSpace = true;
}
if (((ICArrayType) type).isRestrict()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.RESTRICT);
needSpace = true;
}
if (((ICArrayType) type).isStatic()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.STATIC);
needSpace = true;
}
if (((ICArrayType) type).isVolatile()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.VOLATILE);
}
} catch (DOMException e) {
}
}
result.append(Keywords.cpRBRACKET);
} else if (type instanceof IBasicType) {
try {
if (((IBasicType) type).isSigned()) {
result.append(Keywords.SIGNED);
needSpace = true;
} else if (((IBasicType) type).isUnsigned()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.UNSIGNED);
needSpace = true;
}
if (((IBasicType) type).isLong()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.LONG);
needSpace = true;
} else if (((IBasicType) type).isShort()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.SHORT);
needSpace = true;
}
} catch (DOMException e) {
}
if (type instanceof IGPPBasicType) {
try {
if (((IGPPBasicType) type).isLongLong()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.LONG_LONG);
needSpace = true;
}
switch (((IGPPBasicType) type).getType()) {
case IGPPBasicType.t_Complex:
result.append(Keywords.c_COMPLEX);
break;
case IGPPBasicType.t_Imaginary:
result.append(Keywords.c_IMAGINARY);
break;
case IGPPBasicType.t_typeof:
result.append(GCCKeywords.TYPEOF);
break;
}
} catch (DOMException e) {
}
} else if (type instanceof ICPPBasicType) {
try {
switch (((ICPPBasicType) type).getType()) {
case ICPPBasicType.t_bool:
result.append(Keywords.BOOL);
break;
case ICPPBasicType.t_wchar_t:
result.append(Keywords.WCHAR_T);
break;
}
} catch (DOMException e) {
}
} else if (type instanceof ICBasicType) {
try {
switch (((ICBasicType) type).getType()) {
case ICBasicType.t_Bool:
result.append(Keywords.c_BOOL);
break;
case ICBasicType.t_Complex:
result.append(Keywords.c_COMPLEX);
break;
case ICBasicType.t_Imaginary:
result.append(Keywords.c_IMAGINARY);
break;
}
} catch (DOMException e) {
}
}
try {
switch (((IBasicType) type).getType()) {
case IBasicType.t_char:
result.append(Keywords.CHAR);
break;
case IBasicType.t_double:
result.append(Keywords.DOUBLE);
break;
case IBasicType.t_float:
result.append(Keywords.FLOAT);
break;
case IBasicType.t_int:
result.append(Keywords.INT);
break;
case IBasicType.t_void:
result.append(Keywords.VOID);
break;
}
} catch (DOMException e) {
}
} else if (type instanceof ICompositeType) {
if (type instanceof ICPPClassType) {
try {
switch (((ICPPClassType) type).getKey()) {
case ICPPClassType.k_class:
result.append(Keywords.CLASS);
break;
}
} catch (DOMException e) {
}
}
try {
switch (((ICompositeType) type).getKey()) {
case ICompositeType.k_struct:
result.append(Keywords.STRUCT);
break;
case ICompositeType.k_union:
result.append(Keywords.UNION);
break;
}
} catch (DOMException e) {
}
} else if (type instanceof ICPPReferenceType) {
result.append(Keywords.cpAMPER);
} else if (type instanceof ICPPTemplateTypeParameter) {
try {
result.append(getType(((ICPPTemplateTypeParameter) type)
.getDefault()));
} catch (DOMException e) {
}
} else if (type instanceof IEnumeration) {
result.append(Keywords.ENUM);
} else if (type instanceof IFunctionType) {
try {
String temp = getType(((IFunctionType) type).getReturnType());
if (temp != null && !temp.equals(EMPTY_STRING)) {
result.append(temp);
needSpace = true;
}
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
temp = getParameterTypeString((IFunctionType) type);
if (temp != null && !temp.equals(EMPTY_STRING)) {
result.append(temp);
needSpace = false;
}
} catch (DOMException e) {
}
} else if (type instanceof IPointerType) {
result.append(Keywords.cpSTAR);
needSpace = true;
if (type instanceof IGPPPointerType) {
if (((IGPPPointerType) type).isRestrict()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.RESTRICT);
needSpace = true;
}
} else if (type instanceof ICPointerType) {
if (((ICPointerType) type).isRestrict()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.RESTRICT);
needSpace = true;
}
}
try {
if (((IPointerType) type).isConst()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.CONST);
needSpace = true;
}
if (((IPointerType) type).isVolatile()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.VOLATILE);
needSpace = true;
}
} catch (DOMException e) {
}
} else if (type instanceof IQualifierType) {
if (type instanceof ICQualifierType) {
if (((ICQualifierType) type).isRestrict()) {
result.append(Keywords.RESTRICT);
needSpace = true;
}
} else if (type instanceof IGPPQualifierType) {
if (((IGPPQualifierType) type).isRestrict()) {
result.append(Keywords.RESTRICT);
needSpace = true;
}
}
try {
if (((IQualifierType) type).isConst()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.CONST);
needSpace = true;
}
if (((IQualifierType) type).isVolatile()) {
if (needSpace) {
result.append(SPACE);
needSpace = false;
}
result.append(Keywords.VOLATILE);
needSpace = true;
}
} catch (DOMException e) {
}
}
return result.toString();
}
public static String getType(IType type) {
StringBuffer result = new StringBuffer();
IType[] types = new IType[DEAULT_ITYPE_SIZE];
// push all of the types onto the stack
while (type != null && type instanceof ITypeContainer) {
types = (IType[]) ArrayUtil.append(IType.class, types, type);
try {
type = ((ITypeContainer) type).getType();
} catch (DOMException e) {
}
}
if (type != null && !(type instanceof ITypeContainer)) {
types = (IType[]) ArrayUtil.append(IType.class, types, type);
}
// pop all of the types off of the stack, and build the string
// representation while doing so
for (int j = types.length - 1; j >= 0; j--) {
if (types[j] != null)
result.append(getTypeString(types[j]));
if (types[j] != null && j > 0)
result.append(SPACE);
}
return result.toString();
}
public static String getDeclaratorType(IASTDeclarator decltor) {
// get the most nested declarator
while (decltor.getNestedDeclarator() != null)
decltor = decltor.getNestedDeclarator();
IBinding binding = decltor.getName().resolveBinding();
IType type = null;
try {
if (binding instanceof CExternalFunction) {
type = ((CExternalFunction) binding).getType();
} else if (binding instanceof CExternalVariable) {
type = ((CExternalVariable) binding).getType();
} else if (binding instanceof IEnumerator) {
type = ((IEnumerator) binding).getType();
} else if (binding instanceof IFunction) {
type = ((IFunction) binding).getType();
} else if (binding instanceof ITypedef) {
type = ((ITypedef) binding).getType();
} else if (binding instanceof IVariable) {
type = ((IVariable) binding).getType();
}
} catch (DOMException e) {
return EMPTY_STRING;
}
if (type != null) {
return getType(type);
}
return EMPTY_STRING;
}
/**
* Return's the String representation of a node's type (if available). This
* is currently only being used for testing.
*
* TODO Remove this function when done testing if it is no longer needed
*
* @param node
* @return
*/
public static String getNodeType(IASTNode node) {
try {
if (node instanceof IASTDeclarator)
return getDeclaratorType((IASTDeclarator) node);
if (node instanceof IASTName
&& ((IASTName) node).resolveBinding() instanceof IVariable)
return getType(((IVariable) ((IASTName) node).resolveBinding())
.getType());
if (node instanceof IASTName
&& ((IASTName) node).resolveBinding() instanceof IFunction)
return getType(((IFunction) ((IASTName) node).resolveBinding())
.getType());
if (node instanceof IASTName
&& ((IASTName) node).resolveBinding() instanceof IType)
return getType((IType) ((IASTName) node).resolveBinding());
if (node instanceof IASTTypeId)
return getType((IASTTypeId) node);
} catch (DOMException e) {
return EMPTY_STRING;
}
return EMPTY_STRING;
}
public static String getType(IASTTypeId typeId) {
if (typeId instanceof CASTTypeId)
return createCType(typeId.getAbstractDeclarator());
else if (typeId instanceof CPPASTTypeId)
return createCPPType(typeId.getAbstractDeclarator());
return EMPTY_STRING;
}
private static String createCType(IASTDeclarator declarator) {
IType type = CVisitor.createType(declarator);
return getType(type);
}
private static String createCPPType(IASTDeclarator declarator) {
IType type = CPPVisitor.createType(declarator);
return getType(type);
}
}

View file

@ -12,6 +12,7 @@ package org.eclipse.cdt.core.dom.ast.gnu.c;
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName;
@ -29,4 +30,5 @@ public interface ICASTKnRFunctionDeclarator extends IASTFunctionDeclarator {
public static final ASTNodeProperty FUNCTION_PARAMETER = new ASTNodeProperty( "Parameter"); //$NON-NLS-1$
public void setParameterDeclarations(IASTDeclaration[] decls);
public IASTDeclaration[] getParameterDeclarations();
public IASTDeclarator getDeclaratorForParameterName(IASTName name);
}

View file

@ -15,6 +15,12 @@ package org.eclipse.cdt.core.parser;
*/
public class Keywords {
public static final String CAST = "cast"; //$NON-NLS-1$
public static final String ALIGNOF = "alignof"; //$NON-NLS-1$
public static final String TYPEOF = "typeof"; //$NON-NLS-1$
public static final String cpMIN = "<?"; //$NON-NLS-1$
public static final String cpMAX = ">?"; //$NON-NLS-1$
public static final String _BOOL = "_Bool"; //$NON-NLS-1$
public static final String _COMPLEX = "_Complex"; //$NON-NLS-1$
public static final String _IMAGINARY = "_Imaginary"; //$NON-NLS-1$

View file

@ -12,7 +12,9 @@ package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
/**
@ -66,4 +68,27 @@ public class CASTKnRFunctionDeclarator extends CASTDeclarator implements ICASTKn
return true;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator#getDeclaratorForParameterName()
*/
public IASTDeclarator getDeclaratorForParameterName(IASTName name) {
boolean found=false;
for(int i=0; i<parameterNames.length; i++) {
if (parameterNames[i] == name) found = true;
}
if(!found) return null;
for(int i=0; i<parameterDeclarations.length; i++) {
if (parameterDeclarations[i] instanceof IASTSimpleDeclaration) {
IASTDeclarator[] decltors = ((IASTSimpleDeclaration)parameterDeclarations[i]).getDeclarators();
for(int j=0; j<decltors.length; j++) {
if(decltors[j].getName().toString().equals(name.toString()))
return decltors[j];
}
}
}
return null;
}
}

View file

@ -13,7 +13,8 @@ package org.eclipse.cdt.ui.tests.DOMAST;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.IASTServiceProvider;
import org.eclipse.cdt.core.dom.ast.ASTUtil;
import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
@ -33,6 +34,9 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.c.CASTVisitor;
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
@ -107,6 +111,9 @@ import org.eclipse.ui.part.ViewPart;
public class DOMAST extends ViewPart {
private static final String ASTUTIL_MENU_LABEL = "ASTUtil#"; //$NON-NLS-1$
private static final String DISPLAY_TYPE = "getNodeType(IASTNode)"; //$NON-NLS-1$
private static final String DISPLAY_SIGNATURE = "getNodeSignature(IASTNode)"; //$NON-NLS-1$
private static final String DISPLAY_EXPRESSION = "getExpressionString(IASTExpression)"; //$NON-NLS-1$
private static final String DISPLAY_INITIALIZER = "getInitializerString(IASTInitializer)"; //$NON-NLS-1$
private static final String NOT_VALID_COMPILATION_UNIT = "The active editor does not contain a valid compilation unit."; //$NON-NLS-1$
private static final String EXTENSION_CXX = "CXX"; //$NON-NLS-1$
private static final String EXTENSION_CPP = "CPP"; //$NON-NLS-1$
@ -129,6 +136,9 @@ public class DOMAST extends ViewPart {
private Action openDeclarationsAction;
private Action openReferencesAction;
private Action displayNodeTypeAction;
private Action displayNodeSignatureAction;
private Action displayExpressionAction;
private Action displayInitializerAction;
private Action singleClickAction;
private Action loadActiveEditorAction;
private Action refreshAction;
@ -638,25 +648,71 @@ public class DOMAST extends ViewPart {
MenuManager menuMgr = new MenuManager(POPUPMENU);
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
private void hideMenuItems(IMenuManager manager) {
IContributionItem[] items = manager.getItems();
for (int i = 0; i < items.length; i++) {
if (items[i] instanceof IMenuManager) {
hideMenuItems((IMenuManager)items[i]);
}
if (items[i] instanceof ActionContributionItem) {
String text = ((ActionContributionItem) items[i]).getAction().getText();
IASTNode selectedNode = null;
if (viewer.getSelection() instanceof StructuredSelection
&& ((StructuredSelection) viewer.getSelection())
.getFirstElement() instanceof TreeObject) {
selectedNode = ((TreeObject) ((StructuredSelection) viewer
.getSelection()).getFirstElement()).getNode();
}
if (text.equals(OPEN_REFERENCES) || text.equals(OPEN_DECLARATIONS)) {
if (selectedNode instanceof IASTName) {
items[i].setVisible(true);
} else {
items[i].setVisible(false);
}
}
if (text.equals(DISPLAY_SIGNATURE)) {
if (selectedNode instanceof IASTDeclarator ||
selectedNode instanceof IASTDeclSpecifier ||
selectedNode instanceof IASTTypeId) {
items[i].setVisible(true);
} else {
items[i].setVisible(false);
}
} else if (text.equals(DISPLAY_TYPE)) {
if (selectedNode instanceof IASTDeclarator ||
selectedNode instanceof IASTTypeId ||
(selectedNode instanceof IASTName && (
((IASTName)selectedNode).resolveBinding() instanceof IVariable ||
((IASTName)selectedNode).resolveBinding() instanceof IFunction ||
((IASTName)selectedNode).resolveBinding() instanceof IType))) {
items[i].setVisible(true);
} else {
items[i].setVisible(false);
}
} else if (text.equals(DISPLAY_EXPRESSION)) {
if (selectedNode instanceof IASTExpression) {
items[i].setVisible(true);
} else {
items[i].setVisible(false);
}
} else if (text.equals(DISPLAY_INITIALIZER)) {
if (selectedNode instanceof IASTInitializer) {
items[i].setVisible(true);
} else {
items[i].setVisible(false);
}
}
}
}
}
public void menuAboutToShow(IMenuManager manager) {
DOMAST.this.fillContextMenu(manager);
IContributionItem[] items = manager.getItems();
for (int i = 0; i < items.length; i++) {
if (items[i] instanceof ActionContributionItem
&& (((ActionContributionItem) items[i]).getAction()
.getText().equals(OPEN_REFERENCES) || ((ActionContributionItem) items[i])
.getAction().getText().equals(OPEN_DECLARATIONS))) {
if (viewer.getSelection() instanceof StructuredSelection
&& ((StructuredSelection) viewer.getSelection())
.getFirstElement() instanceof TreeObject
&& ((TreeObject) ((StructuredSelection) viewer
.getSelection()).getFirstElement()).getNode() instanceof IASTName) {
items[i].setVisible(true);
} else {
items[i].setVisible(false);
}
}
}
hideMenuItems(manager);
}
});
Menu menu = menuMgr.createContextMenu(viewer.getControl());
@ -681,6 +737,9 @@ public class DOMAST extends ViewPart {
// ASTUtil#... menu
MenuManager astMenu = new MenuManager(ASTUTIL_MENU_LABEL);
astMenu.add(displayNodeTypeAction);
astMenu.add(displayNodeSignatureAction);
astMenu.add(displayExpressionAction);
astMenu.add(displayInitializerAction);
manager.add(astMenu);
manager.add(new Separator());
drillDownAdapter.addNavigationActions(manager);
@ -787,13 +846,52 @@ public class DOMAST extends ViewPart {
if (selection instanceof IStructuredSelection &&
((IStructuredSelection)selection).getFirstElement() instanceof TreeObject &&
((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode() != null) {
showMessage("ASTUtil#getNodeType(IASTNode): \"" + ASTUtil.getNodeType(((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
showMessage("ASTUtil#getNodeType(IASTNode): \"" + ASTTypeUtil.getNodeType(((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
} };
displayNodeTypeAction.setText(DISPLAY_TYPE);
displayNodeTypeAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
.getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
displayNodeSignatureAction = new Action() {
public void run() {
ISelection selection = viewer.getSelection();
if (selection instanceof IStructuredSelection &&
((IStructuredSelection)selection).getFirstElement() instanceof TreeObject &&
((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode() != null) {
showMessage("ASTSignatureUtil#getNodeSignature(IASTNode): \"" + ASTSignatureUtil.getNodeSignature(((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
} };
displayNodeSignatureAction.setText(DISPLAY_SIGNATURE);
displayNodeSignatureAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
.getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
displayExpressionAction = new Action() {
public void run() {
ISelection selection = viewer.getSelection();
if (selection instanceof IStructuredSelection &&
((IStructuredSelection)selection).getFirstElement() instanceof TreeObject &&
((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode() instanceof IASTExpression) {
showMessage("ASTSignatureUtil#getExpressionString(IASTExpression): \"" + ASTSignatureUtil.getExpressionString((IASTExpression)((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
} };
displayExpressionAction.setText(DISPLAY_EXPRESSION);
displayExpressionAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
.getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
displayInitializerAction = new Action() {
public void run() {
ISelection selection = viewer.getSelection();
if (selection instanceof IStructuredSelection &&
((IStructuredSelection)selection).getFirstElement() instanceof TreeObject &&
((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode() instanceof IASTInitializer) {
showMessage("ASTSignatureUtil#getInitializerString(IASTInitializer): \"" + ASTSignatureUtil.getInitializerString((IASTInitializer)((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
} };
displayInitializerAction.setText(DISPLAY_INITIALIZER);
displayInitializerAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
.getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
singleClickAction = new ASTHighlighterAction(part);
}
@ -939,7 +1037,7 @@ public class DOMAST extends ViewPart {
void showMessage(String message) {
MessageDialog.openInformation(viewer.getControl().getShell(), VIEW_NAME,
message);
message.replaceAll("&", "&&")); //$NON-NLS-1$ //$NON-NLS-2$
}
/**

View file

@ -10,6 +10,7 @@
**********************************************************************/
package org.eclipse.cdt.ui.tests.DOMAST;
import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
@ -35,12 +36,7 @@ import org.eclipse.cdt.core.dom.ast.c.ICASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTFieldDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTArrayRangeDesignator;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer;
import org.eclipse.core.runtime.IAdaptable;
@ -48,61 +44,12 @@ import org.eclipse.core.runtime.IAdaptable;
* @author dsteffle
*/
public class TreeObject implements IAdaptable {
private static final String CAST = "cast"; //$NON-NLS-1$
private static final String STATIC_CAST = "static_cast"; //$NON-NLS-1$
private static final String REINTERPRET_CAST = "reinterpret_cast"; //$NON-NLS-1$
private static final String DYNAMIC_CAST = "dynamic_cast"; //$NON-NLS-1$
private static final String CONST_CAST = "const_cast"; //$NON-NLS-1$
private static final String OP_BRACKETEDPRIMARY = "( )"; //$NON-NLS-1$
private static final String OP_TILDE = "~"; //$NON-NLS-1$
private static final String OP_SIZEOF = "sizeof"; //$NON-NLS-1$
private static final String OP_INCR = "++"; //$NON-NLS-1$
private static final String OP_DECR = "--"; //$NON-NLS-1$
private static final String OP_NOT = "!"; //$NON-NLS-1$
private static final String OP_AMPER = "&"; //$NON-NLS-1$
private static final String OP_TYPEOF = "typeof"; //$NON-NLS-1$
private static final String OP_ALIGNOF = "alignof"; //$NON-NLS-1$
private static final String OP_TYPEID = "typeid"; //$NON-NLS-1$
private static final String OP_THROW = "throw"; //$NON-NLS-1$
private static final String VARIABLE_SIZED_ = "* "; //$NON-NLS-1$
private static final String VOLATILE_ = "volatile "; //$NON-NLS-1$
private static final String STATIC_ = "static "; //$NON-NLS-1$
private static final String RESTRICT_ = "restrict "; //$NON-NLS-1$
private static final String CONST_ = "const "; //$NON-NLS-1$
private static final String DASH = "-"; //$NON-NLS-1$
private static final String OP_NOTEQUALS = "!="; //$NON-NLS-1$
private static final String OP_EQUALS = "=="; //$NON-NLS-1$
private static final String OP_BINARYXORASSIGN = "^="; //$NON-NLS-1$
private static final String OP_BINARYANDASSIGN = "&="; //$NON-NLS-1$
private static final String OP_BINARYORASSIGN = "|="; //$NON-NLS-1$
private static final String OP_SHIFTRIGHTASSIGN = ">>="; //$NON-NLS-1$
private static final String OP_SHIFTLEFTASSIGN = "<<="; //$NON-NLS-1$
private static final String OP_MINUSASSIGN = "-="; //$NON-NLS-1$
private static final String OP_PLUSASSIGN = "+="; //$NON-NLS-1$
private static final String OP_MODULOASSIGN = "%="; //$NON-NLS-1$
private static final String OP_DIVIDEASSIGN = "/="; //$NON-NLS-1$
private static final String OP_MULTIPLYASSIGN = "*="; //$NON-NLS-1$
private static final String OP_ASSIGN = "="; //$NON-NLS-1$
private static final String OP_LOGICALOR = "||"; //$NON-NLS-1$
private static final String OP_LOGICALAND = "&&"; //$NON-NLS-1$
private static final String OP_BINARYOR = "|"; //$NON-NLS-1$
private static final String OP_BINARYXOR = "^"; //$NON-NLS-1$
private static final String OP_BINARYAND = "&"; //$NON-NLS-1$
private static final String OP_GREATEREQUAL = ">="; //$NON-NLS-1$
private static final String OP_LESSEQUAL = "<="; //$NON-NLS-1$
private static final String OP_GREATERTHAN = ">"; //$NON-NLS-1$
private static final String OP_LESSTHAN = "<"; //$NON-NLS-1$
private static final String OP_SHIFTRIGHT = ">>"; //$NON-NLS-1$
private static final String OP_SHIFTLEFT = "<<"; //$NON-NLS-1$
private static final String OP_MINUS = DASH; //$NON-NLS-1$
private static final String OP_PLUS = "+"; //$NON-NLS-1$
private static final String OP_MODULO = "%"; //$NON-NLS-1$
private static final String OP_DIVIDE = "/"; //$NON-NLS-1$
private static final String OP_STAR = "*"; //$NON-NLS-1$
private static final String OP_MIN = "<?"; //$NON-NLS-1$
private static final String OP_MAX = ">?"; //$NON-NLS-1$
private static final String OP_PMDOT = "."; //$NON-NLS-1$
private static final String OP_PMARROW = "->"; //$NON-NLS-1$
private static final String FILE_SEPARATOR = "\\"; //$NON-NLS-1$
public static final String BLANK_STRING = ""; //$NON-NLS-1$
private static final String IGCCAST_PREFIX = "IGCCAST"; //$NON-NLS-1$
@ -113,7 +60,7 @@ public class TreeObject implements IAdaptable {
private static final String IAST_PREFIX = "IAST"; //$NON-NLS-1$
private static final String START_OF_LIST = ": "; //$NON-NLS-1$
private static final String LIST_SEPARATOR = ", "; //$NON-NLS-1$
private static final String FILENAME_SEPARATOR = OP_PMDOT; //$NON-NLS-1$
private static final String FILENAME_SEPARATOR = "."; //$NON-NLS-1$
private IASTNode node = null;
private TreeParent parent;
@ -223,13 +170,13 @@ public class TreeObject implements IAdaptable {
buffer.append(node.toString());
} else if ( node instanceof IASTCastExpression ) {
buffer.append(START_OF_LIST);
buffer.append( getCastOperatorString( (IASTCastExpression)node ) );
buffer.append( ASTSignatureUtil.getCastOperatorString( (IASTCastExpression)node ) );
} else if ( node instanceof IASTUnaryExpression ) {
buffer.append(START_OF_LIST);
buffer.append( getUnaryOperatorString( (IASTUnaryExpression)node ) );
buffer.append( ASTSignatureUtil.getUnaryOperatorString( (IASTUnaryExpression)node ) );
} else if ( node instanceof IASTBinaryExpression ) {
buffer.append(START_OF_LIST);
buffer.append( getBinaryOperatorString( (IASTBinaryExpression)node ) );
buffer.append( ASTSignatureUtil.getBinaryOperatorString( (IASTBinaryExpression)node ) );
} else if ( node instanceof ICASTDesignator ) {
if ( node instanceof ICASTArrayDesignator && ((ICASTArrayDesignator)node).getSubscriptExpression() != null ) {
buffer.append(START_OF_LIST);
@ -297,230 +244,6 @@ public class TreeObject implements IAdaptable {
return buffer.toString();
}
/**
* @param expression
* @return
*/
private String getCastOperatorString(IASTCastExpression expression) {
int op = expression.getOperator();
String opString = BLANK_STRING;
if (expression instanceof ICPPASTCastExpression) {
switch (op) {
case ICPPASTCastExpression.op_const_cast:
opString = CONST_CAST;
break;
case ICPPASTCastExpression.op_dynamic_cast:
opString = DYNAMIC_CAST;
break;
case ICPPASTCastExpression.op_reinterpret_cast:
opString = REINTERPRET_CAST;
break;
case ICPPASTCastExpression.op_static_cast:
opString = STATIC_CAST;
break;
default:
break;
}
}
if (!opString.equals(BLANK_STRING)) return opString;
switch(op) {
case IASTCastExpression.op_cast:
opString = CAST;
break;
}
return opString;
}
private String getUnaryOperatorString(IASTUnaryExpression be) {
int op = be.getOperator();
String opString = BLANK_STRING;
if (be instanceof ICPPASTUnaryExpression) {
switch(op) {
case ICPPASTUnaryExpression.op_throw:
opString = OP_THROW;
break;
case ICPPASTUnaryExpression.op_typeid:
opString = OP_TYPEID;
break;
}
} else if (be instanceof IGNUASTUnaryExpression) {
switch(op) {
case IGNUASTUnaryExpression.op_alignOf:
opString = OP_ALIGNOF;
break;
case IGNUASTUnaryExpression.op_typeof:
opString = OP_TYPEOF;
break;
}
}
if (!opString.equals(BLANK_STRING)) return opString;
switch(op) {
case IASTUnaryExpression.op_amper:
opString = OP_AMPER;
break;
case IASTUnaryExpression.op_bracketedPrimary:
opString = OP_BRACKETEDPRIMARY;
break;
case IASTUnaryExpression.op_minus:
opString = OP_MINUS;
break;
case IASTUnaryExpression.op_not:
opString = OP_NOT;
break;
case IASTUnaryExpression.op_plus:
opString = OP_PLUS;
break;
case IASTUnaryExpression.op_postFixDecr:
opString = OP_DECR;
break;
case IASTUnaryExpression.op_postFixIncr:
opString = OP_INCR;
break;
case IASTUnaryExpression.op_prefixDecr:
opString = OP_DECR;
break;
case IASTUnaryExpression.op_prefixIncr:
opString = OP_INCR;
break;
case IASTUnaryExpression.op_sizeof:
opString = OP_SIZEOF;
break;
case IASTUnaryExpression.op_star:
opString = OP_STAR;
break;
case IASTUnaryExpression.op_tilde:
opString = OP_TILDE;
break;
}
return opString;
}
private String getBinaryOperatorString(IASTBinaryExpression be) {
int op = be.getOperator();
String opString = BLANK_STRING;
if (be instanceof ICPPASTBinaryExpression) {
switch(op) {
case ICPPASTBinaryExpression.op_pmarrow:
opString = OP_PMARROW;
break;
case ICPPASTBinaryExpression.op_pmdot:
opString = OP_PMDOT;
break;
}
} else if (be instanceof IGPPASTBinaryExpression) {
switch(op) {
case IGPPASTBinaryExpression.op_max:
opString = OP_MAX;
break;
case IGPPASTBinaryExpression.op_min:
opString = OP_MIN;
break;
}
}
if (!opString.equals(BLANK_STRING)) return opString;
switch(op) {
case IASTBinaryExpression.op_multiply:
opString = OP_STAR;
break;
case IASTBinaryExpression.op_divide:
opString = OP_DIVIDE;
break;
case IASTBinaryExpression.op_modulo:
opString = OP_MODULO;
break;
case IASTBinaryExpression.op_plus:
opString = OP_PLUS;
break;
case IASTBinaryExpression.op_minus:
opString = OP_MINUS;
break;
case IASTBinaryExpression.op_shiftLeft:
opString = OP_SHIFTLEFT;
break;
case IASTBinaryExpression.op_shiftRight:
opString = OP_SHIFTRIGHT;
break;
case IASTBinaryExpression.op_lessThan:
opString = OP_LESSTHAN;
break;
case IASTBinaryExpression.op_greaterThan:
opString = OP_GREATERTHAN;
break;
case IASTBinaryExpression.op_lessEqual:
opString = OP_LESSEQUAL;
break;
case IASTBinaryExpression.op_greaterEqual:
opString = OP_GREATEREQUAL;
break;
case IASTBinaryExpression.op_binaryAnd:
opString = OP_BINARYAND;
break;
case IASTBinaryExpression.op_binaryXor:
opString = OP_BINARYXOR;
break;
case IASTBinaryExpression.op_binaryOr:
opString = OP_BINARYOR;
break;
case IASTBinaryExpression.op_logicalAnd:
opString = OP_LOGICALAND;
break;
case IASTBinaryExpression.op_logicalOr:
opString = OP_LOGICALOR;
break;
case IASTBinaryExpression.op_assign:
opString = OP_ASSIGN;
break;
case IASTBinaryExpression.op_multiplyAssign:
opString = OP_MULTIPLYASSIGN;
break;
case IASTBinaryExpression.op_divideAssign:
opString = OP_DIVIDEASSIGN;
break;
case IASTBinaryExpression.op_moduloAssign:
opString = OP_MODULOASSIGN;
break;
case IASTBinaryExpression.op_plusAssign:
opString = OP_PLUSASSIGN;
break;
case IASTBinaryExpression.op_minusAssign:
opString = OP_MINUSASSIGN;
break;
case IASTBinaryExpression.op_shiftLeftAssign:
opString = OP_SHIFTLEFTASSIGN;
break;
case IASTBinaryExpression.op_shiftRightAssign:
opString = OP_SHIFTRIGHTASSIGN;
break;
case IASTBinaryExpression.op_binaryAndAssign:
opString = OP_BINARYANDASSIGN;
break;
case IASTBinaryExpression.op_binaryXorAssign:
opString = OP_BINARYXORASSIGN;
break;
case IASTBinaryExpression.op_binaryOrAssign:
opString = OP_BINARYORASSIGN;
break;
case IASTBinaryExpression.op_equals:
opString = OP_EQUALS;
break;
case IASTBinaryExpression.op_notequals:
opString = OP_NOTEQUALS;
break;
}
return opString;
}
private String getDeclaratorName(IASTDeclarator decltor) {
String name = BLANK_STRING;
while (decltor != null && decltor.getName() != null && decltor.getName().toString() == null) {