Author: Henry Baba-Weiss
Date: 2026-03-04T11:35:34+01:00
New Revision: 6bdf076137d0cd7f4490522751c0e07efdd4d5e4

URL: 
https://github.com/llvm/llvm-project/commit/6bdf076137d0cd7f4490522751c0e07efdd4d5e4
DIFF: 
https://github.com/llvm/llvm-project/commit/6bdf076137d0cd7f4490522751c0e07efdd4d5e4.diff

LOG: [clang] Predefine `_MSVC_TRADITIONAL` in MSVC compatibility mode (#184278)

As of version 19.15 (Visual Studio 2017 version 15.8), MSVC predefines
the `_MSVC_TRADITIONAL` macro to indicate whether it is using the old
"traditional" preprocessor or the new standards-conforming preprocessor.
Clang now predefines `_MSVC_TRADITIONAL` as 1 when emulating MSVC 19.15
or later, since Clang supports most traditional preprocessor behaviors
(e.g. `/##/` turning into `//`) when running in MSVC compatibility mode.

Currently there isn't a situation where it makes sense for Clang to
report `_MSVC_TRADITIONAL` as 0, since MSVC compatibility mode only
attempts to be compatible with the traditional MSVC preprocessor.
However, this does mean that clang-cl cannot match MSVC's behavior of
implicitly enabling the conforming C preprocessor when compiling with
`/std:c11`, `/std:c17`, or `/std:clatest`.

Fixes #47114

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/include/clang/Basic/LangOptions.h
    clang/lib/Basic/Targets/OSTargets.cpp
    clang/test/Preprocessor/predefined-win-macros.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f2ea45789fd27..047cc8455628f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -382,6 +382,9 @@ Android Support
 Windows Support
 ^^^^^^^^^^^^^^^
 
+- Clang now defines the ``_MSVC_TRADITIONAL`` macro as ``1`` when emulating 
MSVC
+  19.15 (Visual Studio 2017 version 15.8) and later. (#GH47114)
+
 LoongArch Support
 ^^^^^^^^^^^^^^^^^
 

diff  --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/include/clang/Basic/LangOptions.h
index 79ddeebaa7fba..64ec0a87089f9 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -177,6 +177,7 @@ class LangOptionsBase {
     MSVC2017 = 1910,
     MSVC2017_5 = 1912,
     MSVC2017_7 = 1914,
+    MSVC2017_8 = 1915,
     MSVC2019 = 1920,
     MSVC2019_5 = 1925,
     MSVC2019_8 = 1928,

diff  --git a/clang/lib/Basic/Targets/OSTargets.cpp 
b/clang/lib/Basic/Targets/OSTargets.cpp
index e99bbd159929c..f847cea5f5bec 100644
--- a/clang/lib/Basic/Targets/OSTargets.cpp
+++ b/clang/lib/Basic/Targets/OSTargets.cpp
@@ -273,6 +273,13 @@ static void addVisualCDefines(const LangOptions &Opts, 
MacroBuilder &Builder) {
   //
   // Clang currently only supports UTF-8, so we'll use 65001
   Builder.defineMacro("_MSVC_EXECUTION_CHARACTER_SET", "65001");
+
+  // As of version 19.15 (VS 2017 15.8), MSVC predefines this macro to indicate
+  // whether the traditional or standards-conforming preprocessor is in use.
+  // Currently, MSVC compatibility mode only attempts to be compatible with the
+  // traditional preprocessor.
+  if (Opts.isCompatibleWithMSVC(LangOptions::MSVC2017_8))
+    Builder.defineMacro("_MSVC_TRADITIONAL", "1");
 }
 
 void addWindowsDefines(const llvm::Triple &Triple, const LangOptions &Opts,

diff  --git a/clang/test/Preprocessor/predefined-win-macros.c 
b/clang/test/Preprocessor/predefined-win-macros.c
index 86708e02e8dc0..872a872928f89 100644
--- a/clang/test/Preprocessor/predefined-win-macros.c
+++ b/clang/test/Preprocessor/predefined-win-macros.c
@@ -63,6 +63,21 @@
 // CHECK-MS-CPP2C: #define _MSC_VER 1900
 // CHECK-MS-CPP2C: #define _MSVC_LANG 202400L
 
+// RUN: %clang_cc1 %s -E -dM -triple i686-pc-win32 -fms-extensions 
-fms-compatibility \
+// RUN:     -fms-compatibility-version=19.14 -o - | FileCheck 
-match-full-lines %s --check-prefix=CHECK-MSVC-1914
+// CHECK-MSVC-1914: #define _MSC_VER 1914
+// CHECK-MSVC-1914-NOT: #define _MSVC_TRADITIONAL 1
+
+// RUN: %clang_cc1 %s -E -dM -triple i686-pc-win32 -fms-extensions 
-fms-compatibility \
+// RUN:     -fms-compatibility-version=19.15 -o - | FileCheck 
-match-full-lines %s --check-prefix=CHECK-MSVC-1915
+// CHECK-MSVC-1915: #define _MSC_VER 1915
+// CHECK-MSVC-1915: #define _MSVC_TRADITIONAL 1
+
+// RUN: %clang_cc1 %s -E -dM -triple i686-pc-win32 -fms-extensions 
-fms-compatibility \
+// RUN:     -fms-compatibility-version=19.23 -o - | FileCheck 
-match-full-lines %s --check-prefix=CHECK-MSVC-1923
+// CHECK-MSVC-1923: #define _MSC_VER 1923
+// CHECK-MSVC-1923: #define _MSVC_TRADITIONAL 1
+
 // RUN: %clang_cc1 -triple i386-windows %s -E -dM -o - \
 // RUN:   | FileCheck -match-full-lines %s --check-prefix=CHECK-X86-WIN
 


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to