[gnome-builder] clang: ensure we are disposing CXStrings, cache copy when necessary



commit b3f90152730c76a9acc06c3dc628d9814e859901
Author: Christian Hergert <christian hergert me>
Date:   Thu Mar 26 20:51:49 2015 -0700

    clang: ensure we are disposing CXStrings, cache copy when necessary

 libide/clang/ide-clang-completion-item.c  |   26 ++++++++++++---
 libide/clang/ide-clang-private.h          |    3 ++
 libide/clang/ide-clang-service.c          |   51 ++++++++++++++++++++++-------
 libide/clang/ide-clang-translation-unit.c |    9 +++--
 4 files changed, 68 insertions(+), 21 deletions(-)
---
diff --git a/libide/clang/ide-clang-completion-item.c b/libide/clang/ide-clang-completion-item.c
index 8ec5547..c3fb761 100644
--- a/libide/clang/ide-clang-completion-item.c
+++ b/libide/clang/ide-clang-completion-item.c
@@ -40,10 +40,12 @@ struct _IdeClangCompletionItem
   gint              typed_text_index;
   guint             initialized : 1;
 
+  gchar            *brief_comment;
   gchar            *markup;
   GdkPixbuf        *icon;
   IdeRefPtr        *results;
   IdeSourceSnippet *snippet;
+  gchar            *typed_text;
 };
 
 static void completion_proposal_iface_init (GtkSourceCompletionProposalIface *);
@@ -329,6 +331,8 @@ ide_clang_completion_item_finalize (GObject *object)
 
   g_clear_object (&self->icon);
   g_clear_object (&self->snippet);
+  g_clear_pointer (&self->brief_comment, g_free);
+  g_clear_pointer (&self->typed_text, g_free);
   g_clear_pointer (&self->markup, g_free);
   g_clear_pointer (&self->results, ide_ref_ptr_unref);
 
@@ -503,6 +507,9 @@ ide_clang_completion_item_get_typed_text (IdeClangCompletionItem *self)
 
   g_return_val_if_fail (IDE_IS_CLANG_COMPLETION_ITEM (self), NULL);
 
