Index: clang/lib/Driver/ToolChains/MSVC.cpp =================================================================== --- clang/lib/Driver/ToolChains/MSVC.cpp +++ clang/lib/Driver/ToolChains/MSVC.cpp @@ -1609,4 +1609,12 @@ if (DriverArgs.hasArg(options::OPT_fno_rtti, options::OPT_frtti, /*Default=*/false)) CC1Args.push_back("-D_HAS_STATIC_RTTI=0"); + + // Starting with VS 2022 17.1, MSVC predefines the below macro to inform + // users of the execution character set defined at compile time. + // The value given is the Windows Code Page Identifier: + // https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers + // + // Clang currently only supports UTF-8, so we'll use 65001 + CC1Args.push_back("-D_MSVC_EXECUTION_CHARACTER_SET=65001"); } Index: clang/test/Driver/cl-execution-character-set.c =================================================================== --- /dev/null +++ clang/test/Driver/cl-execution-character-set.c @@ -0,0 +1,14 @@ +// Note: %s must be preceded by --, otherwise it may be interpreted as a +// command-line option, e.g. on Mac where %s is commonly under /Users. + +// Only UTF-8 is currently supported, which has the Windows Code +// Page Identifier 65001 + +// Check this works with the clang-cl driver as well as with the GNU clang driver +// RUN: %clang_cl -### -- %s 2>&1 | FileCheck -check-prefix=CHECK-MSVC %s +// RUN: %clang --target=x86_64-pc-windows-msvc -### %s 2>&1 | FileCheck -check-prefix=CHECK-MSVC %s +// CHECK-MSVC: -D_MSVC_EXECUTION_CHARACTER_SET=65001 + +// It shouldn't be defined for any non MSVC targets +// RUN: %clang --target=x86_64-pc-windows-gnu -### %s 2>&1 | FileCheck -check-prefix=CHECK-NON-MSVC %s +// CHECK-NON-MSVC-NOT: -D_MSVC_EXECUTION_CHARACTER_SET