From f91d763e7491a7a28227da55bfa223bb5d71d098 Mon Sep 17 00:00:00 2001 From: Vladimir Hirsl Date: Mon, 18 Apr 2005 18:40:44 +0000 Subject: [PATCH] Utility function added: PerProjectSICollector.calculateCompilerBuiltins(IProject) Static method to return compiler built-in scanner info. --- .../scannerconfig2/PerProjectSICollector.java | 70 ++++++++++++++ .../core/tests/AllStandardBuildTests.java | 1 + .../tests/ScannerConfigDiscoveryTests.java | 91 +++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/standardbuilder/core/tests/ScannerConfigDiscoveryTests.java diff --git a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/scannerconfig2/PerProjectSICollector.java b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/scannerconfig2/PerProjectSICollector.java index 29e49384b90..7e12a48fda1 100644 --- a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/scannerconfig2/PerProjectSICollector.java +++ b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/scannerconfig2/PerProjectSICollector.java @@ -11,6 +11,7 @@ package org.eclipse.cdt.make.internal.core.scannerconfig2; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; @@ -19,14 +20,22 @@ import java.util.Map; import org.eclipse.cdt.core.CCProjectNature; import org.eclipse.cdt.core.CProjectNature; +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.IPathEntry; import org.eclipse.cdt.make.core.MakeCorePlugin; import org.eclipse.cdt.make.core.MakeProjectNature; +import org.eclipse.cdt.make.core.scannerconfig.IExternalScannerInfoProvider; +import org.eclipse.cdt.make.core.scannerconfig.IScannerConfigBuilderInfo2; +import org.eclipse.cdt.make.core.scannerconfig.IScannerInfoCollector; import org.eclipse.cdt.make.core.scannerconfig.IScannerInfoCollector2; import org.eclipse.cdt.make.core.scannerconfig.IScannerInfoCollectorCleaner; import org.eclipse.cdt.make.core.scannerconfig.ScannerInfoTypes; import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager.IDiscoveredPathInfo; import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager.IPerProjectDiscoveredPathInfo; import org.eclipse.cdt.make.internal.core.MakeMessages; +import org.eclipse.cdt.make.internal.core.scannerconfig.DiscoveredPathContainer; import org.eclipse.cdt.make.internal.core.scannerconfig.DiscoveredPathInfo; import org.eclipse.cdt.make.internal.core.scannerconfig.DiscoveredScannerInfoStore; import org.eclipse.cdt.make.internal.core.scannerconfig.ScannerConfigUtil; @@ -36,8 +45,11 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.ISafeRunnable; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; import org.w3c.dom.Element; /** @@ -424,4 +436,62 @@ public class PerProjectSICollector implements IScannerInfoCollector2, IScannerIn return pathInfo; } + /** + * Static method to return compiler built-in scanner info. + * Preconditions: resource has to be contained by a project that has following natures: + * C nature, CC nature (for C++ projects), Make nature and ScannerConfig nature + * + * @param project + * @throws CModelException + */ + public static void calculateCompilerBuiltins(final IProject project) throws CModelException { + createDiscoveredPathContainer(project, new NullProgressMonitor()); + String scdProfileId = ScannerConfigProfileManager.PER_PROJECT_PROFILE_ID; + SCProfileInstance profileInstance = ScannerConfigProfileManager.getInstance(). + getSCProfileInstance(project, scdProfileId); + final IScannerConfigBuilderInfo2 buildInfo = ScannerConfigProfileManager. + createScannerConfigBuildInfo2(MakeCorePlugin.getDefault().getPluginPreferences(), + scdProfileId, true); + final IScannerInfoCollector collector = profileInstance.getScannerInfoCollector(); + if (collector instanceof IScannerInfoCollectorCleaner) { + ((IScannerInfoCollectorCleaner) collector).deleteAll(project); + } + final IExternalScannerInfoProvider esiProvider = profileInstance.createExternalScannerInfoProvider("specsFile");//$NON-NLS-1$ + + // Set the arguments for the provider + + ISafeRunnable runnable = new ISafeRunnable() { + public void run() throws CoreException { + IProgressMonitor monitor = new NullProgressMonitor(); + esiProvider.invokeProvider(monitor, project, "specsFile", buildInfo, collector);//$NON-NLS-1$ + if (collector instanceof IScannerInfoCollector2) { + IScannerInfoCollector2 collector2 = (IScannerInfoCollector2) collector; + collector2.updateScannerConfiguration(monitor); + } + } + + public void handleException(Throwable exception) { + if (exception instanceof OperationCanceledException) { + throw (OperationCanceledException) exception; + } + } + }; + Platform.run(runnable); + } + + private static void createDiscoveredPathContainer(IProject project, IProgressMonitor monitor) throws CModelException { + IPathEntry container = CoreModel.newContainerEntry(DiscoveredPathContainer.CONTAINER_ID); + ICProject cProject = CoreModel.getDefault().create(project); + if (cProject != null) { + IPathEntry[] entries = cProject.getRawPathEntries(); + List newEntries = new ArrayList(Arrays.asList(entries)); + if (!newEntries.contains(container)) { + newEntries.add(container); + cProject.setRawPathEntries((IPathEntry[])newEntries.toArray(new IPathEntry[newEntries.size()]), monitor); + } + } + // create a new discovered scanner config store + MakeCorePlugin.getDefault().getDiscoveryManager().removeDiscoveredInfo(project); + } + } diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/standardbuilder/core/tests/AllStandardBuildTests.java b/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/standardbuilder/core/tests/AllStandardBuildTests.java index 01a9b57efc0..b060af4b847 100644 --- a/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/standardbuilder/core/tests/AllStandardBuildTests.java +++ b/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/standardbuilder/core/tests/AllStandardBuildTests.java @@ -35,6 +35,7 @@ public class AllStandardBuildTests extends TestSuite { TestSuite suite = new AllStandardBuildTests("Test for org.eclipse.cdt.standardbuild.core.tests"); //$JUnit-BEGIN$ suite.addTestSuite(ScannerConfigConsoleParserTests.class); + suite.addTestSuite(ScannerConfigDiscoveryTests.class); //$JUnit-END$ return suite; } diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/standardbuilder/core/tests/ScannerConfigDiscoveryTests.java b/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/standardbuilder/core/tests/ScannerConfigDiscoveryTests.java new file mode 100644 index 00000000000..d6eb36c4b05 --- /dev/null +++ b/build/org.eclipse.cdt.managedbuilder.core.tests/suite/org/eclipse/cdt/standardbuilder/core/tests/ScannerConfigDiscoveryTests.java @@ -0,0 +1,91 @@ +/********************************************************************** + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + **********************************************************************/ +package org.eclipse.cdt.standardbuilder.core.tests; + +import java.io.ByteArrayInputStream; +import java.util.Map; + +import junit.framework.TestCase; + +import org.eclipse.cdt.core.CCProjectNature; +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.parser.IScannerInfo; +import org.eclipse.cdt.make.core.MakeProjectNature; +import org.eclipse.cdt.make.core.scannerconfig.ScannerConfigNature; +import org.eclipse.cdt.make.internal.core.scannerconfig2.PerProjectSICollector; +import org.eclipse.cdt.managedbuilder.testplugin.ManagedBuildTestHelper; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; + +/** + * Scanner configuration discovery tests + * + * @author vhirsl + */ +public class ScannerConfigDiscoveryTests extends TestCase { + private IProject fCProject = null; + private IFile fCFile = null; + IProgressMonitor fMonitor = null; + + /** + * @param name + */ + public ScannerConfigDiscoveryTests(String name) { + super(name); + } + + protected void setUp() throws Exception { + fMonitor = new NullProgressMonitor(); + + fCProject = ManagedBuildTestHelper.createProject("SCD"); + fCFile = fCProject.getProject().getFile("main.c"); + if (!fCFile.exists()) { + fCFile.create(new ByteArrayInputStream(" \n".getBytes()), false, fMonitor); + } + } + + protected void tearDown() throws Exception { + ManagedBuildTestHelper.removeProject("SCDC"); + } + + public void testGetCCompilerBuiltins() throws CoreException { + MakeProjectNature.addNature(fCProject, fMonitor); + ScannerConfigNature.addScannerConfigNature(fCProject); + + PerProjectSICollector.calculateCompilerBuiltins(fCProject); + IScannerInfo scInfo = CCorePlugin.getDefault().getScannerInfoProvider(fCProject). + getScannerInformation(fCFile); + assertNotNull(scInfo); + String[] includes = scInfo.getIncludePaths(); + assertTrue(includes.length > 0); + Map symbols = scInfo.getDefinedSymbols(); + assertFalse(symbols.isEmpty()); + } + + public void testGetCCCompilerBuiltins() throws CoreException { + CCProjectNature.addCCNature(fCProject, fMonitor); + MakeProjectNature.addNature(fCProject, fMonitor); + ScannerConfigNature.addScannerConfigNature(fCProject); + + PerProjectSICollector.calculateCompilerBuiltins(fCProject); + IScannerInfo scInfo = CCorePlugin.getDefault().getScannerInfoProvider(fCProject). + getScannerInformation(fCFile); + assertNotNull(scInfo); + String[] includes = scInfo.getIncludePaths(); + assertTrue(includes.length > 0); + Map symbols = scInfo.getDefinedSymbols(); + assertFalse(symbols.isEmpty()); + } + +}