From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 27558385275C; Wed, 8 Jun 2022 12:18:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 27558385275C Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Added column!() macro X-Act-Checkin: gcc X-Git-Author: M V V S Manoj Kumar X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: b82408fd6a17762fcb6fc7b5efe9307a3e59f8b7 X-Git-Newrev: d9a5bddb4c64268f2411a2b317872fe0c26284c3 Message-Id: <20220608121850.27558385275C@sourceware.org> Date: Wed, 8 Jun 2022 12:18:50 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:18:50 -0000 https://gcc.gnu.org/g:d9a5bddb4c64268f2411a2b317872fe0c26284c3 commit d9a5bddb4c64268f2411a2b317872fe0c26284c3 Author: M V V S Manoj Kumar Date: Thu Mar 10 12:38:29 2022 +0530 Added column!() macro Addresses issue #979 1) Added the column!() macro using the LOCATION_COLUMN() from gcc_linemap 2) Added relevent test cases Signed-off-by : M V V S Manoj Kumar Diff: --- gcc/rust/expand/rust-macro-builtins.cc | 14 +++++++++++++ gcc/rust/expand/rust-macro-builtins.h | 3 +++ gcc/rust/rust-linemap.cc | 9 ++++++++ gcc/rust/rust-linemap.h | 9 ++++++++ gcc/rust/util/rust-hir-map.cc | 1 + .../rust/execute/torture/builtin_macros3.rs | 24 ++++++++++++++++++++++ 6 files changed, 60 insertions(+) diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc index ce0be92dad0..c33a2e86f88 100644 --- a/gcc/rust/expand/rust-macro-builtins.cc +++ b/gcc/rust/expand/rust-macro-builtins.cc @@ -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 ( + new AST::LiteralExpr (std::to_string (current_column), AST::Literal::INT, + PrimitiveCoreType::CORETYPE_U32, {}, invoc_locus))); + + return AST::ASTFragment ({column_no}); +} } // namespace Rust diff --git a/gcc/rust/expand/rust-macro-builtins.h b/gcc/rust/expand/rust-macro-builtins.h index ee42fe1dcc6..d9461295c08 100644 --- a/gcc/rust/expand/rust-macro-builtins.h +++ b/gcc/rust/expand/rust-macro-builtins.h @@ -31,6 +31,9 @@ public: static AST::ASTFragment file (Location invoc_locus, AST::MacroInvocData &invoc); + + static AST::ASTFragment column (Location invoc_locus, + AST::MacroInvocData &invoc); }; } // namespace Rust diff --git a/gcc/rust/rust-linemap.cc b/gcc/rust/rust-linemap.cc index 637d74965dd..b32a965a4aa 100644 --- a/gcc/rust/rust-linemap.cc +++ b/gcc/rust/rust-linemap.cc @@ -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 diff --git a/gcc/rust/rust-linemap.h b/gcc/rust/rust-linemap.h index c8472a17672..0ba95f87575 100644 --- a/gcc/rust/rust-linemap.h +++ b/gcc/rust/rust-linemap.h @@ -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) diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 9190bd92a43..5b0417b4549 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -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 ()); diff --git a/gcc/testsuite/rust/execute/torture/builtin_macros3.rs b/gcc/testsuite/rust/execute/torture/builtin_macros3.rs new file mode 100644 index 00000000000..ed117803c07 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/builtin_macros3.rs @@ -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 +} \ No newline at end of file