From 3e73944e27c161bc49462299001f82119590a8ea Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 28 Jan 2026 12:56:58 -0500 Subject: [PATCH] libgccjit: Add a way to set the language name and the context filename --- gcc/jit/dummy-frontend.cc | 4 ++++ gcc/jit/jit-playback.cc | 9 +++++++++ gcc/jit/jit-recording.cc | 15 +++++++++++++++ gcc/jit/jit-recording.h | 8 ++++++++ gcc/jit/libgccjit.cc | 18 ++++++++++++++++++ gcc/jit/libgccjit.h | 6 ++++++ gcc/jit/libgccjit.map | 6 ++++++ 7 files changed, 66 insertions(+) diff --git a/gcc/jit/dummy-frontend.cc b/gcc/jit/dummy-frontend.cc index 40d94b0d2541b..e84e59a4fd85e 100644 --- a/gcc/jit/dummy-frontend.cc +++ b/gcc/jit/dummy-frontend.cc @@ -246,6 +246,7 @@ static const scoped_attribute_specs *const jit_attribute_table[] = }; char* jit_personality_func_name = NULL; +const char* jit_lang_name = NULL; static tree personality_decl; /* FIXME: This is a hack to preserve trees that we create from the @@ -1086,6 +1087,9 @@ jit_end_diagnostic (diagnostics::text_sink &, static bool jit_langhook_init (void) { + if (jit_lang_name) + lang_hooks.name = jit_lang_name; + jit_gc_root = NULL_TREE; personality_decl = NULL_TREE; gcc_assert (gcc::jit::active_playback_ctxt); diff --git a/gcc/jit/jit-playback.cc b/gcc/jit/jit-playback.cc index b12242c10f3a1..b8f7dbe02f95e 100644 --- a/gcc/jit/jit-playback.cc +++ b/gcc/jit/jit-playback.cc @@ -3319,6 +3319,11 @@ make_fake_args (vec *argvec, ADD_ARG (get_path_c_file ()); ADD_ARG ("-fPIC"); + // Explicitly set the .s file path since the user can customize the path of + // the fake C file via gcc_jit_context_set_filename. + ADD_ARG ("-o"); + ADD_ARG (get_path_s_file ()); + /* Handle int options: */ switch (get_int_option (GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL)) { @@ -3886,6 +3891,10 @@ const char * playback::context:: get_path_c_file () const { + const char *filename = m_recording_ctxt->get_filename (); + if (filename) + return filename; + return m_tempdir->get_path_c_file (); } diff --git a/gcc/jit/jit-recording.cc b/gcc/jit/jit-recording.cc index 57db5464c9472..0dfc5a243f2d4 100644 --- a/gcc/jit/jit-recording.cc +++ b/gcc/jit/jit-recording.cc @@ -638,6 +638,9 @@ recording::context::~context () if (m_owns_last_error_str) if (m_last_error_str != m_first_error_str) free (m_last_error_str); + + if (m_filename) + free (m_filename); } /* Add the given mememto to the list of those tracked by this @@ -1495,6 +1498,18 @@ recording::context::new_case (recording::rvalue *min_value, return result; } +const char * +recording::context::get_filename () +{ + return m_filename; +} + +void +recording::context::set_filename (const char *filename) +{ + m_filename = xstrdup(filename); +} + /* Set the given string option for this context, or add an error if it's not recognized. diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h index c91ac1db4cc52..d782dc031baa4 100644 --- a/gcc/jit/jit-recording.h +++ b/gcc/jit/jit-recording.h @@ -262,6 +262,12 @@ class context : public log_user rvalue *max_value, block *block); + const char * + get_filename (); + + void + set_filename (const char *filename); + void set_str_option (enum gcc_jit_str_option opt, const char *value); @@ -437,6 +443,8 @@ class context : public log_user builtins_manager *m_builtins_manager; // lazily created target_info *m_target_info; + + char *m_filename = nullptr; }; diff --git a/gcc/jit/libgccjit.cc b/gcc/jit/libgccjit.cc index f27d855de0ed9..988f89016c985 100644 --- a/gcc/jit/libgccjit.cc +++ b/gcc/jit/libgccjit.cc @@ -4016,6 +4016,16 @@ gcc_jit_context_set_output_ident (gcc_jit_context *ctxt, ctxt->set_output_ident (output_ident); } +void +gcc_jit_context_set_filename (gcc_jit_context *ctxt, const char *filename) +{ + RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context"); + RETURN_IF_FAIL (filename, ctxt, NULL, "NULL filename"); + JIT_LOG_FUNC (ctxt->get_logger ()); + + ctxt->set_filename (filename); +} + gcc_jit_target_info * gcc_jit_context_get_target_info (gcc_jit_context *ctxt) { @@ -4935,3 +4945,11 @@ gcc_jit_is_lto_supported () return false; } + +extern const char* jit_lang_name; + +void +gcc_jit_set_lang_name (const char *lang_name) +{ + jit_lang_name = lang_name; +} diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h index 2a16834133381..bdcbd8d6dfa74 100644 --- a/gcc/jit/libgccjit.h +++ b/gcc/jit/libgccjit.h @@ -2308,6 +2308,9 @@ gcc_jit_context_set_output_ident (gcc_jit_context *ctxt, #define LIBGCCJIT_HAVE_gcc_jit_context_set_output_ident +extern void +gcc_jit_context_set_filename (gcc_jit_context *ctxt, const char *filename); + /* Add an attribute to a variable. */ extern void gcc_jit_lvalue_add_attribute (gcc_jit_lvalue *variable, @@ -2335,6 +2338,9 @@ gcc_jit_rvalue_set_location (gcc_jit_rvalue *rvalue, extern bool gcc_jit_is_lto_supported (); +extern void +gcc_jit_set_lang_name (const char *lang_name); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map index 3099c95df1e75..ecbf43ddccb71 100644 --- a/gcc/jit/libgccjit.map +++ b/gcc/jit/libgccjit.map @@ -393,3 +393,9 @@ LIBGCCJIT_ABI_46 { global: gcc_jit_is_lto_supported; } LIBGCCJIT_ABI_45; + +LIBGCCJIT_ABI_47 { + global: + gcc_jit_set_lang_name; + gcc_jit_context_set_filename; +} LIBGCCJIT_ABI_46;