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

IASTTranslationUnit#getAllPreprocessorStatements() is now implemented.

This commit is contained in:
John Camelon 2005-02-10 22:11:04 +00:00
parent 8b9f51bf19
commit 131e9d4d75
11 changed files with 2026 additions and 1641 deletions

View file

@ -31,9 +31,13 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorEndifStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorFunctionStyleMacroDefinition; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorFunctionStyleMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfndefStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorObjectStyleMacroDefinition; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorObjectStyleMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorUndefStatement;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement; import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
@ -54,305 +58,386 @@ import org.eclipse.cdt.internal.core.parser.ParserException;
*/ */
public class DOMLocationTests extends AST2BaseTest { public class DOMLocationTests extends AST2BaseTest {
private static final String _TEXT_ = "<text>"; //$NON-NLS-1$ private static final String _TEXT_ = "<text>"; //$NON-NLS-1$
public void testBaseCase() throws ParserException {
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse("int x;", p); //$NON-NLS-1$
IASTDeclaration declaration = tu.getDeclarations()[0];
IASTNodeLocation[] nodeLocations = declaration.getNodeLocations();
assertNotNull(nodeLocations);
assertEquals(nodeLocations.length, 1);
assertTrue(nodeLocations[0] instanceof IASTFileLocation);
IASTFileLocation fileLocation = ((IASTFileLocation) nodeLocations[0]);
assertEquals(fileLocation.getFileName(), _TEXT_); //$NON-NLS-1$
assertEquals(fileLocation.getNodeOffset(), 0);
assertEquals(fileLocation.getNodeLength(), 6);
IASTNodeLocation[] tuLocations = tu.getNodeLocations();
assertEquals(tuLocations.length, nodeLocations.length);
assertEquals(fileLocation.getFileName(),
((IASTFileLocation) tuLocations[0]).getFileName()); //$NON-NLS-1$
assertEquals(fileLocation.getNodeOffset(), tuLocations[0]
.getNodeOffset());
assertEquals(fileLocation.getNodeLength(), tuLocations[0]
.getNodeLength());
}
}
public void testSimpleDeclaration() throws ParserException { public void testBaseCase() throws ParserException {
String code ="int xLen5, * yLength8, zLength16( int );"; //$NON-NLS-1$ for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP : null) {
: null) { IASTTranslationUnit tu = parse("int x;", p); //$NON-NLS-1$
IASTTranslationUnit tu = parse(code, p); IASTDeclaration declaration = tu.getDeclarations()[0];
IASTDeclaration[] declarations = tu.getDeclarations(); IASTNodeLocation[] nodeLocations = declaration.getNodeLocations();
assertEquals(declarations.length, 1); assertNotNull(nodeLocations);
IASTSimpleDeclaration declaration = (IASTSimpleDeclaration) declarations[0]; assertEquals(nodeLocations.length, 1);
IASTNodeLocation[] nodeLocations = declaration.getNodeLocations(); assertTrue(nodeLocations[0] instanceof IASTFileLocation);
assertNotNull(nodeLocations); IASTFileLocation fileLocation = ((IASTFileLocation) nodeLocations[0]);
assertEquals(nodeLocations.length, 1); assertEquals(fileLocation.getFileName(), _TEXT_); //$NON-NLS-1$
assertTrue(nodeLocations[0] instanceof IASTFileLocation); assertEquals(fileLocation.getNodeOffset(), 0);
IASTFileLocation fileLocation = ((IASTFileLocation) nodeLocations[0]); assertEquals(fileLocation.getNodeLength(), 6);
assertEquals(fileLocation.getFileName(), _TEXT_); //$NON-NLS-1$ IASTNodeLocation[] tuLocations = tu.getNodeLocations();
assertEquals(fileLocation.getNodeOffset(), 0); assertEquals(tuLocations.length, nodeLocations.length);
assertEquals(fileLocation.getNodeLength(), code.indexOf( ";") + 1); //$NON-NLS-1$ assertEquals(fileLocation.getFileName(),
IASTDeclarator[] declarators = declaration.getDeclarators(); ((IASTFileLocation) tuLocations[0]).getFileName()); //$NON-NLS-1$
assertEquals( declarators.length, 3 ); assertEquals(fileLocation.getNodeOffset(), tuLocations[0]
for( int i = 0; i < 3; ++i ) .getNodeOffset());
{ assertEquals(fileLocation.getNodeLength(), tuLocations[0]
IASTDeclarator declarator = declarators[i]; .getNodeLength());
switch( i ) }
{ }
case 0:
assertSoleLocation( declarator, code.indexOf( "xLen5"), "xLen5".length() ); //$NON-NLS-1$ //$NON-NLS-2$ public void testSimpleDeclaration() throws ParserException {
break; String code = "int xLen5, * yLength8, zLength16( int );"; //$NON-NLS-1$
case 1: for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
assertSoleLocation( declarator, code.indexOf( "* yLength8"), "* yLength8".length()); //$NON-NLS-1$ //$NON-NLS-2$ : null) {
break; IASTTranslationUnit tu = parse(code, p);
case 2: IASTDeclaration[] declarations = tu.getDeclarations();
assertSoleLocation( declarator, code.indexOf( "zLength16( int )"), "zLength16( int )".length() ); //$NON-NLS-1$ //$NON-NLS-2$ assertEquals(declarations.length, 1);
break; IASTSimpleDeclaration declaration = (IASTSimpleDeclaration) declarations[0];
IASTNodeLocation[] nodeLocations = declaration.getNodeLocations();
assertNotNull(nodeLocations);
assertEquals(nodeLocations.length, 1);
assertTrue(nodeLocations[0] instanceof IASTFileLocation);
IASTFileLocation fileLocation = ((IASTFileLocation) nodeLocations[0]);
assertEquals(fileLocation.getFileName(), _TEXT_); //$NON-NLS-1$
assertEquals(fileLocation.getNodeOffset(), 0);
assertEquals(fileLocation.getNodeLength(), code.indexOf(";") + 1); //$NON-NLS-1$
IASTDeclarator[] declarators = declaration.getDeclarators();
assertEquals(declarators.length, 3);
for (int i = 0; i < 3; ++i) {
IASTDeclarator declarator = declarators[i];
switch (i) {
case 0:
assertSoleLocation(declarator,
code.indexOf("xLen5"), "xLen5".length()); //$NON-NLS-1$ //$NON-NLS-2$
break;
case 1:
assertSoleLocation(declarator,
code.indexOf("* yLength8"), "* yLength8".length()); //$NON-NLS-1$ //$NON-NLS-2$
break;
case 2:
assertSoleLocation(
declarator,
code.indexOf("zLength16( int )"), "zLength16( int )".length()); //$NON-NLS-1$ //$NON-NLS-2$
break;
}
} }
}
} }
} }
public void testSimpleObjectStyleMacroDefinition() throws Exception {
String code = "/* hi */\n#define FOOT 0x01\n\n"; //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(code, p);
IASTDeclaration[] declarations = tu.getDeclarations();
assertEquals(declarations.length, 0);
IASTPreprocessorMacroDefinition[] macros = tu.getMacroDefinitions();
assertNotNull(macros);
assertEquals(macros.length, 1);
assertSoleLocation(
macros[0],
code.indexOf("#"), code.indexOf("0x01") + 4 - code.indexOf("#")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertTrue(macros[0] instanceof IASTPreprocessorObjectStyleMacroDefinition);
assertEquals(macros[0].getName().toString(), "FOOT"); //$NON-NLS-1$
assertEquals(macros[0].getExpansion(), "0x01"); //$NON-NLS-1$
}
}
public void testSimpleObjectStyleMacroDefinition() throws Exception { public void testSimpleFunctionStyleMacroDefinition() throws Exception {
String code ="/* hi */\n#define FOOT 0x01\n\n"; //$NON-NLS-1$ String code = "#define FOOBAH( WOOBAH ) JOHN##WOOBAH\n\n"; //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) { : null) {
IASTTranslationUnit tu = parse(code, p); IASTTranslationUnit tu = parse(code, p);
IASTDeclaration[] declarations = tu.getDeclarations(); IASTDeclaration[] declarations = tu.getDeclarations();
assertEquals(declarations.length, 0); assertEquals(declarations.length, 0);
IASTPreprocessorMacroDefinition [] macros = tu.getMacroDefinitions(); IASTPreprocessorMacroDefinition[] macros = tu.getMacroDefinitions();
assertNotNull( macros ); assertNotNull(macros);
assertEquals( macros.length, 1 ); assertEquals(macros.length, 1);
assertSoleLocation( macros[0], code.indexOf( "#"), code.indexOf( "0x01") + 4 - code.indexOf( "#")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ assertTrue(macros[0] instanceof IASTPreprocessorFunctionStyleMacroDefinition);
assertTrue( macros[0] instanceof IASTPreprocessorObjectStyleMacroDefinition ); assertSoleLocation(
assertEquals( macros[0].getName().toString(), "FOOT" ); //$NON-NLS-1$ macros[0],
assertEquals( macros[0].getExpansion(), "0x01"); //$NON-NLS-1$ code.indexOf("#define"), code.indexOf("##WOOBAH") + 8 - code.indexOf("#define")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$s
} assertEquals(macros[0].getName().toString(), "FOOBAH"); //$NON-NLS-1$
} assertEquals(macros[0].getExpansion(), "JOHN##WOOBAH"); //$NON-NLS-1$
IASTFunctionStyleMacroParameter[] parms = ((IASTPreprocessorFunctionStyleMacroDefinition) macros[0])
.getParameters();
assertNotNull(parms);
assertEquals(parms.length, 1);
assertEquals(parms[0].getParameter(), "WOOBAH"); //$NON-NLS-1$
}
}
public void testSimpleFunctionStyleMacroDefinition() throws Exception { /**
String code = "#define FOOBAH( WOOBAH ) JOHN##WOOBAH\n\n"; //$NON-NLS-1$ * @param declarator
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP * @param offset
: null) { * @param length
IASTTranslationUnit tu = parse(code, p); */
IASTDeclaration[] declarations = tu.getDeclarations(); private void assertSoleLocation(IASTNode n, int offset, int length) {
assertEquals(declarations.length, 0); IASTNodeLocation[] locations = n.getNodeLocations();
IASTPreprocessorMacroDefinition [] macros = tu.getMacroDefinitions(); assertEquals(1, locations.length);
assertNotNull( macros ); IASTNodeLocation nodeLocation = locations[0];
assertEquals( macros.length, 1 ); assertEquals(offset, nodeLocation.getNodeOffset());
assertTrue( macros[0] instanceof IASTPreprocessorFunctionStyleMacroDefinition ); assertEquals(length, nodeLocation.getNodeLength());
assertSoleLocation( macros[0], code.indexOf( "#define"), code.indexOf( "##WOOBAH") + 8 - code.indexOf( "#define")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$s }
assertEquals( macros[0].getName().toString(), "FOOBAH" ); //$NON-NLS-1$
assertEquals( macros[0].getExpansion(), "JOHN##WOOBAH"); //$NON-NLS-1$
IASTFunctionStyleMacroParameter [] parms = ((IASTPreprocessorFunctionStyleMacroDefinition)macros[0]).getParameters();
assertNotNull( parms );
assertEquals( parms.length, 1 );
assertEquals( parms[0].getParameter(), "WOOBAH" ); //$NON-NLS-1$
}
} public void testBug83664() throws Exception {
String code = "int foo(x) int x; {\n return x;\n }\n"; //$NON-NLS-1$
IASTTranslationUnit tu = parse(code, ParserLanguage.C);
IASTDeclaration[] declarations = tu.getDeclarations();
assertEquals(declarations.length, 1);
IASTFunctionDefinition definition = (IASTFunctionDefinition) declarations[0];
IASTFunctionDeclarator declarator = definition.getDeclarator();
assertSoleLocation(
declarator,
code.indexOf("foo"), code.indexOf("int x;") + 6 - code.indexOf("foo")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
IASTCompoundStatement body = (IASTCompoundStatement) definition
.getBody();
assertEquals(body.getStatements().length, 1);
IASTReturnStatement returnStatement = (IASTReturnStatement) body
.getStatements()[0];
IASTIdExpression expression = (IASTIdExpression) returnStatement
.getReturnValue();
assertSoleLocation(expression,
code.indexOf("return ") + "return ".length(), 1); //$NON-NLS-1$ //$NON-NLS-2$
}
/** public void testBug84343() throws Exception {
* @param declarator String code = "class A {}; int f() {\nA * b = 0;\nreturn b;}"; //$NON-NLS-1$
* @param offset IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
* @param length IASTFunctionDefinition f = (IASTFunctionDefinition) tu
*/ .getDeclarations()[1];
private void assertSoleLocation(IASTNode n, int offset, int length) { IASTDeclarationStatement ds = (IASTDeclarationStatement) ((IASTCompoundStatement) f
IASTNodeLocation [] locations = n.getNodeLocations(); .getBody()).getStatements()[0];
assertEquals( 1, locations.length ); IASTSimpleDeclaration b = (IASTSimpleDeclaration) ds.getDeclaration();
IASTNodeLocation nodeLocation = locations[0]; ICPPASTNamedTypeSpecifier namedTypeSpec = (ICPPASTNamedTypeSpecifier) b
assertEquals( offset, nodeLocation.getNodeOffset() ); .getDeclSpecifier();
assertEquals( length, nodeLocation.getNodeLength() ); assertSoleLocation(namedTypeSpec, code.indexOf("\nA") + 1, 1); //$NON-NLS-1$
} }
public void testBug83664() throws Exception { public void testBug84366() throws Exception {
String code = "int foo(x) int x; {\n return x;\n }\n"; //$NON-NLS-1$ String code = "enum hue { red, blue, green };"; //$NON-NLS-1$
IASTTranslationUnit tu = parse( code, ParserLanguage.C ); IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTDeclaration [] declarations = tu.getDeclarations(); IASTSimpleDeclaration d = (IASTSimpleDeclaration) tu.getDeclarations()[0];
assertEquals( declarations.length, 1 ); IASTEnumerationSpecifier enum = (IASTEnumerationSpecifier) d
IASTFunctionDefinition definition = (IASTFunctionDefinition) declarations[0]; .getDeclSpecifier();
IASTFunctionDeclarator declarator = definition.getDeclarator(); IASTEnumerationSpecifier.IASTEnumerator enumerator = enum
assertSoleLocation( declarator, code.indexOf( "foo" ), code.indexOf( "int x;") + 6 - code.indexOf( "foo")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ .getEnumerators()[0];
IASTCompoundStatement body = (IASTCompoundStatement) definition.getBody(); assertSoleLocation(enumerator, code.indexOf("red"), "red".length()); //$NON-NLS-1$ //$NON-NLS-2$
assertEquals( body.getStatements().length, 1 ); }
IASTReturnStatement returnStatement= (IASTReturnStatement) body.getStatements()[0];
IASTIdExpression expression = (IASTIdExpression) returnStatement.getReturnValue();
assertSoleLocation( expression, code.indexOf( "return ") + "return ".length(), 1 ); //$NON-NLS-1$ //$NON-NLS-2$
}
public void testBug84343() throws Exception { public void testBug84375() throws Exception {
String code = "class A {}; int f() {\nA * b = 0;\nreturn b;}"; //$NON-NLS-1$ String code = "class D { public: int x; };\nclass C : public virtual D {};"; //$NON-NLS-1$
IASTTranslationUnit tu = parse( code, ParserLanguage.CPP ); IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTFunctionDefinition f = (IASTFunctionDefinition) tu.getDeclarations()[1]; IASTSimpleDeclaration d2 = (IASTSimpleDeclaration) tu.getDeclarations()[1];
IASTDeclarationStatement ds = (IASTDeclarationStatement) ((IASTCompoundStatement)f.getBody()).getStatements()[0]; ICPPASTCompositeTypeSpecifier classSpec = (ICPPASTCompositeTypeSpecifier) d2
IASTSimpleDeclaration b = (IASTSimpleDeclaration) ds.getDeclaration(); .getDeclSpecifier();
ICPPASTNamedTypeSpecifier namedTypeSpec = (ICPPASTNamedTypeSpecifier) b.getDeclSpecifier(); ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier [] bases = classSpec
assertSoleLocation( namedTypeSpec, code.indexOf( "\nA") + 1, 1 ); //$NON-NLS-1$ .getBaseSpecifiers();
} assertSoleLocation(bases[0],
code.indexOf("public virtual D"), "public virtual D".length()); //$NON-NLS-1$ //$NON-NLS-2$
public void testBug84366() throws Exception { }
String code = "enum hue { red, blue, green };"; //$NON-NLS-1$
IASTTranslationUnit tu = parse( code, ParserLanguage.CPP );
IASTSimpleDeclaration d = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTEnumerationSpecifier enum = (IASTEnumerationSpecifier) d.getDeclSpecifier();
IASTEnumerationSpecifier.IASTEnumerator enumerator = enum.getEnumerators()[0];
assertSoleLocation( enumerator, code.indexOf( "red"), "red".length() ); //$NON-NLS-1$ //$NON-NLS-2$
}
public void testBug84375() throws Exception { public void testBug84357() throws Exception {
String code = "class D { public: int x; };\nclass C : public virtual D {};"; //$NON-NLS-1$ String code = "class X { int a;\n};\nint X:: * pmi = &X::a;"; //$NON-NLS-1$
IASTTranslationUnit tu = parse( code, ParserLanguage.CPP ); IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTSimpleDeclaration d2 = (IASTSimpleDeclaration) tu.getDeclarations()[1]; IASTSimpleDeclaration pmi = (IASTSimpleDeclaration) tu
ICPPASTCompositeTypeSpecifier classSpec = (ICPPASTCompositeTypeSpecifier) d2.getDeclSpecifier(); .getDeclarations()[1];
ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier [] bases = classSpec.getBaseSpecifiers(); IASTDeclarator d = pmi.getDeclarators()[0];
assertSoleLocation( bases[0], code.indexOf( "public virtual D"), "public virtual D".length() ); //$NON-NLS-1$ //$NON-NLS-2$ IASTPointerOperator p = d.getPointerOperators()[0];
assertSoleLocation(p, code.indexOf("X:: *"), "X:: *".length()); //$NON-NLS-1$ //$NON-NLS-2$
}
public void testBug84367() throws Exception {
String code = "void foo( int );"; //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(code, p);
IASTSimpleDeclaration definition = (IASTSimpleDeclaration) tu
.getDeclarations()[0];
IASTStandardFunctionDeclarator declarator = (IASTStandardFunctionDeclarator) definition
.getDeclarators()[0];
IASTParameterDeclaration parameter = declarator.getParameters()[0];
assertSoleLocation(parameter, code.indexOf("int"), 3); //$NON-NLS-1$
} }
}
public void testBug84357() throws Exception { public void testElaboratedTypeSpecifier() throws ParserException {
String code = "class X { int a;\n};\nint X:: * pmi = &X::a;"; //$NON-NLS-1$ String code = "/* blah */ struct A anA; /* blah */"; //$NON-NLS-1$
IASTTranslationUnit tu = parse( code, ParserLanguage.CPP ); for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
IASTSimpleDeclaration pmi = (IASTSimpleDeclaration) tu.getDeclarations()[1]; : null) {
IASTDeclarator d = pmi.getDeclarators()[0]; IASTTranslationUnit tu = parse(code, p);
IASTPointerOperator p = d.getPointerOperators()[0]; IASTSimpleDeclaration declaration = (IASTSimpleDeclaration) tu
assertSoleLocation( p, code.indexOf( "X:: *") , "X:: *".length()); //$NON-NLS-1$ //$NON-NLS-2$ .getDeclarations()[0];
} IASTElaboratedTypeSpecifier elabType = (IASTElaboratedTypeSpecifier) declaration
public void testBug84367() throws Exception { .getDeclSpecifier();
String code = "void foo( int );"; //$NON-NLS-1$ assertSoleLocation(elabType,
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP code.indexOf("struct"), "struct A".length()); //$NON-NLS-1$ //$NON-NLS-2$
: null) { }
IASTTranslationUnit tu = parse(code, p); }
IASTSimpleDeclaration definition = (IASTSimpleDeclaration) tu.getDeclarations()[0];
IASTStandardFunctionDeclarator declarator = (IASTStandardFunctionDeclarator) definition.getDeclarators()[0];
IASTParameterDeclaration parameter = declarator.getParameters()[0];
assertSoleLocation( parameter, code.indexOf( "int" ), 3 ); //$NON-NLS-1$
} public void testBug83852() throws Exception {
} String code = "/* blah */ typedef short jc; int x = 4; jc myJc = (jc)x; "; //$NON-NLS-1$
public void testElaboratedTypeSpecifier() throws ParserException { for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
String code = "/* blah */ struct A anA; /* blah */"; //$NON-NLS-1$ : null) {
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP IASTTranslationUnit tu = parse(code, p);
: null) { IASTDeclaration[] declarations = tu.getDeclarations();
IASTTranslationUnit tu = parse(code, p); assertEquals(3, declarations.length);
IASTSimpleDeclaration declaration = (IASTSimpleDeclaration) tu.getDeclarations()[0]; for (int i = 0; i < 3; ++i) {
IASTElaboratedTypeSpecifier elabType = (IASTElaboratedTypeSpecifier) declaration.getDeclSpecifier(); IASTSimpleDeclaration decl = (IASTSimpleDeclaration) declarations[i];
assertSoleLocation( elabType, code.indexOf( "struct"), "struct A".length() ); //$NON-NLS-1$ //$NON-NLS-2$ int start = 0, length = 0;
} switch (i) {
} case 0:
start = code.indexOf("typedef"); //$NON-NLS-1$
length = "typedef short jc;".length(); //$NON-NLS-1$
public void testBug83852() throws Exception { break;
String code = "/* blah */ typedef short jc; int x = 4; jc myJc = (jc)x; "; //$NON-NLS-1$ case 1:
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP start = code.indexOf("int x = 4;"); //$NON-NLS-1$
: null) { length = "int x = 4;".length(); //$NON-NLS-1$
IASTTranslationUnit tu = parse(code, p); break;
IASTDeclaration [] declarations = tu.getDeclarations(); case 2:
assertEquals( 3, declarations.length ); start = code.indexOf("jc myJc = (jc)x;"); //$NON-NLS-1$
for( int i = 0; i < 3; ++i ) length = "jc myJc = (jc)x;".length(); //$NON-NLS-1$
{ break;
IASTSimpleDeclaration decl = (IASTSimpleDeclaration) declarations[i]; }
int start = 0, length = 0; assertSoleLocation(decl, start, length);
switch( i )
{
case 0:
start = code.indexOf( "typedef"); //$NON-NLS-1$
length = "typedef short jc;".length(); //$NON-NLS-1$
break;
case 1:
start = code.indexOf( "int x = 4;"); //$NON-NLS-1$
length = "int x = 4;".length(); //$NON-NLS-1$
break;
case 2:
start = code.indexOf( "jc myJc = (jc)x;"); //$NON-NLS-1$
length = "jc myJc = (jc)x;".length(); //$NON-NLS-1$
break;
} }
assertSoleLocation( decl, start, length ); IASTInitializerExpression initializer = (IASTInitializerExpression) ((IASTSimpleDeclaration) declarations[2])
} .getDeclarators()[0].getInitializer();
IASTInitializerExpression initializer = (IASTInitializerExpression) ((IASTSimpleDeclaration)declarations[2]).getDeclarators()[0].getInitializer(); IASTCastExpression castExpression = (IASTCastExpression) initializer
IASTCastExpression castExpression = (IASTCastExpression) initializer.getExpression(); .getExpression();
IASTTypeId typeId = castExpression.getTypeId(); IASTTypeId typeId = castExpression.getTypeId();
assertSoleLocation( typeId, code.indexOf( "(jc)") + 1, "jc".length() ); //$NON-NLS-1$ //$NON-NLS-2$ assertSoleLocation(typeId, code.indexOf("(jc)") + 1, "jc".length()); //$NON-NLS-1$ //$NON-NLS-2$
} }
} }
public void testBug83853() throws ParserException { public void testBug83853() throws ParserException {
String code = "int f() {return (1?0:1); }"; //$NON-NLS-1$ String code = "int f() {return (1?0:1); }"; //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) { : null) {
IASTTranslationUnit tu = parse(code, p); IASTTranslationUnit tu = parse(code, p);
IASTFunctionDefinition definition = (IASTFunctionDefinition) tu.getDeclarations()[0]; IASTFunctionDefinition definition = (IASTFunctionDefinition) tu
IASTCompoundStatement statement = (IASTCompoundStatement) definition.getBody(); .getDeclarations()[0];
IASTReturnStatement returnStatement = (IASTReturnStatement) statement.getStatements()[0]; IASTCompoundStatement statement = (IASTCompoundStatement) definition
IASTUnaryExpression unaryExpression = (IASTUnaryExpression) returnStatement.getReturnValue(); .getBody();
assertEquals( unaryExpression.getOperator(), IASTUnaryExpression.op_bracketedPrimary ); IASTReturnStatement returnStatement = (IASTReturnStatement) statement
IASTConditionalExpression conditional = (IASTConditionalExpression) unaryExpression.getOperand(); .getStatements()[0];
assertSoleLocation( conditional,code.indexOf( "1?0:1"), "1?0:1".length() ); //$NON-NLS-1$ //$NON-NLS-2$ IASTUnaryExpression unaryExpression = (IASTUnaryExpression) returnStatement
} .getReturnValue();
} assertEquals(unaryExpression.getOperator(),
IASTUnaryExpression.op_bracketedPrimary);
IASTConditionalExpression conditional = (IASTConditionalExpression) unaryExpression
.getOperand();
assertSoleLocation(conditional,
code.indexOf("1?0:1"), "1?0:1".length()); //$NON-NLS-1$ //$NON-NLS-2$
}
}
public void testBug84374() throws Exception public void testBug84374() throws Exception {
{ String code = "class P1 { public: int x; };\nclass P2 { public: int x; };\nclass B : public P1, public P2 {};\nvoid main() {\nB * b = new B();\n}"; //$NON-NLS-1$
String code = "class P1 { public: int x; };\nclass P2 { public: int x; };\nclass B : public P1, public P2 {};\nvoid main() {\nB * b = new B();\n}"; //$NON-NLS-1$ IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTTranslationUnit tu = parse( code, ParserLanguage.CPP ); IASTFunctionDefinition main = (IASTFunctionDefinition) tu
IASTFunctionDefinition main = (IASTFunctionDefinition) tu.getDeclarations()[3]; .getDeclarations()[3];
IASTCompoundStatement statement = (IASTCompoundStatement) main.getBody(); IASTCompoundStatement statement = (IASTCompoundStatement) main
IASTDeclarationStatement decl = (IASTDeclarationStatement) statement.getStatements()[0]; .getBody();
IASTSimpleDeclaration b = (IASTSimpleDeclaration) decl.getDeclaration(); IASTDeclarationStatement decl = (IASTDeclarationStatement) statement
IASTInitializerExpression initializerExpression = (IASTInitializerExpression) b.getDeclarators()[0].getInitializer(); .getStatements()[0];
assertSoleLocation( initializerExpression, code.indexOf( "new B()"), "new B()".length() ); //$NON-NLS-1$ //$NON-NLS-2$ IASTSimpleDeclaration b = (IASTSimpleDeclaration) decl.getDeclaration();
ICPPASTNewExpression newExpression = (ICPPASTNewExpression) initializerExpression.getExpression(); IASTInitializerExpression initializerExpression = (IASTInitializerExpression) b
assertSoleLocation( newExpression, code.indexOf( "new B()"), "new B()".length() ); //$NON-NLS-1$ //$NON-NLS-2$ .getDeclarators()[0].getInitializer();
} assertSoleLocation(initializerExpression,
code.indexOf("new B()"), "new B()".length()); //$NON-NLS-1$ //$NON-NLS-2$
ICPPASTNewExpression newExpression = (ICPPASTNewExpression) initializerExpression
.getExpression();
assertSoleLocation(newExpression,
code.indexOf("new B()"), "new B()".length()); //$NON-NLS-1$ //$NON-NLS-2$
}
public void testBug83737() throws Exception { public void testBug83737() throws Exception {
String code = "void f() { if( a == 0 ) g( a ); else if( a < 0 ) g( a >> 1 ); else if( a > 0 ) g( *(&a + 2) ); }"; //$NON-NLS-1$ String code = "void f() { if( a == 0 ) g( a ); else if( a < 0 ) g( a >> 1 ); else if( a > 0 ) g( *(&a + 2) ); }"; //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) { : null) {
IASTTranslationUnit tu = parse(code, p); IASTTranslationUnit tu = parse(code, p);
IASTFunctionDefinition definition = (IASTFunctionDefinition) tu.getDeclarations()[0]; IASTFunctionDefinition definition = (IASTFunctionDefinition) tu
IASTCompoundStatement statement = (IASTCompoundStatement) definition.getBody(); .getDeclarations()[0];
IASTIfStatement first_if = (IASTIfStatement) statement.getStatements()[0]; IASTCompoundStatement statement = (IASTCompoundStatement) definition
IASTIfStatement second_if = (IASTIfStatement) first_if.getElseClause(); .getBody();
IASTIfStatement third_if = (IASTIfStatement) second_if.getElseClause(); IASTIfStatement first_if = (IASTIfStatement) statement
assertNull( third_if.getElseClause() ); .getStatements()[0];
int first_if_start = code.indexOf( "if( a == 0 )" ); //$NON-NLS-1$ IASTIfStatement second_if = (IASTIfStatement) first_if
int total_if_length = "if( a == 0 ) g( a ); else if( a < 0 ) g( a >> 1 ); else if( a > 0 ) g( *(&a + 2) );".length(); //$NON-NLS-1$ .getElseClause();
int total_if_end = first_if_start + total_if_length; IASTIfStatement third_if = (IASTIfStatement) second_if
int second_if_start = code.indexOf( "if( a < 0 )"); //$NON-NLS-1$ .getElseClause();
int third_if_start = code.indexOf( "if( a > 0 )"); //$NON-NLS-1$ assertNull(third_if.getElseClause());
assertSoleLocation( first_if, first_if_start, total_if_length ); int first_if_start = code.indexOf("if( a == 0 )"); //$NON-NLS-1$
assertSoleLocation( second_if, second_if_start, total_if_end - second_if_start ); int total_if_length = "if( a == 0 ) g( a ); else if( a < 0 ) g( a >> 1 ); else if( a > 0 ) g( *(&a + 2) );".length(); //$NON-NLS-1$
assertSoleLocation( third_if, third_if_start, total_if_end - third_if_start ); int total_if_end = first_if_start + total_if_length;
} int second_if_start = code.indexOf("if( a < 0 )"); //$NON-NLS-1$
} int third_if_start = code.indexOf("if( a > 0 )"); //$NON-NLS-1$
assertSoleLocation(first_if, first_if_start, total_if_length);
assertSoleLocation(second_if, second_if_start, total_if_end
- second_if_start);
assertSoleLocation(third_if, third_if_start, total_if_end
- third_if_start);
}
}
public void testBug84467() throws Exception public void testBug84467() throws Exception {
{ String code = "class D { };\n D d1;\n const D d2;\n void foo() {\n typeid(d1) == typeid(d2);\n }"; //$NON-NLS-1$
String code = "class D { };\n D d1;\n const D d2;\n void foo() {\n typeid(d1) == typeid(d2);\n }"; //$NON-NLS-1$ IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTTranslationUnit tu = parse( code, ParserLanguage.CPP ); IASTBinaryExpression bexp = (IASTBinaryExpression) ((IASTExpressionStatement) ((IASTCompoundStatement) ((IASTFunctionDefinition) tu
IASTBinaryExpression bexp = (IASTBinaryExpression)((IASTExpressionStatement)((IASTCompoundStatement)((IASTFunctionDefinition)tu.getDeclarations()[3]).getBody()).getStatements()[0]).getExpression(); .getDeclarations()[3]).getBody()).getStatements()[0])
IASTTypeIdExpression exp = (IASTTypeIdExpression)((IASTBinaryExpression)((IASTExpressionStatement)((IASTCompoundStatement)((IASTFunctionDefinition)tu.getDeclarations()[3]).getBody()).getStatements()[0]).getExpression()).getOperand1(); .getExpression();
IASTTypeIdExpression exp = (IASTTypeIdExpression) ((IASTBinaryExpression) ((IASTExpressionStatement) ((IASTCompoundStatement) ((IASTFunctionDefinition) tu
.getDeclarations()[3]).getBody()).getStatements()[0])
.getExpression()).getOperand1();
assertSoleLocation( bexp, code.indexOf( "typeid(d1) == typeid(d2)"), "typeid(d1) == typeid(d2)".length() ); //$NON-NLS-1$ //$NON-NLS-2$ assertSoleLocation(
assertSoleLocation( exp, code.indexOf( "typeid(d1)"), "typeid(d1)".length() ); //$NON-NLS-1$ //$NON-NLS-2$ bexp,
exp = (IASTTypeIdExpression)((IASTBinaryExpression)((IASTExpressionStatement)((IASTCompoundStatement)((IASTFunctionDefinition)tu.getDeclarations()[3]).getBody()).getStatements()[0]).getExpression()).getOperand2(); code.indexOf("typeid(d1) == typeid(d2)"), "typeid(d1) == typeid(d2)".length()); //$NON-NLS-1$ //$NON-NLS-2$
assertSoleLocation( exp, code.indexOf( "typeid(d2)"), "typeid(d2)".length() ); //$NON-NLS-1$ //$NON-NLS-2$ assertSoleLocation(exp,
} code.indexOf("typeid(d1)"), "typeid(d1)".length()); //$NON-NLS-1$ //$NON-NLS-2$
exp = (IASTTypeIdExpression) ((IASTBinaryExpression) ((IASTExpressionStatement) ((IASTCompoundStatement) ((IASTFunctionDefinition) tu
.getDeclarations()[3]).getBody()).getStatements()[0])
.getExpression()).getOperand2();
assertSoleLocation(exp,
code.indexOf("typeid(d2)"), "typeid(d2)".length()); //$NON-NLS-1$ //$NON-NLS-2$
}
public void testBug84576() throws Exception public void testBug84576() throws Exception {
{ String code = "namespace A {\n extern \"C\" int g();\n }"; //$NON-NLS-1$
String code = "namespace A {\n extern \"C\" int g();\n }"; //$NON-NLS-1$ IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTTranslationUnit tu = parse( code, ParserLanguage.CPP ); ICPPASTLinkageSpecification spec = (ICPPASTLinkageSpecification) ((ICPPASTNamespaceDefinition) tu
ICPPASTLinkageSpecification spec = (ICPPASTLinkageSpecification)((ICPPASTNamespaceDefinition)tu.getDeclarations()[0]).getDeclarations()[0]; .getDeclarations()[0]).getDeclarations()[0];
assertSoleLocation( spec, code.indexOf( "extern \"C\""), "extern \"C\" int g();".length() ); //$NON-NLS-1$ //$NON-NLS-2$ assertSoleLocation(spec,
} code.indexOf("extern \"C\""), "extern \"C\" int g();".length()); //$NON-NLS-1$ //$NON-NLS-2$
}
public void testSimplePreprocessorStatements() throws Exception
{
StringBuffer buffer = new StringBuffer();
buffer.append( "#ifndef _APPLE_H_\n"); //$NON-NLS-1$
buffer.append( "#define _APPLE_H_\n"); //$NON-NLS-1$
buffer.append( "#undef _APPLE_H_\n"); //$NON-NLS-1$
buffer.append( "#endif\n"); //$NON-NLS-1$
String code = buffer.toString();
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(code, p);
assertEquals( tu.getDeclarations().length, 0 );
IASTPreprocessorStatement [] statements = tu.getAllPreprocessorStatements();
assertEquals( statements.length, 4 );
IASTPreprocessorIfndefStatement ifndef = (IASTPreprocessorIfndefStatement) statements[0];
assertTrue( ifndef.taken() );
assertSoleLocation( ifndef, code.indexOf( "#ifndef _APPLE_H_"), "#ifndef _APPLE_H_".length() ); //$NON-NLS-1$ //$NON-NLS-2$
IASTPreprocessorObjectStyleMacroDefinition definition = (IASTPreprocessorObjectStyleMacroDefinition) statements[1];
assertSoleLocation( definition, code.indexOf( "#define _APPLE_H_"), "#define _APPLE_H_".length() ); //$NON-NLS-1$ //$NON-NLS-2$
IASTPreprocessorUndefStatement undef = (IASTPreprocessorUndefStatement) statements[2];
assertSoleLocation( undef, code.indexOf("#undef _APPLE_H_"), "#undef _APPLE_H_".length() ); //$NON-NLS-1$ //$NON-NLS-2$
IASTPreprocessorEndifStatement endif = (IASTPreprocessorEndifStatement) statements[3];
assertSoleLocation( endif, code.indexOf( "#endif"), "#endif".length() ); //$NON-NLS-1$ //$NON-NLS-2$
}
}
} }

