public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] Added column!() macro
Date: Wed,  8 Jun 2022 12:18:50 +0000 (GMT)	[thread overview]
Message-ID: <20220608121850.27558385275C@sourceware.org> (raw)

https://gcc.gnu.org/g:d9a5bddb4c64268f2411a2b317872fe0c26284c3

commit d9a5bddb4c64268f2411a2b317872fe0c26284c3
Author: M V V S Manoj Kumar <mvvsmanojkumar@gmail.com>
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 <mvvsmanojkumar@gmail.com>

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<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
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


                 reply	other threads:[~2022-06-08 12:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220608121850.27558385275C@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).