1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-09 01:05:38 +02:00

fix include path issue, and add regression test

This commit is contained in:
Andrew Ferguson 2007-03-28 11:01:08 +00:00
parent b93b822f9d
commit ea99f62e2f
4 changed files with 115 additions and 80 deletions

View file

@ -25,6 +25,7 @@ import junit.framework.Test;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager; import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.index.IIndexLocationConverter; import org.eclipse.cdt.core.index.IIndexLocationConverter;
import org.eclipse.cdt.core.index.IIndexerStateEvent; import org.eclipse.cdt.core.index.IIndexerStateEvent;
import org.eclipse.cdt.core.index.IIndexerStateListener; import org.eclipse.cdt.core.index.IIndexerStateListener;
@ -53,7 +54,8 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
private static final String SDK_VERSION = "com.acme.sdk.version"; private static final String SDK_VERSION = "com.acme.sdk.version";
private static final String ACME_SDK_ID= "com.acme.sdk.4.0.1"; private static final String ACME_SDK_ID= "com.acme.sdk.4.0.1";
private static List toDeleteOnTearDown= new ArrayList(); private static List toDeleteOnTearDown= new ArrayList();
private final static String s= "resources/pdomtests/generatePDOMTests/project1"; private final static String locProject1= "resources/pdomtests/generatePDOMTests/project1";
private final static String locProject2= "resources/pdomtests/generatePDOMTests/project2";
private URI baseURI; private URI baseURI;
public static Test suite() { public static Test suite() {
@ -174,7 +176,7 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
}; };
CCorePlugin.getIndexManager().addIndexerStateListener(listener); CCorePlugin.getIndexManager().addIndexerStateListener(listener);
URL url= FileLocator.find(CTestPlugin.getDefault().getBundle(), new Path(s), null); URL url= FileLocator.find(CTestPlugin.getDefault().getBundle(), new Path(locProject1), null);
String baseDir= FileLocator.toFileURL(url).getFile(); String baseDir= FileLocator.toFileURL(url).getFile();
doGenerate(new String[] { doGenerate(new String[] {
@ -195,14 +197,60 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
assertTrue(stateCount[0] == 2); assertTrue(stateCount[0] == 2);
} }
public void verifyProject1Content(WritablePDOM wpdom) throws CoreException { public void testExternalExportProjectProvider_SysIncludes() throws Exception {
File target= File.createTempFile("test", "pdom");
final int[] stateCount = new int[1];
IIndexerStateListener listener= new IIndexerStateListener() {
public void indexChanged(IIndexerStateEvent event) {
stateCount[0]++;
}
};
CCorePlugin.getIndexManager().addIndexerStateListener(listener);
URL url= FileLocator.find(CTestPlugin.getDefault().getBundle(), new Path(locProject2), null);
String baseDir= FileLocator.toFileURL(url).getFile();
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, ExternalExportProjectProvider.class.getName(),
ExternalExportProjectProvider.OPT_SOURCE, baseDir,
ExternalExportProjectProvider.OPT_FRAGMENT_ID, "hello.world"
});
assertTrue(target.exists());
WritablePDOM wpdom= new WritablePDOM(target, new URIRelativeLocationConverter(baseURI));
verifyProject2Content(wpdom);
}
public void verifyProject1Content(WritablePDOM wpdom) throws Exception {
wpdom.acquireReadLock();
try {
IBinding[] bindings= wpdom.findBindings(Pattern.compile(".*foo.*"), false, IndexFilter.ALL, PROGRESS); IBinding[] bindings= wpdom.findBindings(Pattern.compile(".*foo.*"), false, IndexFilter.ALL, PROGRESS);
assertEquals(1, bindings.length); assertEquals(1, bindings.length);
bindings= wpdom.findBindings(Pattern.compile(".*bar.*"), false, IndexFilter.ALL, PROGRESS); bindings= wpdom.findBindings(Pattern.compile(".*bar.*"), false, IndexFilter.ALL, PROGRESS);
assertEquals(1, bindings.length); assertEquals(1, bindings.length);
} finally {
wpdom.releaseReadLock();
}
} }
public void verifyProject2Content(WritablePDOM wpdom) throws Exception {
wpdom.acquireReadLock();
try {
IBinding[] bindings= wpdom.findBindings(Pattern.compile(".*"), true, IndexFilter.ALL, NPM);
assertEquals(2, bindings.length);
int b= bindings[0].getName().equals("A") ? 1 : 0;
assertTrue(bindings[0] instanceof ICPPClassType);
assertTrue(bindings[1] instanceof ICPPClassType);
assertTrue(((ICPPClassType)bindings[1-b]).getBases().length==0);
assertTrue(((ICPPClassType)bindings[b]).getBases().length==1);
} finally {
wpdom.releaseReadLock();
}
}
private void doGenerate(String[] args) throws CoreException { private void doGenerate(String[] args) throws CoreException {
GeneratePDOMApplication app = new GeneratePDOMApplication(); GeneratePDOMApplication app = new GeneratePDOMApplication();
@ -224,7 +272,7 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
public ICProject createProject() throws CoreException { public ICProject createProject() throws CoreException {
ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER); ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER);
toDeleteOnTearDown.add(cproject); toDeleteOnTearDown.add(cproject);
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), s); CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), locProject1);
return cproject; return cproject;
} }
public Map getExportProperties() {return null;} public Map getExportProperties() {return null;}
@ -236,7 +284,7 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
public ICProject createProject() throws CoreException { public ICProject createProject() throws CoreException {
ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER); ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER);
toDeleteOnTearDown.add(cproject); toDeleteOnTearDown.add(cproject);
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), s); CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), locProject1);
return cproject; return cproject;
} }
public Map getExportProperties() {return null;} public Map getExportProperties() {return null;}
@ -250,7 +298,7 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
public ICProject createProject() throws CoreException { public ICProject createProject() throws CoreException {
ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER); ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER);
toDeleteOnTearDown.add(cproject); toDeleteOnTearDown.add(cproject);
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), s); CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), locProject1);
return cproject; return cproject;
} }
public Map getExportProperties() { public Map getExportProperties() {

View file

@ -0,0 +1,3 @@
#include <base.h>
class B : public A {};

View file

@ -22,9 +22,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.cdt.core.dom.IPDOMManager; import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.index.IIndexLocationConverter; import org.eclipse.cdt.core.index.IIndexLocationConverter;
import org.eclipse.cdt.core.index.ResourceContainerRelativeLocationConverter; import org.eclipse.cdt.core.index.ResourceContainerRelativeLocationConverter;
@ -41,7 +39,6 @@ import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable; import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
@ -59,6 +56,7 @@ import org.eclipse.core.runtime.Path;
* </ul> * </ul>
*/ */
public class ExternalExportProjectProvider extends AbstractExportProjectProvider implements IExportProjectProvider { public class ExternalExportProjectProvider extends AbstractExportProjectProvider implements IExportProjectProvider {
private static final String PREBUILT_PROJECT_OWNER = "org.eclipse.cdt.core.index.export.prebuiltOwner"; //$NON-NLS-1$
private static final String ORG_ECLIPSE_CDT_CORE_INDEX_EXPORT_DATESTAMP = "org.eclipse.cdt.core.index.export.datestamp"; //$NON-NLS-1$ private static final String ORG_ECLIPSE_CDT_CORE_INDEX_EXPORT_DATESTAMP = "org.eclipse.cdt.core.index.export.datestamp"; //$NON-NLS-1$
private static final String CONTENT = "content"; //$NON-NLS-1$ private static final String CONTENT = "content"; //$NON-NLS-1$
public static final String OPT_SOURCE = "-source"; //$NON-NLS-1$ public static final String OPT_SOURCE = "-source"; //$NON-NLS-1$
@ -107,7 +105,6 @@ public class ExternalExportProjectProvider extends AbstractExportProjectProvider
* Convenience method for creating a cproject * Convenience method for creating a cproject
* @param projectName the name for the new project * @param projectName the name for the new project
* @param location the absolute path of some external content * @param location the absolute path of some external content
* @param indexerID the indexer to use
* @param includeFiles a list of include paths to add to the project scanner * @param includeFiles a list of include paths to add to the project scanner
* @return a new project * @return a new project
* @throws CoreException * @throws CoreException
@ -122,23 +119,15 @@ public class ExternalExportProjectProvider extends AbstractExportProjectProvider
ws.run(new IWorkspaceRunnable() { ws.run(new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException { public void run(IProgressMonitor monitor) throws CoreException {
IWorkspaceRoot root = ws.getRoot(); IWorkspace workspace= ResourcesPlugin.getWorkspace();
IProject project= workspace.getRoot().getProject("__prebuilt_index_temp__"+System.currentTimeMillis()); //$NON-NLS-1$
IProjectDescription description = workspace.newProjectDescription(project.getName());
CCorePlugin.getDefault().createCProject(description, project, NPM, PREBUILT_PROJECT_OWNER);
CCorePlugin.getDefault().convertProjectFromCtoCC(project, NPM);
ICProjectDescription pd= CCorePlugin.getDefault().getProjectDescription(project, true);
newCfg(pd, project.getName(), "config"); //$NON-NLS-1$
IProject project = root.getProject(projectName); CoreModel.getDefault().setProjectDescription(project, pd, true, new NullProgressMonitor());
if (!project.exists()) {
project.create(NPM);
} else {
project.refreshLocal(IResource.DEPTH_INFINITE, NPM);
}
if (!project.isOpen()) {
project.open(NPM);
}
if (!project.hasNature(CProjectNature.C_NATURE_ID)) {
addNatureToProject(project, CProjectNature.C_NATURE_ID, NPM);
}
if (!project.hasNature(CCProjectNature.CC_NATURE_ID)) {
addNatureToProject(project, CCProjectNature.CC_NATURE_ID, NPM);
}
ICProject cproject= CCorePlugin.getDefault().getCoreModel().create(project); ICProject cproject= CCorePlugin.getDefault().getCoreModel().create(project);
@ -148,24 +137,23 @@ public class ExternalExportProjectProvider extends AbstractExportProjectProvider
// Setup path entries // Setup path entries
List entries= new ArrayList(Arrays.asList(CoreModel.getRawPathEntries(cproject))); List entries= new ArrayList(Arrays.asList(CoreModel.getRawPathEntries(cproject)));
// pre-include files
for(Iterator j= includeFiles.iterator(); j.hasNext(); ) { for(Iterator j= includeFiles.iterator(); j.hasNext(); ) {
entries.add( String path= (String) j.next();
CoreModel.newIncludeFileEntry( entries.add(CoreModel.newIncludeFileEntry(project.getFullPath(), new Path(path)));
cproject.getProject().getFullPath(),
new Path((String) j.next())
));
} }
// content directory is a source root
entries.add(CoreModel.newSourceEntry(content.getProjectRelativePath())); entries.add(CoreModel.newSourceEntry(content.getProjectRelativePath()));
cproject.setRawPathEntries(
(IPathEntry[]) entries.toArray(new IPathEntry[includeFiles.size()]), // any additional entries
entries.addAll(getAdditionalRawEntries());
cproject.setRawPathEntries((IPathEntry[]) entries.toArray(new IPathEntry[entries.size()]),
new NullProgressMonitor() new NullProgressMonitor()
); );
ICProjectDescription pd= CoreModel.getDefault().createProjectDescription(cproject.getProject(), false); // create the description
newCfg(pd, project.getName(), "cfg1"); //$NON-NLS-1$
CoreModel.getDefault().setProjectDescription(cproject.getProject(), pd, true, new NullProgressMonitor());
newProject[0]= cproject; newProject[0]= cproject;
IndexerPreferences.set(newProject[0].getProject(), IndexerPreferences.KEY_INDEX_ALL_FILES, Boolean.TRUE.toString()); IndexerPreferences.set(newProject[0].getProject(), IndexerPreferences.KEY_INDEX_ALL_FILES, Boolean.TRUE.toString());
@ -176,27 +164,22 @@ public class ExternalExportProjectProvider extends AbstractExportProjectProvider
return newProject[0]; return newProject[0];
} }
/**
* Get additional raw entries (above those added as part of the ExternalExportProjectProvider functionality)
* @return a list of additional entries to add to the project
*/
protected List getAdditionalRawEntries() {
List entries= new ArrayList();
entries.add(CoreModel.newIncludeEntry(content.getProjectRelativePath(), null, content.getLocation(), true));
return entries;
}
private ICConfigurationDescription newCfg(ICProjectDescription des, String project, String config) throws CoreException { private ICConfigurationDescription newCfg(ICProjectDescription des, String project, String config) throws CoreException {
CDefaultConfigurationData data= new CDefaultConfigurationData(project+"."+config, project+" "+config+" name", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ CDefaultConfigurationData data= new CDefaultConfigurationData(project+"."+config, project+" "+config+" name", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
data.initEmptyData(); data.initEmptyData();
String ID= CCorePlugin.PLUGIN_ID + ".defaultConfigDataProvider"; //$NON-NLS-1$ return des.createConfiguration(CCorePlugin.DEFAULT_PROVIDER_ID, data);
return des.createConfiguration(ID, data);
} }
/*
* This should be a platform/CDT API
*/
private static void addNatureToProject(IProject proj, String natureId, IProgressMonitor monitor) throws CoreException {
IProjectDescription description = proj.getDescription();
String[] prevNatures = description.getNatureIds();
String[] newNatures = new String[prevNatures.length + 1];
System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
newNatures[prevNatures.length] = natureId;
description.setNatureIds(newNatures);
proj.setDescription(description, monitor);
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.eclipse.cdt.core.index.export.IExportProjectProvider#getLocationConverter(org.eclipse.cdt.core.model.ICProject) * @see org.eclipse.cdt.core.index.export.IExportProjectProvider#getLocationConverter(org.eclipse.cdt.core.model.ICProject)