Skip to content

Commit b695eb8

Browse files
Merge #970
970: Add file!() builtin r=CohenArthur a=CohenArthur Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2 parents d3a4cf9 + 8cc50f2 commit b695eb8

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

gcc/rust/expand/rust-macro-builtins.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,35 @@
1818

1919
#include "rust-macro-builtins.h"
2020
#include "rust-diagnostics.h"
21+
#include "rust-expr.h"
22+
#include "rust-session-manager.h"
2123

2224
namespace Rust {
25+
namespace {
26+
std::unique_ptr<AST::Expr>
27+
make_string (Location locus, std::string value)
28+
{
29+
return std::unique_ptr<AST::Expr> (
30+
new AST::LiteralExpr (value, AST::Literal::STRING,
31+
PrimitiveCoreType::CORETYPE_STR, {}, locus));
32+
}
33+
} // namespace
34+
2335
AST::ASTFragment
2436
MacroBuiltin::assert (Location invoc_locus, AST::MacroInvocData &invoc)
2537
{
2638
rust_debug ("assert!() called");
2739

2840
return AST::ASTFragment::create_empty ();
2941
}
42+
43+
AST::ASTFragment
44+
MacroBuiltin::file (Location invoc_locus, AST::MacroInvocData &invoc)
45+
{
46+
auto current_file
47+
= Session::get_instance ().linemap->location_file (invoc_locus);
48+
auto file_str = AST::SingleASTNode (make_string (invoc_locus, current_file));
49+
50+
return AST::ASTFragment ({file_str});
51+
}
3052
} // namespace Rust

gcc/rust/expand/rust-macro-builtins.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class MacroBuiltin
2828
public:
2929
static AST::ASTFragment assert (Location invoc_locus,
3030
AST::MacroInvocData &invoc);
31+
32+
static AST::ASTFragment file (Location invoc_locus,
33+
AST::MacroInvocData &invoc);
3134
};
3235
} // namespace Rust
3336

gcc/rust/util/rust-hir-map.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
746746
Location, AST::MacroInvocData &)>>
747747
builtin_macros = {
748748
{"assert", MacroBuiltin::assert},
749+
{"file", MacroBuiltin::file},
749750
};
750751

751752
auto builtin = builtin_macros.find (macro->get_rule_name ());
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// { dg-output "rust/execute/torture/builtin_macros1.rs" }
2+
macro_rules! file {
3+
() => {{}};
4+
}
5+
6+
extern "C" {
7+
fn printf(fmt: *const i8, ...);
8+
}
9+
10+
fn print(s: &str) {
11+
printf("%s\n\0" as *const str as *const i8, s);
12+
}
13+
14+
fn main() -> i32 {
15+
print(file!());
16+
17+
0
18+
}

0 commit comments

Comments
 (0)