-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement <source_location> #664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
7e06756
Implement <source_location>
SuperWig 38d9d9f
Combine calling and argument
SuperWig 8534478
Use __LINE__
SuperWig 5dd4281
Make global global
SuperWig da69f62
Add license banner to header.h
SuperWig 90f321c
Add _NODISCARD to source_location::current()
SuperWig f0a692d
Copyright is probably important...
SuperWig a441e77
Add NTTP test
SuperWig 4bf3253
Fix sub_member_test()
SuperWig 1699518
Remove nttp_test()
SuperWig 6b64f6c
Missing constexpr
SuperWig 3c895e7
Only support clang
SuperWig d3f4c9e
&& not ||
SuperWig 8520e74
Merge remote-tracking branch 'origin/master' into source_location
SuperWig e9e18a7
Update feature_test_macro test
SuperWig 790bf20
Update __msvc_all_public_headers.hpp
SuperWig 5aa751b
Revert "&& not ||"
SuperWig b26b5c8
Revert "Update __msvc_all_public_headers.hpp"
SuperWig 4e2d9b8
Review comments
SuperWig 0d9d103
Casey's guard
SuperWig 8f0cfec
Update source_location part 1
SuperWig 26d1ddc
Update source_location part 2
SuperWig a9531db
__cpp_consteval
SuperWig 1665637
Transition, const, and unsigned int
SuperWig 92f16ae
Missed two __cpp_consteval
SuperWig ce6b054
Merge branch 'master' into source_location
SuperWig 6f03c28
Merge branch 'main' into source_location
StephanTLavavej 9aeb14d
Add source_location to various lists of files.
StephanTLavavej 7dd96df
__msvc_all_public_headers.hpp now has core/non-core sections.
StephanTLavavej ffad77c
Sort VSO_0157762_feature_test_macros.
StephanTLavavej 0e6a025
Note apparent bugs in MSVC 16.10p2.
StephanTLavavej b6cc086
Work around VSO-1285779 'EDG rejects source_location intrinsics withi…
StephanTLavavej 8f88b17
Reported VSO-1285783 'C++20 source_location intrinsic __builtin_FUNCT…
StephanTLavavej 723991a
Column numbers are reasonable. Add comma to TRANSITION.
StephanTLavavej 66324e1
Test source_location in P1502R1_standard_library_header_units.
StephanTLavavej 0183fb4
Use only filenames; internal Contest copies to different directories.
StephanTLavavej 2a81652
/analyze expects different column numbers.
StephanTLavavej c3cd3fa
yvals_core.h: Comment endif.
StephanTLavavej adaeb5d
Add MSVC to include_each_header_alone_matrix.lst.
StephanTLavavej e10b9d8
source_location: Stylistic changes.
StephanTLavavej 1521789
Test style: Avoid depending on header.h's includes and using-directive.
StephanTLavavej 4cd0c85
Test style: Terse static_assert is fine.
StephanTLavavej 5aec486
We can also test is_nothrow_default_constructible_v.
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,7 @@ | |
| "semaphore", | ||
| "set", | ||
| "shared_mutex", | ||
| "source_location", | ||
| "span", | ||
| "sstream", | ||
| "stack", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // source_location standard header (core) | ||
|
|
||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #pragma once | ||
| #ifndef _SOURCE_LOCATION_ | ||
| #define _SOURCE_LOCATION_ | ||
| #include <yvals_core.h> | ||
| #if _STL_COMPILER_PREPROCESSOR | ||
| #ifndef __cpp_consteval | ||
| #pragma message("The contents of <source_location> are available only with C++20 consteval support.") | ||
| #else // ^^^ !defined(__cpp_consteval) / defined(__cpp_consteval) vvv | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| #pragma pack(push, _CRT_PACKING) | ||
| #pragma warning(push, _STL_WARNING_LEVEL) | ||
| #pragma warning(disable : _STL_DISABLED_WARNINGS) | ||
| _STL_DISABLE_CLANG_WARNINGS | ||
| #pragma push_macro("new") | ||
| #undef new | ||
|
|
||
| _STD_BEGIN | ||
| struct source_location { | ||
| _NODISCARD static consteval source_location current(const uint_least32_t _Line_ = __builtin_LINE(), | ||
| const uint_least32_t _Column_ = __builtin_COLUMN(), const char* const _File_ = __builtin_FILE(), | ||
| const char* const _Function_ = __builtin_FUNCTION()) noexcept { | ||
| source_location _Result; | ||
| _Result._Line = _Line_; | ||
| _Result._Column = _Column_; | ||
| _Result._File = _File_; | ||
| _Result._Function = _Function_; | ||
| return _Result; | ||
| } | ||
|
|
||
| _NODISCARD_CTOR constexpr source_location() noexcept = default; | ||
|
|
||
| _NODISCARD constexpr uint_least32_t line() const noexcept { | ||
| return _Line; | ||
| } | ||
| _NODISCARD constexpr uint_least32_t column() const noexcept { | ||
| return _Column; | ||
| } | ||
| _NODISCARD constexpr const char* file_name() const noexcept { | ||
| return _File; | ||
| } | ||
| _NODISCARD constexpr const char* function_name() const noexcept { | ||
| return _Function; | ||
| } | ||
|
|
||
| private: | ||
| uint_least32_t _Line{}; | ||
| uint_least32_t _Column{}; | ||
| const char* _File = ""; | ||
| const char* _Function = ""; | ||
| }; | ||
| _STD_END | ||
|
|
||
| #pragma pop_macro("new") | ||
| _STL_RESTORE_CLANG_WARNINGS | ||
| #pragma warning(pop) | ||
| #pragma pack(pop) | ||
| #endif // !defined(__cpp_consteval) | ||
| #endif // _STL_COMPILER_PREPROCESSOR | ||
| #endif // _SOURCE_LOCATION_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_latest_matrix.lst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #pragma once | ||
| #include <cassert> | ||
| #include <source_location> | ||
| #include <string_view> | ||
|
|
||
| constexpr void header_test() { | ||
| using namespace std; | ||
| const auto x = source_location::current(); | ||
| assert(x.line() == __LINE__ - 1); | ||
| assert(x.column() == 37); | ||
| assert(x.function_name() == "header_test"sv); | ||
| assert(string_view{x.file_name()}.ends_with("header.h"sv)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #if defined(__cpp_consteval) && !defined(__EDG__) // TRANSITION, VSO-1285779 | ||
| #include "header.h" | ||
| #include <cassert> | ||
| #include <source_location> | ||
| #include <string_view> | ||
| #include <type_traits> | ||
| using namespace std; | ||
|
|
||
| static_assert(is_nothrow_default_constructible_v<source_location>); | ||
| static_assert(is_nothrow_move_constructible_v<source_location>); | ||
| static_assert(is_nothrow_move_assignable_v<source_location>); | ||
| static_assert(is_nothrow_swappable_v<source_location>); | ||
|
|
||
| constexpr auto test_cpp = "test.cpp"sv; | ||
|
|
||
| constexpr auto g = source_location::current(); | ||
| static_assert(g.line() == __LINE__ - 1); | ||
| static_assert(g.column() == 37); | ||
| static_assert(g.function_name() == ""sv); | ||
| static_assert(string_view{g.file_name()}.ends_with(test_cpp)); | ||
|
|
||
| constexpr int s_int_line = __LINE__ + 3; | ||
| struct s { | ||
| constexpr s(const source_location x = source_location::current()) : loc(x) {} | ||
| constexpr s(int) {} | ||
| source_location loc = source_location::current(); | ||
| }; | ||
|
|
||
| constexpr int s2_int_line = __LINE__ + 3; | ||
| struct s2 { | ||
| constexpr s2(const source_location l = source_location::current()) : x{l} {} | ||
| constexpr s2(int) {} | ||
| s x = source_location::current(); | ||
| }; | ||
|
|
||
| constexpr void copy_test() { | ||
| const auto rhs = source_location::current(); | ||
| const auto lhs = rhs; | ||
| assert(lhs.line() == rhs.line()); | ||
| assert(lhs.column() == rhs.column()); | ||
| assert(string_view{lhs.function_name()} == string_view{rhs.function_name()}); | ||
| assert(string_view{lhs.file_name()} == string_view{rhs.file_name()}); | ||
| } | ||
|
|
||
| constexpr void local_test() { | ||
| const auto x = source_location::current(); | ||
| assert(x.line() == __LINE__ - 1); | ||
| assert(x.column() == 37); | ||
| assert(x.function_name() == "local_test"sv); | ||
| assert(string_view{x.file_name()}.ends_with(test_cpp)); | ||
| } | ||
|
|
||
| constexpr void argument_test( | ||
| const unsigned int line, const unsigned int column, const source_location x = source_location::current()) { | ||
| assert(x.line() == line); | ||
| assert(x.column() == column); | ||
| assert(x.function_name() == "test"sv); | ||
| assert(string_view{x.file_name()}.ends_with(test_cpp)); | ||
| } | ||
|
|
||
| constexpr void sloc_constructor_test() { | ||
| const s x; | ||
| assert(x.loc.line() == __LINE__ - 1); | ||
| #ifdef _PREFAST_ | ||
| assert(x.loc.column() == 14); | ||
| #else // _PREFAST_ | ||
| assert(x.loc.column() == 13); | ||
| #endif // _PREFAST_ | ||
| if (is_constant_evaluated()) { | ||
| assert(x.loc.function_name() == "main"sv); // TRANSITION, VSO-1285783 | ||
| } else { | ||
| assert(x.loc.function_name() == "sloc_constructor_test"sv); | ||
| } | ||
| assert(string_view{x.loc.file_name()}.ends_with(test_cpp)); | ||
| } | ||
|
|
||
| constexpr void different_constructor_test() { | ||
| const s x{1}; | ||
| assert(x.loc.line() == s_int_line); | ||
| assert(x.loc.column() == 5); | ||
| assert(x.loc.function_name() == "s"sv); | ||
| assert(string_view{x.loc.file_name()}.ends_with(test_cpp)); | ||
| } | ||
|
|
||
| constexpr void sub_member_test() { | ||
| const s2 s; | ||
| assert(s.x.loc.line() == __LINE__ - 1); | ||
| #ifdef _PREFAST_ | ||
| assert(s.x.loc.column() == 15); | ||
| #else // _PREFAST_ | ||
| assert(s.x.loc.column() == 14); | ||
| #endif // _PREFAST_ | ||
| if (is_constant_evaluated()) { | ||
| assert(s.x.loc.function_name() == "main"sv); // TRANSITION, VSO-1285783 | ||
| } else { | ||
| assert(s.x.loc.function_name() == "sub_member_test"sv); | ||
| } | ||
| assert(string_view{s.x.loc.file_name()}.ends_with(test_cpp)); | ||
|
|
||
| const s2 s_i{1}; | ||
| assert(s_i.x.loc.line() == s2_int_line); | ||
| assert(s_i.x.loc.column() == 5); | ||
| assert(s_i.x.loc.function_name() == "s2"sv); | ||
| assert(string_view{s_i.x.loc.file_name()}.ends_with(test_cpp)); | ||
| } | ||
|
|
||
| constexpr void lambda_test() { | ||
| const auto l = [loc = source_location::current()] { return loc; }; | ||
| const auto x = l(); | ||
| assert(x.line() == __LINE__ - 2); | ||
| assert(x.column() == 51); | ||
| assert(x.function_name() == "lambda_test"sv); | ||
| assert(string_view{x.file_name()}.ends_with(test_cpp)); | ||
| } | ||
|
|
||
| template <class T> | ||
| constexpr source_location function_template() { | ||
| return source_location::current(); | ||
| } | ||
|
|
||
| constexpr void function_template_test() { | ||
| const auto x1 = function_template<void>(); | ||
| assert(x1.line() == __LINE__ - 5); | ||
| assert(x1.column() == 29); | ||
| assert(x1.function_name() == "function_template"sv); | ||
| assert(string_view{x1.file_name()}.ends_with(test_cpp)); | ||
|
|
||
| const auto x2 = function_template<int>(); | ||
| assert(x1.line() == x2.line()); | ||
| assert(x1.column() == x2.column()); | ||
| assert(string_view{x1.function_name()} == string_view{x2.function_name()}); | ||
| assert(string_view{x1.file_name()} == string_view{x2.file_name()}); | ||
| } | ||
|
|
||
| constexpr bool test() { | ||
| copy_test(); | ||
| local_test(); | ||
| argument_test(__LINE__, 5); | ||
| const auto loc = source_location::current(); | ||
| argument_test(__LINE__ - 1, 39, loc); | ||
| sloc_constructor_test(); | ||
| different_constructor_test(); | ||
| sub_member_test(); | ||
| lambda_test(); | ||
| function_template_test(); | ||
| header_test(); | ||
| return true; | ||
| } | ||
|
|
||
| int main() { | ||
| test(); | ||
| static_assert(test()); | ||
| return 0; | ||
| } | ||
| #else // ^^^ defined(__cpp_consteval) && !defined(__EDG__) / !defined(__cpp_consteval) || defined(__EDG__) vvv | ||
| int main() {} | ||
| #endif // ^^^ !defined(__cpp_consteval) || defined(__EDG__) ^^^ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.