View file

@ -13,6 +13,8 @@ package org.eclipse.cdt.core.dom.ast;
/** /**
* @author jcamelon * @author jcamelon
*/ */
public interface IASTPreprocessorElsifStatement extends IASTPreprocessorStatement { public interface IASTPreprocessorElifStatement extends IASTPreprocessorStatement {
public boolean taken();
} }

View file

@ -15,4 +15,5 @@ package org.eclipse.cdt.core.dom.ast;
*/ */
public interface IASTPreprocessorElseStatement extends IASTPreprocessorStatement { public interface IASTPreprocessorElseStatement extends IASTPreprocessorStatement {
public boolean taken();
} }

View file

@ -15,4 +15,5 @@ package org.eclipse.cdt.core.dom.ast;
*/ */
public interface IASTPreprocessorIfStatement extends IASTPreprocessorStatement { public interface IASTPreprocessorIfStatement extends IASTPreprocessorStatement {
public boolean taken();
} }

View file

@ -15,4 +15,5 @@ package org.eclipse.cdt.core.dom.ast;
*/ */
public interface IASTPreprocessorIfdefStatement extends IASTPreprocessorStatement { public interface IASTPreprocessorIfdefStatement extends IASTPreprocessorStatement {
public boolean taken();
} }

View file

@ -0,0 +1,20 @@
/**********************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.core.dom.ast;
/**
* @author jcamelon
*/
public interface IASTPreprocessorIfndefStatement extends
IASTPreprocessorStatement {
public boolean taken();
}

View file

@ -174,7 +174,9 @@ public class CASTTranslationUnit extends CASTNode implements IASTTranslationUnit
*/ */
public IASTPreprocessorStatement[] getAllPreprocessorStatements() { public IASTPreprocessorStatement[] getAllPreprocessorStatements() {
if( resolver == null ) return EMPTY_PREPROCESSOR_STATEMENT_ARRAY; if( resolver == null ) return EMPTY_PREPROCESSOR_STATEMENT_ARRAY;
return resolver.getAllPreprocessorStatements(); IASTPreprocessorStatement [] result = resolver.getAllPreprocessorStatements();
setParentRelationship( result, IASTTranslationUnit.PREPROCESSOR_STATEMENT );
return result;
} }

View file

@ -196,7 +196,9 @@ public class CPPASTTranslationUnit extends CPPASTNode implements
public IASTPreprocessorStatement[] getAllPreprocessorStatements() { public IASTPreprocessorStatement[] getAllPreprocessorStatements() {
if (resolver == null) if (resolver == null)
return EMPTY_PREPROCESSOR_STATEMENT_ARRAY; return EMPTY_PREPROCESSOR_STATEMENT_ARRAY;
return resolver.getAllPreprocessorStatements(); IASTPreprocessorStatement [] result = resolver.getAllPreprocessorStatements();
setParentRelationship( result, IASTTranslationUnit.PREPROCESSOR_STATEMENT );
return result;
} }
/* /*

View file

@ -2509,7 +2509,7 @@ abstract class BaseScanner implements IScanner {
if (!branchState(BRANCH_END)) if (!branchState(BRANCH_END))
handleProblem(IProblem.PREPROCESSOR_UNBALANCE_CONDITION, handleProblem(IProblem.PREPROCESSOR_UNBALANCE_CONDITION,
start, ppKeywords.findKey(buffer, start, len)); start, ppKeywords.findKey(buffer, start, len));
processEndif(pos, bufferPos[bufferStackPos]); processEndif(pos, bufferPos[bufferStackPos] + 1);
break; break;
case ppPragma: case ppPragma:
skipToNewLine(); skipToNewLine();