This is an archive of the discontinued LLVM Phabricator instance.

[Damangle] convert dlangDemangle to use std::string_view
ClosedPublic

Authored by nickdesaulniers on May 19 2023, 3:44 PM.

Details

Summary

I was doing this API conversion to use std::string_view top-down in
D149104, but this exposed issues in individual demanglers that needed to
get fixed first. There's no issue with the conversion for the D language
demangler, so convert it.

I have a more aggressive refactoring of the entire D language demangler
to use std::string_view more extensively, but the interface with
llvm::nonMicrosoftDemangle is the more interesting one.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMay 19 2023, 3:44 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
nickdesaulniers requested review of this revision.May 19 2023, 3:44 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMay 19 2023, 3:44 PM
nickdesaulniers planned changes to this revision.Jun 1 2023, 10:47 AM

needs rebasing for presubmit tests

MaskRay added inline comments.Jun 1 2023, 9:39 PM
llvm/lib/Demangle/Demangle.cpp
49

Why is this change? The original contract is that MangledName must be non-null.

nickdesaulniers marked an inline comment as done.Jun 2 2023, 10:20 AM
nickdesaulniers added inline comments.
llvm/lib/Demangle/Demangle.cpp
49

https://fosstodon.org/@llvm/110397650834821908

It's insidious; implicitly constructing a std::string_view from a char* which is nullptr invokes a call to strlen upon construction, which segfaults on my system. Therefor, all of the nullptr checks need to be hoisted into the callers or stated another way exist at the boundary of char* to std::string_view API boundaries (such as right here).

nickdesaulniers marked an inline comment as done.Jun 2 2023, 10:20 AM
nickdesaulniers added inline comments.
llvm/lib/Demangle/Demangle.cpp
49

This is also why the nullptr input test case must be removed from llvm/unittests/Demangle/DLangDemangleTest.cpp below.

  • rebase for presubmits
MaskRay added inline comments.Jun 2 2023, 12:04 PM
llvm/lib/Demangle/Demangle.cpp
49

It's usually code smell if a function taking a C string argument additionally accepts nullptr.
Previously, passing nullptr to nonMicrosoftDemangle will crash as MangledName[0] is accessed. We should retain this strictness.

ninja check-llvm-demangle passes if I remove the 2 lines.

If future refactoring will possibly pass nullptr to nonMicrosoftDemangle, I think we should fix those call sites not to do that...

MaskRay accepted this revision.Jun 2 2023, 2:37 PM

If nonMicrosoftDemangle is going to be changed to take a string_view argument, adding if (!MangledName) return false; should be fine.
If possible, it's best for the change to be deferred to a later patch on the stack, but I won't push strongly on this. You did the work...

This revision is now accepted and ready to land.Jun 2 2023, 2:37 PM
nickdesaulniers added inline comments.Jun 2 2023, 2:52 PM
llvm/lib/Demangle/Demangle.cpp
49

We should retain this strictness.
If future refactoring will possibly pass nullptr to nonMicrosoftDemangle, I think we should fix those call sites not to do that...

Ok, I will drop it then, and add nullptr checks to callers.

ninja check-llvm-demangle passes if I remove the 2 lines.

Then check-llvm-demangle must not be running llvm/unittests/Demangle/DLangDemangleTest.cpp, or you tested with my change to llvm/unittests/Demangle/DLangDemangleTest.cpp from below applied.

nickdesaulniers planned changes to this revision.Jun 2 2023, 3:04 PM
  • rebase on D149675, remove nullptr check

No, I don't want no nullptr-check
A nullptr-check is a guard that can't get no love from me
Hangin' out the passenger side
Of their best friend's ride
Trying to holla at me

This revision is now accepted and ready to land.Jun 2 2023, 3:18 PM
nickdesaulniers marked an inline comment as done.Jun 2 2023, 3:18 PM
This revision was landed with ongoing or failed builds.Jun 2 2023, 3:20 PM
This revision was automatically updated to reflect the committed changes.
efriedma added inline comments.
llvm/lib/Demangle/DLangDemangle.cpp
555–556

Isn't this still assuming MangledName is null-terminated?

llvm/lib/Demangle/DLangDemangle.cpp
555–556

(I have a rewrite of the D language demangler to use std::string_view throughout, which I will post shortly; I believe that addresses your concern)

llvm/lib/Demangle/DLangDemangle.cpp
555–556

D152177 PTAL

[Damangle]

Damangle? dAmangle? *sigh*