From 4d972701feea7221f78724ecde16c2b29251ad1d Mon Sep 17 00:00:00 2001 From: Hiroshi Yamauchi Date: Wed, 30 Jul 2025 14:10:53 -0700 Subject: [PATCH 1/2] Use llvm::popcount instead of __builtin_popcount. This fixes the windows build with MSVC. --- clang/unittests/Tooling/BoundsSafetyBringupMissingChecks.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/unittests/Tooling/BoundsSafetyBringupMissingChecks.cpp b/clang/unittests/Tooling/BoundsSafetyBringupMissingChecks.cpp index 7613f990b878d..f17f6bfdbb7e1 100644 --- a/clang/unittests/Tooling/BoundsSafetyBringupMissingChecks.cpp +++ b/clang/unittests/Tooling/BoundsSafetyBringupMissingChecks.cpp @@ -179,7 +179,7 @@ TEST(BoundsSafetyBringUpMissingChecks, ChkPairValidMask) { static_assert(LangOptions::BS_CHK_None == 0, "expected 0"); for (size_t Idx = 0; Idx < NumChkDescs; ++Idx) { unsigned CurrentMask = CheckKinds[Idx].Mask; - EXPECT_EQ(__builtin_popcount(CurrentMask), 1); // Check is a power of 2 + EXPECT_EQ(llvm::popcount(CurrentMask), 1); // Check is a power of 2 EXPECT_EQ(SeenBits & CurrentMask, 0U); // Doesn't overlap with a previously seen value SeenBits |= CurrentMask; From 752477861395eb8cfe7b2ae002661f279414ecd4 Mon Sep 17 00:00:00 2001 From: Hiroshi Yamauchi Date: Wed, 30 Jul 2025 15:16:52 -0700 Subject: [PATCH 2/2] Set the underlying type of enum BoundsSafetyNewChecks to unsigned. llvm::has_single_bit and llvm::countr_zero both require that the argument type is unsigned but the default underlying type for enums is int, which causes build errors with MSVC. This fixes the windows build with MSVC. ``` FAILED: tools/clang/lib/Driver/CMakeFiles/obj.clangDriver.dir/BoundsSafetyArgs.cpp.obj C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1444~1.352\bin\Hostx64\x64\cl.exe /nologo /TP -DCLANG_BUILD_STATIC -DEXPERIMENTAL_KEY_INSTRUCTIONS -DGTEST_HAS_RTTI=0 -DUNICODE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_HAS_EXCEPTIONS=0 -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__SHORT_FILE__=\"BoundsSafetyArgs.cpp\" -IC:\Users\hiroshi\cas\llvm-project\build\tools\clang\lib\Driver -IC:\Users\hiroshi\cas\llvm-project\clang\lib\Driver -IC:\Users\hiroshi\cas\llvm-project\clang\include -IC:\Users\hiroshi\cas\llvm-project\build\tools\clang\include -IC:\Users\hiroshi\cas\llvm-project\build\include -IC:\Users\hiroshi\cas\llvm-project\llvm\include /DWIN32 /D_WINDOWS /Zc:inline /Zc:preprocessor /Zc:__cplusplus /Oi /bigobj /permissive- /W4 -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd5105 -wd4324 -wd4251 -wd4275 -w14062 -we4238 /Gw /O2 /Ob2 /DNDEBUG -std:c++17 -MD /EHs-c- /GR- /showIncludes /Fotools\clang\lib\Driver\CMakeFiles\obj.clangDriver.dir\BoundsSafetyArgs.cpp.obj /Fdtools\clang\lib\Driver\CMakeFiles\obj.clangDriver.dir\ /FS -c C:\Users\hiroshi\cas\llvm-project\clang\lib\Driver\BoundsSafetyArgs.cpp C:\Users\hiroshi\cas\llvm-project\clang\lib\Driver\BoundsSafetyArgs.cpp(196): error C2672: 'llvm::has_single_bit': no matching overloaded function found C:\Users\hiroshi\cas\llvm-project\llvm\include\llvm/ADT/bit.h(147): note: could be 'bool llvm::has_single_bit(T) noexcept' C:\Users\hiroshi\cas\llvm-project\clang\lib\Driver\BoundsSafetyArgs.cpp(196): note: 'bool llvm::has_single_bit(T) noexcept': could not deduce template argument for '' C:\Users\hiroshi\cas\llvm-project\llvm\include\llvm/ADT/bit.h(146): note: 'std::enable_if_t' : Failed to specialize alias template ``` ``` FAILED: tools/clang/lib/Driver/CMakeFiles/obj.clangDriver.dir/BoundsSafetyArgs.cpp.obj C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1444~1.352\bin\Hostx64\x64\cl.exe /nologo /TP -DCLANG_BUILD_STATIC -DEXPERIMENTAL_KEY_INSTRUCTIONS -DGTEST_HAS_RTTI=0 -DUNICODE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_HAS_EXCEPTIONS=0 -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__SHORT_FILE__=\"BoundsSafetyArgs.cpp\" -IC:\Users\hiroshi\cas\llvm-project\build\tools\clang\lib\Driver -IC:\Users\hiroshi\cas\llvm-project\clang\lib\Driver -IC:\Users\hiroshi\cas\llvm-project\clang\include -IC:\Users\hiroshi\cas\llvm-project\build\tools\clang\include -IC:\Users\hiroshi\cas\llvm-project\build\include -IC:\Users\hiroshi\cas\llvm-project\llvm\include /DWIN32 /D_WINDOWS /Zc:inline /Zc:preprocessor /Zc:__cplusplus /Oi /bigobj /permissive- /W4 -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd5105 -wd4324 -wd4251 -wd4275 -w14062 -we4238 /Gw /O2 /Ob2 /DNDEBUG -std:c++17 -MD /EHs-c- /GR- /showIncludes /Fotools\clang\lib\Driver\CMakeFiles\obj.clangDriver.dir\BoundsSafetyArgs.cpp.obj /Fdtools\clang\lib\Driver\CMakeFiles\obj.clangDriver.dir\ /FS -c C:\Users\hiroshi\cas\llvm-project\clang\lib\Driver\BoundsSafetyArgs.cpp C:\Users\hiroshi\cas\llvm-project\llvm\include\llvm/ADT/bit.h(158): error C2338: static_assert failed: 'Only unsigned integral types are allowed.' C:\Users\hiroshi\cas\llvm-project\llvm\include\llvm/ADT/bit.h(158): note: the template instantiation context (the oldest one first) is ``` --- clang/include/clang/Basic/LangOptions.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h index 5f97f894c9164..086dde70296c3 100644 --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -458,7 +458,7 @@ class LangOptionsBase { }; /* TODO(BoundsSafety) Deprecate the flag */ - enum BoundsSafetyNewChecks { + enum BoundsSafetyNewChecks : unsigned { BS_CHK_None = 0, BS_CHK_AccessSize = 1 << 0, // rdar://72252593