~ubuntu-branches/ubuntu/trusty/geany/trusty

« back to all changes in this revision

Viewing changes to tagmanager/src/tm_symbol.c

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2013-05-10 15:27:35 UTC
  • mfrom: (3.3.12 sid)
  • Revision ID: package-import@ubuntu.com-20130510152735-vbuco9qgrjr61yhr
Tags: 1.23+dfsg-2
* Upload to unstable, fixes FTBFS (Closes: #707368)
* [a472a80] Enable parallel builds
* [17a6378] No-change bump of Standards-Version to 3.9.4
* [ea78f31] Add README.source describing git branch structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
*
 
3
*   Copyright (c) 2001-2002, Biswapesh Chattopadhyay
 
4
*
 
5
*   This source code is released for free distribution under the terms of the
 
6
*   GNU General Public License.
 
7
*
 
8
*/
 
9
 
 
10
#include <stdio.h>
 
11
#include <stdlib.h>
 
12
#include <string.h>
 
13
#include "tm_symbol.h"
 
14
 
 
15
 
 
16
#define SYM_NEW(T)      ((T) = g_slice_new0(TMSymbol))
 
17
#define SYM_FREE(T)     g_slice_free(TMSymbol, (T))
 
18
 
 
19
 
 
20
void tm_symbol_print(TMSymbol *sym, guint level)
 
21
{
 
22
        guint i;
 
23
 
 
24
        g_return_if_fail (sym != NULL);
 
25
        for (i=0; i < level; ++i)
 
26
                fputc('\t', stderr);
 
27
        fprintf(stderr, "%s\n", (sym->tag)?sym->tag->name:"Root");
 
28
        if (sym->info.children)
 
29
        {
 
30
                if (sym->tag && tm_tag_function_t == sym->tag->type)
 
31
                        tm_tag_print(sym->info.equiv, stderr);
 
32
                else
 
33
                {
 
34
                        for (i=0; i < sym->info.children->len; ++i)
 
35
                                tm_symbol_print(TM_SYMBOL(sym->info.children->pdata[i])
 
36
                                  , level + 1);
 
37
                }
 
38
        }
 
39
}
 
40
 
 
41
#define SYM_ORDER(T) (((tm_tag_class_t == (T)->type) || (tm_tag_struct_t ==\
 
42
        (T)->type))?1:(((tm_tag_enum_t == (T)->type) || (tm_tag_interface_t ==\
 
43
        (T)->type))?2:3))
 
44
 
 
45
/* Comparison function for sorting symbols alphabetically */
 
46
int tm_symbol_compare(const void *p1, const void *p2)
 
47
{
 
48
        TMSymbol *s1, *s2;
 
49
 
 
50
        if (!p1 && !p2)
 
51
                return 0;
 
52
        else if (!p2)
 
53
                return 1;
 
54
        else if (!p1)
 
55
                return -1;
 
56
        s1 = *(TMSymbol **) p1;
 
57
        s2 = *(TMSymbol **) p2;
 
58
        if (!s1 && !s2)
 
59
                return 0;
 
60
        else if (!s2)
 
61
                return 1;
 
62
        else if (!s1)
 
63
                return -1;
 
64
        if (!s1->tag && !s2->tag)
 
65
                return 0;
 
66
        else if (!s2->tag)
 
67
                return 1;
 
68
        else if (!s1->tag)
 
69
                return -1;
 
70
        return strcmp(s1->tag->name, s2->tag->name);
 
71
}
 
72
 
 
73
/*
 
74
 * Compares function argument lists.
 
75
 * FIXME: Compare based on types, not an exact string match.
 
76
 */
 
77
int tm_arglist_compare(const TMTag* t1, const TMTag* t2)
 
78
{
 
79
        return strcmp(NVL(t1->atts.entry.arglist, ""),
 
80
                        NVL(t2->atts.entry.arglist, ""));
 
81
}
 
82
 
 
83
/* Need this custom compare function to generate a symbol tree
 
84
in a simgle pass from tag list */
 
85
int tm_symbol_tag_compare(const TMTag **t1, const TMTag **t2)
 
86
{
 
87
        gint s1, s2;
 
88
 
 
89
        if (!t1 && !t2)
 
90
                return 0;
 
91
        if (t1 && t2 && !*t1 && !*t2)
 
92
                return 0;
 
93
        else if (!t1 || !*t1)
 
94
                return -1;
 
95
        else if (!t2 || !*t2)
 
96
                return 1;
 
97
        if ((tm_tag_file_t == (*t1)->type) && (tm_tag_file_t == (*t2)->type))
 
98
                return 0;
 
99
        else if (tm_tag_file_t == (*t1)->type)
 
100
                return -1;
 
101
        else if (tm_tag_file_t == (*t2)->type)
 
102
                return 1;
 
103
 
 
104
        /* Compare on depth of scope - less depth gets higher priortity */
 
105
        s1 = tm_tag_scope_depth(*t1);
 
106
        s2 = tm_tag_scope_depth(*t2);
 
107
        if (s1 != s2)
 
108
                return (s1 - s2);
 
109
 
 
110
        /* Compare of tag type using a symbol ordering routine */
 
111
        s1 = SYM_ORDER(*t1);
 
112
        s2 = SYM_ORDER(*t2);
 
113
        if (s1 != s2)
 
114
                return (s1 - s2);
 
115
 
 
116
        /* Compare names alphabetically */
 
117
        s1 = strcmp((*t1)->name, (*t2)->name);
 
118
        if (s1 != 0)
 
119
                return (s1);
 
120
 
 
121
        /* Compare scope alphabetically */
 
122
        s1 = strcmp(NVL((*t1)->atts.entry.scope, ""),
 
123
          NVL((*t2)->atts.entry.scope, ""));
 
124
        if (s1 != 0)
 
125
                return s1;
 
126
 
 
127
        /* If none of them are function/prototype, they are effectively equal */
 
128
        if ((tm_tag_function_t != (*t1)->type) &&
 
129
            (tm_tag_prototype_t != (*t1)->type)&&
 
130
            (tm_tag_function_t != (*t2)->type) &&
 
131
            (tm_tag_prototype_t != (*t2)->type))
 
132
                return 0;
 
133
 
 
134
        /* Whichever is not a function/prototype goes first */
 
135
        if ((tm_tag_function_t != (*t1)->type) &&
 
136
            (tm_tag_prototype_t != (*t1)->type))
 
137
                return -1;
 
138
        if ((tm_tag_function_t != (*t2)->type) &&
 
139
            (tm_tag_prototype_t != (*t2)->type))
 
140
                return 1;
 
141
 
 
142
        /* Compare the argument list */
 
143
        s1 = tm_arglist_compare(*t1, *t2);
 
144
        if (s1 != 0)
 
145
                return s1;
 
146
 
 
147
        /* Functions go before prototypes */
 
148
        if ((tm_tag_function_t == (*t1)->type) &&
 
149
            (tm_tag_function_t != (*t2)->type))
 
150
                return -1;
 
151
        if ((tm_tag_function_t != (*t1)->type) &&
 
152
            (tm_tag_function_t == (*t2)->type))
 
153
                return 1;
 
154
 
 
155
        /* Give up */
 
156
        return 0;
 
157
}
 
158
 
 
159
TMSymbol *tm_symbol_tree_new(GPtrArray *tags_array)
 
160
{
 
161
        TMSymbol *root = NULL;
 
162
        GPtrArray *tags;
 
163
 
 
164
#ifdef TM_DEBUG
 
165
        g_message("Building symbol tree..");
 
166
#endif
 
167
 
 
168
        if ((!tags_array) || (tags_array->len <= 0))
 
169
                return NULL;
 
170
 
 
171
#ifdef TM_DEBUG
 
172
        fprintf(stderr, "Dumping all tags..\n");
 
173
        tm_tags_array_print(tags_array, stderr);
 
174
#endif
 
175
 
 
176
        tags = tm_tags_extract(tags_array, tm_tag_max_t);
 
177
#ifdef TM_DEBUG
 
178
        fprintf(stderr, "Dumping unordered tags..\n");
 
179
        tm_tags_array_print(tags, stderr);
 
180
#endif
 
181
 
 
182
        if (tags && (tags->len > 0))
 
183
        {
 
184
                guint i;
 
185
                int j;
 
186
                int max_parents = -1;
 
187
                TMTag *tag;
 
188
                TMSymbol *sym = NULL, *sym1;
 
189
                char *parent_name;
 
190
                char *scope_end;
 
191
                gboolean matched;
 
192
                int str_match;
 
193
 
 
194
                SYM_NEW(root);
 
195
                tm_tags_custom_sort(tags, (TMTagCompareFunc) tm_symbol_tag_compare
 
196
                  , FALSE);
 
197
 
 
198
#ifdef TM_DEBUG
 
199
                fprintf(stderr, "Dumping ordered tags..");
 
200
                tm_tags_array_print(tags, stderr);
 
201
                fprintf(stderr, "Rebuilding symbol table..\n");
 
202
#endif
 
203
                for (i=0; i < tags->len; ++i)
 
204
                {
 
205
                        tag = TM_TAG(tags->pdata[i]);
 
206
 
 
207
                        if (tm_tag_prototype_t == tag->type)
 
208
                        {
 
209
                                if (sym && (tm_tag_function_t == sym->tag->type) &&
 
210
                                  (!sym->info.equiv) &&
 
211
                                  (0 == strcmp(NVL(tag->atts.entry.scope, "")
 
212
                                                         , NVL(sym->tag->atts.entry.scope, ""))))
 
213
                                {
 
214
                                        sym->info.equiv = tag;
 
215
                                        continue;
 
216
                                }
 
217
                        }
 
218
                        if (max_parents < 0)
 
219
                        {
 
220
                                if (SYM_ORDER(tag) > 2)
 
221
                                {
 
222
                                        max_parents = i;
 
223
                                        if (max_parents > 0)
 
224
                                                qsort(root->info.children->pdata, max_parents
 
225
                                                  , sizeof(gpointer), tm_symbol_compare);
 
226
                                }
 
227
                        }
 
228
                        SYM_NEW(sym);
 
229
                        sym->tag = tag;
 
230
                        if ((max_parents <= 0) || (!tag->atts.entry.scope))
 
231
                        {
 
232
                                sym->parent = root;
 
233
                                if (!root->info.children)
 
234
                                        root->info.children = g_ptr_array_new();
 
235
                                g_ptr_array_add(root->info.children, sym);
 
236
                        }
 
237
                        else
 
238
                        {
 
239
                                parent_name = tag->atts.entry.scope;
 
240
                                scope_end = strstr(tag->atts.entry.scope, "::");
 
241
                                if (scope_end)
 
242
                                        *scope_end = '\0';
 
243
                                matched = FALSE;
 
244
                                if (('\0' != parent_name[0]) &&
 
245
                                  (0 != strcmp(parent_name, "<anonymous>")))
 
246
                                {
 
247
                                        for (j=0; j < max_parents; ++j)
 
248
                                        {
 
249
                                                sym1 = TM_SYMBOL(root->info.children->pdata[j]);
 
250
                                                str_match = strcmp(sym1->tag->name, parent_name);
 
251
                                                if (str_match == 0)
 
252
                                                {
 
253
                                                        matched = TRUE;
 
254
                                                        sym->parent = sym1;
 
255
                                                        if (!sym1->info.children)
 
256
                                                                sym1->info.children = g_ptr_array_new();
 
257
                                                        g_ptr_array_add(sym1->info.children, sym);
 
258
                                                        break;
 
259
                                                }
 
260
                                                else if (str_match > 0)
 
261
                                                        break;
 
262
                                        }
 
263
                                }
 
264
                                if (!matched)
 
265
                                {
 
266
                                        sym->parent = root;
 
267
                                        if (!root->info.children)
 
268
                                                root->info.children = g_ptr_array_new();
 
269
                                        g_ptr_array_add(root->info.children, sym);
 
270
                                }
 
271
                                if (scope_end)
 
272
                                        *scope_end = ':';
 
273
                        }
 
274
                }
 
275
#ifdef TM_DEBUG
 
276
                fprintf(stderr, "Done.Dumping symbol tree..");
 
277
                tm_symbol_print(root, 0);
 
278
#endif
 
279
        }
 
280
        if (tags)
 
281
                g_ptr_array_free(tags, TRUE);
 
282
 
 
283
        return root;
 
284
}
 
285
 
 
286
static void tm_symbol_free(TMSymbol *sym)
 
287
{
 
288
        if (!sym)
 
289
                return;
 
290
        if ((!sym->tag) || ((tm_tag_function_t != sym->tag->type) &&
 
291
                    (tm_tag_prototype_t != sym->tag->type)))
 
292
        {
 
293
                if (sym->info.children)
 
294
                {
 
295
                        guint i;
 
296
                        for (i=0; i < sym->info.children->len; ++i)
 
297
                                tm_symbol_free(TM_SYMBOL(sym->info.children->pdata[i]));
 
298
                        g_ptr_array_free(sym->info.children, TRUE);
 
299
                        sym->info.children = NULL;
 
300
                }
 
301
        }
 
302
        SYM_FREE(sym);
 
303
}
 
304
 
 
305
void tm_symbol_tree_free(gpointer root)
 
306
{
 
307
        if (root)
 
308
                tm_symbol_free(TM_SYMBOL(root));
 
309
}
 
310
 
 
311
TMSymbol *tm_symbol_tree_update(TMSymbol *root, GPtrArray *tags)
 
312
{
 
313
        if (root)
 
314
                tm_symbol_free(root);
 
315
        if ((tags) && (tags->len > 0))
 
316
                return tm_symbol_tree_new(tags);
 
317
        else
 
318
                return NULL;
 
319
}