-
Notifications
You must be signed in to change notification settings - Fork 587
/
tm_tag.c
1316 lines (1203 loc) · 35.7 KB
/
tm_tag.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright (c) 2001-2002, Biswapesh Chattopadhyay
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
*/
#include <stdlib.h>
#include <string.h>
#include <glib-object.h>
#include "general.h"
#include "entry.h"
#include "parse.h"
#include "read.h"
#define LIBCTAGS_DEFINED
#include "tm_tag.h"
#define TAG_NEW(T) ((T) = g_slice_new0(TMTag))
#define TAG_FREE(T) g_slice_free(TMTag, (T))
#ifdef DEBUG_TAG_REFS
static GHashTable *alive_tags = NULL;
static void foreach_tags_log(gpointer key, gpointer value, gpointer data)
{
gsize *ref_count = data;
const TMTag *tag = value;
*ref_count += (gsize) tag->refcount;
g_debug("Leaked TMTag (%d refs): %s", tag->refcount, tag->name);
}
static void log_refs_at_exit(void)
{
gsize ref_count = 0;
g_hash_table_foreach(alive_tags, foreach_tags_log, &ref_count);
g_debug("TMTag references left at exit: %lu", ref_count);
}
static TMTag *log_tag_new(void)
{
TMTag *tag;
if (! alive_tags)
{
alive_tags = g_hash_table_new(g_direct_hash, g_direct_equal);
atexit(log_refs_at_exit);
}
TAG_NEW(tag);
g_hash_table_insert(alive_tags, tag, tag);
return tag;
}
static void log_tag_free(TMTag *tag)
{
g_return_if_fail(alive_tags != NULL);
if (! g_hash_table_remove(alive_tags, tag)) {
g_critical("Freeing invalid TMTag pointer %p", (void *) tag);
} else {
TAG_FREE(tag);
}
}
#undef TAG_NEW
#undef TAG_FREE
#define TAG_NEW(T) ((T) = log_tag_new())
#define TAG_FREE(T) log_tag_free(T)
#endif /* DEBUG_TAG_REFS */
const TMTagType TM_GLOBAL_TYPE_MASK =
tm_tag_class_t | tm_tag_enum_t | tm_tag_interface_t |
tm_tag_struct_t | tm_tag_typedef_t | tm_tag_union_t | tm_tag_namespace_t;
/* Note: To preserve binary compatibility, it is very important
that you only *append* to this list ! */
enum
{
TA_NAME = 200,
TA_LINE,
TA_LOCAL,
TA_POS, /* Obsolete */
TA_TYPE,
TA_ARGLIST,
TA_SCOPE,
TA_VARTYPE,
TA_INHERITS,
TA_TIME,
TA_ACCESS,
TA_IMPL,
TA_LANG,
TA_INACTIVE, /* Obsolete */
TA_POINTER
};
typedef struct
{
guint *sort_attrs;
gboolean partial;
} TMSortOptions;
static const char *s_tag_type_names[] = {
"class", /* classes */
"enum", /* enumeration names */
"enumerator", /* enumerators (values inside an enumeration) */
"externvar", /* external variable declarations */
"field", /* fields */
"function", /* function definitions */
"interface", /* interfaces */
"macro", /* macro definitions */
"member", /* class, struct, and union members */
"method", /* methods */
"namespace", /* namespaces */
"package", /* packages */
"prototype", /* function prototypes */
"struct", /* structure names */
"typedef", /* typedefs */
"union", /* union names */
"variable", /* variable definitions */
"other" /* Other tag type (non C/C++/Java) */
};
static TMTagType s_tag_types[] = {
tm_tag_class_t,
tm_tag_enum_t,
tm_tag_enumerator_t,
tm_tag_externvar_t,
tm_tag_field_t,
tm_tag_function_t,
tm_tag_interface_t,
tm_tag_macro_t,
tm_tag_member_t,
tm_tag_method_t,
tm_tag_namespace_t,
tm_tag_package_t,
tm_tag_prototype_t,
tm_tag_struct_t,
tm_tag_typedef_t,
tm_tag_union_t,
tm_tag_variable_t,
tm_tag_other_t
};
/* Gets the GType for a TMTag */
GType tm_tag_get_type(void)
{
static GType gtype = 0;
if (G_UNLIKELY (gtype == 0))
{
gtype = g_boxed_type_register_static("TMTag", (GBoxedCopyFunc)tm_tag_ref,
(GBoxedFreeFunc)tm_tag_unref);
}
return gtype;
}
static TMTagType get_tag_type(const char *tag_name)
{
unsigned int i;
int cmp;
g_return_val_if_fail(tag_name, 0);
for (i=0; i < sizeof(s_tag_type_names)/sizeof(char *); ++i)
{
cmp = strcmp(tag_name, s_tag_type_names[i]);
if (0 == cmp)
return s_tag_types[i];
else if (cmp < 0)
break;
}
/* other is not checked above as it is last, not sorted alphabetically */
if (strcmp(tag_name, "other") == 0)
return tm_tag_other_t;
#ifdef TM_DEBUG
fprintf(stderr, "Unknown tag type %s\n", tag_name);
#endif
return tm_tag_undef_t;
}
static char get_tag_impl(const char *impl)
{
if ((0 == strcmp("virtual", impl))
|| (0 == strcmp("pure virtual", impl)))
return TAG_IMPL_VIRTUAL;
#ifdef TM_DEBUG
g_warning("Unknown implementation %s", impl);
#endif
return TAG_IMPL_UNKNOWN;
}
static char get_tag_access(const char *access)
{
if (0 == strcmp("public", access))
return TAG_ACCESS_PUBLIC;
else if (0 == strcmp("protected", access))
return TAG_ACCESS_PROTECTED;
else if (0 == strcmp("private", access))
return TAG_ACCESS_PRIVATE;
else if (0 == strcmp("friend", access))
return TAG_ACCESS_FRIEND;
else if (0 == strcmp("default", access))
return TAG_ACCESS_DEFAULT;
#ifdef TM_DEBUG
g_warning("Unknown access type %s", access);
#endif
return TAG_ACCESS_UNKNOWN;
}
/*
Initializes a TMTag structure with information from a tagEntryInfo struct
used by the ctags parsers. Note that the TMTag structure must be malloc()ed
before calling this function. This function is called by tm_tag_new() - you
should not need to call this directly.
@param tag The TMTag structure to initialize
@param file Pointer to a TMSourceFile struct (it is assigned to the file member)
@param tag_entry Tag information gathered by the ctags parser
@return TRUE on success, FALSE on failure
*/
static gboolean tm_tag_init(TMTag *tag, TMSourceFile *file, const tagEntryInfo *tag_entry)
{
tag->refcount = 1;
if (NULL == tag_entry)
return FALSE;
/* This is a normal tag entry */
if (NULL == tag_entry->name)
return FALSE;
tag->name = g_strdup(tag_entry->name);
tag->type = get_tag_type(tag_entry->kindName);
tag->local = tag_entry->isFileScope;
tag->pointerOrder = 0; /* backward compatibility (use var_type instead) */
tag->line = tag_entry->lineNumber;
if (NULL != tag_entry->extensionFields.arglist)
tag->arglist = g_strdup(tag_entry->extensionFields.arglist);
if ((NULL != tag_entry->extensionFields.scope[1]) &&
(isalpha(tag_entry->extensionFields.scope[1][0]) ||
tag_entry->extensionFields.scope[1][0] == '_' ||
tag_entry->extensionFields.scope[1][0] == '$'))
tag->scope = g_strdup(tag_entry->extensionFields.scope[1]);
if (tag_entry->extensionFields.inheritance != NULL)
tag->inheritance = g_strdup(tag_entry->extensionFields.inheritance);
if (tag_entry->extensionFields.varType != NULL)
tag->var_type = g_strdup(tag_entry->extensionFields.varType);
if (tag_entry->extensionFields.access != NULL)
tag->access = get_tag_access(tag_entry->extensionFields.access);
if (tag_entry->extensionFields.implementation != NULL)
tag->impl = get_tag_impl(tag_entry->extensionFields.implementation);
if ((tm_tag_macro_t == tag->type) && (NULL != tag->arglist))
tag->type = tm_tag_macro_with_arg_t;
tag->file = file;
tag->lang = file->lang;
return TRUE;
}
/*
Creates a new tag structure from a tagEntryInfo pointer and a TMSOurceFile pointer
and returns a pointer to it.
@param file - Pointer to the TMSourceFile structure containing the tag
@param tag_entry Contains tag information generated by ctags
@return the new TMTag structure. This should be free()-ed using tm_tag_free()
*/
TMTag *tm_tag_new(TMSourceFile *file, const tagEntryInfo *tag_entry)
{
TMTag *tag;
TAG_NEW(tag);
if (FALSE == tm_tag_init(tag, file, tag_entry))
{
TAG_FREE(tag);
return NULL;
}
return tag;
}
/*
Initializes an already malloc()ed TMTag structure by reading a tag entry
line from a file. The structure should be allocated beforehand.
@param tag The TMTag structure to populate
@param file The TMSourceFile struct (assigned to the file member)
@param fp FILE pointer from where the tag line is read
@return TRUE on success, FALSE on FAILURE
*/
static gboolean tm_tag_init_from_file(TMTag *tag, TMSourceFile *file, FILE *fp)
{
guchar buf[BUFSIZ];
guchar *start, *end;
gboolean status;
guchar changed_char = TA_NAME;
tag->refcount = 1;
if ((NULL == fgets((gchar*)buf, BUFSIZ, fp)) || ('\0' == *buf))
return FALSE;
for (start = end = buf, status = TRUE; (TRUE == status); start = end, ++ end)
{
while ((*end < TA_NAME) && (*end != '\0') && (*end != '\n'))
++ end;
if (('\0' == *end) || ('\n' == *end))
status = FALSE;
changed_char = *end;
*end = '\0';
if (NULL == tag->name)
{
if (!isprint(*start))
return FALSE;
else
tag->name = g_strdup((gchar*)start);
}
else
{
switch (*start)
{
case TA_LINE:
tag->line = atol((gchar*)start + 1);
break;
case TA_LOCAL:
tag->local = atoi((gchar*)start + 1);
break;
case TA_TYPE:
tag->type = (TMTagType) atoi((gchar*)start + 1);
break;
case TA_ARGLIST:
tag->arglist = g_strdup((gchar*)start + 1);
break;
case TA_SCOPE:
tag->scope = g_strdup((gchar*)start + 1);
break;
case TA_POINTER:
tag->pointerOrder = atoi((gchar*)start + 1);
break;
case TA_VARTYPE:
tag->var_type = g_strdup((gchar*)start + 1);
break;
case TA_INHERITS:
tag->inheritance = g_strdup((gchar*)start + 1);
break;
case TA_TIME: /* Obsolete */
break;
case TA_LANG: /* Obsolete */
break;
case TA_INACTIVE: /* Obsolete */
break;
case TA_ACCESS:
tag->access = (char) *(start + 1);
break;
case TA_IMPL:
tag->impl = (char) *(start + 1);
break;
default:
#ifdef GEANY_DEBUG
g_warning("Unknown attribute %s", start + 1);
#endif
break;
}
}
*end = changed_char;
}
if (NULL == tag->name)
return FALSE;
tag->file = file;
return TRUE;
}
/* alternative parser for Pascal and LaTeX global tags files with the following format
* tagname|return value|arglist|description\n */
static gboolean tm_tag_init_from_file_alt(TMTag *tag, TMSourceFile *file, FILE *fp)
{
guchar buf[BUFSIZ];
guchar *start, *end;
gboolean status;
/*guchar changed_char = TA_NAME;*/
tag->refcount = 1;
if ((NULL == fgets((gchar*)buf, BUFSIZ, fp)) || ('\0' == *buf))
return FALSE;
{
gchar **fields;
guint field_len;
for (start = end = buf, status = TRUE; (TRUE == status); start = end, ++ end)
{
while ((*end < TA_NAME) && (*end != '\0') && (*end != '\n'))
++ end;
if (('\0' == *end) || ('\n' == *end))
status = FALSE;
/*changed_char = *end;*/
*end = '\0';
if (NULL == tag->name && !isprint(*start))
return FALSE;
fields = g_strsplit((gchar*)start, "|", -1);
field_len = g_strv_length(fields);
if (field_len >= 1) tag->name = g_strdup(fields[0]);
else tag->name = NULL;
if (field_len >= 2 && fields[1] != NULL) tag->var_type = g_strdup(fields[1]);
if (field_len >= 3 && fields[2] != NULL) tag->arglist = g_strdup(fields[2]);
tag->type = tm_tag_prototype_t;
g_strfreev(fields);
}
}
if (NULL == tag->name)
return FALSE;
tag->file = file;
return TRUE;
}
/*
Same as tm_tag_init_from_file(), but parsing CTags tag file format
(http://ctags.sourceforge.net/FORMAT)
*/
static gboolean tm_tag_init_from_file_ctags(TMTag *tag, TMSourceFile *file, FILE *fp)
{
gchar buf[BUFSIZ];
gchar *p, *tab;
tag->refcount = 1;
tag->type = tm_tag_function_t; /* default type is function if no kind is specified */
do
{
if ((NULL == fgets(buf, BUFSIZ, fp)) || ('\0' == *buf))
return FALSE;
}
while (strncmp(buf, "!_TAG_", 6) == 0); /* skip !_TAG_ lines */
p = buf;
/* tag name */
if (! (tab = strchr(p, '\t')) || p == tab)
return FALSE;
tag->name = g_strndup(p, (gsize)(tab - p));
p = tab + 1;
/* tagfile, unused */
if (! (tab = strchr(p, '\t')))
{
g_free(tag->name);
tag->name = NULL;
return FALSE;
}
p = tab + 1;
/* Ex command, unused */
if (*p == '/' || *p == '?')
{
gchar c = *p;
for (++p; *p && *p != c; p++)
{
if (*p == '\\' && p[1])
p++;
}
}
else /* assume a line */
tag->line = atol(p);
tab = strstr(p, ";\"");
/* read extension fields */
if (tab)
{
p = tab + 2;
while (*p && *p != '\n' && *p != '\r')
{
gchar *end;
const gchar *key, *value = NULL;
/* skip leading tabulations */
while (*p && *p == '\t') p++;
/* find the separator (:) and end (\t) */
key = end = p;
while (*end && *end != '\t' && *end != '\n' && *end != '\r')
{
if (*end == ':' && ! value)
{
*end = 0; /* terminate the key */
value = end + 1;
}
end++;
}
/* move p paste the so we won't stop parsing by setting *end=0 below */
p = *end ? end + 1 : end;
*end = 0; /* terminate the value (or key if no value) */
if (! value || 0 == strcmp(key, "kind")) /* tag kind */
{
const gchar *kind = value ? value : key;
if (kind[0] && kind[1])
tag->type = get_tag_type(kind);
else
{
switch (*kind)
{
case 'c': tag->type = tm_tag_class_t; break;
case 'd': tag->type = tm_tag_macro_t; break;
case 'e': tag->type = tm_tag_enumerator_t; break;
case 'F': tag->type = tm_tag_other_t; break; /* Obsolete */
case 'f': tag->type = tm_tag_function_t; break;
case 'g': tag->type = tm_tag_enum_t; break;
case 'I': tag->type = tm_tag_class_t; break;
case 'i': tag->type = tm_tag_interface_t; break;
case 'l': tag->type = tm_tag_variable_t; break;
case 'M': tag->type = tm_tag_macro_t; break;
case 'm': tag->type = tm_tag_member_t; break;
case 'n': tag->type = tm_tag_namespace_t; break;
case 'P': tag->type = tm_tag_package_t; break;
case 'p': tag->type = tm_tag_prototype_t; break;
case 's': tag->type = tm_tag_struct_t; break;
case 't': tag->type = tm_tag_typedef_t; break;
case 'u': tag->type = tm_tag_union_t; break;
case 'v': tag->type = tm_tag_variable_t; break;
case 'x': tag->type = tm_tag_externvar_t; break;
default:
#ifdef TM_DEBUG
g_warning("Unknown tag kind %c", *kind);
#endif
tag->type = tm_tag_other_t; break;
}
}
}
else if (0 == strcmp(key, "inherits")) /* comma-separated list of classes this class inherits from */
{
g_free(tag->inheritance);
tag->inheritance = g_strdup(value);
}
else if (0 == strcmp(key, "implementation")) /* implementation limit */
tag->impl = get_tag_impl(value);
else if (0 == strcmp(key, "line")) /* line */
tag->line = atol(value);
else if (0 == strcmp(key, "access")) /* access */
tag->access = get_tag_access(value);
else if (0 == strcmp(key, "class") ||
0 == strcmp(key, "enum") ||
0 == strcmp(key, "function") ||
0 == strcmp(key, "struct") ||
0 == strcmp(key, "union")) /* Name of the class/enum/function/struct/union in which this tag is a member */
{
g_free(tag->scope);
tag->scope = g_strdup(value);
}
else if (0 == strcmp(key, "file")) /* static (local) tag */
tag->local = TRUE;
else if (0 == strcmp(key, "signature")) /* arglist */
{
g_free(tag->arglist);
tag->arglist = g_strdup(value);
}
}
}
tag->file = file;
return TRUE;
}
/*
Same as tm_tag_new() except that the tag attributes are read from file.
@param mode langType to use for the tag.
*/
TMTag *tm_tag_new_from_file(TMSourceFile *file, FILE *fp, gint mode, TMFileFormat format)
{
TMTag *tag;
gboolean result = FALSE;
TAG_NEW(tag);
switch (format)
{
case TM_FILE_FORMAT_TAGMANAGER:
result = tm_tag_init_from_file(tag, file, fp);
break;
case TM_FILE_FORMAT_PIPE:
result = tm_tag_init_from_file_alt(tag, file, fp);
break;
case TM_FILE_FORMAT_CTAGS:
result = tm_tag_init_from_file_ctags(tag, file, fp);
break;
}
if (! result)
{
TAG_FREE(tag);
return NULL;
}
tag->lang = mode;
return tag;
}
/*
Writes tag information to the given FILE *.
@param tag The tag information to write.
@param file FILE pointer to which the tag information is written.
@param attrs Attributes to be written (bitmask).
@return TRUE on success, FALSE on failure.
*/
gboolean tm_tag_write(TMTag *tag, FILE *fp, TMTagAttrType attrs)
{
fprintf(fp, "%s", tag->name);
if (attrs & tm_tag_attr_type_t)
fprintf(fp, "%c%d", TA_TYPE, tag->type);
if ((attrs & tm_tag_attr_arglist_t) && (NULL != tag->arglist))
fprintf(fp, "%c%s", TA_ARGLIST, tag->arglist);
if (attrs & tm_tag_attr_line_t)
fprintf(fp, "%c%ld", TA_LINE, tag->line);
if (attrs & tm_tag_attr_local_t)
fprintf(fp, "%c%d", TA_LOCAL, tag->local);
if ((attrs & tm_tag_attr_scope_t) && (NULL != tag->scope))
fprintf(fp, "%c%s", TA_SCOPE, tag->scope);
if ((attrs & tm_tag_attr_inheritance_t) && (NULL != tag->inheritance))
fprintf(fp, "%c%s", TA_INHERITS, tag->inheritance);
if (attrs & tm_tag_attr_pointer_t)
fprintf(fp, "%c%d", TA_POINTER, tag->pointerOrder);
if ((attrs & tm_tag_attr_vartype_t) && (NULL != tag->var_type))
fprintf(fp, "%c%s", TA_VARTYPE, tag->var_type);
if ((attrs & tm_tag_attr_access_t) && (TAG_ACCESS_UNKNOWN != tag->access))
fprintf(fp, "%c%c", TA_ACCESS, tag->access);
if ((attrs & tm_tag_attr_impl_t) && (TAG_IMPL_UNKNOWN != tag->impl))
fprintf(fp, "%c%c", TA_IMPL, tag->impl);
if (fprintf(fp, "\n"))
return TRUE;
else
return FALSE;
}
/*
Destroys a TMTag structure, i.e. frees all elements except the tag itself.
@param tag The TMTag structure to destroy
@see tm_tag_free()
*/
static void tm_tag_destroy(TMTag *tag)
{
g_free(tag->name);
g_free(tag->arglist);
g_free(tag->scope);
g_free(tag->inheritance);
g_free(tag->var_type);
}
/*
Drops a reference from a TMTag. If the reference count reaches 0, this function
destroys all data in the tag and frees the tag structure as well.
@param tag Pointer to a TMTag structure
*/
void tm_tag_unref(TMTag *tag)
{
/* be NULL-proof because tm_tag_free() was NULL-proof and we indent to be a
* drop-in replacment of it */
if (NULL != tag && g_atomic_int_dec_and_test(&tag->refcount))
{
tm_tag_destroy(tag);
TAG_FREE(tag);
}
}
/*
Adds a reference to a TMTag.
@param tag Pointer to a TMTag structure
@return the passed-in TMTag
*/
TMTag *tm_tag_ref(TMTag *tag)
{
g_atomic_int_inc(&tag->refcount);
return tag;
}
/*
Inbuilt tag comparison function.
*/
static gint tm_tag_compare(gconstpointer ptr1, gconstpointer ptr2, gpointer user_data)
{
unsigned int *sort_attr;
int returnval = 0;
TMTag *t1 = *((TMTag **) ptr1);
TMTag *t2 = *((TMTag **) ptr2);
TMSortOptions *sort_options = user_data;
if ((NULL == t1) || (NULL == t2))
{
g_warning("Found NULL tag");
return t2 - t1;
}
if (NULL == sort_options->sort_attrs)
{
if (sort_options->partial)
return strncmp(FALLBACK(t1->name, ""), FALLBACK(t2->name, ""), strlen(FALLBACK(t1->name, "")));
else
return strcmp(FALLBACK(t1->name, ""), FALLBACK(t2->name, ""));
}
for (sort_attr = sort_options->sort_attrs; returnval == 0 && *sort_attr != tm_tag_attr_none_t; ++ sort_attr)
{
switch (*sort_attr)
{
case tm_tag_attr_name_t:
if (sort_options->partial)
returnval = strncmp(FALLBACK(t1->name, ""), FALLBACK(t2->name, ""), strlen(FALLBACK(t1->name, "")));
else
returnval = strcmp(FALLBACK(t1->name, ""), FALLBACK(t2->name, ""));
break;
case tm_tag_attr_file_t:
returnval = t1->file - t2->file;
break;
case tm_tag_attr_line_t:
returnval = t1->line - t2->line;
break;
case tm_tag_attr_type_t:
returnval = t1->type - t2->type;
break;
case tm_tag_attr_scope_t:
returnval = strcmp(FALLBACK(t1->scope, ""), FALLBACK(t2->scope, ""));
break;
case tm_tag_attr_arglist_t:
returnval = strcmp(FALLBACK(t1->arglist, ""), FALLBACK(t2->arglist, ""));
if (returnval != 0)
{
int line_diff = (t1->line - t2->line);
returnval = line_diff ? line_diff : returnval;
}
break;
case tm_tag_attr_vartype_t:
returnval = strcmp(FALLBACK(t1->var_type, ""), FALLBACK(t2->var_type, ""));
break;
}
}
return returnval;
}
/*
Removes NULL tag entries from an array of tags. Called after tm_tags_dedup() since
this function substitutes duplicate entries with NULL
@param tags_array Array of tags to dedup
@return TRUE on success, FALSE on failure
*/
gboolean tm_tags_prune(GPtrArray *tags_array)
{
guint i, count;
for (i=0, count = 0; i < tags_array->len; ++i)
{
if (NULL != tags_array->pdata[i])
tags_array->pdata[count++] = tags_array->pdata[i];
}
tags_array->len = count;
return TRUE;
}
/*
Deduplicates an array on tags using the inbuilt comparison function based on
the attributes specified. Called by tm_tags_sort() when dedup is TRUE.
@param tags_array Array of tags to dedup.
@param sort_attributes Attributes the array is sorted on. They will be deduped
on the same criteria.
@return TRUE on success, FALSE on failure
*/
gboolean tm_tags_dedup(GPtrArray *tags_array, TMTagAttrType *sort_attributes, gboolean unref_duplicates)
{
TMSortOptions sort_options;
guint i;
if ((!tags_array) || (!tags_array->len))
return TRUE;
sort_options.sort_attrs = sort_attributes;
sort_options.partial = FALSE;
for (i = 1; i < tags_array->len; ++i)
{
if (0 == tm_tag_compare(&(tags_array->pdata[i - 1]), &(tags_array->pdata[i]), &sort_options))
{
if (unref_duplicates)
tm_tag_unref(tags_array->pdata[i-1]);
tags_array->pdata[i-1] = NULL;
}
}
tm_tags_prune(tags_array);
return TRUE;
}
/*
Sort an array of tags on the specified attribuites using the inbuilt comparison
function.
@param tags_array The array of tags to be sorted
@param sort_attributes Attributes to be sorted on (int array terminated by 0)
@param dedup Whether to deduplicate the sorted array
@return TRUE on success, FALSE on failure
*/
gboolean tm_tags_sort(GPtrArray *tags_array, TMTagAttrType *sort_attributes,
gboolean dedup, gboolean unref_duplicates)
{
TMSortOptions sort_options;
if ((!tags_array) || (!tags_array->len))
return TRUE;
sort_options.sort_attrs = sort_attributes;
sort_options.partial = FALSE;
g_ptr_array_sort_with_data(tags_array, tm_tag_compare, &sort_options);
if (dedup)
tm_tags_dedup(tags_array, sort_attributes, unref_duplicates);
return TRUE;
}
void tm_tags_remove_file_tags(TMSourceFile *source_file, GPtrArray *tags_array)
{
guint i;
GPtrArray *to_delete = g_ptr_array_sized_new(source_file->tags_array->len);
for (i = 0; i < source_file->tags_array->len; i++)
{
guint j;
guint tag_count;
TMTag **found;
TMTag *tag = source_file->tags_array->pdata[i];
found = tm_tags_find(tags_array, tag->name, FALSE, TRUE, &tag_count);
for (j = 0; j < tag_count; j++)
{
if (*found != NULL && (*found)->file == source_file)
{
/* we cannot set the pointer to NULL now because the search wouldn't work */
g_ptr_array_add(to_delete, found);
/* no break - if there are multiple tags of the same name, we would
* always find the first instance and wouldn't remove others; duplicates
* in the to_delete list aren't a problem */
}
found++;
}
}
for (i = 0; i < to_delete->len; i++)
{
TMTag **tag = to_delete->pdata[i];
*tag = NULL;
}
g_ptr_array_free(to_delete, TRUE);
tm_tags_prune(tags_array);
}
/* Optimized merge sort for merging sorted values from one array to another
* where one of the arrays is much smaller than the other.
* The merge complexity depends mostly on the size of the small array
* and is almost independent of the size of the big array.
* In addition, get rid of the duplicates (if both big_array and small_array are duplicate-free). */
static GPtrArray *merge(GPtrArray *big_array, GPtrArray *small_array,
TMSortOptions *sort_options, gboolean unref_duplicates) {
guint i1 = 0; /* index to big_array */
guint i2 = 0; /* index to small_array */
guint initial_step;
guint step;
GPtrArray *res_array = g_ptr_array_sized_new(big_array->len + small_array->len);
#ifdef TM_DEBUG
guint cmpnum = 0;
#endif
/* swap the arrays if len(small) > len(big) */
if (small_array->len > big_array->len)
{
GPtrArray *tmp = small_array;
small_array = big_array;
big_array = tmp;
}
/* on average, we are merging a value from small_array every
* len(big_array) / len(small_array) values - good approximation for fast jump
* step size */
initial_step = (small_array->len > 0) ? big_array->len / small_array->len : 1;
initial_step = initial_step > 4 ? initial_step : 1;
step = initial_step;
while (i1 < big_array->len && i2 < small_array->len)
{
gpointer val1;
gpointer val2 = small_array->pdata[i2];
if (step > 4) /* fast path start */
{
guint j1 = (i1 + step < big_array->len) ? i1 + step : big_array->len - 1;
val1 = big_array->pdata[j1];
#ifdef TM_DEBUG
cmpnum++;
#endif
/* if the value in big_array after making the big step is still smaller
* than the value in small_array, we can copy all the values inbetween
* into the result without making expensive string comparisons */
if (tm_tag_compare(&val1, &val2, sort_options) < 0)
{
while (i1 <= j1)
{
val1 = big_array->pdata[i1];
/* we allocated enough space so we are sure we don't need to reallocate
* the array - copy and increment the size directly so it can be inlined */
res_array->pdata[res_array->len++] = val1;
i1++;
}
}
else
{
/* lower the step and try again */
step /= 2;
}
} /* fast path end */
else
{
gint cmpval;
#ifdef TM_DEBUG
cmpnum++;
#endif
val1 = big_array->pdata[i1];
cmpval = tm_tag_compare(&val1, &val2, sort_options);
if (cmpval < 0)
{
g_ptr_array_add(res_array, val1);
i1++;
}
else
{
g_ptr_array_add(res_array, val2);
i2++;
/* value from small_array gets merged - reset the step size */
step = initial_step;
if (cmpval == 0)
{
i1++; /* remove the duplicate, keep just the newly merged value */
if (unref_duplicates)
tm_tag_unref(val1);
}
}
}
}
/* end of one of the arrays reached - copy the rest from the other array */
while (i1 < big_array->len)
g_ptr_array_add(res_array, big_array->pdata[i1++]);
while (i2 < small_array->len)
g_ptr_array_add(res_array, small_array->pdata[i2++]);
#ifdef TM_DEBUG
printf("cmpnums: %u\n", cmpnum);
printf("total tags: %u\n", big_array->len);
printf("merged tags: %u\n\n", small_array->len);
#endif
return res_array;
}
GPtrArray *tm_tags_merge(GPtrArray *big_array, GPtrArray *small_array,
TMTagAttrType *sort_attributes, gboolean unref_duplicates)
{
GPtrArray *res_array;
TMSortOptions sort_options;
sort_options.sort_attrs = sort_attributes;
sort_options.partial = FALSE;
res_array = merge(big_array, small_array, &sort_options, unref_duplicates);
return res_array;
}
/*
This function will extract the tags of the specified types from an array of tags.
The returned value is a GPtrArray which should be free-d with a call to
g_ptr_array_free(array, TRUE). However, do not free the tags themselves since they
are not duplicated.
@param tags_array The original array of tags
@param tag_types - The tag types to extract. Can be a bitmask. For example, passing
(tm_tag_typedef_t | tm_tag_struct_t) will extract all typedefs and structures from
the original array.
@return an array of tags (NULL on failure)
*/
GPtrArray *tm_tags_extract(GPtrArray *tags_array, TMTagType tag_types)
{
GPtrArray *new_tags;
guint i;
if (NULL == tags_array)
return NULL;
new_tags = g_ptr_array_new();
for (i=0; i < tags_array->len; ++i)
{
if (NULL != tags_array->pdata[i])
{
if (tag_types & (((TMTag *) tags_array->pdata[i])->type))
g_ptr_array_add(new_tags, tags_array->pdata[i]);
}
}
return new_tags;
}
/*
Completely frees an array of tags.
@param tags_array Array of tags to be freed.
@param free_array Whether the GptrArray is to be freed as well.
*/