Skip to content

Commit 0b6e350

Browse files
Compiler bug workaround for using format() in a user-defined module (#4919)
1 parent 3cf597e commit 0b6e350

File tree

11 files changed

+127
-22
lines changed

11 files changed

+127
-22
lines changed

stl/inc/format

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,16 +2632,16 @@ template <integral _Integral>
26322632
_NODISCARD constexpr string_view _Get_integral_prefix(const char _Type, const _Integral _Value) noexcept {
26332633
switch (_Type) {
26342634
case 'b':
2635-
return "0b"sv;
2635+
return string_view{"0b", 2}; // TRANSITION, VSO-1775715 (should use UDLs in this function)
26362636
case 'B':
2637-
return "0B"sv;
2637+
return string_view{"0B", 2};
26382638
case 'x':
2639-
return "0x"sv;
2639+
return string_view{"0x", 2};
26402640
case 'X':
2641-
return "0X"sv;
2641+
return string_view{"0X", 2};
26422642
case 'o':
26432643
if (_Value != _Integral{0}) {
2644-
return "0"sv;
2644+
return string_view{"0", 1};
26452645
}
26462646
return {};
26472647
default:

tests/std/test.lst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,5 +762,6 @@ tests\VSO_0849827_multicontainer_emplace_hint_position
762762
tests\VSO_0938757_attribute_order
763763
tests\VSO_0961751_hash_range_erase
764764
tests\VSO_0971246_legacy_await_headers
765+
tests\VSO_1775715_user_defined_modules
765766
tests\VSO_1804139_static_analysis_warning_with_single_element_array
766767
tests\VSO_1925201_iter_traits
Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
11
# Copyright (c) Microsoft Corporation.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
RUNALL_INCLUDE ..\..\..\universal_prefix.lst
5-
RUNALL_CROSSLIST
6-
* PM_CL="/w14365 /D_ENFORCE_FACET_SPECIALIZATIONS=1 /D_STL_CALL_ABORT_INSTEAD_OF_INVALID_PARAMETER /Zc:preprocessor"
7-
RUNALL_CROSSLIST
8-
* PM_CL="/w14640 /Zc:threadSafeInit- /EHsc /DTEST_STANDARD=20 /std:c++20"
9-
* PM_CL="/w14640 /Zc:threadSafeInit- /EHsc /DTEST_STANDARD=23 /std:c++latest"
10-
RUNALL_CROSSLIST
11-
* PM_CL="/MD"
12-
* PM_CL="/MDd"
13-
* PM_CL="/MT"
14-
* PM_CL="/MTd"
15-
* PM_CL="/MDd /analyze:only /analyze:autolog-"
16-
* PM_CL="/MDd /GR- /D_HAS_STATIC_RTTI=0"
17-
* PM_CL="/MDd /utf-8"
18-
RUNALL_CROSSLIST
19-
PM_CL=""
20-
ASAN PM_CL="-fsanitize=address /Zi" PM_LINK="/debug"
4+
RUNALL_INCLUDE ..\modules_20_matrix.lst
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
import os
5+
6+
from stl.test.format import STLTestFormat, TestStep
7+
from stl.test.tests import TestType
8+
9+
10+
class CustomTestFormat(STLTestFormat):
11+
def getBuildSteps(self, test, litConfig, shared):
12+
_, outputBase = test.getTempPaths()
13+
14+
testCpp = test.getSourcePath()
15+
sourceDir = os.path.dirname(testCpp)
16+
userIxx = os.path.join(sourceDir, 'user.ixx')
17+
18+
# Dependency order is important here:
19+
inputPaths = [userIxx, testCpp]
20+
21+
cmd = [test.cxx, *inputPaths, *test.flags, *test.compileFlags]
22+
23+
if TestType.COMPILE in test.testType:
24+
cmd += ['/c']
25+
elif TestType.RUN in test.testType:
26+
shared.execFile = f'{outputBase}.exe'
27+
cmd += [f'/Fe{shared.execFile}', '/link', *test.linkFlags]
28+
29+
yield TestStep(cmd, shared.execDir, shared.env, False)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
use strict;
5+
use warnings;
6+
7+
use Run;
8+
9+
sub CustomBuildHook()
10+
{
11+
my $cwd = Run::GetCWDName();
12+
13+
# Dependency order is important here:
14+
my @inputPaths = ("user.ixx", "test.cpp");
15+
16+
Run::ExecuteCL(join(" ", @inputPaths, "/Fe$cwd.exe"));
17+
}
18+
1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
RUNALL_INCLUDE ..\modules_20_matrix.lst
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
import os
5+
import site
6+
site.addsitedir(os.path.dirname(os.path.dirname(__file__)))
7+
import VSO_1775715_user_defined_modules.custom_format
8+
9+
config.test_format = VSO_1775715_user_defined_modules.custom_format.CustomTestFormat()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
// Note: To properly test the fix for VSO-1775715, don't include any headers here.
5+
6+
import User;
7+
8+
int main() {
9+
user::prepare_test_environment();
10+
user::test_vso_1775715(0, "0 0 0b0 0B0 0x0 0X0 0");
11+
user::test_vso_1775715(77, "77 77 0b1001101 0B1001101 0x4d 0X4D 0115");
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
module;
5+
6+
#include <cassert>
7+
#include <format>
8+
#include <string>
9+
10+
#include <force_include.hpp>
11+
12+
export module User;
13+
14+
namespace user {
15+
export void prepare_test_environment() {
16+
assert(test_environment_preparer.succeeded());
17+
}
18+
19+
// DevCom-10313766 VSO-1775715 "Using std::format in a module
20+
// requires including <format> header in .cpp files using that module"
21+
export template <class T>
22+
void test_vso_1775715(const T& value, const char* const expected_str) {
23+
const std::string actual_str = std::format("{0:#} {0:#d} {0:#b} {0:#B} {0:#x} {0:#X} {0:#o}", value);
24+
assert(actual_str == expected_str);
25+
}
26+
} // namespace user

tests/std/tests/modules_20_matrix.lst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
RUNALL_INCLUDE ..\..\universal_prefix.lst
5+
RUNALL_CROSSLIST
6+
* PM_CL="/w14365 /D_ENFORCE_FACET_SPECIALIZATIONS=1 /D_STL_CALL_ABORT_INSTEAD_OF_INVALID_PARAMETER /Zc:preprocessor"
7+
RUNALL_CROSSLIST
8+
* PM_CL="/w14640 /Zc:threadSafeInit- /EHsc /DTEST_STANDARD=20 /std:c++20"
9+
* PM_CL="/w14640 /Zc:threadSafeInit- /EHsc /DTEST_STANDARD=23 /std:c++latest"
10+
RUNALL_CROSSLIST
11+
* PM_CL="/MD"
12+
* PM_CL="/MDd"
13+
* PM_CL="/MT"
14+
* PM_CL="/MTd"
15+
* PM_CL="/MDd /analyze:only /analyze:autolog-"
16+
* PM_CL="/MDd /GR- /D_HAS_STATIC_RTTI=0"
17+
* PM_CL="/MDd /utf-8"
18+
RUNALL_CROSSLIST
19+
PM_CL=""
20+
ASAN PM_CL="-fsanitize=address /Zi" PM_LINK="/debug"

0 commit comments

Comments
 (0)