Skip to content
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

Various cleanups: Use unnamed namespaces #4124

Conversation

StephanTLavavej
Copy link
Member

(I recommend "ignore whitespace" while reviewing this.)

Over time, we've developed a convention of using unnamed namespaces in stl/src for internal machinery. This clearly separates TU-local code from functions that are accessed from other stl/src TUs, injected into the import lib, or dllexported. I noticed a couple of older TUs that could benefit from being cleaned up.

  • xtime.cpp: The _Epoch and _Nsec100_per_sec constants can be function-local.
  • xtime.cpp: Use an unnamed namespace, following our modern convention.
    • The constexpr variables and static functions had internal linkage, so this is unobservable to other TUs. We can drop static now.
  • stdhndlr.cpp: Use an unnamed namespace, following our modern convention.
    • UCRT <new.h> declares _set_new_handler, which is better than trying to declare it ourselves. We can then drop the new_hand typedef which was being used for that declaration.
    • The _New_handler variable was static, so we can clearly move it into an unnamed namespace and drop the static. We do need to qualify the _STD new_handler type now.
    • The _New_handler_interface helper function was never dllexported nor referred to by other stl/src TUs. We can move it out of namespace std and into the unnamed namespace.
    • The resulting code has our desired distinction between TU-local and dllexported machinery.

The `constexpr` variables and `static` functions had internal linkage, so this is unobservable to other TUs. We can drop `static` now.
UCRT `<new.h>` declares `_set_new_handler`, which is better than trying to declare it ourselves. We can then drop the `new_hand` typedef which was being used for that declaration.

The `_New_handler` variable was `static`, so we can clearly move it into an unnamed namespace and drop the `static`. We do need to qualify the `_STD new_handler` type now.

The `_New_handler_interface` helper function was never dllexported nor referred to by other stl/src TUs. We can move it out of `namespace std` and into the unnamed namespace.

The resulting code has our desired distinction between TU-local and dllexported machinery.
@StephanTLavavej StephanTLavavej added the enhancement Something can be improved label Oct 23, 2023
@StephanTLavavej StephanTLavavej requested a review from a team as a code owner October 23, 2023 16:40
@StephanTLavavej StephanTLavavej self-assigned this Oct 24, 2023
@StephanTLavavej
Copy link
Member Author

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej
Copy link
Member Author

I've resolved the simple merge conflict in xtime.cpp where #4106 added noexcept and this PR moved constants to be local.

@@ -46,7 +46,8 @@ namespace {

_EXTERN_C

_CRTIMP2_PURE long long __cdecl _Xtime_get_ticks() { // get system time in 100-nanosecond intervals since the epoch
_CRTIMP2_PURE long long __cdecl _Xtime_get_ticks() noexcept {
// get system time in 100-nanosecond intervals since the epoch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been hard work causing all the merge conflicts on my own since @BillyONeal left the team.

@StephanTLavavej StephanTLavavej merged commit 5a852a4 into microsoft:main Oct 26, 2023
35 checks passed
@StephanTLavavej StephanTLavavej deleted the dry-cleaning-8-unnamed-namespaces branch October 26, 2023 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Something can be improved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants