[gnome-builder] clang: use g_auto(CXString) when appropriate



commit e8a09c74794bbd3765544d4bd6c1f266f8265ed3
Author: Christian Hergert <chergert redhat com>
Date:   Sat Jan 13 04:01:02 2018 -0800

    clang: use g_auto(CXString) when appropriate

 src/plugins/clang/ide-clang-code-index-entries.c | 11 +++--
 src/plugins/clang/ide-clang-completion-item.c    | 20 +++------
 src/plugins/clang/ide-clang-service.c            |  3 +-
 src/plugins/clang/ide-clang-symbol-tree.c        |  3 +-
 src/plugins/clang/ide-clang-translation-unit.c   | 54 ++++++++----------------
 5 files changed, 30 insertions(+), 61 deletions(-)
---
diff --git a/src/plugins/clang/ide-clang-code-index-entries.c 
b/src/plugins/clang/ide-clang-code-index-entries.c
index cf8dd6546..232cf95fb 100644
--- a/src/plugins/clang/ide-clang-code-index-entries.c
+++ b/src/plugins/clang/ide-clang-code-index-entries.c
@@ -19,6 +19,7 @@
 #define G_LOG_DOMAIN "ide-clang-code-index-entries"
 
 #include "ide-clang-code-index-entries.h"
