-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
ast: add Location
to wrap parser location
#3867
Conversation
a7edc08
to
db47a19
Compare
{ | ||
} | ||
|
||
Sizeof::Sizeof(Diagnostics &d, SizedType type, location loc) | ||
: Expression(d, loc), argtype(type) | ||
Sizeof::Sizeof(Diagnostics &d, SizedType type, Location &&loc) |
Check notice
Code scanning / CodeQL
Large object passed by value Note
SizedType
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 9 days ago
To fix the problem, we should pass the SizedType
object by reference instead of by value. This involves changing the constructor of the Sizeof
class to accept a const SizedType&
instead of a SizedType
. This change will ensure that only a reference to the object is passed, avoiding the overhead of copying the large object.
- Change the constructor signature in the
Sizeof
class to accept aconst SizedType&
for thetype
parameter. - Update the constructor definition accordingly.
-
Copy modified line R65
@@ -64,3 +64,3 @@ | ||
|
||
Sizeof::Sizeof(Diagnostics &d, SizedType type, Location &&loc) | ||
Sizeof::Sizeof(Diagnostics &d, const SizedType &type, Location &&loc) | ||
: Expression(d, std::move(loc)), argtype(type) |
Cast::Cast(Diagnostics &d, SizedType cast_type, Expression *expr, location loc) | ||
: Expression(d, loc), expr(expr) | ||
Cast::Cast(Diagnostics &d, | ||
SizedType cast_type, |
Check notice
Code scanning / CodeQL
Large object passed by value Note
SizedType
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 9 days ago
To fix the problem, we should pass the SizedType
object by reference instead of by value. This will avoid the overhead of copying the object and reduce the risk of stack overflow. Specifically, we will change the parameter type from SizedType
to const SizedType&
in the Cast
constructor.
- Change the parameter type of
cast_type
in theCast
constructor fromSizedType
toconst SizedType&
. - Update the constructor initialization list to use the reference.
-
Copy modified line R165 -
Copy modified line R168
@@ -164,8 +164,7 @@ | ||
Cast::Cast(Diagnostics &d, | ||
SizedType cast_type, | ||
const SizedType &cast_type, | ||
Expression *expr, | ||
Location &&loc) | ||
: Expression(d, std::move(loc)), expr(expr) | ||
: Expression(d, std::move(loc)), expr(expr), type(cast_type) | ||
{ | ||
type = cast_type; | ||
} |
In the future, we will no longer rely on the global singleton to contain the filename and contents, since we want to support multiple files. This change introduces an `ASTSource` class which can be used to contain this metadata, and wraps a reference to this in a new `Location` class. For now, the existing filename and contents are used by the `LogSink`, but this will change very shortly. One related note with this change is the requirement that `Location` be passed to nodes as a r-value. This is a minor change, but means that calls to `make_node` internally must explicitly copy or construct a new location, which makes it clear what is happening. In general, the `Location` type is cheap and can be carried by value. Signed-off-by: Adin Scannell <amscanne@meta.com> stack-info: PR: #3867, branch: user/amscanne/diags/9
db47a19
to
90fc2f7
Compare
// this automatically converts the parser `location` to the `Location` class | ||
// bound to the current source file. | ||
template <typename T> | ||
auto wrap(T &&t) -> decltype(t) |
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.
JC, what else do we intend on wrapping?
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.
Nothing at the moment, but this is also the simplest way to automatically decay all the regular cases to the same type.
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.
Mostly reviewed the new classes and skimmed the rest (relying on our tests here).
Stacked PRs:
Driver
/ASTContext
relationship #3855Location
to wrap parser location #3867ast: add
Location
to wrap parser locationIn the future, we will no longer rely on the global singleton to contain
the filename and contents, since we want to support multiple files. This
change introduces an
ASTSource
class which can be used to contain thismetadata, and wraps a reference to this in a new
Location
class.For now, the existing filename and contents are used by the
LogSink
,but this will change very shortly.
One related note with this change is the requirement that
Location
bepassed to nodes as a r-value. This is a minor change, but means that
calls to
make_node
internally must explicitly copy or construct a newlocation, which makes it clear what is happening. In general, the
Location
type is cheap and can be carried by value.Signed-off-by: Adin Scannell amscanne@meta.com