+  if (self->typed_text)
+    return self->typed_text;
+
   result = ide_clang_completion_item_get_result (self);
 
   /*
@@ -545,8 +552,10 @@ ide_clang_completion_item_get_typed_text (IdeClangCompletionItem *self)
 #endif
 
   cxstr = clang_getCompletionChunkText (result->CompletionString, self->typed_text_index);
+  self->typed_text = g_strdup (clang_getCString (cxstr));
+  clang_disposeString (cxstr);
 
-  return clang_getCString (cxstr);
+  return self->typed_text;
 }
 
 /**
@@ -562,11 +571,18 @@ const gchar *
 ide_clang_completion_item_get_brief_comment (IdeClangCompletionItem *self)
 {
   CXCompletionResult *result;
-  CXString cxstr;
 
   g_return_val_if_fail (IDE_IS_CLANG_COMPLETION_ITEM (self), NULL);
 
-  result = ide_clang_completion_item_get_result (self);
-  cxstr = clang_getCompletionBriefComment (result->CompletionString);
-  return clang_getCString (cxstr);
+  if (self->brief_comment == NULL)
+    {
+      CXString cxstr;
+
+      result = ide_clang_completion_item_get_result (self);
+      cxstr = clang_getCompletionBriefComment (result->CompletionString);
+      self->brief_comment = g_strdup (clang_getCString (cxstr));
+      clang_disposeString (cxstr);
+    }
+
+  return self->brief_comment;
 }
diff --git a/libide/clang/ide-clang-private.h b/libide/clang/ide-clang-private.h
index 334310f..2b8586e 100644
--- a/libide/clang/ide-clang-private.h
+++ b/libide/clang/ide-clang-private.h
@@ -34,6 +34,9 @@ IdeClangTranslationUnit *_ide_clang_translation_unit_new (IdeContext        *con
                                                           GFile             *file,
                                                           IdeHighlightIndex *index,
                                                           gint64             sequence);
+void                     _ide_clang_dispose_string       (CXString          *str);
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (CXString, _ide_clang_dispose_string)
 
 G_END_DECLS
 
diff --git a/libide/clang/ide-clang-service.c b/libide/clang/ide-clang-service.c
index aaa8a2a..819de6f 100644
--- a/libide/clang/ide-clang-service.c
+++ b/libide/clang/ide-clang-service.c
@@ -80,8 +80,7 @@ ide_clang_service_build_index_visitor (CXCursor     cursor,
 {
   IndexRequest *request = user_data;
   enum CXCursorKind kind;
-  const gchar *word;
-  CXString cxstr;
+  const gchar *style_name = NULL;
 
   g_assert (request != NULL);
 
@@ -91,28 +90,37 @@ ide_clang_service_build_index_visitor (CXCursor     cursor,
     {
     case CXCursor_TypedefDecl:
     case CXCursor_TypeAliasDecl:
-      cxstr = clang_getCursorSpelling (cursor);
-      word = clang_getCString (cxstr);
-      ide_highlight_index_insert (request->index, word, "def:type");
+      style_name = "c:type";
       break;
 
     case CXCursor_FunctionDecl:
-      cxstr = clang_getCursorSpelling (cursor);
-      word = clang_getCString (cxstr);
-      ide_highlight_index_insert (request->index, word, "def:function");
+      style_name = "c:function-name";
+      break;
+
+    case CXCursor_EnumDecl:
+    case CXCursor_EnumConstantDecl:
+      style_name = "c:enum-name";
       break;
 
     case CXCursor_MacroDefinition:
-    case CXCursor_MacroExpansion:
-      cxstr = clang_getCursorSpelling (cursor);
-      word = clang_getCString (cxstr);
-      ide_highlight_index_insert (request->index, word, "def:preprocessor");
+      style_name = "c:macro-name";
       break;
 
     default:
       break;
     }
 
+  if (style_name != NULL)
+    {
+      CXString cxstr;
+      const gchar *word;
+
+      cxstr = clang_getCursorSpelling (cursor);
+      word = clang_getCString (cxstr);
+      ide_highlight_index_insert (request->index, word, (gpointer)style_name);
+      clang_disposeString (cxstr);
+    }
+
   return CXChildVisit_Continue;
 }
 
@@ -121,10 +129,14 @@ ide_clang_service_build_index (IdeClangService   *self,
                                CXTranslationUnit  tu,
                                ParseRequest      *request)
 {
+  static const gchar *common_defines[] = {
+    "NULL", "MIN", "MAX", "__LINE__", "__FILE__", NULL
+  };
   IdeHighlightIndex *index;
   IndexRequest client_data;
   CXCursor cursor;
   CXFile file;
+  gsize i;
 
   g_assert (IDE_IS_CLANG_SERVICE (self));
   g_assert (tu != NULL);
@@ -140,6 +152,14 @@ ide_clang_service_build_index (IdeClangService   *self,
   client_data.file = file;
   client_data.filename = request->source_filename;
 
+  /*
+   * Add some common defines so they don't get changed by clang.
+   */
+  for (i = 0; common_defines [i]; i++)
+    ide_highlight_index_insert (index, common_defines [i], "c:common-defines");
+  ide_highlight_index_insert (index, "TRUE", "c:boolean");
+  ide_highlight_index_insert (index, "FALSE", "c:boolean");
+
   cursor = clang_getTranslationUnitCursor (tu);
   clang_visitChildren (cursor, ide_clang_service_build_index_visitor, &client_data);
 
@@ -503,3 +523,10 @@ ide_clang_service_get_cached_translation_unit (IdeClangService *self,
 
   return cached;
 }
+
+void
+_ide_clang_dispose_string (CXString *str)
+{
+  if (str != NULL && str->data != NULL)
+    clang_disposeString (*str);
+}
diff --git a/libide/clang/ide-clang-translation-unit.c b/libide/clang/ide-clang-translation-unit.c
index 54a9540..9e84ab1 100644
--- a/libide/clang/ide-clang-translation-unit.c
+++ b/libide/clang/ide-clang-translation-unit.c
@@ -37,6 +37,7 @@
 #include "ide-unsaved-files.h"
 #include "ide-vcs.h"
 
+
 struct _IdeClangTranslationUnit
 {
   IdeObject          parent_instance;
@@ -218,11 +219,11 @@ create_location (IdeClangTranslationUnit *self,
 
   str = clang_getFileName (cxfile);
   cstr = clang_getCString (str);
-  if (!cstr)
-    return NULL;
-
-  path = get_path (workpath, cstr);
+  if (cstr != NULL)
+    path = get_path (workpath, cstr);
   clang_disposeString (str);
+  if (cstr == NULL)
+    return NULL;
 
   file = ide_project_get_file_for_path (project, path);
 


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