mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-30 12:25:35 +02:00
Fixed failing JUnit tests.
Fixed Macro Definition offsets in inclusions.
This commit is contained in:
parent
b2ff153008
commit
736de0128d
6 changed files with 164 additions and 122 deletions
|
@ -972,7 +972,9 @@ public class AST2Tests extends AST2BaseTest {
|
|||
assertEquals( decls.length, 1 );
|
||||
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[0], compType.getName() );
|
||||
|
||||
|
|
|
@ -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.IASTNodeLocation;
|
||||
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.IASTSimpleDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
|
@ -53,137 +54,164 @@ import org.eclipse.core.resources.IFile;
|
|||
*/
|
||||
public class DOMLocationInclusionTests extends FileBasePluginTest {
|
||||
|
||||
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 IScannerInfo SCANNER_INFO = new ScannerInfo();
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* @param className
|
||||
*/
|
||||
public DOMLocationInclusionTests(String name) {
|
||||
super(name, DOMLocationInclusionTests.class);
|
||||
}
|
||||
private static final IParserLogService NULL_LOG = new NullLogService();
|
||||
|
||||
protected IASTTranslationUnit parse(IFile code, ParserLanguage language)
|
||||
throws Exception {
|
||||
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();
|
||||
private static final ICodeReaderFactory factory = CDOM.getInstance()
|
||||
.getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES);
|
||||
|
||||
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) {
|
||||
IASTProblem[] problems = CVisitor.getProblems(parseResult);
|
||||
assertEquals(problems.length, 0);
|
||||
} else if (language == ParserLanguage.CPP) {
|
||||
IASTProblem[] problems = CPPVisitor.getProblems(parseResult);
|
||||
assertEquals(problems.length, 0);
|
||||
}
|
||||
protected IASTTranslationUnit parse(IFile code, ParserLanguage language)
|
||||
throws Exception {
|
||||
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();
|
||||
|
||||
return parseResult;
|
||||
}
|
||||
if (parser.encounteredError())
|
||||
throw new ParserException("FAILURE"); //$NON-NLS-1$
|
||||
|
||||
/**
|
||||
* @param language
|
||||
* @return
|
||||
*/
|
||||
private IScannerExtensionConfiguration getScannerConfig(
|
||||
ParserLanguage language) {
|
||||
if (language == ParserLanguage.CPP)
|
||||
return new GPPScannerExtensionConfiguration();
|
||||
return new GCCScannerExtensionConfiguration();
|
||||
}
|
||||
if (language == ParserLanguage.C) {
|
||||
IASTProblem[] problems = CVisitor.getProblems(parseResult);
|
||||
assertEquals(problems.length, 0);
|
||||
} else if (language == ParserLanguage.CPP) {
|
||||
IASTProblem[] problems = CPPVisitor.getProblems(parseResult);
|
||||
assertEquals(problems.length, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
return parseResult;
|
||||
}
|
||||
|
||||
public void testSimpleInclusion() throws Exception {
|
||||
String foo = "int FOO;"; //$NON-NLS-1$
|
||||
String code = "int bar;\n#include \"foo.h\"\n"; //$NON-NLS-1$
|
||||
/**
|
||||
* @param language
|
||||
* @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
|
||||
: 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$
|
||||
public void testSimpleInclusion() throws Exception {
|
||||
String foo = "int FOO;"; //$NON-NLS-1$
|
||||
String code = "int bar;\n#include \"foo.h\"\n"; //$NON-NLS-1$
|
||||
|
||||
assertSoleFileLocation(FOO,
|
||||
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
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$
|
||||
IFile cpp = importFile("code.cpp", code); //$NON-NLS-1$
|
||||
|
||||
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, 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
|
||||
: 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$
|
||||
}
|
||||
}
|
||||
|
||||
assertSoleFileLocation(FOO,
|
||||
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
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() {
|
||||
TestSuite suite = new TestSuite(DOMLocationInclusionTests.class);
|
||||
suite.addTest(new DOMLocationInclusionTests("cleanupProject")); //$NON-NLS-1$
|
||||
return suite;
|
||||
}
|
||||
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$
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -504,6 +504,11 @@ public class CVisitor {
|
|||
return ((IVariable)binding).getType();
|
||||
}
|
||||
}
|
||||
else if( expression instanceof IASTUnaryExpression )
|
||||
{
|
||||
if( ((IASTUnaryExpression)expression).getOperator() == IASTUnaryExpression.op_bracketedPrimary )
|
||||
return getExpressionType(((IASTUnaryExpression)expression).getOperand() );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
|
|
|
@ -1346,6 +1346,11 @@ public class CPPVisitor {
|
|||
return fType.getReturnType();
|
||||
}
|
||||
}
|
||||
else if( expression instanceof IASTUnaryExpression )
|
||||
{
|
||||
if( ((IASTUnaryExpression)expression).getOperator() == IASTUnaryExpression.op_bracketedPrimary )
|
||||
return getExpressionType(((IASTUnaryExpression)expression).getOperand() );
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -114,10 +114,10 @@ public class DOMScanner extends BaseScanner {
|
|||
int textEnd, int endingLine, IMacro macro) {
|
||||
if (macro instanceof FunctionStyleMacro)
|
||||
locationMap.defineFunctionStyleMacro((FunctionStyleMacro) macro,
|
||||
startingOffset, idstart, idend, textEnd);
|
||||
resolveOffset( startingOffset ), resolveOffset( idstart ), resolveOffset( idend ), resolveOffset( textEnd) );
|
||||
else if (macro instanceof ObjectStyleMacro)
|
||||
locationMap.defineObjectStyleMacro((ObjectStyleMacro) macro,
|
||||
startingOffset, idstart, idend, textEnd);
|
||||
resolveOffset( startingOffset ), resolveOffset( idstart ), resolveOffset( idend ), resolveOffset( textEnd ) );
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -712,6 +712,8 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
|
|||
_Context nextContext = (_Context) subContexts.get(i);
|
||||
if (nextContext.context_directive_end < offset)
|
||||
bestContext = nextContext;
|
||||
else if( nextContext.context_directive_start < offset && nextContext.context_ends > offset )
|
||||
bestContext = nextContext;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue