ide-clang-code-indexer.c 8.91 KB
Newer Older
Anoop Chandu's avatar
Anoop Chandu committed
1
2
/* ide-clang-code-indexer.c
 *
3
 * Copyright © 2017 Anoop Chandu <anoopchandu96@gmail.com>
Anoop Chandu's avatar
Anoop Chandu committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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/>.
 */

#define G_LOG_DOMAIN "ide-clang-code-indexer"

#include <clang-c/Index.h>

23
24
25
#include "ide-clang-code-index-entries.h"
#include "ide-clang-code-indexer.h"
#include "ide-clang-private.h"
Anoop Chandu's avatar
Anoop Chandu committed
26
27
28
#include "ide-clang-service.h"
#include "ide-clang-translation-unit.h"

29
typedef struct
Anoop Chandu's avatar
Anoop Chandu committed
30
{
31
32
33
  GFile  *file;
  gchar **build_flags;
} BuildRequest;
Anoop Chandu's avatar
Anoop Chandu committed
34

35
36
static void
build_request_free (gpointer data)
Anoop Chandu's avatar
Anoop Chandu committed
37
{
38
  BuildRequest *br = data;
Anoop Chandu's avatar
Anoop Chandu committed
39

40
41
42
43
  g_clear_object (&br->file);
  g_clear_pointer (&br->build_flags, g_strfreev);
  g_slice_free (BuildRequest, br);
}
Anoop Chandu's avatar
Anoop Chandu committed
44

45
46
47
48
49
50
51
52
53
54
55
static void
ide_clang_code_indexer_index_file_worker (GTask        *task,
                                          gpointer      source_object,
                                          gpointer      task_data,
                                          GCancellable *cancellable)
{
  BuildRequest *br = task_data;
  g_auto(CXTranslationUnit) unit = NULL;
  g_auto(CXIndex) index = NULL;
  g_autofree gchar *path = NULL;
  enum CXErrorCode code;
Anoop Chandu's avatar
Anoop Chandu committed
56

57
58
59
60
61
62
63
64
65
66
67
68
69
  g_assert (G_IS_TASK (task));
  g_assert (IDE_IS_CLANG_CODE_INDEXER (source_object));
  g_assert (br != NULL);
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  path = g_file_get_path (br->file);
  index = clang_createIndex (0, 0);
  code = clang_parseTranslationUnit2 (index,
                                      path,
                                      (const char * const *)br->build_flags,
                                      g_strv_length (br->build_flags),
                                      NULL,
                                      0,
70
71
72
                                      (CXTranslationUnit_DetailedPreprocessingRecord |
#if CINDEX_VERSION >= CINDEX_VERSION_ENCODE(0, 35)
                                       CXTranslationUnit_SingleFileParse |
73
                                       CXTranslationUnit_KeepGoing |
74
#endif
75
76
77
78
                                       CXTranslationUnit_SkipFunctionBodies),
                                      &unit);

  if (code != CXError_Success)
Christian Hergert's avatar
Christian Hergert committed
79
80
81
82
83
84
85
86
87
88
89
    g_task_return_new_error (task,
                             G_IO_ERROR,
                             G_IO_ERROR_FAILED,
                             "Failed to index \"%s\"",
                             path);
  else
    g_task_return_pointer (task,
                           ide_clang_code_index_entries_new (g_steal_pointer (&index),
                                                             g_steal_pointer (&unit),
                                                             path),
                           g_object_unref);
Anoop Chandu's avatar
Anoop Chandu committed
90
91
}

92
93
94
95
96
97
98
static void
ide_clang_code_indexer_index_file_async (IdeCodeIndexer      *indexer,
                                         GFile               *file,
                                         const gchar * const *args,
                                         GCancellable        *cancellable,
                                         GAsyncReadyCallback  callback,
                                         gpointer             user_data)
Anoop Chandu's avatar
Anoop Chandu committed
99
100
{
  IdeClangCodeIndexer *self = (IdeClangCodeIndexer *)indexer;
101
102
  g_autoptr(GTask) task = NULL;
  BuildRequest *br;
Anoop Chandu's avatar
Anoop Chandu committed
103

104
105
106
  g_assert (IDE_IS_CLANG_CODE_INDEXER (self));
  g_assert (G_IS_FILE (file));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
Anoop Chandu's avatar
Anoop Chandu committed
107

108
109
110
  task = g_task_new (self, cancellable, callback, user_data);
  g_task_set_source_tag (task, ide_clang_code_indexer_index_file_async);
  g_task_set_priority (task, G_PRIORITY_LOW);
Anoop Chandu's avatar
Anoop Chandu committed
111

112
  if (!g_file_is_native (file))
Anoop Chandu's avatar
Anoop Chandu committed
113
    {
114
115
116
117
118
      g_task_return_new_error (task,
                               G_IO_ERROR,
                               G_IO_ERROR_NOT_SUPPORTED,
                               "Only native files are supported");
      return;
Anoop Chandu's avatar
Anoop Chandu committed
119
120
    }

121
122
123
124
  br = g_slice_new0 (BuildRequest);
  br->build_flags = g_strdupv ((gchar **)args);
  br->file = g_object_ref (file);
  g_task_set_task_data (task, br, build_request_free);
Anoop Chandu's avatar
Anoop Chandu committed
125

126
127
128
129
  ide_thread_pool_push_task (IDE_THREAD_POOL_INDEXER,
                             task,
                             ide_clang_code_indexer_index_file_worker);
}
Anoop Chandu's avatar
Anoop Chandu committed
130

131
132
133
134
135
136
137
static IdeCodeIndexEntries *
ide_clang_code_indexer_index_file_finish (IdeCodeIndexer  *indexer,
                                          GAsyncResult    *result,
                                          GError         **error)
{
  g_assert (IDE_IS_CLANG_CODE_INDEXER (indexer));
  g_assert (G_IS_TASK (result));
138

139
  return g_task_propagate_pointer (G_TASK (result), error);
Anoop Chandu's avatar
Anoop Chandu committed
140
141
142
143
144
145
146
147
148
149
150
}

static void
ide_clang_code_indexer_generate_key_cb (GObject       *object,
                                        GAsyncResult  *result,
                                        gpointer       user_data)
{
  IdeClangService *service = (IdeClangService *)object;
  g_autoptr(IdeClangTranslationUnit) unit = NULL;
  g_autoptr(GTask) task = user_data;
  g_autoptr(GError) error = NULL;
151
  g_autofree gchar *key = NULL;
Anoop Chandu's avatar
Anoop Chandu committed
152
153
154
  IdeSourceLocation *location;

  g_assert (IDE_IS_CLANG_SERVICE (service));
155
  g_assert (G_IS_ASYNC_RESULT (result));
Anoop Chandu's avatar
Anoop Chandu committed
156
157
  g_assert (G_IS_TASK (task));

158
  if (!(unit = ide_clang_service_get_translation_unit_finish (service, result, &error)))
Anoop Chandu's avatar
Anoop Chandu committed
159
160
161
162
163
164
    {
      g_task_return_error (task, g_steal_pointer (&error));
      return;
    }

  location = g_task_get_task_data (task);
165
  g_assert (location != NULL);
Anoop Chandu's avatar
Anoop Chandu committed
166

167
  if (!(key = ide_clang_translation_unit_generate_key (unit, location)))
Anoop Chandu's avatar
Anoop Chandu committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, "Key not found");
  else
    g_task_return_pointer (task, g_steal_pointer (&key), g_free);
}

static void
ide_clang_code_indexer_generate_key_async (IdeCodeIndexer       *indexer,
                                           IdeSourceLocation    *location,
                                           GCancellable         *cancellable,
                                           GAsyncReadyCallback   callback,
                                           gpointer              user_data)
{
  IdeClangCodeIndexer *self = (IdeClangCodeIndexer *)indexer;
  g_autoptr(GTask) task = NULL;
182
183
  IdeClangService *service;
  IdeContext *context;
Anoop Chandu's avatar
Anoop Chandu committed
184
185
186
187
188

  g_return_if_fail (IDE_IS_CLANG_CODE_INDEXER (self));
  g_return_if_fail (location != NULL);
  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));

189
190
191
192
193
  /*
   * The key to generate is what clang calls a "USR". That is a stable key that
   * can be referenced across compilation units.
   */

Anoop Chandu's avatar
Anoop Chandu committed
194
  task = g_task_new (self, cancellable, callback, user_data);
195
196
  g_task_set_source_tag (task, ide_clang_code_indexer_generate_key_async);
  g_task_set_priority (task, G_PRIORITY_LOW);
Anoop Chandu's avatar
Anoop Chandu committed
197
198
199
200
201
202

  g_task_set_task_data (task,
                        ide_source_location_ref (location),
                        (GDestroyNotify)ide_source_location_unref);

  context = ide_object_get_context (IDE_OBJECT (self));
203
  g_assert (IDE_IS_CONTEXT (context));
Anoop Chandu's avatar
Anoop Chandu committed
204

205
206
  service = ide_context_get_service_typed (context, IDE_TYPE_CLANG_SERVICE);
  g_assert (IDE_IS_CLANG_SERVICE (service));
Anoop Chandu's avatar
Anoop Chandu committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220

  ide_clang_service_get_translation_unit_async (service,
                                                ide_source_location_get_file (location),
                                                0,
                                                cancellable,
                                                ide_clang_code_indexer_generate_key_cb,
                                                g_steal_pointer (&task));
}

static gchar *
ide_clang_code_indexer_generate_key_finish (IdeCodeIndexer  *self,
                                            GAsyncResult    *result,
                                            GError         **error)
{
221
222
  g_assert (IDE_IS_CODE_INDEXER (self));
  g_assert (G_IS_TASK (result));
Anoop Chandu's avatar
Anoop Chandu committed
223

224
  return g_task_propagate_pointer (G_TASK (result), error);
Anoop Chandu's avatar
Anoop Chandu committed
225
226
227
}

static void
228
code_indexer_iface_init (IdeCodeIndexerInterface *iface)
Anoop Chandu's avatar
Anoop Chandu committed
229
{
230
231
232
233
  iface->index_file_async = ide_clang_code_indexer_index_file_async;
  iface->index_file_finish = ide_clang_code_indexer_index_file_finish;
  iface->generate_key_async = ide_clang_code_indexer_generate_key_async;
  iface->generate_key_finish = ide_clang_code_indexer_generate_key_finish;
Anoop Chandu's avatar
Anoop Chandu committed
234
235
}

236
struct _IdeClangCodeIndexer { IdeObject parent; };
Anoop Chandu's avatar
Anoop Chandu committed
237

238
239
G_DEFINE_TYPE_WITH_CODE (IdeClangCodeIndexer, ide_clang_code_indexer, IDE_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (IDE_TYPE_CODE_INDEXER, code_indexer_iface_init))
Anoop Chandu's avatar
Anoop Chandu committed
240
241

static void
242
ide_clang_code_indexer_class_init (IdeClangCodeIndexerClass *klass)
Anoop Chandu's avatar
Anoop Chandu committed
243
244
245
246
247
248
249
{
}

static void
ide_clang_code_indexer_init (IdeClangCodeIndexer *self)
{
}