diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2013-10-05 21:15:46 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2013-10-31 13:46:10 -0700 |
| commit | a49e65c2edad450cabc0745e94e7c031c5d4f7f8 (patch) | |
| tree | d63f71dcbb24a4f7f74e60d4e06dd1779e823a0c /src/libsyntax/ext/format.rs | |
| parent | e976de32dc590f759e6c0c72d286844ca373e775 (diff) | |
Implement a concat!() format extension
This extension can be used to concatenate string literals at compile time. C has
this useful ability when placing string literals lexically next to one another,
but this needs to be handled at the syntax extension level to recursively expand
macros.
The major use case for this is something like:
macro_rules! mylog( ($fmt:expr $($arg:tt)*) => {
error2!(concat!(file!(), ":", line!(), " - ", $fmt) $($arg)*);
})
Where the mylog macro will automatically prepend the filename/line number to the
beginning of every log message.
Diffstat (limited to 'src/libsyntax/ext/format.rs')
| -rw-r--r-- | src/libsyntax/ext/format.rs | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index 31befed6c0c..943279d2dc6 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -735,8 +735,10 @@ pub fn expand_args(ecx: @ExtCtxt, sp: Span, (_, None) => { return MRExpr(ecx.expr_uint(sp, 2)); } }; cx.fmtsp = efmt.span; - let (fmt, _fmt_str_style) = expr_to_str(ecx, efmt, - "format argument must be a string literal."); + // Be sure to recursively expand macros just in case the format string uses + // a macro to build the format expression. + let (fmt, _) = expr_to_str(ecx, ecx.expand_expr(efmt), + "format argument must be a string literal."); let mut err = false; do parse::parse_error::cond.trap(|m| { |
