mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-23 17:05:26 +02:00
Bug 508637 - Encode the CDT version in the value of the __CDT_PARSER__ macro
Change-Id: Ic8b571da6cf47297bb615843fab8a8d971c7c2a7
This commit is contained in:
parent
2f3dbb123a
commit
1594835cc6
3 changed files with 53 additions and 3 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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<T> 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue