[gnome-builder] gdiagnose: add plugin for GObject diagnostics



commit c7d1c3892955202ab096e4ab122ac2f1c6e91096
Author: Christian Hergert <chergert redhat com>
Date:   Thu Feb 25 16:57:11 2021 -0800

    gdiagnose: add plugin for GObject diagnostics
    
    Currently this just does checking of chainups using swilmet's gnome-c-utils
    chainup checker. But the idea would be to add more here as we go.

 meson_options.txt                                  |   1 +
 src/plugins/gdiagnose/gbp-gdiagnose-chainups.c     | 198 +++++++++++++++++++++
 src/plugins/gdiagnose/gbp-gdiagnose-chainups.h     |  31 ++++
 .../gdiagnose/gbp-gdiagnose-diagnostic-provider.c  |  94 ++++++++++
 .../gdiagnose/gbp-gdiagnose-diagnostic-provider.h  |  31 ++++
 src/plugins/gdiagnose/gdiagnose-plugin.c           |  34 ++++
 src/plugins/gdiagnose/gdiagnose.gresource.xml      |   6 +
 src/plugins/gdiagnose/gdiagnose.plugin             |  10 ++
 src/plugins/gdiagnose/meson.build                  |  17 ++
 src/plugins/meson.build                            |   1 +
 10 files changed, 423 insertions(+)
---
diff --git a/meson_options.txt b/meson_options.txt
index e2c0dcd62..315274648 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -36,6 +36,7 @@ option('plugin_eslint', type: 'boolean')
 option('plugin_file_search', type: 'boolean')
 option('plugin_flatpak', type: 'boolean')
 option('plugin_gdb', type: 'boolean')
+option('plugin_gdiagnose', type: 'boolean')
 option('plugin_gettext', type: 'boolean')
 option('plugin_git', type: 'boolean')
 option('plugin_gjs_symbols', type: 'boolean')
diff --git a/src/plugins/gdiagnose/gbp-gdiagnose-chainups.c b/src/plugins/gdiagnose/gbp-gdiagnose-chainups.c
new file mode 100644
index 000000000..ba2f3d013
--- /dev/null
+++ b/src/plugins/gdiagnose/gbp-gdiagnose-chainups.c
@@ -0,0 +1,198 @@
+/*
+ * This file is part of gnome-c-utils.
+ *
+ * Copyright © 2016, 2017 Sébastien Wilmet <swilmet gnome org>
+ *
+ * gnome-c-utils is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * gnome-c-utils is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gnome-c-utils.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * Basic check of GObject virtual function chain-ups.
+ *
+ * Usage: gcu-check-chain-ups <file.c>
+ *
+ * For a less verbose output, redirect stdout to /dev/null. The warnings/errors
+ * are printed on stderr.
+ *
+ * The script searches where a vfunc is chained up, by looking at the following
+ * pattern, allowing spaces around the parenthesis and after '->':
+ *
+ *     _parent_class)->vfunc_name
+ *
+ * It extracts "vfunc_name". Then the script searches the function name
+ * containing the chain-up, and checks that the function name has "vfunc_name"
+ * for suffix.
+ *
+ * For example in this code:
+ *
+ * static void
+ * my_class_finalize (GObject *object)
+ * {
+ *   ...
+ *
+ *   G_OBJECT_CLASS (gtk_source_file_loader_parent_class)->dispose (object);
+ * }
+ *
+ * "my_class_finalize" doesn't have the "dispose" suffix, so it'll print a
+ * message on stderr.
+ */
+
+/* TODO A possible improvement is to search the function name
+ * ("my_class_finalize" in the above example) that is present in the pattern:
+ *
+ * ->foo = function_name;
+ *
+ * And check that "foo" is the same as vfunc_name, the chained-up vfunc.
+ *
+ * Of course using a real static analysis tool for the C language would be
+ * better.
+ */
+
+#include "config.h"
+
+#include <gtksourceview/gtksource.h>
+#include <stdlib.h>
+
+#include "gbp-gdiagnose-chainups.h"
+
+static gchar *
+get_function_name (const GtkTextIter *_iter)
+{
+  GtkTextIter iter = *_iter;
+
+  while (gtk_text_iter_backward_line (&iter))
+    {
+      gunichar c;
+
+      c = gtk_text_iter_get_char (&iter);
+      if (g_unichar_isalpha (c) || c == '_')
+        {
+          GtkTextIter function_name_start;
+          GtkTextIter function_name_end;
+
+          function_name_start = iter;
+          function_name_end = iter;
+
+          while (gtk_text_iter_forward_char (&function_name_end))
+            {
+              gunichar end_c;
+
+              end_c = gtk_text_iter_get_char (&function_name_end);
+              if (!g_unichar_isalnum (end_c) && end_c != '_')
+                break;
+            }
+
+          /* A goto label, not a function name. */
+          if (gtk_text_iter_get_char (&function_name_end) == ':')
+            continue;
+
+          return gtk_text_iter_get_text (&function_name_start, &function_name_end);
+        }
+    }
+
+  return NULL;
+}
+
+static void
+check_chain_up (GtkSourceBuffer   *buffer,
+                const GtkTextIter *vfunc_start,
+                GFile             *file,
+                const gchar       *basename,
+                IdeDiagnostics    *diagnostics)
+{
+  gchar *function_name;
+  GtkTextIter vfunc_end;
+  gchar *vfunc;
+
+  function_name = get_function_name (vfunc_start);
+  if (function_name == NULL)
+    return;
+
+  if (!gtk_text_iter_starts_word (vfunc_start))
+    return;
+
+  vfunc_end = *vfunc_start;
+  while (TRUE)
+    {
+      if (gtk_text_iter_get_char (&vfunc_end) == '_')
+        gtk_text_iter_forward_char (&vfunc_end);
+      else if (gtk_text_iter_starts_word (&vfunc_end))
+        gtk_text_iter_forward_word_end (&vfunc_end);
+      else
+        break;
+    }
+
+  vfunc = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (buffer),
+                                    vfunc_start,
+                                    &vfunc_end,
+                                    FALSE);
+
+  if (!g_str_has_suffix (function_name, vfunc))
+    {
+      guint begin_line = gtk_text_iter_get_line (vfunc_start);
+      guint begin_line_offset = gtk_text_iter_get_line_offset (vfunc_start);
+      g_autoptr(IdeDiagnostic) diagnostic = NULL;
+      g_autoptr(IdeLocation) location = NULL;
+      g_autofree gchar *message = NULL;
+
+      message = g_strdup_printf ("%s() chains up to %s which may be incorrect.",
+                                 function_name, vfunc);
+      location = ide_location_new (file, begin_line, begin_line_offset);
+      diagnostic = ide_diagnostic_new (IDE_DIAGNOSTIC_WARNING, message, location);
+
+      ide_diagnostics_add (diagnostics, diagnostic);
+    }
+
+  g_free (function_name);
+  g_free (vfunc);
+}
+
+void
+gbp_gdiagnose_check_chainups (GtkSourceBuffer *buffer,
+                              GFile           *file,
+                              const char      *basename,
+                              IdeDiagnostics  *diagnostics)
+{
+  GtkSourceSearchSettings *search_settings;
+  GtkSourceSearchContext *search_context;
+  GtkTextIter iter;
+  GtkTextIter match_end;
+
+  g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
+  g_return_if_fail (basename != NULL);
+  g_return_if_fail (diagnostics != NULL);
+
+  search_settings = gtk_source_search_settings_new ();
+  gtk_source_search_settings_set_regex_enabled (search_settings, TRUE);
+
+  /* Search "_parent_class)->", with possible spaces. */
+  gtk_source_search_settings_set_search_text (search_settings, "_parent_class\\s*\\)\\s*->\\s*");
+
+  search_context = gtk_source_search_context_new (buffer, search_settings);
+
+  gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &iter);
+
+  while (gtk_source_search_context_forward (search_context,
+                                            &iter,
+                                            NULL,
+                                            &match_end,
+                                            NULL))
+    {
+      check_chain_up (buffer, &match_end, file, basename, diagnostics);
+      iter = match_end;
+    }
+
+  g_object_unref (search_settings);
+  g_object_unref (search_context);
+}
diff --git a/src/plugins/gdiagnose/gbp-gdiagnose-chainups.h b/src/plugins/gdiagnose/gbp-gdiagnose-chainups.h
new file mode 100644
index 000000000..668b2635d
--- /dev/null
+++ b/src/plugins/gdiagnose/gbp-gdiagnose-chainups.h
@@ -0,0 +1,31 @@
+/*
+ * This file is part of gnome-c-utils.
+ *
+ * Copyright © 2016, 2017 Sébastien Wilmet <swilmet gnome org>
+ *
+ * gnome-c-utils is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * gnome-c-utils is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gnome-c-utils.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <libide-code.h>
+
+G_BEGIN_DECLS
+
+void gbp_gdiagnose_check_chainups (GtkSourceBuffer *buffer,
+                                   GFile           *file,
+                                   const char      *basename,
+                                   IdeDiagnostics  *diagnostics);
+
+G_END_DECLS
diff --git a/src/plugins/gdiagnose/gbp-gdiagnose-diagnostic-provider.c 
b/src/plugins/gdiagnose/gbp-gdiagnose-diagnostic-provider.c
new file mode 100644
index 000000000..0b1060981
--- /dev/null
+++ b/src/plugins/gdiagnose/gbp-gdiagnose-diagnostic-provider.c
@@ -0,0 +1,94 @@
+/* gbp-gdiagnose-diagnostic-provider.c
+ *
+ * Copyright 2021 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-gdiagnose-diagnostic-provider"
+
+#include "gbp-gdiagnose-diagnostic-provider.h"
+#include "gbp-gdiagnose-chainups.h"
+
+struct _GbpGdiagnoseDiagnosticProvider
+{
+  IdeObject parent_instance;
+};
+
+static void
+gbp_gdiagnose_diagnostic_provider_diagnose_async (IdeDiagnosticProvider *provider,
+                                                  GFile                 *file,
+                                                  GBytes                *contents,
+                                                  const char            *lang_id,
+                                                  GCancellable          *cancellable,
+                                                  GAsyncReadyCallback    callback,
+                                                  gpointer               user_data)
+{
+  g_autoptr(IdeDiagnostics) diagnostics = NULL;
+  g_autoptr(GtkSourceBuffer) buffer = NULL;
+  g_autoptr(IdeTask) task = NULL;
+  g_autofree gchar *name = NULL;
+
+  g_assert (GBP_IS_GDIAGNOSE_DIAGNOSTIC_PROVIDER (provider));
+  g_assert (G_IS_FILE (file));
+  g_assert (contents != NULL);
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = ide_task_new (provider, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, gbp_gdiagnose_diagnostic_provider_diagnose_async);
+
+  diagnostics = ide_diagnostics_new ();
+
+  if (g_bytes_get_size (contents) > 0)
+    {
+      name = g_file_get_basename (file);
+      buffer = gtk_source_buffer_new (NULL);
+      gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer),
+                                g_bytes_get_data (contents, NULL),
+                                g_bytes_get_size (contents));
+      gbp_gdiagnose_check_chainups (buffer, file, name, diagnostics);
+    }
+
+  ide_task_return_pointer (task, g_steal_pointer (&diagnostics), g_object_unref);
+}
+
+static IdeDiagnostics *
+gbp_gdiagnose_diagnostic_provider_diagnose_finish (IdeDiagnosticProvider  *provider,
+                                                   GAsyncResult           *result,
+                                                   GError                **error)
+{
+  return ide_task_propagate_pointer (IDE_TASK (result), error);
+}
+
+static void
+provider_iface_init (IdeDiagnosticProviderInterface *iface)
+{
+  iface->diagnose_async = gbp_gdiagnose_diagnostic_provider_diagnose_async;
+  iface->diagnose_finish = gbp_gdiagnose_diagnostic_provider_diagnose_finish;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GbpGdiagnoseDiagnosticProvider, gbp_gdiagnose_diagnostic_provider, IDE_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (IDE_TYPE_DIAGNOSTIC_PROVIDER, provider_iface_init))
+
+static void
+gbp_gdiagnose_diagnostic_provider_class_init (GbpGdiagnoseDiagnosticProviderClass *klass)
+{
+}
+
+static void
+gbp_gdiagnose_diagnostic_provider_init (GbpGdiagnoseDiagnosticProvider *self)
+{
+}
diff --git a/src/plugins/gdiagnose/gbp-gdiagnose-diagnostic-provider.h 
b/src/plugins/gdiagnose/gbp-gdiagnose-diagnostic-provider.h
new file mode 100644
index 000000000..962dad60a
--- /dev/null
+++ b/src/plugins/gdiagnose/gbp-gdiagnose-diagnostic-provider.h
@@ -0,0 +1,31 @@
+/* gbp-gdiagnose-diagnostic-provider.h
+ *
+ * Copyright 2021 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <libide-code.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_GDIAGNOSE_DIAGNOSTIC_PROVIDER (gbp_gdiagnose_diagnostic_provider_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpGdiagnoseDiagnosticProvider, gbp_gdiagnose_diagnostic_provider, GBP, 
GDIAGNOSE_DIAGNOSTIC_PROVIDER, IdeObject)
+
+G_END_DECLS
diff --git a/src/plugins/gdiagnose/gdiagnose-plugin.c b/src/plugins/gdiagnose/gdiagnose-plugin.c
new file mode 100644
index 000000000..94522bcb1
--- /dev/null
+++ b/src/plugins/gdiagnose/gdiagnose-plugin.c
@@ -0,0 +1,34 @@
+/* gdiagnose-plugin.c
+ *
+ * Copyright 2021 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include "config.h"
+
+#include <libpeas/peas.h>
+#include <libide-code.h>
+
+#include "gbp-gdiagnose-diagnostic-provider.h"
+
+_IDE_EXTERN void
+_ide_gdiagnose_register_types (PeasObjectModule *module)
+{
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_DIAGNOSTIC_PROVIDER,
+                                              GBP_TYPE_GDIAGNOSE_DIAGNOSTIC_PROVIDER);
+}
diff --git a/src/plugins/gdiagnose/gdiagnose.gresource.xml b/src/plugins/gdiagnose/gdiagnose.gresource.xml
new file mode 100644
index 000000000..80ba8da8b
--- /dev/null
+++ b/src/plugins/gdiagnose/gdiagnose.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/plugins/gdiagnose">
+    <file>gdiagnose.plugin</file>
+  </gresource>
+</gresources>
diff --git a/src/plugins/gdiagnose/gdiagnose.plugin b/src/plugins/gdiagnose/gdiagnose.plugin
new file mode 100644
index 000000000..8193b4717
--- /dev/null
+++ b/src/plugins/gdiagnose/gdiagnose.plugin
@@ -0,0 +1,10 @@
+[Plugin]
+Authors=Christian Hergert <chergert redhat com>
+Builtin=true
+Copyright=Copyright © 2021 Christian Hergert
+Depends=editor;
+Description=GObject Diagnostics
+Embedded=_ide_gdiagnose_register_types
+Module=gdiagnose
+Name=GObject Diagnostics
+X-Diagnostic-Provider-Languages=c,chdr
diff --git a/src/plugins/gdiagnose/meson.build b/src/plugins/gdiagnose/meson.build
new file mode 100644
index 000000000..15c0e3f47
--- /dev/null
+++ b/src/plugins/gdiagnose/meson.build
@@ -0,0 +1,17 @@
+if get_option('plugin_gdiagnose')
+
+plugins_sources += files([
+  'gdiagnose-plugin.c',
+  'gbp-gdiagnose-chainups.c',
+  'gbp-gdiagnose-diagnostic-provider.c',
+])
+
+plugin_gdiagnose_resources = gnome.compile_resources(
+  'gdiagnose-resources',
+  'gdiagnose.gresource.xml',
+  c_name: 'gbp_gdiagnose',
+)
+
+plugins_sources += plugin_gdiagnose_resources
+
+endif
diff --git a/src/plugins/meson.build b/src/plugins/meson.build
index 4143294ba..b8db03be4 100644
--- a/src/plugins/meson.build
+++ b/src/plugins/meson.build
@@ -71,6 +71,7 @@ subdir('file-search')
 subdir('find-other-file')
 subdir('gcc')
 subdir('gdb')
+subdir('gdiagnose')
 subdir('gettext')
 subdir('git')
 subdir('glade')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]