Skip to content

Equivalent of rustc's -Zunpretty=expanded #3420

@aytey

Description

@aytey

When working with macros, it is sometimes useful to use rustc's -Zunpretty=expanded flag to see how a macro is being expanded (think of this like -E when working with gcc for C or C++).

I checked the source tree, the tests and the help of a recent build (4d7e7d9), but I couldn't see anything that looked like it matched dumping -Zunpretty=expanded to stdout/stderr/a file.

Is there an equivalent for gccrs?

Activity

aytey

aytey commented on Feb 4, 2025

@aytey
Author

By way of an example:

${HOME}/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc ${HOME}/clones/gccrs/gcc/testsuite/rust/execute/torture/macros22.rs -Zunpretty=expanded

Gives:

#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
// { dg-output "1\r*\n2\r*\nNaN\r*\n3\r*\n" }

macro_rules! print_num {
    ($l:literal) =>
    { { unsafe { printf("%d\n\0" as *const str as *const i8, $l); } } };
}

extern "C" {
    fn printf(s: *const i8, ...);
}

// Check to make sure that expanding macros does not break the flow of calls
fn main() -> i32 {
    { unsafe { printf("%d\n\0" as *const str as *const i8, 1); } };
    { unsafe { printf("%d\n\0" as *const str as *const i8, 2); } };

    unsafe { printf("NaN\n\0" as *const str as *const i8); }
    {
        unsafe {
            printf("%d\n\0" as *const str as *const i8,
                3);
        }
    };

    0
}

So I was wondering if it is possible to do the same with gccrs.

P-E-P

P-E-P commented on Feb 4, 2025

@P-E-P
Member

You can use -frust-dump-expansion although it is not 100% perfect for now, it'll insert some comments for tail expressions. Some work is already on track to improve that #3212 should allow more customization and #3209 should fix a slight issue with doc comments being converted to attributes.

aytey

aytey commented on Feb 4, 2025

@aytey
Author

Works perfectly -- thank you!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @aytey@P-E-P

        Issue actions

          Equivalent of `rustc`'s `-Zunpretty=expanded` · Issue #3420 · Rust-GCC/gccrs