Skip to content

Commit 41f402f

Browse files
bors[bot]mvvsmk
andauthored
Merge #1004
1004: Added column!() macro r=CohenArthur a=mvvsmk Fixes issue #979 1) Added the column!() macro using the LOCATION_COLUMN() from gcc_linemap 2) To-Do: add relevant test cases. Signed-off-by : M V V S Manoj Kumar <[email protected]> The test case I added always fails, I can't figure out whether there is a problem in my test case or there is something wrong with my implementation of the column!() macro. Do let me know where I am going wrong and also if I missed something . :) Co-authored-by: M V V S Manoj Kumar <[email protected]>
2 parents 8c88e8e + d9a5bdd commit 41f402f

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ MacroBuiltin::file (Location invoc_locus, AST::MacroInvocData &invoc)
4949

5050
return AST::ASTFragment ({file_str});
5151
}
52+
AST::ASTFragment
53+
MacroBuiltin::column (Location invoc_locus, AST::MacroInvocData &invoc)
54+
{
55+
auto current_column
56+
= Session::get_instance ().linemap->location_to_column (invoc_locus);
57+
// auto column_no
58+
// = AST::SingleASTNode (make_string (invoc_locus, current_column));
59+
60+
auto column_no = AST::SingleASTNode (std::unique_ptr<AST::Expr> (
61+
new AST::LiteralExpr (std::to_string (current_column), AST::Literal::INT,
62+
PrimitiveCoreType::CORETYPE_U32, {}, invoc_locus)));
63+
64+
return AST::ASTFragment ({column_no});
65+
}
5266
} // namespace Rust

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class MacroBuiltin
6868

6969
static AST::ASTFragment file (Location invoc_locus,
7070
AST::MacroInvocData &invoc);
71+
72+
static AST::ASTFragment column (Location invoc_locus,
73+
AST::MacroInvocData &invoc);
7174
};
7275
} // namespace Rust
7376

gcc/rust/rust-linemap.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class Gcc_linemap : public Linemap
4242

4343
int location_line (Location);
4444

45+
int location_column (Location);
46+
4547
protected:
4648
Location get_predeclared_location ();
4749

@@ -111,6 +113,13 @@ Gcc_linemap::location_line (Location loc)
111113
return LOCATION_LINE (loc.gcc_location ());
112114
}
113115

116+
// Return the column number for a given location.
117+
int
118+
Gcc_linemap::location_column (Location loc)
119+
{
120+
return LOCATION_COLUMN (loc.gcc_location ());
121+
}
122+
114123
// Stop getting locations.
115124

116125
void

gcc/rust/rust-linemap.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class Linemap
7272
// Return the line number for a given location.
7373
virtual int location_line (Location) = 0;
7474

75+
// Return the column number for a given location.
76+
virtual int location_column (Location) = 0;
77+
7578
protected:
7679
// Return a special Location used for predeclared identifiers. This
7780
// Location should be different from that for any actual source
@@ -149,6 +152,12 @@ class Linemap
149152
rust_assert (Linemap::instance_ != NULL);
150153
return Linemap::instance_->location_line (loc);
151154
}
155+
156+
static int location_to_column (Location loc)
157+
{
158+
rust_assert (Linemap::instance_ != NULL);
159+
return Linemap::instance_->location_column (loc);
160+
}
152161
};
153162

154163
#endif // !defined(RUST_LINEMAP_H)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
750750
builtin_macros = {
751751
{"assert", MacroBuiltin::assert},
752752
{"file", MacroBuiltin::file},
753+
{"column", MacroBuiltin::column},
753754
};
754755

755756
auto builtin = builtin_macros.find (macro->get_rule_name ());
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// { dg-output "14\n42\n" }
2+
macro_rules! column {
3+
() => {{}};
4+
}
5+
6+
extern "C" {
7+
fn printf(fmt: *const i8, ...);
8+
}
9+
10+
fn print(s: u32) {
11+
printf("%u\n\0" as *const str as *const i8, s);
12+
}
13+
14+
fn main() -> i32 {
15+
let c0 = column!();
16+
17+
print(c0);
18+
19+
let c1 = column!();
20+
21+
print(c1);
22+
23+
0
24+
}

0 commit comments

Comments
 (0)