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 a10e2c54dcf..b879a6a5936 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2006 IBM Corporation and others. + * Copyright (c) 2004, 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -8,12 +8,15 @@ * Contributors: * IBM - Initial API and implementation * Markus Schorn (Wind River Systems) + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; +import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression; import org.eclipse.cdt.core.dom.ast.IASTMacroExpansion; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; @@ -344,4 +347,29 @@ public class DOMLocationMacroTests extends AST2BaseTest { assertNotNull( f.getFileLocation() ); } } + + public void testFunctionMacroExpansionWithNameSubstitution_Bug173637() throws Exception + { + StringBuffer buffer = new StringBuffer( "#define PLUS5(x) (x+5)\n"); //$NON-NLS-1$ + buffer.append( "#define FUNCTION PLUS5 \n"); //$NON-NLS-1$ + buffer.append( "int var= FUNCTION(1);"); //$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); + IASTSimpleDeclaration var = (IASTSimpleDeclaration) tu.getDeclarations()[0]; + IASTInitializerExpression initializer= (IASTInitializerExpression)var.getDeclarators()[0].getInitializer(); + IASTExpression expr= initializer.getExpression(); + assertNotNull(expr.getFileLocation()); + IASTNodeLocation [] locations = expr.getNodeLocations(); + assertEquals(1, locations.length); + IASTMacroExpansion macroExpansion = (IASTMacroExpansion) locations[0]; + IASTNodeLocation[] expLocations= macroExpansion.getExpansionLocations(); + assertEquals(1, expLocations.length); + assertEquals(code.indexOf("FUNCTION(1)"), expLocations[0].getNodeOffset()); + assertEquals("FUNCTION(1)".length(), expLocations[0].getNodeLength()); + } + } + } 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 32e21c68ca6..af92f567b0e 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 @@ -4026,9 +4026,13 @@ abstract class BaseScanner implements IScanner { return null; } - MacroData data = (MacroData) bufferData[stackpPos + 1]; - for (int i = bufferStackPos; i > stackpPos; i--) - popContext(); + MacroData data; + IMacro popMacro= macro; + do { + data= (MacroData) bufferData[bufferStackPos]; + popContextForFunctionMacroName(popMacro); + popMacro= data.macro; + } while (bufferStackPos > stackpPos); bufferPos[bufferStackPos] = idx; buffer = bufferStack[bufferStackPos]; @@ -4158,7 +4162,18 @@ abstract class BaseScanner implements IScanner { return result; } - protected char[] replaceArgumentMacros(char[] arg) { + /** + * Called when the buffer limit is reached while expanding a function style macro. + * This special case might be handled differently by subclasses. + * + * @param macro + */ + protected void popContextForFunctionMacroName(IMacro macro) { + // do the default + popContext(); + } + + protected char[] replaceArgumentMacros(char[] arg) { int limit = arg.length; int start = -1, end = -1; Object expObject = 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 79e57396af5..ddbe854303b 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2006 IBM Corporation and others. + * Copyright (c) 2004, 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -8,6 +8,7 @@ * Contributors: * IBM - Initial API and implementation * Markus Schorn (Wind River Systems) + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.parser.scanner2; @@ -250,6 +251,15 @@ public class DOMScanner extends BaseScanner { } } + /* + * @see org.eclipse.cdt.internal.core.parser.scanner2.BaseScanner#popContextForFunctionMacroName(org.eclipse.cdt.core.parser.IMacro) + */ + protected void popContextForFunctionMacroName(IMacro macro) { + // fix the delta before popping + bufferDelta[bufferStackPos] -= macro.getName().length; + popContext(); + } + protected int fsmCount = 0; /*