From 1594835cc6dfad29e8407115beed4b0e3c6f83ba Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Wed, 3 May 2017 22:11:28 -0400 Subject: [PATCH] Bug 508637 - Encode the CDT version in the value of the __CDT_PARSER__ macro Change-Id: Ic8b571da6cf47297bb615843fab8a8d971c7c2a7 --- .../tests/scanner/PreprocessorBugsTests.java | 3 +- .../core/parser/scanner/CPreprocessor.java | 24 +++++++++++++-- .../src/org/eclipse/cdt/core/CCorePlugin.java | 29 +++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorBugsTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorBugsTests.java index 99290b5094e..8400776ef5b 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorBugsTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/PreprocessorBugsTests.java @@ -15,6 +15,7 @@ import junit.framework.TestSuite; import org.eclipse.cdt.core.parser.IProblem; import org.eclipse.cdt.core.parser.IToken; +import org.eclipse.cdt.internal.core.parser.scanner.CPreprocessor; /** @@ -89,7 +90,7 @@ public class PreprocessorBugsTests extends PreprocessorTestsBase { // __CDT_PARSER__ public void testPredefinedCDTMacro_Bug173848() throws Exception { initializeScanner(); - validateInteger("1"); + validateInteger(Integer.toString(CPreprocessor.getCDTVersion())); validateEOF(); validateProblemCount(0); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java index 6dfce181535..00fe6a9fb52 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java @@ -72,6 +72,7 @@ import org.eclipse.cdt.internal.core.parser.scanner.ScannerContext.CodeState; import org.eclipse.cdt.internal.core.parser.scanner.ScannerContext.Conditional; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; +import org.osgi.framework.Version; /** * C-Preprocessor providing tokens for the parsers. The class should not be used directly, @@ -95,7 +96,8 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable { private static final char[] ONE = "1".toCharArray(); //$NON-NLS-1$ // Standard built-ins - private static final ObjectStyleMacro __CDT_PARSER__= new ObjectStyleMacro("__CDT_PARSER__".toCharArray(), ONE); //$NON-NLS-1$ + private static final ObjectStyleMacro __CDT_PARSER__= new ObjectStyleMacro("__CDT_PARSER__".toCharArray(), //$NON-NLS-1$ + Integer.toString(getCDTVersion()).toCharArray()); private static final ObjectStyleMacro __cplusplus = new ObjectStyleMacro("__cplusplus".toCharArray(), "201103L".toCharArray()); //$NON-NLS-1$ //$NON-NLS-2$ private static final ObjectStyleMacro __STDC__ = new ObjectStyleMacro("__STDC__".toCharArray(), ONE); //$NON-NLS-1$ @@ -123,7 +125,25 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable { private static final String TRACE_NO_GUARD = CCorePlugin.PLUGIN_ID + "/debug/scanner/missingIncludeGuards"; //$NON-NLS-1$ - + /** + * Returns an integer, suitable for use as a macro value, representing the current + * version of the CDT feature, composited into a single number. + * For example, version 9.2.1 would be composited into 90201. + * Used as the value of the __CDT_PARSER__ macro. + */ + public static int getCDTVersion() { + Version version = CCorePlugin.getCDTFeatureVersion(); + if (version != null) { + int major = version.getMajor(); + int minor = version.getMinor(); + int micro = version.getMicro(); + int composite = major * 10000 + minor * 100 + micro; + return composite; + } + // Fall back to the old approach of defining __CDT_PARSER__ as 1. + return 1; + } + private final class MacroDictionary implements IMacroDictionary, ISignificantMacros.IVisitor { @Override public boolean satisfies(ISignificantMacros significantMacros) { diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CCorePlugin.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CCorePlugin.java index 4836460df67..6909c65f181 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CCorePlugin.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CCorePlugin.java @@ -75,6 +75,8 @@ import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IBundleGroup; +import org.eclipse.core.runtime.IBundleGroupProvider; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.ICoreRunnable; import org.eclipse.core.runtime.IExtension; @@ -94,6 +96,7 @@ import org.eclipse.core.runtime.jobs.Job; import org.eclipse.core.runtime.preferences.InstanceScope; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; +import org.osgi.framework.Version; import com.ibm.icu.text.MessageFormat; @@ -1617,4 +1620,30 @@ public class CCorePlugin extends Plugin { ServiceReference ref = context.getServiceReference(service); return ref != null ? context.getService(ref) : null; } + + private static final String CDT_FEATURE_ID = "org.eclipse.cdt"; //$NON-NLS-1$ + + /** + * Return the version of the CDT feature in this Eclipse installation, if any. + * + * Note that, while this is a method in CCorePlugin, it's not specific to the + * org.eclipse.cdt.core plugin; it returns the version of the entire + * org.eclipse.cdt feature. + * + * @since 6.3 + */ + public static Version getCDTFeatureVersion() { + IBundleGroupProvider[] providers = Platform.getBundleGroupProviders(); + if (providers != null) { + for (IBundleGroupProvider provider : providers) { + IBundleGroup[] bundleGroups = provider.getBundleGroups(); + for (IBundleGroup group : bundleGroups) { + if (group.getIdentifier().equals(CDT_FEATURE_ID)) { + return new Version(group.getVersion()); + } + } + } + } + return null; + } }