[gnome-builder] gcc: add gcc plugin to perform error-format extraction during builds



commit 372e677f0f7a4a9c779db2621e716680f6fa5d16
Author: Christian Hergert <christian hergert me>
Date:   Wed Dec 23 19:39:08 2015 -0800

    gcc: add gcc plugin to perform error-format extraction during builds
    
    This moves the error extraction for diagnostics to a plugin when using GCC
    in automake. It should work for other build systems that log, given that
    they spit gcc diagnostics to stdout/stderr.
    
    Ideally, in the future, this plugin would include things that would make
    us not dependent on clang.

 configure.ac                             |    2 +
 plugins/Makefile.am                      |    1 +
 plugins/gcc/Makefile.am                  |   29 ++++
 plugins/gcc/configure.ac                 |   12 ++
 plugins/gcc/gbp-gcc-build-result-addin.c |  225 ++++++++++++++++++++++++++++++
 plugins/gcc/gbp-gcc-build-result-addin.h |   32 +++++
 plugins/gcc/gbp-gcc-plugin.c             |   30 ++++
 plugins/gcc/gcc.plugin                   |    8 +
 8 files changed, 339 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index e935c4c..de99dd2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -231,6 +231,7 @@ m4_include([plugins/ctags/configure.ac])
 m4_include([plugins/devhelp/configure.ac])
 m4_include([plugins/file-search/configure.ac])
 m4_include([plugins/fpaste/configure.ac])
+m4_include([plugins/gcc/configure.ac])
 m4_include([plugins/git/configure.ac])
 m4_include([plugins/gnome-code-assistance/configure.ac])
 m4_include([plugins/html-completion/configure.ac])
@@ -484,6 +485,7 @@ echo "  Command Bar .......................... : ${enable_command_bar_plugin}"
 echo "  Ctags ................................ : ${enable_ctags_plugin}"
 echo "  Devhelp .............................. : ${enable_devhelp_plugin}"
 echo "  Fpaste.org ........................... : ${enable_fpaste_plugin}"
+echo "  GCC .................................. : ${enable_gcc_plugin}"
 echo "  Git Version Control .................. : ${enable_git_plugin}"
 echo "  Global File Search ................... : ${enable_file_search_plugin}"
 echo "  GNOME Code Assistance ................ : ${enable_gnome_code_assistance_plugin}"
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index c031839..c2bc0fc 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -8,6 +8,7 @@ SUBDIRS = \
        devhelp \
        file-search \
        fpaste \
+       gcc \
        git \
        gnome-code-assistance \
        html-completion \
