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

Make preprocess and getIncludePath const functions. #4785

Merged
merged 3 commits into from
Jul 16, 2024

Conversation

fruffy
Copy link
Collaborator

@fruffy fruffy commented Jul 4, 2024

There is no compelling reason why the preprocessing function and the getIncludePath function should not be the const. I changed the interface such that these functions can be used as const functions. The only thing that doesn't work is `file = "", not clear to me how it is used.

@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 Jul 4, 2024
Copy link
Contributor

@vlstill vlstill left a comment

Choose a reason for hiding this comment

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

The whole thing around closeInput is ugly, but that is a different problem. Or maybe just add a destructor to PreprocessorResult that closes the FILE * if necessary?

frontends/common/parser_options.h Outdated Show resolved Hide resolved
@fruffy
Copy link
Collaborator Author

fruffy commented Jul 8, 2024

The whole thing around closeInput is ugly, but that is a different problem. Or maybe just add a destructor to PreprocessorResult that closes the FILE * if necessary?

The destructor might close the file too early if not handled correctly. I added an explicit function instead.

Copy link
Contributor

@ChrisDodd ChrisDodd left a comment

Choose a reason for hiding this comment

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

A lot of this mess comes from the incompatibility of popen with iostreams.

@vlstill
Copy link
Contributor

vlstill commented Jul 9, 2024

The whole thing around closeInput is ugly, but that is a different problem. Or maybe just add a destructor to PreprocessorResult that closes the FILE * if necessary?

The destructor might close the file too early if not handled correctly. I added an explicit function instead.

How could an explicit member function close it after the destructor? That would mean the object is already destructed and the member function can't be called. You might need to define a move operator though (the invalidates the source).

@fruffy
Copy link
Collaborator Author

fruffy commented Jul 9, 2024

A lot of this mess comes from the incompatibility of popen with iostreams.

Would be great if we could call into the preprocessors as a library, would solve a bunch of problems.

How could an explicit member function close it after the destructor? That would mean the object is already destructed and the member function can't be called. You might need to define a move operator though (the invalidates the source).

Instead of adding the file handle closing in the destructor I just added a member function that can be called.

@vlstill
Copy link
Contributor

vlstill commented Jul 9, 2024

How could an explicit member function close it after the destructor? That would mean the object is already destructed and the member function can't be called. You might need to define a move operator though (the invalidates the source).

Instead of adding the file handle closing in the destructor I just added a member function that can be called.

I can see that. My point is that you can't call a member function after a destructor. That would mean calling a function on a destructed object, which is UB. So a destructor can't be "too early". The only possible problem with destructor is that there might be multiple copies/moved versions of the same PreprocessorResult. The standard solution in this case is to forbid copying and invalidate the moved-from object so that only the last moved-to object actually closes the stream. That is the C++ way (and exception-safe way) of doing this.

@fruffy
Copy link
Collaborator Author

fruffy commented Jul 9, 2024

The standard solution in this case is to forbid copying and invalidate the moved-from object so that only the last moved-to object actually closes the stream. That is the C++ way (and exception-safe way) of doing this.

With too early I meant what you described, where you have multiple objects and the file handle gets closed prematurely because one of the copies is destroyed. It seems safer to make this explicit instead of worrying about copy/move semantics.

@ChrisDodd
Copy link
Contributor

The standard solution in this case is to forbid copying and invalidate the moved-from object so that only the last moved-to object actually closes the stream. That is the C++ way (and exception-safe way) of doing this.

With too early I meant what you described, where you have multiple objects and the file handle gets closed prematurely because one of the copies is destroyed. It seems safer to make this explicit instead of worrying about copy/move semantics.

Well, making the object non-copyable will avoid that (and is much simpler)

@fruffy
Copy link
Collaborator Author

fruffy commented Jul 9, 2024

Well, making the object non-copyable will avoid that (and is much simpler)

It makes working with the returned object itself a fair bit more involved though. I didn't want to make the return value of the call overly complicated.

@ChrisDodd
Copy link
Contributor

Well, making the object non-copyable will avoid that (and is much simpler)

It makes working with the returned object itself a fair bit more involved though. I didn't want to make the return value of the call overly complicated.

Usually it makes dealing with the returned object much simpler -- you just return by value and it "just works" and you don't need to do anything special. That's the whole point of movable-not-copyable objects.

@fruffy
Copy link
Collaborator Author

fruffy commented Jul 9, 2024

Well, making the object non-copyable will avoid that (and is much simpler)

It makes working with the returned object itself a fair bit more involved though. I didn't want to make the return value of the call overly complicated.

Usually it makes dealing with the returned object much simpler -- you just return by value and it "just works" and you don't need to do anything special. That's the whole point of movable-not-copyable objects.

Okay for file handles this does make sense and after rewriting parseInput the code change was not particularly involved. The previous version I had caused a copy which is why deleting the constructor didn't work. This is better indeed.

Copy link
Contributor

@vlstill vlstill left a comment

Choose a reason for hiding this comment

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

Thanks for the change. I suspect the move on the PreprocessorResult is not used, but if it would, it would be wrong.

Comment on lines 69 to 70
PreprocessorResult &operator=(PreprocessorResult &&) = default;
PreprocessorResult(PreprocessorResult &&) = default;
Copy link
Contributor

Choose a reason for hiding this comment

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

This will not work. The default move will do memberwise move, but for primitive types that is just a copy. So we either need to disable the move constructor too (which would probably work, otherwise there is a double-close somewhere now), or explicitly set orig._file = nullptr in the move operator/ctor.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or, maybe, one could use std::unique_ptr<FILE, decltype(&fclose)> This way the owning semantics would be preserved.

frontends/common/parser_options.h Outdated Show resolved Hide resolved
@fruffy fruffy added this pull request to the merge queue Jul 16, 2024
Merged via the queue into main with commit 0fafda0 Jul 16, 2024
17 checks passed
@fruffy fruffy deleted the fruffy/const_functions branch July 16, 2024 14:37
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