diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java index 0f01e58778d..1154cddc801 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java @@ -248,10 +248,11 @@ public class DOMLocationMacroTests extends AST2BaseTest { assertEquals( flat.getNodeOffset() , code.indexOf( "_PTR _EXFUN(memchr,(const _PTR, int, size_t));")); //$NON-NLS-1$ assertEquals( flat.getNodeLength(), "_PTR _EXFUN(memchr,(const _PTR, int, size_t));".length() ); //$NON-NLS-1$ - IASTNodeLocation [] fullyMonty = tu.getNodeLocations(); - IASTFileLocation flatMonty = tu.flattenLocationsToFile( fullyMonty ); - assertEquals( flatMonty.getNodeOffset(), 0 ); -// assertEquals( flatMonty.getNodeLength(), code.length() ); + IASTDeclarator d = memchr.getDeclarators()[0]; + final IASTNodeLocation[] declaratorLocations = d.getNodeLocations(); + IASTFileLocation f = tu.flattenLocationsToFile( declaratorLocations ); + assertEquals( code.indexOf( "_PTR _EXFUN(memchr,(const _PTR, int, size_t))"), f.getNodeOffset() ); //$NON-NLS-1$ + assertEquals( "_PTR _EXFUN(memchr,(const _PTR, int, size_t))".length(), f.getNodeLength() ); //$NON-NLS-1$ } } @@ -299,4 +300,26 @@ public class DOMLocationMacroTests extends AST2BaseTest { } } + + public void testBug90978() throws Exception { + StringBuffer buffer = new StringBuffer( "#define MACRO mm\n"); //$NON-NLS-1$ + buffer.append( "int MACRO;\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); + IASTPreprocessorObjectStyleMacroDefinition MACRO = (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[0]; + IASTName macro_name = MACRO.getName(); + IMacroBinding binding = (IMacroBinding) macro_name.resolveBinding(); + IASTName [] references = tu.getReferences( binding ); + assertEquals( references.length, 1 ); + IASTName reference = references[0]; + IASTNodeLocation [] nodeLocations = reference.getNodeLocations(); + assertEquals( nodeLocations.length, 1 ); + assertTrue( nodeLocations[0] instanceof IASTFileLocation ); + IASTFileLocation loc = (IASTFileLocation) nodeLocations[0]; + assertEquals( code.indexOf( "int MACRO") + "int ".length(), loc.getNodeOffset() ); //$NON-NLS-1$ //$NON-NLS-2$ + assertEquals( "MACRO".length(), loc.getNodeLength() ); //$NON-NLS-1$ + } + } } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java index d3cb06d21e8..988ebcb4779 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java @@ -30,7 +30,6 @@ public class DOMParserTestSuite extends TestCase { suite.addTestSuite( AST2TemplateTests.class ); suite.addTestSuite( QuickParser2Tests.class ); suite.addTestSuite( CompleteParser2Tests.class ); -// suite.addTestSuite( DOMScannerTests.class ); suite.addTestSuite( DOMLocationTests.class ); suite.addTestSuite( DOMLocationMacroTests.class ); suite.addTest( DOMLocationInclusionTests.suite() ); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java index c9cdda44007..ec9f627be9d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java @@ -2064,8 +2064,8 @@ abstract class BaseScanner implements IScanner { char[] expText = expMacro.expansion; if (expText.length > 0) pushContext(expText, new MacroData( - bufferPos[bufferStackPos] - expMacro.name.length - + 1, bufferPos[bufferStackPos], expMacro)); + bufferPos[bufferStackPos] - expMacro.name.length + 1, + bufferPos[bufferStackPos], expMacro)); } else if (expObject instanceof DynamicStyleMacro) { DynamicStyleMacro expMacro = (DynamicStyleMacro) expObject; char[] expText = expMacro.execute(); 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 7eb167b8017..b1c3f7b96e9 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 @@ -979,8 +979,10 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { } public boolean containsInDirective(int offset, int length) { + if( length > 0 && offset == context_directive_end ) + return false; if (offset >= context_directive_start - && offset + length - 1<= context_directive_end) + && offset + length - 1 <= context_directive_end ) return true; return false; } @@ -1222,7 +1224,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { public IASTPreprocessorMacroDefinition astNode; private IMacroBinding bind; - + public char[] getName() { return name; } @@ -1316,7 +1318,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { expansionName = new ASTMacroName( definition.getName() ); ((ScannerASTNode)expansionName).setParent( rootNode ); ((ScannerASTNode)expansionName).setPropertyInParent( IASTTranslationUnit.EXPANSION_NAME ); - ((ScannerASTNode)expansionName).setOffsetAndLength( context_directive_start, context_directive_end - context_directive_start); + ((ScannerASTNode)expansionName).setOffsetAndLength( context_directive_start, context_directive_end - context_directive_start + 1); } return expansionName; } @@ -1732,6 +1734,18 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog { } if (c instanceof _MacroExpansion) { _MacroExpansion expansion = (_MacroExpansion) c; + //first check to see if we are in the directive rather than the expansion + if( c.containsInDirective( offset, length ) ) + { + _CompositeContext parent = c.parent; + while (!(parent instanceof _CompositeFileContext)) + parent = c.parent; + _CompositeFileContext fc = (_CompositeFileContext) parent; + return new FileLocation(fc.reader.filename, reconcileOffset(fc, + c, offset), length); + } + + IASTNodeLocation[] locations = createSoleLocationArray(c.parent, c.context_directive_start, c.context_directive_end - c.context_directive_start + 1);