Skip to content

[xlscc] Expression cloning #2057

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

kbieganski
Copy link

This patch adds a Clang expression tree cloning utility to xlscc, as well as a unit test for it.

It handles most C++ expressions, with one notable exception being lambdas (as they are more complex but probably won't be needed).
It doesn't support non-standard extensions.

Copy link

google-cla bot commented Apr 25, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

}

namespace xlscc {
const clang::Expr* Clone(clang::ASTContext& ctx, const clang::Expr* expr);
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 when an unsupported type of expression is passed in? Is everything guaranteed to succeed? I imagine this should return absl::StatusOrclang::Expr* (why const?).

Copy link
Author

Choose a reason for hiding this comment

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

An assert in the visitor is triggered, if asserts are off I guess it crashes on null pointer. I was thinking it would always explicitly abort, but it doesn't seem to. Anyway, sure, I'll make it return StatusOr.

It's const because the original AST is const. If the cloned AST isn't const then we leak non-const access to Decls (see here).

Copy link
Author

Choose a reason for hiding this comment

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

Added graceful handling of unsupported expressions. Let me know if the const is OK.

Copy link
Collaborator

@spurserh spurserh left a comment

Choose a reason for hiding this comment

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

Please see comments

Copy link
Collaborator

@spurserh spurserh left a comment

Choose a reason for hiding this comment

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

I would like to make sure that the unsupported error message names the exact Expr type that is unsupported, so that it is quick to add what's missing when needed.

expr->requiresZeroInitialization());
}

clang::Expr* VisitExpr(const clang::Expr* expr) { return nullptr; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you store the specific clang::Expr* here for error reporting?

Copy link
Author

Choose a reason for hiding this comment

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

I don't think StatusOr can hold arbitrary data, but now I'm storing the name of the offending expression here which should be good enough for error reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants