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 <mvvsmanojkumar@gmail.com> 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 <mvvsmanojkumar@gmail.com>
This commit is contained in:
commit
41f402f0b1
|
@ -49,4 +49,18 @@ MacroBuiltin::file (Location invoc_locus, AST::MacroInvocData &invoc)
|
|||
|
||||
return AST::ASTFragment ({file_str});
|
||||
}
|
||||
AST::ASTFragment
|
||||
MacroBuiltin::column (Location invoc_locus, AST::MacroInvocData &invoc)
|
||||
{
|
||||
auto current_column
|
||||
= Session::get_instance ().linemap->location_to_column (invoc_locus);
|
||||
// auto column_no
|
||||
// = AST::SingleASTNode (make_string (invoc_locus, current_column));
|
||||
|
||||
auto column_no = AST::SingleASTNode (std::unique_ptr<AST::Expr> (
|
||||
new AST::LiteralExpr (std::to_string (current_column), AST::Literal::INT,
|
||||
PrimitiveCoreType::CORETYPE_U32, {}, invoc_locus)));
|
||||
|
||||
return AST::ASTFragment ({column_no});
|
||||
}
|
||||
} // namespace Rust
|
||||
|
|
|
@ -68,6 +68,9 @@ public:
|
|||
|
||||
static AST::ASTFragment file (Location invoc_locus,
|
||||
AST::MacroInvocData &invoc);
|
||||
|
||||
static AST::ASTFragment column (Location invoc_locus,
|
||||
AST::MacroInvocData &invoc);
|
||||
};
|
||||
} // namespace Rust
|
||||
|
||||
|
|
|
@ -42,6 +42,8 @@ public:
|
|||
|
||||
int location_line (Location);
|
||||
|
||||
int location_column (Location);
|
||||
|
||||
protected:
|
||||
Location get_predeclared_location ();
|
||||
|
||||
|
@ -111,6 +113,13 @@ Gcc_linemap::location_line (Location loc)
|
|||
return LOCATION_LINE (loc.gcc_location ());
|
||||
}
|
||||
|
||||
// Return the column number for a given location.
|
||||
int
|
||||
Gcc_linemap::location_column (Location loc)
|
||||
{
|
||||
return LOCATION_COLUMN (loc.gcc_location ());
|
||||
}
|
||||
|
||||
// Stop getting locations.
|
||||
|
||||
void
|
||||
|
|
|
@ -72,6 +72,9 @@ public:
|
|||
// Return the line number for a given location.
|
||||
virtual int location_line (Location) = 0;
|
||||
|
||||
// Return the column number for a given location.
|
||||
virtual int location_column (Location) = 0;
|
||||
|
||||
protected:
|
||||
// Return a special Location used for predeclared identifiers. This
|
||||
// Location should be different from that for any actual source
|
||||
|
@ -149,6 +152,12 @@ public:
|
|||
rust_assert (Linemap::instance_ != NULL);
|
||||
return Linemap::instance_->location_line (loc);
|
||||
}
|
||||
|
||||
static int location_to_column (Location loc)
|
||||
{
|
||||
rust_assert (Linemap::instance_ != NULL);
|
||||
return Linemap::instance_->location_column (loc);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(RUST_LINEMAP_H)
|
||||
|
|
|
@ -750,6 +750,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
|
|||
builtin_macros = {
|
||||
{"assert", MacroBuiltin::assert},
|
||||
{"file", MacroBuiltin::file},
|
||||
{"column", MacroBuiltin::column},
|
||||
};
|
||||
|
||||
auto builtin = builtin_macros.find (macro->get_rule_name ());
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
// { dg-output "14\n42\n" }
|
||||
macro_rules! column {
|
||||
() => {{}};
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn printf(fmt: *const i8, ...);
|
||||
}
|
||||
|
||||
fn print(s: u32) {
|
||||
printf("%u\n\0" as *const str as *const i8, s);
|
||||
}
|
||||
|
||||
fn main() -> i32 {
|
||||
let c0 = column!();
|
||||
|
||||
print(c0);
|
||||
|
||||
let c1 = column!();
|
||||
|
||||
print(c1);
|
||||
|
||||
0
|
||||
}
|
Loading…
Reference in New Issue