diff --git a/plugins/gcc/Makefile.am b/plugins/gcc/Makefile.am
new file mode 100644
index 0000000..c0458d0
--- /dev/null
+++ b/plugins/gcc/Makefile.am
@@ -0,0 +1,29 @@
+if ENABLE_GCC_PLUGIN
+
+EXTRA_DIST = $(plugin_DATA)
+
+plugindir = $(libdir)/gnome-builder/plugins
+plugin_LTLIBRARIES = libgcc-plugin.la
+dist_plugin_DATA = gcc.plugin
+
+libgcc_plugin_la_SOURCES = \
+       gbp-gcc-build-result-addin.c \
+       gbp-gcc-build-result-addin.h \
+       gbp-gcc-plugin.c
+
+libgcc_plugin_la_CFLAGS = \
+       $(LIBIDE_CFLAGS) \
+       -I$(top_srcdir)/contrib/egg \
+       -I$(top_srcdir)/libide
+
+libgcc_plugin_la_LDFLAGS = \
+       $(OPTIMIZE_LDFLAGS) \
+       -avoid-version \
+       -export-regex peas_register_types \
+       -module
+
+include $(top_srcdir)/plugins/Makefile.plugin
+
+endif
+
+-include $(top_srcdir)/git.mk
diff --git a/plugins/gcc/configure.ac b/plugins/gcc/configure.ac
new file mode 100644
index 0000000..95db454
--- /dev/null
+++ b/plugins/gcc/configure.ac
@@ -0,0 +1,12 @@
+# --enable-gcc-plugin=yes/no
+AC_ARG_ENABLE([gcc-plugin],
+              [AS_HELP_STRING([--enable-gcc-plugin=@<:@yes/no@:>@],
+                              [Build with support for searching files in global search.])],
+              [enable_gcc_plugin=$enableval],
+              [enable_gcc_plugin=yes])
+
+# for if ENABLE_GCC_PLUGIN in Makefile.am
+AM_CONDITIONAL(ENABLE_GCC_PLUGIN, test x$enable_gcc_plugin != xno)
+
+# Ensure our makefile is generated by autoconf
+AC_CONFIG_FILES([plugins/gcc/Makefile])
diff --git a/plugins/gcc/gbp-gcc-build-result-addin.c b/plugins/gcc/gbp-gcc-build-result-addin.c
new file mode 100644
index 0000000..ca8a289
--- /dev/null
+++ b/plugins/gcc/gbp-gcc-build-result-addin.c
@@ -0,0 +1,225 @@
+/* gbp-gcc-build-result-addin.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * 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/>.
+ */
+
+#include <string.h>
+
+#include "egg-signal-group.h"
+
+#include "gbp-gcc-build-result-addin.h"
+
+
+#define ERROR_FORMAT_REGEX           \
+  "(?<filename>[a-zA-Z0-9\\-\\.]+):" \
+  "(?<line>\\d+):"                   \
+  "(?<column>\\d+): "                \
+  "(?<level>[\\w\\s]+): "            \
+  "(?<message>.*)"
+
+struct _GbpGccBuildResultAddin
+{
+  GObject         parent_instance;
+
+  EggSignalGroup *signals;
+  gchar          *current_dir;
+};
+
+static void build_result_addin_iface_init (IdeBuildResultAddinInterface *iface);
+
+G_DEFINE_TYPE_EXTENDED (GbpGccBuildResultAddin, gbp_gcc_build_result_addin, G_TYPE_OBJECT, 0,
+                        G_IMPLEMENT_INTERFACE (IDE_TYPE_BUILD_RESULT_ADDIN,
+                                               build_result_addin_iface_init))
+
+static GRegex *errfmt;
+
+static IdeDiagnosticSeverity
+parse_severity (const gchar *str)
+{
+  g_autofree gchar *lower = NULL;
+
+  if (str == NULL)
+    return IDE_DIAGNOSTIC_WARNING;
+
+  lower = g_utf8_strdown (str, -1);
+
+  if (strstr (lower, "fatal") != NULL)
+    return IDE_DIAGNOSTIC_FATAL;
+
+  if (strstr (lower, "error") != NULL)
+    return IDE_DIAGNOSTIC_ERROR;
+
+  if (strstr (lower, "warning") != NULL)
+    return IDE_DIAGNOSTIC_WARNING;
+
+  if (strstr (lower, "ignored") != NULL)
+    return IDE_DIAGNOSTIC_IGNORED;
+
+  if (strstr (lower, "deprecated") != NULL)
+    return IDE_DIAGNOSTIC_DEPRECATED;
+
+  if (strstr (lower, "note") != NULL)
+    return IDE_DIAGNOSTIC_NOTE;
+
+  return IDE_DIAGNOSTIC_WARNING;
+}
+
+static IdeDiagnostic *
+create_diagnostic (GbpGccBuildResultAddin *self,
+                   GMatchInfo             *match_info)
+{
+  g_autofree gchar *filename = NULL;
+  g_autofree gchar *line = NULL;
+  g_autofree gchar *column = NULL;
+  g_autofree gchar *message = NULL;
+  g_autofree gchar *level = NULL;
+  g_autoptr(IdeFile) file = NULL;
+  g_autoptr(IdeSourceLocation) location = NULL;
+  IdeDiagnostic *diagnostic;
+  IdeContext *context;
+  struct {
+    gint64 line;
+    gint64 column;
+    IdeDiagnosticSeverity severity;
+  } parsed;
+
+  g_assert (GBP_IS_GCC_BUILD_RESULT_ADDIN (self));
+  g_assert (match_info != NULL);
+
+  filename = g_match_info_fetch_named (match_info, "filename");
+  line = g_match_info_fetch_named (match_info, "line");
+  column = g_match_info_fetch_named (match_info, "column");
+  message = g_match_info_fetch_named (match_info, "message");
+  level = g_match_info_fetch_named (match_info, "level");
+
+  parsed.line = g_ascii_strtoll (line, NULL, 10);
+  if (parsed.line < 1 || parsed.line > G_MAXINT32)
+    return NULL;
+  parsed.line--;
+
+  parsed.column = g_ascii_strtoll (column, NULL, 10);
+  if (parsed.column < 1 || parsed.column > G_MAXINT32)
+    return NULL;
+  parsed.column--;
+
+  parsed.severity = parse_severity (level);
+
+  context = ide_object_get_context (IDE_OBJECT (self));
+
+  if (self->current_dir)
+    {
+      gchar *path;
+
+      path = g_build_filename (self->current_dir, filename, NULL);
+      g_free (filename);
+      filename = path;
+    }
+
+  file = ide_file_new_for_path (context, filename);
+  location = ide_source_location_new (file, parsed.line, parsed.column, 0);
+  diagnostic = ide_diagnostic_new (parsed.severity, message, location);
+
+  return diagnostic;
+}
+
+static void
+gbp_gcc_build_result_addin_log (GbpGccBuildResultAddin *self,
+                                IdeBuildResultLog       log,
+                                const gchar            *message,
+                                IdeBuildResult         *result)
+{
+  GMatchInfo *match_info = NULL;
+  const gchar *enterdir;
+
+  g_assert (GBP_IS_GCC_BUILD_RESULT_ADDIN (self));
+  g_assert (IDE_IS_BUILD_RESULT (result));
+
+  /*
+   * This expects LANG=C, which is defined in the autotools Builder.
+   * Not the most ideal decoupling of logic, but we don't have a whole
+   * lot to work with here.
+   */
+  if ((enterdir = strstr (message, "Entering directory '")))
+    {
+      gsize len;
+
+      enterdir += IDE_LITERAL_LENGTH ("Entering directory '");
+      len = strlen (enterdir);
+
+      if (len > 0)
+        {
+          g_free (self->current_dir);
+          self->current_dir = g_strndup (enterdir, len - 1);
+        }
+    }
+
+  if (g_regex_match (errfmt, message, 0, &match_info))
+    {
+      IdeDiagnostic *diagnostic;
+
+      if ((diagnostic = create_diagnostic (self, match_info)))
+        {
+          ide_build_result_emit_diagnostic (result, diagnostic);
+          ide_diagnostic_unref (diagnostic);
+        }
+    }
+
+  g_match_info_free (match_info);
+}
+
+static void
+gbp_gcc_build_result_addin_class_init (GbpGccBuildResultAddinClass *klass)
+{
+  errfmt = g_regex_new (ERROR_FORMAT_REGEX, G_REGEX_OPTIMIZE | G_REGEX_CASELESS, 0, NULL);
+  g_assert (errfmt != NULL);
+}
+
+static void
+gbp_gcc_build_result_addin_init (GbpGccBuildResultAddin *self)
+{
+  self->signals = egg_signal_group_new (IDE_TYPE_BUILD_RESULT);
+
+  egg_signal_group_connect_object (self->signals,
+                                   "log",
+                                   G_CALLBACK (gbp_gcc_build_result_addin_log),
+                                   self,
+                                   G_CONNECT_SWAPPED);
+}
+
+static void
+gbp_gcc_build_result_addin_load (IdeBuildResultAddin *addin,
+                                 IdeBuildResult      *result)
+{
+  GbpGccBuildResultAddin *self = (GbpGccBuildResultAddin *)addin;
+
+  egg_signal_group_set_target (self->signals, result);
+}
+
+static void
+gbp_gcc_build_result_addin_unload (IdeBuildResultAddin *addin,
+                                   IdeBuildResult      *result)
+{
+  GbpGccBuildResultAddin *self = (GbpGccBuildResultAddin *)addin;
+
+  egg_signal_group_set_target (self->signals, NULL);
+}
+
+static void
+build_result_addin_iface_init (IdeBuildResultAddinInterface *iface)
+{
+  iface->load = gbp_gcc_build_result_addin_load;
+  iface->unload = gbp_gcc_build_result_addin_unload;
+}
diff --git a/plugins/gcc/gbp-gcc-build-result-addin.h b/plugins/gcc/gbp-gcc-build-result-addin.h
new file mode 100644
index 0000000..a225bea
--- /dev/null
+++ b/plugins/gcc/gbp-gcc-build-result-addin.h
@@ -0,0 +1,32 @@
+/* gbp-gcc-build-result-addin.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * 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/>.
+ */
+
+#ifndef GBP_GCC_BUILD_RESULT_ADDIN_H
+#define GBP_GCC_BUILD_RESULT_ADDIN_H
+
+#include <ide.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_GCC_BUILD_RESULT_ADDIN (gbp_gcc_build_result_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpGccBuildResultAddin, gbp_gcc_build_result_addin, GBP, GCC_BUILD_RESULT_ADDIN, 
GObject)
+
+G_END_DECLS
+
+#endif /* GBP_GCC_BUILD_RESULT_ADDIN_H */
diff --git a/plugins/gcc/gbp-gcc-plugin.c b/plugins/gcc/gbp-gcc-plugin.c
new file mode 100644
index 0000000..5504323
--- /dev/null
+++ b/plugins/gcc/gbp-gcc-plugin.c
@@ -0,0 +1,30 @@
+/* gbp-gcc-plugin.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * 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/>.
+ */
+
+#include <ide.h>
+#include <libpeas/peas.h>
+
+#include "gbp-gcc-build-result-addin.h"
+
+void
+peas_register_types (PeasObjectModule *module)
+{
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_BUILD_RESULT_ADDIN,
+                                              GBP_TYPE_GCC_BUILD_RESULT_ADDIN);
+}
diff --git a/plugins/gcc/gcc.plugin b/plugins/gcc/gcc.plugin
new file mode 100644
index 0000000..b84a867
--- /dev/null
+++ b/plugins/gcc/gcc.plugin
@@ -0,0 +1,8 @@
+[Plugin]
+Module=gcc-plugin
+Name=GCC
+Description=Provides various GCC integration hooks
+Authors=Christian Hergert <christian hergert me>
+Copyright=Copyright © 2015 Christian Hergert
+Builtin=true
+Hidden=true


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