[gnome-builder/wip/chergert/layout] clang: add find_nearest_scope() to IdeClangTranslationUnit
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/wip/chergert/layout] clang: add find_nearest_scope() to IdeClangTranslationUnit
- Date: Tue, 4 Jul 2017 00:08:18 +0000 (UTC)
commit 22423fb12937f318c1c2dd1b7fca64d6b7126ff5
Author: Christian Hergert <chergert redhat com>
Date: Mon Jul 3 17:06:32 2017 -0700
clang: add find_nearest_scope() to IdeClangTranslationUnit
This will be used by the symbol resolver to implement the new
"nearest scope" feature.
Signed-off-by: Christian Hergert <chergert redhat com>
plugins/clang/ide-clang-translation-unit.c | 80 ++++++++++++++++++++++++++++
plugins/clang/ide-clang-translation-unit.h | 3 +
2 files changed, 83 insertions(+), 0 deletions(-)
---
diff --git a/plugins/clang/ide-clang-translation-unit.c b/plugins/clang/ide-clang-translation-unit.c
index 64d78e0..4590233 100644
--- a/plugins/clang/ide-clang-translation-unit.c
+++ b/plugins/clang/ide-clang-translation-unit.c
@@ -1141,3 +1141,83 @@ ide_clang_translation_unit_get_symbol_tree_finish (IdeClangTranslationUnit *sel
return g_task_propagate_pointer (task, error);
}
+
+/**
+ * ide_clang_translation_unit_find_nearest_scope:
+ * @self: a #IdeClangTranslationUnit
+ * @location: An #IdeSourceLocation within the unit
+ * @error: A location for a #GError or %NULL
+ *
+ * This locates the nearest scope for @location and returns it
+ * as an #IdeSymbol.
+ *
+ * Returns: (transfer full): An #IdeSymbol or %NULL and @error is set.
+ *
+ * Since: 3.26
+ */
+IdeSymbol *
+ide_clang_translation_unit_find_nearest_scope (IdeClangTranslationUnit *self,
+ IdeSourceLocation *location,
+ GError **error)
+{
+ g_autoptr(IdeSourceLocation) symbol_location = NULL;
+ g_auto(CXString) cxname = { 0 };
+ CXTranslationUnit unit;
+ CXSourceLocation loc;
+ CXCursor cursor;
+ IdeSymbolKind symkind;
+ IdeSymbolFlags symflags;
+ IdeSymbol *ret;
+ CXFile file;
+ IdeFile *ifile;
+ guint line;
+ guint line_offset;
+
+ IDE_ENTRY;
+
+ g_return_val_if_fail (IDE_IS_CLANG_TRANSLATION_UNIT (self), NULL);
+ g_return_val_if_fail (location != NULL, NULL);
+
+ ifile = ide_source_location_get_file (location);
+ line = ide_source_location_get_line (location);
+ line_offset = ide_source_location_get_line_offset (location);
+
+ if (NULL == (file = get_file_for_location (self, location)))
+ {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "Failed to locate file in translation unit");
+ IDE_RETURN (ret);
+ }
+
+ unit = ide_ref_ptr_get (self->native);
+ loc = clang_getLocation (unit, file, line + 1, line_offset + 1);
+ cursor = clang_getCursor (unit, loc);
+
+ if (clang_Cursor_isNull (cursor))
+ {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "Location was not found in translation unit");
+ IDE_RETURN (ret);
+ }
+
+ cursor = clang_getCursorSemanticParent (cursor);
+
+ symbol_location = ide_source_location_new (ifile, line - 1, line_offset - 1, 0);
+ cxname = clang_getCursorSpelling (cursor);
+ symkind = get_symbol_kind (cursor, &symflags);
+
+ ret = ide_symbol_new (clang_getCString (cxname),
+ symkind,
+ symflags,
+ NULL,
+ NULL,
+ symbol_location);
+
+ IDE_TRACE_MSG ("Symbol = %p", ret);
+
+ IDE_RETURN (ret);
+}
diff --git a/plugins/clang/ide-clang-translation-unit.h b/plugins/clang/ide-clang-translation-unit.h
index 8dd0107..47663b2 100644
--- a/plugins/clang/ide-clang-translation-unit.h
+++ b/plugins/clang/ide-clang-translation-unit.h
@@ -55,6 +55,9 @@ IdeSymbol *ide_clang_translation_unit_lookup_symbol (IdeClang
GError **error);
GPtrArray *ide_clang_translation_unit_get_symbols (IdeClangTranslationUnit *self,
IdeFile *file);
+IdeSymbol *ide_clang_translation_unit_find_nearest_scope (IdeClangTranslationUnit *self,
+ IdeSourceLocation *location,
+ GError **error);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]