1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00

Fix and test for macro expansion handling in model builder

This commit is contained in:
Anton Leherbauer 2008-01-15 14:39:19 +00:00
parent 8a24d5717e
commit 01323153a0
6 changed files with 115 additions and 101 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2002, 2006 IBM Corporation and others. * Copyright (c) 2002, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -10,13 +10,9 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.model.failedTests; package org.eclipse.cdt.core.model.failedTests;
import java.util.Stack; import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ISourceRange;
import org.eclipse.cdt.core.model.ISourceReference;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.tests.IntegratedCModelTest; import org.eclipse.cdt.core.model.tests.IntegratedCModelTest;
/** /**
@ -25,6 +21,11 @@ import org.eclipse.cdt.core.model.tests.IntegratedCModelTest;
*/ */
public class FailedMacroTests extends IntegratedCModelTest public class FailedMacroTests extends IntegratedCModelTest
{ {
public static Test suite() {
TestSuite suite= new TestSuite("FailedMacroTests");
return suite;
}
/** /**
* *
*/ */
@ -56,66 +57,4 @@ public class FailedMacroTests extends IntegratedCModelTest
return "MacroTests.cpp"; return "MacroTests.cpp";
} }
private final static boolean failedTest = true;
/* This is a list of elements in the test .c file. It will be used
* in a number of places in the tests
*/
String[] expectedStringList= {"Z", "X", "Y",
"SomeName", "", "A::BCD", "DEFA", "DB", "B::SomeName",
"PINT", "myPINT", "foobar"};
int[] expectedOffsets={ 8,26,39,55,75,89,114,130,152,187,212,227};
int[] expectedLengths={ 1, 1, 1, 1, 1, 8, 4, 2, 18, 4, 6, 6};
/* This is a list of that the types of the above list of elements is
* expected to be.
*/
int[] expectedTypes= { ICElement.C_MACRO, ICElement.C_MACRO,
ICElement.C_MACRO, ICElement.C_STRUCT,
ICElement.C_STRUCT, ICElement.C_VARIABLE, ICElement.C_MACRO,
ICElement.C_MACRO, ICElement.C_VARIABLE, ICElement.C_MACRO,
ICElement.C_VARIABLE, ICElement.C_FUNCTION_DECLARATION};
public void testBug40759 () throws CModelException {
ITranslationUnit myTranslationUnit = getTU();
ICElement myElement;
Stack missing=new Stack();
int x;
for (x=0;x<expectedStringList.length;x++) {
myElement=myTranslationUnit.getElement(expectedStringList[x]);
if (myElement==null)
missing.push(expectedStringList[x]);
else {
assertTrue("Expected:" + expectedStringList[x] + " Got:" + myElement.getElementName(),
expectedStringList[x].equals(myElement.getElementName()));
assertTrue("Expected type for '" + expectedStringList[x] + "':" + expectedTypes[x] + " Got:" + myElement.getElementType(),
expectedTypes[x] == myElement.getElementType());
int offset = -1;
int length = -1;
if (myElement instanceof ISourceReference) {
ISourceRange range = ((ISourceReference)myElement).getSourceRange();
offset = range.getIdStartPos();
length = range.getIdLength();
}
assertTrue("Expected offset for '" + expectedStringList[x] + "':" + expectedOffsets[x] + " Got:" + offset,
expectedOffsets[x] == offset);
if( ! failedTest )
assertTrue( "Expected length for '" + expectedStringList[x] + "':" + expectedLengths[x] + " Got:" + length,
expectedLengths[x] == length);
}
}
if (!missing.empty()) {
String output=new String("Could not get elements: ");
while (!missing.empty())
output+=missing.pop() + " ";
assertTrue(output, false);
}
}
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2000, 2007 QNX Software Systems and others. * Copyright (c) 2000, 2008 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -45,6 +45,8 @@ public class AllCoreTests {
suite.addTest(TranslationUnitTests.suite()); suite.addTest(TranslationUnitTests.suite());
suite.addTest(DeclaratorsTests.suite()); suite.addTest(DeclaratorsTests.suite());
suite.addTest(FailedDeclaratorsTest.suite()); suite.addTest(FailedDeclaratorsTest.suite());
suite.addTest(MacroTests.suite());
// suite.addTest(FailedMacroTests.suite());
suite.addTest(CPathEntryTest.suite()); suite.addTest(CPathEntryTest.suite());
// suite.addTest(CConfigurationDescriptionReferenceTests.suite()); // suite.addTest(CConfigurationDescriptionReferenceTests.suite());
//the CProjectDescriptionTests now groups all New Project Model related tests //the CProjectDescriptionTests now groups all New Project Model related tests

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005, 2006 IBM Corporation and others. * Copyright (c) 2005, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -8,15 +8,23 @@
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * IBM Corporation - initial API and implementation
*******************************************************************************/ *******************************************************************************/
/*
* Created on Jun 9, 2003
* by bnicolle
*/
package org.eclipse.cdt.core.model.tests; package org.eclipse.cdt.core.model.tests;
import java.util.Stack;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.IBuffer;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ISourceRange;
import org.eclipse.cdt.core.model.ISourceReference;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DefaultLineTracker;
import org.eclipse.jface.text.ILineTracker;
/** /**
* @author bnicolle * @author bnicolle
@ -53,6 +61,76 @@ public class MacroTests extends IntegratedCModelTest {
return suite; return suite;
} }
public void testBug40759 () throws CModelException, BadLocationException {
/* This is a list of elements in the test .c file. It will be used
* in a number of places in the tests
*/
String[] expectedStringList= {"Z", "X", "Y",
"SomeName", "A::BCD", "DEFA", "DB", "B::SomeName",
"PINT", "myPINT", "foobar"};
int[] expectedOffsets={ 8,26,39,55,89,114,130,152,187,212,227};
int[] expectedLengths={ 1, 1, 1, 1, 8, 4, 2, 18, 4, 6, 6};
/* This is a list of that the types of the above list of elements is
* expected to be.
*/
int[] expectedTypes= { ICElement.C_MACRO, ICElement.C_MACRO,
ICElement.C_MACRO, ICElement.C_STRUCT,
ICElement.C_VARIABLE, ICElement.C_MACRO,
ICElement.C_MACRO, ICElement.C_VARIABLE, ICElement.C_MACRO,
ICElement.C_VARIABLE, ICElement.C_FUNCTION_DECLARATION};
ITranslationUnit myTranslationUnit = getTU();
// fix offsets in case source file is not in windows format
IBuffer buffer= myTranslationUnit.getBuffer();
ILineTracker lineTracker= new DefaultLineTracker();
lineTracker.set(buffer.getContents());
if (lineTracker.getLineDelimiter(0).length() == 1) {
lineTracker.set(buffer.getContents().replaceAll("[\r\n]","\r\n"));
for (int i = 0; i < expectedOffsets.length; i++) {
expectedOffsets[i] -= lineTracker.getLineNumberOfOffset(expectedOffsets[i]);
}
}
ICElement myElement;
Stack missing=new Stack();
int x;
for (x=0;x<expectedStringList.length;x++) {
myElement=myTranslationUnit.getElement(expectedStringList[x]);
if (myElement==null)
missing.push(expectedStringList[x]);
else {
assertTrue("Expected:" + expectedStringList[x] + " Got:" + myElement.getElementName(),
expectedStringList[x].equals(myElement.getElementName()));
assertTrue("Expected type for '" + expectedStringList[x] + "':" + expectedTypes[x] + " Got:" + myElement.getElementType(),
expectedTypes[x] == myElement.getElementType());
int offset = -1;
int length = -1;
if (myElement instanceof ISourceReference) {
ISourceRange range = ((ISourceReference)myElement).getSourceRange();
offset = range.getIdStartPos();
length = range.getIdLength();
}
assertTrue("Expected offset for '" + expectedStringList[x] + "':" + expectedOffsets[x] + " Got:" + offset,
expectedOffsets[x] == offset);
assertTrue( "Expected length for '" + expectedStringList[x] + "':" + expectedLengths[x] + " Got:" + length,
expectedLengths[x] == length);
}
}
if (!missing.empty()) {
String output=new String("Could not get elements: ");
while (!missing.empty())
output+=missing.pop() + " ";
assertTrue(output, false);
}
}
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2007 Wind River Systems, Inc. and others. * Copyright (c) 2006, 2008 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -192,7 +192,7 @@ public class CModelBuilder2 implements IContributedModelBuilder {
private final TranslationUnit fTranslationUnit; private final TranslationUnit fTranslationUnit;
private String fTranslationUnitFileName; private String fTranslationUnitFileName;
private ASTAccessVisibility fCurrentVisibility; private ASTAccessVisibility fCurrentVisibility;
private Stack fVisibilityStack; private Stack<ASTAccessVisibility> fVisibilityStack;
private IProgressMonitor fProgressMonitor; private IProgressMonitor fProgressMonitor;
/** /**
@ -282,11 +282,11 @@ public class CModelBuilder2 implements IContributedModelBuilder {
*/ */
private void buildModel(IASTTranslationUnit ast) throws CModelException, DOMException { private void buildModel(IASTTranslationUnit ast) throws CModelException, DOMException {
fTranslationUnitFileName= ast.getFilePath(); fTranslationUnitFileName= ast.getFilePath();
fVisibilityStack= new Stack(); fVisibilityStack= new Stack<ASTAccessVisibility>();
// includes // includes
final IASTPreprocessorIncludeStatement[] includeDirectives= ast.getIncludeDirectives(); final IASTPreprocessorIncludeStatement[] includeDirectives= ast.getIncludeDirectives();
Set allIncludes= new HashSet(); Set<Include> allIncludes= new HashSet<Include>();
for (int i= 0; i < includeDirectives.length; i++) { for (int i= 0; i < includeDirectives.length; i++) {
IASTPreprocessorIncludeStatement includeDirective= includeDirectives[i]; IASTPreprocessorIncludeStatement includeDirective= includeDirectives[i];
if (isLocalToFile(includeDirective)) { if (isLocalToFile(includeDirective)) {
@ -311,12 +311,12 @@ public class CModelBuilder2 implements IContributedModelBuilder {
} }
// sort by offset // sort by offset
final List children= fTranslationUnit.getElementInfo().internalGetChildren(); final List<SourceManipulation> children= fTranslationUnit.getElementInfo().internalGetChildren();
Collections.sort(children, new Comparator() { Collections.sort(children, new Comparator<SourceManipulation>() {
public int compare(Object o1, Object o2) { public int compare(SourceManipulation o1, SourceManipulation o2) {
try { try {
final SourceManipulationInfo info1= ((SourceManipulation)o1).getSourceManipulationInfo(); final SourceManipulationInfo info1= o1.getSourceManipulationInfo();
final SourceManipulationInfo info2= ((SourceManipulation)o2).getSourceManipulationInfo(); final SourceManipulationInfo info2= o2.getSourceManipulationInfo();
int delta= info1.getStartPos() - info2.getStartPos(); int delta= info1.getStartPos() - info2.getStartPos();
if (delta == 0) { if (delta == 0) {
delta= info1.getIdStartPos() - info2.getIdStartPos(); delta= info1.getIdStartPos() - info2.getIdStartPos();
@ -358,7 +358,7 @@ public class CModelBuilder2 implements IContributedModelBuilder {
return fTranslationUnitFileName.equals(node.getContainingFilename()); return fTranslationUnitFileName.equals(node.getContainingFilename());
} }
private Include createInclusion(Parent parent, IASTPreprocessorIncludeStatement inclusion, Set allIncludes) throws CModelException{ private Include createInclusion(Parent parent, IASTPreprocessorIncludeStatement inclusion, Set<Include> allIncludes) throws CModelException{
// create element // create element
final IASTName name= inclusion.getName(); final IASTName name= inclusion.getName();
Include element= new Include(parent, ASTStringUtil.getSimpleName(name), inclusion.isSystemInclude()); Include element= new Include(parent, ASTStringUtil.getSimpleName(name), inclusion.isSystemInclude());
@ -1212,12 +1212,7 @@ public class CModelBuilder2 implements IContributedModelBuilder {
* @param astName * @param astName
*/ */
private void setIdentifierPosition(SourceManipulationInfo info, IASTNode astName) { private void setIdentifierPosition(SourceManipulationInfo info, IASTNode astName) {
final IASTFileLocation location; final IASTFileLocation location= astName.getFileLocation();
if (astName instanceof IASTName) {
location= ((IASTName)astName).getImageLocation();
} else {
location= astName.getFileLocation();
}
if (location != null) { if (location != null) {
info.setIdPos(location.getNodeOffset(), location.getNodeLength()); info.setIdPos(location.getNodeOffset(), location.getNodeLength());
} else { } else {
@ -1293,7 +1288,7 @@ public class CModelBuilder2 implements IContributedModelBuilder {
*/ */
private void popDefaultVisibility() { private void popDefaultVisibility() {
if (!fVisibilityStack.isEmpty()) { if (!fVisibilityStack.isEmpty()) {
setCurrentVisibility((ASTAccessVisibility)fVisibilityStack.pop()); setCurrentVisibility(fVisibilityStack.pop());
} }
} }

View file

@ -391,7 +391,7 @@ public class SemanticHighlightingReconciler implements ICReconcilingListener {
*/ */
private void addNameLocation(IASTName name, HighlightingStyle highlightingStyle) { private void addNameLocation(IASTName name, HighlightingStyle highlightingStyle) {
IASTImageLocation imageLocation= name.getImageLocation(); IASTImageLocation imageLocation= name.getImageLocation();
if (imageLocation == null) { if (imageLocation == null || !fFilePath.equals(imageLocation.getFileName())) {
addNodeLocation(name.getFileLocation(), highlightingStyle); addNodeLocation(name.getFileLocation(), highlightingStyle);
} else { } else {
int offset= imageLocation.getNodeOffset(); int offset= imageLocation.getNodeOffset();

View file

@ -14,9 +14,9 @@ package org.eclipse.cdt.internal.ui.search;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; 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.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
@ -107,13 +107,13 @@ public class OccurrencesFinder implements IOccurrencesFinder {
if (binding != null /* && Bindings.equals(binding, fTarget) */) { if (binding != null /* && Bindings.equals(binding, fTarget) */) {
int flag= 0; int flag= 0;
String description= fDescription; String description= fDescription;
IASTNodeLocation nodeLocation= node.getImageLocation(); IASTFileLocation fileLocation= node.getImageLocation();
if (nodeLocation == null) { if (fileLocation == null || !fRoot.getFilePath().equals(fileLocation.getFileName())) {
nodeLocation= node.getFileLocation(); fileLocation= node.getFileLocation();
} }
if (nodeLocation != null) { if (fileLocation != null) {
final int offset= nodeLocation.getNodeOffset(); final int offset= fileLocation.getNodeOffset();
final int length= nodeLocation.getNodeLength(); final int length= fileLocation.getNodeLength();
if (offset >= 0 && length > 0) { if (offset >= 0 && length > 0) {
fResult.add(new OccurrenceLocation(offset, length, flag, description)); fResult.add(new OccurrenceLocation(offset, length, flag, description));
} }