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>
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/c/GCCScannerExtensionConfiguration.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/c/GCCScannerExtensionConfiguration.java
index 7a2e0b6..68693ca 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/c/GCCScannerExtensionConfiguration.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/c/GCCScannerExtensionConfiguration.java
@@ -27,11 +27,17 @@
  * 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 @@
 		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 @@
 	/**
 	 * @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);
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/cpp/GPPScannerExtensionConfiguration.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/cpp/GPPScannerExtensionConfiguration.java
index fa83f02..c3dc69c 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/cpp/GPPScannerExtensionConfiguration.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/cpp/GPPScannerExtensionConfiguration.java
@@ -127,6 +127,11 @@
 		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);
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 4b82a26..2f06777 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
@@ -103,7 +103,6 @@
 			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 @@
 	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__);
diff --git a/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/ScannerExtensionConfiguration.java b/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/ScannerExtensionConfiguration.java
index 29d8c40..f7d2b07 100644
--- a/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/ScannerExtensionConfiguration.java
+++ b/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/ScannerExtensionConfiguration.java
@@ -24,6 +24,7 @@
 public class ScannerExtensionConfiguration extends AbstractScannerExtensionConfiguration {
 
 	private ScannerExtensionConfiguration() {
+		addMacro("__STDC__", "1");
 	}
 
 	public static ScannerExtensionConfiguration createC() {