From 736de0128dd403e1d3a2ae30753e758ba2e78cfa Mon Sep 17 00:00:00 2001 From: John Camelon Date: Thu, 27 Jan 2005 20:31:21 +0000 Subject: [PATCH] Fixed failing JUnit tests. Fixed Macro Definition offsets in inclusions. --- .../cdt/core/parser/tests/ast2/AST2Tests.java | 4 +- .../tests/ast2/DOMLocationInclusionTests.java | 266 ++++++++++-------- .../internal/core/dom/parser/c/CVisitor.java | 5 + .../core/dom/parser/cpp/CPPVisitor.java | 5 + .../core/parser/scanner2/DOMScanner.java | 4 +- .../core/parser/scanner2/LocationMap.java | 2 + 6 files changed, 164 insertions(+), 122 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index 2fe973f5a2f..467fa5dc4f8 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -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() ); diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java index 86ebaba9d54..ee7c1086df6 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationInclusionTests.java @@ -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; + } } \ No newline at end of file diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java index 6cb744b855d..d53c77ddbee 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java @@ -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; } /** diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java index c3c6858a94f..f7318c1dfd8 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java @@ -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; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java index 91fc107789a..dc7beb32076 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/DOMScanner.java @@ -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 ) ); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java index e53a2285cff..61de6b97e07 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/LocationMap.java @@ -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; }