[gnome-builder] symbols: extract some additional symbol information



commit dce53f5841fd1a6d9a806b6e6b4bad48f8e05f7a
Author: Christian Hergert <christian hergert me>
Date:   Fri Mar 27 17:17:36 2015 -0700

    symbols: extract some additional symbol information
    
    What gets contained in a symbol will probably change a lot in the near
    future. This is really just stubs so I can make forward progress.
    
    We will also probably add some sort of flags to determine what types of
    information you want (since it could end up being pretty slow otherwise).

 libide/clang/ide-clang-translation-unit.c |   29 +++++++++-
 libide/ide-internal.h                     |    5 ++-
 libide/ide-symbol.c                       |   82 +++++++++++++++++++++++++++--
 libide/ide-symbol.h                       |   11 +++--
 4 files changed, 115 insertions(+), 12 deletions(-)
---
diff --git a/libide/clang/ide-clang-translation-unit.c b/libide/clang/ide-clang-translation-unit.c
index c69ab2d..b2a90a0 100644
--- a/libide/clang/ide-clang-translation-unit.c
+++ b/libide/clang/ide-clang-translation-unit.c
@@ -714,8 +714,17 @@ ide_clang_translation_unit_lookup_symbol (IdeClangTranslationUnit  *self,
                                           GError                  **error)
 {
   g_autofree gchar *filename = NULL;
+  g_autofree gchar *workpath = NULL;
   g_auto(CXString) cxstr = { 0 };
+  g_autoptr(IdeSourceLocation) declaration = NULL;
+  g_autoptr(IdeSourceLocation) definition = NULL;
+  g_autoptr(IdeSourceLocation) canonical = NULL;
+  IdeProject *project;
+  IdeContext *context;
+  IdeVcs *vcs;
+  GFile *workdir;
   CXSourceLocation cxlocation;
+  CXCursor tmpcursor;
   CXCursor cursor;
   CXFile cxfile;
   IdeSymbol *ret = NULL;
@@ -729,6 +738,12 @@ ide_clang_translation_unit_lookup_symbol (IdeClangTranslationUnit  *self,
   g_return_val_if_fail (IDE_IS_CLANG_TRANSLATION_UNIT (self), NULL);
   g_return_val_if_fail (location != NULL, NULL);
 
+  context = ide_object_get_context (IDE_OBJECT (self));
+  project = ide_context_get_project (context);
+  vcs = ide_context_get_vcs (context);
+  workdir = ide_vcs_get_working_directory (vcs);
+  workpath = g_file_get_path (workdir);
+
   line = ide_source_location_get_line (location);
   line_offset = ide_source_location_get_line_offset (location);
 
@@ -739,13 +754,23 @@ ide_clang_translation_unit_lookup_symbol (IdeClangTranslationUnit  *self,
     IDE_RETURN (NULL);
 
   cxlocation = clang_getLocation (self->tu, cxfile, line + 1, line_offset + 1);
-
   cursor = clang_getCursor (self->tu, cxlocation);
   if (clang_Cursor_isNull (cursor))
     IDE_RETURN (NULL);
 
+  tmpcursor = clang_getCursorReferenced (cursor);
+  if (!clang_Cursor_isNull (tmpcursor))
+    {
+      CXSourceLocation tmploc;
+      CXSourceRange cxrange;
+
+      cxrange = clang_getCursorExtent (tmpcursor);
+      tmploc = clang_getRangeStart (cxrange);
+      definition = create_location (self, project, workpath, tmploc);
+    }
+
   cxstr = clang_getCursorDisplayName (cursor);
-  ret = _ide_symbol_new (clang_getCString (cxstr));
+  ret = _ide_symbol_new (clang_getCString (cxstr), declaration, definition, canonical);
 
   /*
    * TODO: We should also get information about the defintion of the symbol.
diff --git a/libide/ide-internal.h b/libide/ide-internal.h
index 86eeba6..60d5e0d 100644
--- a/libide/ide-internal.h
+++ b/libide/ide-internal.h
@@ -97,7 +97,10 @@ void                _ide_source_view_set_count         (IdeSourceView         *s
                                                         guint                  count);
 void                _ide_source_view_set_modifier      (IdeSourceView         *self,
                                                         gunichar               modifier);
-IdeSymbol          *_ide_symbol_new                    (const gchar           *name);
+IdeSymbol          *_ide_symbol_new                    (const gchar           *name,
+                                                        IdeSourceLocation     *declaration_location,
+                                                        IdeSourceLocation     *definition_location,
+                                                        IdeSourceLocation     *canonical_location);
 IdeUnsavedFile     *_ide_unsaved_file_new              (GFile                 *file,
                                                         GBytes                *content,
                                                         const gchar           *temp_path,
diff --git a/libide/ide-symbol.c b/libide/ide-symbol.c
index be14fb6..5aff1d9 100644
--- a/libide/ide-symbol.c
+++ b/libide/ide-symbol.c
@@ -16,25 +16,44 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define G_LOG_DOMAIN "ide-symbol"
+
 #include "ide-symbol.h"
+#include "ide-source-location.h"
 
 struct _IdeSymbol
 {
   volatile gint ref_count;
-  gchar *name;
+
+  gchar             *name;
+  IdeSourceLocation *declaration_location;
+  IdeSourceLocation *definition_location;
+  IdeSourceLocation *canonical_location;
 };
 
 G_DEFINE_BOXED_TYPE (IdeSymbol, ide_symbol, ide_symbol_ref, ide_symbol_unref)
 
 IdeSymbol *
-_ide_symbol_new (const gchar *name)
+_ide_symbol_new (const gchar       *name,
+                 IdeSourceLocation *declaration_location,
+                 IdeSourceLocation *definition_location,
+                 IdeSourceLocation *canonical_location)
 {
   IdeSymbol *ret;
 
-  ret = g_slice_new0 (IdeSymbol);
+  ret = g_new0 (IdeSymbol, 1);
   ret->ref_count = 1;
   ret->name = g_strdup (name);
 
+  if (declaration_location)
+    ret->declaration_location = ide_source_location_ref (declaration_location);
+
+  if (definition_location)
+    ret->definition_location = ide_source_location_ref (definition_location);
+
+  if (canonical_location)
+    ret->canonical_location = ide_source_location_ref (canonical_location);
+
   return ret;
 }
 
@@ -46,6 +65,56 @@ ide_symbol_get_name (IdeSymbol *self)
   return self->name;
 }
 
+/**
+ * ide_symbol_get_declaration_location:
+ *
+ * The location of a symbol equates to the declaration of the symbol. In C and C++, this would
+ * mean the header location (or forward declaration in a C file before the implementation).
+ *
+ * If the symbol provider did not register this information, %NULL will be returned.
+ *
+ * Returns: (transfer none) (nullable): An #IdeSourceLocation or %NULL.
+ */
+IdeSourceLocation *
+ide_symbol_get_declaration_location (IdeSymbol *self)
+{
+  g_return_val_if_fail (self, NULL);
+
+  return self->declaration_location;
+}
+
+/**
+ * ide_symbol_get_definition_location:
+ *
+ * Like ide_symbol_get_declaration_location() but gets the first declaration (only one can be
+ * the definition).
+ *
+ * Returns: (transfer none) (nullable): An #IdeSourceLocation or %NULL.
+ */
+IdeSourceLocation *
+ide_symbol_get_definition_location (IdeSymbol *self)
+{
+  g_return_val_if_fail (self, NULL);
+
+  return self->definition_location;
+}
+
+/**
+ * ide_symbol_get_canonical_location:
+ *
+ * Gets the location of the symbols "implementation". In C/C++ languages, you can have multiple
+ * declarations by only a single implementation.
+ *
+ * Returns: (transfer none) (nullable): An #IdeSourceLocation or %NULL.
+ */
+IdeSourceLocation *
+ide_symbol_get_canonical_location (IdeSymbol *self)
+{
+  g_return_val_if_fail (self, NULL);
+
+  return self->canonical_location;
+}
+
 IdeSymbol *
 ide_symbol_ref (IdeSymbol *self)
 {
@@ -65,7 +134,10 @@ ide_symbol_unref (IdeSymbol *self)
 
   if (g_atomic_int_dec_and_test (&self->ref_count))
     {
-      g_free (self->name);
-      g_slice_free (IdeSymbol, self);
+      g_clear_pointer (&self->declaration_location, ide_source_location_unref);
+      g_clear_pointer (&self->definition_location, ide_source_location_unref);
+      g_clear_pointer (&self->canonical_location, ide_source_location_unref);
+      g_clear_pointer (&self->name, g_free);
+      g_free (self);
     }
 }
diff --git a/libide/ide-symbol.h b/libide/ide-symbol.h
index fdbc7c6..a024f3c 100644
--- a/libide/ide-symbol.h
+++ b/libide/ide-symbol.h
@@ -25,10 +25,13 @@ G_BEGIN_DECLS
 
 #define IDE_TYPE_SYMBOL (ide_symbol_get_type())
 
-GType        ide_symbol_get_type (void);
-IdeSymbol   *ide_symbol_ref      (IdeSymbol *self);
-void         ide_symbol_unref    (IdeSymbol *self);
-const gchar *ide_symbol_get_name (IdeSymbol *self);
+GType              ide_symbol_get_type                 (void);
+IdeSymbol         *ide_symbol_ref                      (IdeSymbol *self);
+void               ide_symbol_unref                    (IdeSymbol *self);
+const gchar       *ide_symbol_get_name                 (IdeSymbol *self);
+IdeSourceLocation *ide_symbol_get_canonical_location   (IdeSymbol *self);
+IdeSourceLocation *ide_symbol_get_declaration_location (IdeSymbol *self);
+IdeSourceLocation *ide_symbol_get_definition_location  (IdeSymbol *self);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (IdeSymbol, ide_symbol_unref)
 


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