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

cstring-related cleanup, switch to std::string_view for some cstring API #4716

Merged
merged 1 commit into from
Jun 21, 2024

Conversation

asl
Copy link
Contributor

@asl asl commented Jun 8, 2024

  • Switch to std::string_view for some cstring API

@asl asl requested review from vlstill and fruffy June 8, 2024 05:04
@asl asl force-pushed the cstring-improve branch from fe5eebd to 61ef9eb Compare June 8, 2024 07:17
@fruffy fruffy added core Topics concerning the core segments of the compiler (frontend, midend, parser) breaking-change This change may break assumptions of compiler back ends. labels Jun 8, 2024
@asl
Copy link
Contributor Author

asl commented Jun 12, 2024

@vlstill @fruffy Will you please take a look when you will have a chance? Thanks!

@asl asl force-pushed the cstring-improve branch from 61ef9eb to 960dc5b Compare June 12, 2024 16:30
@fruffy fruffy requested a review from ChrisDodd June 12, 2024 23:50
@fruffy
Copy link
Collaborator

fruffy commented Jun 12, 2024

  • Switch to std::string_view for some cstring API

  • Get rid of custom implementation of Util::PathName. Replace with std::filesystem::path, simplify code around

I would consider splitting these into two separate PRs? At least getting rid of Util::PathName, which is a good thing, is fairly involved.

I am not quite clear how many of the changes in this PR are related to the PathName refactoring and how many are related to other cleanups.

lib/path.h Outdated
static PathName empty;
};
// We used to have local implementation. Make this alias for now
using PathName = std::filesystem::path;
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we make this a separate PR I'd consider replacing this alias entirely. I do not see a reason to keep it, really.

Or, add a deprecation notice, if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

std::filesystem::path is quite long to type, in this sense PathName is much shorter even with the namespace. Certainly, this is a bit of bike-shedding :)

But yes, we can get rid of it entirely. Let me split into a separate PR and remove it entirely.

@@ -1581,6 +1581,7 @@ void P4RuntimeSerializer::serializeP4RuntimeIfRequired(const IR::P4Program *prog

void P4RuntimeSerializer::serializeP4RuntimeIfRequired(const P4RuntimeAPI &p4Runtime,
const CompilerOptions &options) {
// FIXME: get rid of cstring here
Copy link
Collaborator

Choose a reason for hiding this comment

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

What happens if you replace it with std::filesystem::path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are more dependencies here including parseFileNames logic, etc. I decided to leave it as-is for now.

@asl
Copy link
Contributor Author

asl commented Jun 13, 2024

I submitted #4721 for PathName part. Will rebase this one to have just cstring bits afterwards

@asl asl marked this pull request as draft June 13, 2024 05:36
@asl asl force-pushed the cstring-improve branch from 960dc5b to 3ec833d Compare June 20, 2024 18:45
Copy link
Collaborator

@fruffy fruffy left a comment

Choose a reason for hiding this comment

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

This ready? I guess the only thing that needs to change is PR title and description.

@@ -254,17 +254,11 @@ class cstring {
}
cstring substr(size_t start, size_t length) const;
cstring replace(char find, char replace) const;
cstring replace(cstring find, cstring replace) const;
cstring replace(std::string_view find, std::string_view replace) const;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This might be the only breaking change in this PR, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Though if before explicit-cstring-construction it was like replace("foo", "bar"), then after this PR it will start working again w/o changes :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I wonder why not return a std::string here. In many cases, we would not need the result to be cstring and if the caller needs it, they can always do that explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I checked this case, and there is a mixture of usages. So I decided to re-evaluate this when we'll disable implicit conversion to / from std::string – then I will catch all such places :)

@asl asl changed the title cstring-related cleanup, step 1 cstring-related cleanup, switch to std::string_view for some cstring API Jun 20, 2024
@asl asl marked this pull request as ready for review June 20, 2024 21:23
@asl asl added this pull request to the merge queue Jun 21, 2024
Merged via the queue into p4lang:main with commit 32e7396 Jun 21, 2024
16 checks passed
@asl asl deleted the cstring-improve branch June 21, 2024 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change This change may break assumptions of compiler back ends. core Topics concerning the core segments of the compiler (frontend, midend, parser)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants