mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-09 09:15:38 +02:00
fix include path issue, and add regression test
This commit is contained in:
parent
b93b822f9d
commit
ea99f62e2f
4 changed files with 115 additions and 80 deletions
|
@ -25,6 +25,7 @@ import junit.framework.Test;
|
|||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.dom.IPDOMManager;
|
||||
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.IIndexerStateEvent;
|
||||
import org.eclipse.cdt.core.index.IIndexerStateListener;
|
||||
|
@ -53,13 +54,14 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
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 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;
|
||||
|
||||
|
||||
public static Test suite() {
|
||||
return suite(GeneratePDOMApplicationTest.class);
|
||||
}
|
||||
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
toDeleteOnTearDown.clear();
|
||||
baseURI= new URI("file:///base/"); // unimportant what the value is
|
||||
|
@ -107,7 +109,7 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
assertTrue(target.exists());
|
||||
WritablePDOM wpdom= new WritablePDOM(target, new URIRelativeLocationConverter(baseURI));
|
||||
verifyProject1Content(wpdom);
|
||||
|
||||
|
||||
String fid= wpdom.getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID);
|
||||
assertNotNull(fid);
|
||||
assertTrue(fid.startsWith("export")); // check for default export id
|
||||
|
@ -122,16 +124,16 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
assertTrue(target.exists());
|
||||
WritablePDOM wpdom= new WritablePDOM(target, new URIRelativeLocationConverter(baseURI));
|
||||
verifyProject1Content(wpdom);
|
||||
|
||||
|
||||
String fid= wpdom.getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID);
|
||||
assertNotNull(fid);
|
||||
assertEquals(ACME_SDK_ID, fid); // check for custom export id
|
||||
|
||||
|
||||
String sdkVer= wpdom.getProperty(SDK_VERSION);
|
||||
assertNotNull(sdkVer);
|
||||
assertEquals("4.0.1", sdkVer); // check for custom property value
|
||||
}
|
||||
|
||||
|
||||
public void testExternalExportProjectProvider_BadCmdLine1() throws Exception {
|
||||
File target= File.createTempFile("test", "pdom");
|
||||
try {
|
||||
|
@ -162,10 +164,10 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
// correct behaviour
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testExternalExportProjectProvider() throws Exception {
|
||||
File target= File.createTempFile("test", "pdom");
|
||||
|
||||
|
||||
final int[] stateCount = new int[1];
|
||||
IIndexerStateListener listener= new IIndexerStateListener() {
|
||||
public void indexChanged(IIndexerStateEvent event) {
|
||||
|
@ -173,10 +175,10 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
}
|
||||
};
|
||||
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();
|
||||
|
||||
|
||||
doGenerate(new String[] {
|
||||
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
|
||||
GeneratePDOMApplication.OPT_PROJECTPROVIDER, ExternalExportProjectProvider.class.getName(),
|
||||
|
@ -184,26 +186,72 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
ExternalExportProjectProvider.OPT_FRAGMENT_ID, "hello.world"
|
||||
});
|
||||
assertTrue(target.exists());
|
||||
|
||||
|
||||
WritablePDOM wpdom= new WritablePDOM(target, new URIRelativeLocationConverter(baseURI));
|
||||
verifyProject1Content(wpdom);
|
||||
|
||||
|
||||
String fid= wpdom.getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID);
|
||||
assertNotNull(fid);
|
||||
assertEquals("hello.world", fid); // check for id passed on command-line
|
||||
|
||||
|
||||
assertTrue(stateCount[0] == 2);
|
||||
}
|
||||
|
||||
public void verifyProject1Content(WritablePDOM wpdom) throws CoreException {
|
||||
IBinding[] bindings= wpdom.findBindings(Pattern.compile(".*foo.*"), false, IndexFilter.ALL, PROGRESS);
|
||||
assertEquals(1, bindings.length);
|
||||
|
||||
bindings= wpdom.findBindings(Pattern.compile(".*bar.*"), false, IndexFilter.ALL, PROGRESS);
|
||||
assertEquals(1, bindings.length);
|
||||
|
||||
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);
|
||||
assertEquals(1, bindings.length);
|
||||
|
||||
bindings= wpdom.findBindings(Pattern.compile(".*bar.*"), false, IndexFilter.ALL, PROGRESS);
|
||||
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 {
|
||||
GeneratePDOMApplication app = new GeneratePDOMApplication();
|
||||
IApplicationContext ac= new MockApplicationContext(args);
|
||||
|
@ -224,7 +272,7 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
public ICProject createProject() throws CoreException {
|
||||
ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER);
|
||||
toDeleteOnTearDown.add(cproject);
|
||||
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), s);
|
||||
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), locProject1);
|
||||
return cproject;
|
||||
}
|
||||
public Map getExportProperties() {return null;}
|
||||
|
@ -236,7 +284,7 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
public ICProject createProject() throws CoreException {
|
||||
ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER);
|
||||
toDeleteOnTearDown.add(cproject);
|
||||
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), s);
|
||||
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), locProject1);
|
||||
return cproject;
|
||||
}
|
||||
public Map getExportProperties() {return null;}
|
||||
|
@ -245,12 +293,12 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
|
|||
}
|
||||
public void setApplicationArguments(String[] arguments) {}
|
||||
}
|
||||
|
||||
|
||||
public static class TestProjectProvider4 implements IExportProjectProvider {
|
||||
public ICProject createProject() throws CoreException {
|
||||
ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER);
|
||||
toDeleteOnTearDown.add(cproject);
|
||||
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), s);
|
||||
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), locProject1);
|
||||
return cproject;
|
||||
}
|
||||
public Map getExportProperties() {
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
#include <base.h>
|
||||
|
||||
class B : public A {};
|
|
@ -0,0 +1 @@
|
|||
class A {};
|
|
@ -22,9 +22,7 @@ import java.util.Iterator;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.cdt.core.CCProjectNature;
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.CProjectNature;
|
||||
import org.eclipse.cdt.core.dom.IPDOMManager;
|
||||
import org.eclipse.cdt.core.index.IIndexLocationConverter;
|
||||
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.IResource;
|
||||
import org.eclipse.core.resources.IWorkspace;
|
||||
import org.eclipse.core.resources.IWorkspaceRoot;
|
||||
import org.eclipse.core.resources.IWorkspaceRunnable;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
|
@ -59,6 +56,7 @@ import org.eclipse.core.runtime.Path;
|
|||
* </ul>
|
||||
*/
|
||||
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 CONTENT = "content"; //$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
|
||||
* @param projectName the name for the new project
|
||||
* @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
|
||||
* @return a new project
|
||||
* @throws CoreException
|
||||
|
@ -122,50 +119,41 @@ public class ExternalExportProjectProvider extends AbstractExportProjectProvider
|
|||
|
||||
ws.run(new IWorkspaceRunnable() {
|
||||
public void run(IProgressMonitor monitor) throws CoreException {
|
||||
IWorkspaceRoot root = ws.getRoot();
|
||||
|
||||
IProject project = root.getProject(projectName);
|
||||
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);
|
||||
|
||||
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$
|
||||
|
||||
CoreModel.getDefault().setProjectDescription(project, pd, true, new NullProgressMonitor());
|
||||
|
||||
ICProject cproject= CCorePlugin.getDefault().getCoreModel().create(project);
|
||||
|
||||
// External content appears under a linked folder
|
||||
content= cproject.getProject().getFolder(CONTENT);
|
||||
content.createLink(new Path(location.getAbsolutePath()), IResource.NONE, null);
|
||||
|
||||
// Setup path entries
|
||||
List entries= new ArrayList(Arrays.asList(CoreModel.getRawPathEntries(cproject)));
|
||||
|
||||
// pre-include files
|
||||
for(Iterator j= includeFiles.iterator(); j.hasNext(); ) {
|
||||
entries.add(
|
||||
CoreModel.newIncludeFileEntry(
|
||||
cproject.getProject().getFullPath(),
|
||||
new Path((String) j.next())
|
||||
));
|
||||
String path= (String) j.next();
|
||||
entries.add(CoreModel.newIncludeFileEntry(project.getFullPath(), new Path(path)));
|
||||
}
|
||||
|
||||
// content directory is a source root
|
||||
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()
|
||||
);
|
||||
|
||||
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;
|
||||
|
||||
IndexerPreferences.set(newProject[0].getProject(), IndexerPreferences.KEY_INDEX_ALL_FILES, Boolean.TRUE.toString());
|
||||
|
@ -175,27 +163,22 @@ public class ExternalExportProjectProvider extends AbstractExportProjectProvider
|
|||
|
||||
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 {
|
||||
CDefaultConfigurationData data= new CDefaultConfigurationData(project+"."+config, project+" "+config+" name", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
data.initEmptyData();
|
||||
String ID= CCorePlugin.PLUGIN_ID + ".defaultConfigDataProvider"; //$NON-NLS-1$
|
||||
return des.createConfiguration(ID, data);
|
||||
return des.createConfiguration(CCorePlugin.DEFAULT_PROVIDER_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)
|
||||
|
|
Loading…
Add table
Reference in a new issue