unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Yuan Fu <casouri@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 64830@debbugs.gnu.org, acm@muc.de, stefankangas@gmail.com
Subject: bug#64830: 30.0.50 C++ treesitter mode no coloration
Date: Tue, 10 Sep 2024 22:09:04 -0700	[thread overview]
Message-ID: <904ED5D6-11A2-4B54-A60C-F972AF3E91B4@gmail.com> (raw)
In-Reply-To: <86y14fg95n.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1917 bytes --]

Finally able to address this again.

> On Aug 28, 2024, at 11:01 PM, Eli Zaretskii <eliz@gnu.org> wrote:
> 
>> From: Yuan Fu <casouri@gmail.com>
>> Date: Wed, 28 Aug 2024 21:54:26 -0700
>> Cc: Stefan Kangas <stefankangas@gmail.com>,
>> Alan Mackenzie <acm@muc.de>,
>> 64830@debbugs.gnu.org
>> 
>> Ah, thanks for the review! TIL. Here’s the revised patch.
> 
> A few more nits:
> 
>> +  const char *sym;
>> +  dynlib_addr((void (*) (void)) langfn, &loaded_lang.filename, &sym);
>               ^^
> We leave one space between the function name and the opening paren.
> Also, do you really need the type-cast for langfn?  Do you get
> compiler warnings if you remove the cast?

Oops. And yeah, I actually get an error:

treesit.c:702:16: error: incompatible function pointer types passing 'TSLanguage *(*)(void)' (aka 'struct TSLanguage *(*)(void)') to parameter of type 'void (*)(void)' [-Wincompatible-function-pointer-types]
  702 |   dynlib_addr (langfn, &loaded_lang.filename, &sym);
      |                ^~~~~~
./dynlib.h:39:26: note: passing argument to parameter 'ptr' here
   39 | void dynlib_addr (void (*ptr) (void), const char **file, const char **sym);
      |                          ^

The error seems reasonable to me, I just don’t know other way to suppress it.

>> +DEFUN ("treesit-grammar-location", Ftreesit_grammar_location,
>> +       Streesit_grammar_location,
>> +       1, 1, 0,
>> +       doc: /* Return the path to the grammar file for LANGUAGE.
>                             ^^^^
> "Path" again.  I suggest "absolute file name" instead.
> 
>> +If LANGUAGE isn't loaded yet, load it first.  If the langauge can't be
>> +loaded or the path couldn't be found, return nil.  */)
>          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> I suggest "or the file name couldn't be determined”.

Done. Please take a look at the latest patch, thanks!

Yuan


[-- Attachment #2: grammar-location.patch --]
[-- Type: application/octet-stream, Size: 6704 bytes --]

From 5158271bf6ef6d6c28d88fe21acd85169e10e93b Mon Sep 17 00:00:00 2001
From: Yuan Fu <casouri@gmail.com>
Date: Tue, 27 Aug 2024 22:31:42 -0700
Subject: [PATCH] Add Ftreesit_grammar_location

* src/treesit.c (treesit_loaded_lang): New struct.
(treesit_load_language): Return a struct instead of just the language
object.  The struct contains both the language object and the path to
the shared library.
(Ftreesit_language_available_p, Ftreesit_language_abi_version)
(treesit_ensure_query_compiled, Ftreesit_parser_create): Update
call of treesit_load_language.
(Ftreesit_grammar_location): New function.
---
 src/treesit.c | 68 +++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 55 insertions(+), 13 deletions(-)

diff --git a/src/treesit.c b/src/treesit.c
index 5aedca44489..f2adf92d8c6 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -22,6 +22,7 @@ Copyright (C) 2021-2024 Free Software Foundation, Inc.
 #include <config.h>
 #include "lisp.h"
 #include "buffer.h"
+#include "coding.h"
 
 #include "treesit.h"
 
@@ -491,6 +492,14 @@ treesit_initialize (void)
 \f
 /*** Loading language library  */
 
+struct treesit_loaded_lang
+{
+  /* The language object.  */
+  TSLanguage *lang;
+  /* The path to the shared library.  */
+  const char *filename;
+};
+
 /* Translate a symbol treesit-<lang> to a C name treesit_<lang>.  */
 static void
 treesit_symbol_to_c_name (char *symbol_name)
@@ -575,7 +584,7 @@ treesit_load_language_push_for_each_suffix (Lisp_Object lib_base_name,
 
    If error occurs, return NULL and fill SIGNAL_SYMBOL and SIGNAL_DATA
    with values suitable for xsignal.  */
-static TSLanguage *
+static struct treesit_loaded_lang
 treesit_load_language (Lisp_Object language_symbol,
 		       Lisp_Object *signal_symbol, Lisp_Object *signal_data)
 {
@@ -626,6 +635,7 @@ treesit_load_language (Lisp_Object language_symbol,
   dynlib_handle_ptr handle;
   const char *error;
   Lisp_Object error_list = Qnil;
+  struct treesit_loaded_lang loaded_lang = { NULL, NULL };
 
   tail = path_candidates;
   error = NULL;
@@ -650,7 +660,7 @@ treesit_load_language (Lisp_Object language_symbol,
          mismatch.  */
       *signal_symbol = Qtreesit_load_language_error;
       *signal_data = Fcons (Qnot_found, Fnreverse (error_list));
-      return NULL;
+      return loaded_lang;
     }
 
   /* Load TSLanguage.  */
@@ -672,7 +682,7 @@ treesit_load_language (Lisp_Object language_symbol,
     {
       *signal_symbol = Qtreesit_load_language_error;
       *signal_data = list2 (Qsymbol_error, build_string (error));
-      return NULL;
+      return loaded_lang;
     }
   TSLanguage *lang = (*langfn) ();
 
@@ -685,9 +695,14 @@ treesit_load_language (Lisp_Object language_symbol,
       *signal_symbol = Qtreesit_load_language_error;
       *signal_data = list2 (Qversion_mismatch,
 			    make_fixnum (ts_language_version (lang)));
-      return NULL;
+      return loaded_lang;
     }
-  return lang;
+
+  const char *sym;
+  dynlib_addr ((void (*) void) langfn, &loaded_lang.filename, &sym);
+
+  loaded_lang.lang = lang;
+  return loaded_lang;
 }
 
 DEFUN ("treesit-language-available-p", Ftreesit_language_available_p,
@@ -704,7 +719,9 @@ DEFUN ("treesit-language-available-p", Ftreesit_language_available_p,
   treesit_initialize ();
   Lisp_Object signal_symbol = Qnil;
   Lisp_Object signal_data = Qnil;
-  if (treesit_load_language (language, &signal_symbol, &signal_data) == NULL)
+  struct treesit_loaded_lang loaded_lang
+    = treesit_load_language (language, &signal_symbol, &signal_data);
+  if (loaded_lang.lang == NULL)
     {
       if (NILP (detail))
 	return Qnil;
@@ -750,9 +767,9 @@ DEFUN ("treesit-language-abi-version", Ftreesit_language_abi_version,
     {
       Lisp_Object signal_symbol = Qnil;
       Lisp_Object signal_data = Qnil;
-      TSLanguage *ts_language = treesit_load_language (language,
-						       &signal_symbol,
-						       &signal_data);
+      struct treesit_loaded_lang lang
+	= treesit_load_language (language, &signal_symbol, &signal_data);
+      TSLanguage *ts_language = lang.lang;
       if (ts_language == NULL)
 	return Qnil;
       uint32_t version =  ts_language_version (ts_language);
@@ -760,6 +777,28 @@ DEFUN ("treesit-language-abi-version", Ftreesit_language_abi_version,
     }
 }
 
+DEFUN ("treesit-grammar-location", Ftreesit_grammar_location,
+       Streesit_grammar_location,
+       1, 1, 0,
+       doc: /* Return the absolute file name of the grammar file for LANGUAGE.
+
+If LANGUAGE isn't loaded yet, load it first.  If the langauge can't be
+loaded or the file name couldn't be determined, return nil.  */)
+  (Lisp_Object language)
+{
+  CHECK_SYMBOL (language);
+
+  Lisp_Object signal_symbol = Qnil;
+  Lisp_Object signal_data = Qnil;
+  struct treesit_loaded_lang lang
+    = treesit_load_language (language, &signal_symbol, &signal_data);
+
+  if (!lang.lang || !lang.filename) return Qnil;
+
+  return DECODE_FILE (make_unibyte_string (lang.filename,
+					   strlen (lang.filename)));
+}
+
 \f
 /*** Parsing functions  */
 
@@ -1305,8 +1344,9 @@ treesit_ensure_query_compiled (Lisp_Object query, Lisp_Object *signal_symbol,
   Lisp_Object language = XTS_COMPILED_QUERY (query)->language;
   /* This is the main reason why we compile query lazily: to avoid
      loading languages early.  */
-  TSLanguage *treesit_lang = treesit_load_language (language, signal_symbol,
-						    signal_data);
+  struct treesit_loaded_lang lang
+    = treesit_load_language (language, signal_symbol, signal_data);
+  TSLanguage *treesit_lang = lang.lang;
   if (treesit_lang == NULL)
     return NULL;
 
@@ -1477,8 +1517,9 @@ DEFUN ("treesit-parser-create",
   Lisp_Object signal_symbol = Qnil;
   Lisp_Object signal_data = Qnil;
   TSParser *parser = ts_parser_new ();
-  TSLanguage *lang = treesit_load_language (language, &signal_symbol,
-					    &signal_data);
+  struct treesit_loaded_lang loaded_lang
+    = treesit_load_language (language, &signal_symbol, &signal_data);
+  TSLanguage *lang = loaded_lang.lang;
   if (lang == NULL)
     xsignal (signal_symbol, signal_data);
   /* We check language version when loading a language, so this should
@@ -4275,6 +4316,7 @@ cons (REGEXP . FN), which is a combination of a regexp and a predicate
   defsubr (&Streesit_language_available_p);
   defsubr (&Streesit_library_abi_version);
   defsubr (&Streesit_language_abi_version);
+  defsubr (&Streesit_grammar_location);
 
   defsubr (&Streesit_parser_p);
   defsubr (&Streesit_node_p);
-- 
2.39.5 (Apple Git-151)


[-- Attachment #3: Type: text/plain, Size: 2 bytes --]




  reply	other threads:[~2024-09-11  5:09 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-24  9:13 bug#64830: 29.1; C++ treesitter mode no coloration David Come
2023-07-24 12:32 ` Eli Zaretskii
2024-08-14 17:35 ` bug#64830: 30.0.50 " Alan Mackenzie
2024-08-15  5:25   ` Eli Zaretskii
2024-08-16 16:44     ` Alan Mackenzie
2024-08-16 17:40       ` Eli Zaretskii
2024-08-16 17:45         ` Eli Zaretskii
2024-08-16 18:06           ` Alan Mackenzie
2024-08-16 18:27             ` Eli Zaretskii
2024-08-20  3:46               ` Yuan Fu
2024-08-24 18:35                 ` Yuan Fu
2024-08-24 19:38                   ` Alan Mackenzie
2024-08-24 20:43                     ` Yuan Fu
2024-08-25  2:19                       ` Alan Mackenzie
2024-08-25  4:54                         ` Eli Zaretskii
2024-08-25 12:08                           ` Alan Mackenzie
2024-08-25 12:17                             ` Eli Zaretskii
2024-08-25 22:40                         ` Yuan Fu
2024-08-26 17:25                           ` Alan Mackenzie
2024-08-26 17:51                             ` Eli Zaretskii
2024-08-26 19:50                               ` Alan Mackenzie
2024-08-26 22:25                                 ` Stefan Kangas
2024-08-27  1:58                                   ` Yuan Fu
2024-08-27 12:09                                     ` Eli Zaretskii
2024-08-28  5:36                                       ` Yuan Fu
2024-08-28 12:33                                         ` Eli Zaretskii
2024-08-29  4:54                                           ` Yuan Fu
2024-08-29  6:01                                             ` Eli Zaretskii
2024-09-11  5:09                                               ` Yuan Fu [this message]
2024-09-11 12:09                                                 ` Eli Zaretskii
2024-09-12  8:06                                                   ` Yuan Fu
2024-08-27 12:01                                 ` Eli Zaretskii
2024-08-27 11:03                             ` Eli Zaretskii

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=904ED5D6-11A2-4B54-A60C-F972AF3E91B4@gmail.com \
    --to=casouri@gmail.com \
    --cc=64830@debbugs.gnu.org \
    --cc=acm@muc.de \
    --cc=eliz@gnu.org \
    --cc=stefankangas@gmail.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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