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

Start reusing rustc's format_args parser #2822

Merged
merged 13 commits into from
Feb 26, 2024

Conversation

CohenArthur
Copy link
Member

@CohenArthur CohenArthur commented Jan 30, 2024

This pull request adds a base for reusing the rustc_format_parser crate to parse the format strings in format_args!() invocations.
There are still a lot of things missing, and the build system stuff is probably wrong. The commit history needs to be fixed as well.

Fixes #2832

This PR depends on #2802 #2853 for the proper libgrust Changelog file

gcc/rust/checks/errors/borrowck/ffi-polonius/Cargo.lock Outdated Show resolved Hide resolved
}

auto format_string = fmt_expr->as_string ();
auto pieces = Fmt::Pieces::collect (format_string);
Copy link
Member Author

Choose a reason for hiding this comment

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

we need to figure out exactly what to give here, e.g if we do

format_args!("heyo {bit}", bit = 15);

should we parse heyo {bit} or \"heyo {bit}\", bit = 15. I believe it is the second one but that's not what we do at the moment

Copy link
Member Author

Choose a reason for hiding this comment

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

for this I think we can reuse the tokenstream from @P-E-P and turn the entire invocation tokenstream into a string which we'll then parse using the lib

Copy link
Member Author

Choose a reason for hiding this comment

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

this is done per cfaf9fa but I think we do get garbage values when dropping the PieceSlice - needs to investigate

Copy link
Member Author

Choose a reason for hiding this comment

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

now fixed in 71680f5 by keeping ownership of the string to parse. this is ready for merging I think?

@CohenArthur CohenArthur changed the title reuse rustc format parser Start reusing rustc's format_args parser Jan 30, 2024
@CohenArthur CohenArthur force-pushed the reuse-rustc-format-parser branch 3 times, most recently from e747d1a to 71680f5 Compare February 13, 2024 15:32
@CohenArthur CohenArthur marked this pull request as ready for review February 13, 2024 15:33
@CohenArthur
Copy link
Member Author

I'll open an issue to cleanup the implementation once it is merged, we can write better Rust in there and separate into better modules with better names.

This commit adds a base class for parsing the various constructs of a
Rust format string, according to the grammar in the reference:

https://doc.rust-lang.org/std/fmt/index.html#syntax

gcc/rust/ChangeLog:

	* Make-lang.in: Compile rust-fmt object
	* ast/rust-fmt.cc: New file.
	* ast/rust-fmt.h: New file.
@CohenArthur CohenArthur force-pushed the reuse-rustc-format-parser branch 4 times, most recently from 65efcdd to 7d2d639 Compare February 26, 2024 16:31
Compile libformat_parser and link to it.

gcc/rust/ChangeLog:

	* Make-lang.in: Compile libformat_parser.
	* ast/rust-fmt.cc: New FFI definitions.
	* ast/rust-fmt.h: Likewise.
	* expand/rust-macro-builtins.cc (MacroBuiltin::format_args_handler): Call
	into libformat_parser.
	* expand/rust-macro-builtins.h: Define format_args!() handler proper.

libgrust/ChangeLog:

	* libformat_parser/Cargo.lock: New file.
	* libformat_parser/Cargo.toml: New file.
	* libformat_parser/generic_format_parser/Cargo.toml: New file.
	* libformat_parser/generic_format_parser/src/lib.rs: New file.
	* libformat_parser/src/bin.rs: New file.
	* libformat_parser/src/lib.rs: New file.
libgrust/ChangeLog:

	* libformat_parser/generic_format_parser/src/lib.rs: Add generic
	library.
	* libformat_parser/src/lib.rs: Add base for FFI interface.
libgrust/ChangeLog:

	* libformat_parser/cbindgen.toml: New file.
	* libformat_parser/libformat-parser.h: New file.

gcc/rust/ChangeLog:

	* ast/rust-fmt.h: Add remaining FFI types.
ChangeLog:

	* .gitignore: Add libgrust target folders to the ignore list.
gcc/rust/ChangeLog:

	* ast/rust-fmt.cc (Pieces::collect): Use new Pieces API.
	* ast/rust-fmt.h: Update interface with new FFI bindings.

libgrust/ChangeLog:

	* libformat_parser/src/lib.rs: Add IntoFFI trait.
	* libformat_parser/libformat-parser.h: Removed.
gcc/rust/ChangeLog:

	* ast/rust-fmt.cc (Pieces::~Pieces): Call libformat_parser's release
	function in destructor.
	* ast/rust-fmt.h (struct PieceSlice): Add capacity.
	(destroy_pieces): New.
	(struct Pieces): Add destructor.

libgrust/ChangeLog:

	* libformat_parser/src/lib.rs: Leak Boxes properly for C++ to
	see them, add memory release function.
gcc/rust/ChangeLog:

	* expand/rust-macro-builtins.cc (MacroBuiltin::format_args_handler):
	Construct string to parser properly.
gcc/rust/ChangeLog:

	* expand/rust-macro-builtins.cc (MacroBuiltin::format_args_handler):
	Transform entire invocation token stream into string for the parser.
gcc/rust/ChangeLog:

	* ast/rust-fmt.cc (Pieces::collect): Fix signature to take ownership
	of the given string.
	* ast/rust-fmt.h (struct Pieces): Store parsed string in the struct.

libgrust/ChangeLog:

	* libformat_parser/src/lib.rs: Add debug prompt.
libgrust/ChangeLog:

	* libformat_parser/generic_format_parser/src/lib.rs: Remove
	unused deprecated attribute and unused import.
	* libformat_parser/src/lib.rs: Remove unused import.
Workaround for Ubuntu 18.04, since we still use it for the GCC 4.8 CI.
The default Rust package is 1.65 (and unlikely to change I assume?),
but the generic format parser library uses `is_some_and` which was
introduced in 1.70. So this is a simple reimplementation, directly taken
from the standard library sources.

libgrust/ChangeLog:

	* libformat_parser/generic_format_parser/src/lib.rs: Add IsSomeAnd<T>
	trait, impl it for Option<T>.
ChangeLog:

	* .github/workflows/ccpp.yml: Install cargo for GCC 4.8 job.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Add parser for format_args!() strings
1 participant