Skip to content

Commit 6ff78e4

Browse files
authored
Add a mechanism to suppress std::source_location (#1260)
1 parent a213190 commit 6ff78e4

File tree

13 files changed

+494
-11
lines changed

13 files changed

+494
-11
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ jobs:
9696
compiler: [MSVC, clang-cl]
9797
arch: [x86, x64, arm64]
9898
config: [Debug, Release]
99-
test_exe: [test, test_cpp20, test_win7, test_fast, test_slow, test_old, test_module_lock_custom, test_module_lock_none]
99+
test_exe: [test, test_cpp20, test_cpp20_no_sourcelocation, test_win7, test_fast, test_slow, test_old, test_module_lock_custom, test_module_lock_none]
100100
exclude:
101101
- arch: arm64
102102
config: Debug
@@ -294,7 +294,7 @@ jobs:
294294
- name: Build tests
295295
run: |
296296
cd build
297-
cmake --build . -j2 --target test-vanilla test_cpp20 test_win7 test_old
297+
cmake --build . -j2 --target test-vanilla test_cpp20 test_cpp20_no_sourcelocation test_win7 test_old
298298
299299
- name: Upload test binaries
300300
uses: actions/upload-artifact@v3
@@ -355,7 +355,7 @@ jobs:
355355
- name: Build tests
356356
run: |
357357
cd build
358-
cmake --build . -j2 --target test-vanilla test_cpp20 test_win7 test_old
358+
cmake --build . -j2 --target test-vanilla test_cpp20 test_cpp20_no_sourcelocation test_win7 test_old
359359
360360
- name: Run tests
361361
run: |

build_test_all.cmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ call msbuild /p:Configuration=%target_configuration%,Platform=%target_platform%,
3232

3333
call msbuild /m /p:Configuration=%target_configuration%,Platform=%target_platform%,CppWinRTBuildVersion=%target_version% cppwinrt.sln /t:test\test
3434
call msbuild /m /p:Configuration=%target_configuration%,Platform=%target_platform%,CppWinRTBuildVersion=%target_version% cppwinrt.sln /t:test\test_cpp20
35+
call msbuild /m /p:Configuration=%target_configuration%,Platform=%target_platform%,CppWinRTBuildVersion=%target_version% cppwinrt.sln /t:test\test_cpp20_no_sourcelocation
3536
call msbuild /m /p:Configuration=%target_configuration%,Platform=%target_platform%,CppWinRTBuildVersion=%target_version% cppwinrt.sln /t:test\test_win7
3637
call msbuild /m /p:Configuration=%target_configuration%,Platform=%target_platform%,CppWinRTBuildVersion=%target_version% cppwinrt.sln /t:test\test_fast
3738
call msbuild /m /p:Configuration=%target_configuration%,Platform=%target_platform%,CppWinRTBuildVersion=%target_version% cppwinrt.sln /t:test\test_slow
3839
call msbuild /m /p:Configuration=%target_configuration%,Platform=%target_platform%,CppWinRTBuildVersion=%target_version% cppwinrt.sln /t:test\test_module_lock_custom
3940
call msbuild /m /p:Configuration=%target_configuration%,Platform=%target_platform%,CppWinRTBuildVersion=%target_version% cppwinrt.sln /t:test\test_module_lock_none
41+
call msbuild /m /p:Configuration=%target_configuration%,Platform=%target_platform%,CppWinRTBuildVersion=%target_version% cppwinrt.sln /t:test\test_module_lock_none
4042
call msbuild /m /p:Configuration=%target_configuration%,Platform=%target_platform%,CppWinRTBuildVersion=%target_version% cppwinrt.sln /t:test\old_tests\test_old
4143

4244
call run_tests.cmd %target_platform% %target_configuration%

cppwinrt.sln

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_component", "test\test
3434
EndProject
3535
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "test\test\test.vcxproj", "{D2961EA1-A8CA-4A62-B760-948403DC8494}"
3636
ProjectSection(ProjectDependencies) = postProject
37+
{A91B8BF3-28E4-4D9E-8DBA-64B70E4F0270} = {A91B8BF3-28E4-4D9E-8DBA-64B70E4F0270}
3738
{D613FB39-5035-4043-91E2-BAB323908AF4} = {D613FB39-5035-4043-91E2-BAB323908AF4}
3839
{F1C915B3-2C64-4992-AFB7-7F035B1A7607} = {F1C915B3-2C64-4992-AFB7-7F035B1A7607}
39-
{A91B8BF3-28E4-4D9E-8DBA-64B70E4F0270} = {A91B8BF3-28E4-4D9E-8DBA-64B70E4F0270}
4040
EndProjectSection
4141
EndProject
4242
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_component_folders", "test\test_component_folders\test_component_folders.vcxproj", "{85695954-3800-4558-9857-966E69E9F9EC}"
@@ -76,8 +76,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_slow", "test\test_slow
7676
EndProject
7777
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_fast_fwd", "test\test_fast_fwd\test_fast_fwd.vcxproj", "{303CC0FE-7D66-4F9F-B7A1-0AF7F9359074}"
7878
ProjectSection(ProjectDependencies) = postProject
79-
{D613FB39-5035-4043-91E2-BAB323908AF4} = {D613FB39-5035-4043-91E2-BAB323908AF4}
8079
{0E0ACA62-A92F-44CF-BD41-AEB541946DF8} = {0E0ACA62-A92F-44CF-BD41-AEB541946DF8}
80+
{D613FB39-5035-4043-91E2-BAB323908AF4} = {D613FB39-5035-4043-91E2-BAB323908AF4}
8181
EndProjectSection
8282
EndProject
8383
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fast_fwd", "fast_fwd\fast_fwd.vcxproj", "{A63B3AD1-AB7B-461E-9FFF-2447F5BCD459}"
@@ -86,9 +86,9 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "scratch", "scratch\scratch.
8686
EndProject
8787
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_module_lock_none", "test\test_module_lock_none\test_module_lock_none.vcxproj", "{D48A96C2-8512-4CC3-B6E4-7CFF07ED8ED3}"
8888
ProjectSection(ProjectDependencies) = postProject
89-
{D613FB39-5035-4043-91E2-BAB323908AF4} = {D613FB39-5035-4043-91E2-BAB323908AF4}
90-
{13333A6F-6A4A-48CD-865C-0F65135EB018} = {13333A6F-6A4A-48CD-865C-0F65135EB018}
9189
{0080F6D1-AEC3-4F89-ADE1-3D22A7EBF99E} = {0080F6D1-AEC3-4F89-ADE1-3D22A7EBF99E}
90+
{13333A6F-6A4A-48CD-865C-0F65135EB018} = {13333A6F-6A4A-48CD-865C-0F65135EB018}
91+
{D613FB39-5035-4043-91E2-BAB323908AF4} = {D613FB39-5035-4043-91E2-BAB323908AF4}
9292
EndProjectSection
9393
EndProject
9494
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_module_lock_custom", "test\test_module_lock_custom\test_module_lock_custom.vcxproj", "{08C40663-B6A3-481E-8755-AE32BAD99501}"
@@ -100,16 +100,21 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{3C7EA5F8-6
100100
EndProject
101101
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_win7", "test\test_win7\test_win7.vcxproj", "{2EF696B9-7F4A-410F-AE5C-5301565C0F08}"
102102
ProjectSection(ProjectDependencies) = postProject
103+
{A91B8BF3-28E4-4D9E-8DBA-64B70E4F0270} = {A91B8BF3-28E4-4D9E-8DBA-64B70E4F0270}
103104
{D613FB39-5035-4043-91E2-BAB323908AF4} = {D613FB39-5035-4043-91E2-BAB323908AF4}
104105
{F1C915B3-2C64-4992-AFB7-7F035B1A7607} = {F1C915B3-2C64-4992-AFB7-7F035B1A7607}
105-
{A91B8BF3-28E4-4D9E-8DBA-64B70E4F0270} = {A91B8BF3-28E4-4D9E-8DBA-64B70E4F0270}
106106
EndProjectSection
107107
EndProject
108108
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_cpp20", "test\test_cpp20\test_cpp20.vcxproj", "{5FF6CD6C-515A-4D55-97B6-62AD9BCB77EA}"
109109
ProjectSection(ProjectDependencies) = postProject
110110
{D613FB39-5035-4043-91E2-BAB323908AF4} = {D613FB39-5035-4043-91E2-BAB323908AF4}
111111
EndProjectSection
112112
EndProject
113+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_cpp20_no_sourcelocation", "test\test_cpp20_no_sourcelocation\test_cpp20_no_sourcelocation.vcxproj", "{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}"
114+
ProjectSection(ProjectDependencies) = postProject
115+
{D613FB39-5035-4043-91E2-BAB323908AF4} = {D613FB39-5035-4043-91E2-BAB323908AF4}
116+
EndProjectSection
117+
EndProject
113118
Global
114119
GlobalSection(SolutionConfigurationPlatforms) = preSolution
115120
Debug|ARM = Debug|ARM
@@ -458,6 +463,22 @@ Global
458463
{5FF6CD6C-515A-4D55-97B6-62AD9BCB77EA}.Release|x64.Build.0 = Release|x64
459464
{5FF6CD6C-515A-4D55-97B6-62AD9BCB77EA}.Release|x86.ActiveCfg = Release|Win32
460465
{5FF6CD6C-515A-4D55-97B6-62AD9BCB77EA}.Release|x86.Build.0 = Release|Win32
466+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Debug|ARM.ActiveCfg = Debug|ARM
467+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Debug|ARM.Build.0 = Debug|ARM
468+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Debug|ARM64.ActiveCfg = Debug|ARM64
469+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Debug|ARM64.Build.0 = Debug|ARM64
470+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Debug|x64.ActiveCfg = Debug|x64
471+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Debug|x64.Build.0 = Debug|x64
472+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Debug|x86.ActiveCfg = Debug|Win32
473+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Debug|x86.Build.0 = Debug|Win32
474+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Release|ARM.ActiveCfg = Release|ARM
475+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Release|ARM.Build.0 = Release|ARM
476+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Release|ARM64.ActiveCfg = Release|ARM64
477+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Release|ARM64.Build.0 = Release|ARM64
478+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Release|x64.ActiveCfg = Release|x64
479+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Release|x64.Build.0 = Release|x64
480+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Release|x86.ActiveCfg = Release|Win32
481+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374}.Release|x86.Build.0 = Release|Win32
461482
EndGlobalSection
462483
GlobalSection(SolutionProperties) = preSolution
463484
HideSolutionNode = FALSE
@@ -481,6 +502,7 @@ Global
481502
{08C40663-B6A3-481E-8755-AE32BAD99501} = {3C7EA5F8-6E8C-4376-B499-2CAF596384B0}
482503
{2EF696B9-7F4A-410F-AE5C-5301565C0F08} = {3C7EA5F8-6E8C-4376-B499-2CAF596384B0}
483504
{5FF6CD6C-515A-4D55-97B6-62AD9BCB77EA} = {3C7EA5F8-6E8C-4376-B499-2CAF596384B0}
505+
{D4C8F881-84D5-4A7B-8BDE-AB4E34A05374} = {3C7EA5F8-6E8C-4376-B499-2CAF596384B0}
484506
EndGlobalSection
485507
GlobalSection(ExtensibilityGlobals) = postSolution
486508
SolutionGuid = {2783B8FD-EA3B-4D6B-9F81-662D289E02AA}

run_tests.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ if "%target_configuration%"=="" set target_configuration=Debug
1010

1111
call :run_test test
1212
call :run_test test_cpp20
13+
call :run_test test_cpp20_no_sourcelocation
1314
call :run_test test_win7
1415
call :run_test test_fast
1516
call :run_test test_slow

strings/base_error.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ WINRT_EXPORT namespace winrt
319319
// information is available on the caller who generated the error.
320320
if (winrt_throw_hresult_handler)
321321
{
322-
#ifdef __cpp_lib_source_location
322+
#ifdef WINRT_SOURCE_LOCATION_ACTIVE
323323
winrt_throw_hresult_handler(sourceInformation.line(), sourceInformation.file_name(), sourceInformation.function_name(), WINRT_IMPL_RETURNADDRESS(), code);
324324
#else
325325
winrt_throw_hresult_handler(0, nullptr, nullptr, WINRT_IMPL_RETURNADDRESS(), code);
@@ -448,7 +448,7 @@ WINRT_EXPORT namespace winrt
448448
{
449449
if (winrt_throw_hresult_handler)
450450
{
451-
#ifdef __cpp_lib_source_location
451+
#ifdef WINRT_SOURCE_LOCATION_ACTIVE
452452
winrt_throw_hresult_handler(sourceInformation.line(), sourceInformation.file_name(), sourceInformation.function_name(), WINRT_IMPL_RETURNADDRESS(), result);
453453
#else
454454
winrt_throw_hresult_handler(0, nullptr, nullptr, WINRT_IMPL_RETURNADDRESS(), result);

strings/base_macros.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,19 @@ typedef struct _GUID GUID;
8181
// to be the calling code, not cppwinrt itself, so that it is useful to developers building on top of this library. As a
8282
// result any public-facing method that can result in an error needs a default-constructed source_location argument. Because
8383
// this type does not exist in C++17 we need to use a macro to optionally add parameters and forwarding wherever appropriate.
84-
#ifdef __cpp_lib_source_location
84+
//
85+
// Some projects may decide to disable std::source_location support to prevent source code information from ending up in their
86+
// release binaries, or to reduce binary size. Defining WINRT_NO_SOURCE_LOCATION will prevent this feature from activating.
87+
#if defined(__cpp_lib_source_location) && !defined(WINRT_NO_SOURCE_LOCATION)
8588
#define WINRT_IMPL_SOURCE_LOCATION_ARGS_NO_DEFAULT , std::source_location const& sourceInformation
8689
#define WINRT_IMPL_SOURCE_LOCATION_ARGS , std::source_location const& sourceInformation = std::source_location::current()
8790
#define WINRT_IMPL_SOURCE_LOCATION_ARGS_SINGLE_PARAM std::source_location const& sourceInformation = std::source_location::current()
8891

8992
#define WINRT_IMPL_SOURCE_LOCATION_FORWARD , sourceInformation
9093
#define WINRT_IMPL_SOURCE_LOCATION_FORWARD_SINGLE_PARAM sourceInformation
9194

95+
#define WINRT_SOURCE_LOCATION_ACTIVE
96+
9297
#ifdef _MSC_VER
9398
#pragma detect_mismatch("WINRT_SOURCE_LOCATION", "true")
9499
#endif

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ set(SKIP_LARGE_PCH FALSE CACHE BOOL "Skip building large precompiled headers.")
106106

107107
add_subdirectory(test)
108108
add_subdirectory(test_cpp20)
109+
add_subdirectory(test_cpp20_no_sourcelocation)
109110
add_subdirectory(test_win7)
110111

111112
if(HAS_WINDOWSNUMERICS)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
set(CMAKE_CXX_STANDARD 20)
2+
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
3+
# std::format, std::ranges::is_heap, std::views::reverse, std::ranges::max
4+
# are experimental in libc++ as of Clang 15.
5+
# FIXME: Should probably use compile test instead?
6+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexperimental-library")
7+
endif()
8+
9+
if(ENABLE_TEST_SANITIZERS)
10+
# As of LLVM 15, custom_error.cpp doesn't build with ASAN due to:
11+
# error: cannot make section .ASAN$GL associative with sectionless symbol _ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE4nposE
12+
set_source_files_properties(custom_error.cpp PROPERTIES COMPILE_OPTIONS "-fno-sanitize=address")
13+
set_source_files_properties(custom_error.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS true)
14+
endif()
15+
16+
file(GLOB TEST_SRCS
17+
LIST_DIRECTORIES false
18+
CONFIGURE_DEPENDS
19+
*.cpp
20+
)
21+
list(FILTER TEST_SRCS EXCLUDE REGEX "/(main|pch)\\.cpp")
22+
23+
24+
list(APPEND BROKEN_TESTS
25+
# No broken tests.
26+
)
27+
28+
# Exclude broken tests
29+
foreach(TEST_SRCS_EXCLUDE_ITEM IN LISTS BROKEN_TESTS)
30+
list(FILTER TEST_SRCS EXCLUDE REGEX "/${TEST_SRCS_EXCLUDE_ITEM}\\.cpp")
31+
endforeach()
32+
33+
add_executable(test_cpp20_no_sourcelocation main.cpp ${TEST_SRCS})
34+
35+
target_compile_definitions(test_cpp20_no_sourcelocation PRIVATE WINRT_NO_SOURCE_LOCATION)
36+
37+
target_precompile_headers(test_cpp20_no_sourcelocation PRIVATE pch.h)
38+
set_source_files_properties(
39+
main.cpp
40+
PROPERTIES SKIP_PRECOMPILE_HEADERS true
41+
)
42+
43+
add_dependencies(test_cpp20_no_sourcelocation build-cppwinrt-projection)
44+
45+
add_test(
46+
NAME test_cpp20_no_sourcelocation
47+
COMMAND "$<TARGET_FILE:test_cpp20_no_sourcelocation>" ${TEST_COLOR_ARG}
48+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "pch.h"
2+
3+
using namespace winrt;
4+
using namespace Windows::Foundation;
5+
6+
namespace
7+
{
8+
static bool s_loggerCalled = false;
9+
10+
// Note that we are checking that the source line number matches expectations. If lines above this are changed
11+
// then this value needs to change as well.
12+
void FailOnLine15()
13+
{
14+
// Validate that handler translated exception
15+
REQUIRE_THROWS_AS(check_hresult(0x80000018), hresult_illegal_delegate_assignment);
16+
}
17+
18+
static struct {
19+
uint32_t lineNumber;
20+
char const* fileName;
21+
char const* functionName;
22+
void* returnAddress;
23+
winrt::hresult result;
24+
} s_loggerArgs{};
25+
26+
void __stdcall logger(uint32_t lineNumber, char const* fileName, char const* functionName, void* returnAddress, winrt::hresult const result) noexcept
27+
{
28+
s_loggerArgs = {
29+
.lineNumber = lineNumber,
30+
.fileName = fileName,
31+
.functionName = functionName,
32+
.returnAddress = returnAddress,
33+
.result = result,
34+
};
35+
s_loggerCalled = true;
36+
}
37+
}
38+
39+
TEST_CASE("custom_error_logger")
40+
{
41+
// Set up global handler
42+
REQUIRE(!s_loggerCalled);
43+
REQUIRE(!winrt_throw_hresult_handler);
44+
winrt_throw_hresult_handler = logger;
45+
46+
FailOnLine15();
47+
REQUIRE(s_loggerCalled);
48+
// In C++20 these fields should be filled in by std::source_location. However, this binary has
49+
// specified WINRT_NO_SOURCE_LOCATION so that support should be removed. As a result these should
50+
// return the same (empty) values as C++17.
51+
REQUIRE(s_loggerArgs.lineNumber == 0);
52+
REQUIRE(s_loggerArgs.fileName == nullptr);
53+
REQUIRE(s_loggerArgs.functionName == nullptr);
54+
55+
REQUIRE(s_loggerArgs.returnAddress);
56+
REQUIRE(s_loggerArgs.result == 0x80000018); // E_ILLEGAL_DELEGATE_ASSIGNMENT)
57+
58+
// Remove global handler
59+
winrt_throw_hresult_handler = nullptr;
60+
s_loggerCalled = false;
61+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <crtdbg.h>
2+
#define CATCH_CONFIG_RUNNER
3+
4+
// Force reportFatal to be available on mingw-w64
5+
#define CATCH_CONFIG_WINDOWS_SEH
6+
7+
#include "catch.hpp"
8+
#include "winrt/base.h"
9+
10+
using namespace winrt;
11+
12+
int main(int const argc, char** argv)
13+
{
14+
init_apartment();
15+
std::set_terminate([] { reportFatal("Abnormal termination"); ExitProcess(1); });
16+
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
17+
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
18+
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
19+
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
20+
return Catch::Session().run(argc, argv);
21+
}
22+
23+
CATCH_TRANSLATE_EXCEPTION(hresult_error const& e)
24+
{
25+
return to_string(e.message());
26+
}

0 commit comments

Comments
 (0)