summaryrefslogtreecommitdiffstats
path: root/tools/profiler/rust-api/macros
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /tools/profiler/rust-api/macros
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/profiler/rust-api/macros')
-rw-r--r--tools/profiler/rust-api/macros/Cargo.toml13
-rw-r--r--tools/profiler/rust-api/macros/src/lib.rs65
2 files changed, 78 insertions, 0 deletions
diff --git a/tools/profiler/rust-api/macros/Cargo.toml b/tools/profiler/rust-api/macros/Cargo.toml
new file mode 100644
index 0000000000..b8bd9910dc
--- /dev/null
+++ b/tools/profiler/rust-api/macros/Cargo.toml
@@ -0,0 +1,13 @@
+[package]
+name = "profiler-macros"
+version = "0.1.0"
+authors = ["The Mozilla Project Developers"]
+edition = "2018"
+license = "MPL-2.0"
+
+[lib]
+proc-macro = true
+
+[dependencies]
+syn = "1"
+quote = "1.0"
diff --git a/tools/profiler/rust-api/macros/src/lib.rs b/tools/profiler/rust-api/macros/src/lib.rs
new file mode 100644
index 0000000000..48617b758e
--- /dev/null
+++ b/tools/profiler/rust-api/macros/src/lib.rs
@@ -0,0 +1,65 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#![deny(warnings)]
+
+//! A procedural macro as a syntactical sugar to `gecko_profiler_label!` macro.
+//! You can use this macro on top of functions to automatically append the
+//! label frame to the function.
+//!
+//! Example usage:
+//! ```rust
+//! #[gecko_profiler_fn_label(DOM)]
+//! fn foo(bar: u32) -> u32 {
+//! bar
+//! }
+//!
+//! #[gecko_profiler_fn_label(Javascript, IonMonkey)]
+//! pub fn bar(baz: i8) -> i8 {
+//! baz
+//! }
+//! ```
+//!
+//! See the documentation of `gecko_profiler_label!` macro to learn more about
+//! its parameters.
+
+extern crate proc_macro;
+
+use proc_macro::TokenStream;
+use quote::quote;
+use syn::{parse_macro_input, AttributeArgs, ItemFn};
+
+#[proc_macro_attribute]
+pub fn gecko_profiler_fn_label(attrs: TokenStream, input: TokenStream) -> TokenStream {
+ let attr_args = parse_macro_input!(attrs as AttributeArgs);
+ let input = parse_macro_input!(input as ItemFn);
+
+ if attr_args.is_empty() || attr_args.len() > 2 {
+ panic!("Expected one or two arguments as ProfilingCategory or ProfilingCategoryPair but {} arguments provided!", attr_args.len());
+ }
+
+ let category_name = &attr_args[0];
+ // Try to get the subcategory if possible. Otherwise, use `None`.
+ let subcategory_if_provided = match attr_args.get(1) {
+ Some(subcategory) => quote!(, #subcategory),
+ None => quote!(),
+ };
+
+ let ItemFn {
+ attrs,
+ vis,
+ sig,
+ block,
+ } = input;
+ let stmts = &block.stmts;
+
+ let new_fn = quote! {
+ #(#attrs)* #vis #sig {
+ gecko_profiler_label!(#category_name#subcategory_if_provided);
+ #(#stmts)*
+ }
+ };
+
+ new_fn.into()
+}