1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-19 06:55:23 +02:00

JUnit-tests and fixes for the image-locations.

This commit is contained in:
Markus Schorn 2007-11-15 16:17:30 +00:00
parent 74a6d68f29
commit 4f852d99c1
5 changed files with 135 additions and 1 deletions

View file

@ -35,6 +35,7 @@ public class DOMParserTestSuite extends TestCase {
suite.addTest( CompleteParser2Tests.suite() );
suite.addTest( DOMLocationTests.suite() );
suite.addTestSuite( DOMLocationMacroTests.class );
suite.addTest (ImageLocationTests.suite());
suite.addTest( DOMLocationInclusionTests.suite() );
suite.addTestSuite( AST2KnRTests.class );
suite.addTestSuite( AST2UtilTests.class );

View file

@ -0,0 +1,126 @@
/*******************************************************************************
* 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 (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;
import junit.framework.Test;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTImageLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.parser.ParserLanguage;
/**
* @author jcamelon
*/
public class ImageLocationTests extends AST2BaseTest {
private static final int CODE = IASTImageLocation.REGULAR_CODE;
private static final int MACRO = IASTImageLocation.MACRO_DEFINITION;
private static final int MACRO_ARG = IASTImageLocation.ARGUMENT_TO_MACRO_EXPANSION;
public static Test suite() {
return suite(ImageLocationTests.class);
}
public ImageLocationTests() {
}
public ImageLocationTests(String name) {
setName(name);
}
// int a;
public void testFileLocation() throws Exception {
String code= getContents(1)[0].toString();
IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTDeclaration declaration = tu.getDeclarations()[0];
IASTName name= getName(declaration);
IASTImageLocation loc= name.getImageLocation();
assertLocation(CODE, code, "a", 0, loc);
}
// #define M result1
// #define F() result2
// int M;
// int F();
public void testMacroLocation() throws Exception {
String code= getContents(1)[0].toString();
IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTDeclaration declaration = tu.getDeclarations()[0];
IASTName name= getName(declaration);
IASTImageLocation loc= name.getImageLocation();
assertLocation(MACRO, code, "result1", 0, loc);
declaration = tu.getDeclarations()[1];
name= getName(declaration);
loc= name.getImageLocation();
assertLocation(MACRO, code, "result2", 0, loc);
}
// #define M result
// #define F() M
// int F();
public void testIndirectMacroLocation() throws Exception {
String code= getContents(1)[0].toString();
IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTDeclaration declaration = tu.getDeclarations()[0];
IASTName name= getName(declaration);
IASTImageLocation loc= name.getImageLocation();
assertLocation(MACRO, code, "result", 0, loc);
}
// #define M result1
// #define F(x) x
// int F(result2);
// int F(M);
public void testMacroArgumentLocation() throws Exception {
String code= getContents(1)[0].toString();
IASTTranslationUnit tu = parse(code, ParserLanguage.CPP);
IASTDeclaration declaration = tu.getDeclarations()[0];
IASTName name= getName(declaration);
IASTImageLocation loc= name.getImageLocation();
assertLocation(MACRO_ARG, code, "result2", 0, loc);
declaration = tu.getDeclarations()[1];
name= getName(declaration);
loc= name.getImageLocation();
assertLocation(MACRO, code, "result1", 0, loc);
}
private void assertLocation(int kind, String code, String name, int extra, IASTImageLocation loc) {
assertNotNull(loc);
assertEquals(kind, loc.getLocationKind());
assertEquals(code.indexOf(name), loc.getNodeOffset());
assertEquals(name.length()-extra, loc.getNodeLength());
}
private IASTName getName(IASTNode node) {
final IASTName[] result= {null};
node.accept(new ASTVisitor() {
{
shouldVisitNames= true;
}
public int visit(IASTName name) {
result[0]= name;
return PROCESS_ABORT;
}
});
return result[0];
}
}

View file

@ -245,7 +245,7 @@ public class MacroExpander {
private ImageLocationInfo createImageLocationInfo(Token t) {
final Object s= t.fSource;
if (s instanceof ObjectStyleMacro) {
return new MacroImageLocationInfo((ObjectStyleMacro) s, fEndOffset, fEndOffset);
return new MacroImageLocationInfo((ObjectStyleMacro) s, t.getOffset(), t.getEndOffset());
}
else if (s instanceof CPreprocessor) {
int sequenceNumber= fLocationMap.getSequenceNumberForOffset(t.getOffset());

View file

@ -136,8 +136,10 @@ class ObjectStyleMacro extends PreprocessorMacro {
}
private void setSource(Token t) {
final int shift= -fExpansionOffset;
while (t != null) {
t.fSource= this;
t.shiftOffset(shift);
t= (Token) t.getNext();
}
}

View file

@ -65,6 +65,11 @@ public class Token implements IToken, Cloneable {
fEndOffset= endOffset;
}
public void shiftOffset(int shift) {
fOffset+= shift;
fEndOffset+= shift;
}
public char[] getCharImage() {
return TokenUtil.getImage(getType());
}