From c161d18085a3703cc355c36c541950be0f337923 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Sat, 10 Aug 2024 06:09:01 -0700 Subject: [PATCH 1/3] Build the STL, benchmarks, and validator with the conformant preprocessor. I've verified that the compiler option is active in each directory. --- benchmarks/CMakeLists.txt | 2 +- stl/CMakeLists.txt | 2 +- tools/validate/CMakeLists.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index 32c6e1925f..6b71cb76f2 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -62,7 +62,7 @@ endif() set(CMAKE_BUILD_TYPE RelWithDebInfo) # /utf-8 affects . -add_compile_options("$<$:/diagnostics:caret;/W4;/WX;/w14265;/w15038;/w15262;/utf-8>") +add_compile_options("$<$:/diagnostics:caret;/W4;/WX;/w14265;/w15038;/w15262;/utf-8;/Zc:preprocessor>") if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/google-benchmark/.git") message(FATAL_ERROR "google-benchmark is not checked out; make sure to run\n git submodule update --init benchmarks/google-benchmark") diff --git a/stl/CMakeLists.txt b/stl/CMakeLists.txt index 376f4ef9cd..0cbcb75f1d 100644 --- a/stl/CMakeLists.txt +++ b/stl/CMakeLists.txt @@ -421,7 +421,7 @@ add_compile_definitions(_CRTBLD _VCRT_ALLOW_INTERNALS _HAS_OLD_IOSTREAMS_MEMBERS set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "Embedded") add_compile_options(/WX /Gy - "$<$:/diagnostics:caret;/W4;/w14265;/w15038;/fastfail;/guard:cf;/Zp8;/std:c++latest;/permissive-;/Zc:threadSafeInit-;/Zl>" + "$<$:/diagnostics:caret;/W4;/w14265;/w15038;/fastfail;/guard:cf;/Zp8;/std:c++latest;/permissive-;/Zc:preprocessor;/Zc:threadSafeInit-;/Zl>" "$<$:/W3;/nologo;/quiet>" ) diff --git a/tools/validate/CMakeLists.txt b/tools/validate/CMakeLists.txt index 2be5cc5fb3..17eae8077b 100644 --- a/tools/validate/CMakeLists.txt +++ b/tools/validate/CMakeLists.txt @@ -6,7 +6,7 @@ project(msvc_standard_libraries_validate LANGUAGES CXX) add_executable(validate-binary validate.cpp) # we use SAL annotations, so pass /analyze -target_compile_options(validate-binary PRIVATE /W4 /WX /analyze) +target_compile_options(validate-binary PRIVATE /W4 /WX /analyze /Zc:preprocessor) set_target_properties(validate-binary PROPERTIES CXX_STANDARD 23 From 7cb36761bdfadefe9c062a9365ddcd5452354c46 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Sat, 10 Aug 2024 06:40:16 -0700 Subject: [PATCH 2/3] Drop "we use SAL annotations" comment. This was added by GH 2671 on 2022-05-01, then `_Printf_format_string_` was removed by GH 3919 on 2023-08-03. We can keep using `/analyze`, though. It adds ~4.5 seconds on my machine, but it's not part of the dev inner loop, and Code Format Validation isn't on the critical path (the Early Builds take longer). --- tools/validate/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/validate/CMakeLists.txt b/tools/validate/CMakeLists.txt index 17eae8077b..5d2d5f34d7 100644 --- a/tools/validate/CMakeLists.txt +++ b/tools/validate/CMakeLists.txt @@ -5,7 +5,6 @@ cmake_minimum_required(VERSION 3.29.0) project(msvc_standard_libraries_validate LANGUAGES CXX) add_executable(validate-binary validate.cpp) -# we use SAL annotations, so pass /analyze target_compile_options(validate-binary PRIVATE /W4 /WX /analyze /Zc:preprocessor) set_target_properties(validate-binary PROPERTIES From 9a2cd3b777a018490c4757958f43bc7630c6da8c Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Sat, 10 Aug 2024 06:54:38 -0700 Subject: [PATCH 3/3] Use `_wfopen_s()` instead of defining `_CRT_SECURE_NO_WARNINGS`. Also avoid crashing in `~BinaryFile()` if the file wasn't successfully opened. --- tools/validate/validate.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tools/validate/validate.cpp b/tools/validate/validate.cpp index 37e4dc78e6..0a7936b50a 100644 --- a/tools/validate/validate.cpp +++ b/tools/validate/validate.cpp @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#define _CRT_SECURE_NO_WARNINGS #include #include #include @@ -21,8 +20,10 @@ constexpr size_t max_line_length = 120; class BinaryFile { public: - explicit BinaryFile(const filesystem::path& filepath) : m_file(_wfopen(filepath.c_str(), L"rb")) { - if (!m_file) { + explicit BinaryFile(const filesystem::path& filepath) { + const auto err = _wfopen_s(&m_file, filepath.c_str(), L"rb"); + + if (err != 0 || !m_file) { println(stderr, "Validation failed: {} couldn't be opened.", filepath.string()); } } @@ -40,7 +41,7 @@ class BinaryFile { } ~BinaryFile() { - if (fclose(m_file) != 0) { + if (m_file && fclose(m_file) != 0) { println(stderr, "fclose() failed."); abort(); }