+#include "ide-clang-private.h"
 
  /*
   * This is an implementation of IdeCodeIndexEntries. This will have a TU and
@@ -121,7 +122,7 @@ ide_clang_code_index_entries_real_get_next_entry (IdeClangCodeIndexEntries *self
   guint column = 0;
   guint offset = 0;
   enum CXLinkageKind linkage;
-  CXString cx_name;
+  g_auto(CXString) cx_name = {0};
   const gchar *cname = NULL;
   gchar *prefix = NULL;
   g_autofree gchar *name = NULL;
@@ -259,7 +260,7 @@ ide_clang_code_index_entries_real_get_next_entry (IdeClangCodeIndexEntries *self
   else
     prefix = "x\x1F";
 
-  name = g_strconcat (prefix, clang_getCString (cx_name), NULL);
+  name = g_strconcat (prefix, cname, NULL);
 
   if (clang_isCursorDefinition (*cursor))
     flags |= IDE_SYMBOL_FLAGS_IS_DEFINITION;
@@ -276,14 +277,12 @@ ide_clang_code_index_entries_real_get_next_entry (IdeClangCodeIndexEntries *self
     }
   else
     {
-      CXString usr;
+      g_auto(CXString) usr = {0};
+
       usr = clang_getCursorUSR (*cursor);
       key = g_strdup (clang_getCString (usr));
-      clang_disposeString (usr);
     }
 
-  clang_disposeString (cx_name);
-
   return g_object_new (IDE_TYPE_CODE_INDEX_ENTRY,
                        "name", name,
                        "key", key,
diff --git a/src/plugins/clang/ide-clang-completion-item.c b/src/plugins/clang/ide-clang-completion-item.c
index 872782de6..b266d3e87 100644
--- a/src/plugins/clang/ide-clang-completion-item.c
+++ b/src/plugins/clang/ide-clang-completion-item.c
@@ -201,7 +201,6 @@ ide_clang_completion_item_create_snippet (IdeClangCompletionItem *self)
   CXCompletionResult *result;
   IdeSourceSnippet *snippet;
   unsigned num_chunks;
-  unsigned i;
   guint tab_stop = 0;
 
   g_assert (IDE_IS_CLANG_COMPLETION_ITEM (self));
@@ -210,12 +209,12 @@ ide_clang_completion_item_create_snippet (IdeClangCompletionItem *self)
   snippet = ide_source_snippet_new (NULL, NULL);
   num_chunks = clang_getNumCompletionChunks (result->CompletionString);
 
-  for (i = 0; i < num_chunks; i++)
+  for (unsigned i = 0; i < num_chunks; i++)
     {
+      g_auto(CXString) cxstr = {0};
       enum CXCompletionChunkKind kind;
       IdeSourceSnippetChunk *chunk;
       const gchar *text;
-      CXString cxstr;
 
       kind = clang_getCompletionChunkKind (result->CompletionString, i);
       cxstr = clang_getCompletionChunkText (result->CompletionString, i);
@@ -298,8 +297,6 @@ ide_clang_completion_item_create_snippet (IdeClangCompletionItem *self)
         default:
           break;
         }
-
-      clang_disposeString (cxstr);
     }
 
   return snippet;
@@ -464,7 +461,7 @@ const gchar *
 ide_clang_completion_item_get_typed_text (IdeClangCompletionItem *self)
 {
   CXCompletionResult *result;
-  CXString cxstr;
+  g_auto(CXString) cxstr = {0};
 
   g_return_val_if_fail (IDE_IS_CLANG_COMPLETION_ITEM (self), NULL);
 
@@ -479,12 +476,9 @@ ide_clang_completion_item_get_typed_text (IdeClangCompletionItem *self)
    */
   if (G_UNLIKELY (self->typed_text_index == -1))
     {
-      guint num_chunks;
-      guint i;
-
-      num_chunks = clang_getNumCompletionChunks (result->CompletionString);
+      guint num_chunks = clang_getNumCompletionChunks (result->CompletionString);
 
-      for (i = 0; i < num_chunks; i++)
+      for (guint i = 0; i < num_chunks; i++)
         {
           enum CXCompletionChunkKind kind;
 
@@ -525,7 +519,6 @@ ide_clang_completion_item_get_typed_text (IdeClangCompletionItem *self)
 
   cxstr = clang_getCompletionChunkText (result->CompletionString, self->typed_text_index);
   self->typed_text = g_strdup (clang_getCString (cxstr));
-  clang_disposeString (cxstr);
 
   return self->typed_text;
 }
@@ -548,12 +541,11 @@ ide_clang_completion_item_get_brief_comment (IdeClangCompletionItem *self)
 
   if (self->brief_comment == NULL)
     {
-      CXString cxstr;
+      g_auto(CXString) cxstr = {0};
 
       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/src/plugins/clang/ide-clang-service.c b/src/plugins/clang/ide-clang-service.c
index 1726e12f7..ea1bbfbc6 100644
--- a/src/plugins/clang/ide-clang-service.c
+++ b/src/plugins/clang/ide-clang-service.c
@@ -125,13 +125,12 @@ ide_clang_service_build_index_visitor (CXCursor     cursor,
 
   if (style_name != NULL)
     {
-      CXString cxstr;
+      g_auto(CXString) cxstr = {0};
       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;
diff --git a/src/plugins/clang/ide-clang-symbol-tree.c b/src/plugins/clang/ide-clang-symbol-tree.c
index bfb77ef20..7afa52a48 100644
--- a/src/plugins/clang/ide-clang-symbol-tree.c
+++ b/src/plugins/clang/ide-clang-symbol-tree.c
@@ -86,7 +86,7 @@ static gboolean
 cursor_is_recognized (TraversalState *state,
                       CXCursor        cursor)
 {
-  CXString filename;
+  g_auto(CXString) filename = {0};
   CXSourceLocation cxloc;
   CXFile file;
   enum CXCursorKind kind;
@@ -115,7 +115,6 @@ cursor_is_recognized (TraversalState *state,
       clang_getFileLocation (cxloc, &file, NULL, NULL, NULL);
       filename = clang_getFileName (file);
       ret = dzl_str_equal0 (clang_getCString (filename), state->path);
-      clang_disposeString (filename);
       break;
 
     default:
diff --git a/src/plugins/clang/ide-clang-translation-unit.c b/src/plugins/clang/ide-clang-translation-unit.c
index 98bb9e734..f22a90139 100644
--- a/src/plugins/clang/ide-clang-translation-unit.c
+++ b/src/plugins/clang/ide-clang-translation-unit.c
@@ -269,19 +269,15 @@ static gboolean
 cxfile_equal (CXFile  cxfile,
               GFile  *file)
 {
-  CXString cxstr;
-  gchar *path;
-  gboolean ret;
+  g_auto(CXString) cxstr = {0};
+  g_autofree gchar *path = NULL;
+  const gchar *cstr;
 
   cxstr = clang_getFileName (cxfile);
+  cstr = clang_getCString (cxstr);
   path = g_file_get_path (file);
 
-  ret = (0 == g_strcmp0 (clang_getCString (cxstr), path));
-
-  clang_disposeString (cxstr);
-  g_free (path);
-
-  return ret;
+  return dzl_str_equal0 (cstr, path);
 }
 
 static IdeDiagnostic *
@@ -294,8 +290,8 @@ create_diagnostic (IdeClangTranslationUnit *self,
   enum CXDiagnosticSeverity cxseverity;
   IdeDiagnosticSeverity severity;
   IdeDiagnostic *diag;
-  g_autofree gchar *spelling = NULL;
-  CXString cxstr;
+  const gchar *spelling;
+  g_auto(CXString) cxstr = {0};
   CXSourceLocation cxloc;
   CXFile cxfile = NULL;
   guint num_ranges;
@@ -314,8 +310,7 @@ create_diagnostic (IdeClangTranslationUnit *self,
   severity = translate_severity (cxseverity);
 
   cxstr = clang_getDiagnosticSpelling (cxdiag);
-  spelling = g_strdup (clang_getCString (cxstr));
-  clang_disposeString (cxstr);
+  spelling = clang_getCString (cxstr);
 
   /*
    * I thought we could use an approach like the following to get deprecation
@@ -398,13 +393,12 @@ ide_clang_translation_unit_get_diagnostics_for_file (IdeClangTranslationUnit *se
                 {
                   g_autoptr(IdeSourceRange) range = NULL;
                   g_autoptr(IdeFixit) fixit = NULL;
+                  g_auto(CXString) cxstr = {0};
                   CXSourceRange cxrange;
-                  CXString cxstr;
 
                   cxstr = clang_getDiagnosticFixIt (cxdiag, j, &cxrange);
                   range = create_range (self, workpath, cxrange);
                   fixit = ide_fixit_new (range, clang_getCString (cxstr));
-                  clang_disposeString (cxstr);
 
                   if (fixit != NULL)
                     ide_diagnostic_take_fixit (diag, g_steal_pointer (&fixit));
@@ -951,13 +945,12 @@ static IdeSymbol *
 create_symbol (CXCursor         cursor,
                GetSymbolsState *state)
 {
-  g_auto(CXString) cxname = { 0 };
+  g_auto(CXString) cxname = {0};
   g_autoptr(IdeSourceLocation) srcloc = NULL;
   CXSourceLocation cxloc;
   IdeSymbolKind symkind;
   IdeSymbolFlags symflags;
   const gchar *name;
-  IdeSymbol *symbol;
   guint line;
   guint line_offset;
 
@@ -966,12 +959,9 @@ create_symbol (CXCursor         cursor,
   cxloc = clang_getCursorLocation (cursor);
   clang_getFileLocation (cxloc, NULL, &line, &line_offset, NULL);
   srcloc = ide_source_location_new (state->file, line-1, line_offset-1, 0);
-
   symkind = get_symbol_kind (cursor, &symflags);
 
-  symbol = ide_symbol_new (name, symkind, symflags, NULL, NULL, srcloc);
-
-  return symbol;
+  return ide_symbol_new (name, symkind, symflags, NULL, NULL, srcloc);
 }
 
 static enum CXChildVisitResult
@@ -981,7 +971,7 @@ ide_clang_translation_unit_get_symbols__visitor_cb (CXCursor     cursor,
 {
   GetSymbolsState *state = user_data;
   g_autoptr(IdeSymbol) symbol = NULL;
-  g_auto(CXString) filename = { 0 };
+  g_auto(CXString) filename = {0};
   CXSourceLocation cxloc;
   CXFile file;
   enum CXCursorKind kind;
@@ -1159,7 +1149,7 @@ ide_clang_translation_unit_find_nearest_scope (IdeClangTranslationUnit  *self,
                                                GError                  **error)
 {
   g_autoptr(IdeSourceLocation) symbol_location = NULL;
-  g_auto(CXString) cxname = { 0 };
+  g_auto(CXString) cxname = {0};
   CXTranslationUnit unit;
   CXSourceLocation loc;
   CXCursor cursor;
@@ -1252,14 +1242,13 @@ gchar *
 ide_clang_translation_unit_generate_key (IdeClangTranslationUnit  *self,
                                          IdeSourceLocation        *location)
 {
+  g_auto(CXString) cx_usr = {0};
   CXTranslationUnit unit;
   CXFile file;
   CXSourceLocation cx_location;
   CXCursor reference;
   CXCursor declaration;
-  CXString cx_usr;
   const gchar *usr;
-  g_autofree gchar *ret = NULL;
   guint line = 0;
   guint column = 0;
   enum CXLinkageKind linkage;
@@ -1277,20 +1266,11 @@ ide_clang_translation_unit_generate_key (IdeClangTranslationUnit  *self,
   reference = clang_getCursor (unit, cx_location);
   declaration = clang_getCursorReferenced (reference);
   cx_usr = clang_getCursorUSR (declaration);
-
-  linkage = clang_getCursorLinkage (declaration);
-
-  if (linkage == CXLinkage_Internal || linkage == CXLinkage_NoLinkage)
-    return NULL;
-
   usr = clang_getCString (cx_usr);
+  linkage = clang_getCursorLinkage (declaration);
 
-  if (usr == NULL)
+  if (linkage == CXLinkage_Internal || linkage == CXLinkage_NoLinkage || usr == NULL)
     return NULL;
 
-  ret = g_strdup (usr);
-
-  clang_disposeString (cx_usr);
-
-  return g_steal_pointer (&ret);
+  return g_strdup (usr);
 }


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