1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-30 21:55:31 +02:00

Bug 360919 - [MSVC] preprocessor symbol __STDC__ always defined

Move the __STDC__ macro to the scanner configuration extension, where we can
differentiate compiler type. Only add it when compiler type is not MSVC.
This will miss the case where MSVC is compiling in C mode and /Za is used,
then __STDC__ should be defined but this is a much less common case and would be
addressed likely outside scanner configuration.

See also
https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros

Change-Id: Icc5d3ef038fb468efe33802a04fc78fc1e5e583e
Signed-off-by: Marc-Andre Laperle <malaperle@gmail.com>
This commit is contained in:
Marc-Andre Laperle 2019-09-28 23:02:05 -04:00 committed by Marc-André Laperle
parent 232e3d7153
commit 4e16631f30
4 changed files with 31 additions and 3 deletions

View file

@ -27,11 +27,17 @@ import org.eclipse.cdt.core.parser.IScannerInfo;
* Configures the preprocessor for parsing c-sources as accepted by gcc.
*/
public class GCCScannerExtensionConfiguration extends GNUScannerExtensionConfiguration {
private static enum CompilerType {
GCC, MSVC
}
private static final int VERSION_4_2 = version(4, 2);
private static final int VERSION_4_7 = version(4, 7);
private static GCCScannerExtensionConfiguration CONFIG = new GCCScannerExtensionConfiguration();
private static GCCScannerExtensionConfiguration CONFIG_4_2 = new GCCScannerExtensionConfiguration(VERSION_4_2);
private static GCCScannerExtensionConfiguration CONFIG_4_7 = new GCCScannerExtensionConfiguration(VERSION_4_7);
private static GCCScannerExtensionConfiguration CONFIG_MSVC = new GCCScannerExtensionConfiguration(
CompilerType.MSVC, 0 /* version is ignored for now */);
/**
* @since 5.1
@ -47,6 +53,12 @@ public class GCCScannerExtensionConfiguration extends GNUScannerExtensionConfigu
if (info != null) {
try {
final Map<String, String> definedSymbols = info.getDefinedSymbols();
String mscVer = definedSymbols.get("_MSC_VER"); //$NON-NLS-1$
if (mscVer != null && Integer.valueOf(mscVer) > 0) {
return CONFIG_MSVC;
}
int major = Integer.valueOf(definedSymbols.get("__GNUC__")); //$NON-NLS-1$
int minor = Integer.valueOf(definedSymbols.get("__GNUC_MINOR__")); //$NON-NLS-1$
int version = version(major, minor);
@ -70,11 +82,23 @@ public class GCCScannerExtensionConfiguration extends GNUScannerExtensionConfigu
/**
* @since 5.5
*/
@SuppressWarnings("nls")
public GCCScannerExtensionConfiguration(int version) {
this(CompilerType.GCC, version);
}
/**
* @since 6.9
*/
@SuppressWarnings("nls")
public GCCScannerExtensionConfiguration(CompilerType compiler, int version) {
addMacro("__null", "(void *)0");
addMacro("__builtin_offsetof(T,m)", "((size_t) &((T *)0)->m)");
if (compiler != CompilerType.MSVC) {
// MSVC only defines this when compiling in C mode and /Za is used.
addMacro("__STDC__", "1");
}
if (version >= VERSION_4_2) {
addKeyword(GCCKeywords.cp_decimal32, IGCCToken.t_decimal32);
addKeyword(GCCKeywords.cp_decimal64, IGCCToken.t_decimal64);

View file

@ -127,6 +127,11 @@ public class GPPScannerExtensionConfiguration extends GNUScannerExtensionConfigu
addKeyword(Keywords.c_COMPLEX, IToken.t__Complex);
addKeyword(Keywords.c_IMAGINARY, IToken.t__Imaginary);
if (compiler != CompilerType.MSVC) {
// MSVC only defines this when compiling in C mode and /Za is used.
addMacro("__STDC__", "1");
}
if (compiler == CompilerType.GCC) {
if (version >= VERSION_4_2) {
addKeyword(GCCKeywords.cp_decimal32, IGCCToken.t_decimal32);

View file

@ -103,7 +103,6 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
Integer.toString(getCDTVersion()).toCharArray());
private static final ObjectStyleMacro __cplusplus = new ObjectStyleMacro("__cplusplus".toCharArray(), //$NON-NLS-1$
"201103L".toCharArray()); //$NON-NLS-1$
private static final ObjectStyleMacro __STDC__ = new ObjectStyleMacro("__STDC__".toCharArray(), ONE); //$NON-NLS-1$
private static final ObjectStyleMacro __STDC_HOSTED__ = new ObjectStyleMacro("__STDC_HOSTED__".toCharArray(), ONE); //$NON-NLS-1$
private static final ObjectStyleMacro __STDC_VERSION__ = new ObjectStyleMacro("__STDC_VERSION__".toCharArray(), //$NON-NLS-1$
"199901L".toCharArray()); //$NON-NLS-1$
@ -507,7 +506,6 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
private void setupMacroDictionary(IScannerExtensionConfiguration config, IScannerInfo info, ParserLanguage lang) {
// Built-in macros
fMacroDictionary.put(__CDT_PARSER__.getNameCharArray(), __CDT_PARSER__);
fMacroDictionary.put(__STDC__.getNameCharArray(), __STDC__);
fMacroDictionary.put(__FILE__.getNameCharArray(), __FILE__);
fMacroDictionary.put(__DATE__.getNameCharArray(), __DATE__);
fMacroDictionary.put(__TIME__.getNameCharArray(), __TIME__);

View file

@ -24,6 +24,7 @@ import org.eclipse.cdt.core.dom.parser.AbstractScannerExtensionConfiguration;
public class ScannerExtensionConfiguration extends AbstractScannerExtensionConfiguration {
private ScannerExtensionConfiguration() {
addMacro("__STDC__", "1");
}
public static ScannerExtensionConfiguration createC() {