1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Test cases and fixes for the location map

This commit is contained in:
Markus Schorn 2007-10-23 12:47:10 +00:00
parent bad4dcae70
commit ac3d4db76b
8 changed files with 101 additions and 60 deletions

View file

@ -38,21 +38,30 @@ import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree.IASTInclusionNode;
import org.eclipse.cdt.core.testplugin.CTestPlugin;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.cdt.internal.core.dom.Linkage;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTranslationUnit;
import org.eclipse.cdt.internal.core.parser.scanner.ILocationCtx;
import org.eclipse.cdt.internal.core.parser.scanner.IPreprocessorMacro;
import org.eclipse.cdt.internal.core.parser.scanner.ImageLocationInfo;
import org.eclipse.cdt.internal.core.parser.scanner.LocationMap;
public class LocationMapTests extends BaseTestCase {
private static final String FN = "filename";
private static final int ROLE_DEFINITION = IASTNameOwner.r_definition;
private static final int ROLE_UNCLEAR = IASTNameOwner.r_unclear;
private static final int ROLE_REFERENCE = IASTNameOwner.r_reference;
private static final ASTNodeProperty PROP_PST = IASTTranslationUnit.PREPROCESSOR_STATEMENT;
final static char[] DIGITS= "0123456789abcdef".toCharArray();
final static char[] LONGDIGITS= new char[1024];
static {
for (int i = 0; i < LONGDIGITS.length; i++) {
LONGDIGITS[i]= (char) i;
}
}
private LocationMap fLocationMap;
private CPPASTTranslationUnit fTu;
@ -227,7 +236,7 @@ public class LocationMapTests extends BaseTestCase {
checkASTNode(st, fTu, PROP_PST, filename, offset, length, line, line, directive);
}
private void checkMacroDefinition(IASTPreprocessorMacroDefinition macro, IBinding binding, String image, String name,
private void checkMacroDefinition(IASTPreprocessorMacroDefinition macro, IMacroBinding binding, String image, String name,
String nameImage, String expansion, String[] parameters,
String filename, int offset, int length, int line, int nameOffset, int nameLength) {
assertEquals(expansion, macro.getExpansion());
@ -243,6 +252,12 @@ public class LocationMapTests extends BaseTestCase {
checkASTNode(mp, fd, IASTPreprocessorFunctionStyleMacroDefinition.PARAMETER, filename, -1, 0, -1, 0, null);
}
}
int expectCount= offset >= 0 ? 1 : 0;
IASTName[] decls= fLocationMap.getDeclarations(binding);
assertEquals(expectCount, decls.length);
if (expectCount > 0) {
assertSame(macro.getName(), decls[0]);
}
}
private void checkMacroUndef(IASTPreprocessorStatement s, IBinding binding, String image, String name, String nameImage,
@ -394,9 +409,9 @@ public class LocationMapTests extends BaseTestCase {
}
public void testIndexDefine() {
IPreprocessorMacro macro1= new TestMacro("n1", "exp1", null);
IMacroBinding macro1= new TestMacro("n1", "exp1", null);
final String[] params = new String[]{"p1", "p2"};
IPreprocessorMacro macro2= new TestMacro("n2", "exp2", params);
IMacroBinding macro2= new TestMacro("n2", "exp2", params);
init(DIGITS);
fLocationMap.registerMacroFromIndex(macro1, "fidx1", 0, 0, 0);
fLocationMap.registerMacroFromIndex(macro2, "fidx2", 1, 4, 8);
@ -407,7 +422,7 @@ public class LocationMapTests extends BaseTestCase {
}
public void testUndefine() {
IPreprocessorMacro macro1= new TestMacro("n1", "exp1", null);
IMacroBinding macro1= new TestMacro("n1", "exp1", null);
init(DIGITS);
fLocationMap.encounterPoundUndef(null, 0, 0, 0, 0, "n1".toCharArray());
@ -418,6 +433,36 @@ public class LocationMapTests extends BaseTestCase {
checkMacroUndef(prep[1], macro1, new String(DIGITS), "n2", "3456", FN, 0, 16, 1, 3, 4);
}
public void testMacroExpansion() {
ImageLocationInfo ili= new ImageLocationInfo();
IMacroBinding macro1= new TestMacro("n1", "exp1", null);
IMacroBinding macro2= new TestMacro("n2", "exp2", null);
IMacroBinding macro3= new TestMacro("n3", "exp3", null);
init(LONGDIGITS);
assertEquals(1, fLocationMap.getCurrentLineNumber('\n'));
assertEquals(2, fLocationMap.getCurrentLineNumber('\n'+1));
fLocationMap.registerPredefinedMacro(macro1);
fLocationMap.registerMacroFromIndex(macro2, "ifile", 2, 12, 32);
fLocationMap.encounterPoundDefine(3, 13, 33, 63, 103, macro3);
IASTName name1= fLocationMap.encounterImplicitMacroExpansion(macro1, ili);
IASTName name2= fLocationMap.encounterImplicitMacroExpansion(macro2, ili);
fLocationMap.pushMacroExpansion(110, 115, 125, 30, macro3, new IASTName[]{name1, name2}, new ImageLocationInfo[0]);
fLocationMap.encounteredComment(12, 23, false);
checkComment(fLocationMap.getComments()[0], new String(LONGDIGITS, 110, 15), false, FN, 110, 15, 2, 2);
IASTName[] refs= fLocationMap.getReferences(macro1);
assertEquals(1, refs.length);
checkName(refs[0], macro1, "n1", fTu, IASTTranslationUnit.EXPANSION_NAME, ROLE_REFERENCE, FN, 110, 15, 2, 2, new String(LONGDIGITS, 110, 15));
refs= fLocationMap.getReferences(macro2);
assertEquals(1, refs.length);
checkName(refs[0], macro2, "n2", fTu, IASTTranslationUnit.EXPANSION_NAME, ROLE_REFERENCE, FN, 110, 15, 2, 2, new String(LONGDIGITS, 110, 15));
refs= fLocationMap.getReferences(macro3);
assertEquals(1, refs.length);
checkName(refs[0], macro3, "n3", fTu, IASTTranslationUnit.EXPANSION_NAME, ROLE_REFERENCE, FN, 110, 5, 2, 2, new String(LONGDIGITS, 110, 5));
}
public void testContexts() {
init(DIGITS);
assertEquals(FN, fLocationMap.getTranslationUnitPath());
@ -485,5 +530,28 @@ public class LocationMapTests extends BaseTestCase {
checkLocation(fLocationMap.getMappedFileLocation(36, 0), "pre2", 0, 0, 1, 1);
checkLocation(fLocationMap.getMappedFileLocation(45, 0), "pre2", 9, 0, 1, 1);
checkLocation(fLocationMap.getMappedFileLocation(46, 0), FN, 0, 0, 1, 1);
checkLocation(fLocationMap.getMappedFileLocation(0, 7), FN, 0, 0, 1, 1);
checkLocation(fLocationMap.getMappedFileLocation(6, 10), "pre1", 0, 9, 1, 1);
checkLocation(fLocationMap.getMappedFileLocation(6, 20), "pre1", 0, 10, 1, 1);
checkLocation(fLocationMap.getMappedFileLocation(15, 11), "pre1", 6, 4, 1, 1);
checkLocation(fLocationMap.getMappedFileLocation(25, 2), FN, 0, 0, 1, 1);
checkLocation(fLocationMap.getMappedFileLocation(26, 5), FN, 0, 0, 1, 1);
checkLocation(fLocationMap.getMappedFileLocation(30, 7), FN, 0, 0, 1, 1);
checkLocation(fLocationMap.getMappedFileLocation(36, 11), FN, 0, 0, 1, 1);
checkLocation(fLocationMap.getMappedFileLocation(46, 5), FN, 0, 1, 1, 1);
IDependencyTree tree= fLocationMap.getDependencyTree();
assertEquals(FN, tree.getTranslationUnitPath());
IASTInclusionNode[] inclusions= tree.getInclusions();
assertEquals(2, inclusions.length);
checkInclude(inclusions[0].getIncludeDirective(), "", "", "pre1", "pre1", false, true, FN, 0, 0, 1, 0, 0);
checkInclude(inclusions[1].getIncludeDirective(), "", "", "pre2", "pre2", false, true, FN, 0, 0, 1, 0, 0);
assertEquals(0, inclusions[1].getNestedInclusions().length);
inclusions= inclusions[0].getNestedInclusions();
assertEquals(1, inclusions.length);
checkInclude(inclusions[0].getIncludeDirective(), "b4b", "4", "pre11", "pre11", false, true, "pre1", 6, 3, 1, 7, 1);
assertEquals(0, inclusions[0].getNestedInclusions().length);
}
}

View file

@ -12,11 +12,11 @@ package org.eclipse.cdt.core.parser.tests.scanner;
import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.internal.core.parser.scanner.IPreprocessorMacro;
import org.eclipse.core.runtime.CoreException;
final class TestMacro implements IPreprocessorMacro {
final class TestMacro implements IMacroBinding {
private String fName;
private String fExpansion;
private String[] fParams;

View file

@ -19,6 +19,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.internal.core.dom.Linkage;
/**
@ -127,16 +128,8 @@ class ASTBuiltinName extends ASTPreprocessorDefinition {
}
class ASTMacroReferenceName extends ASTPreprocessorName {
public ASTMacroReferenceName(IASTNode parent, IPreprocessorMacro macro, ImageLocationInfo imgLocationInfo) {
super(parent, IASTTranslationUnit.EXPANSION_NAME, 0, 0, macro.getNameCharArray(), macro);
}
public String getContainingFilename() {
return getTranslationUnit().getContainingFilename();
}
public String getRawSignature() {
return toString();
public ASTMacroReferenceName(IASTNode parent, int offset, int endOffset, IMacroBinding macro, ImageLocationInfo imgLocationInfo) {
super(parent, IASTTranslationUnit.EXPANSION_NAME, offset, endOffset, macro.getNameCharArray(), macro);
}
public boolean isReference() {

View file

@ -1,21 +0,0 @@
/*******************************************************************************
* Copyright (c) 2007 Wind River Systems, Inc. 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.parser.scanner;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
/**
* Interface for the location map when using the macros from the preprocessor.
* @since 5.0
*/
public interface IPreprocessorMacro extends ILocationCtx, IMacroBinding {
}

View file

@ -16,6 +16,6 @@ package org.eclipse.cdt.internal.core.parser.scanner;
*
* @since 5.0
*/
class ImageLocationInfo {
public class ImageLocationInfo {
}

View file

@ -203,6 +203,7 @@ class ContainerLocationCtx extends LocationCtx {
}
public void getInclusions(ArrayList result) {
if (fChildren != null) {
for (Iterator iterator = fChildren.iterator(); iterator.hasNext();) {
LocationCtx ctx= (LocationCtx) iterator.next();
if (ctx.getInclusionStatement() != null) {
@ -213,6 +214,7 @@ class ContainerLocationCtx extends LocationCtx {
}
}
}
}
public int getLineNumber(int offset) {

View file

@ -59,7 +59,7 @@ public class LocationMap implements ILocationResolver {
registerPredefinedMacro(macro, null, -1, -1, -1);
}
public void registerMacroFromIndex(IPreprocessorMacro macro, String filename, int nameOffset, int nameEndOffset, int expansionOffset) {
public void registerMacroFromIndex(IMacroBinding macro, String filename, int nameOffset, int nameEndOffset, int expansionOffset) {
registerPredefinedMacro(macro, filename, nameOffset, nameEndOffset, expansionOffset);
}
@ -131,13 +131,12 @@ public class LocationMap implements ILocationResolver {
* @param macro the macro that has been expanded
* @param imageLocationInfo the image-location for the name of the macro.
*/
public IASTName encounterImplicitMacroExpansion(IPreprocessorMacro macro, ImageLocationInfo imageLocationInfo) {
return new ASTMacroReferenceName(fTranslationUnit, macro, imageLocationInfo);
public IASTName encounterImplicitMacroExpansion(IMacroBinding macro, ImageLocationInfo imageLocationInfo) {
return new ASTMacroReferenceName(fTranslationUnit, 0, 0, macro, imageLocationInfo);
}
/**
* Creates a new context for the result of a (recursive) macro-expansion.
* @param startOffset offset within the current context where macro-expansion starts.
* @param nameOffset offset within the current context where the name for the macro-expansion starts.
* @param nameEndOffset offset within the current context where the name for the macro-expansion ends.
* @param endOffset offset within the current context where the entire macro-expansion ends.
@ -145,31 +144,30 @@ public class LocationMap implements ILocationResolver {
* @param implicitMacroReferences an array of implicit macro-expansions.
* @param imageLocations an array of image-locations for the new context.
*/
public ILocationCtx pushMacroExpansion(int startOffset, int nameOffset, int nameEndOffset, int endOffset, int contextLength,
IPreprocessorMacro macro, IASTName[] implicitMacroReferences, ImageLocationInfo[] imageLocations) {
public ILocationCtx pushMacroExpansion(int nameOffset, int nameEndOffset, int endOffset, int contextLength,
IMacroBinding macro, IASTName[] implicitMacroReferences, ImageLocationInfo[] imageLocations) {
assert fCurrentContext instanceof ContainerLocationCtx;
int startNumber= getSequenceNumberForOffset(startOffset);
int nameNumber= getSequenceNumberForOffset(nameOffset);
int nameEndNumber= getSequenceNumberForOffset(nameEndOffset);
int endNumber= getSequenceNumberForOffset(endOffset);
final int length= endNumber-nameNumber;
for (int i = 0; i < implicitMacroReferences.length; i++) {
ASTMacroReferenceName name = (ASTMacroReferenceName) implicitMacroReferences[i];
name.setOffsetAndLength(startNumber, endNumber);
addExpansion((IPreprocessorMacro) name.getBinding(), name);
name.setOffsetAndLength(nameNumber, length);
addExpansion((IMacroBinding) name.getBinding(), name);
}
ASTPreprocessorName expansion= new ASTPreprocessorName(fTranslationUnit, IASTTranslationUnit.EXPANSION_NAME,
nameNumber, nameEndNumber, macro.getNameCharArray(), macro);
ASTMacroReferenceName expansion= new ASTMacroReferenceName(fTranslationUnit, nameNumber, nameEndNumber, macro, null);
addExpansion(macro, expansion);
fCurrentContext= new MacroExpansionCtx((ContainerLocationCtx) fCurrentContext, startOffset, endOffset, endNumber, contextLength, imageLocations, expansion);
fCurrentContext= new MacroExpansionCtx((ContainerLocationCtx) fCurrentContext, nameOffset, endOffset, endNumber, contextLength, imageLocations, expansion);
fLastChildInsertionOffset= 0;
return fCurrentContext;
}
private void addExpansion(IPreprocessorMacro macro, ASTPreprocessorName name) {
private void addExpansion(IMacroBinding macro, ASTPreprocessorName name) {
List list= (List) fMacroExpansions.get(macro);
if (list == null) {
list= new ArrayList();

View file

@ -11,6 +11,7 @@
package org.eclipse.cdt.internal.core.parser.scanner;
import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
@ -20,7 +21,7 @@ import org.eclipse.cdt.internal.core.dom.Linkage;
* Models macros used by the preprocessor
* @since 5.0
*/
abstract class PreprocessorMacro implements IPreprocessorMacro {
abstract class PreprocessorMacro implements IMacroBinding {
final private char[] fName;
public PreprocessorMacro(char[] name) {