[gnome-builder] clang: add autocleanups and utils header-only helpers



commit 3181319fb0769bb7974869290e4d5ad976100576
Author: Christian Hergert <chergert redhat com>
Date:   Thu Apr 19 15:58:43 2018 -0700

    clang: add autocleanups and utils header-only helpers

 src/plugins/clang/ide-clang-autocleanups.h       | 69 ++++++++++++++++++++
 src/plugins/clang/ide-clang-code-index-entries.c | 68 ++------------------
 src/plugins/clang/ide-clang-private.h            | 11 +---
 src/plugins/clang/ide-clang-service.c            | 28 ---------
 src/plugins/clang/ide-clang-util.h               | 80 ++++++++++++++++++++++++
 5 files changed, 154 insertions(+), 102 deletions(-)
---
diff --git a/src/plugins/clang/ide-clang-autocleanups.h b/src/plugins/clang/ide-clang-autocleanups.h
new file mode 100644
index 000000000..ae3c8ee53
--- /dev/null
+++ b/src/plugins/clang/ide-clang-autocleanups.h
@@ -0,0 +1,69 @@
+/* ide-clang-autocleanups.h
+ *
+ * Copyright 2018 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/>.
+ */
+
+#pragma once
+
+#include <clang-c/Index.h>
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+static inline void
+_ide_clang_dispose_string (CXString *str)
+{
+  if (str != NULL && str->data != NULL)
+    clang_disposeString (*str);
+}
+
+static inline void
+_ide_clang_dispose_diagnostic (CXDiagnostic *diag)
+{
+  if (diag != NULL)
+    clang_disposeDiagnostic (diag);
+}
+
+static inline void
+_ide_clang_dispose_index (CXIndex *idx)
+{
+  if (idx != NULL && *idx != NULL)
+    clang_disposeIndex (*idx);
+}
+
+static inline void
+_ide_clang_dispose_unit (CXTranslationUnit *unit)
+{
+  if (unit != NULL && *unit != NULL)
+    clang_disposeTranslationUnit (*unit);
+}
+
+static inline void
+_ide_clang_dispose_cursor (CXCursor *cursor)
+{
+  /* Only use when g_slice_dup()'ing cursor (which means you should
+   * be using *cursor to dereference and pass to Clang API.
+   */
+  g_slice_free (CXCursor, cursor);
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (CXString,          _ide_clang_dispose_string)
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (CXIndex,           _ide_clang_dispose_index)
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (CXTranslationUnit, _ide_clang_dispose_unit)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC    (CXDiagnostic,      _ide_clang_dispose_diagnostic)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC    (CXCursor,          _ide_clang_dispose_cursor)
+
+G_END_DECLS
diff --git a/src/plugins/clang/ide-clang-code-index-entries.c 
b/src/plugins/clang/ide-clang-code-index-entries.c
index 9d2fa92a1..41885df0d 100644
--- a/src/plugins/clang/ide-clang-code-index-entries.c
+++ b/src/plugins/clang/ide-clang-code-index-entries.c
@@ -20,6 +20,7 @@
 
 #include "ide-clang-code-index-entries.h"
 #include "ide-clang-private.h"
+#include "ide-clang-util.h"
 
  /*
   * This is an implementation of IdeCodeIndexEntries. This will have a TU and
@@ -60,67 +61,6 @@ struct _IdeClangCodeIndexEntries
   guint has_run : 1;
 };
 
-static void
-cx_cursor_free (CXCursor *cursor)
-{
-  g_slice_free (CXCursor, cursor);
-}
-
-G_DEFINE_AUTOPTR_CLEANUP_FUNC (CXCursor, cx_cursor_free)
-
-static IdeSymbolKind
-translate_kind (enum CXCursorKind cursor_kind)
-{
-  switch ((int)cursor_kind)
-    {
-    case CXCursor_StructDecl:
-      return IDE_SYMBOL_STRUCT;
-
-    case CXCursor_UnionDecl:
-      return IDE_SYMBOL_UNION;
-
-    case CXCursor_ClassDecl:
-      return IDE_SYMBOL_CLASS;
-
-    case CXCursor_EnumDecl:
-      return IDE_SYMBOL_ENUM;
-
-    case CXCursor_FieldDecl:
-      return IDE_SYMBOL_FIELD;
-
-    case CXCursor_EnumConstantDecl:
-      return IDE_SYMBOL_ENUM_VALUE;
-
-    case CXCursor_FunctionDecl:
-      return IDE_SYMBOL_FUNCTION;
-
-    case CXCursor_CXXMethod:
-      return IDE_SYMBOL_METHOD;
-
-    case CXCursor_VarDecl:
-    case CXCursor_ParmDecl:
-      return IDE_SYMBOL_VARIABLE;
-
-    case CXCursor_TypedefDecl:
-    case CXCursor_NamespaceAlias:
-    case CXCursor_TypeAliasDecl:
-      return IDE_SYMBOL_ALIAS;
-
-    case CXCursor_Namespace:
-      return IDE_SYMBOL_NAMESPACE;
-
-    case CXCursor_FunctionTemplate:
-    case CXCursor_ClassTemplate:
-      return IDE_SYMBOL_TEMPLATE;
-
-    case CXCursor_MacroDefinition:
-      return IDE_SYMBOL_MACRO;
-
-    default:
-      return IDE_SYMBOL_NONE;
-    }
-}
-
 static const gchar *
 get_symbol_prefix (IdeSymbolKind kind)
 {
@@ -289,7 +229,7 @@ ide_clang_code_index_entries_real_get_next_entry (IdeClangCodeIndexEntries *self
       cursor_kind = clang_getCursorKind (temp);
     }
 
-  kind = translate_kind (cursor_kind);
+  kind = ide_clang_translate_kind (cursor_kind);
   prefix = get_symbol_prefix (kind);
   name = g_strconcat (prefix, cname, NULL);
 
@@ -427,10 +367,10 @@ ide_clang_code_index_entries_finalize (GObject *object)
 {
   IdeClangCodeIndexEntries *self = (IdeClangCodeIndexEntries *)object;
 
-  g_queue_foreach (&self->decl_cursors, (GFunc)cx_cursor_free, NULL);
+  g_queue_foreach (&self->decl_cursors, (GFunc)_ide_clang_dispose_cursor, NULL);
   g_queue_clear (&self->decl_cursors);
 
-  g_queue_foreach (&self->cursors, (GFunc)cx_cursor_free, NULL);
+  g_queue_foreach (&self->cursors, (GFunc)_ide_clang_dispose_cursor, NULL);
   g_queue_clear (&self->cursors);
 
   g_clear_pointer (&self->unit, clang_disposeTranslationUnit);
diff --git a/src/plugins/clang/ide-clang-private.h b/src/plugins/clang/ide-clang-private.h
index 5599ae5a2..50bd41bff 100644
--- a/src/plugins/clang/ide-clang-private.h
+++ b/src/plugins/clang/ide-clang-private.h
@@ -21,6 +21,7 @@
 #include <clang-c/Index.h>
 #include <ide.h>
 
+#include "ide-clang-autocleanups.h"
 #include "ide-clang-service.h"
 #include "ide-clang-symbol-node.h"
 #include "ide-clang-translation-unit.h"
@@ -32,10 +33,6 @@ IdeClangTranslationUnit *_ide_clang_translation_unit_new     (IdeContext
                                                               GFile              *file,
                                                               IdeHighlightIndex  *index,
                                                               gint64              serial);
-void                     _ide_clang_dispose_diagnostic       (CXDiagnostic       *diag);
-void                     _ide_clang_dispose_index            (CXIndex            *index);
-void                     _ide_clang_dispose_string           (CXString           *str);
-void                     _ide_clang_dispose_unit             (CXTranslationUnit  *unit);
 IdeSymbolNode           *_ide_clang_symbol_node_new          (IdeContext         *context,
                                                               CXCursor            cursor);
 CXCursor                 _ide_clang_symbol_node_get_cursor   (IdeClangSymbolNode *self);
@@ -43,10 +40,4 @@ GArray                  *_ide_clang_symbol_node_get_children (IdeClangSymbolNode
 void                     _ide_clang_symbol_node_set_children (IdeClangSymbolNode *self,
                                                               GArray             *children);
 
-G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (CXString, _ide_clang_dispose_string)
-G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (CXIndex, _ide_clang_dispose_index)
-G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (CXTranslationUnit, _ide_clang_dispose_unit)
-
-G_DEFINE_AUTOPTR_CLEANUP_FUNC (CXDiagnostic, _ide_clang_dispose_diagnostic)
-
 G_END_DECLS
diff --git a/src/plugins/clang/ide-clang-service.c b/src/plugins/clang/ide-clang-service.c
index 546568495..b2468a03e 100644
--- a/src/plugins/clang/ide-clang-service.c
+++ b/src/plugins/clang/ide-clang-service.c
@@ -818,31 +818,3 @@ ide_clang_service_get_cached_translation_unit (IdeClangService *self,
 
   return cached ? g_object_ref (cached) : NULL;
 }
-
-void
-_ide_clang_dispose_string (CXString *str)
-{
-  if (str != NULL && str->data != NULL)
-    clang_disposeString (*str);
-}
-
-void
-_ide_clang_dispose_diagnostic (CXDiagnostic *diag)
-{
-  if (diag != NULL)
-    clang_disposeDiagnostic (diag);
-}
-
-void
-_ide_clang_dispose_index (CXIndex *idx)
-{
-  if (idx != NULL && *idx != NULL)
-    clang_disposeIndex (*idx);
-}
-
-void
-_ide_clang_dispose_unit (CXTranslationUnit *unit)
-{
-  if (unit != NULL && *unit != NULL)
-    clang_disposeTranslationUnit (*unit);
-}
diff --git a/src/plugins/clang/ide-clang-util.h b/src/plugins/clang/ide-clang-util.h
new file mode 100644
index 000000000..2b99bfd6c
--- /dev/null
+++ b/src/plugins/clang/ide-clang-util.h
@@ -0,0 +1,80 @@
+/* ide-clang-util.h
+ *
+ * Copyright 2018 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/>.
+ */
+
+#pragma once
+
+#include <ide.h>
+
+#include "ide-clang-autocleanups.h"
+
+G_BEGIN_DECLS
+
+static inline IdeSymbolKind
+ide_clang_translate_kind (enum CXCursorKind cursor_kind)
+{
+  switch ((int)cursor_kind)
+    {
+    case CXCursor_StructDecl:
+      return IDE_SYMBOL_STRUCT;
+
+    case CXCursor_UnionDecl:
+      return IDE_SYMBOL_UNION;
+
+    case CXCursor_ClassDecl:
+      return IDE_SYMBOL_CLASS;
+
+    case CXCursor_EnumDecl:
+      return IDE_SYMBOL_ENUM;
+
+    case CXCursor_FieldDecl:
+      return IDE_SYMBOL_FIELD;
+
+    case CXCursor_EnumConstantDecl:
+      return IDE_SYMBOL_ENUM_VALUE;
+
+    case CXCursor_FunctionDecl:
+      return IDE_SYMBOL_FUNCTION;
+
+    case CXCursor_CXXMethod:
+      return IDE_SYMBOL_METHOD;
+
+    case CXCursor_VarDecl:
+    case CXCursor_ParmDecl:
+      return IDE_SYMBOL_VARIABLE;
+
+    case CXCursor_TypedefDecl:
+    case CXCursor_NamespaceAlias:
+    case CXCursor_TypeAliasDecl:
+      return IDE_SYMBOL_ALIAS;
+
+    case CXCursor_Namespace:
+      return IDE_SYMBOL_NAMESPACE;
+
+    case CXCursor_FunctionTemplate:
+    case CXCursor_ClassTemplate:
+      return IDE_SYMBOL_TEMPLATE;
+
+    case CXCursor_MacroDefinition:
+      return IDE_SYMBOL_MACRO;
+
+    default:
+      return IDE_SYMBOL_NONE;
+    }
+}
+
+G_END_DECLS


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