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;
@ -54,11 +55,11 @@ 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 IParserLogService NULL_LOG = new NullLogService();
private static final ICodeReaderFactory factory = CDOM
.getInstance() private static final ICodeReaderFactory factory = CDOM.getInstance()
.getCodeReaderFactory( .getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES);
CDOM.PARSE_SAVED_RESOURCES);
/** /**
* @param name * @param name
@ -72,8 +73,9 @@ public class DOMLocationInclusionTests extends FileBasePluginTest {
throws Exception { throws Exception {
InputStream stream = code.getContents(); InputStream stream = code.getContents();
IScanner scanner = new DOMScanner(new CodeReader(code.getLocation() IScanner scanner = new DOMScanner(new CodeReader(code.getLocation()
.toOSString(), stream), SCANNER_INFO, ParserMode.COMPLETE_PARSE, .toOSString(), stream), SCANNER_INFO,
language, NULL_LOG, getScannerConfig(language), factory); ParserMode.COMPLETE_PARSE, language, NULL_LOG,
getScannerConfig(language), factory);
ISourceCodeParser parser = null; ISourceCodeParser parser = null;
if (language == ParserLanguage.CPP) { if (language == ParserLanguage.CPP) {
parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE, parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE,
@ -146,10 +148,12 @@ public class DOMLocationInclusionTests extends FileBasePluginTest {
assertSoleFileLocation(FOO, assertSoleFileLocation(FOO,
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ "foo.h", foo.indexOf("int"), foo.indexOf(";") + 1); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
IASTPreprocessorIncludeStatement [] incs = tu.getIncludeDirectives(); IASTPreprocessorIncludeStatement[] incs = tu.getIncludeDirectives();
assertNotNull( incs ); assertNotNull(incs);
assertEquals( incs.length, 1 ); 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(
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$
} }
} }
@ -168,18 +172,42 @@ public class DOMLocationInclusionTests extends FileBasePluginTest {
IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0]; IASTSimpleDeclaration bar = (IASTSimpleDeclaration) declarations[0];
IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1]; IASTSimpleDeclaration FOO = (IASTSimpleDeclaration) declarations[1];
IASTSimpleDeclaration byob = (IASTSimpleDeclaration) declarations[2]; IASTSimpleDeclaration byob = (IASTSimpleDeclaration) declarations[2];
assertSoleFileLocation(bar, assertSoleFileLocation(
"code.cpp", code.indexOf("int"), code.indexOf("r;") + 2 - code.indexOf( "int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ bar,
assertSoleFileLocation(FOO, "code.cpp", code.indexOf("int"), code.indexOf("r;") + 2 - code.indexOf("int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"foo.h", foo.indexOf("int"), foo.indexOf(";") + 1 - foo.indexOf( "int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ assertSoleFileLocation(
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$ FOO,
IASTPreprocessorIncludeStatement [] incs = tu.getIncludeDirectives(); "foo.h", foo.indexOf("int"), foo.indexOf(";") + 1 - foo.indexOf("int")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
assertNotNull( incs ); assertSoleFileLocation(
assertEquals( incs.length, 1 ); byob,
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$ "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() { public static Test suite() {
TestSuite suite = new TestSuite(DOMLocationInclusionTests.class); TestSuite suite = new TestSuite(DOMLocationInclusionTests.class);

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;
} }