1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-01 05:15:43 +02:00

Fixed failing JUnit tests.

Fixed Macro Definition offsets in inclusions.
This commit is contained in:
John Camelon 2005-01-27 20:31:21 +00:00
parent b2ff153008
commit 736de0128d
6 changed files with 164 additions and 122 deletions

View file

@ -972,7 +972,9 @@ public class AST2Tests extends AST2BaseTest {
assertEquals( decls.length, 1 ); assertEquals( decls.length, 1 );
assertEquals( decls[0], fdef.getDeclarator().getName() ); assertEquals( decls[0], fdef.getDeclarator().getName() );
decls = tu.getDeclarations( ((IASTElaboratedTypeSpecifier)((IASTCastExpression)((IASTFieldReference)expStatement.getExpression()).getFieldOwner()).getTypeId().getDeclSpecifier()).getName().resolveBinding() ); IASTCastExpression castExpression = (IASTCastExpression) ((IASTUnaryExpression)((IASTFieldReference)expStatement.getExpression()).getFieldOwner()).getOperand();
IASTElaboratedTypeSpecifier elaboratedTypeSpecifier = ((IASTElaboratedTypeSpecifier)castExpression.getTypeId().getDeclSpecifier());
decls = tu.getDeclarations( elaboratedTypeSpecifier.getName().resolveBinding() );
assertEquals( decls.length, 1 ); assertEquals( decls.length, 1 );
assertEquals( decls[0], compType.getName() ); assertEquals( decls[0], compType.getName() );

View file

@ -22,6 +22,7 @@ import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode; 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.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTProblem; import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
@ -53,137 +54,164 @@ import org.eclipse.core.resources.IFile;
*/ */
public class DOMLocationInclusionTests extends FileBasePluginTest { public class DOMLocationInclusionTests extends FileBasePluginTest {
private static final IScannerInfo SCANNER_INFO = new ScannerInfo(); private static final IScannerInfo SCANNER_INFO = new ScannerInfo();
private static final IParserLogService NULL_LOG = new NullLogService();
private static final ICodeReaderFactory factory = CDOM
.getInstance()
.getCodeReaderFactory(
CDOM.PARSE_SAVED_RESOURCES);
/** private static final IParserLogService NULL_LOG = new NullLogService();
* @param name
* @param className
*/
public DOMLocationInclusionTests(String name) {
super(name, DOMLocationInclusionTests.class);
}
protected IASTTranslationUnit parse(IFile code, ParserLanguage language) private static final ICodeReaderFactory factory = CDOM.getInstance()
throws Exception { .getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES);
InputStream stream = code.getContents();
IScanner scanner = new DOMScanner(new CodeReader(code.getLocation()
.toOSString(), stream), SCANNER_INFO, ParserMode.COMPLETE_PARSE,
language, NULL_LOG, getScannerConfig(language), factory);
ISourceCodeParser parser = null;
if (language == ParserLanguage.CPP) {
parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, new GPPParserExtensionConfiguration());
} else {
parser = new GNUCSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, new GCCParserExtensionConfiguration());
}
stream.close();
IASTTranslationUnit parseResult = parser.parse();
if (parser.encounteredError()) /**
throw new ParserException("FAILURE"); //$NON-NLS-1$ * @param name
* @param className
*/
public DOMLocationInclusionTests(String name) {
super(name, DOMLocationInclusionTests.class);
}
if (language == ParserLanguage.C) { protected IASTTranslationUnit parse(IFile code, ParserLanguage language)
IASTProblem[] problems = CVisitor.getProblems(parseResult); throws Exception {
assertEquals(problems.length, 0); InputStream stream = code.getContents();
} else if (language == ParserLanguage.CPP) { IScanner scanner = new DOMScanner(new CodeReader(code.getLocation()
IASTProblem[] problems = CPPVisitor.getProblems(parseResult); .toOSString(), stream), SCANNER_INFO,
assertEquals(problems.length, 0); ParserMode.COMPLETE_PARSE, language, NULL_LOG,
} getScannerConfig(language), factory);
ISourceCodeParser parser = null;
if (language == ParserLanguage.CPP) {
parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, new GPPParserExtensionConfiguration());
} else {
parser = new GNUCSourceParser(scanner, ParserMode.COMPLETE_PARSE,
NULL_LOG, new GCCParserExtensionConfiguration());
}
stream.close();
IASTTranslationUnit parseResult = parser.parse();
return parseResult; if (parser.encounteredError())
} throw new ParserException("FAILURE"); //$NON-NLS-1$
/** if (language == ParserLanguage.C) {
* @param language IASTProblem[] problems = CVisitor.getProblems(parseResult);
* @return assertEquals(problems.length, 0);
*/ } else if (language == ParserLanguage.CPP) {
private IScannerExtensionConfiguration getScannerConfig( IASTProblem[] problems = CPPVisitor.getProblems(parseResult);
ParserLanguage language) { assertEquals(problems.length, 0);
if (language == ParserLanguage.CPP) }
return new GPPScannerExtensionConfiguration();
return new GCCScannerExtensionConfiguration();
}
/** return parseResult;
* @param pathEndsWith }
* TODO
* @param offset
* @param length
* @param declarator
*/
private void assertSoleFileLocation(IASTNode n, String pathEndsWith,
int offset, int length) {
IASTNodeLocation[] locations = n.getNodeLocations();
assertEquals(locations.length, 1);
IASTFileLocation nodeLocation = (IASTFileLocation) locations[0];
assertTrue(nodeLocation.getFileName().endsWith(pathEndsWith));
assertEquals(nodeLocation.getNodeOffset(), offset);
assertEquals(nodeLocation.getNodeLength(), length);
}
public void testSimpleInclusion() throws Exception { /**
String foo = "int FOO;"; //$NON-NLS-1$ * @param language
String code = "int bar;\n#include \"foo.h\"\n"; //$NON-NLS-1$ * @return
*/
private IScannerExtensionConfiguration getScannerConfig(
ParserLanguage language) {
if (language == ParserLanguage.CPP)
return new GPPScannerExtensionConfiguration();
return new GCCScannerExtensionConfiguration();
}
importFile("foo.h", foo); //$NON-NLS-1$ /**
IFile cpp = importFile("code.cpp", code); //$NON-NLS-1$ * @param pathEndsWith
* TODO
* @param offset
* @param length
* @param declarator
*/
private void assertSoleFileLocation(IASTNode n, String pathEndsWith,
int offset, int length) {
IASTNodeLocation[] locations = n.getNodeLocations();
assertEquals(locations.length, 1);
IASTFileLocation nodeLocation = (IASTFileLocation) locations[0];
assertTrue(nodeLocation.getFileName().endsWith(pathEndsWith));
assertEquals(nodeLocation.getNodeOffset(), offset);
assertEquals(nodeLocation.getNodeLength(), length);
}
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP public void testSimpleInclusion() throws Exception {
: null) { String foo = "int FOO;"; //$NON-NLS-1$
IASTTranslationUnit tu = parse(cpp, p); //$NON-NLS-1$ String code = "int bar;\n#include \"foo.h\"\n"; //$NON-NLS-1$
IASTDeclaration[] declarations = tu.getDeclarations();
assertEquals(declarations.length, 2);
IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0];
IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1];
assertSoleFileLocation(bar,
"code.cpp", code.indexOf("int"), code.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation(FOO, importFile("foo.h", foo); //$NON-NLS-1$
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ IFile cpp = importFile("code.cpp", code); //$NON-NLS-1$
IASTPreprocessorIncludeStatement [] incs = tu.getIncludeDirectives();
assertNotNull( incs );
assertEquals( incs.length, 1 );
assertSoleFileLocation( incs[0], "code.cpp", code.indexOf( "#inc"), code.indexOf( ".h\"\n") + ".h\"".length() - code.indexOf( "#inc") ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
}
}
public void testSimpleInclusion2() throws Exception {
String foo = "int FOO;"; //$NON-NLS-1$
String code = "int bar;\n#include \"foo.h\"\nfloat byob;\n"; //$NON-NLS-1$
importFile("foo.h", foo); //$NON-NLS-1$ for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
IFile cpp = importFile("code.cpp", code); //$NON-NLS-1$ : null) {
IASTTranslationUnit tu = parse(cpp, p); //$NON-NLS-1$
IASTDeclaration[] declarations = tu.getDeclarations();
assertEquals(declarations.length, 2);
IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0];
IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1];
assertSoleFileLocation(bar,
"code.cpp", code.indexOf("int"), code.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP assertSoleFileLocation(FOO,
: null) { "foo.h", foo.indexOf("int"), foo.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
IASTTranslationUnit tu = parse(cpp, p); //$NON-NLS-1$ IASTPreprocessorIncludeStatement[] incs = tu.getIncludeDirectives();
IASTDeclaration[] declarations = tu.getDeclarations(); assertNotNull(incs);
assertEquals(declarations.length, 3); assertEquals(incs.length, 1);
IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0]; assertSoleFileLocation(
IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1]; incs[0],
IASTSimpleDeclaration byob = (IASTSimpleDeclaration) declarations[2]; "code.cpp", code.indexOf("#inc"), code.indexOf(".h\"\n") + ".h\"".length() - code.indexOf("#inc")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
assertSoleFileLocation(bar, }
"code.cpp", code.indexOf("int"), code.indexOf("r;") + 2 - code.indexOf( "int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ }
assertSoleFileLocation(FOO,
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1 - foo.indexOf( "int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation( byob, "code.cpp", code.indexOf( "float"), code.indexOf( "b;") + 2 - code.indexOf( "float") ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
IASTPreprocessorIncludeStatement [] incs = tu.getIncludeDirectives();
assertNotNull( incs );
assertEquals( incs.length, 1 );
assertSoleFileLocation( incs[0], "code.cpp", code.indexOf( "#inc"), code.indexOf( ".h\"\n") + ".h\"".length() - code.indexOf( "#inc") ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
}
}
public static Test suite() { public void testSimpleInclusion2() throws Exception {
TestSuite suite = new TestSuite(DOMLocationInclusionTests.class); String foo = "int FOO;"; //$NON-NLS-1$
suite.addTest(new DOMLocationInclusionTests("cleanupProject")); //$NON-NLS-1$ String code = "int bar;\n#include \"foo.h\"\nfloat byob;\n"; //$NON-NLS-1$
return suite;
} importFile("foo.h", foo); //$NON-NLS-1$
IFile cpp = importFile("code.cpp", code); //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(cpp, p); //$NON-NLS-1$
IASTDeclaration[] declarations = tu.getDeclarations();
assertEquals(declarations.length, 3);
IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0];
IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1];
IASTSimpleDeclaration byob = (IASTSimpleDeclaration) declarations[2];
assertSoleFileLocation(
bar,
"code.cpp", code.indexOf("int"), code.indexOf("r;") + 2 - code.indexOf("int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
FOO,
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1 - foo.indexOf("int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation(
byob,
"code.cpp", code.indexOf("float"), code.indexOf("b;") + 2 - code.indexOf("float")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
IASTPreprocessorIncludeStatement[] incs = tu.getIncludeDirectives();
assertNotNull(incs);
assertEquals(incs.length, 1);
assertSoleFileLocation(
incs[0],
"code.cpp", code.indexOf("#inc"), code.indexOf(".h\"\n") + ".h\"".length() - code.indexOf("#inc")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
}
}
public void testMacrosInIncludeFile() throws Exception {
String c_file_code = "#define X 4\n\n#include \"blarg.h\"\n\n"; //$NON-NLS-1$
String h_file_code = "#ifndef _BLARG_H_\r\n#define _BLARG_H_\r\n// macro\r\n#define PRINT(s,m) printf(s,m)\r\n#endif //_BLARG_H_\r\n"; //$NON-NLS-1$
importFile( "blarg.h", h_file_code ); //$NON-NLS-1$
IFile c_file = importFile( "blarg.c", c_file_code ); //$NON-NLS-1$
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
: null) {
IASTTranslationUnit tu = parse(c_file, p); //$NON-NLS-1$
assertEquals( tu.getDeclarations().length, 0 );
IASTPreprocessorMacroDefinition[] macroDefinitions = tu.getMacroDefinitions();
assertNotNull( macroDefinitions );
assertEquals( macroDefinitions.length, 3 );
assertSoleFileLocation( macroDefinitions[0], "blarg.c", c_file_code.indexOf( "#define"), c_file_code.indexOf( "4" ) + 1- c_file_code.indexOf( "#define" ) ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertSoleFileLocation( macroDefinitions[1], "blarg.h", h_file_code.indexOf( "#define _BLARG_H_"), "#define _BLARG_H_\r".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertSoleFileLocation( macroDefinitions[2], "blarg.h", h_file_code.indexOf( "#define PRINT(s,m) printf(s,m)\r"), "#define PRINT(s,m) printf(s,m)".length() ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
}
public static Test suite() {
TestSuite suite = new TestSuite(DOMLocationInclusionTests.class);
suite.addTest(new DOMLocationInclusionTests("cleanupProject")); //$NON-NLS-1$
return suite;
}
} }

View file

@ -504,6 +504,11 @@ public class CVisitor {
return ((IVariable)binding).getType(); return ((IVariable)binding).getType();
} }
} }
else if( expression instanceof IASTUnaryExpression )
{
if( ((IASTUnaryExpression)expression).getOperator() == IASTUnaryExpression.op_bracketedPrimary )
return getExpressionType(((IASTUnaryExpression)expression).getOperand() );
}
return null; return null;
} }
/** /**

View file

@ -1346,6 +1346,11 @@ public class CPPVisitor {
return fType.getReturnType(); return fType.getReturnType();
} }
} }
else if( expression instanceof IASTUnaryExpression )
{
if( ((IASTUnaryExpression)expression).getOperator() == IASTUnaryExpression.op_bracketedPrimary )
return getExpressionType(((IASTUnaryExpression)expression).getOperand() );
}
return null; return null;
} }

View file

@ -114,10 +114,10 @@ public class DOMScanner extends BaseScanner {
int textEnd, int endingLine, IMacro macro) { int textEnd, int endingLine, IMacro macro) {
if (macro instanceof FunctionStyleMacro) if (macro instanceof FunctionStyleMacro)
locationMap.defineFunctionStyleMacro((FunctionStyleMacro) macro, locationMap.defineFunctionStyleMacro((FunctionStyleMacro) macro,
startingOffset, idstart, idend, textEnd); resolveOffset( startingOffset ), resolveOffset( idstart ), resolveOffset( idend ), resolveOffset( textEnd) );
else if (macro instanceof ObjectStyleMacro) else if (macro instanceof ObjectStyleMacro)
locationMap.defineObjectStyleMacro((ObjectStyleMacro) macro, locationMap.defineObjectStyleMacro((ObjectStyleMacro) macro,
startingOffset, idstart, idend, textEnd); resolveOffset( startingOffset ), resolveOffset( idstart ), resolveOffset( idend ), resolveOffset( textEnd ) );
} }

View file

@ -712,6 +712,8 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
_Context nextContext = (_Context) subContexts.get(i); _Context nextContext = (_Context) subContexts.get(i);
if (nextContext.context_directive_end < offset) if (nextContext.context_directive_end < offset)
bestContext = nextContext; bestContext = nextContext;
else if( nextContext.context_directive_start < offset && nextContext.context_ends > offset )
bestContext = nextContext;
else else
break; break;
} }