-
Notifications
You must be signed in to change notification settings - Fork 444
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
Add abseil string helpers #4971
Conversation
This required few breaking changes though:
|
Nice, maybe we should wait for #4964 to be merged to avoid compatibility issues? |
I not quite sure. Even more, given the size of the backend, doing project-wide refactoring and changes could be prohibitely expensive as soon as it will be merged in. |
ba1e77b
to
40d0a66
Compare
Tofino failures are due to mentioned protobuf ambiguities. They will likely will be easier to fix after the subsequent PR that removes implicit conversion to |
What are the planned changes for the next PR? Maybe you can stack it on this one so we can see? |
@@ -147,7 +147,7 @@ class BFRuntimeArchHandler : public P4RuntimeArchHandlerCommon<arch> { | |||
} | |||
auto *externType = p4info->add_externs(); | |||
externType->set_extern_type_id(static_cast<p4rt_id_t>(typeId)); | |||
externType->set_extern_type_name(typeName); | |||
externType->set_extern_type_name(typeName.string_view()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this required here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned, Protobuf strings have two constructors: from const char*
and string_view
. In order to use cstring
directly we need to make this conversion explicit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I assume the reason is that we also have an implicit conversion to const char*
? Is the follow-up PR working on that? Maybe it should be first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. And this is misused in many places, e.g. passing cstring's
to C functions like strlen
, improper checks for nulls, etc. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added commits from subsequent PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes paired with the subsequent Pr look much better to me. Definitely an improvement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might missed few cases here and there. However, I believe that we do not need lambdas in the example like in #4951 (comment) anymore.
@@ -191,7 +191,7 @@ class cstring { | |||
std::string_view string_view() const { | |||
return str ? std::string_view(str) : std::string_view(""); | |||
} | |||
explicit operator std::string_view() const { return string_view(); } | |||
operator std::string_view() const { return string_view(); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would add a comment here why this shouldn't be explicit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was already discussed in #4688. But essentially: we are going to make interface similar to std::string
essentially allowing string_view
into corresponding cstring-backed storage. This would fix lots of cstring
misuse.
@fruffy @vlstill @ChrisDodd Will you please take a look when you will have a chance? |
Changes generally look good to me, I am waiting for comments by others since this is a breaking change. The Tofino breakage looks easy enough to fix. I will also push some more Tofino fixes later which could help this PR. |
We really need multiple formatters for things like IDs and Nodes:
In the past, we've used Where does abseil formatting fit in this? Is it for user messages or logging? Whatever the choice, it needs to be clearly documented. |
This simplifies The converters are to simplify - std::string tcActionParam = absl::StrCat("\n\tparam ", paramName.string_view(), " type ");
+ std::string tcActionParam = absl::StrCat("\n\tparam ", paramName, " type "); Or, instead of name = absl::StrCat(absl::StrJoin(stack, ".", [](std::string *out, cstring s) {
absl::StrAppend(out, s.string_view());
}), name.string_view()) we end with name = absl::StrCat(absl::StrJoin(stack, "."), name); There are all different ways to "convert" I will add comment describing the intended usage. |
For an |
That's correct. But neither user-side error reporting, nor debug printing are affected / changed / or even touched. These are just helpers to simplify the usage of abseil string manipulation functions. So, I might just put wrong title of the PR. To put things simpler: here we are entirely in the |
ir/id.h
Outdated
@@ -58,13 +59,20 @@ struct ID : Util::IHasSourceInfo, public IHasDbPrint { | |||
bool operator!=(const char *a) const { return name != a; } | |||
/// Defer to cstring's notion of less, which is a lexicographical and not a pointer comparison. | |||
bool operator<(const char *a) const { return name < a; } | |||
explicit operator bool() const { return name; } | |||
explicit operator bool() const { return name.c_str(); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe should be return name != nullptr;
for clarity?
- cstring - IR::Node - IR::ID This improves the code itself as we no longer need to do explicit .toString() / .string_view() calls. Also, this prints directly to sink, providing some performance improvements. Signed-off-by: Anton Korobeynikov <[email protected]>
Signed-off-by: Anton Korobeynikov <[email protected]>
…ambiguous Signed-off-by: Anton Korobeynikov <[email protected]>
Signed-off-by: Anton Korobeynikov <[email protected]>
Signed-off-by: Anton Korobeynikov <[email protected]>
Signed-off-by: Anton Korobeynikov <[email protected]>
Signed-off-by: Anton Korobeynikov <[email protected]>
Add abseil string manipulator helpers for
This improves the code itself as we no longer need to do explicit .toString() / .string_view() calls. Also, this prints directly to sink, providing some performance improvements.